Repository: karaf
Updated Branches:
  refs/heads/master f9a03a363 -> f4ad8c4fa


http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/util/src/main/java/org/apache/karaf/util/tracker/BaseActivator.java
----------------------------------------------------------------------
diff --git 
a/util/src/main/java/org/apache/karaf/util/tracker/BaseActivator.java 
b/util/src/main/java/org/apache/karaf/util/tracker/BaseActivator.java
index 0948f0b..d55a7de 100644
--- a/util/src/main/java/org/apache/karaf/util/tracker/BaseActivator.java
+++ b/util/src/main/java/org/apache/karaf/util/tracker/BaseActivator.java
@@ -48,7 +48,7 @@ public class BaseActivator implements BundleActivator, 
SingleServiceTracker.Sing
     private long schedulerStopTimeout = TimeUnit.MILLISECONDS.convert(30, 
TimeUnit.SECONDS);
 
     private List<ServiceRegistration> registrations;
-    private Map<String, SingleServiceTracker> trackers = new HashMap<String, 
SingleServiceTracker>();
+    private Map<String, SingleServiceTracker> trackers = new HashMap<>();
     private ServiceRegistration managedServiceRegistration;
     private Dictionary<String, ?> configuration;
 
@@ -88,6 +88,16 @@ public class BaseActivator implements BundleActivator, 
SingleServiceTracker.Sing
     }
 
     protected void doOpen() throws Exception {
+        Services services = getClass().getAnnotation(Services.class);
+        if (services != null) {
+            for (RequireService require : services.requires()) {
+                trackService(require.value(), require.filter());
+            }
+        }
+        Managed managed = getClass().getAnnotation(Managed.class);
+        if (managed != null) {
+            manage(managed.value());
+        }
     }
 
     protected void doClose() {
@@ -115,7 +125,7 @@ public class BaseActivator implements BundleActivator, 
SingleServiceTracker.Sing
      * Called in {@link #doOpen()}
      */
     protected void manage(String pid) {
-        Hashtable<String, Object> props = new Hashtable<String, Object>();
+        Hashtable<String, Object> props = new Hashtable<>();
         props.put(Constants.SERVICE_PID, pid);
         managedServiceRegistration = bundleContext.registerService(
                 "org.osgi.service.cm.ManagedService", this, props);
@@ -220,9 +230,9 @@ public class BaseActivator implements BundleActivator, 
SingleServiceTracker.Sing
     /**
      * Called in {@link #doOpen()}
      */
-    protected void trackService(Class clazz) {
+    protected void trackService(Class<?> clazz) throws InvalidSyntaxException {
         if (!trackers.containsKey(clazz.getName())) {
-            SingleServiceTracker tracker = new 
SingleServiceTracker(bundleContext, clazz, this);
+            SingleServiceTracker tracker = new 
SingleServiceTracker<>(bundleContext, clazz, this);
             tracker.open();
             trackers.put(clazz.getName(), tracker);
         }
@@ -231,20 +241,12 @@ public class BaseActivator implements BundleActivator, 
SingleServiceTracker.Sing
     /**
      * Called in {@link #doOpen()}
      */
-    protected void trackService(String clazz) {
-        if (!trackers.containsKey(clazz)) {
-            SingleServiceTracker tracker = new 
SingleServiceTracker(bundleContext, clazz, this);
-            tracker.open();
-            trackers.put(clazz, tracker);
-        }
-    }
-
-    /**
-     * Called in {@link #doOpen()}
-     */
-    protected void trackService(Class clazz, String filter) throws 
InvalidSyntaxException {
+    protected void trackService(Class<?> clazz, String filter) throws 
InvalidSyntaxException {
         if (!trackers.containsKey(clazz.getName())) {
-            SingleServiceTracker tracker = new 
SingleServiceTracker(bundleContext, clazz, filter, this);
+            if (filter != null && filter.isEmpty()) {
+                filter = null;
+            }
+            SingleServiceTracker tracker = new 
SingleServiceTracker<>(bundleContext, clazz, filter, this);
             tracker.open();
             trackers.put(clazz.getName(), tracker);
         }
@@ -264,19 +266,8 @@ public class BaseActivator implements BundleActivator, 
SingleServiceTracker.Sing
     /**
      * Called in {@link #doStart()}
      */
-    protected Object getTrackedService(String clazz) {
-        SingleServiceTracker tracker = trackers.get(clazz);
-        if (tracker == null) {
-            throw new IllegalStateException("Service not tracked for class " + 
clazz);
-        }
-        return tracker.getService();
-    }
-
-    /**
-     * Called in {@link #doStart()}
-     */
     protected void registerMBean(Object mbean, String type) {
-        Hashtable<String, Object> props = new Hashtable<String, Object>();
+        Hashtable<String, Object> props = new Hashtable<>();
         props.put("jmx.objectname", "org.apache.karaf:" + type + ",name=" + 
System.getProperty("karaf.name"));
         
trackRegistration(bundleContext.registerService(getInterfaceNames(mbean), 
mbean, props));
     }
@@ -284,54 +275,44 @@ public class BaseActivator implements BundleActivator, 
SingleServiceTracker.Sing
     /**
      * Called in {@link #doStart()}
      */
-    protected void register(Class clazz, Object service) {
-        register(clazz, service, null);
-    }
-
-    /**
-     * Called in {@link #doStart()}
-     */
-    protected void register(Class clazz, Object service, Dictionary<String, ?> 
props) {
-        trackRegistration(bundleContext.registerService(clazz, service, 
props));
-    }
-
-    /**
-     * Called in {@link #doStart()}
-     */
-    protected void register(String clazz, Object service) {
+    protected <T> void register(Class<T> clazz, T service) {
         register(clazz, service, null);
     }
 
     /**
      * Called in {@link #doStart()}
      */
-    protected void register(String clazz, Object service, Dictionary<String, 
?> props) {
+    protected <T> void register(Class<T> clazz, T service, Dictionary<String, 
?> props) {
         trackRegistration(bundleContext.registerService(clazz, service, 
props));
     }
 
     /**
      * Called in {@link #doStart()}
      */
-    protected void register(String[] clazz, Object service) {
+    protected void register(Class[] clazz, Object service) {
         register(clazz, service, null);
     }
 
     /**
      * Called in {@link #doStart()}
      */
-    protected void register(String[] clazz, Object service, Dictionary<String, 
?> props) {
-        trackRegistration(bundleContext.registerService(clazz, service, 
props));
+    protected void register(Class[] clazz, Object service, Dictionary<String, 
?> props) {
+        String[] names = new String[clazz.length];
+        for (int i = 0; i < clazz.length; i++) {
+            names[i] = clazz[i].getName();
+        }
+        trackRegistration(bundleContext.registerService(names, service, 
props));
     }
 
     private void trackRegistration(ServiceRegistration registration) {
         if (registrations == null) {
-            registrations = new ArrayList<ServiceRegistration>();
+            registrations = new ArrayList<>();
         }
         registrations.add(registration);
     }
 
     protected String[] getInterfaceNames(Object object) {
-        List<String> names = new ArrayList<String>();
+        List<String> names = new ArrayList<>();
         for (Class cl = object.getClass(); cl != Object.class; cl = 
cl.getSuperclass()) {
             addSuperInterfaces(names, cl);
         }

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/util/src/main/java/org/apache/karaf/util/tracker/Managed.java
----------------------------------------------------------------------
diff --git a/util/src/main/java/org/apache/karaf/util/tracker/Managed.java 
b/util/src/main/java/org/apache/karaf/util/tracker/Managed.java
new file mode 100644
index 0000000..4aec385
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/tracker/Managed.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.karaf.util.tracker;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE})
+public @interface Managed {
+
+    String value();
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/util/src/main/java/org/apache/karaf/util/tracker/ProvideService.java
----------------------------------------------------------------------
diff --git 
a/util/src/main/java/org/apache/karaf/util/tracker/ProvideService.java 
b/util/src/main/java/org/apache/karaf/util/tracker/ProvideService.java
new file mode 100644
index 0000000..7913746
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/tracker/ProvideService.java
@@ -0,0 +1,27 @@
+/*
+ * 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.karaf.util.tracker;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ProvideService {
+
+    Class value();
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/util/src/main/java/org/apache/karaf/util/tracker/RequireService.java
----------------------------------------------------------------------
diff --git 
a/util/src/main/java/org/apache/karaf/util/tracker/RequireService.java 
b/util/src/main/java/org/apache/karaf/util/tracker/RequireService.java
new file mode 100644
index 0000000..da391e2
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/tracker/RequireService.java
@@ -0,0 +1,31 @@
+/*
+ * 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.karaf.util.tracker;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface RequireService {
+
+    Class value();
+
+    String filter() default "";
+
+    boolean optional() default false;
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/util/src/main/java/org/apache/karaf/util/tracker/Services.java
----------------------------------------------------------------------
diff --git a/util/src/main/java/org/apache/karaf/util/tracker/Services.java 
b/util/src/main/java/org/apache/karaf/util/tracker/Services.java
new file mode 100644
index 0000000..7575138
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/tracker/Services.java
@@ -0,0 +1,32 @@
+/*
+ * 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.karaf.util.tracker;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE})
+public @interface Services {
+
+    ProvideService[] provides() default {};
+
+    RequireService[] requires() default {};
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/util/src/main/java/org/apache/karaf/util/tracker/SingleServiceTracker.java
----------------------------------------------------------------------
diff --git 
a/util/src/main/java/org/apache/karaf/util/tracker/SingleServiceTracker.java 
b/util/src/main/java/org/apache/karaf/util/tracker/SingleServiceTracker.java
index eca364f..0e547e7 100644
--- a/util/src/main/java/org/apache/karaf/util/tracker/SingleServiceTracker.java
+++ b/util/src/main/java/org/apache/karaf/util/tracker/SingleServiceTracker.java
@@ -48,8 +48,8 @@ public final class SingleServiceTracker<T> {
     private final AtomicReference<ServiceReference> ref = new 
AtomicReference<ServiceReference>();
     private final AtomicBoolean open = new AtomicBoolean(false);
     private final SingleServiceListener serviceListener;
-    private String filterString;
-    private Filter filter;
+    private final String filterString;
+    private final Filter filter;
 
     private final ServiceListener listener = new ServiceListener() {
         public void serviceChanged(ServiceEvent event) {
@@ -66,22 +66,16 @@ public final class SingleServiceTracker<T> {
         }
     };
 
-    public SingleServiceTracker(BundleContext context, String className, 
SingleServiceListener sl) {
-        ctx = context;
-        this.className = className;
-        serviceListener = sl;
-    }
-
-    public SingleServiceTracker(BundleContext context, Class<T> clazz, 
SingleServiceListener sl) {
-        ctx = context;
-        this.className = clazz.getName();
-        serviceListener = sl;
+    public SingleServiceTracker(BundleContext context, Class<T> clazz, 
SingleServiceListener sl) throws InvalidSyntaxException {
+        this(context, clazz, null, sl);
     }
 
     public SingleServiceTracker(BundleContext context, Class<T> clazz, String 
filterString, SingleServiceListener sl) throws InvalidSyntaxException {
-        this(context, clazz, sl);
+        this.ctx = context;
+        this.className = clazz.getName();
+        this.serviceListener = sl;
         this.filterString = filterString;
-        if (filterString != null) filter = context.createFilter(filterString);
+        this.filter = (filterString != null) ? 
context.createFilter(filterString) : null;
     }
 
     public T getService() {

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/web/pom.xml
----------------------------------------------------------------------
diff --git a/web/pom.xml b/web/pom.xml
index 037ee5f..a2754f3 100644
--- a/web/pom.xml
+++ b/web/pom.xml
@@ -86,6 +86,10 @@
         </resources>
         <plugins>
             <plugin>
+                <groupId>org.apache.karaf.tooling</groupId>
+                <artifactId>karaf-services-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <configuration>
@@ -101,6 +105,12 @@
                             org.apache.karaf.web.management.internal,
                             org.apache.karaf.util.tracker
                         </Private-Package>
+                        <Provide-Capability>
+                            ${capabilities}
+                        </Provide-Capability>
+                        <Require-Capability>
+                            ${requirements}
+                        </Require-Capability>
                         <Bundle-Activator>
                             org.apache.karaf.web.internal.osgi.Activator
                         </Bundle-Activator>

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/web/src/main/java/org/apache/karaf/web/internal/osgi/Activator.java
----------------------------------------------------------------------
diff --git 
a/web/src/main/java/org/apache/karaf/web/internal/osgi/Activator.java 
b/web/src/main/java/org/apache/karaf/web/internal/osgi/Activator.java
index e5bcfe1..4709e81 100644
--- a/web/src/main/java/org/apache/karaf/web/internal/osgi/Activator.java
+++ b/web/src/main/java/org/apache/karaf/web/internal/osgi/Activator.java
@@ -17,6 +17,10 @@
 package org.apache.karaf.web.internal.osgi;
 
 import org.apache.karaf.util.tracker.BaseActivator;
+import org.apache.karaf.util.tracker.Managed;
+import org.apache.karaf.util.tracker.ProvideService;
+import org.apache.karaf.util.tracker.RequireService;
+import org.apache.karaf.util.tracker.Services;
 import org.apache.karaf.web.WebContainerService;
 import org.apache.karaf.web.internal.WebContainerServiceImpl;
 import org.apache.karaf.web.internal.WebEventHandler;
@@ -24,14 +28,14 @@ import 
org.apache.karaf.web.management.internal.WebMBeanImpl;
 import org.ops4j.pax.web.service.spi.WarManager;
 import org.ops4j.pax.web.service.spi.WebListener;
 
+@Services(
+        requires = @RequireService(WarManager.class),
+        provides = @ProvideService(WebContainerService.class)
+)
+@Managed("org.apache.karaf.shell")
 public class Activator extends BaseActivator {
 
     @Override
-    protected void doOpen() throws Exception {
-        trackService(WarManager.class);
-    }
-
-    @Override
     protected void doStart() throws Exception {
         WarManager warManager = getTrackedService(WarManager.class);
         if (warManager == null) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/wrapper/pom.xml
----------------------------------------------------------------------
diff --git a/wrapper/pom.xml b/wrapper/pom.xml
index 6f36786..823621b 100644
--- a/wrapper/pom.xml
+++ b/wrapper/pom.xml
@@ -95,6 +95,10 @@
                 </configuration>
             </plugin>
             <plugin>
+                <groupId>org.apache.karaf.tooling</groupId>
+                <artifactId>karaf-services-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <configuration>
@@ -121,6 +125,12 @@
                         <Bundle-Activator>
                             org.apache.karaf.wrapper.internal.osgi.Activator
                         </Bundle-Activator>
+                        <Provide-Capability>
+                            ${capabilities}
+                        </Provide-Capability>
+                        <Require-Capability>
+                            ${requirements}
+                        </Require-Capability>
                         <Karaf-Commands>
                             org.apache.karaf.wrapper.commands
                         </Karaf-Commands>

http://git-wip-us.apache.org/repos/asf/karaf/blob/005c0a30/wrapper/src/main/java/org/apache/karaf/wrapper/internal/osgi/Activator.java
----------------------------------------------------------------------
diff --git 
a/wrapper/src/main/java/org/apache/karaf/wrapper/internal/osgi/Activator.java 
b/wrapper/src/main/java/org/apache/karaf/wrapper/internal/osgi/Activator.java
index 0cb8242..43aebe9 100644
--- 
a/wrapper/src/main/java/org/apache/karaf/wrapper/internal/osgi/Activator.java
+++ 
b/wrapper/src/main/java/org/apache/karaf/wrapper/internal/osgi/Activator.java
@@ -17,10 +17,13 @@
 package org.apache.karaf.wrapper.internal.osgi;
 
 import org.apache.karaf.util.tracker.BaseActivator;
+import org.apache.karaf.util.tracker.ProvideService;
+import org.apache.karaf.util.tracker.Services;
 import org.apache.karaf.wrapper.WrapperService;
 import org.apache.karaf.wrapper.internal.WrapperServiceImpl;
 import org.apache.karaf.wrapper.management.internal.WrapperMBeanImpl;
 
+@Services(provides = @ProvideService(WrapperService.class))
 public class Activator extends BaseActivator {
 
     @Override

Reply via email to