Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/65#discussion_r56539375
--- Diff:
api/src/main/java/org/apache/brooklyn/api/framework/FrameworkLookup.java ---
@@ -0,0 +1,179 @@
+/*
+ * 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.apache.brooklyn.util.osgi.OsgiUtil;
+import org.osgi.framework.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+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.
+ *
+ * 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 {
+
+ 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 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 <T> The type for the class.
+ * @return A maybe of the instance found in the framework.
+ */
+ public static <T> Maybe<T> lookup (Class<T> 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 <tt>null</tt>,
+ * the system class loader (or, failing that, the bootstrap
class loader) is used.
+ * @param <T> The type for the class.
+ * @return A maybe of the instance found in the framework.
+ */
+ public static <T> Maybe<T> lookup (Class<T> clazz, ClassLoader loader)
{
--- End diff --
Personal preference, could cut this file in half if supporting only the
`lookupAll` case with users getting the first entry where needed.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---