1 45 package org.openejb.alt.config; 46 47 import org.exolab.castor.xml.MarshalException; 48 import org.exolab.castor.xml.ValidationException; 49 import org.openejb.OpenEJBException; 50 import org.openejb.alt.config.sys.Deployments; 51 import org.openejb.alt.config.sys.Openejb; 52 import org.openejb.loader.SystemInstance; 53 import org.openejb.util.Logger; 54 import org.openejb.util.Messages; 55 56 import java.io.File ; 57 import java.io.FileNotFoundException ; 58 import java.io.FileOutputStream ; 59 import java.io.FileReader ; 60 import java.io.FileWriter ; 61 import java.io.IOException ; 62 import java.io.InputStream ; 63 import java.io.OutputStream ; 64 import java.io.Reader ; 65 import java.io.Writer ; 66 import java.net.URL ; 67 import java.net.UnknownHostException ; 68 import java.util.Enumeration ; 69 import java.util.Properties ; 70 71 76 public class ConfigUtils { 77 78 public static Messages messages = new Messages("org.openejb.util.resources"); 79 public static Logger logger = Logger.getInstance("OpenEJB", "org.openejb.util.resources"); 80 81 public static Openejb readConfig() throws OpenEJBException { 82 return readConfig(searchForConfiguration()); 83 } 84 85 public static Openejb readConfig(String confFile) throws OpenEJBException { 86 File file = new File (confFile); 87 return (Openejb) Unmarshaller.unmarshal(Openejb.class, file.getName(), file.getParent()); 88 } 89 93 public static Openejb _readConfig(String confFile) throws OpenEJBException { 94 Openejb obj = null; 95 Reader reader = null; 96 try { 97 reader = new FileReader (confFile); 98 org.exolab.castor.xml.Unmarshaller unmarshaller = new org.exolab.castor.xml.Unmarshaller(Openejb.class); 99 unmarshaller.setWhitespacePreserve(true); 100 obj = (Openejb) unmarshaller.unmarshal(reader); 101 } catch (FileNotFoundException e) { 102 throw new OpenEJBException(messages.format("conf.1900", confFile, e.getLocalizedMessage())); 103 } catch (MarshalException e) { 104 if (e.getException() instanceof IOException ) { 105 throw new OpenEJBException(messages.format("conf.1110", confFile, e.getLocalizedMessage())); 106 } else if (e.getException() instanceof UnknownHostException ) { 107 throw new OpenEJBException(messages.format("conf.1121", confFile, e.getLocalizedMessage())); 108 } else { 109 throw new OpenEJBException(messages.format("conf.1120", confFile, e.getLocalizedMessage())); 110 } 111 } catch (ValidationException e) { 112 119 122 throw new OpenEJBException(messages.format("conf.1130", confFile, e.getLocalizedMessage())); 123 } 124 try { 125 reader.close(); 126 } catch (Exception e) { 127 throw new OpenEJBException(messages.format("file.0020", confFile, e.getLocalizedMessage())); 128 } 129 return obj; 130 } 131 132 133 public static void writeConfig(String confFile, Openejb confObject) throws OpenEJBException { 134 140 Writer writer = null; 141 try { 142 File file = new File (confFile); 143 writer = new FileWriter (file); 144 confObject.marshal(writer); 145 } catch (IOException e) { 146 throw new OpenEJBException(messages.format("conf.1040", confFile, e.getLocalizedMessage())); 147 } catch (MarshalException e) { 148 if (e.getException() instanceof IOException ) { 149 throw new OpenEJBException(messages.format("conf.1040", confFile, e.getLocalizedMessage())); 150 } else { 151 throw new OpenEJBException(messages.format("conf.1050", confFile, e.getLocalizedMessage())); 152 } 153 } catch (ValidationException e) { 154 161 165 throw new OpenEJBException(messages.format("conf.1060", confFile, e.getLocalizedMessage())); 166 } 167 try { 168 writer.close(); 169 } catch (Exception e) { 170 throw new OpenEJBException(messages.format("file.0020", confFile, e.getLocalizedMessage())); 171 } 172 } 173 174 182 public static String searchForConfiguration() throws OpenEJBException { 183 return searchForConfiguration(System.getProperty("openejb.configuration")); 184 } 185 186 public static String searchForConfiguration(String path) throws OpenEJBException { 187 return ConfigUtils.searchForConfiguration(path, System.getProperties()); 188 } 189 190 public static String searchForConfiguration(String path, Properties props) throws OpenEJBException { 191 File file = null; 192 if (path != null) { 193 197 file = new File (path); 198 if (file != null && file.exists() && file.isFile()) { 199 return file.getAbsolutePath(); 200 } 201 202 205 try { 206 file = SystemInstance.get().getBase().getFile(path); 207 if (file != null && file.exists() && file.isFile()) { 208 return file.getAbsolutePath(); 209 } 210 } catch (FileNotFoundException ignored) { 211 } catch (IOException ignored) { 212 } 213 214 217 try { 218 file = SystemInstance.get().getHome().getFile(path); 219 if (file != null && file.exists() && file.isFile()) { 220 return file.getAbsolutePath(); 221 } 222 } catch (FileNotFoundException ignored) { 223 } catch (IOException ignored) { 224 } 225 226 } 227 228 logger.warning("Cannot find the configuration file [" + path + "], Trying conf/openejb.conf instead."); 229 230 try { 231 235 try { 236 file = SystemInstance.get().getBase().getFile("conf/openejb.conf"); 237 if (file != null && file.exists() && file.isFile()) { 238 return file.getAbsolutePath(); 239 } 240 } catch (java.io.FileNotFoundException e) { 241 } 242 243 247 try { 248 file = SystemInstance.get().getHome().getFile("conf/openejb.conf"); 249 if (file != null && file.exists() && file.isFile()) { 250 return file.getAbsolutePath(); 251 } 252 } catch (java.io.FileNotFoundException e) { 253 } 254 255 logger.warning("Cannot find the configuration file [conf/openejb.conf], Creating one."); 256 257 261 File confDir = SystemInstance.get().getBase().getDirectory("conf", true); 263 264 file = createConfig(new File (confDir, "openejb.conf")); 268 269 } catch (java.io.IOException e) { 270 e.printStackTrace(); 271 throw new OpenEJBException("Could not locate config file: ", e); 272 } 273 274 278 return (file == null) ? null : file.getAbsolutePath(); 279 } 280 281 public static File createConfig(File config) throws java.io.IOException { 282 InputStream in = null; 283 OutputStream out = null; 284 try { 285 URL defaultConfig = new URL ("resource:/default.openejb.conf"); 286 in = defaultConfig.openStream(); 287 out = new FileOutputStream (config); 288 289 int b; 290 while ((b = in.read()) != -1) { 291 out.write(b); 292 } 293 } finally { 294 if (in != null) { 295 in.close(); 296 } 297 if (out != null) { 298 out.close(); 299 } 300 301 } 302 return config; 303 } 304 305 public static boolean addDeploymentEntryToConfig(String jarLocation, Openejb config) { 306 Enumeration enumeration = config.enumerateDeployments(); 307 File jar = new File (jarLocation); 308 309 310 while (enumeration.hasMoreElements()) { 311 Deployments d = (Deployments) enumeration.nextElement(); 312 313 if (d.getJar() != null) { 314 try { 315 File target = SystemInstance.get().getBase().getFile(d.getJar(), false); 316 317 321 if (jar.equals(target)) return false; 322 } catch (java.io.IOException e) { 323 327 } 328 } else if (d.getDir() != null) { 329 try { 330 File target = SystemInstance.get().getBase().getFile(d.getDir(), false); 331 File jarDir = jar.getAbsoluteFile().getParentFile(); 332 333 339 if (jarDir != null && jarDir.equals(target)) return false; 340 } catch (java.io.IOException e) { 341 345 } 346 } 347 } 348 349 350 Deployments dep = new Deployments(); 351 dep.setJar(jarLocation); 352 config.addDeployments(dep); 353 return true; 354 } 355 } 356 | Popular Tags |