001: package org.jacorb.orb.giop;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.configuration.Configurable;
028: import org.apache.avalon.framework.configuration.Configuration;
029: import org.apache.avalon.framework.configuration.ConfigurationException;
030: import org.apache.avalon.framework.logger.Logger;
031: import org.jacorb.orb.ORB;
032: import org.omg.CORBA.BAD_PARAM;
033: import org.omg.ETF.Factories;
034:
035: /**
036: * This class manages connections.
037: *
038: * @author Gerald Brose, FU Berlin
039: * @version $Id: ClientConnectionManager.java,v 1.32 2006/07/26 07:38:56 alphonse.bendt Exp $
040: */
041:
042: public class ClientConnectionManager implements Configurable {
043: private final org.jacorb.orb.ORB orb;
044:
045: /** connection mgmt. */
046: private final Map connections = new HashMap();
047:
048: private RequestListener request_listener;
049:
050: private MessageReceptorPool receptor_pool;
051:
052: private final TransportManager transport_manager;
053: private final GIOPConnectionManager giop_connection_manager;
054:
055: /** the configuration object */
056: private Logger logger;
057:
058: public ClientConnectionManager(ORB orb,
059: TransportManager transport_manager,
060: GIOPConnectionManager giop_connection_manager) {
061: this .orb = orb;
062: this .transport_manager = transport_manager;
063: this .giop_connection_manager = giop_connection_manager;
064: }
065:
066: /**
067: * configure this connection manager
068: */
069:
070: public void configure(Configuration myConfiguration)
071: throws ConfigurationException {
072: // Moved from the constructor to facilitate logging.
073: receptor_pool = new MessageReceptorPool("client",
074: "ClientMessageReceptor", myConfiguration);
075:
076: org.jacorb.config.Configuration configuration = (org.jacorb.config.Configuration) myConfiguration;
077: logger = configuration.getNamedLogger("jacorb.orb.giop");
078:
079: request_listener = new NoBiDirClientRequestListener(logger);
080: }
081:
082: public void setRequestListener(RequestListener listener) {
083: request_listener = listener;
084: }
085:
086: public synchronized ClientConnection getConnection(
087: org.omg.ETF.Profile profile) {
088: /* look for an existing connection */
089:
090: ClientConnection clientConnection = (ClientConnection) connections
091: .get(profile);
092:
093: if (clientConnection == null) {
094: int tag = profile.tag();
095: Factories factories = transport_manager.getFactories(tag);
096: if (factories == null) {
097: throw new BAD_PARAM(
098: "No transport plugin for profile tag " + tag);
099: }
100: GIOPConnection connection = giop_connection_manager
101: .createClientGIOPConnection(profile, factories
102: .create_connection(null), request_listener,
103: null);
104:
105: clientConnection = new ClientConnection(connection, orb,
106: this , profile, true);
107:
108: if (logger.isInfoEnabled()) {
109: logger.info("ClientConnectionManager: created new "
110: + clientConnection.getGIOPConnection()
111: .toString());
112: }
113:
114: receptor_pool.connectionCreated(connection);
115: connections.put(profile, clientConnection);
116: } else {
117: if (logger.isInfoEnabled()) {
118: logger.info("ClientConnectionManager: found "
119: + clientConnection.getGIOPConnection()
120: .toString());
121: }
122: }
123:
124: clientConnection.incClients();
125:
126: return clientConnection;
127: }
128:
129: /**
130: * Only used by Delegate for client-initiated connections.
131: */
132: public synchronized void releaseConnection(
133: ClientConnection connection) {
134: if (connection.decClients()) {
135: if (logger.isDebugEnabled()) {
136: logger.debug("ClientConnectionManager: releasing "
137: + connection.getGIOPConnection().toString());
138: }
139: connection.close();
140: connections.remove(connection.getRegisteredProfile());
141: } else {
142: // not sure if this should be a warning or even an error
143: if (logger.isDebugEnabled()) {
144: logger.debug("ClientConnectionManager: cannot release "
145: + connection.getGIOPConnection().toString()
146: + " (still has " + connection.numClients()
147: + " client(s))");
148: }
149: }
150: }
151:
152: /**
153: * Only used by ClientConnection to unregister server-side of
154: * BiDir connection.
155: */
156: public synchronized void removeConnection(
157: ClientConnection connection) {
158: connections.remove(connection.getRegisteredProfile());
159: }
160:
161: public synchronized void addConnection(GIOPConnection connection,
162: org.omg.ETF.Profile profile) {
163: if (!connections.containsKey(profile)) {
164: ClientConnection clientConnection = new ClientConnection(
165: connection, orb, this , profile, false);
166:
167: //this is a bit of a hack: the bidirectional client
168: //connections have to persist until their underlying GIOP
169: //connection is closed. Therefore, we set the initial
170: //client count to 1, so the connection will be kept even
171: //if there are currently no associated Delegates.
172:
173: clientConnection.incClients();
174:
175: connections.put(profile, clientConnection);
176: }
177: }
178:
179: public synchronized void shutdown() {
180: /* release all open connections */
181:
182: for (Iterator i = connections.values().iterator(); i.hasNext();) {
183: ((ClientConnection) i.next()).close();
184: }
185:
186: if (logger.isDebugEnabled()) {
187: logger
188: .debug("ClientConnectionManager shut down (all connections released)");
189: }
190:
191: connections.clear();
192: receptor_pool.shutdown();
193: }
194: }
|