User: user57  
  Date: 02/03/02 00:05:19

  Added:       src/main/org/jboss/deployment/scanner
                        AbstractDeploymentScanner.java
                        URLDeploymentScanner.java
                        URLDeploymentScannerMBean.java
  Log:
   o Adding a URLDeploymentScanner
  
  Revision  Changes    Path
  1.1                  
jboss-system/src/main/org/jboss/deployment/scanner/AbstractDeploymentScanner.java
  
  Index: AbstractDeploymentScanner.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.deployment.scanner;
  
  import java.io.File;
  
  import java.util.Collection;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Set;
  import java.util.HashSet;
  import java.util.StringTokenizer;
  
  import java.net.URL;
  import java.net.MalformedURLException;
  
  import javax.management.ObjectName;
  import javax.management.MBeanServer;
  
  import org.jboss.system.ServiceMBeanSupport;
  import org.jboss.system.ConfigurationException;
  
  import org.jboss.deployment.MainDeployerMBean;
  import org.jboss.deployment.DeploymentInfo;
  import org.jboss.deployment.DeploymentException;
  import org.jboss.deployment.DeploymentSorter;
  
  import org.jboss.system.server.ServerConfigImplMBean;
  
  import org.jboss.logging.Logger;
  
  import org.jboss.util.NullArgumentException;
  import org.jboss.util.MuLong;
  import org.jboss.util.MuBoolean;
  import org.jboss.util.jmx.MBeanProxy;
  
  /**
   * An abstract support class for implementing a deployment scanner.
   *
   * <p>Sub-class must implement {@link DeploymentScanner#scan}.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public abstract class AbstractDeploymentScanner
     extends ServiceMBeanSupport
     implements DeploymentScanner, DeploymentScannerMBean
  {
     /** The scan period in milliseconds */
     protected MuLong scanPeriod = new MuLong(5000);
  
     /** True if period based scanning is enabled. */
     protected MuBoolean scanEnabled = new MuBoolean(true);
  
     /** A proxy to the main deployer. */
     protected MainDeployerMBean deployer;
  
     /** The scanner thread. */
     protected ScannerThread scannerThread;
  
     /** A helper for sorting deployments. */
     protected DeploymentSorter sorter = new DeploymentSorter();
  
  
     /////////////////////////////////////////////////////////////////////////
     //                           DeploymentScanner                         //
     /////////////////////////////////////////////////////////////////////////
  
     /**
      * Period must be >= 0.
      */
     public void setScanPeriod(final long period)
     {
        if (period < 0)
           throw new IllegalArgumentException("ScanPeriod must be >= 0; have: " + 
period);
  
        this.scanPeriod.set(period);
     }
  
     public long getScanPeriod()
     {
        return scanPeriod.longValue();
     }
  
     public void setScanEnabled(final boolean flag)
     {
        this.scanEnabled.set(flag);
     }
  
     public boolean isScanEnabled()
     {
        return scanEnabled.get();
     }
  
  
     /////////////////////////////////////////////////////////////////////////
     //                           Scanner Thread                            //
     /////////////////////////////////////////////////////////////////////////
  
     /**
      * Should use Timer/TimerTask instead?  This has some issues with
      * interaction with ScanEnabled attribute.  ScanEnabled works only
      * when starting/stopping.
      */
     public class ScannerThread
        extends Thread
     {
        /** We get our own logger. */
        protected Logger log = Logger.getLogger(ScannerThread.class);
  
        /** True if the scan loop should run. */
        protected boolean enabled;
  
        /** True if we are shutting down. */
        protected boolean shuttingDown;
  
        /** Lock/notify object. */
        protected Object lock = new Object();
  
        public ScannerThread(boolean enabled)
        {
           super("ScannerThread");
  
           this.enabled = enabled;
        }
  
        public void setEnabled(boolean enabled)
        {
           this.enabled = enabled;
  
           synchronized (lock) {
              lock.notifyAll();
           }
  
           if (log.isDebugEnabled()) {
              log.debug("Notified that enabled: " + enabled);
           }
        }
  
        public void shutdown()
        {
           enabled = false;
           shuttingDown = true;
  
           synchronized (lock) {
              lock.notifyAll();
           }
  
           if (log.isDebugEnabled()) {
              log.debug("Notified to shutdown");
           }
        }
      
        public void run()
        {
           log.info("Running");
  
           while (!shuttingDown) {
  
              // If we are not enabled, then wait
              if (!enabled) {
                 try {
                    log.debug("Disabled, waiting for notification");
                    synchronized (lock) {
                       lock.wait();
                    }
                 }
                 catch (InterruptedException ignore) {}
              }
  
              loop();
           }
  
           log.info("Shutdown");
        }
  
        protected void loop()
        {
           while (enabled) {
              // Scan for new/removed/changed/whatever
              try {
                 scan();
              }
              catch (Exception e) {
                 log.error("Scanning failed; continuing", e);
              }
  
              // Sleep for scan period
              try {
                 log.trace("Sleeping...");
                 Thread.sleep(scanPeriod.longValue());
              }
              catch (InterruptedException ignore) {}
           }
        }
     }
  
  
     /////////////////////////////////////////////////////////////////////////
     //                     Service/ServiceMBeanSupport                     //
     /////////////////////////////////////////////////////////////////////////
  
     protected void createService() throws Exception
     {
        // setup deployer
        deployer = (MainDeployerMBean)
           MBeanProxy.create(MainDeployerMBean.class, 
                             MainDeployerMBean.OBJECT_NAME,
                             server);
  
        // setup + start scanner thread
        scannerThread = new ScannerThread(false);
        scannerThread.start();
        log.debug("Scanner thread started");
     }
     
     protected void startService() throws Exception 
     {
        // scan before we enable the thread, so JBoss version shows up afterwards
        scan();
  
        // enable scanner thread if we are enabled
        scannerThread.setEnabled(scanEnabled.get());
     }
     
     protected void stopService() throws Exception 
     {
        // disable scanner thread
        scannerThread.setEnabled(false);
     }
     
     protected void destroyService() throws Exception 
     {
        // drop our ref to deployer, so scan will fail
        deployer = null;
  
        // shutdown scanner thread
        scannerThread.shutdown();
  
        // help gc
        scannerThread = null;
     }
  }
  
  
  
  1.1                  
jboss-system/src/main/org/jboss/deployment/scanner/URLDeploymentScanner.java
  
  Index: URLDeploymentScanner.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.deployment.scanner;
  
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.MalformedURLException;
  
  import java.io.File;
  
  import java.util.List;
  import java.util.ArrayList;
  import java.util.LinkedList;
  import java.util.Iterator;
  import java.util.Set;
  import java.util.HashSet;
  import java.util.StringTokenizer;
  import java.util.Collections;
  
  import javax.management.ObjectName;
  import javax.management.MBeanServer;
  
  import org.jboss.system.server.ServerConfig;
  import org.jboss.system.server.ServerConfigLocator;
  
  import org.jboss.util.NullArgumentException;
  
  /**
   * A URL-based deployment scanner.  Supports local directory 
   * scanning for file-based urls.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class URLDeploymentScanner
     extends AbstractDeploymentScanner
     implements DeploymentScanner, URLDeploymentScannerMBean
  {
     /** 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;
  
     public void setURLList(final List list)
     {
        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()) {
           URL url = (URL)iter.next();
           if (url == null)
              throw new NullArgumentException("list element");
  
           addURL(url);
        }
  
        if (debug) {
           log.debug("URL list: " + urlList);
        }
     }
  
     public List getURLList()
     {
        // too bad, List isn't a cloneable
        return new ArrayList(urlList);
     }
  
     public void addURL(final URL url)
     {
        if (url == null)
           throw new NullArgumentException("url");
  
        urlList.add(url);
        if (log.isDebugEnabled()) {
           log.debug("Added url: " + url);
        }
     }
  
     public void removeURL(final URL url)
     {
        if (url == null)
           throw new NullArgumentException("url");
  
        boolean success = urlList.remove(url);
        if (success && log.isDebugEnabled()) {
           log.debug("Removed url: " + url);
        }
     }
  
     public boolean hasURL(final URL url)
     {
        if (url == null)
           throw new NullArgumentException("url");
  
        return urlList.contains(url);
     }
  
  
     /////////////////////////////////////////////////////////////////////////
     //                  Management/Configuration Helpers                   //
     /////////////////////////////////////////////////////////////////////////
  
     public void setURLs(final String listspec) throws MalformedURLException
     {
        if (listspec == null)
           throw new NullArgumentException("listspec");
  
        boolean debug = log.isDebugEnabled();
  
        List list = new LinkedList();
  
        StringTokenizer stok = new StringTokenizer(listspec, ",");
        while (stok.hasMoreTokens()) {
           String urlspec = stok.nextToken().trim();
  
           if (debug) {
              log.debug("Adding URL from spec: " + urlspec);
           }
  
           URL url = makeURL(urlspec);
           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
     {
        URL url;
  
        try {
           url = new URL(urlspec);
        }
        catch (MalformedURLException e) {
           File file = new File(urlspec);
           if (!file.isAbsolute()) {
              file = new File(serverHome, file.getName());
           }
  
           try {
              file = file.getCanonicalFile();
           }
           catch (java.io.IOException x) {
              throw new MalformedURLException("Invalid urlspec: " + urlspec);
           }
  
           url = file.toURL();
        }
        
        return url;
     }
  
     public void addURL(final String urlspec) throws MalformedURLException
     {
        addURL(makeURL(urlspec));
     }
  
     public void removeURL(final String urlspec) throws MalformedURLException
     {
        removeURL(makeURL(urlspec));
     }
  
     public boolean hasURL(final String urlspec) throws MalformedURLException
     {
        return hasURL(makeURL(urlspec));
     }
  
  
     /////////////////////////////////////////////////////////////////////////
     //                           DeploymentScanner                         //
     /////////////////////////////////////////////////////////////////////////
  
     /**
      * A container and help class for a deployed URL.
      */
     protected class DeployedURL
     {
        public URL url;
        public long lastDeployed;
  
        public DeployedURL(final URL url) 
        {
           this.url = url;
        }
  
        public void deployed()
        {
           lastDeployed = System.currentTimeMillis();
        }
  
        public boolean isFile()
        {
           return url.getProtocol().equals("file");
        }
  
        public File getFile()
        {
           return new File(url.getFile());
        }
  
        public boolean isRemoved()
        {
           if (isFile()) {
              File file = getFile();
              return !file.exists();
           }
           return false;
        }
  
        public boolean isModified() 
        {
           try {
              long lastModified = -1;
  
              if (isFile()) {
                 File file = getFile();
                 lastModified = file.lastModified();
              }
              else {
                 URLConnection connection = url.openConnection();
                 lastModified = connection.getLastModified();
              }
  
              return lastDeployed < lastModified;
           }
           catch (java.io.IOException e) {
              log.warn("Failed to check modfication of deployed url: " + url, e);
           }
  
           return false;
        }
  
        public int hashCode()
        {
           return url.hashCode();
        }
  
        public boolean equals(final Object other) 
        {
           if (other instanceof DeployedURL)
           {
              return ((DeployedURL)other).url.equals(this.url);
           }
           return false;
        }
  
        public String toString() 
        {
           return super.toString() + 
              "{ url=" + url +
              ", lastDeployed=" + lastDeployed +
              " }";
        }
     }
  
     /**
      * A helper to deploy the given URL with the deployer.
      */
     protected void deploy(final DeployedURL du)
     {
        try {
           if (log.isTraceEnabled()) {
              log.trace("Deploying: " + du);
           }
           deployer.deploy(du.url.toString());
           du.deployed();
           
           if (!deployedSet.contains(du)) {
              deployedSet.add(du);
           }
        }
        catch (Exception e)
        {
           log.error("Failed to deploy: " + du, e);
        }
     }
  
     /**
      * A helper to undeploy the given URL from the deployer.
      */
     protected void undeploy(final DeployedURL du)
     {
        try {
           if (log.isTraceEnabled()) {
              log.trace("Undeploying: " + du);
           }
           deployer.undeploy(du.url.toString());
           deployedSet.remove(du);
        }
        catch (Exception e)
        {
           log.error("Failed to undeploy: " + du, e);
        }
     }
  
     /**
      * Checks if the url is in the deployed set.
      */
     protected boolean isDeployed(final URL url)
     {
        DeployedURL du = new DeployedURL(url);
        return deployedSet.contains(du);
     }
  
     public void scan() throws Exception
     {
        if (urlList == null)
           throw new IllegalStateException("not initialized");
  
        boolean trace = log.isTraceEnabled();
  
        // Scan for new deployements 
  
        if (trace) {
           log.trace("Scanning for new deployments");
        }
  
        synchronized (urlList) {
           for (Iterator iter = urlList.iterator(); iter.hasNext();) 
           {
              URL url = (URL)iter.next();
  
              if (!isDeployed(url)) {
                 if (url.getProtocol().equals("file")) {
                    // it is a file url which gets special handling
                    scanDirectory(url);
                 }
                 else {
                    // just deploy, not a file url and not yet deployed
                    deploy(new DeployedURL(url));
                 }
              }
           }
        }
  
        // Scan for removed or changed deployments
  
        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();
  
           // check if it was removed
           if (du.isRemoved()) {
              removed.add(du);
           }
           
           // check if was modified
           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);
        }
     }
  
     /**
      * @todo Add configurable filter support.
      * @todo Add configurable nesting which recurses back to scanDirectory.
      */
     protected void scanDirectory(URL url) throws Exception
     {
        boolean trace = log.isTraceEnabled();
  
        if (trace) {
           log.trace("Scanning directory: " + 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()) {
           log.warn("Skipping non-existant file: " + file);
           return;
        }
           
        // if it is not a directory, and it is not deployed then add it
        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 = file.listFiles();
        if (files == null) {
           throw new Exception("Null files returned from directory listing");
        }
        
        for (int i = 0; i < files.length; i++)
        {
           if (files[i].isDirectory()) {
              if (trace) {
                 log.trace("Ignoring nested directory: " + files[i]);
              }
              continue;
           }
              
           if (trace) {
              log.trace("Checking deployment file: " + files[i]);
           }
              
           // It is a new file
           url = files[i].toURL();
           if (!isDeployed(url)) {
              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);
     }
  }
  
  
  
  1.1                  
jboss-system/src/main/org/jboss/deployment/scanner/URLDeploymentScannerMBean.java
  
  Index: URLDeploymentScannerMBean.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.deployment.scanner;
  
  import java.net.URL;
  import java.net.MalformedURLException;
  
  import java.util.List;
  
  /**
   * The JMX management interface for the {@link URLDeploymentScanner} 
   * component.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public interface URLDeploymentScannerMBean
     extends DeploymentScannerMBean
  {
     /**
      * Set the list of URLs to scan.
      *
      * @param list    The list of URLs to scan.
      */
     void setURLList(List list);
  
     /**
      * Get the list of URLs being scanned.
      *
      * @return   The list of URLs being scanned.
      */
     List getURLList();
  
     /**
      * Add a URL to the scan list.
      *
      * @param url    The URL to add to the scan list.
      */
     void addURL(URL url);
  
     /**
      * Remove a URL from the scan list.
      *
      * @param url    The URL to remove from the scan list.
      */
     void removeURL(URL url);
  
     /**
      * Check if the scanner has the given url in its scan list.
      *
      * @param url    The URL to check.
      * @return       True if the url is in the scan list.
      */
     boolean hasURL(URL url);
  
  
     /////////////////////////////////////////////////////////////////////////
     //                  Management/Configuration Helpers                   //
     /////////////////////////////////////////////////////////////////////////
  
     /**
      * Set the list of URLs to scan.
      *
      * @param listspec    A comma seperated list of URL names to scan.
      *
      * @throws MalformedURLException    Invalid URL.
      */
     void setURLs(String listspec) throws MalformedURLException;
  
     /**
      * Add a URL to the scan list.
      *
      * @param urlspec    The URL to add to the scan list.
      *
      * @throws MalformedURLException    Invalid URL.
      */
     void addURL(String urlspec) throws MalformedURLException;
  
     /**
      * Remove a URL from the scan list.
      *
      * @param urlspec    The URL to remove from the scan list.
      *
      * @throws MalformedURLException    Invalid URL.
      */
     void removeURL(String urlspec) throws MalformedURLException;
  
     /**
      * Check if the scanner has the given url in its scan list.
      *
      * @param url    The URL to check.
      * @return       True if the url is in the scan list.
      *
      * @throws MalformedURLException    Invalid URL.
      */
     boolean hasURL(String urlspec) throws MalformedURLException;
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to