From: Scott M S. <st...@us...> - 2002-08-01 08:04:26
|
User: starksm Date: 02/08/01 01:04:24 Modified: src/main/org/jboss/deployment/scanner Tag: Branch_3_0 URLDeploymentScanner.java Log: - The deploy method has to catch any exception thrown by the deployer and still add the deployment to the deployed set to avoid looping on bad deployments - At the end of the scan the deployer has to be queried for the current incomplete deployment because the last IncompleteDeploymentException seen may not be valid Revision Changes Path No revision No revision 1.13.2.3 +207 -166 jboss-system/src/main/org/jboss/deployment/scanner/URLDeploymentScanner.java Index: URLDeploymentScanner.java =================================================================== RCS file: /cvsroot/jboss/jboss-system/src/main/org/jboss/deployment/scanner/URLDeploymentScanner.java,v retrieving revision 1.13.2.2 retrieving revision 1.13.2.3 diff -u -r1.13.2.2 -r1.13.2.3 --- URLDeploymentScanner.java 24 Jun 2002 17:32:31 -0000 1.13.2.2 +++ URLDeploymentScanner.java 1 Aug 2002 08:04:24 -0000 1.13.2.3 @@ -15,7 +15,7 @@ import java.io.File; import java.io.FileFilter; - +import java.io.IOException; import java.util.List; import java.util.ArrayList; import java.util.LinkedList; @@ -29,22 +29,19 @@ import javax.management.ObjectName; import javax.management.MBeanServer; +import org.jboss.deployment.DeploymentSorter; +import org.jboss.deployment.IncompleteDeploymentException; import org.jboss.system.server.ServerConfig; import org.jboss.system.server.ServerConfigLocator; - import org.jboss.util.NullArgumentException; -import org.jboss.deployment.DeploymentSorter; -import org.jboss.deployment.IncompleteDeploymentException; - - /** - * A URL-based deployment scanner. Supports local directory + * A URL-based deployment scanner. Supports local directory * scanning for file-based urls. * * @jmx:mbean extends="org.jboss.deployment.scanner.DeploymentScannerMBean" * - * @version <tt>$Revision: 1.13.2.2 $</tt> + * @version <tt>$Revision: 1.13.2.3 $</tt> * @author <a href="mailto:ja...@pl...">Jason Dillon</a> */ public class URLDeploymentScanner @@ -53,21 +50,20 @@ { /** The list of URLs to scan. */ protected List urlList = Collections.synchronizedList(new ArrayList()); - + /** A set of scanned urls which have been deployed. */ protected Set deployedSet = Collections.synchronizedSet(new HashSet()); - + /** The server's home directory, for relative paths. */ protected File serverHome; - + /** HACK to sort urls from a scaned directory. */ protected Comparator sorter; /** Allow a filter for scanned directories */ protected FileFilter filter; - private IncompleteDeploymentException lastIncompleteDeploymentException; - + /** * @jmx:managed-attribute */ @@ -75,22 +71,24 @@ { if (list == null) throw new NullArgumentException("list"); - + boolean debug = log.isDebugEnabled(); - + // start out with a fresh list urlList.clear(); - + Iterator iter = list.iterator(); - while (iter.hasNext()) { + while (iter.hasNext()) + { URL url = (URL)iter.next(); if (url == null) throw new NullArgumentException("list element"); - + addURL(url); } - - if (debug) { + + if (debug) + { log.debug("URL list: " + urlList); } } @@ -116,7 +114,7 @@ public String getURLComparator() { if (sorter == null) - return null; + return null; return sorter.getClass().getName(); } @@ -141,10 +139,10 @@ public String getFilter() { if (filter == null) - return null; + return null; return filter.getClass().getName(); } - + /** * @jmx:managed-attribute */ @@ -153,7 +151,7 @@ // too bad, List isn't a cloneable return new ArrayList(urlList); } - + /** * @jmx:managed-operation */ @@ -161,13 +159,14 @@ { if (url == null) throw new NullArgumentException("url"); - + urlList.add(url); - if (log.isDebugEnabled()) { + if (log.isDebugEnabled()) + { log.debug("Added url: " + url); } } - + /** * @jmx:managed-operation */ @@ -175,13 +174,14 @@ { if (url == null) throw new NullArgumentException("url"); - + boolean success = urlList.remove(url); - if (success && log.isDebugEnabled()) { + if (success && log.isDebugEnabled()) + { log.debug("Removed url: " + url); } } - + /** * @jmx:managed-operation */ @@ -189,15 +189,15 @@ { if (url == null) throw new NullArgumentException("url"); - + return urlList.contains(url); } - - + + ///////////////////////////////////////////////////////////////////////// // Management/Configuration Helpers // ///////////////////////////////////////////////////////////////////////// - + /** * @jmx:managed-attribute */ @@ -205,62 +205,70 @@ { if (listspec == null) throw new NullArgumentException("listspec"); - + boolean debug = log.isDebugEnabled(); - + List list = new LinkedList(); - + StringTokenizer stok = new StringTokenizer(listspec, ","); - while (stok.hasMoreTokens()) { + while (stok.hasMoreTokens()) + { String urlspec = stok.nextToken().trim(); - - if (debug) { + + if (debug) + { log.debug("Adding URL from spec: " + urlspec); } - + URL url = makeURL(urlspec); - if (debug) { + if (debug) + { log.debug("URL: " + url); } list.add(url); } - + setURLList(list); } - + /** * A helper to make a URL from a full url, or a filespec. */ protected URL makeURL(final String urlspec) throws MalformedURLException { boolean trace = log.isTraceEnabled(); - + URL url; - - try { + + try + { url = new URL(urlspec); if (trace) log.trace("using raw url: " + url); } - catch (MalformedURLException e) { + catch (MalformedURLException e) + { File file = new File(urlspec); if (trace) log.trace("converted to file: " + file); - - if (!file.isAbsolute()) { + + if (!file.isAbsolute()) + { file = new File(serverHome, urlspec); if (trace) log.trace("made absolute: " + file); } - try { + try + { file = file.getCanonicalFile(); if (trace) log.trace("made canonical: " + file); } - catch (java.io.IOException x) { + catch (IOException x) + { throw new MalformedURLException("Invalid urlspec: " + urlspec); } - + url = file.toURL(); } - + return url; } @@ -271,7 +279,7 @@ { addURL(makeURL(urlspec)); } - + /** * @jmx:managed-operation */ @@ -279,7 +287,7 @@ { removeURL(makeURL(urlspec)); } - + /** * @jmx:managed-operation */ @@ -287,12 +295,12 @@ { return hasURL(makeURL(urlspec)); } - - + + ///////////////////////////////////////////////////////////////////////// // DeploymentScanner // ///////////////////////////////////////////////////////////////////////// - + /** * A container and help class for a deployed URL. * should be static at this point, with the explicit scanner ref, but I'm (David) lazy. @@ -302,12 +310,12 @@ public URL url; public URL watchUrl; public long deployedLastModified; - - public DeployedURL(final URL url) + + public DeployedURL(final URL url) { this.url = url; } - + public void deployed() { deployedLastModified = getLastModified(); @@ -316,70 +324,78 @@ { return url.getProtocol().equals("file"); } - + public File getFile() { return new File(url.getFile()); } - + public boolean isRemoved() { - if (isFile()) { + if (isFile()) + { File file = getFile(); return !file.exists(); } return false; } - - public long getLastModified() + + public long getLastModified() { if (watchUrl == null) { - try { + try + { Object o = getServer().invoke(getDeployer(), "getWatchUrl", - new Object[] { url }, - new String[] { URL.class.getName() }); + new Object[] { url }, + new String[] { URL.class.getName() }); watchUrl = o == null ? url : (URL)o; getLog().debug("Watch URL for: " + url + " -> " + watchUrl); - } catch (Exception e) { + } + catch (Exception e) + { watchUrl = url; getLog().debug("Unable to obtain watchUrl from deployer. " + "Use url: " + url, e); } } - try { + try + { URLConnection connection; - if (watchUrl != null) { - connection = watchUrl.openConnection(); - } else { - connection = url.openConnection(); + if (watchUrl != null) + { + connection = watchUrl.openConnection(); } - // no need to do special checks for files... - // org.jboss.net.protocol.file.FileURLConnection correctly + else + { + connection = url.openConnection(); + } + // no need to do special checks for files... + // org.jboss.net.protocol.file.FileURLConnection correctly // implements the getLastModified method. long lastModified = connection.getLastModified(); - return lastModified; } - catch (java.io.IOException e) { + catch (java.io.IOException e) + { getLog().warn("Failed to check modfication of deployed url: " + url, e); } - + return -1; } - - public boolean isModified() + + public boolean isModified() { - return deployedLastModified != getLastModified(); + return deployedLastModified != getLastModified(); } - + public int hashCode() { return url.hashCode(); } - - public boolean equals(final Object other) + + public boolean equals(final Object other) { if (other instanceof DeployedURL) { @@ -387,10 +403,10 @@ } return false; } - - public String toString() + + public String toString() { - return super.toString() + + return super.toString() + "{ url=" + url + ", deployedLastModified=" + deployedLastModified + " }"; @@ -402,29 +418,28 @@ */ protected void deploy(final DeployedURL du) { - try { - if (log.isTraceEnabled()) { - log.trace("Deploying: " + du); - } - lastIncompleteDeploymentException = null; - try - { - deployer.deploy(du.url); - } - catch (IncompleteDeploymentException ide) - { - lastIncompleteDeploymentException = ide; - } // end of try-catch - - du.deployed(); - - if (!deployedSet.contains(du)) { - deployedSet.add(du); - } + if (log.isTraceEnabled()) + { + log.trace("Deploying: " + du); + } + try + { + deployer.deploy(du.url); + } + catch (IncompleteDeploymentException e) + { + lastIncompleteDeploymentException = e; } catch (Exception e) { log.error("Failed to deploy: " + du, e); + } // end of try-catch + + du.deployed(); + + if (!deployedSet.contains(du)) + { + deployedSet.add(du); } } @@ -433,8 +448,10 @@ */ protected void undeploy(final DeployedURL du) { - try { - if (log.isTraceEnabled()) { + try + { + if (log.isTraceEnabled()) + { log.trace("Undeploying: " + du); } deployer.undeploy(du.url); @@ -445,7 +462,7 @@ log.error("Failed to undeploy: " + du, e); } } - + /** * Checks if the url is in the deployed set. */ @@ -454,32 +471,35 @@ DeployedURL du = new DeployedURL(url); return deployedSet.contains(du); } - + public synchronized void scan() throws Exception { lastIncompleteDeploymentException = null; - if (urlList == null) throw new IllegalStateException("not initialized"); - + boolean trace = log.isTraceEnabled(); - // Scan for new deployements - - if (trace) { + // Scan for new deployements + if (trace) + { log.trace("Scanning for new deployments"); } - synchronized (urlList) { - URL[] urls = (URL[]) urlList.toArray( new URL[] {} ); + synchronized (urlList) + { + URL[] urls = (URL[]) urlList.toArray( new URL[]{} ); for( int i = 0; i < urls.length; i++ ) { - if (!isDeployed(urls[i])) { - if (urls[i].getProtocol().equals("file")) { + if (!isDeployed(urls[i])) + { + if (urls[i].getProtocol().equals("file")) + { // it is a file url which gets special handling scanDirectory(urls[i]); } - else { + else + { // just deploy, not a file url and not yet deployed deploy(new DeployedURL(urls[i])); } @@ -488,53 +508,66 @@ } // Scan for removed or changed deployments - - if (trace) { + if (trace) + { log.trace("Scanning existing deployments for removal or modification"); } - + // Avoid ConcurrentModificationExceptions - + List removed = new LinkedList(); List modified = new LinkedList(); - + for (Iterator iter=deployedSet.iterator(); iter.hasNext();) { DeployedURL du = (DeployedURL)iter.next(); - - if (trace) { + + if (trace) + { log.trace("Checking deployment: " + du); } - + // check if it was removed - if (du.isRemoved()) { + if (du.isRemoved()) + { removed.add(du); } // check if was modified - else if (du.isModified()) { + else if (du.isModified()) + { modified.add(du); } } - + for (Iterator iter=removed.iterator(); iter.hasNext();) { DeployedURL du = (DeployedURL)iter.next(); undeploy(du); } - + for (Iterator iter=modified.iterator(); iter.hasNext();) { DeployedURL du = (DeployedURL)iter.next(); undeploy(du); deploy(du); } - //report any problems present at end of scan - if (lastIncompleteDeploymentException != null) + + // Validate that there are still incomplete deployments + if( lastIncompleteDeploymentException != null ) { - log.error(lastIncompleteDeploymentException); - } // end of if () - + try + { + Object[] args = {}; + String[] sig = {}; + Object o = getServer().invoke(getDeployer(), + "checkIncompleteDeployments", args, sig); + } + catch (Exception e) + { + log.error(e); + } + } } /** @@ -544,82 +577,90 @@ protected void scanDirectory(URL url) throws Exception { boolean trace = log.isTraceEnabled(); - - if (trace) { + + if (trace) + { log.trace("Scanning directory: " + url); } - - if (isDeployed(url)) { + + if (isDeployed(url)) + { // short-circuit on deployed urls, for nesting support return; } - + File file = new File(url.getFile()); - + // make sure it exists - if (!file.exists()) { - if (trace) { + if (!file.exists()) + { + if (trace) + { log.trace("Skipping non-existant file: " + file); } return; } - + // if it is not a directory, and it is not deployed then add it - if (!file.isDirectory()) { + if (!file.isDirectory()) + { // just a plain file which isn't deployed deploy(new DeployedURL(url)); return; } // else it is a directory, scan it - + File[] files = filter == null ? file.listFiles() : file.listFiles(filter); - if (files == null) { + if (files == null) + { throw new Exception("Null files returned from directory listing"); } - + // list of urls to deploy List list = new LinkedList(); - + for (int i = 0; i < files.length; i++) { - if (trace) { + if (trace) + { log.trace("Checking deployment file: " + files[i]); } - + // It is a new file url = files[i].toURL(); - if (!isDeployed(url)) { + if (!isDeployed(url)) + { list.add(url); } } - + // // HACK, sort the elements so dependencies have a better chance of working - // + // if (sorter != null) - Collections.sort(list, sorter); - + Collections.sort(list, sorter); + // deploy each url Iterator iter = list.iterator(); - while (iter.hasNext()) + while (iter.hasNext()) { url = (URL)iter.next(); deploy(new DeployedURL(url)); } } - - + + ///////////////////////////////////////////////////////////////////////// // Service/ServiceMBeanSupport // ///////////////////////////////////////////////////////////////////////// - + public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { // get server's home for relative paths, need this for setting // attribute final values, so we need todo it here serverHome = ServerConfigLocator.locate().getServerHomeDir(); - + return super.preRegister(server, name); } } |