From a51fdc71c9a1620e35cd88b1b6643ca4439b719d Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Fri, 4 Mar 2016 10:52:46 +0000 Subject: [PATCH 01/12] Use OSGI lookup for ApiObjectsFactory and fall back to ServiceLoader. Abstract out the lookup into a utility. --- .../api/framework/FrameworkLookup.java | 85 +++++++++++++++++++ .../api/internal/ApiObjectsFactory.java | 21 +++-- .../CampToSpecTransformerFactory.java | 22 +++++ .../OSGI-INF/blueprint/blueprint.xml | 49 +++++++++++ 4 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java create mode 100644 camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java create mode 100644 camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml diff --git a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java new file mode 100644 index 0000000000..978a716f00 --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java @@ -0,0 +1,85 @@ +/* + * 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.brooklyn.api.framework; + +import org.apache.brooklyn.util.guava.Maybe; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ServiceLoader; + +/** + * A utility to fetch an instance of a class from either OSGI if available, or by a Service Loader otherwise. + * + * The intention of this class is to provide a means to lookup beans in the OSGI service registry that were + * previously looked up via a ServiceLoader, but still maintain the backward compatible behaviour when not running + * in an OSGI container. + */ +public class FrameworkLookup { + + private static final Logger LOG = LoggerFactory.getLogger(FrameworkLookup.class); + + /** + * Find an instance of the given class in the framework. + * This first performs an OSGI lookup if the OSGI framework is available. If it is not, or no implementation is + * found in OSGI, then it falls back to attempting a lookup from the current context classpath via a ServiceLoader. + * + * @param clazz The class (typically the class of an interface) to search in the framework. + * @param The type for the class. + * @return A maybe of the instance found in the framework. + */ + public static Maybe lookup (Class clazz) { + Maybe result = lookupInOsgi(clazz); + + if (result.isAbsent()) { + result = lookupViaServiceLoader(clazz); + } + + return result; + } + + private static Maybe lookupViaServiceLoader(Class clazz) { + LOG.debug("Looking up via ServiceLoader"); + + Maybe result = Maybe.absent("No result found with ServiceLoader"); + ServiceLoader LOADER = ServiceLoader.load(clazz); + for (T item : LOADER) { + return Maybe.of(item); + } + return result; + } + + private static Maybe lookupInOsgi(Class clazz) { + Maybe result = Maybe.absent("No result found with OSGI"); + + final Bundle bundle = FrameworkUtil.getBundle(clazz); + if (bundle != null) { + LOG.debug("Looking up T in OSGI"); + BundleContext ctx = bundle.getBundleContext(); + final ServiceReference reference = ctx.getServiceReference(clazz); + final T service = ctx.getService(reference); + result = Maybe.of(service); + } + return result; + } +} diff --git a/api/src/main/java/org/apache/brooklyn/api/internal/ApiObjectsFactory.java b/api/src/main/java/org/apache/brooklyn/api/internal/ApiObjectsFactory.java index 51c0185d49..55b4a569cc 100644 --- a/api/src/main/java/org/apache/brooklyn/api/internal/ApiObjectsFactory.java +++ b/api/src/main/java/org/apache/brooklyn/api/internal/ApiObjectsFactory.java @@ -18,11 +18,12 @@ */ package org.apache.brooklyn.api.internal; -import java.util.ServiceLoader; - +import org.apache.brooklyn.api.framework.FrameworkLookup; import org.apache.brooklyn.util.guava.Maybe; import com.google.common.annotations.Beta; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class grants access to implementations in core for operations needed in API classes. @@ -34,6 +35,8 @@ */ @Beta public class ApiObjectsFactory { + + private static final Logger LOG = LoggerFactory.getLogger(ApiObjectsFactory.class); private static Maybe INSTANCE; @@ -41,14 +44,14 @@ private static synchronized ApiObjectsFactoryInterface getFactoryInstance() { // defer initialization to allow any other static initialization to complete, // and use maybe so we (1) don't check multiple times, but (2) do throw error in the caller's stack if (INSTANCE!=null) return INSTANCE.get(); - - ServiceLoader LOADER = ServiceLoader.load(ApiObjectsFactoryInterface.class); - for (ApiObjectsFactoryInterface item : LOADER) { - INSTANCE = Maybe.of(item); - return INSTANCE.get(); + + INSTANCE = Maybe.absent("Implementation of " + ApiObjectsFactoryInterface.class + " not found."); + + final Maybe factory = FrameworkLookup.lookup(ApiObjectsFactoryInterface.class); + if (factory.isPresent()) { + INSTANCE = factory; } - INSTANCE = Maybe.absent("Implementation of " + ApiObjectsFactoryInterface.class + " not found on classpath; " - + "can be caused by IDE not copying resources, or by something else clobbering non-class resources needed for service loading"); + return INSTANCE.get(); } diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java new file mode 100644 index 0000000000..fdb8ede361 --- /dev/null +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java @@ -0,0 +1,22 @@ +/* + * 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.brooklyn.camp.brooklyn.spi.creation; + +public class CampToSpecTransformerFactory { +} diff --git a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 0000000000..f9b7607663 --- /dev/null +++ b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + org.apache.brooklyn.api.internal.ApiObjectsFactoryInterface + + + + + + + + From 00d1338b5c500a5585aaa91c38b3205668254821 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Mon, 7 Mar 2016 16:19:39 +0000 Subject: [PATCH 02/12] Add framework lookup for (deprecated) PlanToSpecFactory. --- .../api/framework/FrameworkLookup.java | 78 ++++++++++++++++--- .../CampToSpecTransformerFactory.java | 8 ++ .../OSGI-INF/blueprint/blueprint.xml | 25 ++---- .../brooklyn/core/plan/PlanToSpecFactory.java | 9 ++- .../resources/OSGI-INF/blueprint/service.xml | 2 +- 5 files changed, 85 insertions(+), 37 deletions(-) diff --git a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java index 978a716f00..6239dcb4d1 100644 --- a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java +++ b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java @@ -19,13 +19,14 @@ package org.apache.brooklyn.api.framework; import org.apache.brooklyn.util.guava.Maybe; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; +import org.apache.brooklyn.util.osgi.OsgiUtil; +import org.osgi.framework.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.ServiceLoader; /** @@ -41,27 +42,68 @@ public class FrameworkLookup { /** * Find an instance of the given class in the framework. - * This first performs an OSGI lookup if the OSGI framework is available. If it is not, or no implementation is - * found in OSGI, then it falls back to attempting a lookup from the current context classpath via a ServiceLoader. + * This first performs an OSGI lookup if the OSGI framework is available. If it is not then it falls back to + * attempting a lookup from the current context classpath via a ServiceLoader. * * @param clazz The class (typically the class of an interface) to search in the framework. * @param The type for the class. * @return A maybe of the instance found in the framework. */ public static Maybe lookup (Class clazz) { - Maybe result = lookupInOsgi(clazz); - if (result.isAbsent()) { + Maybe result; + if (OsgiUtil.isBrooklynInsideFramework()) { + result = lookupInOsgi(clazz); + } else { result = lookupViaServiceLoader(clazz); } return result; } + /** + * Find all instances of the given class in the framework. + * This first performs an OSGI lookup if the OSGI framework is available. If it is not then it falls back to + * attempting a lookup from the current context classpath via a ServiceLoader. + * + * @param clazz The class (typically the class of an interface) to search in the framework. + * @param The type for the class. + * @return An iterable over the instances found in the framework. + */ + public static Iterable lookupAll(Class clazz) { + + Iterable result; + if (OsgiUtil.isBrooklynInsideFramework()) { + result = lookupAllInOsgi(clazz); + } else { + result = lookupAllViaServiceLoader(clazz); + } + return result; + } + + private static Iterable lookupAllViaServiceLoader(Class clazz) { + LOG.debug("Looking up all " + clazz.getSimpleName() + " via ServiceLoader"); + + return ServiceLoader.load(clazz); + } + + private static Iterable lookupAllInOsgi(Class clazz) { + final List result = Collections.emptyList(); + final Bundle bundle = FrameworkUtil.getBundle(clazz); + if (bundle != null) { + LOG.debug("Looking up all " + clazz.getSimpleName() + " in OSGI"); + BundleContext ctx = bundle.getBundleContext(); + for (ServiceReference reference: getServiceReferences(clazz, ctx)) { + result.add(ctx.getService(reference)); + } + } + return result; + } + private static Maybe lookupViaServiceLoader(Class clazz) { - LOG.debug("Looking up via ServiceLoader"); + LOG.debug("Looking up " + clazz.getSimpleName() + " via ServiceLoader"); - Maybe result = Maybe.absent("No result found with ServiceLoader"); + Maybe result = Maybe.absent("No class " + clazz.getSimpleName() + " found with ServiceLoader"); ServiceLoader LOADER = ServiceLoader.load(clazz); for (T item : LOADER) { return Maybe.of(item); @@ -70,11 +112,11 @@ private static Maybe lookupViaServiceLoader(Class clazz) { } private static Maybe lookupInOsgi(Class clazz) { - Maybe result = Maybe.absent("No result found with OSGI"); + Maybe result = Maybe.absent("No class " + clazz.getSimpleName() + " found with OSGI"); final Bundle bundle = FrameworkUtil.getBundle(clazz); if (bundle != null) { - LOG.debug("Looking up T in OSGI"); + LOG.debug("Looking up " + clazz.getSimpleName() + " in OSGI"); BundleContext ctx = bundle.getBundleContext(); final ServiceReference reference = ctx.getServiceReference(clazz); final T service = ctx.getService(reference); @@ -82,4 +124,16 @@ private static Maybe lookupInOsgi(Class clazz) { } return result; } + + private static Collection> getServiceReferences(Class clazz, BundleContext ctx) { + try { + return ctx.getServiceReferences(clazz, null); + } catch (InvalidSyntaxException e) { + // handle the checked(!) exception declared on the method, we pass null as filter above + throw new RuntimeException(e); + } + } + + + } diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java index fdb8ede361..b0802a38a1 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java @@ -18,5 +18,13 @@ */ package org.apache.brooklyn.camp.brooklyn.spi.creation; +/** + * Deprecated class. Only required for OSGI integration. + */ +@Deprecated /** @deprecated since 0.9.0 use CampTypePlanTransformer */ public class CampToSpecTransformerFactory { + + public static CampToSpecTransformer newInstance() { + return new CampToSpecTransformer(); + } } diff --git a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml index f9b7607663..d6108a9236 100644 --- a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -23,27 +23,12 @@ limitations under the License. http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd "> - + - + - - - - - org.apache.brooklyn.api.internal.ApiObjectsFactoryInterface - - - - - - diff --git a/core/src/main/java/org/apache/brooklyn/core/plan/PlanToSpecFactory.java b/core/src/main/java/org/apache/brooklyn/core/plan/PlanToSpecFactory.java index 0bfa4dee02..25c6c94c29 100644 --- a/core/src/main/java/org/apache/brooklyn/core/plan/PlanToSpecFactory.java +++ b/core/src/main/java/org/apache/brooklyn/core/plan/PlanToSpecFactory.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.ServiceLoader; +import org.apache.brooklyn.api.framework.FrameworkLookup; import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.core.typereg.BrooklynTypePlanTransformer; import org.apache.brooklyn.core.typereg.TypePlanTransformers; @@ -48,13 +49,13 @@ public class PlanToSpecFactory { private static final Logger log = LoggerFactory.getLogger(PlanToSpecFactory.class); private static Collection getAll(boolean includeDeprecated) { - ServiceLoader loader = ServiceLoader.load(PlanToSpecTransformer.class); - return ImmutableList.copyOf(includeDeprecated ? loader : filterDeprecated(loader)); + Iterable transformers = FrameworkLookup.lookupAll(PlanToSpecTransformer.class); + return ImmutableList.copyOf(includeDeprecated ? transformers : filterDeprecated(transformers)); } - private static Iterable filterDeprecated(ServiceLoader loader) { + private static Iterable filterDeprecated(Iterable transformers) { List result = MutableList.of(); - for (PlanToSpecTransformer t: loader) { + for (PlanToSpecTransformer t: transformers) { if (!isDeprecated(t.getClass())) { result.add(t); } diff --git a/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml b/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml index 0c19c136c1..f8ccb08437 100644 --- a/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml +++ b/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml @@ -16,7 +16,7 @@ limitations under the License. --> Date: Mon, 7 Mar 2016 17:08:18 +0000 Subject: [PATCH 03/12] Apply FrameworkLookup to TypePlanTransformers. --- .../CampTypePlanTransformerFactory.java | 29 ++++++++++++++++++ .../OSGI-INF/blueprint/blueprint.xml | 13 ++++++-- ...vaClassNameTypePlanTransformerFactory.java | 30 +++++++++++++++++++ .../core/typereg/TypePlanTransformers.java | 3 +- .../OSGI-INF/blueprint/blueprint.xml | 8 +++++ 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java create mode 100644 core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java new file mode 100644 index 0000000000..9e78bbb9ba --- /dev/null +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java @@ -0,0 +1,29 @@ +/* + * 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.brooklyn.camp.brooklyn.spi.creation; + +/** + * OSGI Factory for a CampTypePlanTransformer. + */ +public class CampTypePlanTransformerFactory { + + public static CampTypePlanTransformer newInstance() { + return new CampTypePlanTransformer(); + } +} diff --git a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml index d6108a9236..a714294797 100644 --- a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -23,12 +23,19 @@ limitations under the License. http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd "> + + + class="org.apache.brooklyn.camp.brooklyn.spi.creation.CampToSpecTransformerFactory" + factory-method="newInstance" /> + interface="org.apache.brooklyn.camp.brooklyn.spi.creation.CampToSpecTransformer" /> + + diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java b/core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java new file mode 100644 index 0000000000..01065d83f6 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java @@ -0,0 +1,30 @@ +/* + * 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.brooklyn.core.typereg; + +/** + * OSGI factory for JavaClassNameTypePlanTransformer. + */ +public class JavaClassNameTypePlanTransformerFactory { + + public static JavaClassNameTypePlanTransformer newInstance() { + return new JavaClassNameTypePlanTransformer(); + } +} + diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java b/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java index fb1bbd663c..f799d98db5 100644 --- a/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java +++ b/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java @@ -27,6 +27,7 @@ import java.util.ServiceLoader; import java.util.TreeMap; +import org.apache.brooklyn.api.framework.FrameworkLookup; import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.api.typereg.BrooklynTypeRegistry; import org.apache.brooklyn.api.typereg.RegisteredType; @@ -51,7 +52,7 @@ public class TypePlanTransformers { private static final Logger log = LoggerFactory.getLogger(TypePlanTransformers.class); private static Collection getAll() { - return ImmutableList.copyOf(ServiceLoader.load(BrooklynTypePlanTransformer.class)); + return ImmutableList.copyOf(FrameworkLookup.lookupAll(BrooklynTypePlanTransformer.class)); } private static Collection> OVERRIDE; diff --git a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 6ce5c926ac..9b0bbf57bf 100644 --- a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -30,4 +30,12 @@ limitations under the License. + + + + + From 8f88e41afc94bcdf7dfc7896dbaa44ceaffc464f Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Tue, 8 Mar 2016 09:51:55 +0000 Subject: [PATCH 04/12] Use prototype scope on beans rather than explicit factory classes. --- .../CampToSpecTransformerFactory.java | 30 ------------------- .../CampTypePlanTransformerFactory.java | 29 ------------------ .../OSGI-INF/blueprint/blueprint.xml | 8 ++--- ...vaClassNameTypePlanTransformerFactory.java | 30 ------------------- .../OSGI-INF/blueprint/blueprint.xml | 9 ++++-- 5 files changed, 11 insertions(+), 95 deletions(-) delete mode 100644 camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java delete mode 100644 camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java delete mode 100644 core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java deleted file mode 100644 index b0802a38a1..0000000000 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampToSpecTransformerFactory.java +++ /dev/null @@ -1,30 +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.brooklyn.camp.brooklyn.spi.creation; - -/** - * Deprecated class. Only required for OSGI integration. - */ -@Deprecated /** @deprecated since 0.9.0 use CampTypePlanTransformer */ -public class CampToSpecTransformerFactory { - - public static CampToSpecTransformer newInstance() { - return new CampToSpecTransformer(); - } -} diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java deleted file mode 100644 index 9e78bbb9ba..0000000000 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampTypePlanTransformerFactory.java +++ /dev/null @@ -1,29 +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.brooklyn.camp.brooklyn.spi.creation; - -/** - * OSGI Factory for a CampTypePlanTransformer. - */ -public class CampTypePlanTransformerFactory { - - public static CampTypePlanTransformer newInstance() { - return new CampTypePlanTransformer(); - } -} diff --git a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml index a714294797..a82b1f40d0 100644 --- a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -24,12 +24,12 @@ limitations under the License. "> + class="org.apache.brooklyn.camp.brooklyn.spi.creation.CampTypePlanTransformer" + scope="prototype"/> + class="org.apache.brooklyn.camp.brooklyn.spi.creation.CampToSpecTransformer" + scope="prototype"/> diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java b/core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java deleted file mode 100644 index 01065d83f6..0000000000 --- a/core/src/main/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerFactory.java +++ /dev/null @@ -1,30 +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.brooklyn.core.typereg; - -/** - * OSGI factory for JavaClassNameTypePlanTransformer. - */ -public class JavaClassNameTypePlanTransformerFactory { - - public static JavaClassNameTypePlanTransformer newInstance() { - return new JavaClassNameTypePlanTransformer(); - } -} - diff --git a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 9b0bbf57bf..915ab2b256 100644 --- a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -31,11 +31,16 @@ limitations under the License. interface="org.apache.brooklyn.core.BrooklynVersionService" /> + class="org.apache.brooklyn.core.typereg.JavaClassNameTypePlanTransformer" + scope="prototype"/> + + + From ebaeae7beac048f488b5a46c5430395ddc0ca35b Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Tue, 8 Mar 2016 10:30:13 +0000 Subject: [PATCH 05/12] Replace ServiceLoader with FrameworkLookup for EntitySpecResolver. --- .../entity/DelegatingEntitySpecResolver.java | 3 +- .../OSGI-INF/blueprint/blueprint.xml | 18 +++++++-- .../OSGI-INF/blueprint/blueprint.xml | 37 +++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml diff --git a/core/src/main/java/org/apache/brooklyn/core/resolve/entity/DelegatingEntitySpecResolver.java b/core/src/main/java/org/apache/brooklyn/core/resolve/entity/DelegatingEntitySpecResolver.java index 688c4ddf46..a0cd24a2c8 100644 --- a/core/src/main/java/org/apache/brooklyn/core/resolve/entity/DelegatingEntitySpecResolver.java +++ b/core/src/main/java/org/apache/brooklyn/core/resolve/entity/DelegatingEntitySpecResolver.java @@ -27,6 +27,7 @@ import javax.annotation.Nonnull; import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.framework.FrameworkLookup; import org.apache.brooklyn.api.mgmt.classloading.BrooklynClassLoadingContext; import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.exceptions.PropagatedRuntimeException; @@ -52,7 +53,7 @@ public DelegatingEntitySpecResolver(@Nonnull List resolvers) } protected static ImmutableList getRegisteredResolvers() { - return ImmutableList.copyOf(ServiceLoader.load(EntitySpecResolver.class)); + return ImmutableList.copyOf(FrameworkLookup.lookupAll(EntitySpecResolver.class)); } @Override diff --git a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 915ab2b256..fd5de71a15 100644 --- a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -31,16 +31,28 @@ limitations under the License. interface="org.apache.brooklyn.core.BrooklynVersionService" /> + class="org.apache.brooklyn.core.typereg.JavaClassNameTypePlanTransformer" + scope="prototype"/> + class="org.apache.brooklyn.core.catalog.internal.JavaCatalogToSpecTransformer"/> + + + + + + + + diff --git a/software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 0000000000..f0d62e13ce --- /dev/null +++ b/software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + From 98d1b6003e9b71c3a0d85d25152e31c5414ccf92 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Tue, 8 Mar 2016 14:26:05 +0000 Subject: [PATCH 06/12] Replace ServiceLoader with FrameworkLookup for ServiceTypeResolver. --- .../api/framework/FrameworkLookup.java | 42 ++++++++++++++++--- .../BrooklynComponentTemplateResolver.java | 11 ++--- .../OSGI-INF/blueprint/blueprint.xml | 2 +- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java index 6239dcb4d1..d965ba5b6b 100644 --- a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java +++ b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java @@ -50,12 +50,27 @@ public class FrameworkLookup { * @return A maybe of the instance found in the framework. */ public static Maybe lookup (Class clazz) { + return lookup(clazz, null); + } + + /** + * Find an instance of the given class in the framework. + * This first performs an OSGI lookup if the OSGI framework is available. If it is not then it falls back to + * attempting a lookup from the current context classpath via a ServiceLoader. + * + * @param clazz The class (typically the class of an interface) to search in the framework. + * @param loader If falling back to ServiceLoader, this class loader is used to load the class. If null, + * the system class loader (or, failing that, the bootstrap class loader) is used. + * @param The type for the class. + * @return A maybe of the instance found in the framework. + */ + public static Maybe lookup (Class clazz, ClassLoader loader) { Maybe result; if (OsgiUtil.isBrooklynInsideFramework()) { result = lookupInOsgi(clazz); } else { - result = lookupViaServiceLoader(clazz); + result = lookupViaServiceLoader(clazz, loader); } return result; @@ -71,20 +86,35 @@ public static Maybe lookup (Class clazz) { * @return An iterable over the instances found in the framework. */ public static Iterable lookupAll(Class clazz) { + return lookupAll(clazz, null); + } + + /** + * Find all instances of the given class in the framework. + * This first performs an OSGI lookup if the OSGI framework is available. If it is not then it falls back to + * attempting a lookup from the current context classpath via a ServiceLoader. + * + * @param clazz The class (typically the class of an interface) to search in the framework. + * @param loader If falling back to ServiceLoader, this class loader is used to load the class. If null, + * the system class loader (or, failing that, the bootstrap class loader) is used. + * @param The type for the class. + * @return An iterable over the instances found in the framework. + */ + public static Iterable lookupAll(Class clazz, ClassLoader loader) { Iterable result; if (OsgiUtil.isBrooklynInsideFramework()) { result = lookupAllInOsgi(clazz); } else { - result = lookupAllViaServiceLoader(clazz); + result = lookupAllViaServiceLoader(clazz, loader); } return result; } - private static Iterable lookupAllViaServiceLoader(Class clazz) { + private static Iterable lookupAllViaServiceLoader(Class clazz, ClassLoader loader) { LOG.debug("Looking up all " + clazz.getSimpleName() + " via ServiceLoader"); - return ServiceLoader.load(clazz); + return ServiceLoader.load(clazz, loader); } private static Iterable lookupAllInOsgi(Class clazz) { @@ -100,11 +130,11 @@ private static Iterable lookupAllInOsgi(Class clazz) { return result; } - private static Maybe lookupViaServiceLoader(Class clazz) { + private static Maybe lookupViaServiceLoader(Class clazz, ClassLoader loader) { LOG.debug("Looking up " + clazz.getSimpleName() + " via ServiceLoader"); Maybe result = Maybe.absent("No class " + clazz.getSimpleName() + " found with ServiceLoader"); - ServiceLoader LOADER = ServiceLoader.load(clazz); + ServiceLoader LOADER = ServiceLoader.load(clazz, loader); for (T item : LOADER) { return Maybe.of(item); } diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java index fdc2559e50..3e0acf2884 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java @@ -18,19 +18,14 @@ */ package org.apache.brooklyn.camp.brooklyn.spi.creation; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.Set; +import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.Nullable; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.framework.FrameworkLookup; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.api.mgmt.classloading.BrooklynClassLoadingContext; @@ -180,7 +175,7 @@ public EntitySpec resolveSpec(Set encounteredRegis private List getServiceTypeResolverOverrides() { List overrides = new ArrayList<>(); - ServiceLoader loader = ServiceLoader.load(ServiceTypeResolver.class, mgmt.getCatalogClassLoader()); + Iterable loader = FrameworkLookup.lookupAll(ServiceTypeResolver.class, mgmt.getCatalogClassLoader()); for (ServiceTypeResolver resolver : loader) { overrides.add(new ServiceTypeResolverAdaptor(this, resolver)); } diff --git a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index fd5de71a15..c28c155e10 100644 --- a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -41,7 +41,7 @@ limitations under the License. + interface="org.apache.brooklyn.core.plan.PlanToSpecTransformer"/> Date: Tue, 8 Mar 2016 14:56:22 +0000 Subject: [PATCH 07/12] Replace ServiceLoader with FrameworkLookup for LocationResolver. --- .../core/location/BasicLocationRegistry.java | 4 +- .../OSGI-INF/blueprint/blueprint.xml | 51 +++++++++++++++++-- .../OSGI-INF/blueprint/blueprint.xml | 38 ++++++++++++++ .../org/apache/brooklyn/cli/ItemLister.java | 4 +- .../OSGI-INF/blueprint/blueprint.xml | 10 ++-- 5 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 locations/jclouds/src/main/resources/OSGI-INF/blueprint/blueprint.xml diff --git a/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java b/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java index 0dc88d0213..51a036daf9 100644 --- a/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java +++ b/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java @@ -27,11 +27,11 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; -import java.util.ServiceLoader; import java.util.Set; import org.apache.brooklyn.api.catalog.BrooklynCatalog; import org.apache.brooklyn.api.catalog.CatalogItem; +import org.apache.brooklyn.api.framework.FrameworkLookup; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.api.location.LocationDefinition; import org.apache.brooklyn.api.location.LocationRegistry; @@ -152,7 +152,7 @@ public BasicLocationRegistry(ManagementContext mgmt) { } protected void findServices() { - ServiceLoader loader = ServiceLoader.load(LocationResolver.class, mgmt.getCatalogClassLoader()); + Iterable loader = FrameworkLookup.lookupAll(LocationResolver.class, mgmt.getCatalogClassLoader()); MutableList loadedResolvers; try { loadedResolvers = MutableList.copyOf(loader); diff --git a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index c28c155e10..227aae2b3d 100644 --- a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -46,13 +46,58 @@ limitations under the License. - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/locations/jclouds/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/locations/jclouds/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 0000000000..96d964f7a6 --- /dev/null +++ b/locations/jclouds/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + diff --git a/server-cli/src/main/java/org/apache/brooklyn/cli/ItemLister.java b/server-cli/src/main/java/org/apache/brooklyn/cli/ItemLister.java index 7b18fa7635..d7eddcec97 100644 --- a/server-cli/src/main/java/org/apache/brooklyn/cli/ItemLister.java +++ b/server-cli/src/main/java/org/apache/brooklyn/cli/ItemLister.java @@ -36,6 +36,7 @@ import org.apache.brooklyn.api.catalog.CatalogItem; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.ImplementedBy; +import org.apache.brooklyn.api.framework.FrameworkLookup; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.api.location.LocationResolver; import org.apache.brooklyn.api.objs.BrooklynObject; @@ -216,7 +217,8 @@ protected Map populateDescriptors() throws MalformedURLException policies.addAll(ItemDescriptors.toItemDescriptors(getTypes(urls, Policy.class), headingsOnly, "name")); enrichers.addAll(ItemDescriptors.toItemDescriptors(getTypes(urls, Enricher.class), headingsOnly, "name")); locations.addAll(ItemDescriptors.toItemDescriptors(getTypes(urls, Location.class, false), headingsOnly, "type")); - locationResolvers.addAll(ItemDescriptors.toItemDescriptors(ImmutableList.copyOf(ServiceLoader.load(LocationResolver.class)), true)); + locationResolvers.addAll(ItemDescriptors.toItemDescriptors( + ImmutableList.copyOf(FrameworkLookup.lookupAll(LocationResolver.class)), true)); } if (!yamlToScan.isEmpty()) { List urls = getYamlUrls(); diff --git a/software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml index f0d62e13ce..58c88a29c5 100644 --- a/software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/software/base/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -25,13 +25,17 @@ limitations under the License. - - + + + + From e6a412bfc0b0347364f50aee92977d6488d3d3e9 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Tue, 8 Mar 2016 16:56:17 +0000 Subject: [PATCH 08/12] Fix list handling. --- .../org/apache/brooklyn/api/framework/FrameworkLookup.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java index d965ba5b6b..32922b6154 100644 --- a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java +++ b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java @@ -24,8 +24,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.ServiceLoader; @@ -118,7 +118,7 @@ private static Iterable lookupAllViaServiceLoader(Class clazz, ClassLo } private static Iterable lookupAllInOsgi(Class clazz) { - final List result = Collections.emptyList(); + final List result = new ArrayList<>(); final Bundle bundle = FrameworkUtil.getBundle(clazz); if (bundle != null) { LOG.debug("Looking up all " + clazz.getSimpleName() + " in OSGI"); From 6c3c508c8da03447742d19567d225bcf045da7c0 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Thu, 10 Mar 2016 15:37:31 +0000 Subject: [PATCH 09/12] ServiceLookups should be in core, and use correct interface for campToSpecTransformerService. --- .../OSGI-INF/blueprint/blueprint.xml | 2 +- .../OSGI-INF/blueprint/blueprint.xml | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml index a82b1f40d0..63f759c1d2 100644 --- a/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/camp/camp-brooklyn/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -32,7 +32,7 @@ limitations under the License. scope="prototype"/> + interface="org.apache.brooklyn.core.plan.PlanToSpecTransformer" /> diff --git a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 227aae2b3d..f517f2b87b 100644 --- a/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -30,27 +30,36 @@ limitations under the License. + + + + + + org.apache.brooklyn.api.internal.ApiObjectsFactoryInterface + + + + class="org.apache.brooklyn.core.typereg.JavaClassNameTypePlanTransformer" + scope="prototype"/> + class="org.apache.brooklyn.core.catalog.internal.JavaCatalogToSpecTransformer"/> + class="org.apache.brooklyn.core.resolve.entity.JavaEntitySpecResolver"/> + class="org.apache.brooklyn.core.resolve.entity.CatalogEntitySpecResolver"/> From afcb689f0a31995a27c953abab73f4902d8f2754 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Wed, 16 Mar 2016 12:43:38 +0000 Subject: [PATCH 10/12] Update call to ServiceLoader.load to match previous call pattern. (Previous in the sense of prior to the FrameworkLookup change.) --- .../org/apache/brooklyn/api/framework/FrameworkLookup.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java index 32922b6154..3dbe64f6fa 100644 --- a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java +++ b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java @@ -111,9 +111,12 @@ public static Iterable lookupAll(Class clazz, ClassLoader loader) { return result; } - private static Iterable lookupAllViaServiceLoader(Class clazz, ClassLoader loader) { + private static ServiceLoader lookupAllViaServiceLoader(Class clazz, ClassLoader loader) { LOG.debug("Looking up all " + clazz.getSimpleName() + " via ServiceLoader"); + if (null == loader) { + return ServiceLoader.load(clazz); + } return ServiceLoader.load(clazz, loader); } @@ -134,7 +137,7 @@ private static Maybe lookupViaServiceLoader(Class clazz, ClassLoader l LOG.debug("Looking up " + clazz.getSimpleName() + " via ServiceLoader"); Maybe result = Maybe.absent("No class " + clazz.getSimpleName() + " found with ServiceLoader"); - ServiceLoader LOADER = ServiceLoader.load(clazz, loader); + ServiceLoader LOADER = lookupAllViaServiceLoader(clazz, loader); for (T item : LOADER) { return Maybe.of(item); } From a93c170b6753110b034dcb8244e33ea859bac544 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Wed, 16 Mar 2016 15:02:18 +0000 Subject: [PATCH 11/12] Add Javadoc note on use of FrameworkLooup and OSGI service reference count. --- .../org/apache/brooklyn/api/framework/FrameworkLookup.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java index 3dbe64f6fa..898f043ca4 100644 --- a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java +++ b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java @@ -35,6 +35,13 @@ * The intention of this class is to provide a means to lookup beans in the OSGI service registry that were * previously looked up via a ServiceLoader, but still maintain the backward compatible behaviour when not running * in an OSGI container. + * + * NOTE, when OSGI bundle registry is used, this code performs a BundleContext.getService(). When calling + * BundleContext.getService() the reference count for the service in question is increased, and without any calls to + * BundleContext.ungetService() the only moment it will drop to zero is when the client bundle is unloaded, + * which this code does not do. Therefore this class is not suitable for use in a situation where client code needs + * to take account of services coming and going, and explicitly avoid using the service when its reference count has + * gone to zero. */ public class FrameworkLookup { From 71d5a1083f250ac419cdb34b139ba62abef1bd4d Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Fri, 18 Mar 2016 16:41:26 +0000 Subject: [PATCH 12/12] Use FrameworkLookup class to get the bundle context. Necessary to avoid NPE at line 135 and 160 if the class being looked up is in a bundle that hasn't started yet. We don't really need to get the bundle context from that class, any suitable one will do, and we know that the bundle we live in has started! --- .../org/apache/brooklyn/api/framework/FrameworkLookup.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java index 898f043ca4..c2d68c47ca 100644 --- a/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java +++ b/api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java @@ -129,7 +129,7 @@ private static ServiceLoader lookupAllViaServiceLoader(Class clazz, Cl private static Iterable lookupAllInOsgi(Class clazz) { final List result = new ArrayList<>(); - final Bundle bundle = FrameworkUtil.getBundle(clazz); + final Bundle bundle = FrameworkUtil.getBundle(FrameworkLookup.class); if (bundle != null) { LOG.debug("Looking up all " + clazz.getSimpleName() + " in OSGI"); BundleContext ctx = bundle.getBundleContext(); @@ -154,7 +154,7 @@ private static Maybe lookupViaServiceLoader(Class clazz, ClassLoader l private static Maybe lookupInOsgi(Class clazz) { Maybe result = Maybe.absent("No class " + clazz.getSimpleName() + " found with OSGI"); - final Bundle bundle = FrameworkUtil.getBundle(clazz); + final Bundle bundle = FrameworkUtil.getBundle(FrameworkLookup.class); if (bundle != null) { LOG.debug("Looking up " + clazz.getSimpleName() + " in OSGI"); BundleContext ctx = bundle.getBundleContext();