KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > sockets > DefaultSocketManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.blocks.sockets;
19
20 import java.util.HashMap JavaDoc;
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 /**
35  * Implementation of SocketManager.
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @avalon.component name="socket-manager" lifestyle="singleton"
39  * @avalon.service type="org.apache.avalon.cornerstone.services.sockets.SocketManager"
40  */

41 public class DefaultSocketManager
42     extends AbstractLogEnabled
43     implements SocketManager, Contextualizable, Configurable, Initializable
44 {
45     protected final HashMap JavaDoc m_serverSockets = new HashMap JavaDoc();
46     protected final HashMap JavaDoc m_sockets = new HashMap JavaDoc();
47
48     protected Context m_context;
49     protected Configuration m_configuration;
50
51    /**
52     * @avalon.entry key="urn:avalon:name" alias="block.name"
53     * @avalon.entry key="urn:avalon:partition" alias="app.name"
54     * @avalon.entry key="urn:avalon:home" type="java.io.File" alias="app.home"
55     */

56     public void contextualize( final Context context )
57     {
58         m_context = context;
59     }
60
61     /**
62      * Configure the SocketManager.
63      *
64      * @param configuration the Configuration
65      * @exception ConfigurationException if an error occurs
66      * @avalon.configuration schema="http://relaxng.org/ns/structure/1.0"
67      */

68     public void configure( final Configuration configuration )
69         throws ConfigurationException
70     {
71         m_configuration = configuration;
72     }
73
74     public void initialize()
75         throws Exception JavaDoc
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 JavaDoc name = element.getAttribute( "name" );
84             final String JavaDoc 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 JavaDoc name = element.getAttribute( "name" );
96             final String JavaDoc className = element.getAttribute( "class" );
97
98             setupClientSocketFactory( name, className, element );
99         }
100     }
101
102     protected void setupServerSocketFactory( final String JavaDoc name,
103                                              final String JavaDoc className,
104                                              final Configuration configuration )
105         throws Exception JavaDoc
106     {
107         final Object JavaDoc object = createFactory( name, className, configuration );
108
109         if( !( object instanceof ServerSocketFactory ) )
110         {
111             throw new Exception JavaDoc( "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 JavaDoc name,
121                                              final String JavaDoc className,
122                                              final Configuration configuration )
123         throws Exception JavaDoc
124     {
125         final Object JavaDoc object = createFactory( name, className, configuration );
126
127         if( !( object instanceof SocketFactory ) )
128         {
129             throw new Exception JavaDoc( "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 JavaDoc createFactory( final String JavaDoc name,
139                                     final String JavaDoc className,
140                                     final Configuration configuration )
141         throws Exception JavaDoc
142     {
143         Object JavaDoc factory = null;
144
145         try
146         {
147             final ClassLoader JavaDoc classLoader =
148                 Thread.currentThread().getContextClassLoader();
149             factory = classLoader.loadClass( className ).newInstance();
150         }
151         catch( final Throwable JavaDoc e )
152         {
153             final String JavaDoc 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     /**
168      * Retrieve a server socket factory by name.
169      *
170      * @param name the name of server socket factory
171      * @return the ServerSocketFactory
172      * @exception Exception if server socket factory is not available
173      */

174     public ServerSocketFactory getServerSocketFactory( String JavaDoc name )
175         throws Exception JavaDoc
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 JavaDoc( "Unable to locate server socket factory " +
186                                  "named " + name );
187         }
188     }
189
190     /**
191      * Retrieve a client socket factory by name.
192      *
193      * @param name the name of client socket factory
194      * @return the SocketFactory
195      * @exception Exception if socket factory is not available
196      */

197     public SocketFactory getSocketFactory( final String JavaDoc name )
198         throws Exception JavaDoc
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 JavaDoc( "Unable to locate client socket factory " +
209                                  "named " + name );
210         }
211     }
212 }
213
Popular Tags