1 45 package org.openejb.alt.config; 46 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.FileOutputStream ; 50 import java.io.FileWriter ; 51 import java.io.IOException ; 52 import java.io.Writer ; 53 import java.util.Properties ; 54 import java.util.Vector ; 55 import java.util.jar.JarEntry ; 56 import java.util.jar.JarFile ; 57 58 import org.exolab.castor.util.Configuration; 59 import org.exolab.castor.util.LocalConfiguration; 60 import org.exolab.castor.xml.MarshalException; 61 import org.exolab.castor.xml.ValidationException; 62 import org.openejb.OpenEJBException; 63 import org.openejb.alt.config.ejb11.EjbJar; 64 import org.openejb.alt.config.ejb11.EnterpriseBeansItem; 65 import org.openejb.alt.config.ejb11.OpenejbJar; 66 import org.openejb.alt.config.sys.Container; 67 import org.openejb.loader.SystemInstance; 68 import org.openejb.util.JarUtils; 69 import org.openejb.util.Logger; 70 import org.openejb.util.Messages; 71 72 76 public class EjbJarUtils { 77 78 public static final Messages messages = new Messages("org.openejb.util.resources"); 79 public static final Logger logger = Logger.getInstance("OpenEJB.startup", "org.openejb.util.resources"); 80 81 private final EjbJar ejbJar; 82 private final String jarLocation; 83 private OpenejbJar openejbJar; 84 85 static { 86 Properties properties = LocalConfiguration.getInstance().getProperties(); 87 properties.setProperty( Configuration.Property.Indent, "true"); 88 } 89 90 public EjbJarUtils(String jarLocation) throws OpenEJBException { 93 94 this.jarLocation = jarLocation; 95 this.ejbJar = readEjbJar(jarLocation); 96 try { 97 this.openejbJar = readOpenEjbJar(jarLocation); 98 } catch (OpenEJBException e) { 99 logger.warning("Reading openejb-jar.xml.",e); 100 } 101 } 102 103 111 private OpenejbJar readOpenEjbJar(String jarLocation) throws OpenEJBException { 112 return (OpenejbJar) Unmarshaller.unmarshal(OpenejbJar.class, "META-INF/openejb-jar.xml", jarLocation); 113 } 114 115 private EjbJar readEjbJar(String jarLocation) throws OpenEJBException { 116 return (EjbJar)Unmarshaller.unmarshal(EjbJar.class, "META-INF/ejb-jar.xml", jarLocation); 117 } 118 119 public String getJarLocation() { 120 return jarLocation; 121 } 122 123 public EjbJar getEjbJar() { 124 return ejbJar; 125 } 126 127 public OpenejbJar getOpenejbJar() { 128 return openejbJar; 129 } 130 131 public void setOpenejbJar(OpenejbJar openejbJar) { 132 this.openejbJar = openejbJar; 133 } 134 135 public void writeEjbJar(String xmlFile) throws OpenEJBException { 136 142 Writer writer = null; 143 try { 144 File file = new File (xmlFile); 145 writer = new FileWriter (file); 146 ejbJar.marshal(writer); 147 } catch (IOException e) { 148 throw new OpenEJBException(messages.format("conf.3040", xmlFile, e.getLocalizedMessage())); 149 } catch (MarshalException e) { 150 if (e.getException() instanceof IOException ) { 151 throw new OpenEJBException(messages.format("conf.3040", xmlFile, e.getLocalizedMessage())); 152 } else { 153 throw new OpenEJBException(messages.format("conf.3050", xmlFile, e.getLocalizedMessage())); 154 } 155 } catch (ValidationException e) { 156 163 167 throw new OpenEJBException(messages.format("conf.3060", xmlFile, e.getLocalizedMessage())); 168 } 169 try { 170 writer.close(); 171 } catch (Exception e) { 172 throw new OpenEJBException(messages.format("file.0020", xmlFile, e.getLocalizedMessage())); 173 } 174 } 175 176 public static String moveJar(String jar, boolean overwrite) throws OpenEJBException { 177 File origFile = new File (jar); 178 179 if (!origFile.exists()) { 181 handleException("deploy.m.010", origFile.getAbsolutePath()); 182 } 183 184 if (origFile.isDirectory()) { 185 handleException("deploy.m.020", origFile.getAbsolutePath()); 186 } 187 188 if (!origFile.isFile()) { 189 handleException("deploy.m.030", origFile.getAbsolutePath()); 190 } 191 192 String jarName = origFile.getName(); 194 File beansDir = null; 195 try { 196 beansDir = SystemInstance.get().getBase().getDirectory("beans"); 197 } catch (java.io.IOException ioe) { 198 throw new OpenEJBException(messages.format("deploy.m.040", origFile.getAbsolutePath(), ioe.getMessage())); 199 } 200 201 File newFile = new File (beansDir, jarName); 202 boolean moved = false; 203 204 try { 205 if (newFile.exists()) { 206 if (overwrite) { 207 newFile.delete(); 208 } else { 209 throw new OpenEJBException(messages.format("deploy.m.061", origFile.getAbsolutePath(), beansDir.getAbsolutePath())); 210 } 211 } 212 moved = origFile.renameTo(newFile); 213 } catch (SecurityException se) { 214 throw new OpenEJBException(messages.format("deploy.m.050", origFile.getAbsolutePath(), se.getMessage())); 215 } 216 217 if (!moved) { 218 throw new OpenEJBException(messages.format("deploy.m.060", origFile.getAbsolutePath(), newFile.getAbsoluteFile())); 219 } 220 return newFile.getAbsolutePath(); 221 } 222 223 public static String copyJar(String jar, boolean overwrite) throws OpenEJBException { 224 File origFile = new File (jar); 225 226 if (!origFile.exists()) { 228 handleException("deploy.c.010", origFile.getAbsolutePath()); 229 return jar; 230 } 231 232 if (origFile.isDirectory()) { 233 handleException("deploy.c.020", origFile.getAbsolutePath()); 234 return jar; 235 } 236 237 if (!origFile.isFile()) { 238 handleException("deploy.c.030", origFile.getAbsolutePath()); 239 return jar; 240 } 241 242 String jarName = origFile.getName(); 244 File beansDir = null; 245 try { 246 beansDir = SystemInstance.get().getBase().getDirectory("beans"); 247 } catch (java.io.IOException ioe) { 248 throw new OpenEJBException(messages.format("deploy.c.040", origFile.getAbsolutePath(), ioe.getMessage())); 249 } 250 251 File newFile = new File (beansDir, jarName); 252 253 try { 254 if (newFile.exists()) { 255 if (overwrite) { 256 newFile.delete(); 257 } else { 258 throw new OpenEJBException(messages.format("deploy.c.061", origFile.getAbsolutePath(), beansDir.getAbsolutePath())); 259 } 260 } 261 262 FileInputStream in = new FileInputStream (origFile); 263 FileOutputStream out = new FileOutputStream (newFile); 264 265 int b = in.read(); 266 while (b != -1) { 267 out.write(b); 268 b = in.read(); 269 } 270 271 in.close(); 272 out.close(); 273 274 } catch (SecurityException e) { 275 throw new OpenEJBException(messages.format("deploy.c.050", origFile.getAbsolutePath(), beansDir.getAbsolutePath(), e.getMessage())); 276 } catch (IOException e) { 277 handleException("deploy.c.060", origFile.getAbsolutePath(), newFile.getAbsolutePath(), e.getClass().getName(), e.getMessage()); 278 } 279 280 return newFile.getAbsolutePath(); 281 } 282 283 public static Container[] getUsableContainers(Container[] containers, Bean bean) { 284 Vector c = new Vector (); 285 286 for (int i = 0; i < containers.length; i++) { 287 if (containers[i].getCtype().equals(bean.getType())) { 288 c.add(containers[i]); 289 } 290 } 291 292 Container[] useableContainers = new Container[c.size()]; 293 c.copyInto(useableContainers); 294 295 return useableContainers; 296 } 297 298 public Bean[] getBeans() { 299 EnterpriseBeansItem[] items = ejbJar.getEnterpriseBeans().getEnterpriseBeansItem(); 300 Bean[] beans = new Bean[items.length]; 301 for (int i = 0; i < items.length; i++) { 302 if (items[i].getEntity() == null) { 303 beans[i] = new SessionBean(items[i].getSession()); 304 } else { 305 beans[i] = new EntityBean(items[i].getEntity()); 306 } 307 } 308 return beans; 309 } 310 311 312 313 314 public static void handleException(String errorCode, Object arg0, Object arg1, Object arg2, Object arg3) throws OpenEJBException { 315 throw new OpenEJBException(messages.format(errorCode, arg0, arg1, arg2, arg3)); 316 } 317 318 public static void handleException(String errorCode, Object arg0) throws OpenEJBException { 319 throw new OpenEJBException(messages.format(errorCode, arg0)); 320 } 321 322 public static void handleException(String errorCode) throws OpenEJBException { 323 throw new OpenEJBException(messages.message(errorCode)); 324 } 325 326 public static boolean checkForOpenejbJar(String jarFile) throws OpenEJBException { 327 328 JarFile jar = JarUtils.getJarFile(jarFile); 329 330 331 JarEntry entry = jar.getJarEntry("META-INF/openejb-jar.xml"); 332 if (entry == null) entry = jar.getJarEntry("openejb-jar.xml"); 333 if (entry == null) return false; 334 335 return true; 336 } 337 338 public static void writeOpenejbJar(String xmlFile, OpenejbJar openejbJarObject) throws OpenEJBException { 339 345 Writer writer = null; 346 try { 347 File file = new File (xmlFile); 348 File dirs = file.getParentFile(); 349 if (dirs != null) dirs.mkdirs(); 350 writer = new FileWriter (file); 351 openejbJarObject.marshal(writer); 352 } catch (SecurityException e) { 353 throw new OpenEJBException(messages.format("conf.2040", xmlFile, e.getLocalizedMessage())); 354 } catch (IOException e) { 355 throw new OpenEJBException(messages.format("conf.2040", xmlFile, e.getLocalizedMessage())); 356 } catch (MarshalException e) { 357 if (e.getException() instanceof IOException ) { 358 throw new OpenEJBException(messages.format("conf.2040", xmlFile, e.getLocalizedMessage())); 359 } else { 360 throw new OpenEJBException(messages.format("conf.2050", xmlFile, e.getLocalizedMessage())); 361 } 362 } catch (ValidationException e) { 363 370 374 throw new OpenEJBException(messages.format("conf.2060", xmlFile, e.getLocalizedMessage())); 375 } 376 try { 377 writer.close(); 378 } catch (Exception e) { 379 throw new OpenEJBException(messages.format("file.0020", xmlFile, e.getLocalizedMessage())); 380 } 381 } 382 } | Popular Tags |