1 23 24 package com.sun.enterprise.admin.common; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.FileReader ; 29 import java.io.BufferedReader ; 30 import java.util.Hashtable ; 31 import java.util.Enumeration ; 32 33 36 public final class PasswordConfReader 37 { 38 public static final String KEY_STORE_ALIAS = "KeyStore"; 39 40 public static final String TRUST_STORE_ALIAS = "TrustStore"; 41 42 public static final String PASSWORD_FILE_PROPERTY = 43 "com.sun.aas.ssl.passwordfile"; 44 45 static final String CONFIG = "config"; 46 47 static final String PASSWORD_CONF = "password.conf"; 48 49 private static final String INSTANCE_ROOT = 50 System.getProperty("com.sun.aas.instanceRoot"); 51 52 private static final String SERVER_NAME_PROPERTY = "com.sun.aas.instanceName"; 53 54 private static PasswordFile pf; 55 private static Hashtable entries; 56 57 58 private PasswordConfReader() 59 { 60 } 61 62 64 public static String getKeyStorePassword() throws IOException 65 { 66 return getPassword(KEY_STORE_ALIAS); 67 } 68 69 71 public static String getTrustStorePassword() throws IOException 72 { 73 return getPassword(TRUST_STORE_ALIAS); 74 } 75 76 78 public static String getPassword(String alias) throws IOException 79 { 80 if (!isInSync()) { sync(); } 81 return get(alias); 82 } 83 84 public static Enumeration listAliases() throws IOException 85 { 86 if (!isInSync()) { sync(); } 87 return entries.keys(); 88 } 89 90 private static boolean isInSync() throws IOException 91 { 92 return getPasswordFile().equals(pf); 93 } 94 95 private static void sync() throws IOException 96 { 97 synchronized (PasswordConfReader.class) 98 { 99 pf = getPasswordFile(); 100 } 101 loadEntries(); 102 } 103 104 private static PasswordFile getPasswordFile() throws IOException 105 { 106 final String 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 getDefaultPasswordConf() 120 { 121 String 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 132 { 133 if (entries == null) 134 { 135 entries = new Hashtable (); 136 } 137 BufferedReader reader = null; 138 try 139 { 140 reader = new BufferedReader (new FileReader (pf.getPath())); 141 String 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 get(String key) throws IOException 159 { 160 final String password = (String )entries.get(key); 161 if (password == null) 162 { 163 throw new IOException ("No entry found for " + key); 164 } 165 return password; 166 } 167 168 private static final class PasswordFile 169 { 170 private String path; 171 private long lastModified; 172 173 private PasswordFile(String path) throws IOException 174 { 175 final File f = new File (path).getCanonicalFile(); 176 if (!f.exists()) 177 { 178 throw new IOException ("Password file does not exist. " 179 + f.getAbsolutePath()); 180 } 181 this.path = f.getAbsolutePath(); 182 lastModified = f.lastModified(); 183 } 184 185 public String getPath() 186 { 187 return path; 188 } 189 190 public boolean equals(Object 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 toString() 200 { 201 return "Path = " + getPath() + ' ' + "lastModified = " + lastModified; 202 } 203 } 204 } 205 | Popular Tags |