1 25 26 package org.objectweb.easybeans.deployer; 27 28 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2; 29 30 import java.io.File ; 31 import java.io.FileNotFoundException ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.net.URL ; 35 import java.util.ArrayList ; 36 import java.util.List ; 37 import java.util.jar.JarFile ; 38 39 import javax.enterprise.deploy.shared.ModuleType ; 40 41 import org.objectweb.easybeans.api.EZBArchive; 42 import org.objectweb.easybeans.api.EZBContainer; 43 import org.objectweb.easybeans.api.EZBContainerException; 44 import org.objectweb.easybeans.container.archive.ArchiveManager; 45 import org.objectweb.easybeans.loader.EasyBeansClassLoader; 46 import org.objectweb.easybeans.log.JLog; 47 import org.objectweb.easybeans.log.JLogFactory; 48 import org.objectweb.easybeans.persistence.PersistenceUnitManager; 49 import org.objectweb.easybeans.persistence.xml.PersistenceXmlFileAnalyzer; 50 import org.objectweb.easybeans.persistence.xml.PersistenceXmlFileAnalyzerException; 51 import org.objectweb.easybeans.server.Embedded; 52 import org.objectweb.easybeans.util.files.FileUtils; 53 import org.objectweb.easybeans.util.files.FileUtilsException; 54 import org.objectweb.easybeans.util.url.URLUtilsException; 55 56 60 public final class Deployer implements IDeployer { 61 62 65 private Embedded embedded = null; 66 67 70 private static JLog logger = JLogFactory.getLog(Deployer.class); 71 72 75 private static final String DEFAULT_FOLDER = "EasyBeans-Deployer"; 76 77 81 public Deployer(final Embedded embedded) { 82 this.embedded = embedded; 83 } 84 85 93 public String deployFile(final Integer typeparam, final java.lang.Byte [] bfile, final String filename) { 94 logger.info("Trying to deploy file with param {0}, filename = {1}", typeparam, filename); 95 96 int type = typeparam.intValue(); 97 98 String returnedPath = null; 99 100 File rootFolder = new File (System.getProperty("java.io.tmpdir") + File.separator + DEFAULT_FOLDER); 101 rootFolder.mkdirs(); 102 103 File tmpFolder = new File (rootFolder, "TMP"); 105 tmpFolder.mkdirs(); 106 File file = new File (tmpFolder, filename); 107 108 FileOutputStream out = null; 109 try { 110 out = new FileOutputStream (file); 111 } catch (FileNotFoundException e) { 112 throw new IllegalStateException ("Cannot build an outputstream on file '" + file + "'., e"); 113 } 114 byte[] bfileused = new byte[bfile.length]; 115 for (int i = 0; i < bfile.length; i++) { 116 bfileused[i] = bfile[i].byteValue(); 117 } 118 try { 119 out.write(bfileused); 120 } catch (IOException e) { 121 throw new IllegalStateException ("Cannot write byte in outputstream", e); 122 } 123 try { 124 out.close(); 125 } catch (IOException e) { 126 throw new IllegalStateException ("Cannot close outpustream", e); 127 } 128 129 if (type == ModuleType.EAR.getValue()) { 130 JarFile jarFile = null; 131 try { 132 jarFile = new JarFile (file); 133 } catch (IOException e) { 134 throw new IllegalStateException ("Cannot build a Jar file on file '" + file + "'.", e); 135 } 136 File appsFolder = new File (rootFolder, "APPS"); 137 appsFolder.mkdirs(); 138 File dest = new File (appsFolder, filename); 139 try { 140 FileUtils.unpack(jarFile, dest); 141 } catch (FileUtilsException e) { 142 throw new IllegalStateException ("Cannot unpack file '" + jarFile + "'.", e); 143 } 144 logger.info("Unpack of ear {0} in folder {1}", file, dest); 145 try { 146 jarFile.close(); 147 } catch (IOException e) { 148 throw new IllegalStateException ("Cannot close jarFile '" + jarFile + "'.", e); 149 } 150 returnedPath = dest.getAbsolutePath(); 151 } 152 153 if (returnedPath == null) { 154 throw new IllegalStateException ("Returned path should not be null"); 155 } 156 logger.info("Returning path = {0}", returnedPath); 157 return returnedPath; 158 } 159 160 164 public void deployEar(final String fileName) { 165 logger.info("Deploying ear {0}", fileName); 166 167 File earFile = new File (fileName); 169 if (!earFile.isDirectory()) { 170 throw new IllegalArgumentException ("Handle only EAR directories"); 171 } 172 173 List <File > libraries = new ArrayList <File >(); 175 176 List <File > ejbFiles = new ArrayList <File >(); 178 File [] files = earFile.listFiles(); 179 if (files != null) { 180 for (File f : files) { 181 if (f.getName().toLowerCase().endsWith("_ejb.jar")) { 182 ejbFiles.add(f); 183 } 184 185 if (f.isDirectory() && f.getName().equals("lib")) { 187 File [] libs = f.listFiles(); 189 if (libs != null) { 190 for (File lib : libs) { 191 libraries.add(lib); 192 } 193 } 194 } 195 } 196 } 197 198 URL [] urls = new URL [libraries.size() + ejbFiles.size()]; 200 int u = 0; 201 for (File library : libraries) { 202 try { 203 urls[u++] = fileToURL2(library); 204 } catch (URLUtilsException e) { 205 throw new IllegalStateException ("Cannot transform file '" + library + "' into an URL.", e); 206 } 207 } 208 for (File ejb : ejbFiles) { 209 try { 210 urls[u++] = fileToURL2(ejb); 211 } catch (URLUtilsException e) { 212 throw new IllegalStateException ("Cannot transform file '" + ejb + "' into an URL.", e); 213 } 214 } 215 ClassLoader appClassLoader = new EasyBeansClassLoader(urls, Thread.currentThread().getContextClassLoader()); 216 217 PersistenceUnitManager persistenceUnitManager = null; 219 for (File library : libraries) { 220 PersistenceUnitManager builtPersistenceUnitManager = null; 221 try { 222 EZBArchive archive = ArchiveManager.getInstance().getArchive(library); 223 builtPersistenceUnitManager = PersistenceXmlFileAnalyzer.analyzePersistenceXmlFile(archive, appClassLoader); 224 } catch (PersistenceXmlFileAnalyzerException e) { 225 throw new IllegalStateException ("Failure when analyzing the persistence.xml file", e); 226 } 227 if (persistenceUnitManager != null) { 228 throw new IllegalStateException ("There was an existing persistence.xml in another library of this EAR." 229 + " This is not supported."); 230 } 231 persistenceUnitManager = builtPersistenceUnitManager; 232 } 233 234 235 if (ejbFiles.size() == 0) { 236 throw new IllegalStateException ("No EJB-JAR file was found in the ear file."); 237 } 238 239 List <EZBContainer> containers = new ArrayList <EZBContainer>(); 241 for (File ejbFile : ejbFiles) { 242 containers.add(createContainer(ejbFile.getAbsolutePath())); 243 } 244 245 for (EZBContainer container : containers) { 247 container.setClassLoader(appClassLoader); 249 container.setPersistenceUnitManager(persistenceUnitManager); 251 } 252 253 254 255 for (EZBContainer container : containers) { 257 try { 258 container.start(); 259 } catch (EZBContainerException e) { 260 logger.error("Cannot start container {0}", container.getName(), e); 261 } 262 } 263 264 } 265 266 270 public void deployEjbJar(final String fileName) { 271 logger.info("Deploying EJB JAR {0}", fileName); 272 EZBContainer container = createContainer(fileName); 273 try { 274 container.start(); 275 } catch (EZBContainerException e) { 276 logger.error("Cannot start container {0}", container.getName(), e); 277 } 278 } 279 280 285 private EZBContainer createContainer(final String fileName) { 286 File f = new File (fileName); 287 EZBArchive archive = ArchiveManager.getInstance().getArchive(f); 288 return embedded.createContainer(archive); 289 } 290 291 } 292 | Popular Tags |