1 45 package org.exolab.jms.net.util; 46 47 import java.io.IOException ; 48 import java.io.File ; 49 import java.util.Map ; 50 import java.util.HashMap ; 51 import java.util.Properties ; 52 53 54 60 public class SSLUtil { 61 62 69 public static Map getHTTPSProperties(String keystore, String password) 70 throws IOException { 71 String path = getKeyStorePath(keystore); 72 Map result = new HashMap (); 73 result.put("org.exolab.jms.net.https.keyStore", path); 74 result.put("org.exolab.jms.net.https.keyStorePassword", password); 75 result.put("org.exolab.jms.net.https.trustStore", path); 76 return result; 77 } 78 79 86 public static Map getTCPSProperties(String keystore, String password) 87 throws IOException { 88 String path = getKeyStorePath(keystore); 89 Map result = new HashMap (); 90 result.put("org.exolab.jms.net.tcps.keyStore", path); 91 result.put("org.exolab.jms.net.tcps.keyStorePassword", password); 92 result.put("org.exolab.jms.net.tcps.trustStore", path); 93 return result; 94 } 95 96 105 public static SSLProperties getSSLProperties(String keystore, 106 String password) 107 throws IOException { 108 String path = getKeyStorePath(keystore); 109 SSLProperties result = new SSLProperties(); 110 result.setKeyStore(path); 111 result.setKeyStorePassword(password); 112 result.setTrustStore(path); 113 return result; 114 } 115 116 122 public static String getKeyStorePath(String name) throws IOException { 123 File keystore = new File (name); 124 if (!keystore.isAbsolute() || !keystore.exists()) { 125 final String paths[] = {"modules", "net", "target"}; 126 String workingDir = System.getProperty("user.dir"); 127 for (int i = 0; i < paths.length; ++i) { 128 if (workingDir.indexOf(paths[i]) == -1) { 129 if (!workingDir.endsWith(File.separator)) { 130 workingDir += File.separator; 131 } 132 workingDir += paths[i]; 133 } 134 } 135 keystore = new File (workingDir + File.separator + name); 136 } 137 if (!keystore.exists()) { 138 throw new IOException ("Failed to locate keystore: " + keystore); 139 } 140 return keystore.getPath(); 141 } 142 143 147 public static void clearProperties() { 148 Properties properties = System.getProperties(); 149 properties.remove(SSLHelper.KEY_STORE); 150 properties.remove(SSLHelper.KEY_STORE_PASSWORD); 151 properties.remove(SSLHelper.KEY_STORE_TYPE); 152 properties.remove(SSLHelper.TRUST_STORE); 153 properties.remove(SSLHelper.TRUST_STORE_PASSWORD); 154 properties.remove(SSLHelper.TRUST_STORE_TYPE); 155 } 156 157 158 } 159 | Popular Tags |