KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > net > SSLConfiguration


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Marc Wick.
21  * Contributor(s): ______________________.
22  */

23
24 package org.continuent.sequoia.common.net;
25
26 import java.io.File JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29 import org.continuent.sequoia.common.xml.ControllerXmlTags;
30
31 /**
32  * This class defines a SSLConfiguration
33  *
34  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
35  * @version 1.0
36  */

37 public class SSLConfiguration implements Serializable JavaDoc
38 {
39   private static final long serialVersionUID = -7030030045041996566L;
40
41   /** kestore file */
42   private File JavaDoc keyStore;
43   /** keystore password */
44   private String JavaDoc keyStorePassword;
45   /** key password */
46   private String JavaDoc keyStoreKeyPassword;
47
48   // TODO : provide support for naming aliases
49

50   /** need client authentication */
51   private boolean isClientAuthenticationRequired = false;
52
53   /** truststore file */
54   private File JavaDoc trustStore;
55   /** truststore password */
56   private String JavaDoc trustStorePassword;
57
58   /**
59    * Returns the isClientAuthenticationRequired value.
60    *
61    * @return Returns the isClientAuthenticationRequired.
62    */

63   public boolean isClientAuthenticationRequired()
64   {
65     return isClientAuthenticationRequired;
66   }
67
68   /**
69    * Sets the isClientAuthenticationRequired value.
70    *
71    * @param isClientAuthenticationRequired The isClientAuthenticationRequired to
72    * set.
73    */

74   public void setClientAuthenticationRequired(
75       boolean isClientAuthenticationRequired)
76   {
77     this.isClientAuthenticationRequired = isClientAuthenticationRequired;
78   }
79
80   /**
81    * Returns the keyStore value.
82    *
83    * @return Returns the keyStore.
84    */

85   public File JavaDoc getKeyStore()
86   {
87     return keyStore;
88   }
89
90   /**
91    * Sets the keyStore value.
92    *
93    * @param keyStore The keyStore to set.
94    */

95   public void setKeyStore(File JavaDoc keyStore)
96   {
97     this.keyStore = keyStore;
98   }
99
100   /**
101    * Returns the keyStoreKeyPassword value.
102    *
103    * @return Returns the keyStoreKeyPassword.
104    */

105   public String JavaDoc getKeyStoreKeyPassword()
106   {
107     if (keyStoreKeyPassword != null)
108       return keyStoreKeyPassword;
109     return getKeyStorePassword();
110   }
111
112   /**
113    * Sets the keyStoreKeyPassword value.
114    *
115    * @param keyStoreKeyPassword The keyStoreKeyPassword to set.
116    */

117   public void setKeyStoreKeyPassword(String JavaDoc keyStoreKeyPassword)
118   {
119     this.keyStoreKeyPassword = keyStoreKeyPassword;
120   }
121
122   /**
123    * Returns the keyStorePassword value.
124    *
125    * @return Returns the keyStorePassword.
126    */

127   public String JavaDoc getKeyStorePassword()
128   {
129     return keyStorePassword;
130   }
131
132   /**
133    * Sets the keyStorePassword value.
134    *
135    * @param keyStorePassword The keyStorePassword to set.
136    */

137   public void setKeyStorePassword(String JavaDoc keyStorePassword)
138   {
139     this.keyStorePassword = keyStorePassword;
140   }
141
142   /**
143    * Returns the trustStore value.
144    *
145    * @return Returns the trustStore.
146    */

147   public File JavaDoc getTrustStore()
148   {
149     if (trustStore != null)
150       return trustStore;
151
152     return getKeyStore();
153   }
154
155   /**
156    * Sets the trustStore value.
157    *
158    * @param trustStore The trustStore to set.
159    */

160   public void setTrustStore(File JavaDoc trustStore)
161   {
162     this.trustStore = trustStore;
163   }
164
165   /**
166    * Returns the trustStorePassword value.
167    *
168    * @return Returns the trustStorePassword.
169    */

170   public String JavaDoc getTrustStorePassword()
171   {
172     if (trustStorePassword != null)
173       return trustStorePassword;
174
175     return getKeyStorePassword();
176   }
177
178   /**
179    * Sets the trustStorePassword value.
180    *
181    * @param trustStorePassword The trustStorePassword to set.
182    */

183   public void setTrustStorePassword(String JavaDoc trustStorePassword)
184   {
185     this.trustStorePassword = trustStorePassword;
186   }
187
188   /**
189    * create a SSLConfiguration with the java default behaviour (using System
190    * properties)
191    *
192    * @return config
193    */

194   public static SSLConfiguration getDefaultConfig()
195   {
196     SSLConfiguration config = new SSLConfiguration();
197     String JavaDoc keyStoreProperty = System.getProperty("javax.net.ssl.keyStore");
198     if (keyStoreProperty == null)
199       throw new RuntimeException JavaDoc(
200           "javax.net.ssl.keyStore has not been properly defined");
201     config.keyStore = new File JavaDoc(keyStoreProperty);
202
203     config.keyStorePassword = System
204         .getProperty("javax.net.ssl.keyStorePassword");
205     if (config.keyStorePassword == null)
206       throw new RuntimeException JavaDoc(
207           "javax.net.ssl.keyStorePassword has not been properly defined");
208
209     config.keyStoreKeyPassword = System
210         .getProperty("javax.net.ssl.keyStoreKeyPassword");
211     if (config.keyStoreKeyPassword == null)
212       config.keyStoreKeyPassword = config.keyStorePassword;
213
214     String JavaDoc trustStoreProperty = System.getProperty("javax.net.ssl.trustStore");
215     if (trustStoreProperty == null)
216       trustStoreProperty = keyStoreProperty;
217     config.trustStore = new File JavaDoc(trustStoreProperty);
218
219     config.trustStorePassword = System
220         .getProperty("javax.net.ssl.trustStorePassword");
221     if (config.trustStorePassword == null)
222       config.trustStorePassword = config.keyStorePassword;
223     return config;
224   }
225
226   /**
227    * Dump the XML settings of this SSL configuration
228    *
229    * @return an XML dump of the configuration
230    */

231   public String JavaDoc getXml()
232   {
233     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
234     info.append("<" + ControllerXmlTags.ELT_SSL + " "
235         + ControllerXmlTags.ATT_SSL_KEYSTORE + "=\"" + getKeyStore() + "\" "
236         + ControllerXmlTags.ATT_SSL_KEYSTORE_PASSWORD + "=\""
237         + getKeyStorePassword() + "\" "
238         + ControllerXmlTags.ATT_SSL_KEYSTORE_KEYPASSWORD + "=\""
239         + getKeyStoreKeyPassword() + "\" "
240         + ControllerXmlTags.ATT_SSL_NEED_CLIENT_AUTH + "=\""
241         + isClientAuthenticationRequired() + "\" "
242         + ControllerXmlTags.ATT_SSL_TRUSTSTORE + "=\"" + getTrustStore()
243         + "\" " + ControllerXmlTags.ATT_SSL_TRUSTSTORE_PASSWORD + "=\""
244         + getTrustStorePassword() + "\"/>");
245     return info.toString();
246   }
247
248 }
249
Popular Tags