1 45 46 package org.exolab.jms.server; 47 48 import java.lang.reflect.Constructor ; 49 import javax.naming.NamingException ; 50 import javax.naming.Context ; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 55 import org.exolab.jms.config.Configuration; 56 import org.exolab.jms.config.Connector; 57 import org.exolab.jms.config.ConnectorResource; 58 import org.exolab.jms.config.ConnectorHelper; 59 import org.exolab.jms.config.types.SchemeType; 60 import org.exolab.jms.service.Service; 61 import org.exolab.jms.service.ServiceException; 62 63 64 70 class ConnectorService extends Service { 71 72 75 private final Configuration _config; 76 77 81 private ServerConnector[] _interfaces = null; 82 83 86 private static final Log _log = LogFactory.getLog(ConnectorService.class); 87 88 89 94 public ConnectorService(Configuration config) { 95 super("ConnectorService"); 96 _config = config; 97 } 98 99 105 public synchronized void start() throws ServiceException { 106 super.start(); 107 try { 108 Context context = NamingHelper.getInitialContext(_config); 109 initConnectors(context); 110 } catch (NamingException exception) { 111 throw new ServiceException(exception.getMessage(), exception); 112 } 113 } 114 115 121 public synchronized void stop() throws ServiceException { 122 super.stop(); 123 for (int i = 0; i < _interfaces.length; ++i) { 124 _interfaces[i].close(); 125 } 126 } 127 128 135 protected void initConnectors(Context context) 136 throws NamingException , ServiceException { 137 138 Connector[] connectors = _config.getConnectors().getConnector(); 139 _interfaces = new ServerConnector[connectors.length]; 140 141 for (int i = 0; i < connectors.length; ++i) { 142 Connector connector = connectors[i]; 143 _interfaces[i] = initConnector(connector, context); 144 } 145 } 146 147 156 protected ServerConnector initConnector(Connector connector, Context context) 157 throws NamingException , ServiceException { 158 159 _log.info("Creating server interface for the " + connector.getScheme() 160 + " connector"); 161 162 ServerConnector server; 163 ConnectorResource resource = ConnectorHelper.getConnectorResource( 164 connector.getScheme()); 165 166 String className = resource.getServer().getImplementationClass(); 167 Class clazz; 168 try { 169 clazz = Class.forName(className); 170 } catch (ClassNotFoundException exception) { 171 throw new ServiceException( 172 "Failed to load class " + className); 173 } 174 175 if (!ServerConnector.class.isAssignableFrom(clazz)) { 176 throw new ServiceException( 177 "Class " + className + " does not implement ServerConnector"); 178 } 179 try { 180 SchemeType scheme = connector.getScheme(); 181 Constructor ctor = clazz.getConstructor( 182 new Class []{SchemeType.class, Configuration.class}); 183 server = (ServerConnector) ctor.newInstance( 184 new Object []{scheme, _config}); 185 } catch (NoSuchMethodError ignore) { 186 try { 188 server = (ServerConnector) clazz.newInstance(); 189 } catch (Exception exception) { 190 throw new ServiceException( 191 exception.getMessage(), exception); 192 } 193 } catch (Exception exception) { 194 throw new ServiceException( 195 exception.getMessage(), exception); 196 } 197 198 _log.debug("Created an instance of " + className 199 + " as a server interface"); 200 201 server.init(); 203 204 server.bindConnectionFactories(context); 206 207 return server; 208 } 209 210 211 212 } 213 | Popular Tags |