1 23 package com.sun.enterprise.util; 24 25 import java.util.*; 26 import java.io.*; 27 import com.sun.enterprise.repository.*; 28 import com.sun.enterprise.*; 29 30 import java.util.logging.*; 32 import com.sun.logging.*; 33 35 36 public class LegacyConfigConverter { 37 38 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 40 42 private static final String OLD_JDBC_DRIVER_KEY = "jdbc.drivers"; 43 private static final String OLD_JDBC_DATASOURCES_KEY = "jdbc.datasources"; 44 private static final String OLD_JDBC_XA_DATASOURCES_KEY = 45 "jdbc20.datasources"; 46 private static final String OLD_JDBC_DELIMITER = "|"; 47 private static final String KEY_DELIMITER = "."; 48 private static final String OLD_XA_DATASOURCE_PREFIX = "xadatasource"; 49 private static final String OLD_XA_JNDI_NAME = "jndiname"; 50 private static final String OLD_XA_CLASSNAME = "classname"; 51 private static final String OLD_XA_DB_USER = "dbuser"; 52 private static final String OLD_XA_DB_PASSWORD = "dbpassword"; 53 private static final String OLD_PROP_INDICATOR = "prop"; 54 55 56 private static final boolean debug = com.sun.enterprise.util.logging.Debug.enabled; 59 61 public static void main(String args[]) { 62 if( args.length == 2 ) { 63 String defaultPropsFilename = args[0]; 64 String destinationPropsFilename = args[1]; 65 LegacyConfigConverter conv = new LegacyConfigConverter(); 66 72 if (_logger.isLoggable(Level.FINE)) { 74 _logger.log(Level.FINE,"Reading old jdbc properties from " 75 + defaultPropsFilename); 76 _logger.log(Level.FINE,"Destination file is " 77 + destinationPropsFilename); 78 } 79 81 conv.convert(defaultPropsFilename, 82 destinationPropsFilename); 83 84 } else { 85 System.out.println("Usage : <old_props_file> <new_props_file>"); 86 System.out.println("For example : " + "/a/b/default.properties " 87 + "/a/b/resource.properties"); 88 } 89 } 90 91 private J2EEResourceFactory resourceFactory_; 92 93 public LegacyConfigConverter() { 94 } 95 96 public void convert(String oldFile, String newFile) { 97 98 InputStream inputStream = null; 99 100 try { 101 File defaultPropsFile = new File(oldFile); 102 FileInputStream fis = new FileInputStream(defaultPropsFile); 103 inputStream = new BufferedInputStream(fis); 104 Properties defaultProps = new Properties(); 105 defaultProps.load(inputStream); 106 107 resourceFactory_ = ServerConfiguration.getJ2EEResourceFactory(); 108 109 J2EEResourceCollection resources = 110 resourceFactory_.loadResourceCollection(newFile); 111 112 convertJdbcDrivers(defaultProps, resources); 114 convertJdbcResources(defaultProps, resources); 115 convertJdbcXAResources(defaultProps, resources); 116 117 resourceFactory_.storeResourceCollection(resources, newFile); 118 119 122 if (_logger.isLoggable(Level.FINE)) { 124 _logger.log(Level.FINE,"Conversion complete. " + oldFile + " has not been changed. Please remove any obsolete jdbc properties if it will be used as a default.properties file in j2ee"); 125 } 126 128 } catch(Exception e) { 129 132 _logger.log(Level.SEVERE,"enterprise_util.excep_legacyconfigconverter_convert",e); 134 if( inputStream != null ) { 136 try { 137 inputStream.close(); 138 } catch(Exception ce) {} 139 } 140 } 141 } 142 143 private void convertJdbcDrivers(Properties defaultProps, 144 J2EEResourceCollection resources) 145 throws Exception { 146 147 String drivers = defaultProps.getProperty(OLD_JDBC_DRIVER_KEY); 148 149 if( drivers == null ) { 150 153 _logger.log(Level.FINE,"No Jdbc Drivers..."); 155 return; 157 } 158 159 StringTokenizer tokenizer = 160 new StringTokenizer(drivers, OLD_JDBC_DELIMITER); 161 162 while(tokenizer.hasMoreTokens()) { 163 String nextDriverClass = tokenizer.nextToken(); 164 J2EEResource driver = 165 resourceFactory_.createResource(J2EEResource.JDBC_DRIVER, 166 nextDriverClass); 167 resources.addResource(driver); 168 if( debug ) { 169 172 if (_logger.isLoggable(Level.FINE)) 174 _logger.log(Level.FINE,"Adding " + driver); 175 } 177 } 178 } 179 180 private void convertJdbcResources(Properties defaultProps, 181 J2EEResourceCollection resources) 182 throws Exception { 183 184 String jdbcResources = defaultProps.getProperty 185 (OLD_JDBC_DATASOURCES_KEY); 186 187 if( jdbcResources == null ) { 188 191 _logger.log(Level.FINE,"No Jdbc Resources..."); 193 return; 195 } 196 197 StringTokenizer tokenizer = 198 new StringTokenizer(jdbcResources, OLD_JDBC_DELIMITER); 199 200 while(tokenizer.hasMoreTokens()) { 201 String jndiName = tokenizer.nextToken(); 202 String url = tokenizer.nextToken(); 203 204 JdbcResource jdbcResource = (JdbcResource) 205 resourceFactory_.createResource(J2EEResource.JDBC_RESOURCE, 206 jndiName); 207 resources.addResource(jdbcResource); 209 if( debug ) { 210 213 if (_logger.isLoggable(Level.FINE)) 215 _logger.log(Level.FINE,"Adding " + jdbcResource); 216 } 218 } 219 } 220 221 private void convertJdbcXAResources(Properties defaultProps, 222 J2EEResourceCollection resources) 223 throws Exception { 224 225 String xaResources = defaultProps.getProperty 226 (OLD_JDBC_XA_DATASOURCES_KEY); 227 228 if( xaResources == null ) { 229 232 _logger.log(Level.FINE,"No XA Jdbc Resources..."); 234 return; 236 } 237 238 StringTokenizer tokenizer = 239 new StringTokenizer(xaResources, OLD_JDBC_DELIMITER); 240 241 int xaDriverCount = 0; 242 Hashtable xaDriverResources = new Hashtable(); 243 244 while(tokenizer.hasMoreTokens()) { 245 String name = tokenizer.nextToken(); 246 247 String xaJndiName = tokenizer.nextToken(); 248 249 JdbcXAResource jdbcXAResource = (JdbcXAResource) 250 resourceFactory_.createResource(J2EEResource.JDBC_XA_RESOURCE, 251 name); 252 253 xaDriverResources.put(xaJndiName, jdbcXAResource); 254 resources.addResource(jdbcXAResource); 255 256 xaDriverCount++; 257 } 258 259 for(int driverIndex = 0; driverIndex < xaDriverCount; driverIndex++) { 260 String propPrefix = OLD_XA_DATASOURCE_PREFIX + KEY_DELIMITER + 261 driverIndex + KEY_DELIMITER; 262 String xaJndiName = defaultProps.getProperty 263 (propPrefix + OLD_XA_JNDI_NAME); 264 265 JdbcXAResource jdbcXAResource = (JdbcXAResource) 266 xaDriverResources.get(xaJndiName); 267 268 String className = defaultProps.getProperty 269 (propPrefix + OLD_XA_CLASSNAME); 270 String dbuser = defaultProps.getProperty 271 (propPrefix + OLD_XA_DB_USER); 272 String dbPassword = defaultProps.getProperty 273 (propPrefix + OLD_XA_DB_PASSWORD); 274 275 jdbcXAResource.setClassname(className); 276 279 setJdbcXAProps(defaultProps, jdbcXAResource, driverIndex); 280 281 if( debug ) { 282 285 if (_logger.isLoggable(Level.FINE)) 287 _logger.log(Level.FINE,"Adding " + jdbcXAResource); 288 } 290 } 291 } 292 293 private void setJdbcXAProps(Properties defaultProps, 294 JdbcXAResource resource, 295 int index) { 296 Set propSet = defaultProps.entrySet(); 297 String keyPrefix = OLD_XA_DATASOURCE_PREFIX + KEY_DELIMITER + 298 index + KEY_DELIMITER + OLD_PROP_INDICATOR + KEY_DELIMITER; 299 300 for(Iterator iter = propSet.iterator(); iter.hasNext(); ) { 301 Map.Entry next = (Map.Entry) iter.next(); 302 String key = (String ) next.getKey(); 303 String value = (String ) next.getValue(); 304 305 if( key.startsWith(keyPrefix) ) { 306 int propNameIndex = key.lastIndexOf(KEY_DELIMITER); 307 String propName = key.substring(propNameIndex + 1); 308 ResourceProperty rs = 309 new ResourcePropertyImpl(propName, value); 310 resource.addProperty(rs); 311 } 312 } 313 } 314 315 } 316 | Popular Tags |