SLING-2140 :  Add a ConfigurationPrinter to output the Repository Descriptors

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1147909 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/jcr/base/internal/RepositoryPrinter.java b/src/main/java/org/apache/sling/jcr/base/internal/RepositoryPrinter.java
new file mode 100644
index 0000000..58a9b35
--- /dev/null
+++ b/src/main/java/org/apache/sling/jcr/base/internal/RepositoryPrinter.java
@@ -0,0 +1,88 @@
+/*
+ * 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.jcr.base.internal;
+
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.jcr.Repository;
+
+/**
+ * The Printer Plugin
+ */
+public class RepositoryPrinter {
+
+    private final Repository repository;
+
+    private final String name;
+
+    public RepositoryPrinter(final Repository repo, final Map<String, Object> props) {
+        this.repository = repo;
+        if ( repo.getDescriptor(Repository.REP_NAME_DESC) != null ) {
+            name = "Repository " + repo.getDescriptor(Repository.REP_NAME_DESC);
+        } else  if ( props.get("name") != null ) {
+            name = "Repository " + props.get("name").toString();
+        } else {
+            this.name = "Repository @" + repo.hashCode();
+        }
+    }
+
+    public String getTitle() {
+        return this.name;
+    }
+
+    public Dictionary<String, Object> getProperties() {
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put("felix.webconsole.label", this.name);
+        props.put("felix.webconsole.title", this.name);
+        props.put("felix.webconsole.configprinter.modes", "always");
+
+        return props;
+    }
+
+    public void printConfiguration(final PrintWriter pw) {
+        // try to get repository
+        final Repository repo = this.repository;
+        writeHeader(pw, "Repository Properties");
+        final String[] keys = repo.getDescriptorKeys();
+        Arrays.sort(keys);
+        for (final String key : keys) {
+            final String val = repo.getDescriptor(key);
+            writeEntry(pw, key, val);
+        }
+    }
+
+    private void writeHeader(final PrintWriter pw, final String value) {
+        pw.print(value);
+        pw.println(":");
+    }
+
+    private void writeEntry(final PrintWriter pw, final String key, final String value) {
+        pw.print(key);
+        pw.print(": ");
+        if ( value != null ) {
+            pw.println(value);
+        } else {
+            pw.println("-");
+        }
+    }
+}
diff --git a/src/main/java/org/apache/sling/jcr/base/internal/SlingRepositoryProvider.java b/src/main/java/org/apache/sling/jcr/base/internal/SlingRepositoryProvider.java
index 44903eb..5e94a20 100644
--- a/src/main/java/org/apache/sling/jcr/base/internal/SlingRepositoryProvider.java
+++ b/src/main/java/org/apache/sling/jcr/base/internal/SlingRepositoryProvider.java
@@ -111,21 +111,42 @@
         }
     }
 
+    private void registerPrinter(final BundleContext processContext,
+            final Repository repo,
+            final Map<String, Object> props) {
+        logger.info("Providing new configuration printer for {} : {}", repo, props);
+        final Long key = (Long)props.get(Constants.SERVICE_ID);
+        RepositoryRegistration reg;
+        synchronized ( this.registrations ) {
+            reg = this.registrations.get(key);
+        }
+        if ( reg == null ) {
+            reg = new RepositoryRegistration();
+            synchronized ( this.registrations ) {
+                this.registrations.put(key, reg);
+            }
+        }
+        final RepositoryPrinter printer = new RepositoryPrinter(repo, props);
+        reg.printer = processContext.registerService(RepositoryPrinter.class.getName(),
+                printer, printer.getProperties());
+    }
+
     /**
      * Bind a new repository.
      */
     protected void bindRepository(final Repository repo, final Map<String, Object> props) {
-        if ( !isSlingRepository(props) ) {
-            final BundleContext processContext;
-            synchronized ( pendingServices ) {
-                processContext = this.bundleContext;
-                if ( processContext == null ) {
-                    this.pendingServices.add(new PendingService(repo, props));
-                }
+        final BundleContext processContext;
+        synchronized ( pendingServices ) {
+            processContext = this.bundleContext;
+            if ( processContext == null ) {
+                this.pendingServices.add(new PendingService(repo, props));
             }
-            if ( processContext != null ) {
+        }
+        if ( processContext != null ) {
+            if ( !isSlingRepository(props) ) {
                 this.registerRepository(processContext, repo, props);
             }
+            this.registerPrinter(processContext, repo, props);
         }
     }
 
@@ -133,20 +154,23 @@
      * Unind a new repository.
      */
     protected void unbindRepository(final Repository repo, final Map<String, Object> props) {
-        if ( !isSlingRepository(props) ) {
-            synchronized ( pendingServices ) {
-                this.pendingServices.remove(new PendingService(repo, props));
-            }
-            final Long key = (Long)props.get(Constants.SERVICE_ID);
-            final RepositoryRegistration slingRepo;
-            synchronized ( this.registrations ) {
-                slingRepo = this.registrations.remove(key);
-            }
-            if ( slingRepo != null ) {
+        synchronized ( pendingServices ) {
+            this.pendingServices.remove(new PendingService(repo, props));
+        }
+        final Long key = (Long)props.get(Constants.SERVICE_ID);
+        final RepositoryRegistration slingRepo;
+        synchronized ( this.registrations ) {
+            slingRepo = this.registrations.remove(key);
+        }
+        if ( slingRepo != null ) {
+            if ( slingRepo.wrapper != null ) {
                 logger.debug("Unregistering SlingRepository for {} : {}", repo, props);
                 slingRepo.wrapper.dispose();
                 slingRepo.registration.unregister();
             }
+            if ( slingRepo.printer != null ) {
+                slingRepo.printer.unregister();
+            }
         }
     }
 
@@ -187,6 +211,7 @@
      */
     private static final class RepositoryRegistration {
         public ServiceRegistration registration;
+        public ServiceRegistration printer;
         public SlingRepositoryWrapper wrapper;
     }
 }