You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ra...@apache.org on 2015/11/25 17:32:58 UTC

ignite git commit: IGNITE-1270 Review comments. Changes below:

Repository: ignite
Updated Branches:
  refs/heads/ignite-1270 4e4c01ab0 -> b8597f7ba


IGNITE-1270 Review comments. Changes below:

* IgniteOsgiContextActivator => IgniteAbstractOsgiContextActivator.
* IgniteOsgiUtils#classloaders() => make protected.
* ContainerSweepClassLoader => skip unresolved bundles and fragments.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/b8597f7b
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/b8597f7b
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/b8597f7b

Branch: refs/heads/ignite-1270
Commit: b8597f7bad23d5d00b67ce33e3f18ed394a97d4e
Parents: 4e4c01a
Author: Raul Kripalani <ra...@apache.org>
Authored: Wed Nov 25 16:32:49 2015 +0000
Committer: Raul Kripalani <ra...@apache.org>
Committed: Wed Nov 25 16:32:49 2015 +0000

----------------------------------------------------------------------
 .../IgniteAbstractOsgiContextActivator.java     | 218 +++++++++++++++++++
 .../ignite/osgi/IgniteOsgiContextActivator.java | 173 ---------------
 .../org/apache/ignite/osgi/IgniteOsgiUtils.java |   2 +-
 .../classloaders/ContainerSweepClassLoader.java |   9 +-
 .../activators/BasicIgniteTestActivator.java    |   4 +-
 5 files changed, 229 insertions(+), 177 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/b8597f7b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java
----------------------------------------------------------------------
diff --git a/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java
new file mode 100644
index 0000000..8fea896
--- /dev/null
+++ b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java
@@ -0,0 +1,218 @@
+/*
+ * 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.ignite.osgi;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.util.typedef.internal.A;
+import org.apache.ignite.osgi.classloaders.BundleDelegatingClassLoader;
+import org.apache.ignite.osgi.classloaders.ContainerSweepClassLoader;
+import org.apache.ignite.osgi.classloaders.OsgiClassLoadingStrategyType;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+/**
+ * This {@link BundleActivator} starts Apache Ignite inside the OSGi container when the bundle is started.
+ * <p>
+ * Create an implementation of this class and set the {@code Bundle-Activator} OSGi Manifest header to the FQN of
+ * your class.
+ * <p>
+ * You must provide the {@link IgniteConfiguration} to start by implementing the {@link #igniteConfiguration()}
+ * abstract method. The return value of this method cannot be {@code null}. For example, if your implementation is
+ * called {@code org.myorg.osgi.IgniteActivator}, your bundle must provide the following header:
+ * <pre>
+ * Bundle-Activator: org.myorg.osgi.IgniteActivator
+ * </pre>
+ * You may use the
+ * <a href="https://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html">Maven
+ * Bundle Plugin</a> to generate your bundle (or bundle manifest), including the required header.
+ * <p>
+ * This activator also exports the Ignite instance as an OSGi service, with the property {@code ignite.name} set
+ * to the value of {@link Ignite#name()}, if and only if the name is not null.
+ * <p>
+ * Currently, Ignite only allows a single instance per container. We may remove this limitation if enough demand
+ * builds up in the community.
+ *
+ * @see <a href="http://wiki.osgi.org/wiki/Bundle-Activator">Bundle-Activator OSGi Manifest header</a>
+ *
+ */
+public abstract class IgniteAbstractOsgiContextActivator implements BundleActivator {
+
+    /** OSGI service property name. */
+    public static final String OSGI_SERVICE_PROP_IGNITE_NAME = "ignite.name";
+
+    /** The instance of Ignite started by this Activator. */
+    protected Ignite ignite;
+
+    /** Our bundle context. */
+    private BundleContext bundleContext;
+
+    /** Ignite logger. */
+    private IgniteLogger logger;
+
+    /**
+     * Method invoked by OSGi to start the bundle. It starts the specified Ignite configuration.
+     *
+     * @param context Bundle context.
+     * @throws Exception
+     */
+    @Override public final void start(BundleContext context) throws Exception {
+        // Ensure that no other instances are running.
+        if (IgniteOsgiUtils.gridCount() > 0) {
+            throw new IgniteException("Failed to start Ignite instance (another instance is already running " +
+                "and ignite-osgi is currently limited to a single instance per container).");
+        }
+
+        bundleContext = context;
+
+        // Start the Ignite configuration specified by the user.
+        IgniteConfiguration config = igniteConfiguration();
+
+        A.notNull(config, "Ignite configuration cannot be null");
+
+        // Override the classloader with the classloading strategy chosen by the user.
+        ClassLoader clsLdr;
+
+        if (classLoadingStrategy() == OsgiClassLoadingStrategyType.BUNDLE_DELEGATING)
+            clsLdr = new BundleDelegatingClassLoader(bundleContext.getBundle(), Ignite.class.getClassLoader());
+        else
+            clsLdr = new ContainerSweepClassLoader(bundleContext.getBundle(), Ignite.class.getClassLoader());
+
+        config.setClassLoader(clsLdr);
+
+        preStart(context);
+
+        // Start Ignite.
+        ignite = Ignition.start(config);
+
+        logger = ignite.log();
+
+        logger.info("Started Ignite from OSGi Activator [name=" + ignite.name() + ']');
+
+        // Add into Ignite's OSGi registry.
+        IgniteOsgiUtils.classloaders().put(ignite, clsLdr);
+
+        // Export Ignite as a service.
+        exportOsgiService(ignite);
+
+        postStart(context);
+
+    }
+
+    /**
+     * Stops Ignite when the bundle is stopping.
+     *
+     * @param context Bundle context.
+     * @throws Exception
+     */
+    @Override public final void stop(BundleContext context) throws Exception {
+        preStop(context);
+
+        try {
+            ignite.close();
+        }
+        catch (Exception e) {
+            logger.error("Failed to stop Ignite via OSGi Activator [errMsg=" + e.getMessage() + ']');
+            return;
+        }
+
+        logger.info("Stopped Ignite from OSGi Activator [name=" + ignite.name() + ']');
+
+        IgniteOsgiUtils.classloaders().remove(ignite);
+
+        postStop(context);
+    }
+
+    /**
+     * This method is called before Ignite initialises.
+     * <p>
+     * The default implementation is empty. Override it to introduce custom logic.
+     *
+     * @param context The {@link BundleContext}.
+     */
+    private void preStart(BundleContext context) { }
+
+    /**
+     * This method is called after Ignite initialises, only if initialization succeeded.
+     * <p>
+     * The default implementation is empty. Override it to introduce custom logic.
+     *
+     * @param context The {@link BundleContext}.
+     */
+    private void postStart(BundleContext context) { }
+
+    /**
+     * This method is called before Ignite stops.
+     * <p>
+     * The default implementation is empty. Override it to introduce custom logic.
+     *
+     * @param context The {@link BundleContext}.
+     */
+    private void preStop(BundleContext context) { }
+
+    /**
+     * This method is called after Ignite stops, only if the operation succeeded.
+     * <p>
+     * The default implementation is empty. Override it to introduce custom logic.
+     *
+     * @param context The {@link BundleContext}.
+     */
+    private void postStop(BundleContext context) {
+    }
+
+    /**
+     * Override this method to provide the Ignite configuration this bundle will start.
+     *
+     * @return The Ignite configuration.
+     */
+    public abstract IgniteConfiguration igniteConfiguration();
+
+    /**
+     * Override this method to indicate which classloading strategy to use.
+     *
+     * @return The strategy.
+     */
+    public OsgiClassLoadingStrategyType classLoadingStrategy() {
+        return OsgiClassLoadingStrategyType.BUNDLE_DELEGATING;
+    }
+
+    /**
+     * Exports the Ignite instance onto the OSGi Service Registry.
+     * @param ignite
+     */
+    private void exportOsgiService(Ignite ignite) {
+        Dictionary<String, String> dict = new Hashtable<>();
+
+        // Only add the service property if the grid name != null.
+        if (ignite.name() != null) {
+            dict.put(OSGI_SERVICE_PROP_IGNITE_NAME, ignite.name());
+        }
+
+        bundleContext.registerService(Ignite.class, ignite, dict);
+
+        logger.info("Exported OSGi service for Ignite with properties " + dict);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/b8597f7b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiContextActivator.java
----------------------------------------------------------------------
diff --git a/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiContextActivator.java b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiContextActivator.java
deleted file mode 100644
index 8eac0d8..0000000
--- a/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiContextActivator.java
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * 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.ignite.osgi;
-
-import java.util.Dictionary;
-import java.util.Hashtable;
-
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteException;
-import org.apache.ignite.IgniteLogger;
-import org.apache.ignite.Ignition;
-import org.apache.ignite.configuration.IgniteConfiguration;
-import org.apache.ignite.internal.util.typedef.internal.A;
-import org.apache.ignite.osgi.classloaders.BundleDelegatingClassLoader;
-import org.apache.ignite.osgi.classloaders.ContainerSweepClassLoader;
-import org.apache.ignite.osgi.classloaders.OsgiClassLoadingStrategyType;
-
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-
-/**
- * This {@link BundleActivator} starts Apache Ignite inside the OSGi container when the bundle is started.
- * <p>
- * Create an implementation of this class and set the {@code Bundle-Activator} OSGi Manifest header to the FQN of
- * your class.
- * <p>
- * You must provide the {@link IgniteConfiguration} to start by implementing the {@link #igniteConfiguration()}
- * abstract method. The return value of this method cannot be {@code null}. For example, if your implementation is
- * called {@code org.myorg.osgi.IgniteActivator}, your bundle must provide the following header:
- * <pre>
- * Bundle-Activator: org.myorg.osgi.IgniteActivator
- * </pre>
- * You may use the
- * <a href="https://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html">Maven
- * Bundle Plugin</a> to generate your bundle (or bundle manifest), including the required header.
- * <p>
- * This activator also exports the Ignite instance as an OSGi service, with the property {@code ignite.name} set
- * to the value of {@link Ignite#name()}, if and only if the name is not null.
- * <p>
- * Currently, Ignite only allows a single instance per container. We may remove this limitation if enough demand
- * builds up in the community.
- *
- * @see <a href="http://wiki.osgi.org/wiki/Bundle-Activator">Bundle-Activator OSGi Manifest header</a>
- *
- */
-public abstract class IgniteOsgiContextActivator implements BundleActivator {
-
-    /** OSGI service property name. */
-    public static final String OSGI_SERVICE_PROP_IGNITE_NAME = "ignite.name";
-
-    /** The instance of Ignite started by this Activator. */
-    protected Ignite ignite;
-
-    /** Our bundle context. */
-    private BundleContext bundleContext;
-
-    /** Ignite logger. */
-    private IgniteLogger logger;
-
-    /**
-     * Method invoked by OSGi to start the bundle. It starts the specified Ignite configuration.
-     *
-     * @param context Bundle context.
-     * @throws Exception
-     */
-    @Override public void start(BundleContext context) throws Exception {
-        // Ensure that no other instances are running.
-        if (IgniteOsgiUtils.gridCount() > 0) {
-            throw new IgniteException("Failed to start Ignite instance (another instance is already running " +
-                "and ignite-osgi is currently limited to a single instance per container).");
-        }
-
-        bundleContext = context;
-
-        // Start the Ignite configuration specified by the user.
-        IgniteConfiguration config = igniteConfiguration();
-
-        A.notNull(config, "Ignite configuration cannot be null");
-
-        // Override the classloader with the classloading strategy chosen by the user.
-        ClassLoader clsLdr;
-
-        if (classLoadingStrategy() == OsgiClassLoadingStrategyType.BUNDLE_DELEGATING)
-            clsLdr = new BundleDelegatingClassLoader(bundleContext.getBundle(), Ignite.class.getClassLoader());
-        else
-            clsLdr = new ContainerSweepClassLoader(bundleContext.getBundle(), Ignite.class.getClassLoader());
-
-        config.setClassLoader(clsLdr);
-
-        // Start Ignite.
-        ignite = Ignition.start(config);
-
-        logger = ignite.log();
-
-        logger.info("Started Ignite from OSGi Activator [name=" + ignite.name() + ']');
-
-        // Add into Ignite's OSGi registry.
-        IgniteOsgiUtils.classloaders().put(ignite, clsLdr);
-
-        // Export Ignite as a service.
-        exportOsgiService(ignite);
-
-    }
-
-    /**
-     * Stops Ignite when the bundle is stopping.
-     *
-     * @param context Bundle context.
-     * @throws Exception
-     */
-    @Override public void stop(BundleContext context) throws Exception {
-        try {
-            ignite.close();
-        }
-        catch (Exception e) {
-            logger.error("Failed to stop Ignite via OSGi Activator [errMsg=" + e.getMessage() + ']');
-            return;
-        }
-
-        logger.info("Stopped Ignite from OSGi Activator [name=" + ignite.name() + ']');
-
-        IgniteOsgiUtils.classloaders().remove(ignite);
-    }
-
-    /**
-     * Override this method to provide the Ignite configuration this bundle will start.
-     *
-     * @return The Ignite configuration.
-     */
-    public abstract IgniteConfiguration igniteConfiguration();
-
-    /**
-     * Override this method to indicate which classloading strategy to use.
-     *
-     * @return The strategy.
-     */
-    public OsgiClassLoadingStrategyType classLoadingStrategy() {
-        return OsgiClassLoadingStrategyType.BUNDLE_DELEGATING;
-    }
-
-    /**
-     * Exports the Ignite instance onto the OSGi Service Registry.
-     * @param ignite
-     */
-    private void exportOsgiService(Ignite ignite) {
-        Dictionary<String, String> dict = new Hashtable<>();
-
-        // Only add the service property if the grid name != null.
-        if (ignite.name() != null) {
-            dict.put(OSGI_SERVICE_PROP_IGNITE_NAME, ignite.name());
-        }
-
-        bundleContext.registerService(Ignite.class, ignite, dict);
-
-        logger.info("Exported OSGi service for Ignite with properties " + dict);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/ignite/blob/b8597f7b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiUtils.java
----------------------------------------------------------------------
diff --git a/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiUtils.java b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiUtils.java
index 54f29aa..6199285 100644
--- a/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiUtils.java
+++ b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteOsgiUtils.java
@@ -57,7 +57,7 @@ public class IgniteOsgiUtils {
      *
      * @return The {@link Map}.
      */
-    public static Map<Ignite, ClassLoader> classloaders() {
+    protected static Map<Ignite, ClassLoader> classloaders() {
         return CLASSLOADERS;
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b8597f7b/modules/osgi/src/main/java/org/apache/ignite/osgi/classloaders/ContainerSweepClassLoader.java
----------------------------------------------------------------------
diff --git a/modules/osgi/src/main/java/org/apache/ignite/osgi/classloaders/ContainerSweepClassLoader.java b/modules/osgi/src/main/java/org/apache/ignite/osgi/classloaders/ContainerSweepClassLoader.java
index 7c11dcf..ed3c200 100644
--- a/modules/osgi/src/main/java/org/apache/ignite/osgi/classloaders/ContainerSweepClassLoader.java
+++ b/modules/osgi/src/main/java/org/apache/ignite/osgi/classloaders/ContainerSweepClassLoader.java
@@ -24,6 +24,7 @@ import org.apache.ignite.internal.util.GridConcurrentHashSet;
 
 import org.jsr166.ConcurrentHashMap8;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
 
 /**
  * A {@link ClassLoader} implementation that first attempts to load the class from the associated {@link Bundle}. As
@@ -112,8 +113,14 @@ public class ContainerSweepClassLoader extends BundleDelegatingClassLoader {
         int bundleIdx = 0;
 
         for (; bundleIdx < bundles.length; bundleIdx++) {
+            Bundle b = bundles[bundleIdx];
+
+            // Skip bundles that haven't reached RESOLVED state; skip fragments.
+            if (b.getState() <= Bundle.RESOLVED || b.getHeaders().get(Constants.FRAGMENT_HOST) != null)
+                continue;
+
             try {
-                cls = bundles[bundleIdx].loadClass(name);
+                cls = b.loadClass(name);
                 break;
             }
             catch (ClassNotFoundException e) {

http://git-wip-us.apache.org/repos/asf/ignite/blob/b8597f7b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java
----------------------------------------------------------------------
diff --git a/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java
index 328f875..13623bc 100644
--- a/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java
+++ b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java
@@ -18,13 +18,13 @@
 package org.apache.ignite.osgi.activators;
 
 import org.apache.ignite.configuration.IgniteConfiguration;
-import org.apache.ignite.osgi.IgniteOsgiContextActivator;
+import org.apache.ignite.osgi.IgniteAbstractOsgiContextActivator;
 import org.apache.ignite.osgi.classloaders.OsgiClassLoadingStrategyType;
 
 /**
  * Basic Ignite Activator for testing.
  */
-public class BasicIgniteTestActivator extends IgniteOsgiContextActivator {
+public class BasicIgniteTestActivator extends IgniteAbstractOsgiContextActivator {
 
     /**
      * @return