KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > PasswordConfReader


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.common;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.BufferedReader JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Enumeration JavaDoc;
32
33 /**
34  * Provides api to read passwords from password conf file.
35  */

36 public final class PasswordConfReader
37 {
38     public static final String JavaDoc KEY_STORE_ALIAS = "KeyStore";
39
40     public static final String JavaDoc TRUST_STORE_ALIAS = "TrustStore";
41
42     public static final String JavaDoc PASSWORD_FILE_PROPERTY =
43         "com.sun.aas.ssl.passwordfile";
44
45     static final String JavaDoc CONFIG = "config";
46
47     static final String JavaDoc PASSWORD_CONF = "password.conf";
48
49     private static final String JavaDoc INSTANCE_ROOT =
50         System.getProperty("com.sun.aas.instanceRoot");
51
52     private static final String JavaDoc SERVER_NAME_PROPERTY = "com.sun.aas.instanceName";
53
54     private static PasswordFile pf;
55     private static Hashtable JavaDoc entries;
56
57     /* Constructs new PasswordConfReader object */
58     private PasswordConfReader()
59     {
60     }
61
62     /**
63      */

64     public static String JavaDoc getKeyStorePassword() throws IOException JavaDoc
65     {
66         return getPassword(KEY_STORE_ALIAS);
67     }
68
69     /**
70      */

71     public static String JavaDoc getTrustStorePassword() throws IOException JavaDoc
72     {
73         return getPassword(TRUST_STORE_ALIAS);
74     }
75
76     /**
77      */

78     public static String JavaDoc getPassword(String JavaDoc alias) throws IOException JavaDoc
79     {
80         if (!isInSync()) { sync(); }
81         return get(alias);
82     }
83
84     public static Enumeration JavaDoc listAliases() throws IOException JavaDoc
85     {
86         if (!isInSync()) { sync(); }
87         return entries.keys();
88     }
89
90     private static boolean isInSync() throws IOException JavaDoc
91     {
92         return getPasswordFile().equals(pf);
93     }
94
95     private static void sync() throws IOException JavaDoc
96     {
97         synchronized (PasswordConfReader.class)
98         {
99             pf = getPasswordFile();
100         }
101         loadEntries();
102     }
103
104     private static PasswordFile getPasswordFile() throws IOException JavaDoc
105     {
106         final String JavaDoc prop = System.getProperty(PASSWORD_FILE_PROPERTY);
107         PasswordFile f;
108         if ((prop != null) && (prop.length() > 0))
109         {
110             f = new PasswordFile(prop);
111         }
112         else
113         {
114             f = new PasswordFile(getDefaultPasswordConf());
115         }
116         return f;
117     }
118
119     private static String JavaDoc getDefaultPasswordConf()
120     {
121         String JavaDoc defaultConf = null;
122         if (INSTANCE_ROOT != null)
123         {
124             defaultConf = INSTANCE_ROOT + File.separator
125                             + File.separator + CONFIG + File.separator
126                             + PASSWORD_CONF;
127         }
128         return defaultConf;
129     }
130
131     private static synchronized void loadEntries() throws IOException JavaDoc
132     {
133         if (entries == null)
134         {
135             entries = new Hashtable JavaDoc();
136         }
137         BufferedReader JavaDoc reader = null;
138         try
139         {
140             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(pf.getPath()));
141             String JavaDoc str;
142             while ((str = reader.readLine()) != null)
143             {
144                 int index = str.indexOf(':');
145                 if (index > 0)
146                 {
147                     entries.put(str.substring(0, index),
148                                 str.substring(index+1));
149                 }
150             }
151         }
152         finally
153         {
154             if (reader != null) { reader.close(); }
155         }
156     }
157
158     private static synchronized String JavaDoc get(String JavaDoc key) throws IOException JavaDoc
159     {
160         final String JavaDoc password = (String JavaDoc)entries.get(key);
161         if (password == null)
162         {
163             throw new IOException JavaDoc("No entry found for " + key);
164         }
165         return password;
166     }
167
168     private static final class PasswordFile
169     {
170         private String JavaDoc path;
171         private long lastModified;
172
173         private PasswordFile(String JavaDoc path) throws IOException JavaDoc
174         {
175             final File JavaDoc f = new File JavaDoc(path).getCanonicalFile();
176             if (!f.exists())
177             {
178                 throw new IOException JavaDoc("Password file does not exist. "
179                                       + f.getAbsolutePath());
180             }
181             this.path = f.getAbsolutePath();
182             lastModified = f.lastModified();
183         }
184
185         public String JavaDoc getPath()
186         {
187             return path;
188         }
189
190         public boolean equals(Object JavaDoc o)
191         {
192             if (o == null) { return false; }
193             if (o == this) { return true; }
194             PasswordFile that = (PasswordFile)o;
195             return (path.equals(that.path) &&
196                     (lastModified == that.lastModified));
197         }
198
199         public String JavaDoc toString()
200         {
201             return "Path = " + getPath() + ' ' + "lastModified = " + lastModified;
202         }
203     }
204 }
205
Popular Tags