KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > services > connection > AbstractService


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.services.connection;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23
24 import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory;
25 import org.apache.avalon.cornerstone.services.sockets.SocketManager;
26 import org.apache.avalon.cornerstone.services.threads.ThreadManager;
27
28 import org.apache.excalibur.thread.ThreadPool;
29
30 import org.apache.avalon.framework.activity.Disposable;
31 import org.apache.avalon.framework.activity.Initializable;
32 import org.apache.avalon.framework.component.ComponentException;
33 import org.apache.avalon.framework.component.WrapperComponentManager;
34 import org.apache.avalon.framework.configuration.Configurable;
35 import org.apache.avalon.framework.configuration.Configuration;
36 import org.apache.avalon.framework.configuration.ConfigurationException;
37 import org.apache.avalon.framework.container.ContainerUtil;
38 import org.apache.avalon.framework.context.Context;
39 import org.apache.avalon.framework.context.ContextException;
40 import org.apache.avalon.framework.context.Contextualizable;
41 import org.apache.avalon.framework.logger.AbstractLogEnabled;
42 import org.apache.avalon.framework.logger.Logger;
43 import org.apache.avalon.framework.service.ServiceException;
44 import org.apache.avalon.framework.service.ServiceManager;
45 import org.apache.avalon.framework.service.Serviceable;
46
47 /**
48  * Helper class to create protocol services.
49  *
50  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
51  */

52 public abstract class AbstractService
53     extends AbstractLogEnabled
54     implements Contextualizable, Serviceable, Configurable, Initializable, Disposable
55 {
56     protected ConnectionManager m_connectionManager;
57     protected SocketManager m_socketManager;
58     protected ConnectionHandlerFactory m_factory;
59     protected ThreadManager m_threadManager;
60     protected ThreadPool m_threadPool;
61     protected String JavaDoc m_serverSocketType;
62     protected int m_port;
63     protected InetAddress JavaDoc m_bindTo; //network interface to bind to
64
protected ServerSocket JavaDoc m_serverSocket;
65     protected String JavaDoc m_connectionName;
66
67     public AbstractService()
68     {
69         m_factory = createFactory();
70         m_serverSocketType = "plain";
71     }
72
73     protected String JavaDoc getThreadPoolName()
74     {
75         return null;
76     }
77
78     protected abstract ConnectionHandlerFactory createFactory();
79
80     public void enableLogging( final Logger logger )
81     {
82         super.enableLogging( logger );
83         ContainerUtil.enableLogging( m_factory, logger );
84     }
85
86     public void contextualize( final Context context )
87         throws ContextException
88     {
89         ContainerUtil.contextualize( m_factory, context );
90     }
91
92     public void service( final ServiceManager serviceManager )
93         throws ServiceException
94     {
95         m_connectionManager = (ConnectionManager)serviceManager.lookup( ConnectionManager.ROLE );
96         m_socketManager = (SocketManager)serviceManager.lookup( SocketManager.ROLE );
97         if( null != getThreadPoolName() )
98         {
99             m_threadManager =
100                 (ThreadManager)serviceManager.lookup( ThreadManager.ROLE );
101             m_threadPool = m_threadManager.getThreadPool( getThreadPoolName() );
102         }
103         ContainerUtil.service( m_factory, serviceManager );
104         try
105         {
106             ContainerUtil.compose( m_factory, new WrapperComponentManager( serviceManager ) );
107         }
108         catch( final ComponentException ce )
109         {
110             throw new ServiceException( ConnectionHandlerFactory.class.getName(), ce.getMessage(), ce );
111         }
112     }
113
114     public void configure( final Configuration configuration )
115         throws ConfigurationException
116     {
117         ContainerUtil.configure( m_factory, configuration );
118     }
119
120     public void initialize()
121         throws Exception JavaDoc
122     {
123         ContainerUtil.initialize( m_factory );
124
125         if( null == m_connectionName )
126         {
127             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128             sb.append( m_serverSocketType );
129             sb.append( ':' );
130             sb.append( m_port );
131
132             if( null != m_bindTo )
133             {
134                 sb.append( '/' );
135                 sb.append( m_bindTo );
136             }
137
138             m_connectionName = sb.toString();
139         }
140
141         final ServerSocketFactory factory =
142             m_socketManager.getServerSocketFactory( m_serverSocketType );
143
144         if( null == m_bindTo )
145         {
146             m_serverSocket = factory.createServerSocket( m_port );
147         }
148         else
149         {
150             m_serverSocket = factory.createServerSocket( m_port, 5, m_bindTo );
151         }
152
153         if( null == m_threadPool )
154         {
155             m_connectionManager.connect( m_connectionName, m_serverSocket,
156                                          m_factory );
157         }
158         else
159         {
160             m_connectionManager.connect( m_connectionName, m_serverSocket,
161                                          m_factory, m_threadPool );
162         }
163     }
164
165     public void dispose()
166     {
167         try
168         {
169             m_connectionManager.disconnect( m_connectionName );
170         }
171         catch( final Exception JavaDoc e )
172         {
173             final String JavaDoc message = "Error disconnecting";
174             getLogger().warn( message, e );
175         }
176
177         try
178         {
179             m_serverSocket.close();
180         }
181         catch( final IOException JavaDoc ioe )
182         {
183             final String JavaDoc message = "Error closing server socket";
184             getLogger().warn( message, ioe );
185         }
186     }
187 }
188
Popular Tags