You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/01/13 18:09:14 UTC

svn commit: r611598 - in /servicemix/smx4/runtime/trunk/filemonitor: pom.xml src/main/java/org/apache/servicemix/runtime/filemonitor/FileMonitor.java src/main/java/org/apache/servicemix/runtime/filemonitor/Scanner.java

Author: gnodet
Date: Sun Jan 13 09:09:13 2008
New Revision: 611598

URL: http://svn.apache.org/viewvc?rev=611598&view=rev
Log:
SMX4RUN-6: Add logging in filemonitor module, patch provided by Jeff Yu

Modified:
    servicemix/smx4/runtime/trunk/filemonitor/pom.xml
    servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/FileMonitor.java
    servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/Scanner.java

Modified: servicemix/smx4/runtime/trunk/filemonitor/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/runtime/trunk/filemonitor/pom.xml?rev=611598&r1=611597&r2=611598&view=diff
==============================================================================
--- servicemix/smx4/runtime/trunk/filemonitor/pom.xml (original)
+++ servicemix/smx4/runtime/trunk/filemonitor/pom.xml Sun Jan 13 09:09:13 2008
@@ -44,7 +44,11 @@
     <dependency>
       <groupId>org.apache.servicemix.bundles</groupId>
       <artifactId>org.apache.servicemix.bundles.ant-${ant.version}</artifactId>
-      <version>1.0-m2-SNAPSHOT</version>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+       <groupId>commons-logging</groupId>
+       <artifactId>commons-logging</artifactId>
     </dependency>
     <dependency>
       <groupId>org.apache.felix</groupId>

Modified: servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/FileMonitor.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/FileMonitor.java?rev=611598&r1=611597&r2=611598&view=diff
==============================================================================
--- servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/FileMonitor.java (original)
+++ servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/FileMonitor.java Sun Jan 13 09:09:13 2008
@@ -32,6 +32,8 @@
 import java.util.Properties;
 import java.util.Set;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.taskdefs.Jar;
 import org.osgi.framework.Bundle;
@@ -51,20 +53,13 @@
  * @version $Revision: 1.1 $
  */
 public class FileMonitor {
-	// Define a few logging levels.
-	public static final int TRACE = 0;
-	public static final int DEBUG = 1;
-	public static final int INFO  = 2;
-	public static final int WARN  = 3;
-	public static final int ERROR = 4;
-	public static final int FATAL = 5;
-	public static final int NONE = 6;
 	
     public final static String CONFIG_DIR = "org.apache.servicemix.filemonitor.configDir";
     public final static String DEPLOY_DIR = "org.apache.servicemix.filemonitor.monitorDir";
     public final static String GENERATED_JAR_DIR = "org.apache.servicemix.filemonitor.generatedJarDir";
     public final static String SCAN_INTERVAL = "org.apache.servicemix.filemonitor.scanInterval";
     protected static final String ALIAS_KEY = "_alias_factory_pid";
+    private static Log logger = LogFactory.getLog(FileMonitor.class);
     private FileMonitorActivator activator;
     private File configDir = new File("./etc");
     private File deployDir = new File("./deploy");
@@ -77,8 +72,6 @@
     private List<Bundle> bundlesToStart = new ArrayList<Bundle>();
     private List<Bundle> bundlesToUpdate = new ArrayList<Bundle>();
     private Map<String, String> artifactToBundle = new HashMap<String, String>();
-    
-    private int logLevel = NONE;
 
     public FileMonitor() {
     }
@@ -120,16 +113,16 @@
         scanner.setScanInterval(scanInterval);
 
         scanner.addListener(new Scanner.BulkListener() {
-            public void filesChanged(List filenames) throws Exception {
+            public void filesChanged(List<String> filenames) throws Exception {
                 onFilesChanged(filenames);
             }
         });
 
-        log("Starting to monitor the deploy directory: " + deployDir + " every " + scanInterval + " millis");
+        logger.info("Starting to monitor the deploy directory: " + deployDir + " every " + scanInterval + " millis");
         if (configDir != null) {
-            log("Config directory is at: " + configDir);
+            logger.info("Config directory is at: " + configDir);
         }
-        log("Will generate bundles from expanded source directories to: " + generateDir);
+        logger.info("Will generate bundles from expanded source directories to: " + generateDir);
 
         scanner.start();
     }
@@ -196,7 +189,7 @@
     // Implementation methods
     //-------------------------------------------------------------------------
 
-    protected void onFilesChanged(List filenames) {
+    protected void onFilesChanged(List<String> filenames) {
         changedBundles.clear();
         bundlesToStart.clear();
         bundlesToUpdate.clear();
@@ -208,7 +201,7 @@
             
             File file = new File(name);
             try {
-                debug("File changed: " + filename + " with type: " + filename.getClass().getName());
+                logger.debug("File changed: " + filename + " with type: " + filename.getClass().getName());
                 
                 if (file.exists()) {
                     file = transformArtifact(file);
@@ -261,7 +254,7 @@
                 }
             }
             catch (Exception e) {
-                warn("Failed to process: " + file + ". Reason: " + e, e);
+                logger.warn("Failed to process: " + file + ". Reason: " + e, e);
             }
         }
         refreshPackagesAndStartOrUpdateBundles();
@@ -288,7 +281,7 @@
 	}
 
     protected void deployBundle(File file) throws IOException, BundleException {
-        log("Deloying: " + file.getCanonicalPath());
+        logger.info("Deloying: " + file.getCanonicalPath());
 
         InputStream in = new FileInputStream(file);
 
@@ -311,11 +304,11 @@
     }
 
     protected void undeployBundle(File file) throws BundleException, IOException {
-        log("Undeloying: " + file.getCanonicalPath());
+        logger.info("Undeloying: " + file.getCanonicalPath());
         Bundle bundle = getBundleForJarFile(file);
 
         if (bundle == null) {
-            warn("Could not find Bundle for file: " + file.getCanonicalPath());
+            logger.warn("Could not find Bundle for file: " + file.getCanonicalPath());
         }
         else {
             changedBundles.add(bundle);
@@ -341,7 +334,7 @@
         ConfigurationAdmin configurationAdmin = activator.getConfigurationAdmin();
         if (configurationAdmin == null) {
             if (!loggedConfigAdminWarning) {
-                warn("No ConfigurationAdmin so cannot deploy configurations");
+                logger.warn("No ConfigurationAdmin so cannot deploy configurations");
                 loggedConfigAdminWarning = true;
             }
         }
@@ -352,7 +345,7 @@
                 properties.load(in);
                 closeQuietly(in);
                 String[] pid = parsePid(file);
-                Hashtable hashtable = new Hashtable();
+                Hashtable<Object, Object> hashtable = new Hashtable<Object, Object>();
                 hashtable.putAll(properties);
                 if (pid[1] != null) {
                     hashtable.put(ALIAS_KEY, pid[1]);
@@ -439,20 +432,20 @@
         for (Bundle bundle : bundlesToUpdate) {
             try {
                 bundle.update();
-                log("Updated: " + bundle);
+                logger.info("Updated: " + bundle);
 
             }
             catch (BundleException e) {
-                warn("Failed to update bundle: " + bundle + ". Reason: " + e, e);
+                logger.warn("Failed to update bundle: " + bundle + ". Reason: " + e, e);
             }
         }
         for (Bundle bundle : bundlesToStart) {
             try {
                 bundle.start();
-                log("Started: " + bundle);
+                logger.info("Started: " + bundle);
             }
             catch (BundleException e) {
-                warn("Failed to start bundle: " + bundle + ". Reason: " + e, e);
+                logger.warn("Failed to start bundle: " + bundle + ". Reason: " + e, e);
             }
         }
     }
@@ -465,7 +458,7 @@
             undeployBundle(destFile);
             destFile.delete();
         }
-        log("Creating jar:  " + destFile + " from dir: " + dir);
+        logger.info("Creating jar:  " + destFile + " from dir: " + dir);
         jar.setDestFile(destFile);
         jar.setManifest(new File(new File(dir, "META-INF"), "MANIFEST.MF"));
         jar.setBasedir(dir);
@@ -549,32 +542,8 @@
             in.close();
         }
         catch (IOException e) {
-            warn("Failed to close stream. " + e, e);
-        }
-    }
-
-    protected void debug(String message) {
-        if (logLevel <= DEBUG) {
-            System.out.println("DEBUG: " + message);
-        }
-    }
-
-    protected void log(String message) {
-        if (logLevel <= INFO) {
-            System.out.println("INFO: " + message);
-        }
-    }
-
-    protected void warn(String message) {
-        if (logLevel <= WARN) {
-            System.out.println("WARN: " + message);
+            logger.warn("Failed to close stream. " + e, e);
         }
     }
 
-    protected void warn(String message, Throwable e) {
-        if (logLevel <= WARN) {
-            System.out.println("WARN: " + message);
-            e.printStackTrace();
-        }
-    }
 }

Modified: servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/Scanner.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/Scanner.java?rev=611598&r1=611597&r2=611598&view=diff
==============================================================================
--- servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/Scanner.java (original)
+++ servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/Scanner.java Sun Jan 13 09:09:13 2008
@@ -19,6 +19,9 @@
 import java.io.IOException;
 import java.util.*;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * Scanner
  * <p/>
@@ -30,11 +33,13 @@
  * @version $Revision: 1.1 $
  */
 public class Scanner {
+    
+    private static Log logger = LogFactory.getLog(Scanner.class);
     private long _scanInterval;
-    private List _listeners = Collections.synchronizedList(new ArrayList());
-    private Map _prevScan = Collections.EMPTY_MAP;
+    private List<Listener> _listeners = Collections.synchronizedList(new ArrayList<Listener>());
+    private Map<String, Long> _prevScan = Collections.emptyMap();
     private FilenameFilter _filter;
-    private List _scanDirs;
+    private List<File> _scanDirs;
     private volatile boolean _running = false;
     private boolean _reportExisting = true;
     private Timer _timer;
@@ -57,7 +62,7 @@
     }
 
     public interface BulkListener extends Listener {
-        public void filesChanged(List filenames) throws Exception;
+        public void filesChanged(List<String> filenames) throws Exception;
     }
 
     /**
@@ -101,7 +106,7 @@
      * @deprecated use setScanDirs(List dirs) instead
      */
     public void setScanDir(File dir) {
-        _scanDirs = new ArrayList();
+        _scanDirs = new ArrayList<File>();
         _scanDirs.add(dir);
     }
 
@@ -115,11 +120,11 @@
         return (_scanDirs == null ? null : (File) _scanDirs.get(0));
     }
 
-    public void setScanDirs(List dirs) {
+    public void setScanDirs(List<File> dirs) {
         _scanDirs = dirs;
     }
 
-    public List getScanDirs() {
+    public List<File> getScanDirs() {
         return _scanDirs;
     }
 
@@ -245,7 +250,7 @@
      * Perform a pass of the scanner and report changes
      */
     public void scan() {
-        Map currentScan = scanFiles();
+        Map<String, Long> currentScan = scanFiles();
         reportDifferences(currentScan, _prevScan);
         _prevScan = currentScan;
     }
@@ -255,13 +260,13 @@
      *
      * @return Map of name of file to last modified time
      */
-    public Map scanFiles() {
+    public Map<String, Long> scanFiles() {
         if (_scanDirs == null) {
-            return Collections.EMPTY_MAP;
+            return Collections.emptyMap();
         }
 
-        HashMap scanInfo = new HashMap();
-        Iterator itor = _scanDirs.iterator();
+        HashMap<String, Long> scanInfo = new HashMap<String, Long>();
+        Iterator<File> itor = _scanDirs.iterator();
         while (itor.hasNext()) {
             File dir = (File) itor.next();
 
@@ -279,21 +284,21 @@
      * @param currentScan the info from the most recent pass
      * @param oldScan     info from the previous pass
      */
-    public void reportDifferences(Map currentScan, Map oldScan) {
-        List bulkChanges = new ArrayList();
+    public void reportDifferences(Map<String, Long> currentScan, Map<String, Long> oldScan) {
+        List<String> bulkChanges = new ArrayList<String>();
 
-        Set oldScanKeys = new HashSet(oldScan.keySet());
-        Iterator itor = currentScan.entrySet().iterator();
+        Set<String> oldScanKeys = new HashSet<String>(oldScan.keySet());
+        Iterator<Map.Entry<String, Long>> itor = currentScan.entrySet().iterator();
         while (itor.hasNext()) {
-            Map.Entry entry = (Map.Entry) itor.next();
+            Map.Entry<String, Long> entry = itor.next();
             if (!oldScanKeys.contains(entry.getKey())) {
-                debug("File added: " + entry.getKey());
-                reportAddition((String) entry.getKey());
+                logger.debug("File added: " + entry.getKey());
+                reportAddition(entry.getKey());
                 bulkChanges.add(entry.getKey());
             }
             else if (!oldScan.get(entry.getKey()).equals(entry.getValue())) {
-                debug("File changed: " + entry.getKey());
-                reportChange((String) entry.getKey());
+                logger.debug("File changed: " + entry.getKey());
+                reportChange(entry.getKey());
                 oldScanKeys.remove(entry.getKey());
                 bulkChanges.add(entry.getKey());
             }
@@ -304,10 +309,10 @@
 
         if (!oldScanKeys.isEmpty()) {
 
-            Iterator keyItor = oldScanKeys.iterator();
+            Iterator<String> keyItor = oldScanKeys.iterator();
             while (keyItor.hasNext()) {
-                String filename = (String) keyItor.next();
-                debug("File removed: " + filename);
+                String filename = keyItor.next();
+                logger.debug("File removed: " + filename);
                 reportRemoval(filename);
                 bulkChanges.add(filename);
             }
@@ -325,7 +330,7 @@
      * @param f           file or directory
      * @param scanInfoMap map of filenames to last modified times
      */
-    private void scanFile(File f, Map scanInfoMap) {
+    private void scanFile(File f, Map<String, Long> scanInfoMap) {
         try {
             if (!f.exists()) {
                 return;
@@ -346,7 +351,7 @@
             }
         }
         catch (IOException e) {
-            warn("Error scanning watched files", e);
+            logger.warn("Error scanning watched files", e);
         }
     }
 
@@ -356,19 +361,19 @@
      * @param filename
      */
     private void reportAddition(String filename) {
-        Iterator itor = _listeners.iterator();
+        Iterator<Listener> itor = _listeners.iterator();
         while (itor.hasNext()) {
             try {
-                Object l = itor.next();
+                Listener l = itor.next();
                 if (l instanceof DiscreteListener) {
                     ((DiscreteListener) l).fileAdded(filename);
                 }
             }
             catch (Exception e) {
-                warn(e);
+                logger.warn(e);
             }
             catch (Error e) {
-                warn(e);
+                logger.warn(e);
             }
         }
     }
@@ -379,19 +384,19 @@
      * @param filename
      */
     private void reportRemoval(String filename) {
-        Iterator itor = _listeners.iterator();
+        Iterator<Listener> itor = _listeners.iterator();
         while (itor.hasNext()) {
             try {
-                Object l = itor.next();
+                Listener l = itor.next();
                 if (l instanceof DiscreteListener) {
                     ((DiscreteListener) l).fileRemoved(filename);
                 }
             }
             catch (Exception e) {
-                warn(e);
+                logger.warn(e);
             }
             catch (Error e) {
-                warn(e);
+                logger.warn(e);
             }
         }
     }
@@ -402,55 +407,39 @@
      * @param filename
      */
     private void reportChange(String filename) {
-        Iterator itor = _listeners.iterator();
+        Iterator<Listener> itor = _listeners.iterator();
         while (itor.hasNext()) {
             try {
-                Object l = itor.next();
+                Listener l = itor.next();
                 if (l instanceof DiscreteListener) {
                     ((DiscreteListener) l).fileChanged(filename);
                 }
             }
             catch (Exception e) {
-                warn(e);
+                logger.warn(e);
             }
             catch (Error e) {
-                warn(e);
+                logger.warn(e);
             }
         }
     }
 
-    private void reportBulkChanges(List filenames) {
-        Iterator itor = _listeners.iterator();
+    private void reportBulkChanges(List<String> filenames) {
+        Iterator<Listener> itor = _listeners.iterator();
         while (itor.hasNext()) {
             try {
-                Object l = itor.next();
+                Listener l = itor.next();
                 if (l instanceof BulkListener) {
                     ((BulkListener) l).filesChanged(filenames);
                 }
             }
             catch (Exception e) {
-                warn(e);
+                logger.warn(e);
             }
             catch (Error e) {
-                warn(e);
+                logger.warn(e);
             }
         }
     }
-
-    protected void debug(String message) {
-        // TODO replace with clogging or OSGi Log
-
-    }
-
-    protected void warn(Throwable t) {
-        // TODO replace with clogging or OSGi Log
-        System.out.println("WARN: " + t);
-        t.printStackTrace();
-    }
-
-    protected void warn(String message, Throwable t) {
-        // TODO replace with clogging or OSGi Log
-        System.out.println("WARN: " + message + " " + t);
-        t.printStackTrace();
-    }
+    
 }