1 23 package com.sun.enterprise.util; 24 25 import com.sun.enterprise.util.i18n.StringManager; 26 import java.io.File ; 27 28 import com.sun.enterprise.util.i18n.StringManagerBase; 29 import com.sun.logging.LogDomains; 30 31 import com.sun.enterprise.security.store.PasswordAdapter; 32 import com.sun.enterprise.security.store.IdentityManager; 33 import java.io.IOException ; 34 import java.security.KeyStoreException ; 35 import java.security.NoSuchAlgorithmException ; 36 import java.security.UnrecoverableKeyException ; 37 import java.security.cert.CertificateException ; 38 39 import java.util.logging.Logger ; 40 import java.util.logging.Level ; 41 import java.util.ArrayList ; 42 43 58 public class RelativePathResolver { 59 60 private static Logger _logger = null; 61 62 private static RelativePathResolver _instance = null; 63 private PasswordAdapter pwdAdapter = null; 64 65 private static final String ALIAS_TOKEN = "ALIAS"; 66 private static final String ALIAS_DELIMITER = "="; 67 68 protected synchronized static Logger getLogger() { 69 if (_logger == null) { 70 _logger = LogDomains.getLogger(LogDomains.UTIL_LOGGER); 71 } 72 return _logger; 73 } 74 75 private synchronized static RelativePathResolver getInstance() 76 { 77 if (_instance == null) { 78 _instance = new RelativePathResolver(); 79 } 80 return _instance; 81 } 82 83 public static String unresolvePath(String path, String [] propNames) 84 { 85 return getInstance().unresolve(path, propNames); 86 } 87 88 public static String resolvePath(String path) 89 { 90 return getInstance().resolve(path); 91 } 92 93 public RelativePathResolver() 94 { 95 } 96 97 101 public String unresolve(String path, String [] propNames) { 102 if (path != null) { 103 int startIdx; 104 String propVal; 105 106 path = path.replace(File.separatorChar, '/'); 110 for (int i = 0; i < propNames.length; i++) { 111 propVal = getPropertyValue(propNames[i], true); 112 if (propVal != null) { 113 propVal = propVal.replace(File.separatorChar, '/'); 116 startIdx = path.indexOf(propVal); 117 if (startIdx >= 0) { 118 path = path.substring(0, startIdx) + 119 "${" + propNames[i] + "}" + 120 path.substring(startIdx + propVal.length()); 121 } 122 } else { 123 getLogger().log(Level.SEVERE, 124 "enterprise_util.path_unresolver_missing_property", 125 new Object [] {propNames[i], path}); 126 } 127 } 128 } 129 return path; 130 } 131 132 138 protected void fatalError(String message, String path) { 139 getLogger().log(Level.SEVERE, message, new Object [] {path}); 140 StringManagerBase sm = StringManagerBase.getStringManager(getLogger().getResourceBundleName()); 141 throw new RuntimeException (sm.getString(message, path)); 142 } 143 144 private void appendChar (char c, StringBuffer propName, StringBuffer result) 145 { 146 if (propName == null) { 147 result.append(c); 148 } else { 149 propName.append(c); 150 } 151 } 152 153 154 160 static public String getAlias(String propName) 161 { 162 String aliasName=null; 163 String starter = "${" + ALIAS_TOKEN + "="; String ender = "}"; 165 166 propName = propName.trim(); 167 if (propName.startsWith(starter) && propName.endsWith(ender) ) { 168 propName = propName.substring(starter.length() ); 169 int lastIdx = propName.length() - 1; 170 if (lastIdx > 1) { 171 propName = propName.substring(0,lastIdx); 172 if (propName!=null) 173 aliasName = propName.trim(); 174 } 175 } 176 return aliasName; 177 } 178 179 180 188 protected String getPropertyValue(String propName, boolean bIncludingEnvironmentVariables) 189 { 190 if(!bIncludingEnvironmentVariables) 191 return null; 192 193 String result = System.getProperty(propName); 195 if (result == null) { 196 int idx1 = propName.indexOf(ALIAS_TOKEN); 198 if (idx1 >= 0) { 199 int idx2 = propName.indexOf(ALIAS_DELIMITER, ALIAS_TOKEN.length()); 200 if (idx2 > 0) { 201 String aliasName = propName.substring(idx2 + 1).trim(); 202 try { 204 if (pwdAdapter==null) { 205 String masterPassword = IdentityManager.getMasterPassword(); 208 pwdAdapter = new PasswordAdapter(masterPassword.toCharArray()); 209 } 210 result = pwdAdapter.getPasswordForAlias(aliasName); 211 } catch (Exception ex) { 213 getLogger().log(Level.WARNING, "enterprise_util.path_resolver_alias_exception", 214 new Object [] {ex, aliasName, propName}); 215 getLogger().log(Level.FINE, "enterprise_util.path_resolver_alias_exception", 216 ex); 217 } 218 } 219 } 220 } 221 return result; 222 } 223 224 public String resolve(String path) { 225 return resolve(path, true); 226 } 227 232 public String resolve(String path, boolean bIncludingEnvironmentVariables) { 233 if (path == null) { 234 return path; 235 } 236 237 int size = path.length(); 242 StringBuffer result = new StringBuffer (size); 243 StringBuffer propName = null; 244 String propVal; 245 boolean foundOne = false; 248 char c; 249 for (int i = 0; i < size; i++) { 250 c = path.charAt(i); 251 switch(c) { 252 case '$': { 253 if (i < size - 1 && path.charAt(i + 1) == '{') { 254 foundOne = true; 256 i++; 257 if (propName == null) { propName = new StringBuffer (); 259 break; 260 } else { fatalError( 262 "enterprise_util.path_resolver_missing_closing_delim", 263 path); 264 return path; } 266 } else { 267 appendChar(c, propName, result); 268 } 269 break; 270 } case '}': { 271 if (foundOne) { if (propName != null) { 273 propVal = getPropertyValue(propName.toString(), bIncludingEnvironmentVariables); 274 if (propVal != null) { 275 result.append(propVal.replace(File.separatorChar, '/')); 278 } else { 279 result.append("${" + propName + "}"); 286 } 287 propName = null; 288 } else { fatalError( 290 "enterprise_util.path_resolver_missing_starting_delim", 291 path); 292 return path; } 294 } else { 295 appendChar(c, propName, result); 296 } 297 break; 298 } default : { 299 appendChar(c, propName, result); 300 break; 301 } 302 } 303 } 304 305 if (propName != null) { fatalError( 307 "enterprise_util.path_resolver_missing_closing_delim", 308 path); 309 return path; } 311 312 return result.toString(); 313 } 314 315 318 public boolean isResolvable(String path, boolean bIncludingEnvironmentVariables) { 319 String resolved = resolve(path, bIncludingEnvironmentVariables); 320 return (resolved.indexOf("${")<0); 321 } 322 323 public static void main(String [] args) { 324 if (args[0].equalsIgnoreCase("unresolve")) { 325 for (int i = 2; i < args.length; i++) { 326 String result = unresolvePath(args[i], new String [] {args[1]}); 327 System.out.println(args[i] + " " + result + " " + resolvePath(result)); 328 } 329 } else { 330 for (int i = 0; i < args.length; i++) { 331 System.out.println(args[i] + " " + resolvePath(args[i])); 332 } 333 } 334 } 335 355 public static String getRealPasswordFromAlias(final String at) throws 356 KeyStoreException , CertificateException , IOException , NoSuchAlgorithmException , 357 UnrecoverableKeyException { 358 try { 359 if (at == null || RelativePathResolver.getAlias(at) == null) { 360 return ( at ); 361 } 362 } catch (final Exception e) { return (at); 364 } 365 final String an = RelativePathResolver.getAlias(at); 366 final String sp = IdentityManager.getMasterPassword(); 367 final PasswordAdapter pa = new PasswordAdapter(sp.toCharArray()); final boolean exists = pa.aliasExists(an); 369 if (!exists) { 370 final StringManager lsm = StringManager.getManager(RelativePathResolver.class); 371 final String msg = lsm.getString("no_such_alias", an, at); 372 throw new IllegalArgumentException (msg); 373 } 374 final String real = pa.getPasswordForAlias(an); 375 return ( real ); 376 } 377 } 378 | Popular Tags |