You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by dj...@apache.org on 2011/04/27 01:15:33 UTC
svn commit: r1096945 [3/4] - in /geronimo/server/trunk:
framework/buildsupport/car-maven-plugin/src/main/java/org/apache/geronimo/mavenplugins/car/
framework/configs/client-system/src/main/plan/
framework/configs/j2ee-system/ framework/configs/j2ee-sys...
Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/MockBundleContext.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/MockBundleContext.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/MockBundleContext.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/MockBundleContext.java Tue Apr 26 23:15:32 2011
@@ -111,7 +111,7 @@ public class MockBundleContext implement
}
BundleContext bundleContext = new WrappingBundleContext(bundle);
bundle.setBundleContext(bundleContext);
- configurationData.setBundleContext(bundleContext);
+ configurationData.setBundle(bundleContext.getBundle());
try {
configurationManager.loadConfiguration(configurationData);
} catch (NoSuchConfigException e) {
Modified: geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java Tue Apr 26 23:15:32 2011
@@ -27,7 +27,6 @@ import org.apache.geronimo.kernel.osgi.M
import org.apache.geronimo.kernel.repository.Artifact;
import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
-import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
import org.apache.geronimo.naming.java.RootContext;
import org.apache.geronimo.naming.java.javaURLContextFactory;
import org.apache.xbean.naming.context.ImmutableContext;
@@ -105,7 +104,7 @@ public class JavaCompGBeanTest extends A
ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
ConfigurationData configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
- configurationData.setBundleContext(bundleContext);
+ configurationData.setBundle(bundleContext.getBundle());
configurationData.addGBean("GlobalContext", GlobalContextGBean.class);
configurationData.addGBean("JavaComp", JavaCompContextGBean.class);
Modified: geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/KernelContextGBeanTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/KernelContextGBeanTest.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/KernelContextGBeanTest.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/KernelContextGBeanTest.java Tue Apr 26 23:15:32 2011
@@ -18,7 +18,6 @@ package org.apache.geronimo.gjndi;
import java.util.Collections;
import java.util.HashMap;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
@@ -45,7 +44,6 @@ import org.apache.geronimo.kernel.reposi
import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
import org.apache.geronimo.naming.java.RootContext;
import org.apache.xbean.naming.context.ImmutableContext;
-import org.apache.xbean.naming.global.GlobalContextManager;
/**
* @version $Rev$ $Date$
@@ -196,7 +194,7 @@ public class KernelContextGBeanTest exte
configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
- configurationData.setBundleContext(bundleContext);
+ configurationData.setBundle(bundleContext.getBundle());
configurationData.addGBean("GlobalContext", GlobalContextGBean.class);
GBeanInfoBuilder builder = new GBeanInfoBuilder(ImmutableContext.class);
Modified: geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/binding/GBeanBindingTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/binding/GBeanBindingTest.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/binding/GBeanBindingTest.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/binding/GBeanBindingTest.java Tue Apr 26 23:15:32 2011
@@ -55,7 +55,7 @@ public class GBeanBindingTest extends Ab
* InitialContext ctx = new InitialContext();
assertEq(globalBindings, ctx);
-
+
//
// stop ds2
//
@@ -109,7 +109,7 @@ public class GBeanBindingTest extends Ab
ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
ConfigurationData configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
- configurationData.setBundleContext(bundleContext);
+ configurationData.setBundle(bundleContext.getBundle());
configurationData.addGBean("GlobalContext", GlobalContextGBean.class);
// dataSources
@@ -130,7 +130,7 @@ public class GBeanBindingTest extends Ab
dsBinding.setAttribute("abstractNameQuery", new AbstractNameQuery(null,
Collections.singletonMap("name", "ds1"),
DataSource.class.getName()));
-
+
GBeanData ds1Binding = configurationData.addGBean("ds1Binding", GBeanBinding.class);
ds1Binding.setReferencePattern("Context", writableName);
ds1Binding.setAttribute("name", "ds1");
Added: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/ConfigurationExtender.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/ConfigurationExtender.java?rev=1096945&view=auto
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/ConfigurationExtender.java (added)
+++ geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/ConfigurationExtender.java Tue Apr 26 23:15:32 2011
@@ -0,0 +1,276 @@
+/**
+ * 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.system.configuration;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.geronimo.gbean.AbstractName;
+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;
+import org.apache.geronimo.gbean.annotation.ParamSpecial;
+import org.apache.geronimo.gbean.annotation.SpecialAttributeType;
+import org.apache.geronimo.kernel.Kernel;
+import org.apache.geronimo.kernel.config.Configuration;
+import org.apache.geronimo.kernel.config.ConfigurationData;
+import org.apache.geronimo.kernel.config.ConfigurationManager;
+import org.apache.geronimo.kernel.config.ConfigurationUtil;
+import org.apache.geronimo.kernel.config.InvalidConfigException;
+import org.apache.geronimo.kernel.config.LifecycleException;
+import org.apache.geronimo.kernel.config.NoSuchConfigException;
+import org.apache.geronimo.kernel.repository.Artifact;
+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.xbean.osgi.bundle.util.BundleUtils;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.SynchronousBundleListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @version $Rev$ $Date$
+ */
+@GBean
+@OsgiService
+public class ConfigurationExtender implements SynchronousBundleListener, GBeanLifecycle {
+
+ private static final Logger logger = LoggerFactory.getLogger(ConfigurationExtender.class);
+
+ private BundleContext bundleContext;
+
+ private ConfigurationManager configurationManager;
+
+ private DependencyManager dependencyManager;
+
+ private Map<Long, Artifact> bundleIdArtifactMap = new ConcurrentHashMap<Long, Artifact>();
+
+ private Set<Long> loadedBundleIds = Collections.synchronizedSet(new HashSet<Long>());
+
+ private Map<Long, BundleListener> bundleIdListenerMap = new ConcurrentHashMap<Long, BundleListener>();
+
+ public ConfigurationExtender(@ParamReference(name = "ConfigurationManager", namingType = "ConfigurationManager") ConfigurationManager configurationManager,
+ @ParamReference(name = "DependencyManager") DependencyManager dependencyManager, @ParamSpecial(type = SpecialAttributeType.bundleContext) BundleContext bundleContext) {
+ this.bundleContext = bundleContext;
+ this.configurationManager = configurationManager;
+ this.dependencyManager = dependencyManager;
+ for (Bundle bundle : bundleContext.getBundles()) {
+ //After the first start, all the bundles are cached, and some of them will be resolved by some rules by OSGi container.
+ //And DependencyManager is not installed while those bundles are resolved, so we need to invoke the install method to make sure
+ //that all the dependent bundles are resolved first, especially those car bundles.
+ if (!loadedBundleIds.contains(bundle.getBundleId())) {
+ recursiveLoadConfigurationData(bundle);
+ }
+ }
+ }
+
+ @Override
+ public void bundleChanged(BundleEvent event) {
+ try {
+ int eventType = event.getType();
+ Bundle bundle = event.getBundle();
+ if (eventType == BundleEvent.RESOLVED) {
+ if (!loadedBundleIds.contains(bundle.getBundleId())) {
+ recursiveLoadConfigurationData(bundle);
+ }
+ } else if (eventType == BundleEvent.STOPPING) {
+ stopConfiguration(bundle);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ protected void loadConfiguration(Bundle bundle) {
+ PluginArtifactType pluginArtifact = dependencyManager.getCachedPluginMetadata(bundle);
+ if (pluginArtifact == null) {
+ if (BundleUtils.isResolved(bundle)) {
+ loadedBundleIds.add(bundle.getBundleId());
+ }
+ return;
+ }
+ Set<Long> dependentBundleIds = new HashSet<Long>();
+ for (DependencyType dependency : pluginArtifact.getDependency()) {
+ Long dependentBundleId = dependencyManager.getBundle(dependency.toArtifact()).getBundleId();
+ if (!loadedBundleIds.contains(dependentBundleId)) {
+ dependentBundleIds.add(dependentBundleId);
+ }
+ }
+ if (dependentBundleIds.size() > 0) {
+ bundleIdListenerMap.put(bundle.getBundleId(), new BundleListener(bundle, dependentBundleIds));
+ return;
+ }
+ _loadConfiguration(bundle);
+ loadedBundleIds.add(bundle.getBundleId());
+
+ boolean bundleStatusChanged;
+ do {
+ bundleStatusChanged = false;
+ for (Iterator<Map.Entry<Long, BundleListener>> it = bundleIdListenerMap.entrySet().iterator(); it.hasNext();) {
+ Map.Entry<Long, BundleListener> entry = it.next();
+ if (entry.getValue().bundleChanged(bundle)) {
+ bundleStatusChanged = true;
+ it.remove();
+ }
+ }
+ } while (bundleStatusChanged);
+ }
+
+ private void _loadConfiguration(Bundle bundle) {
+ loadedBundleIds.add(bundle.getBundleId());
+ URL configSerURL = bundle.getEntry("META-INF/config.ser");
+ if (configSerURL == null) {
+ return;
+ }
+ InputStream in = null;
+ try {
+ in = configSerURL.openStream();
+ //TODO there are additional consistency checks in RepositoryConfigurationStore that we should use.
+ ConfigurationData data = ConfigurationUtil.readConfigurationData(in);
+ data.setBundle(bundle);
+ configurationManager.loadConfiguration(data);
+ bundleIdArtifactMap.put(bundle.getBundleId(), data.getId());
+ } catch (IOException e) {
+ logger.error("Could not read the config.ser file from bundle " + bundle.getLocation(), e);
+ } catch (ClassNotFoundException e) {
+ logger.error("Could not load required classes from bundle " + bundle.getLocation(), e);
+ } catch (NoSuchConfigException e) {
+ logger.error("Could not load configuration from bundle " + bundle.getLocation(), e);
+ } catch (LifecycleException e) {
+ logger.error("Could not load configuration from bundle " + bundle.getLocation(), e);
+ } finally {
+ IOUtils.close(in);
+ }
+ }
+
+ protected void stopConfiguration(Bundle bundle) {
+ Artifact id = getArtifact(bundle);
+ if (id == null) {
+ return;
+ }
+ ServiceReference kernelReference = null;
+ try {
+ kernelReference = bundleContext.getServiceReference(Kernel.class.getName());
+ if (kernelReference == null) {
+ return;
+ }
+ Kernel kernel = (Kernel) bundleContext.getService(kernelReference);
+ AbstractName name = Configuration.getConfigurationAbstractName(id);
+ //TODO investigate how this is called and whether just stopping/unloading the configuration gbean will
+ //leave the configuration model in a consistent state. We might need a shutdown flag set elsewhere to avoid
+ //overwriting the load attribute in config.xml. This code mimics the shutdown hook in KernelConfigurationManager
+ //see https://issues.apache.org/jira/browse/GERONIMO-4909
+ try {
+ kernel.stopGBean(name);
+ } catch (Exception e) {
+ //ignore
+ }
+ try {
+ kernel.unloadGBean(name);
+ } catch (Exception e) {
+ }
+ //TODO this code is more symmetrical with start, but currently sets the load attribute to false in config.xml,
+ //which prevents restarting the server.
+ //ConfigurationManager manager = ConfigurationUtil.getConfigurationManager(kernel);
+ //manager.unloadConfiguration(id);
+ } catch (InvalidConfigException e) {
+ //
+ } finally {
+ if (kernelReference != null) {
+ try {
+ bundleContext.ungetService(kernelReference);
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+
+ private Artifact getArtifact(Bundle bundle) {
+ return bundleIdArtifactMap.get(bundle.getBundleId());
+ }
+
+ @Override
+ public void doStart() throws Exception {
+ bundleContext.addBundleListener(this);
+ }
+
+ @Override
+ public void doStop() throws Exception {
+ bundleContext.removeBundleListener(this);
+ }
+
+ @Override
+ public void doFail() {
+ try {
+ doStop();
+ } catch (Exception e) {
+ }
+ }
+
+ private void recursiveLoadConfigurationData(Bundle bundle) {
+ PluginArtifactType pluginArtifact = dependencyManager.getCachedPluginMetadata(bundle);
+ if (pluginArtifact != null) {
+ for (DependencyType dependency : pluginArtifact.getDependency()) {
+ Bundle dependentBundle = dependencyManager.getBundle(dependency.toArtifact());
+ if (dependentBundle == null || loadedBundleIds.contains(dependentBundle.getBundleId())) {
+ continue;
+ }
+ recursiveLoadConfigurationData(dependentBundle);
+ loadConfiguration(dependentBundle);
+ }
+ }
+ loadConfiguration(bundle);
+ }
+
+ private class BundleListener {
+
+ private Bundle hostBundle;
+
+ private Set<Long> dependentBundleIds = new HashSet<Long>();
+
+ public BundleListener(Bundle hostBundle, Set<Long> dependentBundleIds) {
+ this.hostBundle = hostBundle;
+ this.dependentBundleIds = dependentBundleIds;
+ }
+
+ public boolean bundleChanged(Bundle bundle) {
+ Long dependentBundleId = bundle.getBundleId();
+ if (!dependentBundleIds.contains(dependentBundleId)) {
+ return false;
+ }
+ dependentBundleIds.remove(dependentBundleId);
+ if (dependentBundleIds.size() == 0) {
+ _loadConfiguration(hostBundle);
+ return true;
+ }
+ return false;
+ }
+ }
+}
Propchange: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/ConfigurationExtender.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/ConfigurationExtender.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/ConfigurationExtender.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/DependencyManager.java Tue Apr 26 23:15:32 2011
@@ -1,490 +1,538 @@
-/*
- * 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.system.configuration;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-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;
-import org.apache.geronimo.gbean.annotation.ParamSpecial;
-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;
-import org.apache.geronimo.system.plugin.model.PluginXmlUtil;
-import org.apache.xbean.osgi.bundle.util.BundleDescription.ExportPackage;
-import org.apache.xbean.osgi.bundle.util.BundleUtils;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.SynchronousBundleListener;
-import org.osgi.service.packageadmin.ExportedPackage;
-import org.osgi.service.packageadmin.PackageAdmin;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @version $Rev$ $Date$
- */
-@GBean
-@OsgiService
-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 RepositoryAdmin repositoryAdmin;
-
- private final ArtifactResolver artifactResolver;
-
- 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<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) {
- this.bundleContext = bundleContext;
- this.repositories = repositories;
- this.artifactResolver = artifactResolver;
- }
-
- public void bundleChanged(BundleEvent bundleEvent) {
- int eventType = bundleEvent.getType();
- //TODO Need to optimize the codes, as we will not receive the INSTALLED event after the cache is created
- if (eventType == BundleEvent.INSTALLED || eventType == BundleEvent.RESOLVED) {
- installed(bundleEvent.getBundle());
- } else if (eventType == BundleEvent.STARTING) {
- starting(bundleEvent.getBundle());
- } else if (eventType == BundleEvent.UNINSTALLED) {
- uninstall(bundleEvent.getBundle());
- }
- }
-
- public Set<ExportPackage> getExportedPackages(Bundle bundle) {
- 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;
- }
- }
-
- public List<Bundle> getDependentBundles(Bundle bundle) {
- Set<Long> dependentBundleIds = getDependentBundleIds(bundle);
- if (dependentBundleIds.size() == 0) {
- return Collections.<Bundle> emptyList();
- }
- List<Bundle> dependentBundles = new ArrayList<Bundle>(dependentBundleIds.size());
- for (Long dependentBundleId : dependentBundleIds) {
- dependentBundles.add(bundleContext.getBundle(dependentBundleId));
- }
- 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()) {
- try {
- if (artifactResolver != null) {
- artifact = artifactResolver.resolveInClassLoader(artifact);
- }
- } catch (MissingDependencyException e) {
- }
- }
- return artifactBundleMap.get(artifact);
- }
-
- public Artifact getArtifact(long bundleId) {
- return bundleIdArtifactMap.get(bundleId);
- }
-
- public Artifact toArtifact(String installationLocation) {
- if (installationLocation == null) {
- 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 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());
- }
- }
- }
-
- private void removeArtifactBundleEntry(Bundle bundle) {
- Artifact artifact = toArtifact(bundle.getLocation());
- if (artifact != null) {
- artifactBundleMap.remove(artifact);
- bundleIdArtifactMap.remove(bundle.getBundleId());
- }
- }
-
- private Set<ExportPackage> getExportPackagesInternal(Bundle bundle) {
- 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) {
- Set<ExportPackage> exportPackageNames = new HashSet<ExportPackage>();
- 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()));
- }
- return exportPackageNames;
- }
- return Collections.<ExportPackage> emptySet();
- } finally {
- if (reference != null) {
- bundleContext.ungetService(reference);
- }
- }
- }
-
- private PluginArtifactType getPluginMetadata(Bundle bundle) {
- PluginArtifactType pluginArtifactType = null;
- URL info = bundle.getEntry("META-INF/geronimo-plugin.xml");
- if (info != null) {
- if (log.isDebugEnabled()) {
- log.debug("found geronimo-plugin.xml for bundle " + bundle);
- }
- InputStream in = null;
- try {
- in = info.openStream();
- PluginType pluginType = PluginXmlUtil.loadPluginMetadata(in);
- pluginArtifactType = pluginType.getPluginArtifact().get(0);
- } catch (Throwable e) {
- log.warn("Could not read geronimo metadata for bundle: " + bundle, e);
- } finally {
- IOUtils.close(in);
- }
- } else {
- if (log.isDebugEnabled()) {
- log.debug("did not find geronimo-plugin.xml for bundle " + bundle);
- }
- }
- return pluginArtifactType;
- }
-
- private void uninstall(Bundle bundle) {
- removeArtifactBundleEntry(bundle);
- dependentBundleIdsMap.remove(bundle.getBundleId());
- fullDependentBundleIdsMap.remove(bundle.getBundleId());
- pluginMap.remove(bundle.getBundleId());
- }
-
- private void installRepository(Bundle bundle) {
- if (repositoryAdmin != null) {
- URL info = bundle.getEntry("OSGI-INF/obr/repository.xml");
- if (info != null) {
- if (log.isDebugEnabled()) {
- log.debug("found repository.xml for bundle " + bundle);
- }
- try {
- repositoryAdmin.addRepository(info);
- } catch (Exception e) {
- log.info("Error adding respository.xml for bundle " + bundle, e);
- }
- } else {
- if (log.isDebugEnabled()) {
- log.debug("did not find respository.xml for bundle " + bundle);
- }
- }
- }
- }
-
- public PluginArtifactType getCachedPluginMetadata(Bundle bundle) {
- PluginArtifactType pluginArtifactType = pluginMap.get(bundle.getBundleId());
- if (pluginArtifactType == null) {
- pluginArtifactType = getPluginMetadata(bundle);
- if (pluginArtifactType != null) {
- pluginMap.put(bundle.getBundleId(), pluginArtifactType);
- }
- //take this opportunity to install obr repo fragment
- // installRepository(bundle);
- }
- return pluginArtifactType;
- }
-
- public void installed(Bundle bundle) {
- if (bundleIdArtifactMap.containsKey(bundle.getBundleId())) {
- return;
- }
- addArtifactBundleEntry(bundle);
- PluginArtifactType pluginArtifactType = getCachedPluginMetadata(bundle);
- 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);
- }
- }
- fullDependentBundleIdsMap.put(bundle.getBundleId(), fullDependentBundleIds);
- dependentBundleIdsMap.put(bundle.getBundleId(), dependentBundleIds);
- } catch (Exception e) {
- log.error("Could not install bundle dependency", e);
- }
- }
-
- private void starting(Bundle bundle) {
- PluginArtifactType pluginArtifactType = getCachedPluginMetadata(bundle);
- if (pluginArtifactType != null) {
- List<Bundle> bundles = new ArrayList<Bundle>();
- List<DependencyType> dependencies = pluginArtifactType.getDependency();
- try {
- for (DependencyType dependencyType : dependencies) {
- if (log.isDebugEnabled()) {
- log.debug("Starting artifact: " + dependencyType);
- }
- Artifact artifact = dependencyType.toArtifact();
- if (artifactResolver != null) {
- artifact = artifactResolver.resolveInClassLoader(artifact);
- }
- String location = locateBundle(artifact);
- Bundle b = bundleContext.installBundle(location);
- if (b.getState() != Bundle.ACTIVE ) {
- bundles.add(b);
- }
- }
-
- for (Bundle b : bundles) {
- if (BundleUtils.canStart(b)) {
- try {
- b.start(Bundle.START_TRANSIENT);
- } catch (BundleException e) {
- log.warn("Could not start bundle: " + b, e);
- }
- }
- }
- } catch (Exception e) {
- log.error("Could not install bundle dependecy", e);
- }
- }
- }
-
- 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());
- }
- for (Repository repo : repositories) {
- if (repo.contains(configurationId)) {
- return "reference:file://" + repo.getLocation(configurationId).getAbsolutePath();
- }
- }
- 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) {
- }
- }
-}
+/*
+ * 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.system.configuration;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+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;
+import org.apache.geronimo.gbean.annotation.ParamSpecial;
+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.ImportType;
+import org.apache.geronimo.system.plugin.model.PluginArtifactType;
+import org.apache.geronimo.system.plugin.model.PluginType;
+import org.apache.geronimo.system.plugin.model.PluginXmlUtil;
+import org.apache.xbean.osgi.bundle.util.BundleDescription.ExportPackage;
+import org.apache.xbean.osgi.bundle.util.BundleUtils;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.SynchronousBundleListener;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @version $Rev: 1063963 $ $Date: 2011-01-27 10:41:13 +0800 (Thu, 27 Jan 2011) $
+ */
+@GBean
+@OsgiService
+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 RepositoryAdmin repositoryAdmin;
+
+ private final ArtifactResolver artifactResolver;
+
+ 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<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;
+
+ private ThreadLocal<List<Long>> threadInstalledBundles = new ThreadLocal<List<Long>>();
+
+ private ThreadLocal<List<Long>> threadEagerStartBundles = new ThreadLocal<List<Long>>();
+
+ public DependencyManager(@ParamSpecial(type = SpecialAttributeType.bundleContext) BundleContext bundleContext,
+ @ParamReference(name = "Repositories", namingType = "Repository") Collection<Repository> repositories,
+ @ParamReference(name = "ArtifactResolver", namingType = "ArtifactResolver") ArtifactResolver artifactResolver) {
+ this.bundleContext = bundleContext;
+ this.repositories = repositories;
+ this.artifactResolver = artifactResolver;
+ }
+
+ public void bundleChanged(BundleEvent bundleEvent) {
+ int eventType = bundleEvent.getType();
+ Bundle bundle = bundleEvent.getBundle();
+ if ((eventType == BundleEvent.INSTALLED || eventType == BundleEvent.RESOLVED) && !dependentBundleIdsMap.containsKey(bundle.getBundleId())) {
+ installed(bundle);
+ } else if (eventType == BundleEvent.STARTING) {
+ starting(bundle);
+ } else if (eventType == BundleEvent.UNINSTALLED) {
+ uninstall(bundle);
+ }
+ }
+
+ public Set<ExportPackage> getExportedPackages(Bundle bundle) {
+ 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;
+ }
+ }
+
+ public List<Bundle> getDependentBundles(Bundle bundle) {
+ Set<Long> dependentBundleIds = getDependentBundleIds(bundle);
+ if (dependentBundleIds.size() == 0) {
+ return Collections.<Bundle> emptyList();
+ }
+ List<Bundle> dependentBundles = new ArrayList<Bundle>(dependentBundleIds.size());
+ for (Long dependentBundleId : dependentBundleIds) {
+ dependentBundles.add(bundleContext.getBundle(dependentBundleId));
+ }
+ 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) {
+ try {
+ if (artifactResolver != null) {
+ artifact = artifactResolver.resolveInClassLoader(artifact);
+ }
+ } catch (MissingDependencyException e) {
+ }
+ return artifactBundleMap.get(artifact);
+ }
+
+ public Artifact getArtifact(long bundleId) {
+ return bundleIdArtifactMap.get(bundleId);
+ }
+
+ public Artifact toArtifact(String installationLocation) {
+ if (installationLocation == null) {
+ 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 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());
+ }
+ }
+ }
+
+ private void removeArtifactBundleEntry(Bundle bundle) {
+ Artifact artifact = toArtifact(bundle.getLocation());
+ if (artifact != null) {
+ artifactBundleMap.remove(artifact);
+ bundleIdArtifactMap.remove(bundle.getBundleId());
+ }
+ }
+
+ private Set<ExportPackage> getExportPackagesInternal(Bundle bundle) {
+ 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) {
+ Set<ExportPackage> exportPackageNames = new HashSet<ExportPackage>();
+ 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()));
+ }
+ return exportPackageNames;
+ }
+ return Collections.<ExportPackage> emptySet();
+ } finally {
+ if (reference != null) {
+ bundleContext.ungetService(reference);
+ }
+ }
+ }
+
+ private PluginArtifactType getPluginMetadata(Bundle bundle) {
+ PluginArtifactType pluginArtifactType = null;
+ URL info = bundle.getEntry("META-INF/geronimo-plugin.xml");
+ if (info != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("found geronimo-plugin.xml for bundle " + bundle);
+ }
+ InputStream in = null;
+ try {
+ in = info.openStream();
+ PluginType pluginType = PluginXmlUtil.loadPluginMetadata(in);
+ pluginArtifactType = pluginType.getPluginArtifact().get(0);
+ } catch (Throwable e) {
+ log.warn("Could not read geronimo metadata for bundle: " + bundle, e);
+ } finally {
+ IOUtils.close(in);
+ }
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("did not find geronimo-plugin.xml for bundle " + bundle);
+ }
+ }
+ return pluginArtifactType;
+ }
+
+ private void uninstall(Bundle bundle) {
+ removeArtifactBundleEntry(bundle);
+ dependentBundleIdsMap.remove(bundle.getBundleId());
+ fullDependentBundleIdsMap.remove(bundle.getBundleId());
+ pluginMap.remove(bundle.getBundleId());
+ }
+
+ private void installRepository(Bundle bundle) {
+ if (repositoryAdmin != null) {
+ URL info = bundle.getEntry("OSGI-INF/obr/repository.xml");
+ if (info != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("found repository.xml for bundle " + bundle);
+ }
+ try {
+ repositoryAdmin.addRepository(info);
+ } catch (Exception e) {
+ log.info("Error adding respository.xml for bundle " + bundle, e);
+ }
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("did not find respository.xml for bundle " + bundle);
+ }
+ }
+ }
+ }
+
+ public PluginArtifactType getCachedPluginMetadata(Bundle bundle) {
+ PluginArtifactType pluginArtifactType = pluginMap.get(bundle.getBundleId());
+ if (pluginArtifactType == null) {
+ pluginArtifactType = getPluginMetadata(bundle);
+ if (pluginArtifactType != null) {
+ pluginMap.put(bundle.getBundleId(), pluginArtifactType);
+ }
+ //take this opportunity to install obr repo fragment
+ // installRepository(bundle);
+ }
+ return pluginArtifactType;
+ }
+
+ public void installed(Bundle bundle) {
+ List<Long> installedBundles = threadInstalledBundles.get();
+ List<Long> eagerStartBundles = threadEagerStartBundles.get();
+ boolean created = false;
+ if (installedBundles == null) {
+ installedBundles = new LinkedList<Long>();
+ threadInstalledBundles.set(installedBundles);
+ eagerStartBundles = new LinkedList<Long>();
+ threadEagerStartBundles.set(eagerStartBundles);
+ created = true;
+ }
+ try {
+ if (bundleIdArtifactMap.containsKey(bundle.getBundleId())) {
+ return;
+ }
+ addArtifactBundleEntry(bundle);
+ PluginArtifactType pluginArtifactType = getCachedPluginMetadata(bundle);
+ if (pluginArtifactType == null) {
+ return;
+ }
+ List<DependencyType> dependencies = pluginArtifactType.getDependency();
+ Set<Long> dependentBundleIds = new HashSet<Long>();
+ Set<Long> fullDependentBundleIds = new HashSet<Long>();
+
+ 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();
+ installedBundles.add(installedDependentBundleId);
+ dependentBundleIds.add(installedDependentBundleId);
+
+ if (dependencyType.isEagerStart() && BundleUtils.canStart(installedDependentBundle)) {
+ eagerStartBundles.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);
+ }
+ }
+ installedBundles.add(bundle.getBundleId());
+ fullDependentBundleIdsMap.put(bundle.getBundleId(), fullDependentBundleIds);
+ dependentBundleIdsMap.put(bundle.getBundleId(), dependentBundleIds);
+ } catch (Exception e) {
+ log.error("Could not install bundle", e);
+ } finally {
+ if (created) {
+ threadInstalledBundles.remove();
+ threadEagerStartBundles.remove();
+ try {
+ for (Long bundleId : installedBundles) {
+ Bundle currentBundle = bundleContext.getBundle(bundleId);
+ if (!BundleUtils.isResolved(currentBundle)) {
+ BundleUtils.resolve(currentBundle);
+ if (!BundleUtils.isResolved(currentBundle)) {
+ log.error("Could not resolve the dependency bundle" + currentBundle.getLocation());
+ }
+ }
+ }
+ for (Long bundleId : eagerStartBundles) {
+ Bundle currentBundle = bundleContext.getBundle(bundleId);
+ if (BundleUtils.canStart(currentBundle)) {
+ currentBundle.start(Bundle.START_TRANSIENT);
+ }
+ }
+ } catch (Exception e) {
+ log.warn("Fail to resolve or start the bundle", e);
+ }
+
+ }
+ }
+ }
+
+ private void starting(Bundle bundle) {
+ PluginArtifactType pluginArtifactType = getCachedPluginMetadata(bundle);
+ if (pluginArtifactType != null) {
+ List<Bundle> bundles = new ArrayList<Bundle>();
+ List<DependencyType> dependencies = pluginArtifactType.getDependency();
+ try {
+ for (DependencyType dependencyType : dependencies) {
+ if (log.isDebugEnabled()) {
+ log.debug("Starting artifact: " + dependencyType);
+ }
+ Artifact artifact = dependencyType.toArtifact();
+ if (artifactResolver != null) {
+ artifact = artifactResolver.resolveInClassLoader(artifact);
+ }
+ String location = locateBundle(artifact);
+ Bundle b = bundleContext.installBundle(location);
+ //Do not start the bundle of ImportType.CLASSES, for class loading use, they are only required to be in the RESOLVED status
+ if (b.getState() != Bundle.ACTIVE && (!ImportType.CLASSES.equals(dependencyType.getImport()) || dependencyType.isEagerStart())) {
+ bundles.add(b);
+ }
+ }
+
+ for (Bundle b : bundles) {
+ if (BundleUtils.canStart(b)) {
+ try {
+ b.start(Bundle.START_TRANSIENT);
+ } catch (BundleException e) {
+ log.warn("Could not start bundle: " + b, e);
+ }
+ }
+ }
+ } catch (Exception e) {
+ log.error("Could not install bundle dependecy", e);
+ }
+ }
+ }
+
+ 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());
+ }
+ for (Repository repo : repositories) {
+ if (repo.contains(configurationId)) {
+ return "reference:file://" + repo.getLocation(configurationId).getAbsolutePath();
+ }
+ }
+ 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) {
+ }
+ }
+}
Modified: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java Tue Apr 26 23:15:32 2011
@@ -123,9 +123,9 @@ public class LocalAttributeManager imple
this.readOnly = readOnly;
this.serverInfo = serverInfo;
serverOverride = new ServerOverride();
-
+
log.debug("setting configSubstitutionsFile to: {}", configSubstitutionsFile);
-
+
infoFactory = newGBeanInfoFactory();
}
@@ -194,8 +194,8 @@ public class LocalAttributeManager imple
}
// set the attributes
- for (Iterator iterator = gbeanDatas.iterator(); iterator.hasNext();) {
- GBeanData data = (GBeanData) iterator.next();
+ for (Iterator<GBeanData> iterator = gbeanDatas.iterator(); iterator.hasNext();) {
+ GBeanData data = iterator.next();
boolean load = setAttributes(data, configuration, configName, bundle);
if (!load) {
iterator.remove();
@@ -243,9 +243,9 @@ public class LocalAttributeManager imple
}
configuration.setLoad(load);
configuration.setCondition(condition);
-
+
log.debug("Added gbeans for module: {} load: {}", moduleName, load);
-
+
attributeChanged();
}
@@ -674,7 +674,7 @@ public class LocalAttributeManager imple
try {
FileOutputStream out = new FileOutputStream(configSubstitutionsFile);
try {
- out.write(INSTRUCTION);
+ out.write(INSTRUCTION);
properties.store(out, null);
} finally {
out.close();
Modified: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/osgi/BootActivator.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/osgi/BootActivator.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/osgi/BootActivator.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/osgi/BootActivator.java Tue Apr 26 23:15:32 2011
@@ -66,7 +66,7 @@ public class BootActivator implements Bu
try {
//TODO there are additional consistency checks in RepositoryConfigurationStore that we should use.
ConfigurationData data = ConfigurationUtil.readConfigurationData(in);
- data.setBundleContext(bundleContext);
+ data.setBundle(bundleContext.getBundle());
AbstractName name = ConfigurationUtil.loadBootstrapConfiguration(kernel, data, bundleContext, false);
// Artifact id = data.getId();
// manager.startConfiguration(id);
@@ -75,7 +75,7 @@ public class BootActivator implements Bu
}
// register Main service if Main GBean present
- if (bundleContext.getServiceReference(org.apache.geronimo.main.Main.class.getName()) == null) {
+ if (bundleContext.getServiceReference(org.apache.geronimo.main.Main.class.getName()) == null) {
registerMainService(bundleContext, kernel);
}
@@ -96,17 +96,17 @@ public class BootActivator implements Bu
configurationActivator.stop(bundleContext);
}
}
-
+
private void registerMainService(BundleContext bundleContext, Kernel kernel) {
try {
final Main main = kernel.getGBean(Main.class);
bundleContext.registerService(
- org.apache.geronimo.main.Main.class.getName(),
+ org.apache.geronimo.main.Main.class.getName(),
new org.apache.geronimo.main.Main() {
public int execute(Object opaque) {
return main.execute(opaque);
- }
- },
+ }
+ },
null);
} catch (GBeanNotFoundException e) {
// ignore
Modified: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/plugin/model/DependencyType.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/plugin/model/DependencyType.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/plugin/model/DependencyType.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/plugin/model/DependencyType.java Tue Apr 26 23:15:32 2011
@@ -1,16 +1,14 @@
//
-// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.3-b01-fcs
-// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
-// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2009.04.08 at 04:38:27 PM PDT
+// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.3-b01-fcs
+// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
+// Any modifications to this file will be lost upon recompilation of the source schema.
+// Generated on: 2009.04.08 at 04:38:27 PM PDT
//
package org.apache.geronimo.system.plugin.model;
import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@@ -20,14 +18,14 @@ import org.apache.geronimo.kernel.reposi
/**
- *
+ *
* See pluginType/dependency above
- *
- *
+ *
+ *
* <p>Java class for dependencyType complex type.
- *
+ *
* <p>The following schema fragment specifies the expected content contained within this class.
- *
+ *
* <pre>
* <complexType name="dependencyType">
* <complexContent>
@@ -40,8 +38,8 @@ import org.apache.geronimo.kernel.reposi
* </complexContent>
* </complexType>
* </pre>
- *
- *
+ *
+ *
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "dependencyType", propOrder = {
@@ -56,14 +54,16 @@ public class DependencyType
protected ImportType _import;
@XmlAttribute
protected Boolean start;
+ @XmlAttribute
+ protected Boolean eagerStart;
/**
* Gets the value of the import property.
- *
+ *
* @return
* possible object is
* {@link ImportType }
- *
+ *
*/
public ImportType getImport() {
return _import;
@@ -71,11 +71,11 @@ public class DependencyType
/**
* Sets the value of the import property.
- *
+ *
* @param value
* allowed object is
* {@link ImportType }
- *
+ *
*/
public void setImport(ImportType value) {
this._import = value;
@@ -83,11 +83,11 @@ public class DependencyType
/**
* Gets the value of the start property.
- *
+ *
* @return
* possible object is
* {@link Boolean }
- *
+ *
*/
public boolean isStart() {
if (start == null) {
@@ -99,16 +99,28 @@ public class DependencyType
/**
* Sets the value of the start property.
- *
+ *
* @param value
* allowed object is
* {@link Boolean }
- *
+ *
*/
public void setStart(Boolean value) {
this.start = value;
}
+ public boolean isEagerStart() {
+ if (eagerStart == null) {
+ return false;
+ } else {
+ return eagerStart;
+ }
+ }
+
+ public void setEagerStart(Boolean value) {
+ this.eagerStart = value;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -119,6 +131,7 @@ public class DependencyType
if (_import != that._import) return false;
if (start != null ? !start.equals(that.start) : that.start != null) return false;
+ if (eagerStart != null ? !eagerStart.equals(that.eagerStart) : that.eagerStart != null) return false;
return true;
}
@@ -128,6 +141,7 @@ public class DependencyType
int result = super.hashCode();
result = 31 * result + (_import != null ? _import.hashCode() : 0);
result = 31 * result + (start != null ? start.hashCode() : 0);
+ result = 31 * result + (eagerStart != null ? eagerStart.hashCode() : 0);
return result;
}
Modified: geronimo/server/trunk/plugins/connector-1_6/geronimo-connector-builder-1_6/src/test/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilderTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/connector-1_6/geronimo-connector-builder-1_6/src/test/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilderTest.java?rev=1096945&r1=1096944&r2=1096945&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/connector-1_6/geronimo-connector-builder-1_6/src/test/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilderTest.java (original)
+++ geronimo/server/trunk/plugins/connector-1_6/geronimo-connector-builder-1_6/src/test/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilderTest.java Tue Apr 26 23:15:32 2011
@@ -471,7 +471,7 @@ public class ConnectorModuleBuilderTest
}
private void verifyDeployment(ConfigurationData configurationData, ClassLoader cl, AbstractName moduleAbstractName, String resourceAdapterName, boolean is15, String moduleName) throws Exception {
- configurationData.setBundleContext(bundleContext);
+ configurationData.setBundle(bundleContext.getBundle());
DataSource ds = null;
try {