KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.activity.Initializable;
21 import org.apache.avalon.framework.configuration.Configurable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.container.ContainerUtil;
25 import org.apache.avalon.framework.context.Context;
26 import org.apache.avalon.framework.context.Contextualizable;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28
29 /**
30  * Contains the code common for both TLS socket factories. They both
31  * need to use an SSLFactoryBuilder which is configured using
32  * configuration and context given by the container. Then, they both
33  * set timeouts on the manufactured sockets.
34  *
35  * @author <a HREF="mailto:greg-avalon-apps at nest.cx">Greg Steuck</a>
36  */

37 public abstract class AbstractTLSSocketFactory
38     extends AbstractLogEnabled
39     implements Contextualizable, Configurable, Initializable
40 {
41     private final static int WAIT_FOREVER = 0;
42     protected int m_socketTimeOut;
43
44     private Context m_context;
45     private Configuration m_childConfig;
46
47     public void contextualize( final Context context )
48     {
49         m_context = context;
50     }
51
52     /**
53      * Configures the factory.
54      *
55      * @param configuration the Configuration
56      * @exception ConfigurationException if an error occurs
57      */

58     public void configure( final Configuration configuration )
59         throws ConfigurationException
60     {
61         m_socketTimeOut = configuration.getChild( "timeout" ).getValueAsInteger( WAIT_FOREVER );
62         m_childConfig = configuration.getChild( "ssl-factory", false );
63         if( m_childConfig == null )
64         {
65             final String JavaDoc message = "ssl-factory child not found, please" +
66                 " update your configuration according to" +
67                 " the documentation. Reverting to the" +
68                 " old configuration format.";
69             getLogger().warn( message );
70             // not completely compatible though
71
m_childConfig = configuration;
72         }
73     }
74
75     /**
76      * Creates an SSL factory using the confuration values.
77      */

78     public void initialize() throws Exception JavaDoc
79     {
80         final SSLFactoryBuilder builder = new SSLFactoryBuilder();
81         setupLogger( builder );
82         ContainerUtil.contextualize( builder, m_context );
83         ContainerUtil.configure( builder, m_childConfig );
84         ContainerUtil.initialize( builder );
85
86         visitBuilder( builder );
87
88         ContainerUtil.shutdown( builder );
89         m_context = null;
90         m_childConfig = null;
91     }
92
93     /**
94      * The child factories have to use an instance of
95      * <tt>SSLFactoryBuilder</tt> to obtain their factories. So they
96      * are given an instance when it's ready. Another alternative was
97      * to have the SSLFactoryBuilder export buildContext method, but
98      * that would mean SSLContext which is deep in Sun guts will be
99      * aired in 3-4 classes instead of 1.
100      */

101     protected abstract void visitBuilder( SSLFactoryBuilder builder );
102 }
103
Popular Tags