1 package org.jacorb.orb.factory; 2 3 22 23 import java.net.*; 24 import java.lang.reflect.Constructor ; 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 private static final String FACTORY_PROP = "jacorb.net.socket_factory"; 37 private static final String 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 44 private Configuration configuration = null; 45 private Logger logger = null; 46 private String serverFactoryClassName = null; 47 private String factoryClassName = null; 48 private String 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 socketFactory = new PortRangeSocketFactory(); 81 } 82 else 83 { 84 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 serverFactory = new DefaultServerSocketFactory(); 107 } 108 else 109 { 110 serverFactory = getServerFactory(serverFactoryClassName); 111 } 112 113 return serverFactory; 114 } 115 116 private SocketFactory getFactory(String className) 117 { 118 java.lang.Object 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 ("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 140 ("Custom factory " + className + " does not implement SocketFactory"); 141 } 142 } 143 144 private ServerSocketFactory getServerFactory(String className) 145 { 146 java.lang.Object 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 ("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 ("Custom factory " + 168 className + 169 " does not implement ServerSocketFactory"); 170 } 171 } 172 173 private java.lang.Object getFactoryObject(ORB orb, String className) 174 { 175 Constructor ctor = null; 176 Class sfClass; 177 java.lang.Object factory; 178 179 try 180 { 181 sfClass = ObjectUtil.classForName(className); 182 183 if (orb != null) 184 { 185 try 186 { 187 ctor = sfClass.getConstructor (new Class [] { ORB.class }); 189 } 190 catch (Exception ex) 191 { 192 } 194 } 195 196 if (ctor != null) 197 { 198 factory = ctor.newInstance (new Object [] { orb }); 200 } 201 else 202 { 203 factory = sfClass.newInstance(); 205 } 206 } 207 catch (Exception ex) 208 { 209 if (logger.isDebugEnabled()) 210 { 211 logger.debug(ex.getMessage()); 212 } 213 throw new RuntimeException ("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 |