KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > rmi > socket > ssl > SSLServerSocketFactory


1 /*
2  * $Id: SSLServerSocketFactory.java 6301 2005-12-12 12:46:44Z jonesde $
3  *
4  * Copyright (c) 2004-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25
26 package org.ofbiz.service.rmi.socket.ssl;
27
28 import java.io.FileInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.net.ServerSocket JavaDoc;
32 import java.rmi.server.RMIServerSocketFactory JavaDoc;
33 import java.security.GeneralSecurityException JavaDoc;
34 import java.security.KeyStore JavaDoc;
35 import java.security.KeyStoreException JavaDoc;
36 import java.security.NoSuchAlgorithmException JavaDoc;
37 import java.security.cert.CertificateException JavaDoc;
38 import javax.net.ssl.SSLServerSocket;
39
40 import org.ofbiz.base.util.Debug;
41 import org.ofbiz.base.util.SSLUtil;
42 import org.ofbiz.base.util.UtilProperties;
43
44 /**
45  * RMI SSL Server Socket Factory
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 6301 $
49  * @since 3.3
50  */

51 public class SSLServerSocketFactory implements RMIServerSocketFactory JavaDoc, Serializable JavaDoc {
52
53     public static final String JavaDoc module = SSLServerSocketFactory.class.getName();
54     protected boolean clientAuth = false;
55
56     public void setNeedClientAuth(boolean clientAuth) {
57         this.clientAuth = clientAuth;
58     }
59
60     public ServerSocket JavaDoc createServerSocket(int port) throws IOException JavaDoc {
61         String JavaDoc storeType = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore.type", "jks");
62         String JavaDoc storeFile = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore", null);
63         String JavaDoc storeAlias = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore.alias", null);
64         String JavaDoc storePass = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore.password", null);
65         char[] passphrase = null;
66         if (storePass != null) {
67             passphrase = storePass.toCharArray();
68         }
69
70         KeyStore JavaDoc ks = null;
71         try {
72             ks = KeyStore.getInstance(storeType);
73             ks.load(new FileInputStream JavaDoc(storeFile), passphrase);
74         } catch (NoSuchAlgorithmException JavaDoc e) {
75             Debug.logError(e, module);
76             throw new IOException JavaDoc(e.getMessage());
77         } catch (CertificateException JavaDoc e) {
78             Debug.logError(e, module);
79             throw new IOException JavaDoc(e.getMessage());
80         } catch (KeyStoreException JavaDoc e) {
81             Debug.logError(e, module);
82             throw new IOException JavaDoc(e.getMessage());
83         }
84
85         if (ks == null) {
86             throw new IOException JavaDoc("Unable to load KeyStore containing Service Engine RMI SSL certificate");
87         }
88
89
90         javax.net.ssl.SSLServerSocketFactory factory = null;
91         try {
92             factory = SSLUtil.getSSLServerSocketFactory(ks, storePass, storeAlias);
93         } catch (GeneralSecurityException JavaDoc e) {
94             Debug.logError(e, "Error getting javax.net.ssl.SSLServerSocketFactory instance for Service Engine RMI calls: " + e.toString(), module);
95             throw new IOException JavaDoc(e.toString());
96         }
97
98         if (factory == null) {
99             throw new IOException JavaDoc("Unable to obtain SSLServerSocketFactory for provided KeyStore");
100         }
101
102         SSLServerSocket socket = (SSLServerSocket) factory.createServerSocket(port);
103         socket.setNeedClientAuth(clientAuth);
104         return socket;
105     }
106 }
107
Popular Tags