You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:54:46 UTC
[sling-org-apache-sling-models-impl] 04/10: SLING-3335 - add a
models configuration printer to see the currently registered injectors
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.models.impl-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit 40ea6f5d34ffd495e996e1fe66b83424367127c8
Author: Justin Edelson <ju...@apache.org>
AuthorDate: Fri Jan 31 04:06:59 2014 +0000
SLING-3335 - add a models configuration printer to see the currently registered injectors
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1563047 13f79535-47bb-0310-9956-ffa450edef68
---
.../sling/models/impl/ModelAdapterFactory.java | 33 +++++++++++++--
.../models/impl/ModelConfigurationPrinter.java | 47 ++++++++++++++++++++++
.../sling/models/impl/OSGiInjectionTest.java | 2 +
3 files changed, 78 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
index aeb0655..92fc791 100644
--- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
+++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
@@ -55,11 +55,13 @@ import org.apache.sling.commons.osgi.ServiceUtil;
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 implements AdapterFactory, Runnable {
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 implements AdapterFactory, Runnable {
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 implements AdapterFactory, Runnable {
@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 implements AdapterFactory, Runnable {
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 implements AdapterFactory, Runnable {
}
}
+ Injector[] getInjectors() {
+ return sortedInjectors;
+ }
+
}
diff --git a/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java b/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java
new file mode 100644
index 0000000..3e874b1
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java
@@ -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
diff --git a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
index a7989ee..b26eb13 100644
--- a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
+++ b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
@@ -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);
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.