You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ff...@apache.org on 2009/02/06 11:13:31 UTC

svn commit: r741500 - in /servicemix/smx4/nmr/trunk/jbi: deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/ management/src/main/java/org/apache/servicemix/jbi/management/

Author: ffang
Date: Fri Feb  6 10:13:30 2009
New Revision: 741500

URL: http://svn.apache.org/viewvc?rev=741500&view=rev
Log:
[SMX4NMR-17]get MBeans working for listComponent/installSharedLib/uninstallSharedLib/listSharedLib

Added:
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AbstractInstaller.java   (with props)
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/ComponentInstaller.java
      - copied, changed from r741389, servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/Installer.java
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/SharedLibInstaller.java   (with props)
Removed:
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/Installer.java
Modified:
    servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/ComponentImpl.java
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsService.java
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsServiceMBean.java
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminService.java
    servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/InstallationService.java

Modified: servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/ComponentImpl.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/ComponentImpl.java?rev=741500&r1=741499&r2=741500&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/ComponentImpl.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/ComponentImpl.java Fri Feb  6 10:13:30 2009
@@ -38,6 +38,7 @@
 import org.apache.servicemix.jbi.deployer.Component;
 import org.apache.servicemix.jbi.deployer.ServiceUnit;
 import org.apache.servicemix.jbi.deployer.descriptor.ComponentDesc;
+import org.apache.servicemix.jbi.deployer.descriptor.SharedLibraryList;
 import org.apache.servicemix.jbi.runtime.ComponentWrapper;
 import org.osgi.service.prefs.BackingStoreException;
 import org.osgi.service.prefs.Preferences;
@@ -124,13 +125,17 @@
         stop(true);
     }
 
-    protected Set<ServiceAssemblyImpl> getServiceAssemblies() {
+    public Set<ServiceAssemblyImpl> getServiceAssemblies() {
         Set<ServiceAssemblyImpl> sas = new HashSet<ServiceAssemblyImpl>();
         for (ServiceUnitImpl su : serviceUnits) {
             sas.add(su.getServiceAssemblyImpl());
         }
         return sas;
     }
+    
+    public SharedLibraryList[] getSharedLibraries() {
+        return componentDesc.getSharedLibraries();
+    }
 
     public void stop(boolean saveState) throws JBIException {
         LOGGER.info("Stopping component " + getName());

Added: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AbstractInstaller.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AbstractInstaller.java?rev=741500&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AbstractInstaller.java (added)
+++ servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AbstractInstaller.java Fri Feb  6 10:13:30 2009
@@ -0,0 +1,197 @@
+/*
+ * 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.jbi.management;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.kernel.filemonitor.DeploymentListener;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
+
+public class AbstractInstaller {
+	
+	private static final Log LOGGER = LogFactory.getLog(ComponentInstaller.class);
+
+    private BundleContext bundleContext;
+    
+    
+    private Map<String, String> artifactToBundle = new HashMap<String, String>();
+    private Bundle bundle;
+
+	public synchronized void deployFile(String filename) {
+    	File file = new File(filename);
+        try {
+        	LOGGER.info("File is: " + filename);
+        	// Transformation step
+        	if (file.exists()) {
+        		LOGGER.info("File exist");
+        		File f = transformArtifact(file);
+        		if (f == null) {
+        			LOGGER.info("Unsupported deployment: " + filename);
+        			return;
+                }
+        		file = f;
+            } else {
+            	String transformedFile = artifactToBundle.get(filename);
+            	if (transformedFile != null) {
+            		file = new File(transformedFile);
+            		if (file.exists()) {
+            			file.delete();
+                	}
+                }
+            }
+
+            // Handle final bundles
+        	if (file.exists()) {
+        		deployBundle(file);
+            }
+        	
+        } catch (Exception e) {
+        	LOGGER.info("Failed to process: " + file + ". Reason: " + e, e);
+        }
+            	
+    }
+    
+    protected Bundle getBundleForJarFile(File file) throws IOException {
+        String absoluteFilePath = file.getAbsoluteFile().toURI().toString();
+        Bundle bundles[] = bundleContext.getBundles();
+        for (Bundle bundle : bundles) {
+            String location = bundle.getLocation();
+            if (filePathsMatch(absoluteFilePath, location)) {
+                return bundle;
+            }
+        }
+        return null;
+    }
+    
+    protected static boolean filePathsMatch(String p1, String p2) {
+        p1 = normalizeFilePath(p1);
+        p2 = normalizeFilePath(p2);
+        return (p1 != null && p1.equalsIgnoreCase(p2));
+    }
+
+    protected static String normalizeFilePath( String path ) {
+        if (path != null) {
+            path = path.replaceFirst("file:/*", "");
+            path = path.replaceAll("[\\\\/]+", "/");
+        }
+        return path;
+    }
+
+    
+    protected void deployBundle(File file) throws IOException, BundleException {
+        LOGGER.info("Deploying: " + file.getCanonicalPath());
+
+        InputStream in = new FileInputStream(file);
+
+        try {
+            Bundle bundle = getBundleForJarFile(file);
+            if (bundle != null) {
+            	bundle.update();
+            }
+            else {
+                bundle = bundleContext.installBundle(file.getCanonicalFile().toURI().toString(), in);
+                bundle.start();
+            }
+            setBundle(bundle);
+        }
+        finally {
+            closeQuietly(in);
+        }
+    }
+    
+    protected void closeQuietly(Closeable in) {
+        try {
+            in.close();
+        }
+        catch (IOException e) {
+            LOGGER.info("Failed to close stream. " + e, e);
+        }
+    }
+    
+    public File getGenerateDir() {
+    	String base = System.getProperty("servicemix.base", ".");
+        return new File(base, "data/generated-bundles");
+    } 
+    
+    protected File transformArtifact(File file) throws Exception {
+        // Check registered deployers
+        ServiceReference[] srvRefs = bundleContext.getAllServiceReferences(DeploymentListener.class.getName(), null);
+		if(srvRefs != null) {
+		    for(ServiceReference sr : srvRefs) {
+		    	try {
+		    		DeploymentListener deploymentListener = (DeploymentListener) bundleContext.getService(sr);
+		    		if (deploymentListener.canHandle(file)) {
+		    			File transformedFile = deploymentListener.handle(file, getGenerateDir());
+		    			artifactToBundle.put(file.getAbsolutePath(), transformedFile.getAbsolutePath());
+		    			return transformedFile;
+		    		}
+		    	} finally {
+		    		bundleContext.ungetService(sr);
+		    	}
+		    }
+		}
+        JarFile jar = null;
+        try {
+            // Handle OSGi bundles with the default deployer
+            if (file.getName().endsWith("txt") || file.getName().endsWith("xml")
+            		|| file.getName().endsWith("properties")) {
+            	// that's file type which is not supported as bundle and avoid exception in the log
+                return null;
+            }
+            jar = new JarFile(file);
+            Manifest m = jar.getManifest();
+            if (m.getMainAttributes().getValue(new Attributes.Name("Bundle-SymbolicName")) != null &&
+                m.getMainAttributes().getValue(new Attributes.Name("Bundle-Version")) != null) {
+                return file;
+            }
+        } catch (Exception e) {
+            LOGGER.info("Error transforming artifact " + file.getName(), e);
+        } finally {
+            if (jar != null) {
+                jar.close();
+            }
+        }
+        return null;
+	}
+    
+    public void setBundle(Bundle bundle) {
+		this.bundle = bundle;
+	}
+
+	public Bundle getBundle() {
+		return bundle;
+	}
+	
+	public void setBundleContext(BundleContext bundleContext) {
+		this.bundleContext = bundleContext;		
+	}
+
+}

Propchange: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AbstractInstaller.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AbstractInstaller.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsService.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsService.java?rev=741500&r1=741499&r2=741500&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsService.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsService.java Fri Feb  6 10:13:30 2009
@@ -17,21 +17,29 @@
 package org.apache.servicemix.jbi.management;
 
 
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
 import javax.jbi.JBIException;
 import javax.jbi.management.LifeCycleMBean;
 
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+
+
 import org.apache.servicemix.jbi.deployer.Component;
+import org.apache.servicemix.jbi.deployer.descriptor.SharedLibraryList;
+import org.apache.servicemix.jbi.deployer.impl.ComponentImpl;
 import org.apache.servicemix.jbi.deployer.impl.Deployer;
+import org.apache.servicemix.jbi.deployer.impl.ServiceAssemblyImpl;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.springframework.osgi.context.BundleContextAware;
 
 public class AdminCommandsService implements AdminCommandsServiceMBean, BundleContextAware {
 
-	private static final Log LOGGER = LogFactory.getLog(AdminCommandsService.class);
 	
     private AdminService adminService;
     private InstallationService installationService;
@@ -65,7 +73,7 @@
      *            installation properties
      * @return
      */
-    public String installComponent(String fileName, boolean deferException) throws Exception {
+    public String installComponent(String fileName) throws Exception {
         try {
         	getInstallationService().install(fileName, null, false);
         	return ManagementSupport.createSuccessMessage("installComponent", fileName);
@@ -125,14 +133,8 @@
 	 * @param file
 	 * @return
 	 */
-    public String installSharedLibrary(String file, boolean deferException) throws Exception {
-    	return null;
-        /*if (deferException) {
-            container.updateExternalArchive(file);
-            return ManagementSupport.createSuccessMessage("installSharedLibrary", file);
-        } else {
-            return installationService.installSharedLibrary(file);
-        }*/
+    public String installSharedLibrary(String file) throws Exception {
+    	return installationService.installSharedLibrary(file);
     }
 
     /**
@@ -142,7 +144,35 @@
      * @return
      */
     public String uninstallSharedLibrary(String name) throws Exception {
-    	return ManagementSupport.createSuccessMessage("to be done");
+    	//Check that the library is installed
+        boolean isInstalled = getInstallationService().containsSharedLibrary(name);
+        if (!isInstalled) {
+            throw ManagementSupport.failure("uninstallSharedLibrary", "Shared library '" + name + "' is not installed.");
+        }
+        // Check that it is not used by a running component
+        ServiceReference[] refs = getAdminService().getComponentServiceReferences(null);
+        
+        
+        for (ServiceReference ref : refs) {
+        	ComponentImpl component = (ComponentImpl) getBundleContext().getService(ref);
+            if (!component.getCurrentState().equalsIgnoreCase(LifeCycleMBean.SHUTDOWN)) {
+                SharedLibraryList[] sls = component.getSharedLibraries();
+                if (sls != null) {
+                    for (int i = 0; i < sls.length; i++) {
+                    	if (name.equals(sls[i].getName())) {
+                    		throw ManagementSupport.failure("uninstallSharedLibrary", "Shared library '" + name
+                                            + "' is used by component '" + component.getName() + "'.");
+                        }
+                    }
+                }
+            }
+        }
+        boolean success = getInstallationService().uninstallSharedLibrary(name);
+        if (success) {
+            return ManagementSupport.createSuccessMessage("uninstallSharedLibrary", name);
+        } else {
+            throw ManagementSupport.failure("uninstallSharedLibrary", name);
+        }
     }
 
     /**
@@ -208,7 +238,7 @@
      * @param file
      * @return
      */
-    public String deployServiceAssembly(String file, boolean deferException) throws Exception {
+    public String deployServiceAssembly(String file) throws Exception {
     	return null;
         /*if (deferException) {
             container.updateExternalArchive(file);
@@ -258,23 +288,15 @@
         return getDeploymentService().shutDown(name);
     }
 
-    /**
-     * load an archive from an external location and starts it The archive can
-     * be a Component, Service Assembly or Shared Library.
-     *
-     * @param location -
-     *            can either be a url or filename (if relative - must be
-     *            relative to the container)
-     * @return status
-     * @throws Exception
-     */
-    public String installArchive(String location) throws Exception {
-    	return ManagementSupport.createSuccessMessage("to be done");
-    }
-
+    
     /**
      * Prints information about all components (Service Engine or Binding
-     * Component) installed
+     * Component) installedServiceReference[] refs = getAdminService().getComponentServiceReferences(filter);
+        if (excludeBCs && excludeSEs) {
+        	refs = new ServiceReference[0];
+        }
+        List<Component> components = new ArrayList<Component>();
+        
      *
      * @param serviceEngines
      * @param bindingComponents
@@ -285,8 +307,83 @@
      */
     public String listComponents(boolean excludeSEs, boolean excludeBCs, String requiredState,
                     String sharedLibraryName, String serviceAssemblyName) throws Exception {
-        // validate requiredState
-    	return ManagementSupport.createSuccessMessage("to be done");
+    	//validate requiredState
+        if (requiredState != null && requiredState.length() > 0
+                        && !LifeCycleMBean.UNKNOWN.equalsIgnoreCase(requiredState)
+                        && !LifeCycleMBean.SHUTDOWN.equalsIgnoreCase(requiredState)
+                        && !LifeCycleMBean.STOPPED.equalsIgnoreCase(requiredState)
+                        && !LifeCycleMBean.STARTED.equalsIgnoreCase(requiredState)) {
+            throw ManagementSupport.failure("listComponents", "Required state '" + requiredState + "' is not a valid state.");
+        }
+        // Get components
+        String filter = null;
+        if (excludeSEs && !excludeBCs) {
+        	filter = "(" + Deployer.TYPE + "=binding-component)";
+        }
+        if (excludeBCs && !excludeSEs) {
+        	filter = "(" + Deployer.TYPE + "=service-engine)";
+        }
+        ServiceReference[] refs = getAdminService().getComponentServiceReferences(filter);
+        if (excludeBCs && excludeSEs) {
+        	refs = new ServiceReference[0];
+        }
+        List<Component> components = new ArrayList<Component>();
+        for (ServiceReference ref : refs) {
+            Component component = (Component) getBundleContext().getService(ref);
+            
+            // Check status
+            if (requiredState != null && requiredState.length() > 0 && !requiredState.equalsIgnoreCase(component.getCurrentState())) {
+                continue;
+            }
+            // Check shared library
+            // TODO: check component dependency on SL
+            if (sharedLibraryName != null && sharedLibraryName.length() > 0
+                            && !getInstallationService().containsSharedLibrary(sharedLibraryName)) {
+                continue;
+            }
+            // Check deployed service assembly
+            if (serviceAssemblyName != null && serviceAssemblyName.length() > 0) {
+            	ComponentImpl compImpl = (ComponentImpl)component;
+            	Set<ServiceAssemblyImpl> saImpls = compImpl.getServiceAssemblies();
+            	boolean found = false;
+                for (ServiceAssemblyImpl sa : saImpls) {
+                    if (serviceAssemblyName.equals(sa.getName())) {
+                        found = true;
+                        break;
+                    }
+                }
+                if (!found) {
+                    continue;
+                }
+            }
+            components.add(component);
+        }
+
+        StringBuffer buffer = new StringBuffer();
+        buffer.append("<?xml version='1.0'?>\n");
+        buffer.append("<component-info-list xmlns='http://java.sun.com/xml/ns/jbi/component-info-list' version='1.0'>\n");
+        for (Iterator<Component> iter = components.iterator(); iter.hasNext();) {
+            Component component = iter.next();
+            buffer.append("  <component-info");
+            if (!getAdminService().isBinding(component.getName()) 
+            		&& getAdminService().isEngine(component.getName())) {
+                buffer.append(" type='service-engine'");
+            } else if (getAdminService().isBinding(component.getName()) 
+            		&& !getAdminService().isEngine(component.getName())) {
+                buffer.append(" type='binding-component'");
+            }
+            buffer.append(" name='" + component.getName() + "'");
+            buffer.append(" state='" + component.getCurrentState() + "'>\n");
+            if (component.getDescription() != null) {
+                buffer.append("    <description>");
+                buffer.append(component.getDescription());
+                buffer.append("</description>\n");
+            }
+            buffer.append("  </component-info>\n");
+        }
+        buffer.append("</component-info-list>");
+        return buffer.toString();
+
     }
 
     /**
@@ -297,7 +394,32 @@
      * @return
      */
     public String listSharedLibraries(String componentName, String sharedLibraryName) throws Exception {
-    	return ManagementSupport.createSuccessMessage("to be done");
+    	Set<String> libs = new HashSet<String>();
+        if (sharedLibraryName != null && sharedLibraryName.length() > 0) {
+        	if (getInstallationService().containsSharedLibrary(sharedLibraryName)) {
+                libs.add(sharedLibraryName);
+            }
+        } else if (componentName != null && componentName.length() > 0) {
+        	ServiceReference ref = getAdminService().getComponentServiceReference("(" + Deployer.NAME + "=" + componentName + ")");
+            if (ref == null) {
+                throw new JBIException("Component '" + componentName + "' not found");
+            }
+            ComponentImpl component = (ComponentImpl) bundleContext.getService(ref);
+            for (SharedLibraryList sl : component.getSharedLibraries()) {
+            	libs.add(sl.getName());
+            }
+        } else {
+            libs = getInstallationService().getInstalledSharedLibs();
+        }
+        StringBuffer buffer = new StringBuffer();
+        buffer.append("<?xml version='1.0'?>\n");
+        buffer.append("<component-info-list xmlns='http://java.sun.com/xml/ns/jbi/component-info-list' version='1.0'>\n");
+        for (String sl : libs) {
+            buffer.append("  <component-info type='shared-library' name='").append(sl).append("' state='Started'>");
+            buffer.append("  </component-info>\n");
+        }
+        buffer.append("</component-info-list>");
+        return buffer.toString();
     }
 
     /**

Modified: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsServiceMBean.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsServiceMBean.java?rev=741500&r1=741499&r2=741500&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsServiceMBean.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminCommandsServiceMBean.java Fri Feb  6 10:13:30 2009
@@ -16,24 +16,21 @@
  */
 package org.apache.servicemix.jbi.management;
 
-import java.util.Properties;
-
 
 
 public interface AdminCommandsServiceMBean { 
-	String installComponent(String file, boolean deferException) throws Exception;
+	String installComponent(String file) throws Exception;
 	String uninstallComponent(String name) throws Exception;
-	String installSharedLibrary(String file, boolean deferException) throws Exception;
+	String installSharedLibrary(String file) throws Exception;
 	String uninstallSharedLibrary(String name) throws Exception;
 	String startComponent(String name) throws Exception;
 	String stopComponent(String name) throws Exception;
 	String shutdownComponent(String name) throws Exception;
-	String deployServiceAssembly(String file, boolean deferException) throws Exception;
+	String deployServiceAssembly(String file) throws Exception;
 	String undeployServiceAssembly(String name) throws Exception;
 	String startServiceAssembly(String name) throws Exception;
 	String stopServiceAssembly(String name) throws Exception;
 	String shutdownServiceAssembly(String name) throws Exception;
-	String installArchive(String location) throws Exception;
 	String listComponents(boolean excludeSEs,
 	                      boolean excludeBCs,
 	                      String requiredState,

Modified: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminService.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminService.java?rev=741500&r1=741499&r2=741500&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminService.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/AdminService.java Fri Feb  6 10:13:30 2009
@@ -20,7 +20,6 @@
 import javax.management.ObjectName;
 
 import org.apache.servicemix.jbi.deployer.impl.Deployer;
-import org.apache.servicemix.jbi.runtime.impl.ManagementContext;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.springframework.osgi.context.BundleContextAware;

Copied: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/ComponentInstaller.java (from r741389, servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/Installer.java)
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/ComponentInstaller.java?p2=servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/ComponentInstaller.java&p1=servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/Installer.java&r1=741389&r2=741500&rev=741500&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/Installer.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/ComponentInstaller.java Fri Feb  6 10:13:30 2009
@@ -17,16 +17,7 @@
 package org.apache.servicemix.jbi.management;
 
 
-import java.io.Closeable;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.jar.Attributes;
-import java.util.jar.JarFile;
-import java.util.jar.Manifest;
 
 import javax.jbi.JBIException;
 import javax.jbi.management.DeploymentException;
@@ -35,28 +26,25 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.servicemix.kernel.filemonitor.DeploymentListener;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
+
 import org.osgi.framework.BundleException;
-import org.osgi.framework.ServiceReference;
-import org.springframework.osgi.context.BundleContextAware;
 
-public class Installer implements InstallerMBean, BundleContextAware {
+public class ComponentInstaller extends AbstractInstaller 
+	implements InstallerMBean {
     
-    private static final Log LOGGER = LogFactory.getLog(Installer.class);
+    private static final Log LOGGER = LogFactory.getLog(ComponentInstaller.class);
 
     
     private InstallationContextImpl context;
     private File jbiArtifact;
     private ObjectName objectName;
-    private BundleContext bundleContext;
     private AdminService adminService;
-    private Bundle bundle;
+
     
-    private Map<String, String> artifactToBundle = new HashMap<String, String>();
 
-    public Installer(InstallationContextImpl ic, File jbiArtifact, AdminService adminService) throws DeploymentException {
+
+    public ComponentInstaller(InstallationContextImpl ic, File jbiArtifact, AdminService adminService) throws DeploymentException {
         this.context = ic;
         this.jbiArtifact = jbiArtifact;
         this.adminService = adminService;
@@ -164,153 +152,4 @@
         this.objectName = objectName;
     }
 
-    public synchronized void deployFile(String filename) {
-    	File file = new File(filename);
-        try {
-        	LOGGER.info("File is: " + filename);
-        	// Transformation step
-        	if (file.exists()) {
-        		LOGGER.info("File exist");
-        		File f = transformArtifact(file);
-        		if (f == null) {
-        			LOGGER.info("Unsupported deployment: " + filename);
-        			return;
-                }
-        		file = f;
-            } else {
-            	String transformedFile = artifactToBundle.get(filename);
-            	if (transformedFile != null) {
-            		file = new File(transformedFile);
-            		if (file.exists()) {
-            			file.delete();
-                	}
-                }
-            }
-
-            // Handle final bundles
-        	if (file.exists()) {
-        		deployBundle(file);
-            }
-        	
-        } catch (Exception e) {
-        	LOGGER.info("Failed to process: " + file + ". Reason: " + e, e);
-        }
-            	
-    }
-    
-    protected Bundle getBundleForJarFile(File file) throws IOException {
-        String absoluteFilePath = file.getAbsoluteFile().toURI().toString();
-        Bundle bundles[] = bundleContext.getBundles();
-        for (Bundle bundle : bundles) {
-            String location = bundle.getLocation();
-            if (filePathsMatch(absoluteFilePath, location)) {
-                return bundle;
-            }
-        }
-        return null;
-    }
-    
-    protected static boolean filePathsMatch(String p1, String p2) {
-        p1 = normalizeFilePath(p1);
-        p2 = normalizeFilePath(p2);
-        return (p1 != null && p1.equalsIgnoreCase(p2));
-    }
-
-    protected static String normalizeFilePath( String path ) {
-        if (path != null) {
-            path = path.replaceFirst("file:/*", "");
-            path = path.replaceAll("[\\\\/]+", "/");
-        }
-        return path;
-    }
-
-    
-    protected void deployBundle(File file) throws IOException, BundleException {
-        LOGGER.info("Deploying: " + file.getCanonicalPath());
-
-        InputStream in = new FileInputStream(file);
-
-        try {
-            Bundle bundle = getBundleForJarFile(file);
-            if (bundle != null) {
-            	bundle.update();
-            }
-            else {
-                bundle = bundleContext.installBundle(file.getCanonicalFile().toURI().toString(), in);
-                bundle.start();
-            }
-            setBundle(bundle);
-        }
-        finally {
-            closeQuietly(in);
-        }
-    }
-    
-    protected void closeQuietly(Closeable in) {
-        try {
-            in.close();
-        }
-        catch (IOException e) {
-            LOGGER.info("Failed to close stream. " + e, e);
-        }
-    }
-    
-    public File getGenerateDir() {
-    	String base = System.getProperty("servicemix.base", ".");
-        return new File(base, "data/generated-bundles");
-    } 
-    
-    private File transformArtifact(File file) throws Exception {
-        // Check registered deployers
-        ServiceReference[] srvRefs = bundleContext.getAllServiceReferences(DeploymentListener.class.getName(), null);
-		if(srvRefs != null) {
-		    for(ServiceReference sr : srvRefs) {
-		    	try {
-		    		DeploymentListener deploymentListener = (DeploymentListener) bundleContext.getService(sr);
-		    		if (deploymentListener.canHandle(file)) {
-		    			File transformedFile = deploymentListener.handle(file, getGenerateDir());
-		    			artifactToBundle.put(file.getAbsolutePath(), transformedFile.getAbsolutePath());
-		    			return transformedFile;
-		    		}
-		    	} finally {
-		    		bundleContext.ungetService(sr);
-		    	}
-		    }
-		}
-        JarFile jar = null;
-        try {
-            // Handle OSGi bundles with the default deployer
-            if (file.getName().endsWith("txt") || file.getName().endsWith("xml")
-            		|| file.getName().endsWith("properties")) {
-            	// that's file type which is not supported as bundle and avoid exception in the log
-                return null;
-            }
-            jar = new JarFile(file);
-            Manifest m = jar.getManifest();
-            if (m.getMainAttributes().getValue(new Attributes.Name("Bundle-SymbolicName")) != null &&
-                m.getMainAttributes().getValue(new Attributes.Name("Bundle-Version")) != null) {
-                return file;
-            }
-        } catch (Exception e) {
-            LOGGER.info("Error transforming artifact " + file.getName(), e);
-        } finally {
-            if (jar != null) {
-                jar.close();
-            }
-        }
-        return null;
-	}
-
-	public void setBundleContext(BundleContext bundleContext) {
-		this.bundleContext = bundleContext;		
-	}
-
-	public void setBundle(Bundle bundle) {
-		this.bundle = bundle;
-	}
-
-	public Bundle getBundle() {
-		return bundle;
-	}
-
 }

Modified: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/InstallationService.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/InstallationService.java?rev=741500&r1=741499&r2=741500&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/InstallationService.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/InstallationService.java Fri Feb  6 10:13:30 2009
@@ -20,6 +20,7 @@
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.jbi.JBIException;
@@ -39,9 +40,11 @@
 
     private static final Log LOG = LogFactory.getLog(InstallationService.class);
 
-    private Map<String, Installer> installers = new ConcurrentHashMap<String, Installer>();
+    private Map<String, ComponentInstaller> installers = new ConcurrentHashMap<String, ComponentInstaller>();
 
-    private Map<String, Installer> nonLoadedInstallers = new ConcurrentHashMap<String, Installer>();
+    private Map<String, ComponentInstaller> nonLoadedInstallers = new ConcurrentHashMap<String, ComponentInstaller>();
+    
+    private Map<String, SharedLibInstaller> sharedLibinstallers = new ConcurrentHashMap<String, SharedLibInstaller>();
 
     private AdminService adminService;
     
@@ -70,7 +73,7 @@
                 if (desc != null && desc.getComponent() != null) {
                     String componentName = desc.getComponent().getIdentification().getName();
                     if (!installers.containsKey(componentName)) {
-                        Installer installer = doInstallArchive(desc, jarfile);
+                        ComponentInstaller installer = doInstallArchive(desc, jarfile);
                         if (installer != null) {
                             result = installer.getObjectName();
                             installers.put(componentName, installer);
@@ -98,8 +101,8 @@
         }
     }
 
-    private Installer doInstallArchive(Descriptor desc, File jarfile) throws Exception {
-		return new Installer(new InstallationContextImpl(desc.getComponent(), getNamingStrategy(), getManagementAgent()), 
+    private ComponentInstaller doInstallArchive(Descriptor desc, File jarfile) throws Exception {
+		return new ComponentInstaller(new InstallationContextImpl(desc.getComponent(), getNamingStrategy(), getManagementAgent()), 
 			jarfile, getAdminService());
 	}
 
@@ -112,7 +115,7 @@
      *         existing installation context.
      */
     public ObjectName loadInstaller(String aComponentName) {
-        Installer installer = installers.get(aComponentName);
+        ComponentInstaller installer = installers.get(aComponentName);
         if (installer == null) {
             installer = nonLoadedInstallers.get(aComponentName);
             if (installer != null) {
@@ -142,8 +145,8 @@
     public boolean unloadInstaller(String componentName, boolean isToBeDeleted) {
     	boolean result = false;
         try {
-        	Installer installer = installers.remove(componentName);
-            result = installer != null;
+        	ComponentInstaller installer = installers.remove(componentName);
+        	result = installer != null;
             if (result) {
             	getManagementAgent().unregister(installer.getObjectName());
                 if (isToBeDeleted) {
@@ -167,8 +170,42 @@
      *            URI locating a jar file containing a shared library.
      * @return - the name of the shared library loaded from aSharedLibURI.
      */
-    public String installSharedLibrary(String aSharedLibURI) {
-    	return ManagementSupport.createSuccessMessage("to be done");
+    public String installSharedLibrary(String location) {
+    	File jarfile = new File(location);
+    	String slName = null;
+    	try {
+        if (jarfile.exists()) {
+        	Descriptor desc = null;
+			try {
+				desc = Transformer.getDescriptor(jarfile);
+			} catch (Exception e) {
+				LOG.error("install sharedLib failed", e);
+				throw new DeploymentException("install sharedLib failed", e);
+			}
+            if (desc != null) {
+                if (desc.getSharedLibrary() == null) {
+                    throw new DeploymentException("JBI descriptor is not a sharedLib descriptor");
+                }
+                slName = desc.getSharedLibrary().getIdentification().getName();
+                LOG.info("Install ShareLib " + slName);
+                if (sharedLibinstallers.containsKey(slName)) {
+                    throw new DeploymentException("ShareLib " + slName+ " is already installed");
+                }
+                SharedLibInstaller slInstaller = new SharedLibInstaller(slName, this.getAdminService());
+                slInstaller.install(location);
+                sharedLibinstallers.put(slName, slInstaller);
+                       
+                return slName;
+            } else {
+                throw new DeploymentException("Could not find JBI descriptor");
+            }
+        } else {
+            throw new DeploymentException("Could not find sharedLib");
+        }
+    	} catch (Exception e) {
+    		LOG.error("install SharedLib failed", e);
+    	}
+    	return slName;
     }
 
     /**
@@ -179,9 +216,20 @@
      * @return - true iff the uninstall was successful.
      */
     public boolean uninstallSharedLibrary(String aSharedLibName) {
-        // TODO
-    	return false;
-        
+    	boolean result = false;
+        try {
+        	SharedLibInstaller installer = sharedLibinstallers.remove(aSharedLibName);
+        	result = installer != null;
+            if (result) {
+            	installer.uninstall();
+                
+            }
+        } catch (Exception e) {
+            String errStr = "Problem uninstall SL: " + aSharedLibName;
+            LOG.error(errStr, e);
+        } finally {
+        }
+        return result;
     }
 
 	/**
@@ -211,7 +259,7 @@
                 throw new DeploymentException("Could not find JBI descriptor");
             }
         } else {
-            throw new DeploymentException("Could not find JBI descriptor");
+            throw new DeploymentException("Could not find component");
         }
     }
     
@@ -229,7 +277,7 @@
             if (installers.containsKey(componentName)) {
                 throw new DeploymentException("Component " + componentName + " is already installed");
             }
-            Installer installer = null;
+            ComponentInstaller installer = null;
 			try {
 				installer = doInstallArchive(desc, jarfile);
 			} catch (Exception e1) {
@@ -289,5 +337,13 @@
 	public ManagementAgent getManagementAgent() {
 		return managementAgent;
 	}
+
+	public boolean containsSharedLibrary(String sharedLibraryName) {
+		return sharedLibinstallers.containsKey(sharedLibraryName);
+	}
+	
+	public Set<String> getInstalledSharedLibs() {
+		return sharedLibinstallers.keySet(); 
+	}
 	
 }

Added: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/SharedLibInstaller.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/SharedLibInstaller.java?rev=741500&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/SharedLibInstaller.java (added)
+++ servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/SharedLibInstaller.java Fri Feb  6 10:13:30 2009
@@ -0,0 +1,59 @@
+/*
+ * 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.jbi.management;
+
+import javax.jbi.JBIException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+
+public class SharedLibInstaller extends AbstractInstaller {
+
+	private static final Log LOGGER = LogFactory.getLog(SharedLibInstaller.class);
+	
+	private String name;
+    private AdminService adminService;
+	
+	public SharedLibInstaller(String name, AdminService adminService) {
+		this.name = name;
+		this.adminService = adminService;
+		setBundleContext(adminService.getBundleContext());
+	}
+	
+	public void install(String filename) {
+		deployFile(filename);
+	}
+	
+	public void uninstall() throws javax.jbi.JBIException {
+		try {
+        	Bundle bundle = getBundle();
+
+            if (bundle == null) {
+                LOGGER.warn("Could not find Bundle for shared lib: " + name);
+            }
+            else {
+                bundle.stop();
+                bundle.uninstall();
+            }
+        } catch (BundleException e) {
+        	LOGGER.error("failed to uninstall shared lib: " + name, e);
+        	throw new JBIException(e);
+		} 
+    }
+}

Propchange: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/SharedLibInstaller.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: servicemix/smx4/nmr/trunk/jbi/management/src/main/java/org/apache/servicemix/jbi/management/SharedLibInstaller.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date