1 /*
   2  * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.transport;
  27 
  28 import java.util.ArrayList;
  29 import java.util.Collection;
  30 
  31 import com.sun.corba.se.pept.broker.Broker;
  32 import com.sun.corba.se.pept.transport.Acceptor;
  33 import com.sun.corba.se.pept.transport.Connection;
  34 import com.sun.corba.se.pept.transport.InboundConnectionCache;
  35 
  36 import com.sun.corba.se.spi.monitoring.LongMonitoredAttributeBase;
  37 import com.sun.corba.se.spi.monitoring.MonitoringConstants;
  38 import com.sun.corba.se.spi.monitoring.MonitoringFactories;
  39 import com.sun.corba.se.spi.monitoring.MonitoredObject;
  40 import com.sun.corba.se.spi.orb.ORB;
  41 import com.sun.corba.se.spi.transport.CorbaConnectionCache;
  42 import com.sun.corba.se.spi.transport.CorbaAcceptor;
  43 
  44 import com.sun.corba.se.impl.orbutil.ORBUtility;
  45 
  46 /**
  47  * @author Harold Carr
  48  */
  49 public class CorbaInboundConnectionCacheImpl
  50     extends
  51         CorbaConnectionCacheBase
  52     implements
  53         InboundConnectionCache
  54 {
  55     protected Collection connectionCache;
  56 
  57     private Acceptor acceptor;
  58 
  59     public CorbaInboundConnectionCacheImpl(ORB orb, Acceptor acceptor)
  60     {
  61         super(orb, acceptor.getConnectionCacheType(),
  62               ((CorbaAcceptor)acceptor).getMonitoringName());
  63         this.connectionCache = new ArrayList();
  64         this.acceptor = acceptor;
  65         if (orb.transportDebugFlag) {
  66             dprint(": " + acceptor );
  67         }
  68     }
  69 
  70     ////////////////////////////////////////////////////
  71     //
  72     // pept.transport.InboundConnectionCache
  73     //
  74 
  75     public void close () {
  76 
  77         super.close();
  78         if (orb.transportDebugFlag) {
  79             dprint(".close: " + acceptor );
  80         }
  81         this.acceptor.close();
  82 
  83     }
  84 
  85     public Connection get(Acceptor acceptor)
  86     {
  87         throw wrapper.methodShouldNotBeCalled();
  88     }
  89 
  90     public Acceptor getAcceptor () {
  91         return acceptor;
  92     }
  93 
  94     public void put(Acceptor acceptor, Connection connection)
  95     {
  96         if (orb.transportDebugFlag) {
  97             dprint(".put: " + acceptor + " " + connection);
  98         }
  99         synchronized (backingStore()) {
 100             connectionCache.add(connection);
 101             connection.setConnectionCache(this);
 102             dprintStatistics();
 103         }
 104     }
 105 
 106     public void remove(Connection connection)
 107     {
 108         if (orb.transportDebugFlag) {
 109             dprint(".remove: " +  connection);
 110         }
 111         synchronized (backingStore()) {
 112             connectionCache.remove(connection);
 113             dprintStatistics();
 114         }
 115     }
 116 
 117     ////////////////////////////////////////////////////
 118     //
 119     // Implementation
 120     //
 121 
 122     public Collection values()
 123     {
 124         return connectionCache;
 125     }
 126 
 127     protected Object backingStore()
 128     {
 129         return connectionCache;
 130     }
 131 
 132     protected void registerWithMonitoring()
 133     {
 134         // ORB
 135         MonitoredObject orbMO =
 136             orb.getMonitoringManager().getRootMonitoredObject();
 137 
 138         // REVISIT - add ORBUtil mkdir -p like operation for this.
 139 
 140         // CONNECTION
 141         MonitoredObject connectionMO =
 142             orbMO.getChild(MonitoringConstants.CONNECTION_MONITORING_ROOT);
 143         if (connectionMO == null) {
 144             connectionMO =
 145                 MonitoringFactories.getMonitoredObjectFactory()
 146                     .createMonitoredObject(
 147                         MonitoringConstants.CONNECTION_MONITORING_ROOT,
 148                         MonitoringConstants.CONNECTION_MONITORING_ROOT_DESCRIPTION);
 149             orbMO.addChild(connectionMO);
 150         }
 151 
 152         // INBOUND CONNECTION
 153         MonitoredObject inboundConnectionMO =
 154             connectionMO.getChild(
 155                 MonitoringConstants.INBOUND_CONNECTION_MONITORING_ROOT);
 156         if (inboundConnectionMO == null) {
 157             inboundConnectionMO =
 158                 MonitoringFactories.getMonitoredObjectFactory()
 159                     .createMonitoredObject(
 160                         MonitoringConstants.INBOUND_CONNECTION_MONITORING_ROOT,
 161                         MonitoringConstants.INBOUND_CONNECTION_MONITORING_ROOT_DESCRIPTION);
 162             connectionMO.addChild(inboundConnectionMO);
 163         }
 164 
 165         // NODE FOR THIS CACHE
 166         MonitoredObject thisMO =
 167             inboundConnectionMO.getChild(getMonitoringName());
 168         if (thisMO == null) {
 169             thisMO =
 170                 MonitoringFactories.getMonitoredObjectFactory()
 171                     .createMonitoredObject(
 172                         getMonitoringName(),
 173                         MonitoringConstants.CONNECTION_MONITORING_DESCRIPTION);
 174             inboundConnectionMO.addChild(thisMO);
 175         }
 176 
 177         LongMonitoredAttributeBase attribute;
 178 
 179         // ATTRIBUTE
 180         attribute = new
 181             LongMonitoredAttributeBase(
 182                 MonitoringConstants.CONNECTION_TOTAL_NUMBER_OF_CONNECTIONS,
 183                 MonitoringConstants.CONNECTION_TOTAL_NUMBER_OF_CONNECTIONS_DESCRIPTION)
 184             {
 185                 public Object getValue() {
 186                     return new Long(CorbaInboundConnectionCacheImpl.this.numberOfConnections());
 187                 }
 188             };
 189         thisMO.addAttribute(attribute);
 190 
 191         // ATTRIBUTE
 192         attribute = new
 193             LongMonitoredAttributeBase(
 194                 MonitoringConstants.CONNECTION_NUMBER_OF_IDLE_CONNECTIONS,
 195                 MonitoringConstants.CONNECTION_NUMBER_OF_IDLE_CONNECTIONS_DESCRIPTION)
 196             {
 197                 public Object getValue() {
 198                     return new Long(CorbaInboundConnectionCacheImpl.this.numberOfIdleConnections());
 199                 }
 200             };
 201         thisMO.addAttribute(attribute);
 202 
 203         // ATTRIBUTE
 204         attribute = new
 205             LongMonitoredAttributeBase(
 206                 MonitoringConstants.CONNECTION_NUMBER_OF_BUSY_CONNECTIONS,
 207                 MonitoringConstants.CONNECTION_NUMBER_OF_BUSY_CONNECTIONS_DESCRIPTION)
 208             {
 209                 public Object getValue() {
 210                     return new Long(CorbaInboundConnectionCacheImpl.this.numberOfBusyConnections());
 211                 }
 212             };
 213         thisMO.addAttribute(attribute);
 214     }
 215 
 216     protected void dprint(String msg)
 217     {
 218         ORBUtility.dprint("CorbaInboundConnectionCacheImpl", msg);
 219     }
 220 }
 221 
 222 // End of file.