CorbaOutboundConnectionCacheImpl.java revision 608:7e06bf1dcb09
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
26package com.sun.corba.se.impl.transport;
27
28import java.util.Collection;
29import java.util.Hashtable;
30import java.util.Iterator;
31
32import com.sun.corba.se.pept.broker.Broker;
33import com.sun.corba.se.pept.transport.ContactInfo;
34import com.sun.corba.se.pept.transport.Connection;
35import com.sun.corba.se.pept.transport.OutboundConnectionCache;
36
37import com.sun.corba.se.spi.monitoring.LongMonitoredAttributeBase;
38import com.sun.corba.se.spi.monitoring.MonitoringConstants;
39import com.sun.corba.se.spi.monitoring.MonitoringFactories;
40import com.sun.corba.se.spi.monitoring.MonitoredObject;
41import com.sun.corba.se.spi.orb.ORB;
42import com.sun.corba.se.spi.transport.CorbaConnectionCache;
43import com.sun.corba.se.spi.transport.CorbaContactInfo;
44
45import com.sun.corba.se.impl.orbutil.ORBUtility;
46
47/**
48 * @author Harold Carr
49 */
50public class CorbaOutboundConnectionCacheImpl
51    extends
52        CorbaConnectionCacheBase
53    implements
54        OutboundConnectionCache
55{
56    protected Hashtable connectionCache;
57
58    public CorbaOutboundConnectionCacheImpl(ORB orb, ContactInfo contactInfo)
59    {
60        super(orb, contactInfo.getConnectionCacheType(),
61              ((CorbaContactInfo)contactInfo).getMonitoringName());
62        this.connectionCache = new Hashtable();
63    }
64
65    ////////////////////////////////////////////////////
66    //
67    // pept.transport.OutboundConnectionCache
68    //
69
70    public Connection get(ContactInfo contactInfo)
71    {
72        if (orb.transportDebugFlag) {
73            dprint(".get: " + contactInfo + " " + contactInfo.hashCode());
74        }
75        synchronized (backingStore()) {
76            dprintStatistics();
77            return (Connection) connectionCache.get(contactInfo);
78        }
79    }
80
81    public void put(ContactInfo contactInfo, Connection connection)
82    {
83        if (orb.transportDebugFlag) {
84            dprint(".put: " + contactInfo + " " + contactInfo.hashCode() + " "
85                   + connection);
86        }
87        synchronized (backingStore()) {
88            connectionCache.put(contactInfo, connection);
89            connection.setConnectionCache(this);
90            dprintStatistics();
91        }
92    }
93
94    public void remove(ContactInfo contactInfo)
95    {
96        if (orb.transportDebugFlag) {
97            dprint(".remove: " + contactInfo + " " + contactInfo.hashCode());
98        }
99        synchronized (backingStore()) {
100            if (contactInfo != null) {
101                connectionCache.remove(contactInfo);
102            }
103            dprintStatistics();
104        }
105    }
106
107    ////////////////////////////////////////////////////
108    //
109    // Implementation
110    //
111
112    public Collection values()
113    {
114        return connectionCache.values();
115    }
116
117    protected Object backingStore()
118    {
119        return connectionCache;
120    }
121
122    protected void registerWithMonitoring()
123    {
124        // ORB
125        MonitoredObject orbMO =
126            orb.getMonitoringManager().getRootMonitoredObject();
127
128        // CONNECTION
129        MonitoredObject connectionMO =
130            orbMO.getChild(MonitoringConstants.CONNECTION_MONITORING_ROOT);
131        if (connectionMO == null) {
132            connectionMO =
133                MonitoringFactories.getMonitoredObjectFactory()
134                    .createMonitoredObject(
135                        MonitoringConstants.CONNECTION_MONITORING_ROOT,
136                        MonitoringConstants.CONNECTION_MONITORING_ROOT_DESCRIPTION);
137            orbMO.addChild(connectionMO);
138        }
139
140        // OUTBOUND CONNECTION
141        MonitoredObject outboundConnectionMO =
142            connectionMO.getChild(
143                MonitoringConstants.OUTBOUND_CONNECTION_MONITORING_ROOT);
144        if (outboundConnectionMO == null) {
145            outboundConnectionMO =
146                MonitoringFactories.getMonitoredObjectFactory()
147                    .createMonitoredObject(
148                        MonitoringConstants.OUTBOUND_CONNECTION_MONITORING_ROOT,
149                        MonitoringConstants.OUTBOUND_CONNECTION_MONITORING_ROOT_DESCRIPTION);
150            connectionMO.addChild(outboundConnectionMO);
151        }
152
153        // NODE FOR THIS CACHE
154        MonitoredObject thisMO =
155            outboundConnectionMO.getChild(getMonitoringName());
156        if (thisMO == null) {
157            thisMO =
158                MonitoringFactories.getMonitoredObjectFactory()
159                    .createMonitoredObject(
160                        getMonitoringName(),
161                        MonitoringConstants.CONNECTION_MONITORING_DESCRIPTION);
162            outboundConnectionMO.addChild(thisMO);
163        }
164
165        LongMonitoredAttributeBase attribute;
166
167        // ATTRIBUTE
168        attribute = new
169            LongMonitoredAttributeBase(
170                MonitoringConstants.CONNECTION_TOTAL_NUMBER_OF_CONNECTIONS,
171                MonitoringConstants.CONNECTION_TOTAL_NUMBER_OF_CONNECTIONS_DESCRIPTION)
172            {
173                public Object getValue() {
174                    return new Long(CorbaOutboundConnectionCacheImpl.this.numberOfConnections());
175                }
176            };
177        thisMO.addAttribute(attribute);
178
179        // ATTRIBUTE
180        attribute = new
181            LongMonitoredAttributeBase(
182                MonitoringConstants.CONNECTION_NUMBER_OF_IDLE_CONNECTIONS,
183                MonitoringConstants.CONNECTION_NUMBER_OF_IDLE_CONNECTIONS_DESCRIPTION)
184            {
185                public Object getValue() {
186                    return new Long(CorbaOutboundConnectionCacheImpl.this.numberOfIdleConnections());
187                }
188            };
189        thisMO.addAttribute(attribute);
190
191        // ATTRIBUTE
192        attribute = new
193            LongMonitoredAttributeBase(
194                MonitoringConstants.CONNECTION_NUMBER_OF_BUSY_CONNECTIONS,
195                MonitoringConstants.CONNECTION_NUMBER_OF_BUSY_CONNECTIONS_DESCRIPTION)
196            {
197                public Object getValue() {
198                    return new Long(CorbaOutboundConnectionCacheImpl.this.numberOfBusyConnections());
199                }
200            };
201        thisMO.addAttribute(attribute);
202    }
203
204    protected void dprint(String msg)
205    {
206        ORBUtility.dprint("CorbaOutboundConnectionCacheImpl", msg);
207    }
208}
209
210// End of file.
211