servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r608793 - in /servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor: DeploymentListener.java FileMonitor.java
Date Fri, 04 Jan 2008 10:20:24 GMT
Author: gnodet
Date: Fri Jan  4 02:20:21 2008
New Revision: 608793

URL: http://svn.apache.org/viewvc?rev=608793&view=rev
Log:
Add a DeploymentListener interface, patch provided by Jeff Yu

Added:
    servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/DeploymentListener.java
Modified:
    servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/FileMonitor.java

Added: servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/DeploymentListener.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/DeploymentListener.java?rev=608793&view=auto
==============================================================================
--- servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/DeploymentListener.java
(added)
+++ servicemix/smx4/runtime/trunk/filemonitor/src/main/java/org/apache/servicemix/runtime/filemonitor/DeploymentListener.java
Fri Jan  4 02:20:21 2008
@@ -0,0 +1,36 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.runtime.filemonitor;
+
+import java.io.File;
+
+
+public interface DeploymentListener {
+	
+    /**
+     * Returns true if the listener can process the given file
+     */
+    boolean canHandle(File artifact);
+
+   /**
+    * Process the given file (canHandle returned true previously)
+    * Can return <null> or a pointer to a transformed file.
+    */
+    File handle(File artifact, File tmpDir);
+   
+}

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=608793&r1=608792&r2=608793&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
Fri Jan  4 02:20:21 2008
@@ -24,9 +24,11 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Dictionary;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
@@ -36,6 +38,7 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.packageadmin.PackageAdmin;
@@ -73,6 +76,7 @@
     private List<Bundle> changedBundles = new ArrayList<Bundle>();
     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;
 
@@ -107,7 +111,7 @@
         deployDir.mkdirs();
         generateDir.mkdirs();
 
-        List dirs = new ArrayList();
+        List<File> dirs = new ArrayList<File>();
         if (configDir != null) {
             dirs.add(configDir);
         }
@@ -200,11 +204,26 @@
 
         for (Object filename : filenames) {
             String name = filename.toString();
-
-            // now lets iterate to find the parent directory
+            boolean added = true;
+            
             File file = new File(name);
             try {
                 debug("File changed: " + filename + " with type: " + filename.getClass().getName());
+                
+                if (file.exists()) {
+                    file = transformArtifact(file);
+                } else {
+                	added = false;
+                	String transformedFile = artifactToBundle.get(name);
+                	if (transformedFile != null) {
+                		file = new File(transformedFile);
+                		if (file.exists()) {
+                			file.delete();
+                		}
+                	}
+                }
+                
+                // now lets iterate to find the parent directory
                 File jardir = getExpandedBundleRootDirectory(file);
                 if (jardir != null) {
                     if (file.exists() && !bundleJarsCreated.contains(jardir)) {
@@ -213,23 +232,23 @@
                         deployBundle(newBundle);
                     }
                 }
-                else if (name.endsWith(".jar")) {
-                    if (file.exists()) {
+                else if (file.getName().endsWith(".jar")) {
+                    if (added) {
                         deployBundle(file);
                     }
                     else {
                         undeployBundle(file);
                     }
                 }
-                else if (name.endsWith(".cfg")) {
-                    if (file.exists()) {
+                else if (file.getName().endsWith(".cfg")) {
+                    if (added) {
                         updateConfiguration(file);
                     }
                     else {
                         deleteConfiguration(file);
                     }
                 }
-                else if (name.equals("MANIFEST.MF")) {
+                else if (file.getName().equals("MANIFEST.MF")) {
                     File parentFile = file.getParentFile();
                     if (parentFile.getName().equals("META-INF")) {
                         File bundleDir = parentFile.getParentFile();
@@ -245,6 +264,26 @@
         }
         refreshPackagesAndStartOrUpdateBundles();
     }
+    
+    
+	private File transformArtifact(File file) throws Exception {
+		ServiceReference[] srvRefs = getContext().getAllServiceReferences(DeploymentListener.class.getName(),
null);
+		if(srvRefs != null) {
+		    for(ServiceReference sr : srvRefs) {
+		    	try {
+		    		DeploymentListener deploymentListener = (DeploymentListener)getContext().getService(sr);
+		    		if (deploymentListener.canHandle(file)) {
+		    			File transformedFile =  deploymentListener.handle(file, getGenerateDir());
+		    			artifactToBundle.put(file.getAbsolutePath(), transformedFile.getAbsolutePath());
+		    			file = transformedFile;
+		    		}
+		    	} finally {
+		    		getContext().ungetService(sr);
+		    	}
+		    }
+		}
+		return file;
+	}
 
     protected void deployBundle(File file) throws IOException, BundleException {
         log("Deloying: " + file.getCanonicalPath());
@@ -284,14 +323,12 @@
     }
 
     protected Bundle getBundleForJarFile(File file) throws IOException {
-        String absoluteFilePath = file.getCanonicalPath();
+        String absoluteFilePath = file.getAbsolutePath();
         Bundle bundles[] = getContext().getBundles();
         for (int i = 0; i < bundles.length; i++) {
             Bundle bundle = bundles[i];
             String location = bundle.getLocation();
-            File locationFile = new File(location);
-            String absoluteLocation = locationFile.getCanonicalPath();
-            if (absoluteFilePath.equals(absoluteLocation)) {
+            if (location.endsWith(absoluteFilePath)) {
                 return bundle;
             }
         }



Mime
View raw message