geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xuhaih...@apache.org
Subject svn commit: r1075818 - in /geronimo/server/branches/3.0-M2/framework/modules: geronimo-kernel/src/main/java/org/apache/geronimo/kernel/repository/ geronimo-system/src/main/java/org/apache/geronimo/system/configuration/
Date Tue, 01 Mar 2011 13:41:44 GMT
Author: xuhaihong
Date: Tue Mar  1 13:41:43 2011
New Revision: 1075818

URL: http://svn.apache.org/viewvc?rev=1075818&view=rev
Log:
Port changes from trunk, some new methods are added in DependencyManager for accessing dependency
tree

Modified:
    geronimo/server/branches/3.0-M2/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/repository/AbstractRepository.java
    geronimo/server/branches/3.0-M2/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java

Modified: geronimo/server/branches/3.0-M2/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/repository/AbstractRepository.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-M2/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/repository/AbstractRepository.java?rev=1075818&r1=1075817&r2=1075818&view=diff
==============================================================================
--- geronimo/server/branches/3.0-M2/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/repository/AbstractRepository.java
(original)
+++ geronimo/server/branches/3.0-M2/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/repository/AbstractRepository.java
Tue Mar  1 13:41:43 2011
@@ -1,133 +1,137 @@
-/**
- *  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.geronimo.kernel.repository;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.regex.Pattern;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipException;
-import java.util.zip.ZipFile;
-
-import org.apache.geronimo.kernel.util.InputUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @version $Rev: 506425 $ $Date: 2007-02-12 22:49:46 +1100 (Mon, 12 Feb 2007) $
- */
-public abstract class AbstractRepository implements WriteableRepository {
-    protected final Logger log = LoggerFactory.getLogger(getClass());
-    private final static ArtifactTypeHandler DEFAULT_TYPE_HANDLER = new CopyArtifactTypeHandler();
-    private final static Pattern ILLEGAL_CHARS = Pattern.compile("[\\.]{2}|[()<>,;:\\\\/\"\']");
-    protected final File rootFile;
-    private final Map<String, ArtifactTypeHandler> typeHandlers = new HashMap<String,
ArtifactTypeHandler>();
-
-    public AbstractRepository(File rootFile) {
-        if (rootFile == null) throw new NullPointerException("root is null");
-
-        if (!rootFile.exists() || !rootFile.isDirectory() || !rootFile.canRead()) {
-            throw new IllegalStateException("Maven2Repository must have a root that's a valid
readable directory (not " + rootFile.getAbsolutePath() + ")");
-        }
-
-        this.rootFile = rootFile;
-        log.debug("Repository root is {}", rootFile.getAbsolutePath());
-
-//        typeHandlers.put("car", new UnpackArtifactTypeHandler());
-    }
-
-    public boolean contains(Artifact artifact) {
-        // Note: getLocation(artifact) does an artifact.isResolved() check - no need to do
it here.
-        File location = getLocation(artifact);
-        return location.canRead() && (location.isFile() || new File(location, "META-INF").isDirectory());
-    }
-
-    public void setTypeHandler(String type, ArtifactTypeHandler handler) {
-        typeHandlers.put(type, handler);
-    }
-
-    public void copyToRepository(File source, Artifact destination, FileWriteMonitor monitor)
throws IOException {
-
-        // ensure there are no illegal chars in destination elements
-        InputUtils.validateSafeInput(new ArrayList(Arrays.asList(destination.getGroupId(),
destination.getArtifactId(), destination.getVersion().toString(), destination.getType())));
-
-        if(!destination.isResolved()) {
-            throw new IllegalArgumentException("Artifact "+destination+" is not fully resolved");
-        }
-        if (!source.exists() || !source.canRead() || source.isDirectory()) {
-            throw new IllegalArgumentException("Cannot read source file at " + source.getAbsolutePath());
-        }
-        int size = 0;
-        ZipFile zip = null;
-        try {
-            zip = new ZipFile(source);
-            for (Enumeration entries=zip.entries(); entries.hasMoreElements();) {
-            	ZipEntry entry = (ZipEntry)entries.nextElement();
-            	size += entry.getSize();
-            }
-        } catch (ZipException ze) {
-            size = (int)source.length();
-        } finally {
-            if (zip != null) {
-                zip.close();
-            }
-        }
-        FileInputStream is = new FileInputStream(source);
-        try {
-            copyToRepository(is, size, destination, monitor);
-        } finally {
-            try {
-                is.close();
-            } catch (IOException ignored) {
-                // ignored
-            }
-        }
-    }
-
-    public void copyToRepository(InputStream source, int size, Artifact destination, FileWriteMonitor
monitor) throws IOException {
-        if(!destination.isResolved()) {
-            throw new IllegalArgumentException("Artifact "+destination+" is not fully resolved");
-        }
-        // is this a writable repository
-        if (!rootFile.canWrite()) {
-            throw new IllegalStateException("This repository is not writable: " + rootFile.getAbsolutePath()
+ ")");
-        }
-
-        // where are we going to install the file
-        File location = getLocation(destination);
-
-        // assure that there isn't already a file installed at the specified location
-        if (location.exists()) {
-            throw new IllegalArgumentException("Destination " + location.getAbsolutePath()
+ " already exists!");
-        }
-
-        ArtifactTypeHandler typeHandler = typeHandlers.get(destination.getType());
-        if (typeHandler == null) typeHandler = DEFAULT_TYPE_HANDLER;
-        typeHandler.install(source, size, destination, monitor, location);
-        
-        if (destination.getType().equalsIgnoreCase("car")) {
-            log.debug("Installed module configuration; id={}; location={}", destination,
location);
-        }
-    }
-}
+/**
+ *  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.geronimo.kernel.repository;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
+
+import org.apache.geronimo.kernel.util.InputUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @version $Rev: 506425 $ $Date: 2007-02-12 22:49:46 +1100 (Mon, 12 Feb 2007) $
+ */
+public abstract class AbstractRepository implements WriteableRepository {
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+    private final static ArtifactTypeHandler DEFAULT_TYPE_HANDLER = new CopyArtifactTypeHandler();
+    private final static Pattern ILLEGAL_CHARS = Pattern.compile("[\\.]{2}|[()<>,;:\\\\/\"\']");
+    protected final File rootFile;
+    private final Map<String, ArtifactTypeHandler> typeHandlers = new HashMap<String,
ArtifactTypeHandler>();
+
+    public AbstractRepository(File rootFile) {
+        if (rootFile == null) throw new NullPointerException("root is null");
+
+        if (!rootFile.exists() || !rootFile.isDirectory() || !rootFile.canRead()) {
+            throw new IllegalStateException("Maven2Repository must have a root that's a valid
readable directory (not " + rootFile.getAbsolutePath() + ")");
+        }
+
+        this.rootFile = rootFile;
+        log.debug("Repository root is {}", rootFile.getAbsolutePath());
+
+//        typeHandlers.put("car", new UnpackArtifactTypeHandler());
+    }
+
+    public boolean contains(Artifact artifact) {
+        // Note: getLocation(artifact) does an artifact.isResolved() check - no need to do
it here.
+        File location = getLocation(artifact);
+        return location.canRead() && (location.isFile() || new File(location, "META-INF").isDirectory());
+    }
+
+    public void setTypeHandler(String type, ArtifactTypeHandler handler) {
+        typeHandlers.put(type, handler);
+    }
+
+    public void copyToRepository(File source, Artifact destination, FileWriteMonitor monitor)
throws IOException {
+
+        // ensure there are no illegal chars in destination elements
+        InputUtils.validateSafeInput(new ArrayList(Arrays.asList(destination.getGroupId(),
destination.getArtifactId(), destination.getVersion().toString(), destination.getType())));
+
+        if(!destination.isResolved()) {
+            throw new IllegalArgumentException("Artifact "+destination+" is not fully resolved");
+        }
+        if (!source.exists() || !source.canRead() || source.isDirectory()) {
+            throw new IllegalArgumentException("Cannot read source file at " + source.getAbsolutePath());
+        }
+        int size = 0;
+        ZipFile zip = null;
+        try {
+            zip = new ZipFile(source);
+            for (Enumeration entries=zip.entries(); entries.hasMoreElements();) {
+            	ZipEntry entry = (ZipEntry)entries.nextElement();
+            	size += entry.getSize();
+            }
+        } catch (ZipException ze) {
+            size = (int)source.length();
+        } finally {
+            if (zip != null) {
+                zip.close();
+            }
+        }
+        FileInputStream is = new FileInputStream(source);
+        try {
+            copyToRepository(is, size, destination, monitor);
+        } finally {
+            try {
+                is.close();
+            } catch (IOException ignored) {
+                // ignored
+            }
+        }
+    }
+
+    public void copyToRepository(InputStream source, int size, Artifact destination, FileWriteMonitor
monitor) throws IOException {
+        if(!destination.isResolved()) {
+            throw new IllegalArgumentException("Artifact "+destination+" is not fully resolved");
+        }
+        // is this a writable repository
+        if (!rootFile.canWrite()) {
+            throw new IllegalStateException("This repository is not writable: " + rootFile.getAbsolutePath()
+ ")");
+        }
+
+        // where are we going to install the file
+        File location = getLocation(destination);
+
+        // assure that there isn't already a file installed at the specified location
+        if (location.exists()) {
+            throw new IllegalArgumentException("Destination " + location.getAbsolutePath()
+ " already exists!");
+        }
+
+        ArtifactTypeHandler typeHandler = typeHandlers.get(destination.getType());
+        if (typeHandler == null) typeHandler = DEFAULT_TYPE_HANDLER;
+        typeHandler.install(source, size, destination, monitor, location);
+
+        if (destination.getType().equalsIgnoreCase("car")) {
+            log.debug("Installed module configuration; id={}; location={}", destination,
location);
+        }
+    }
+
+    public File getRootFile() {
+        return rootFile;
+    }
+}

Modified: geronimo/server/branches/3.0-M2/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-M2/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java?rev=1075818&r1=1075817&r2=1075818&view=diff
==============================================================================
--- geronimo/server/branches/3.0-M2/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java
(original)
+++ geronimo/server/branches/3.0-M2/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java
Tue Mar  1 13:41:43 2011
@@ -19,6 +19,7 @@
 
 package org.apache.geronimo.system.configuration;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -34,6 +35,7 @@ import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.felix.bundlerepository.RepositoryAdmin;
+import org.apache.geronimo.gbean.GBeanLifecycle;
 import org.apache.geronimo.gbean.annotation.GBean;
 import org.apache.geronimo.gbean.annotation.OsgiService;
 import org.apache.geronimo.gbean.annotation.ParamReference;
@@ -41,10 +43,12 @@ import org.apache.geronimo.gbean.annotat
 import org.apache.geronimo.gbean.annotation.SpecialAttributeType;
 import org.apache.geronimo.kernel.config.InvalidConfigException;
 import org.apache.geronimo.kernel.config.NoSuchConfigException;
+import org.apache.geronimo.kernel.repository.AbstractRepository;
 import org.apache.geronimo.kernel.repository.Artifact;
 import org.apache.geronimo.kernel.repository.ArtifactResolver;
 import org.apache.geronimo.kernel.repository.MissingDependencyException;
 import org.apache.geronimo.kernel.repository.Repository;
+import org.apache.geronimo.kernel.util.IOUtils;
 import org.apache.geronimo.system.plugin.model.DependencyType;
 import org.apache.geronimo.system.plugin.model.PluginArtifactType;
 import org.apache.geronimo.system.plugin.model.PluginType;
@@ -68,52 +72,38 @@ import org.slf4j.LoggerFactory;
  */
 @GBean
 @OsgiService
-public class DependencyManager implements SynchronousBundleListener {
+public class DependencyManager implements SynchronousBundleListener, GBeanLifecycle {
+
     private static final Logger log = LoggerFactory.getLogger(DependencyManager.class);
 
     private final BundleContext bundleContext;
+
     private final Collection<Repository> repositories;
 
-    private final RepositoryAdmin repositoryAdmin;
+    private RepositoryAdmin repositoryAdmin;
 
     private final ArtifactResolver artifactResolver;
 
-    private final Map<Long, PluginArtifactType> pluginMap =
-            Collections.synchronizedMap(new WeakHashMap<Long, PluginArtifactType>());
+    private final Map<Long, PluginArtifactType> pluginMap = Collections.synchronizedMap(new
WeakHashMap<Long, PluginArtifactType>());
 
     private final Map<Long, Set<Long>> dependentBundleIdsMap = new ConcurrentHashMap<Long,
Set<Long>>();
-    private final Map<Long, Set<ExportPackage>> bundleExportPackagesMap = new
ConcurrentHashMap<Long, Set<ExportPackage>>();
+
+    private final Map<Long, Set<Long>> fullDependentBundleIdsMap = new ConcurrentHashMap<Long,
Set<Long>>();
+
+    private final Map<Long, Set<ExportPackage>> bundleExportPackagesMap = new
HashMap<Long, Set<ExportPackage>>();
+
     private final Map<Artifact, Bundle> artifactBundleMap = new ConcurrentHashMap<Artifact,
Bundle>();
 
+    private final Map<Long, Artifact> bundleIdArtifactMap = new ConcurrentHashMap<Long,
Artifact>();
+
+    private ServiceReference respositoryAdminReference;
+
     public DependencyManager(@ParamSpecial(type = SpecialAttributeType.bundleContext) BundleContext
bundleContext,
-                             @ParamReference(name = "Repositories", namingType = "Repository")
Collection<Repository> repositories,
-                             @ParamReference(name="ArtifactResolver", namingType = "ArtifactResolver")
ArtifactResolver artifactResolver) {
+            @ParamReference(name = "Repositories", namingType = "Repository") Collection<Repository>
repositories,
+            @ParamReference(name = "ArtifactResolver", namingType = "ArtifactResolver") ArtifactResolver
artifactResolver) {
         this.bundleContext = bundleContext;
         this.repositories = repositories;
         this.artifactResolver = artifactResolver;
-        bundleContext.addBundleListener(this);
-        ServiceReference ref = bundleContext.getServiceReference(RepositoryAdmin.class.getName());
-        repositoryAdmin = ref == null? null: (RepositoryAdmin) bundleContext.getService(ref);
-        //init installed bundles
-        for (Bundle bundle : bundleContext.getBundles()) {
-            installed(bundle);
-        }
-        //Check the car who loads me ...
-        try {
-            PluginArtifactType pluginArtifact = getCachedPluginMetadata(bundleContext.getBundle());
-            if (pluginArtifact != null) {
-                Set<Long> dependentBundleIds = new HashSet<Long>();
-                for (DependencyType dependency : pluginArtifact.getDependency()) {
-                    Bundle dependentBundle = getBundle(dependency.toArtifact());
-                    if (dependentBundle != null) {
-                        dependentBundleIds.add(dependentBundle.getBundleId());
-                    }
-                }
-                dependentBundleIdsMap.put(bundleContext.getBundle().getBundleId(), dependentBundleIds);
-            }
-        } catch (Exception e) {
-            log.error("Fail to read the dependency info from bundle " + bundleContext.getBundle().getLocation());
-        }
     }
 
     public void bundleChanged(BundleEvent bundleEvent) {
@@ -129,17 +119,23 @@ public class DependencyManager implement
     }
 
     public Set<ExportPackage> getExportedPackages(Bundle bundle) {
-        Set<ExportPackage> exportPackages = bundleExportPackagesMap.get(bundle.getBundleId());
-        if (exportPackages == null) {
-            exportPackages = getExportPackagesInternal(bundle);
-            bundleExportPackagesMap.put(bundle.getBundleId(), exportPackages);
+        return getExportedPackages(bundle.getBundleId());
+    }
+
+    public Set<ExportPackage> getExportedPackages(Long bundleId) {
+        synchronized (bundleExportPackagesMap) {
+            Set<ExportPackage> exportPackages = bundleExportPackagesMap.get(bundleId);
+            if (exportPackages == null) {
+                exportPackages = getExportPackagesInternal(bundleContext.getBundle(bundleId));
+                bundleExportPackagesMap.put(bundleId, exportPackages);
+            }
+            return exportPackages;
         }
-        return exportPackages;
     }
 
     public List<Bundle> getDependentBundles(Bundle bundle) {
-        Set<Long> dependentBundleIds = dependentBundleIdsMap.get(bundle.getBundleId());
-        if (dependentBundleIds == null || dependentBundleIds.size() == 0) {
+        Set<Long> dependentBundleIds = getDependentBundleIds(bundle);
+        if (dependentBundleIds.size() == 0) {
             return Collections.<Bundle> emptyList();
         }
         List<Bundle> dependentBundles = new ArrayList<Bundle>(dependentBundleIds.size());
@@ -149,8 +145,38 @@ public class DependencyManager implement
         return dependentBundles;
     }
 
+    public Set<Long> getDependentBundleIds(Bundle bundle) {
+        Set<Long> dependentBundleIds = dependentBundleIdsMap.get(bundle.getBundleId());
+        return dependentBundleIds == null ? Collections.<Long> emptySet() : new HashSet<Long>(dependentBundleIds);
+    }
+
+    public List<Bundle> getFullDependentBundles(Bundle bundle) {
+        return getFullDependentBundles(bundle.getBundleId());
+    }
+
+    public List<Bundle> getFullDependentBundles(Long bundleId) {
+        Set<Long> fullDependentBundleIds = getFullDependentBundleIds(bundleId);
+        if (fullDependentBundleIds.size() == 0) {
+            return Collections.<Bundle> emptyList();
+        }
+        List<Bundle> dependentBundles = new ArrayList<Bundle>(fullDependentBundleIds.size());
+        for (Long dependentBundleId : fullDependentBundleIds) {
+            dependentBundles.add(bundleContext.getBundle(dependentBundleId));
+        }
+        return dependentBundles;
+    }
+
+    public Set<Long> getFullDependentBundleIds(Bundle bundle) {
+        return getFullDependentBundleIds(bundle.getBundleId());
+    }
+
+    public Set<Long> getFullDependentBundleIds(Long bundleId) {
+        Set<Long> fullDependentBundleIds = fullDependentBundleIdsMap.get(bundleId);
+        return fullDependentBundleIds == null ? Collections.<Long> emptySet() : new
HashSet<Long>(fullDependentBundleIds);
+    }
+
     public Bundle getBundle(Artifact artifact) {
-        if(!artifact.isResolved()) {
+        if (!artifact.isResolved()) {
             try {
                 if (artifactResolver != null) {
                     artifact = artifactResolver.resolveInClassLoader(artifact);
@@ -161,21 +187,53 @@ public class DependencyManager implement
         return artifactBundleMap.get(artifact);
     }
 
+    public Artifact getArtifact(long bundleId) {
+        return bundleIdArtifactMap.get(bundleId);
+    }
+
     public Artifact toArtifact(String installationLocation) {
-        if (installationLocation == null || !installationLocation.startsWith("mvn:")) {
+        if (installationLocation == null) {
             return null;
         }
-        String[] artifactFragments = installationLocation.substring(4).split("[/]");
-        if(artifactFragments.length < 2) {
-            return null;
+        if (installationLocation.startsWith("mvn:")) {
+            String[] artifactFragments = installationLocation.substring(4).split("[/]");
+            if (artifactFragments.length < 2) {
+                return null;
+            }
+            return new Artifact(artifactFragments[0], artifactFragments[1], artifactFragments.length
> 2 ? artifactFragments[2] : "",
+                    artifactFragments.length > 3 && artifactFragments[3].length()
> 0 ? artifactFragments[3] : "jar");
+        } else if(installationLocation.startsWith("reference:file://")) {
+            //TODO a better way for this ???
+            installationLocation = installationLocation.substring("reference:file://".length());
+            for (Repository repo : repositories) {
+                if (repo instanceof AbstractRepository) {
+                    File rootFile = ((AbstractRepository) repo).getRootFile();
+                    if (installationLocation.startsWith(rootFile.getAbsolutePath())) {
+                        String artifactString = installationLocation.substring(rootFile.getAbsolutePath().length());
+                        if (artifactString.startsWith(File.separator)) {
+                            artifactString = artifactString.substring(File.separator.length());
+                        }
+                        String[] filePathFragments = artifactString.split("[" + (File.separator.equals("\\")
? "\\\\" : File.separator) + "]");
+                        if (filePathFragments.length >= 4) {
+                            StringBuilder groupId = new StringBuilder(filePathFragments[0]);
+                            for (int i = 1; i <= filePathFragments.length - 4; i++) {
+                                groupId.append(".").append(filePathFragments[i]);
+                            }
+                            return new Artifact(groupId.toString(), filePathFragments[filePathFragments.length
- 3], filePathFragments[filePathFragments.length - 2],
+                                    filePathFragments[filePathFragments.length - 1].substring(filePathFragments[filePathFragments.length
- 1].lastIndexOf('.') + 1));
+                        }
+                    }
+                }
+            }
         }
-        return new Artifact(artifactFragments[0], artifactFragments[1], artifactFragments.length
> 2 ? artifactFragments[2] : "" ,artifactFragments.length > 3 && artifactFragments[3].length()
> 0 ?artifactFragments[3] : "jar" );
+        return null;
     }
 
     private void addArtifactBundleEntry(Bundle bundle) {
         Artifact artifact = toArtifact(bundle.getLocation());
         if (artifact != null) {
             artifactBundleMap.put(artifact, bundle);
+            bundleIdArtifactMap.put(bundle.getBundleId(), artifact);
         } else {
             if (log.isDebugEnabled()) {
                 log.debug("fail to resovle artifact from the bundle location " + bundle.getLocation());
@@ -187,6 +245,7 @@ public class DependencyManager implement
         Artifact artifact = toArtifact(bundle.getLocation());
         if (artifact != null) {
             artifactBundleMap.remove(artifact);
+            bundleIdArtifactMap.remove(bundle.getBundleId());
         }
     }
 
@@ -194,6 +253,10 @@ public class DependencyManager implement
         ServiceReference reference = null;
         try {
             reference = bundleContext.getServiceReference(PackageAdmin.class.getName());
+            if(reference == null) {
+                log.warn("No PackageAdmin service is found, fail to get export packages of
" + bundle.getLocation());
+                return Collections.<ExportPackage>emptySet();
+            }
             PackageAdmin packageAdmin = (PackageAdmin) bundleContext.getService(reference);
             ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle);
             if (exportedPackages != null) {
@@ -201,11 +264,11 @@ public class DependencyManager implement
                 for (ExportedPackage exportedPackage : exportedPackages) {
                     Map<String, String> attributes = new HashMap<String, String>();
                     attributes.put(Constants.VERSION_ATTRIBUTE, exportedPackage.getVersion().toString());
-                    exportPackageNames.add(new ExportPackage(exportedPackage.getName(), attributes,
Collections.<String, String>emptyMap()));
+                    exportPackageNames.add(new ExportPackage(exportedPackage.getName(), attributes,
Collections.<String, String> emptyMap()));
                 }
                 return exportPackageNames;
             }
-            return Collections.<ExportPackage>emptySet();
+            return Collections.<ExportPackage> emptySet();
         } finally {
             if (reference != null) {
                 bundleContext.ungetService(reference);
@@ -228,12 +291,12 @@ public class DependencyManager implement
             } catch (Throwable e) {
                 log.warn("Could not read geronimo metadata for bundle: " + bundle, e);
             } finally {
-                if (in != null) {
-                    try { in.close(); } catch (IOException e) {}
-                }
+                IOUtils.close(in);
             }
         } else {
-            log.debug("did not find geronimo-plugin.xml for bundle " + bundle);
+            if (log.isDebugEnabled()) {
+                log.debug("did not find geronimo-plugin.xml for bundle " + bundle);
+            }
         }
         return pluginArtifactType;
     }
@@ -241,6 +304,7 @@ public class DependencyManager implement
     private void uninstall(Bundle bundle) {
         removeArtifactBundleEntry(bundle);
         dependentBundleIdsMap.remove(bundle.getBundleId());
+        fullDependentBundleIdsMap.remove(bundle.getBundleId());
         pluginMap.remove(bundle.getBundleId());
     }
 
@@ -264,7 +328,7 @@ public class DependencyManager implement
         }
     }
 
-    private PluginArtifactType getCachedPluginMetadata(Bundle bundle) {
+    public PluginArtifactType getCachedPluginMetadata(Bundle bundle) {
         PluginArtifactType pluginArtifactType = pluginMap.get(bundle.getBundleId());
         if (pluginArtifactType == null) {
             pluginArtifactType = getPluginMetadata(bundle);
@@ -277,39 +341,46 @@ public class DependencyManager implement
         return pluginArtifactType;
     }
 
-    private void installed(Bundle bundle) {
+    public void installed(Bundle bundle) {
+        if (bundleIdArtifactMap.containsKey(bundle.getBundleId())) {
+            return;
+        }
         addArtifactBundleEntry(bundle);
         PluginArtifactType pluginArtifactType = getCachedPluginMetadata(bundle);
-        if (pluginArtifactType != null) {
-            List<DependencyType> dependencies = pluginArtifactType.getDependency();
-            Set<Long> dependentBundleIds = new HashSet<Long>();
-            try {
-                for (DependencyType dependencyType : dependencies) {
-                    if (log.isDebugEnabled()) {
-                        log.debug("Installing artifact: " + dependencyType);
-                    }
-                    Artifact artifact = dependencyType.toArtifact();
-                    if (artifactResolver != null) {
-                        artifact = artifactResolver.resolveInClassLoader(artifact);
-                    }
-                    String location = locateBundle(artifact);
-                    try {
-                        Bundle installedBundle = bundleContext.installBundle(location);
-                        if (dependentBundleIds.add(installedBundle.getBundleId())) {
-                            Set<Long> parentDependentBundleIds = dependentBundleIdsMap.get(installedBundle
-                                    .getBundleId());
-                            if (parentDependentBundleIds != null) {
-                                dependentBundleIds.addAll(parentDependentBundleIds);
-                            }
+        if (pluginArtifactType == null) {
+            return;
+        }
+        List<DependencyType> dependencies = pluginArtifactType.getDependency();
+        Set<Long> dependentBundleIds = new HashSet<Long>();
+        Set<Long> fullDependentBundleIds = new HashSet<Long>();
+        try {
+            for (DependencyType dependencyType : dependencies) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Installing artifact: " + dependencyType);
+                }
+                Artifact artifact = dependencyType.toArtifact();
+                if (artifactResolver != null) {
+                    artifact = artifactResolver.resolveInClassLoader(artifact);
+                }
+                String location = locateBundle(artifact);
+                try {
+                    Bundle installedDependentBundle = bundleContext.installBundle(location);
+                    long installedDependentBundleId = installedDependentBundle.getBundleId();
+                    dependentBundleIds.add(installedDependentBundleId);
+                    if (fullDependentBundleIds.add(installedDependentBundleId)) {
+                        Set<Long> parentDependentBundleIds = fullDependentBundleIdsMap.get(installedDependentBundleId);
+                        if (parentDependentBundleIds != null) {
+                            fullDependentBundleIds.addAll(parentDependentBundleIds);
                         }
-                    } catch (BundleException e) {
-                        log.warn("Could not install bundle for artifact: " + artifact, e);
                     }
+                } catch (BundleException e) {
+                    log.warn("Could not install bundle for artifact: " + artifact, e);
                 }
-                dependentBundleIdsMap.put(bundle.getBundleId(), dependentBundleIds);
-            } catch (Exception e) {
-                log.error("Could not install bundle dependency", e);
             }
+            fullDependentBundleIdsMap.put(bundle.getBundleId(), fullDependentBundleIds);
+            dependentBundleIdsMap.put(bundle.getBundleId(), dependentBundleIds);
+        } catch (Exception e) {
+            log.error("Could not install bundle dependency", e);
         }
     }
 
@@ -329,7 +400,7 @@ public class DependencyManager implement
                     }
                     String location = locateBundle(artifact);
                     Bundle b = bundleContext.installBundle(location);
-                    if (b.getState() != Bundle.ACTIVE) {
+                    if (b.getState() != Bundle.ACTIVE ) {
                         bundles.add(b);
                     }
                 }
@@ -351,7 +422,8 @@ public class DependencyManager implement
 
     private String locateBundle(Artifact configurationId) throws NoSuchConfigException, IOException,
InvalidConfigException {
         if (System.getProperty("geronimo.build.car") == null) {
-            return "mvn:" + configurationId.getGroupId() + "/" + configurationId.getArtifactId()
+ "/" + configurationId.getVersion() + ("jar".equals(configurationId.getType())?  "": "/"
+ configurationId.getType());
+            return "mvn:" + configurationId.getGroupId() + "/" + configurationId.getArtifactId()
+ "/" + configurationId.getVersion()
+                    + ("jar".equals(configurationId.getType()) ? "" : "/" + configurationId.getType());
         }
         for (Repository repo : repositories) {
             if (repo.contains(configurationId)) {
@@ -360,4 +432,59 @@ public class DependencyManager implement
         }
         throw new NoSuchConfigException(configurationId);
     }
+
+    @Override
+    public void doStart() throws Exception {
+        bundleContext.addBundleListener(this);
+        respositoryAdminReference = bundleContext.getServiceReference(RepositoryAdmin.class.getName());
+        repositoryAdmin = respositoryAdminReference == null ? null : (RepositoryAdmin) bundleContext.getService(respositoryAdminReference);
+        //init installed bundles
+        for (Bundle bundle : bundleContext.getBundles()) {
+            installed(bundle);
+        }
+        //Check the car who loads me ...
+        try {
+            PluginArtifactType pluginArtifact = getCachedPluginMetadata(bundleContext.getBundle());
+            if (pluginArtifact != null) {
+                Set<Long> dependentBundleIds = new HashSet<Long>();
+                for (DependencyType dependency : pluginArtifact.getDependency()) {
+                    Bundle dependentBundle = getBundle(dependency.toArtifact());
+                    if (dependentBundle != null) {
+                        dependentBundleIds.add(dependentBundle.getBundleId());
+                    }
+                }
+                long bundleId = bundleContext.getBundle().getBundleId();
+                dependentBundleIdsMap.put(bundleId, dependentBundleIds);
+                fullDependentBundleIdsMap.put(bundleId, dependentBundleIds);
+            }
+        } catch (Exception e) {
+            log.error("Fail to read the dependency info from bundle " + bundleContext.getBundle().getLocation());
+        }
+    }
+
+    @Override
+    public void doStop() throws Exception {
+        if (respositoryAdminReference != null) {
+            try {
+                bundleContext.ungetService(respositoryAdminReference);
+            } catch (Exception e) {
+            }
+        }
+        bundleContext.removeBundleListener(this);
+        //Some clean up work
+        pluginMap.clear();
+        dependentBundleIdsMap.clear();
+        fullDependentBundleIdsMap.clear();
+        bundleExportPackagesMap.clear();
+        artifactBundleMap.clear();
+        bundleIdArtifactMap.clear();
+    }
+
+    @Override
+    public void doFail() {
+        try {
+            doStop();
+        } catch (Exception e) {
+        }
+    }
 }



Mime
View raw message