You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2014/01/31 05:06:59 UTC

svn commit: r1563047 - in /sling/trunk/bundles/extensions/models/impl/src: main/java/org/apache/sling/models/impl/ test/java/org/apache/sling/models/impl/

Author: justin
Date: Fri Jan 31 04:06:59 2014
New Revision: 1563047

URL: http://svn.apache.org/r1563047
Log:
SLING-3335 - add a models configuration printer to see the currently registered injectors

Added:
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java
Modified:
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java

Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java?rev=1563047&r1=1563046&r2=1563047&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java Fri Jan 31 04:06:59 2014
@@ -55,11 +55,13 @@ import org.apache.sling.commons.osgi.Ser
 import org.apache.sling.models.annotations.Default;
 import org.apache.sling.models.annotations.Model;
 import org.apache.sling.models.annotations.Optional;
-import org.apache.sling.models.annotations.Via;
 import org.apache.sling.models.annotations.Source;
+import org.apache.sling.models.annotations.Via;
 import org.apache.sling.models.spi.DisposalCallback;
 import org.apache.sling.models.spi.DisposalCallbackRegistry;
 import org.apache.sling.models.spi.Injector;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
@@ -68,7 +70,7 @@ import org.slf4j.LoggerFactory;
 @Component
 public class ModelAdapterFactory implements AdapterFactory, Runnable {
 
-    public static class DisposalCallbackRegistryImpl implements DisposalCallbackRegistry {
+    private static class DisposalCallbackRegistryImpl implements DisposalCallbackRegistry {
 
         private List<DisposalCallback> callbacks = new ArrayList<DisposalCallback>();
 
@@ -93,7 +95,7 @@ public class ModelAdapterFactory impleme
 
     private ConcurrentMap<java.lang.ref.Reference<Object>, DisposalCallbackRegistryImpl> disposalCallbacks;
 
-    public static class MapBackedInvocationHandler implements InvocationHandler {
+    private static class MapBackedInvocationHandler implements InvocationHandler {
 
         private Map<Method, Object> methods;
 
@@ -129,6 +131,8 @@ public class ModelAdapterFactory impleme
 
     private ServiceRegistration jobRegistration;
 
+    private ServiceRegistration configPrinterRegistration;
+
     @SuppressWarnings("unchecked")
     public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
         Model modelAnnotation = type.getAnnotation(Model.class);
@@ -579,16 +583,29 @@ public class ModelAdapterFactory impleme
 
     @Activate
     protected void activate(final ComponentContext ctx) {
+        BundleContext bundleContext = ctx.getBundleContext();
         this.queue = new ReferenceQueue<Object>();
         this.disposalCallbacks = new ConcurrentHashMap<java.lang.ref.Reference<Object>, DisposalCallbackRegistryImpl>();
         Hashtable<Object, Object> properties = new Hashtable<Object, Object>();
+        properties.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
+        properties.put(Constants.SERVICE_DESCRIPTION, "Sling Models OSGi Service Disposal Job");
         properties.put("scheduler.concurrent", false);
         properties.put("scheduler.period", Long.valueOf(30));
 
-        this.jobRegistration = ctx.getBundleContext().registerService(Runnable.class.getName(), this,
+        this.jobRegistration = bundleContext.registerService(Runnable.class.getName(), this,
                 properties);
 
         this.listener = new ModelPackageBundleListener(ctx.getBundleContext(), this);
+
+        Hashtable<Object, Object> printerProps = new Hashtable<Object, Object>();
+        printerProps.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
+        printerProps.put(Constants.SERVICE_DESCRIPTION, "Sling Models Configuration Printer");
+        printerProps.put("felix.webconsole.label", "slingmodels");
+        printerProps.put("felix.webconsole.title", "Sling Models");
+        printerProps.put("felix.webconsole.configprinter.modes", "always");
+
+        this.configPrinterRegistration = bundleContext.registerService(Object.class.getName(),
+            new ModelConfigurationPrinter(this), printerProps);
     }
 
     @Deactivate
@@ -598,6 +615,10 @@ public class ModelAdapterFactory impleme
             jobRegistration.unregister();
             jobRegistration = null;
         }
+        if (configPrinterRegistration != null) {
+            configPrinterRegistration.unregister();
+            configPrinterRegistration = null;
+        }
     }
 
     protected void bindInjector(final Injector injector, final Map<String, Object> props) {
@@ -614,4 +635,8 @@ public class ModelAdapterFactory impleme
         }
     }
 
+    Injector[] getInjectors() {
+        return sortedInjectors;
+    }
+
 }

Added: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java?rev=1563047&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java Fri Jan 31 04:06:59 2014
@@ -0,0 +1,47 @@
+/*
+ * 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.sling.models.impl;
+
+import java.io.PrintWriter;
+
+import org.apache.sling.models.spi.Injector;
+
+public class ModelConfigurationPrinter {
+
+    private final ModelAdapterFactory modelAdapterFactory;
+
+    /**
+     * @param modelAdapterFactory
+     */
+    ModelConfigurationPrinter(ModelAdapterFactory modelAdapterFactory) {
+        this.modelAdapterFactory = modelAdapterFactory;
+    }
+
+    public void printConfiguration(PrintWriter printWriter) {
+        printWriter.println("Sling Models Injectors:");
+        Injector[] injectors = modelAdapterFactory.getInjectors();
+        if (injectors == null) {
+            printWriter.println("none");
+        } else {
+            for (Injector injector : injectors) {
+                printWriter.printf("%s - %s", injector.getName(), injector.getClass().getName());
+                printWriter.println();
+            }
+        }
+    }
+
+}
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java?rev=1563047&r1=1563046&r2=1563047&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java Fri Jan 31 04:06:59 2014
@@ -107,6 +107,7 @@ public class OSGiInjectionTest {
 
         verify(bundleContext).registerService(eq(Runnable.class.getName()), eq(factory), any(Dictionary.class));
         verify(bundleContext).addBundleListener(any(BundleListener.class));
+        verify(bundleContext).registerService(eq(Object.class.getName()), any(Object.class), any(Dictionary.class));
         verify(bundleContext).getBundles();
         verifyNoMoreInteractions(bundleContext);
     }
@@ -203,6 +204,7 @@ public class OSGiInjectionTest {
 
         verify(bundleContext).registerService(eq(Runnable.class.getName()), eq(factory), any(Dictionary.class));
         verify(bundleContext).addBundleListener(any(BundleListener.class));
+        verify(bundleContext).registerService(eq(Object.class.getName()), any(Object.class), any(Dictionary.class));
         verify(bundleContext).getBundles();
         verifyNoMoreInteractions(res, bundleContext);
     }