KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > source > impl > HTTPSClientSourceFactory


1 /*
2  * Copyright 2002-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 package org.apache.excalibur.source.impl;
18
19 import java.security.Provider JavaDoc;
20 import java.security.Security JavaDoc;
21
22 import org.apache.avalon.framework.parameters.ParameterException;
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.commons.httpclient.protocol.Protocol;
25 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
26
27 /**
28  * {@link HTTPClientSource} Factory class.
29  *
30  * @avalon.component
31  * @avalon.service type=org.apache.excalibur.source.SourceFactory
32  * @x-avalon.info name=httpsclient-source
33  * @x-avalon.lifestyle type=singleton
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @version CVS $Id: HTTPSClientSourceFactory.java,v 1.5 2004/02/28 11:47:24 cziegeler Exp $
37  */

38 public class HTTPSClientSourceFactory extends HTTPClientSourceFactory
39 {
40     /**
41      * SSL implementation provider.
42      */

43     public static final String JavaDoc SSL_PROVIDER = "provider";
44
45     /**
46      * SSL socket factory.
47      */

48     public static final String JavaDoc SOCKET_FACTORY = "socket-factory";
49
50     /**
51      * HTTPS constant.
52      */

53     public static final String JavaDoc HTTPS = "https";
54
55     /**
56      * Parameterize this {@link org.apache.excalibur.source.SourceFactory SourceFactory}.
57      *
58      * @param params {@link Parameters} instance
59      * @exception ParameterException if an error occurs
60      */

61     public void parameterize( final Parameters params )
62         throws ParameterException
63     {
64         super.parameterize( params );
65
66         setProvider( params );
67         setSocketFactory( params );
68     }
69
70     /**
71      * Method to set up the SSL provider for this factory
72      * instance.
73      *
74      * @param params configuration {@link Parameters}
75      * @exception ParameterException if an error occurs
76      */

77     private void setProvider( final Parameters params )
78         throws ParameterException
79     {
80         String JavaDoc provider = null;
81
82         try
83         {
84             provider = params.getParameter( SSL_PROVIDER );
85         }
86         catch ( final ParameterException e )
87         {
88             return; // this is ok, means no custom SSL provider
89
}
90
91         Security.addProvider( (Provider JavaDoc) getInstance( provider ) );
92     }
93
94     /**
95      * Method to set up the SSL socket factory for this
96      * source factory instance.
97      *
98      * @param params configuration {@link Parameters}
99      * @exception ParameterException if an error occurs
100      */

101     private void setSocketFactory( final Parameters params )
102         throws ParameterException
103     {
104         String JavaDoc factoryName = null;
105
106         try
107         {
108             factoryName = params.getParameter( SOCKET_FACTORY );
109         }
110         catch ( final ParameterException e )
111         {
112             return; // this is ok, means no custom socket factory
113
}
114
115         final Protocol protocol =
116             new Protocol(
117                 HTTPS,
118                 ( SecureProtocolSocketFactory ) getInstance( factoryName ),
119                 443
120             );
121         Protocol.registerProtocol( HTTPS, protocol );
122     }
123
124     /**
125      * Helper method to create a single instance from a class name. Assumes
126      * given class name has a no-parameter constructor.
127      *
128      * @param className class name to instantiate
129      * @return instantiated class
130      * @exception Exception if an error occurs
131      */

132     private Object JavaDoc getInstance( final String JavaDoc className )
133         throws ParameterException
134     {
135         try
136         {
137             return Class.forName( className ).newInstance();
138         }
139         catch ( final Exception JavaDoc e )
140         {
141             throw new ParameterException(
142                 "Unable to instantiate: " + className, e
143             );
144         }
145     }
146 }
147
Popular Tags