1 17 18 package org.apache.avalon.cornerstone.blocks.sockets; 19 20 import java.util.HashMap ; 21 import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory; 22 import org.apache.avalon.cornerstone.services.sockets.SocketFactory; 23 import org.apache.avalon.cornerstone.services.sockets.SocketManager; 24 import org.apache.avalon.framework.CascadingException; 25 import org.apache.avalon.framework.activity.Initializable; 26 import org.apache.avalon.framework.configuration.Configurable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.container.ContainerUtil; 30 import org.apache.avalon.framework.context.Context; 31 import org.apache.avalon.framework.context.Contextualizable; 32 import org.apache.avalon.framework.logger.AbstractLogEnabled; 33 34 41 public class DefaultSocketManager 42 extends AbstractLogEnabled 43 implements SocketManager, Contextualizable, Configurable, Initializable 44 { 45 protected final HashMap m_serverSockets = new HashMap (); 46 protected final HashMap m_sockets = new HashMap (); 47 48 protected Context m_context; 49 protected Configuration m_configuration; 50 51 56 public void contextualize( final Context context ) 57 { 58 m_context = context; 59 } 60 61 68 public void configure( final Configuration configuration ) 69 throws ConfigurationException 70 { 71 m_configuration = configuration; 72 } 73 74 public void initialize() 75 throws Exception 76 { 77 final Configuration[] serverSockets = 78 m_configuration.getChild( "server-sockets" ).getChildren( "factory" ); 79 80 for( int i = 0; i < serverSockets.length; i++ ) 81 { 82 final Configuration element = serverSockets[ i ]; 83 final String name = element.getAttribute( "name" ); 84 final String className = element.getAttribute( "class" ); 85 86 setupServerSocketFactory( name, className, element ); 87 } 88 89 final Configuration[] clientSockets = 90 m_configuration.getChild( "client-sockets" ).getChildren( "factory" ); 91 92 for( int i = 0; i < clientSockets.length; i++ ) 93 { 94 final Configuration element = clientSockets[ i ]; 95 final String name = element.getAttribute( "name" ); 96 final String className = element.getAttribute( "class" ); 97 98 setupClientSocketFactory( name, className, element ); 99 } 100 } 101 102 protected void setupServerSocketFactory( final String name, 103 final String className, 104 final Configuration configuration ) 105 throws Exception 106 { 107 final Object object = createFactory( name, className, configuration ); 108 109 if( !( object instanceof ServerSocketFactory ) ) 110 { 111 throw new Exception ( "Error creating factory " + name + 112 " with class " + className + " as " + 113 "it does not implement the correct " + 114 "interface (ServerSocketFactory)" ); 115 } 116 117 m_serverSockets.put( name, object ); 118 } 119 120 protected void setupClientSocketFactory( final String name, 121 final String className, 122 final Configuration configuration ) 123 throws Exception 124 { 125 final Object object = createFactory( name, className, configuration ); 126 127 if( !( object instanceof SocketFactory ) ) 128 { 129 throw new Exception ( "Error creating factory " + name + 130 " with class " + className + " as " + 131 "it does not implement the correct " + 132 "interface (SocketFactory)" ); 133 } 134 135 m_sockets.put( name, object ); 136 } 137 138 protected Object createFactory( final String name, 139 final String className, 140 final Configuration configuration ) 141 throws Exception 142 { 143 Object factory = null; 144 145 try 146 { 147 final ClassLoader classLoader = 148 Thread.currentThread().getContextClassLoader(); 149 factory = classLoader.loadClass( className ).newInstance(); 150 } 151 catch( final Throwable e ) 152 { 153 final String error = 154 "Error creating factory with class " + className; 155 getLogger().error( "## CLASSLOADER: " + Thread.currentThread().getContextClassLoader() ); 156 throw new CascadingException( error, e ); 157 } 158 159 ContainerUtil.enableLogging( factory, getLogger() ); 160 ContainerUtil.contextualize( factory, m_context ); 161 ContainerUtil.configure( factory, configuration ); 162 ContainerUtil.initialize( factory ); 163 164 return factory; 165 } 166 167 174 public ServerSocketFactory getServerSocketFactory( String name ) 175 throws Exception 176 { 177 final ServerSocketFactory factory = (ServerSocketFactory)m_serverSockets.get( name ); 178 179 if( null != factory ) 180 { 181 return factory; 182 } 183 else 184 { 185 throw new Exception ( "Unable to locate server socket factory " + 186 "named " + name ); 187 } 188 } 189 190 197 public SocketFactory getSocketFactory( final String name ) 198 throws Exception 199 { 200 final SocketFactory factory = (SocketFactory)m_sockets.get( name ); 201 202 if( null != factory ) 203 { 204 return factory; 205 } 206 else 207 { 208 throw new Exception ( "Unable to locate client socket factory " + 209 "named " + name ); 210 } 211 } 212 } 213 | Popular Tags |