/* * JBoss, the OpenSource EJB server * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.deployment; import java.net.URL; import java.net.MalformedURLException; import java.net.URLClassLoader; import java.io.File; import java.io.FilenameFilter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Collection; import java.util.Iterator; import java.util.Hashtable; import java.util.Vector; import org.jboss.deployment.DeploymentException; import org.jboss.logging.Logger; /** This class is used by the J2eeDeployer to create, remove or find a particular * Deployment. It uses and instance of Installer to create a Deployment. * * @see org.jboss.deployment.Installer * @author Daniel Schulze * @author Bill Burke * @author Scott.Stark@jboss.org * @version $Revision: 1.3.6.5 $ */ public class InstallerFactory { // Constants ----------------------------------------------------- // Attributes ---------------------------------------------------- /** the deployment base directory (for the temporary files) */ protected File baseDir; /** the logger if there is something to say */ protected Logger log; public static FilenameFilter getDeployableFilter() { return new FilenameFilter() { public boolean accept(File dir, String filename) { boolean accept = false; // Check for unarchived packages File pkgDir = new File(filename); if( pkgDir.isDirectory() ) { for(int f = 0; f < Installer.files.length; f ++) { File dd = new File(pkgDir, Installer.files[f]); accept |= dd.exists(); } } filename = filename.toLowerCase(); // Check for package archives if( accept == false ) { accept = filename.endsWith(".jar") || filename.endsWith(".war") || filename.endsWith(".ear") || filename.endsWith(".zip"); } return accept; } }; } // Constructors -------------------------------------------------- /** Constructs a new InstallerFactory, only one is needed per J2eeDeployer * @param _tmpDir the temporary deployment directory * @param _log the Log for output */ public InstallerFactory(File _tmpDir, Logger _log) throws IOException { baseDir = _tmpDir.getCanonicalFile(); log = _log; } // Public -------------------------------------------------------- /** installs the J2ee component the URL points to and returns a Deployment object as its * representation. * @param src J2ee module (ejb/war/ear) to deploy * @return a Deployment object representing the deployment * @throws J2eeDeploymentException if the module is not installable for some reasons * (syntactical errors, ...?) * @throws IOException if a file operation (_src download jar file extraction) fails */ public Deployment install(URL src) throws J2eeDeploymentException, IOException { Installer installer = null; String protocol = src.getProtocol(); if( protocol.equals("file") ) { File srcDir = new File(src.getFile()); if( srcDir.isDirectory() == true ) installer = new LocalDirInstaller(); else installer = new LegacyInstaller(); } else { installer = new LegacyInstaller(); } return installer.execute(this, src); } /** uninstalls the files represented by the given Deployment. * @param _d Deployment to remove * @throws IOException if file deletion fails */ public void uninstall(Deployment _d) throws IOException { File appDir = new File(_d.localUrl.getFile()); deleteRecursive(appDir); } /** Finds all Deployments currently installed. * @return array of all found deployments */ public Deployment[] getDeployments() { Vector found = new Vector(); File[] files = baseDir.listFiles(); for (int i = 0, l = files.length; i delete deleteRecursive(parts[j]); } } } catch (IOException _ioe) { log.error("exception while uncluttering deployment "+files[i]+": ", _ioe); } } else { // is something we dont know -> we dont care about it... deleteRecursive(files[i]); } } } // Private ------------------------------------------------------- /** Deserializes the Deployments pointed to by the given File. * @param _file a serialized Deployment * @return the deserialized Deployment * @throws IOException when something went wrong */ private Deployment loadConfig(File _file) throws IOException { Deployment d = null; try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(_file)); d = (Deployment)in.readObject(); in.close(); } catch (ClassNotFoundException _snfe) {} // should never happen... return d; } /** Truncates the the name (the last cluster of letters after the last slash. * @param _url an URL or something like that */ private String getName(String _url) { String result = _url; if (result.endsWith("/")) result = result.substring(0, result.length() - 1); result = result.substring(result.lastIndexOf("/") + 1); return result; } /** deletes the given File recursive. * @param _file to delete * @throws IOException when something goes wrong */ private void deleteRecursive(File _file) throws IOException { if (_file.exists()) { if (_file.isDirectory()) { File[] files = _file.listFiles(); for (int i = 0, l = files.length; i < l; ++i) deleteRecursive(files[i]); } _file.delete(); } } }