KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > giop > TransportManager


1 package org.jacorb.orb.giop;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.io.*;
24 import java.net.*;
25 import java.util.*;
26 import java.lang.reflect.Constructor JavaDoc;
27
28 import org.apache.avalon.framework.logger.Logger;
29 import org.apache.avalon.framework.configuration.*;
30
31 import org.omg.ETF.*;
32
33 import org.jacorb.orb.*;
34 import org.jacorb.orb.factory.*;
35 import org.jacorb.orb.iiop.*;
36 import org.jacorb.util.ObjectUtil;
37
38 /**
39  * This class manages Transports. On the one hand it creates them, and
40  * on the other it enforces an upper limit on the open transports.
41  *
42  * @author Nicolas Noffke
43  * @version $Id: TransportManager.java,v 1.20 2004/05/06 12:40:00 nicolas Exp $
44  * */

45
46 public class TransportManager
47     implements Configurable
48 {
49     private SocketFactory socket_factory = null;
50     private SocketFactory ssl_socket_factory = null;
51
52     private ORB orb = null;
53
54     /** the configuration object */
55     private org.jacorb.config.Configuration configuration = null;
56
57     /** configuration properties */
58     private Logger logger = null;
59     private List factoryClassNames = null;
60     private ProfileSelector profileSelector = null;
61     private SocketFactoryManager socketFactoryManager = null;
62
63     /**
64      * Maps ETF Profile tags (Integer) to ETF Factories objects.
65      */

66     private Map factoriesMap = null;
67
68     /**
69      * List of all installed ETF Factories. This list contains an
70      * instance of each Factories class, ordered in the same way as
71      * they were specified in the jacorb.transport.factories property.
72      */

73     private List factoriesList = null;
74
75     public TransportManager( ORB orb )
76     {
77         this.orb = orb;
78         socketFactoryManager = new SocketFactoryManager(orb);
79     }
80
81     public void configure(Configuration myConfiguration)
82         throws ConfigurationException
83     {
84         this.configuration = (org.jacorb.config.Configuration)myConfiguration;
85         logger =
86             configuration.getNamedLogger("jacorb.orb.giop");
87         socketFactoryManager.configure(configuration);
88
89         // get factory class names
90
factoryClassNames =
91             this.configuration.getAttributeList("jacorb.transport.factories");
92
93         if (factoryClassNames.isEmpty())
94             factoryClassNames.add("org.jacorb.orb.iiop.IIOPFactories");
95
96         // get profile selector info
97
profileSelector =
98             (ProfileSelector)configuration.getAttributeAsObject("jacorb.transport.client.selector");
99
100         if (profileSelector == null)
101         {
102             profileSelector = new DefaultProfileSelector();
103         }
104
105         if( configuration.getAttribute("jacorb.security.support_ssl","off").equals("on"))
106         {
107             String JavaDoc s = configuration.getAttribute("jacorb.ssl.socket_factory", "");
108             if (s.length() == 0)
109             {
110                 throw new RuntimeException JavaDoc( "SSL support is on, but the property \"jacorb.ssl.socket_factory\" is not set!" );
111             }
112
113             try
114             {
115                 Class JavaDoc ssl = ObjectUtil.classForName(s);
116
117                 Constructor JavaDoc constr =
118                     ssl.getConstructor( new Class JavaDoc[]{ ORB.class });
119
120                 ssl_socket_factory =
121                     (SocketFactory)constr.newInstance( new Object JavaDoc[]{ orb });
122             }
123             catch (Exception JavaDoc e)
124             {
125
126                 if (logger.isErrorEnabled())
127                     logger.error(e.getMessage());
128
129                 throw new RuntimeException JavaDoc( "SSL support is on, but the ssl socket factory can't be instantiated ("+ e.getMessage()+")!" );
130             }
131         }
132
133         socket_factory = socketFactoryManager.getSocketFactory();
134     }
135
136     public ProfileSelector getProfileSelector()
137     {
138         return profileSelector;
139     }
140
141     public SocketFactoryManager getSocketFactoryManager()
142     {
143         return socketFactoryManager;
144     }
145
146     public SocketFactory getSocketFactory()
147     {
148         return socket_factory;
149     }
150
151     public SocketFactory getSSLSocketFactory()
152     {
153         return ssl_socket_factory;
154     }
155
156
157     /**
158      * Returns an ETF Factories object for the given tag, or null
159      * if no Factories class has been defined for this tag.
160      */

161     public org.omg.ETF.Factories getFactories(int tag)
162     {
163         if (factoriesMap == null)
164         {
165             loadFactories();
166         }
167         return (Factories)factoriesMap.get (new Integer JavaDoc (tag));
168     }
169
170     /**
171      * Returns a list of Factories for all configured transport plugins,
172      * in the same order as they were specified in the
173      * jacorb.transport.factories property.
174      */

175     public List getFactoriesList()
176     {
177         if (factoriesList == null)
178         {
179             loadFactories();
180         }
181         return Collections.unmodifiableList(factoriesList);
182     }
183
184     /**
185      * Build the factoriesMap and factoriesList.
186      */

187     private void loadFactories()
188     {
189         if (configuration == null )
190             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc("TransportManager not configured!");
191
192         if (factoryClassNames == null )
193             throw new org.omg.CORBA.INTERNAL JavaDoc("factoryClassNames should not be null");
194
195         factoriesMap = new HashMap();
196         factoriesList = new ArrayList();
197
198         for (Iterator i = factoryClassNames.iterator(); i.hasNext();)
199         {
200             String JavaDoc className = (String JavaDoc)i.next();
201             Factories f = instantiateFactories(className);
202             factoriesMap.put(new Integer JavaDoc(f.profile_tag()), f);
203             factoriesList.add (f);
204         }
205     }
206
207     /**
208      * Instantiates the given Factories class.
209      */

210     private org.omg.ETF.Factories instantiateFactories (String JavaDoc className)
211     {
212         try
213         {
214             // ObjectUtil.classForName() uses the context class loader.
215
// This is important here because JacORB might be on the
216
// bootclasspath, and the external transport on the normal
217
// classpath.
218
Class JavaDoc c = ObjectUtil.classForName(className);
219             Configurable configurable = (Configurable)c.newInstance();
220             configurable.configure(configuration);
221             return (Factories)configurable;
222         }
223         catch (Exception JavaDoc e)
224         {
225             throw new RuntimeException JavaDoc
226                 ("could not instantiate Factories class " + className
227                  + ", exception: " + e);
228         }
229     }
230
231 }
232
Popular Tags