001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020: package org.jacorb.orb.giop;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
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.util.ObjectUtil;
032:
033: /**
034: * @author Nicolas Noffke
035: * @version $Id: GIOPConnectionManager.java,v 1.14 2006/06/28 12:41:43 alphonse.bendt Exp $
036: */
037:
038: public class GIOPConnectionManager implements Configurable {
039: /** the configuration object */
040: private org.jacorb.config.Configuration configuration = null;
041:
042: /** configuration properties */
043: private Logger logger = null;
044:
045: //private List client_giop_connections = null;
046: private final List server_giop_connections;
047: private int max_server_giop_connections = 0;
048: private Class statistics_provider_class = null;
049: private SelectionStrategy selection_strategy = null;
050: private int wait_for_idle_interval = 0;
051:
052: public GIOPConnectionManager() {
053: server_giop_connections = new LinkedList();
054: }
055:
056: /**
057: * configures the GIOPConnectionManager
058: */
059:
060: public void configure(Configuration myConfiguration)
061: throws ConfigurationException {
062: this .configuration = (org.jacorb.config.Configuration) myConfiguration;
063: logger = configuration.getNamedLogger("jacorb.orb.giop.conn");
064:
065: max_server_giop_connections = configuration
066: .getAttributeAsInteger(
067: "jacorb.connection.max_server_connections",
068: Integer.MAX_VALUE);
069:
070: selection_strategy = (SelectionStrategy) configuration
071: .getAttributeAsObject("jacorb.connection.selection_strategy_class");
072:
073: wait_for_idle_interval = configuration.getAttributeAsInteger(
074: "jacorb.connection.wait_for_idle_interval", 500);
075:
076: String statisticsProviderClassName = configuration
077: .getAttribute(
078: "jacorb.connection.statistics_provider_class",
079: "");
080:
081: if (statisticsProviderClassName.length() > 0) {
082: try {
083: statistics_provider_class = ObjectUtil
084: .classForName(statisticsProviderClassName);
085: } catch (Exception e) {
086: if (logger.isErrorEnabled()) {
087: logger
088: .error("Unable to create class from property >jacorb.connection.statistics_provider_class<: "
089: + e.toString());
090: }
091: }
092: }
093:
094: }
095:
096: public ServerGIOPConnection createServerGIOPConnection(
097: org.omg.ETF.Profile profile,
098: org.omg.ETF.Connection transport,
099: RequestListener request_listener,
100: ReplyListener reply_listener) {
101: //if too many open connections, shut one down
102: if (server_giop_connections.size() >= max_server_giop_connections) {
103: if (selection_strategy == null) {
104: if (logger.isErrorEnabled()) {
105: logger
106: .error("No. of max server giop connections set, but no SelectionStrategy present");
107: }
108: } else {
109: while (server_giop_connections.size() >= max_server_giop_connections) {
110: ServerGIOPConnection to_close = null;
111:
112: synchronized (server_giop_connections) {
113: to_close = selection_strategy
114: .selectForClose(server_giop_connections);
115: }
116:
117: if (to_close != null && to_close.tryClose()) {
118: break;
119: }
120:
121: try {
122: Thread.sleep(wait_for_idle_interval);
123: } catch (InterruptedException e) {
124: // ignored
125: }
126: }
127: }
128: }
129:
130: ServerGIOPConnection connection = new ServerGIOPConnection(
131: profile, transport, request_listener, reply_listener,
132: getStatisticsProvider(), this );
133:
134: try {
135: connection.configure(configuration);
136: } catch (ConfigurationException ce) {
137: logger.warn("ConfigurationException", ce);
138: }
139:
140: synchronized (server_giop_connections) {
141: server_giop_connections.add(connection);
142: }
143:
144: if (logger.isDebugEnabled()) {
145: logger.debug("GIOPConnectionManager: created new "
146: + connection.toString());
147: }
148:
149: return connection;
150: }
151:
152: public void unregisterServerGIOPConnection(
153: ServerGIOPConnection connection) {
154: synchronized (server_giop_connections) {
155: server_giop_connections.remove(connection);
156: }
157: }
158:
159: public GIOPConnection createClientGIOPConnection(
160: org.omg.ETF.Profile profile,
161: org.omg.ETF.Connection transport,
162: RequestListener request_listener,
163: ReplyListener reply_listener) {
164: ClientGIOPConnection connection = new ClientGIOPConnection(
165: profile, transport, request_listener, reply_listener,
166: null);
167:
168: try {
169: connection.configure(configuration);
170: } catch (ConfigurationException ce) {
171: logger.warn("ConfigurationException", ce);
172: }
173: return connection;
174: }
175:
176: /**
177: * Closes all server-side GIOP connections.
178: */
179: public void shutdown() {
180: List connections = null;
181: synchronized (server_giop_connections) {
182: connections = new ArrayList(server_giop_connections);
183: }
184:
185: if (logger.isDebugEnabled()) {
186: logger.debug("GIOPConnectionManager.shutdown(), "
187: + connections.size() + " connections");
188: }
189:
190: for (Iterator i = connections.iterator(); i.hasNext();) {
191: ServerGIOPConnection connection = (ServerGIOPConnection) i
192: .next();
193: connection.tryClose();
194: }
195:
196: }
197:
198: private StatisticsProvider getStatisticsProvider() {
199: StatisticsProvider result = null;
200: if (statistics_provider_class != null) {
201: try {
202: result = (StatisticsProvider) statistics_provider_class
203: .newInstance();
204: } catch (Exception e) {
205: if (logger.isErrorEnabled()) {
206: logger
207: .error("Unable to create instance from Class >"
208: + statistics_provider_class + '<');
209: }
210: }
211: }
212: return result;
213: }
214:
215: }// GIOPConnectionManager
|