KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > factory > SocketFactoryManager


1 package org.jacorb.orb.factory;
2
3 /*
4  * Written for JacORB - a free Java ORB
5  *
6  * Copyright (C) 2000-2004 Nicolas Noffke, 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.net.*;
24 import java.lang.reflect.Constructor JavaDoc;
25
26 import org.apache.avalon.framework.logger.*;
27 import org.apache.avalon.framework.configuration.*;
28
29 import org.jacorb.orb.*;
30 import org.jacorb.util.ObjectUtil;
31
32 public class SocketFactoryManager
33     implements Configurable
34 {
35     // Properties used to define custom socket and server socket factories
36
private static final String JavaDoc FACTORY_PROP = "jacorb.net.socket_factory";
37     private static final String JavaDoc SERVER_FACTORY_PROP = "jacorb.net.server_socket_factory";
38
39     private SocketFactory socketFactory = null;
40     private ServerSocketFactory serverFactory = null;
41     private ORB orb;
42
43     /** the configuration object */
44     private Configuration configuration = null;
45     private Logger logger = null;
46     private String JavaDoc serverFactoryClassName = null;
47     private String JavaDoc factoryClassName = null;
48     private String JavaDoc portNo = null;
49
50     public SocketFactoryManager(ORB orb)
51     {
52         this.orb = orb;
53     }
54
55
56     public void configure(Configuration configuration)
57         throws ConfigurationException
58     {
59         this.configuration = configuration;
60         logger =
61             ((org.jacorb.config.Configuration)configuration).getNamedLogger("jacorb.orb.factory");
62         serverFactoryClassName = configuration.getAttribute(SERVER_FACTORY_PROP, "");
63         factoryClassName = configuration.getAttribute(FACTORY_PROP, "");
64         portNo = configuration.getAttribute(PortRangeSocketFactory.MIN_PROP, "");
65     }
66
67
68     public synchronized SocketFactory getSocketFactory()
69     {
70         if (socketFactory != null)
71         {
72            return socketFactory;
73         }
74
75         if ( factoryClassName.length() == 0)
76         {
77             if ( portNo.length() > 0)
78             {
79                 // If mimimum port number configured construct PortRangeSocketFactory
80
socketFactory = new PortRangeSocketFactory();
81             }
82             else
83             {
84                 // Construct default socket factory
85
socketFactory = new DefaultSocketFactory();
86             }
87         }
88         else
89         {
90             socketFactory = getFactory(factoryClassName);
91         }
92
93         return socketFactory;
94     }
95
96     public synchronized ServerSocketFactory getServerSocketFactory()
97     {
98         if (serverFactory != null)
99         {
100            return serverFactory;
101         }
102
103         if ((serverFactoryClassName == null) || (serverFactoryClassName.length() == 0))
104         {
105             // Construct default server socket factory
106
serverFactory = new DefaultServerSocketFactory();
107         }
108         else
109         {
110             serverFactory = getServerFactory(serverFactoryClassName);
111         }
112
113         return serverFactory;
114     }
115
116     private SocketFactory getFactory(String JavaDoc className)
117     {
118        java.lang.Object JavaDoc factory = getFactoryObject(orb, className);
119
120        if (factory instanceof SocketFactory)
121        {
122           if (factory instanceof Configurable)
123           {
124              try
125              {
126                 ((Configurable)factory).configure(configuration);
127              }
128              catch (ConfigurationException ce)
129              {
130                throw new RuntimeException JavaDoc("Configurable custom socket factory " +
131                                           className +
132                                           " could not be configured!: "+ ce);
133              }
134           }
135           return ((SocketFactory) factory);
136        }
137        else
138        {
139            throw new RuntimeException JavaDoc
140                ("Custom factory " + className + " does not implement SocketFactory");
141        }
142     }
143
144     private ServerSocketFactory getServerFactory(String JavaDoc className)
145     {
146        java.lang.Object JavaDoc factory = getFactoryObject (orb, className);
147
148        if (factory instanceof ServerSocketFactory)
149        {
150           if (factory instanceof Configurable)
151           {
152              try
153              {
154                 ((Configurable)factory).configure(configuration);
155              }
156              catch (ConfigurationException ce)
157              {
158                throw new RuntimeException JavaDoc("Configurable custom server socket factory " +
159                                           className +
160                                           " could not be configured!: " + ce);
161              }
162           }
163           return ((ServerSocketFactory) factory);
164        }
165        else
166        {
167            throw new RuntimeException JavaDoc("Custom factory " +
168                                       className +
169                                       " does not implement ServerSocketFactory");
170        }
171     }
172
173     private java.lang.Object JavaDoc getFactoryObject(ORB orb, String JavaDoc className)
174     {
175         Constructor JavaDoc ctor = null;
176         Class JavaDoc sfClass;
177         java.lang.Object JavaDoc factory;
178
179         try
180         {
181             sfClass = ObjectUtil.classForName(className);
182
183             if (orb != null)
184             {
185                 try
186                 {
187                     // First try getting constructor with ORB parameter
188
ctor = sfClass.getConstructor (new Class JavaDoc[] { ORB.class });
189                 }
190                 catch (Exception JavaDoc ex)
191                 {
192                     // Ignore
193
}
194             }
195
196             if (ctor != null)
197             {
198                 // Construct passing ORB as parameter
199
factory = ctor.newInstance (new Object JavaDoc[] { orb });
200             }
201             else
202             {
203                 // Default construction
204
factory = sfClass.newInstance();
205             }
206         }
207         catch (Exception JavaDoc ex)
208         {
209             if (logger.isDebugEnabled())
210             {
211                 logger.debug(ex.getMessage());
212             }
213             throw new RuntimeException JavaDoc("Failed to create custom socket factory: " +
214                                        className);
215         }
216
217         if (logger.isDebugEnabled())
218         {
219             logger.debug("SocketFactoryManager: created " +
220                          factory.getClass().getName());
221         }
222
223         return factory;
224     }
225 }
226
Popular Tags