From: Jason D. <us...@us...> - 2002-05-16 04:12:09
|
User: user57 Date: 02/05/15 21:12:09 Modified: src/main/org/jboss/deployment SARDeployer.java Log: o abstarcted out the file listing logic o cleaned up some exception logging and nesting o Using Strings.toURL() o Using Streams.copyb() instead of custom stream copy Revision Changes Path 1.14 +78 -170 jboss-system/src/main/org/jboss/deployment/SARDeployer.java Index: SARDeployer.java =================================================================== RCS file: /cvsroot/jboss/jboss-system/src/main/org/jboss/deployment/SARDeployer.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- SARDeployer.java 12 May 2002 23:51:26 -0000 1.13 +++ SARDeployer.java 16 May 2002 04:12:09 -0000 1.14 @@ -9,23 +9,17 @@ package org.jboss.deployment; - - - - - - - - import java.io.File; import java.io.FileOutputStream; -import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; + import java.lang.reflect.Method; + import java.net.MalformedURLException; import java.net.URL; + import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; @@ -40,6 +34,7 @@ import java.util.StringTokenizer; import java.util.jar.JarEntry; import java.util.jar.JarFile; + import javax.management.InstanceNotFoundException; import javax.management.MBeanException; import javax.management.MBeanServer; @@ -48,16 +43,23 @@ import javax.management.ReflectionException; import javax.management.RuntimeErrorException; import javax.management.RuntimeMBeanException; + import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; + import org.jboss.deployment.DeploymentInfo; + import org.jboss.system.Service; import org.jboss.system.ServiceControllerMBean; import org.jboss.system.ServiceMBeanSupport; import org.jboss.system.server.ServerConfig; import org.jboss.system.server.ServerConfigLocator; + import org.jboss.util.jmx.MBeanProxy; +import org.jboss.util.stream.Streams; +import org.jboss.util.Strings; + import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -74,7 +76,7 @@ * @author <a href="mailto:Dav...@or...">David Maplesden</a> * @author <a href="mailto:d_j...@us...">David Jencks</a> * @author <a href="mailto:ja...@pl...">Jason Dillon</a> - * @version $Revision: 1.13 $ + * @version $Revision: 1.14 $ * * <p><b>20010830 marc fleury:</b> * <ul> @@ -136,11 +138,10 @@ // Public -------------------------------------------------------- /** - * Gets the FilenameFilter that the AutoDeployer uses to decide which files - * will be deployed by the ServiceDeployer. Currently .sar, and files - * ending in service.xml are accepted. + * Returns true if this deployer can deploy the given DeploymentInfo. * - * @return The FileNameFilter for use by the AutoDeployer. + * @return True if this deployer can deploy the given DeploymentInfo. + * * @jmx:managed-operation */ public boolean accepts(DeploymentInfo di) @@ -187,7 +188,6 @@ // Get the document parseDocument(di); - // In case there is a dependent classpath defined parse it // This creates parseXMLClasspath(di); @@ -217,7 +217,6 @@ } catch (Exception e) { - log.error("Problem in init", e); throw new DeploymentException(e); } @@ -317,9 +316,8 @@ } catch (Exception e) { - log.error("create operation failed for package " + di.url, e); destroy(di); - throw new DeploymentException(e); + throw new DeploymentException("create operation failed for package " + di.url, e); } } @@ -346,15 +344,33 @@ } } catch (Exception e) { - log.error("start operation failed on package " + di.url, e); stop(di); destroy(di); - throw new DeploymentException(e); + throw new DeploymentException("start operation failed on package " + di.url, e); } } + + protected File[] listFiles(final String urlspec) throws Exception + { + URL url = Strings.toURL(urlspec); + + // url is already canonical thanks to Strings.toURL + File dir = new File(url.getFile()); + + File[] files = dir.listFiles(new java.io.FileFilter() + { + public boolean accept(File pathname) + { + String name = pathname.getName().toLowerCase(); + return (name.endsWith(".jar") || name.endsWith(".zip")); + } + }); + + return files; + } protected void parseXMLClasspath(DeploymentInfo di) - throws DeploymentException + throws Exception { Set classpath = new HashSet(); @@ -388,12 +404,7 @@ if (!(codebase.startsWith("http:") || codebase.startsWith("file:"))) { // put the jboss/system base in front of it - try { - codebase = new URL(installURL, codebase).toString(); - } - catch (MalformedURLException e) { - throw new DeploymentException(e); - } + codebase = new URL(installURL, codebase).toString(); } // Let's make sure the formatting of the codebase ends with the / @@ -414,54 +425,17 @@ if (codebase.startsWith("file:") && archives.equals("*")) { - try - { - URL fileURL = new URL(codebase); - File dir = new File(fileURL.getFile()); - // The patchDir can only be a File one, local - File[] jars = dir.listFiles( - new java.io.FileFilter() - { - /** - * filters for jar and zip files in the local directory. - * - * @param pathname Path to the candidate file. - * @return True if the file is a jar or zip - * file. - */ - public boolean accept(File pathname) - { - String name2 = pathname.getName(); - return - (name2.endsWith(".jar") || name2.endsWith(".zip")); - } - }); - - for (int j = 0; jars != null && j < jars.length; j++) - { - classpath.add(jars[j].getCanonicalFile().toURL()); - } - } - catch (Exception e) + File[] jars = listFiles(codebase); + + for (int j = 0; jars != null && j < jars.length; j++) { - log.error("problem listing files in directory", e); - throw new DeploymentException("problem listing files in directory", e); + classpath.add(jars[j].getCanonicalFile().toURL()); } } // A directory that is to be added to the classpath else if(codebase.startsWith("file:") && archives.equals("")) { - try - { - URL fileURL = new URL(codebase); - File dir = new File(fileURL.getFile()); - classpath.add(dir.getCanonicalFile().toURL()); - } - catch(Exception e) - { - log.error("Failed to add classpath dir", e); - throw new DeploymentException("Failed to add classpath dir", e); - } + classpath.add(Strings.toURL(codebase)); } // We have an archive whatever the codebase go ahead and load the libraries else if (!archives.equals("")) @@ -479,56 +453,30 @@ ("No wildcard permitted in non-file URL deployment you must specify individual jars"); } - try - { - URL fileURL = new URL(codebase); - File dir = new File(fileURL.getFile()); - // The patchDir can only be a File one, local - File[] jars = dir.listFiles( - new java.io.FileFilter() - { - /** - * filters for jar and zip files in the local directory. - * - * @param pathname Path to the candidate file. - * @return True if the file is a jar or zip - * file. - */ - public boolean accept(File pathname) - { - String name2 = pathname.getName(); - return name2.endsWith(".jar") || name2.endsWith(".zip"); - } - }); - - for (int j = 0; jars != null && j < jars.length; j++) - { - classpath.add(jars[j].getCanonicalFile().toURL()); - } - } - catch (Exception e) + File[] jars = listFiles(codebase); + + for (int j = 0; jars != null && j < jars.length; j++) { - log.error("problem listing files in directory", e); - throw new DeploymentException("problem listing files in directory", e); + classpath.add(jars[j].getCanonicalFile().toURL()); } } else // A real archive listing (as opposed to wildcard) { StringTokenizer jars = new StringTokenizer(archives, ","); - //iterate through the packages in archives + + // iterate through the packages in archives while (jars.hasMoreTokens()) { // The format is simple codebase + jar + String archive = codebase + jars.nextToken().trim(); try { - String archive = codebase + jars.nextToken().trim(); - URL archiveURL = new URL(archive); - classpath.add(archiveURL); + classpath.add(Strings.toURL(archive)); } catch (MalformedURLException mfue) { - log.error("couldn't resolve package reference: ", mfue); + log.error("couldn't resolve package reference: " + archive , mfue); } // end of try-catch } } @@ -555,19 +503,12 @@ neededUrl = (URL)jars.next(); // Call the main deployer with it - try - { - // Create a new Deployment not as a subdeployment, - // An external package is not a "subdeployment" it is a stand alone - // deployment scanned as such - DeploymentInfo sub = new DeploymentInfo(neededUrl, null); - mainDeployer.deploy(sub); - } - catch (Exception e) - { - log.error("operation failed", e); - } + // Create a new Deployment not as a subdeployment, + // An external package is not a "subdeployment" it is a stand alone + // deployment scanned as such + DeploymentInfo sub = new DeploymentInfo(neededUrl, null); + mainDeployer.deploy(sub); log.debug("deployed classes for " + neededUrl); } @@ -745,49 +686,30 @@ } - /** - * Supply our default object name - * - * @param server Our mbean server. - * @param name Our proposed object name. - * @return Our actual object name - * - * @throws Exception Thrown if we are supplied an invalid name. - */ - public ObjectName preRegister(MBeanServer server, ObjectName name) - throws Exception + protected ObjectName getObjectName(MBeanServer server, ObjectName name) + throws MalformedObjectNameException { - super.preRegister(server, name); - log.debug("ServiceDeployer preregistered with mbean server"); return name == null ? OBJECT_NAME : name; } - protected void parseDocument(DeploymentInfo di) - throws DeploymentException + throws Exception { - try - { - DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - InputStream stream = null; - - // If we are in a xml only get the URL - if (di.isXML) - stream = di.localUrl.openStream(); - // Else load from the jar or directory - else - stream = di.localCl.getResourceAsStream("META-INF/jboss-service.xml"); - // Validate that the stream is not null - if (stream == null) - throw new DeploymentException("Failed to find META-INF/jboss-service.xml"); - - InputSource is = new InputSource(stream); - di.document = parser.parse(is); - } - catch (Exception e) - { - throw new DeploymentException("Exception getting document", e); - } + DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + InputStream stream = null; + + // If we are in a xml only get the URL + if (di.isXML) + stream = di.localUrl.openStream(); + // Else load from the jar or directory + else + stream = di.localCl.getResourceAsStream("META-INF/jboss-service.xml"); + // Validate that the stream is not null + if (stream == null) + throw new DeploymentException("Failed to find META-INF/jboss-service.xml"); + + InputSource is = new InputSource(stream); + di.document = parser.parse(is); } @@ -825,7 +747,6 @@ (JarURLConnection)jarUrl.openConnection(); JarFile jarFile = jarConnection.getJarFile(); */ - String filename = url.getFile(); JarFile jarFile = new JarFile(filename); @@ -835,31 +756,18 @@ { JarEntry entry = (JarEntry)e.nextElement(); String name = entry.getName(); + if (path == null || name.startsWith(path)) { File outFile = new File(destDir, name); if (!outFile.exists()) { - if (entry.isDirectory()) { outFile.mkdirs(); } else { - InputStream in = jarFile.getInputStream(entry); - OutputStream out = new FileOutputStream(outFile); - - try - { - byte[] buffer = new byte[1024]; - int read; - while ((read = in.read(buffer)) > 0) { - out.write(buffer, 0, read); - } - } - finally - { - in.close();out.close(); - } + Streams.copyb(jarFile.getInputStream(entry), + new FileOutputStream(outFile)); } } // end of if (outFile.exists()) } // end of if (matches path) |