FELIX-4785 : Incompatible SCR API

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1660307 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/bnd.bnd b/scr/bnd.bnd
index b84f0a3..0a175a2 100644
--- a/scr/bnd.bnd
+++ b/scr/bnd.bnd
@@ -9,6 +9,7 @@
                    
 
 Export-Package: org.apache.felix.scr.component;version=1.1.0;provide:=true, \
+ org.apache.felix.scr.info;version=1.0.0;provide:=true, \
  org.osgi.service.component;version=1.3;-split-package:=first;provide:=true, \
  org.osgi.service.component.runtime;version=1.3;provide:=true, \
  org.osgi.service.component.runtime.dto;version=1.3;provide:=true, \
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
index 72f0ff1..d134db9 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
@@ -165,6 +165,7 @@
         super.doStart();
 
         m_scrCommand = ScrCommand.register(m_context, runtime, m_configuration);
+        m_configuration.setScrCommand( m_scrCommand );
     }
 
     @Override
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java b/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java
index 9fb7fcb..4559ef9 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java
@@ -33,6 +33,7 @@
 import java.util.TreeMap;
 import java.util.regex.Pattern;
 
+import org.apache.felix.scr.info.ScrInfo;
 import org.apache.felix.scr.impl.config.ScrConfiguration;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -52,13 +53,14 @@
  * {@link #register(BundleContext, ScrService, ScrConfiguration)} method
  * instantiates and registers the Gogo and Shell commands as possible.
  */
-public class ScrCommand
+public class ScrCommand implements ScrInfo
 {
 
     private final BundleContext bundleContext;
     private final ServiceComponentRuntime scrService;
     private final ScrConfiguration scrConfiguration;
-
+    
+    private ServiceRegistration<ScrInfo> reg;
     private ServiceRegistration<?> gogoReg;
     private ServiceRegistration<?> shellReg;
 
@@ -145,7 +147,29 @@
 
     // ---------- Actual implementation
 
-
+    
+    public void update( boolean infoAsService )
+    {
+        if (infoAsService)
+        {
+            if ( reg == null )
+            {
+                final Hashtable<String, Object> props = new Hashtable<String, Object>();
+                props.put(Constants.SERVICE_DESCRIPTION, "SCR Info service");
+                props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+                reg = bundleContext.registerService( ScrInfo.class, this, props );
+            }
+        }
+        else
+        {
+            if ( reg != null )
+            {
+                reg.unregister();
+                reg = null;
+            }
+        }
+    }
+    
     /* (non-Javadoc)
      * @see org.apache.felix.scr.impl.ScrInfo#list(java.lang.String, java.io.PrintStream, java.io.PrintStream)
      */
@@ -505,6 +529,8 @@
         out.println(scrConfiguration.stopTimeout());
         out.print("Global extender: ");
         out.println(scrConfiguration.globalExtender());
+        out.print("Info Service registered: ");
+        out.println(scrConfiguration.infoAsService() ? "Supported" : "Unsupported");
     }
 
     private String toStateString(int state)
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfiguration.java b/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfiguration.java
index 5f6d3c0..3f61a87 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfiguration.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfiguration.java
@@ -23,6 +23,7 @@
 import java.util.Hashtable;
 
 import org.apache.felix.scr.impl.Activator;
+import org.apache.felix.scr.impl.ScrCommand;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
@@ -63,6 +64,8 @@
 
     public static final String PROP_DELAYED_KEEP_INSTANCES = "ds.delayed.keepInstances";
 
+    public static final String PROP_INFO_SERVICE = "ds.info.service";
+
     public static final String PROP_LOCK_TIMEOUT = "ds.lock.timeout.milliseconds";
 
     public static final String PROP_STOP_TIMEOUT = "ds.stop.timeout.milliseconds";
@@ -94,7 +97,9 @@
     private boolean factoryEnabled;
 
     private boolean keepInstances;
-
+    
+    private boolean infoAsService;
+    
     private long lockTimeout = DEFAULT_LOCK_TIMEOUT_MILLISECONDS;
 
     private long stopTimeout = DEFAULT_STOP_TIMEOUT_MILLISECONDS;
@@ -104,6 +109,8 @@
     private BundleContext bundleContext;
 
     private ServiceRegistration<ManagedService> managedService;
+    
+    private ScrCommand scrCommand;
 
     public ScrConfiguration( Activator activator )
     {
@@ -140,6 +147,12 @@
 
         this.bundleContext = null;
     }
+    
+    public void setScrCommand(ScrCommand scrCommand)
+    {
+        this.scrCommand = scrCommand;
+        scrCommand.update(infoAsService());
+    }
 
     // Called from the ScrManagedService.updated method to reconfigure
     void configure( Dictionary<String, ?> config, boolean fromConfig )
@@ -157,6 +170,7 @@
                         logLevel = LogService.LOG_ERROR;
                         factoryEnabled = false;
                         keepInstances = false;
+                        infoAsService = false;
                         lockTimeout = DEFAULT_LOCK_TIMEOUT_MILLISECONDS;
                         stopTimeout = DEFAULT_STOP_TIMEOUT_MILLISECONDS;
                         newGlobalExtender = false;
@@ -166,6 +180,7 @@
                         logLevel = getDefaultLogLevel();
                         factoryEnabled = getDefaultFactoryEnabled();
                         keepInstances = getDefaultKeepInstances();
+                        infoAsService = getDefaultInfoAsService();
                         lockTimeout = getDefaultLockTimeout();
                         stopTimeout = getDefaultStopTimeout();
                         newGlobalExtender = getDefaultGlobalExtender();
@@ -181,12 +196,17 @@
                 logLevel = getLogLevel( config.get( PROP_LOGLEVEL ) );
                 factoryEnabled = VALUE_TRUE.equalsIgnoreCase( String.valueOf( config.get( PROP_FACTORY_ENABLED ) ) );
                 keepInstances = VALUE_TRUE.equalsIgnoreCase( String.valueOf( config.get( PROP_DELAYED_KEEP_INSTANCES ) ) );
+                infoAsService = VALUE_TRUE.equalsIgnoreCase( String.valueOf( config.get( PROP_INFO_SERVICE) ) );
                 Long timeout = ( Long ) config.get( PROP_LOCK_TIMEOUT );
                 lockTimeout = timeout == null? DEFAULT_LOCK_TIMEOUT_MILLISECONDS: timeout;
                 timeout = ( Long ) config.get( PROP_STOP_TIMEOUT );
                 stopTimeout = timeout == null? DEFAULT_STOP_TIMEOUT_MILLISECONDS: timeout;
                 newGlobalExtender = VALUE_TRUE.equalsIgnoreCase( String.valueOf( config.get( PROP_GLOBAL_EXTENDER) ) );
             }
+            if ( scrCommand != null )
+            {
+                scrCommand.update( infoAsService() );
+            }
             oldGlobalExtender = this.globalExtender;
             this.globalExtender = newGlobalExtender;
         }
@@ -216,6 +236,11 @@
     {
         return keepInstances;
     }
+    
+    public boolean infoAsService()
+    {
+        return infoAsService;
+    }
 
     public long lockTimeout()
     {
@@ -248,6 +273,11 @@
     {
         return getLogLevel( bundleContext.getProperty( PROP_LOGLEVEL ) );
     }
+    
+    private boolean getDefaultInfoAsService()
+    {
+        return VALUE_TRUE.equalsIgnoreCase( bundleContext.getProperty( PROP_INFO_SERVICE) );
+    }
 
     private long getDefaultLockTimeout()
     {
diff --git a/scr/src/main/java/org/apache/felix/scr/info/ScrInfo.java b/scr/src/main/java/org/apache/felix/scr/info/ScrInfo.java
new file mode 100644
index 0000000..97c5730
--- /dev/null
+++ b/scr/src/main/java/org/apache/felix/scr/info/ScrInfo.java
@@ -0,0 +1,53 @@
+/*
+ * 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.felix.scr.info;
+
+import java.io.PrintWriter;
+
+/**
+ * Abstraction of command interface.
+ *
+ */
+public interface ScrInfo
+{
+
+    /**
+     * List in text the components for the bundle specified, or all components if null, sorted by component ID
+     * @param bundleIdentifier bundle the components are in or null for all components
+     * @param out PrintStream for normal output
+     * @throws IllegalArgumentException if nothing can be found
+     */
+    void list(String bundleIdentifier, PrintWriter out);
+
+    /**
+     * List in text detailed information about the specified components.  Components can be specified by
+     * numeric componentId, component name, a regexp to match for component name, or null for all components.
+     * @param componentId specifier for desired components
+     * @param out PrintStream for normal output
+     * @throws IllegalArgumentException if nothing can be found
+     */
+    void info(String componentId, PrintWriter out);
+
+    /**
+     * List in text the current SCR configuration
+     * @param out PrintStream for output.
+     */
+    void config(PrintWriter out);
+
+}
\ No newline at end of file