Return-Path: X-Original-To: apmail-brooklyn-commits-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0B9331919B for ; Wed, 30 Mar 2016 13:59:41 +0000 (UTC) Received: (qmail 5862 invoked by uid 500); 30 Mar 2016 13:59:39 -0000 Delivered-To: apmail-brooklyn-commits-archive@brooklyn.apache.org Received: (qmail 5823 invoked by uid 500); 30 Mar 2016 13:59:39 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 5408 invoked by uid 99); 30 Mar 2016 13:59:39 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Mar 2016 13:59:39 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A6F3DE93DB; Wed, 30 Mar 2016 13:59:38 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: heneveld@apache.org To: commits@brooklyn.apache.org Date: Wed, 30 Mar 2016 13:59:44 -0000 Message-Id: <1127cecca70246499cdc5ced7ae4c4cc@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [07/50] brooklyn-server git commit: Make EmbeddedFelixFramework pluggable Make EmbeddedFelixFramework pluggable Needs different implementations depending on whether running in Karaf or classical. Can't have EmbeddedFelixFramework loaded in Karaf because felix bundle doesn't export org.apache.framework packages, just generic OSGi ones. Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/f8db60f2 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/f8db60f2 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/f8db60f2 Branch: refs/heads/master Commit: f8db60f26f5ffccecad44b4c31727fd7a1637240 Parents: f8476be Author: Svetoslav Neykov Authored: Wed Mar 16 18:41:52 2016 +0200 Committer: Svetoslav Neykov Committed: Thu Mar 17 15:18:38 2016 +0200 ---------------------------------------------------------------------- .../brooklyn/core/mgmt/ha/OsgiManager.java | 13 ++--- .../util/core/osgi/ContainerFramework.java | 47 +++++++++++++++++ .../apache/brooklyn/util/core/osgi/Osgis.java | 38 +++----------- .../util/core/osgi/SystemFrameworkLoader.java | 36 +++++++++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 4 ++ .../core/mgmt/osgi/OsgiStandaloneTest.java | 4 +- .../brooklyn/util/osgi/SystemFramework.java | 45 ++++++++++++++++ .../rt/felix/EmbeddedFelixFramework.java | 5 -- .../brooklyn/rt/felix/EmbeddedFramework.java | 54 ++++++++++++++++++++ ...rg.apache.brooklyn.util.osgi.SystemFramework | 19 +++++++ 10 files changed, 216 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java index ba56b98..28b20b3 100644 --- a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java @@ -36,15 +36,16 @@ import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.BrooklynVersion; import org.apache.brooklyn.core.server.BrooklynServerConfig; import org.apache.brooklyn.core.server.BrooklynServerPaths; -import org.apache.brooklyn.rt.felix.EmbeddedFelixFramework; import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.collections.MutableSet; import org.apache.brooklyn.util.core.osgi.Osgis; import org.apache.brooklyn.util.core.osgi.Osgis.BundleFinder; +import org.apache.brooklyn.util.core.osgi.SystemFrameworkLoader; import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.guava.Maybe; import org.apache.brooklyn.util.os.Os; import org.apache.brooklyn.util.os.Os.DeletionResult; +import org.apache.brooklyn.util.osgi.SystemFramework; import org.apache.brooklyn.util.repeat.Repeater; import org.apache.brooklyn.util.text.Strings; import org.apache.brooklyn.util.time.Duration; @@ -192,15 +193,7 @@ public class OsgiManager { Class clazz; //Extension bundles don't support loadClass. //Instead load from the app classpath. - if (EmbeddedFelixFramework.isExtensionBundle(b)) { - @SuppressWarnings("unchecked") - Class c = (Class)Class.forName(type); - clazz = c; - } else { - @SuppressWarnings("unchecked") - Class c = (Class)b.loadClass(type); - clazz = c; - } + clazz = SystemFrameworkLoader.get().loadClassFromBundle(type, b); return Maybe.of(clazz); } else { bundleProblems.put(osgiBundle, ((Maybe.Absent)bundle).getException()); http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/core/src/main/java/org/apache/brooklyn/util/core/osgi/ContainerFramework.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/util/core/osgi/ContainerFramework.java b/core/src/main/java/org/apache/brooklyn/util/core/osgi/ContainerFramework.java new file mode 100644 index 0000000..874f369 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/util/core/osgi/ContainerFramework.java @@ -0,0 +1,47 @@ +/* + * Copyright 2015 The Apache Software Foundation. + * + * Licensed 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.brooklyn.util.core.osgi; + +import org.apache.brooklyn.util.osgi.SystemFramework; +import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.launch.Framework; + +public class ContainerFramework implements SystemFramework { + + @Override + public Framework getFramework(String felixCacheDir, boolean clean) { + final Bundle bundle = FrameworkUtil.getBundle(Osgis.class); + return (Framework) bundle.getBundleContext().getBundle(0); + } + + @Override + public void ungetFramework(Framework framework) { + } + + @Override + public boolean isSystemBundle(Bundle bundle) { + return false; + } + + @Override + public Class loadClassFromBundle(String type, Bundle b) throws ClassNotFoundException { + @SuppressWarnings("unchecked") + Class c = (Class)b.loadClass(type); + return c; + } + +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java b/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java index c2598ad..0a03366 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java @@ -31,7 +31,6 @@ import java.util.jar.JarInputStream; import java.util.jar.Manifest; import org.apache.brooklyn.api.catalog.CatalogItem.CatalogBundle; -import org.apache.brooklyn.rt.felix.EmbeddedFelixFramework; import org.apache.brooklyn.util.collections.MutableList; import org.apache.brooklyn.util.core.ResourceUtils; import org.apache.brooklyn.util.exceptions.Exceptions; @@ -42,13 +41,9 @@ import org.apache.brooklyn.util.osgi.OsgiUtils; import org.apache.brooklyn.util.stream.Streams; import org.apache.brooklyn.util.text.Strings; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; import org.osgi.framework.Version; import org.osgi.framework.launch.Framework; -import org.osgi.framework.launch.FrameworkFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -291,12 +286,6 @@ public class Osgis { return bundleFinder(framework).symbolicName(symbolicName).version(Predicates.equalTo(version)).findUnique(); } - /** @deprecated since 0.9.0, replaced by {@link EmbeddedFelixFramework#newFrameworkFactory() */ - @Deprecated - public static FrameworkFactory newFrameworkFactory() { - return EmbeddedFelixFramework.newFrameworkFactory(); - } - /** @deprecated since 0.9.0, replaced by {@link #getFramework(java.lang.String, boolean) } */ @Deprecated public static Framework newFrameworkStarted(String felixCacheDir, boolean clean, Map extraStartupConfig) { @@ -315,14 +304,7 @@ public class Osgis { * @todo Use felixCacheDir ? */ public static Framework getFramework(String felixCacheDir, boolean clean) { - final Bundle bundle = FrameworkUtil.getBundle(Osgis.class); - if (bundle != null) { - // already running inside an OSGi container - return (Framework) bundle.getBundleContext().getBundle(0); - } else { - // not running inside OSGi container - return EmbeddedFelixFramework.newFrameworkStarted(felixCacheDir, clean, null); - } + return SystemFrameworkLoader.get().getFramework(felixCacheDir, clean); } /** @@ -333,10 +315,7 @@ public class Osgis { * @param framework */ public static void ungetFramework(Framework framework) { - final Bundle bundle = FrameworkUtil.getBundle(Osgis.class); - if (bundle == null) { - EmbeddedFelixFramework.stopFramework(framework); - } + SystemFrameworkLoader.get().ungetFramework(framework); } @@ -425,7 +404,7 @@ public class Osgis { String versionedId = OsgiUtils.getVersionedId(manifest); for (Bundle installedBundle : framework.getBundleContext().getBundles()) { if (versionedId.equals(OsgiUtils.getVersionedId(installedBundle))) { - if (EmbeddedFelixFramework.isSystemBundle(installedBundle)) { + if (SystemFrameworkLoader.get().isSystemBundle(installedBundle)) { LOG.debug("Already have system bundle "+versionedId+" from "+installedBundle+"/"+installedBundle.getLocation()+" when requested "+url+"; not installing"); // "System bundles" (ie things on the classpath) cannot be overridden return installedBundle; @@ -442,11 +421,11 @@ public class Osgis { private static InputStream getUrlStream(String url) { return ResourceUtils.create(Osgis.class).getResourceFromUrl(url); } - - /** @deprecated since 0.9.0, replaced with {@link EmbeddedFelixFramework#isExtensionBundle(Bundle)} */ + + /** @deprecated since 0.9.0, replaced with {@code SystemFrameworkLoader.get().isSystemBundle(bundle)} */ @Deprecated public static boolean isExtensionBundle(Bundle bundle) { - return EmbeddedFelixFramework.isExtensionBundle(bundle); + return SystemFrameworkLoader.get().isSystemBundle(bundle); } /** @deprecated since 0.9.0, replaced with {@link OsgiUtils#parseOsgiIdentifier(java.lang.String) } */ @@ -461,9 +440,4 @@ public class Osgis { }); } - /** @deprecated since 0.9.0, replaced with {@link org.apache.brooklyn.rt.felix.ManifestHelper} */ - @Deprecated - public static class ManifestHelper extends org.apache.brooklyn.rt.felix.ManifestHelper { - - } } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/core/src/main/java/org/apache/brooklyn/util/core/osgi/SystemFrameworkLoader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/util/core/osgi/SystemFrameworkLoader.java b/core/src/main/java/org/apache/brooklyn/util/core/osgi/SystemFrameworkLoader.java new file mode 100644 index 0000000..e5a5f89 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/util/core/osgi/SystemFrameworkLoader.java @@ -0,0 +1,36 @@ +/* + * Copyright 2015 The Apache Software Foundation. + * + * Licensed 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.brooklyn.util.core.osgi; + +import org.apache.brooklyn.api.framework.FrameworkLookup; +import org.apache.brooklyn.util.osgi.SystemFramework; + +public class SystemFrameworkLoader { + private static SystemFramework framework; + public static SystemFramework get() { + if (framework == null) { + return createFramework(); + } else { + return framework; + } + } + private synchronized static SystemFramework createFramework() { + if (framework == null) { + framework = FrameworkLookup.lookup(SystemFramework.class).get(); + } + return framework; + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index bd0c7eb..26adeb9 100644 --- a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -61,4 +61,8 @@ limitations under the License. + + http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java index 524e248..9267967 100644 --- a/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java @@ -20,11 +20,11 @@ package org.apache.brooklyn.core.mgmt.osgi; import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.rt.felix.EmbeddedFelixFramework; import org.apache.brooklyn.test.support.TestResourceUnavailableException; import org.apache.brooklyn.util.core.ResourceUtils; import org.apache.brooklyn.util.core.osgi.OsgiTestBase; import org.apache.brooklyn.util.core.osgi.Osgis; +import org.apache.brooklyn.util.core.osgi.SystemFrameworkLoader; import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.maven.MavenArtifact; import org.apache.brooklyn.util.maven.MavenRetriever; @@ -106,7 +106,7 @@ public class OsgiStandaloneTest extends OsgiTestBase { //Make sure that we still get the initially loaded //bundle after trying to install the same version. Bundle bundle = install(url); - Assert.assertTrue(EmbeddedFelixFramework.isExtensionBundle(bundle)); + Assert.assertTrue(SystemFrameworkLoader.get().isSystemBundle(bundle)); } @Test http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/utils/common/src/main/java/org/apache/brooklyn/util/osgi/SystemFramework.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/osgi/SystemFramework.java b/utils/common/src/main/java/org/apache/brooklyn/util/osgi/SystemFramework.java new file mode 100644 index 0000000..7b57a02 --- /dev/null +++ b/utils/common/src/main/java/org/apache/brooklyn/util/osgi/SystemFramework.java @@ -0,0 +1,45 @@ +/* + * Copyright 2015 The Apache Software Foundation. + * + * Licensed 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.brooklyn.util.osgi; + +import org.osgi.framework.Bundle; +import org.osgi.framework.launch.Framework; + +public interface SystemFramework { + /** + * Provides an OSGI framework. + * + * When running inside an OSGi container, the container framework is returned. + * When running standalone a new Apache Felix container is created. + */ + public Framework getFramework(String felixCacheDir, boolean clean); + + /** + * Stops/ungets the OSGi framework, depending on the environment. + */ + public void ungetFramework(Framework framework); + + /** + * Whether the bundle is exporting classes from the application class path. + */ + public boolean isSystemBundle(Bundle bundle); + + /** + * Loads a class from the passed bundle. The embedded environment has some special + * requirements for loading classes so the implementation will abstract those. + */ + public Class loadClassFromBundle(String type, Bundle b) throws ClassNotFoundException; +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFramework.java ---------------------------------------------------------------------- diff --git a/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFramework.java b/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFramework.java index 2bc467a..ed6b0a5 100644 --- a/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFramework.java +++ b/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFramework.java @@ -256,11 +256,6 @@ public class EmbeddedFelixFramework { return atts.containsKey(new Attributes.Name(Constants.BUNDLE_MANIFESTVERSION)); } - public static boolean isSystemBundle(Bundle bundle) { - String versionedId = OsgiUtils.getVersionedId( bundle ); - return SYSTEM_BUNDLES.contains(versionedId); - } - public static boolean isExtensionBundle(Bundle bundle) { String location = bundle.getLocation(); return location != null && http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFramework.java ---------------------------------------------------------------------- diff --git a/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFramework.java b/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFramework.java new file mode 100644 index 0000000..d12cb80 --- /dev/null +++ b/utils/rt-felix/src/main/java/org/apache/brooklyn/rt/felix/EmbeddedFramework.java @@ -0,0 +1,54 @@ +/* + * Copyright 2015 The Apache Software Foundation. + * + * Licensed 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.brooklyn.rt.felix; + +import org.apache.brooklyn.util.osgi.SystemFramework; +import org.osgi.framework.Bundle; +import org.osgi.framework.launch.Framework; + +public class EmbeddedFramework implements SystemFramework { + + @Override + public Framework getFramework(String felixCacheDir, boolean clean) { + return EmbeddedFelixFramework.newFrameworkStarted(felixCacheDir, clean, null); + } + + @Override + public void ungetFramework(Framework framework) { + EmbeddedFelixFramework.stopFramework(framework); + } + + @Override + public boolean isSystemBundle(Bundle bundle) { + return EmbeddedFelixFramework.isExtensionBundle(bundle); + } + + @Override + public Class loadClassFromBundle(String type, Bundle b) throws ClassNotFoundException { + Class clazz; + if (EmbeddedFelixFramework.isExtensionBundle(b)) { + @SuppressWarnings("unchecked") + Class c = (Class)Class.forName(type); + clazz = c; + } else { + @SuppressWarnings("unchecked") + Class c = (Class)b.loadClass(type); + clazz = c; + } + return clazz; + } + +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8db60f2/utils/rt-felix/src/main/resources/META-INF/services/org.apache.brooklyn.util.osgi.SystemFramework ---------------------------------------------------------------------- diff --git a/utils/rt-felix/src/main/resources/META-INF/services/org.apache.brooklyn.util.osgi.SystemFramework b/utils/rt-felix/src/main/resources/META-INF/services/org.apache.brooklyn.util.osgi.SystemFramework new file mode 100644 index 0000000..8e0a94b --- /dev/null +++ b/utils/rt-felix/src/main/resources/META-INF/services/org.apache.brooklyn.util.osgi.SystemFramework @@ -0,0 +1,19 @@ +# +# 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. +# +org.apache.brooklyn.rt.felix.EmbeddedFramework