You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2013/07/24 13:19:17 UTC

svn commit: r1506498 - in /sling/trunk/bundles/extensions/settings: ./ src/main/java/org/apache/sling/settings/ src/main/java/org/apache/sling/settings/impl/ src/main/resources/ src/main/resources/OSGI-INF/ src/main/resources/OSGI-INF/metatype/

Author: cziegeler
Date: Wed Jul 24 11:19:16 2013
New Revision: 1506498

URL: http://svn.apache.org/r1506498
Log:
SLING-2976 :  Add support for instance name and description 

Added:
    sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java   (with props)
    sling/trunk/bundles/extensions/settings/src/main/resources/
    sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/
    sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/
    sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/metatype.properties   (with props)
    sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml   (with props)
Modified:
    sling/trunk/bundles/extensions/settings/pom.xml
    sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/SlingSettingsService.java
    sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/ServicesListener.java
    sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java

Modified: sling/trunk/bundles/extensions/settings/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/pom.xml?rev=1506498&r1=1506497&r2=1506498&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/settings/pom.xml (original)
+++ sling/trunk/bundles/extensions/settings/pom.xml Wed Jul 24 11:19:16 2013
@@ -70,9 +70,12 @@
                         <Bundle-Activator>
                             org.apache.sling.settings.impl.Activator
                         </Bundle-Activator>
+                        <DynamicImport-Package>
+                            org.osgi.service.cm
+                        </DynamicImport-Package>
                         <Import-Package>
-                            org.apache.felix.shell;
-                            resolution:=optional, *
+                            org.apache.felix.shell;resolution:=optional, 
+                            *
                         </Import-Package>
                     </instructions>
                 </configuration>

Modified: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/SlingSettingsService.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/SlingSettingsService.java?rev=1506498&r1=1506497&r2=1506498&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/SlingSettingsService.java (original)
+++ sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/SlingSettingsService.java Wed Jul 24 11:19:16 2013
@@ -120,26 +120,12 @@ public interface SlingSettingsService {
     /**
      * Return the optional name of the instance.
      * @return The name of the instance or <code>null</code>.
-     * @see #setSlingName(String)
      */
     String getSlingName();
 
     /**
-     * Set the name of the instance.
-     * @param value A new name.
-     */
-    void setSlingName(String value);
-
-    /**
      * Return the optional description of the instance.
      * @return The description of the instance or <code>null</code>.
-     * @see #setSlingDescrption(String)
      */
     String getSlingDescription();
-
-    /**
-     * Set the description of the instance.
-     * @param value A new description.
-     */
-    void setSlingDescrption(String value);
 }

Modified: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/ServicesListener.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/ServicesListener.java?rev=1506498&r1=1506497&r2=1506498&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/ServicesListener.java (original)
+++ sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/ServicesListener.java Wed Jul 24 11:19:16 2013
@@ -23,13 +23,16 @@ import java.util.Hashtable;
 
 import org.apache.sling.launchpad.api.StartupHandler;
 import org.apache.sling.settings.SlingSettingsService;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ManagedService;
 
 /**
  * The <code>ServicesListener</code> listens for the required services
@@ -47,6 +50,9 @@ public class ServicesListener {
     /** The registration of the settings service. */
     private ServiceRegistration settingsReg;
 
+    /** The registration of the managed service. */
+    private ServiceRegistration managedServiceReg;
+
     /**
      * Start listeners
      */
@@ -70,10 +76,9 @@ public class ServicesListener {
     }
 
     private void activate(final StartupHandler handler) {
-        final SlingSettingsService settingsService = new SlingSettingsServiceImpl(bundleContext, handler);
+        final SlingSettingsServiceImpl settingsService = new SlingSettingsServiceImpl(bundleContext, handler);
 
         final Dictionary<String, String> props = new Hashtable<String, String>();
-        props.put(Constants.SERVICE_PID, settingsService.getClass().getName());
         props.put(Constants.SERVICE_DESCRIPTION,
             "Apache Sling Settings Service");
         props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
@@ -84,14 +89,35 @@ public class ServicesListener {
         SlingSettingsPrinter.initPlugin(bundleContext, settingsService);
         try {
             RunModeCommand.initPlugin(bundleContext, settingsService.getRunModes());
-        } catch (Throwable ignore) {
+        } catch (final Throwable ignore) {
             // we just ignore this
         }
+        // setup manager service for configuration handling
+        final Dictionary<String, String> msProps = new Hashtable<String, String>();
+        msProps.put(Constants.SERVICE_PID, settingsService.getClass().getName());
+        msProps.put(Constants.SERVICE_DESCRIPTION,
+            "Apache Sling Managed Service for the Settings Service");
+        msProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        managedServiceReg = this.bundleContext.registerService(ManagedService.class.getName(), new ServiceFactory() {
+
+            public void ungetService(final Bundle bundle, final ServiceRegistration registration,
+                    final Object service) {
+                // nothing to do
+            }
+
+            public Object getService(final Bundle bundle, final ServiceRegistration registration) {
+                return new SettingsServiceConfigurator(settingsService);
+            }
+        }, msProps);
     }
     /**
      * Deactivate this listener.
      */
     public void deactivate() {
+        if ( this.managedServiceReg != null ) {
+            this.managedServiceReg.unregister();
+            this.managedServiceReg = null;
+        }
         this.startupListener.deactivate();
         if ( this.settingsReg != null ) {
             this.settingsReg.unregister();

Added: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java?rev=1506498&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java (added)
+++ sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java Wed Jul 24 11:19:16 2013
@@ -0,0 +1,38 @@
+/*
+ * 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.settings.impl;
+
+import java.util.Dictionary;
+
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+
+public class SettingsServiceConfigurator implements ManagedService {
+
+    private final SlingSettingsServiceImpl settings;
+
+    public SettingsServiceConfigurator(final SlingSettingsServiceImpl s) {
+        this.settings = s;
+    }
+
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public void updated(final Dictionary properties) throws ConfigurationException {
+        this.settings.update(properties);
+    }
+}

Propchange: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Modified: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java?rev=1506498&r1=1506497&r2=1506498&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java (original)
+++ sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java Wed Jul 24 11:19:16 2013
@@ -22,18 +22,18 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.io.OutputStream;
 import java.io.Serializable;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Dictionary;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Properties;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
@@ -65,24 +65,20 @@ public class SlingSettingsServiceImpl
     /** The sling home */
     private String slingHome;
 
-    /** The sling properties */
-    private Properties slingProps;
-
     /** The sling home url */
     private URL slingHomeUrl;
 
+    /** The set of run modes .*/
     private Set<String> runModes;
 
-    /** The name of the data file holding additional properties. */
-    private static final String PROPS_FILE = "sling.props.file";
-
     /** The name of the data file holding the sling id. */
     private static final String ID_FILE = "sling.id.file";
 
     /** The name of the data file holding install run mode options */
     private static final String OPTIONS_FILE = "sling.options.file";
 
-    private File slingPropsFile;
+    /** The properties for name, description. */
+    private final Map<String, String> slingProps = new HashMap<String, String>();
 
     /**
      * Create the service and search the Sling home urls and
@@ -141,34 +137,13 @@ public class SlingSettingsServiceImpl
      * Get / create sling id
      */
     private void setupSlingProps(final BundleContext context) {
-        // try to read the props from the file
-        this.slingPropsFile = context.getDataFile(PROPS_FILE);
-        if ( this.slingPropsFile == null ) {
-            // the OSGi framework does not support storing something in the file system
-            throw new RuntimeException("Unable to read from bundle data file.");
-        }
-        this.slingProps = new Properties();
-        if ( this.slingPropsFile.exists() ) {
-            InputStream reader = null;
-            try {
-                reader = new FileInputStream(this.slingPropsFile);
-                this.slingProps.load(reader);
-
-            } catch ( final IOException ioe ) {
-                logger.error("Unable to read properties file " + this.slingPropsFile + " : " + ioe.getMessage(), ioe);
-            } finally {
-                if ( reader != null ) {
-                    try {
-                        reader.close();
-                    } catch (final IOException ignore) {}
-                }
+        synchronized ( this.slingProps ) {
+            if ( this.slingProps.get(SLING_NAME) == null && context.getProperty(SLING_NAME) != null ) {
+                this.slingProps.put(SLING_NAME, context.getProperty(SLING_NAME));
+            }
+            if ( this.slingProps.get(SLING_DESCRIPTION) == null && context.getProperty(SLING_DESCRIPTION) != null ) {
+                this.slingProps.put(SLING_DESCRIPTION, context.getProperty(SLING_DESCRIPTION));
             }
-        }
-        if ( this.slingProps.getProperty(SLING_NAME) == null && context.getProperty(SLING_NAME) != null ) {
-            this.slingProps.setProperty(SLING_NAME, context.getProperty(SLING_NAME));
-        }
-        if ( this.slingProps.getProperty(SLING_DESCRIPTION) == null && context.getProperty(SLING_DESCRIPTION) != null ) {
-            this.slingProps.setProperty(SLING_DESCRIPTION, context.getProperty(SLING_DESCRIPTION));
         }
     }
 
@@ -403,64 +378,41 @@ public class SlingSettingsServiceImpl
      * @see org.apache.sling.settings.SlingSettingsService#getSlingName()
      */
     public String getSlingName() {
-        String name = this.slingProps.getProperty(SLING_NAME);
-        if ( name == null ) {
-            name = "Instance " + this.slingId; // default
-        }
-        return name;
-    }
-
-    /**
-     * @see org.apache.sling.settings.SlingSettingsService#setSlingName(java.lang.String)
-     */
-    public void setSlingName(final String value) {
-        if ( value == null ) {
-            this.slingProps.setProperty(SLING_NAME, "");
-        } else {
-            this.slingProps.setProperty(SLING_NAME, value);
+        synchronized ( this.slingProps ) {
+            String name = this.slingProps.get(SLING_NAME);
+            if ( name == null ) {
+                name = "Instance " + this.slingId; // default
+            }
+            return name;
         }
-        this.writeSlingProps();
     }
 
     /**
      * @see org.apache.sling.settings.SlingSettingsService#getSlingDescription()
      */
     public String getSlingDescription() {
-        String desc = this.slingProps.getProperty(SLING_DESCRIPTION);
-        if ( desc == null ) {
-            desc = "Instance with id " + this.slingId + " and run modes " + this.getRunModes(); // default
+        synchronized ( this.slingProps ) {
+            String desc = this.slingProps.get(SLING_DESCRIPTION);
+            if ( desc == null ) {
+                desc = "Instance with id " + this.slingId + " and run modes " + this.getRunModes(); // default
+            }
+            return desc;
         }
-        return desc;
     }
 
     /**
-     * @see org.apache.sling.settings.SlingSettingsService#setSlingDescrption(java.lang.String)
+     * Update the configuration of this service
      */
-    public void setSlingDescrption(final String value) {
-        if ( value == null ) {
-            this.slingProps.setProperty(SLING_DESCRIPTION, "");
-        } else {
-            this.slingProps.setProperty(SLING_DESCRIPTION, value);
-        }
-        this.writeSlingProps();
-    }
-
-    private void writeSlingProps() {
-        if ( this.slingPropsFile != null ) {
-            OutputStream writer = null;
-            try {
-                writer = new FileOutputStream(this.slingPropsFile);
-                this.slingProps.store(writer, null);
-            } catch ( final IOException ioe ) {
-                logger.error("Unable to write properties file " + this.slingPropsFile + " : " + ioe.getMessage(), ioe);
-            } finally {
-                if ( writer != null ) {
-                    try {
-                        writer.close();
-                    } catch (final IOException ignore) {}
+    public void update(final Dictionary<String, Object> properties) {
+        if ( properties != null ) {
+            synchronized ( this.slingProps ) {
+                if ( properties.get(SLING_NAME) != null ) {
+                    this.slingProps.put(SLING_NAME, properties.get(SLING_NAME).toString());
+                }
+                if ( properties.get(SLING_DESCRIPTION) != null ) {
+                    this.slingProps.put(SLING_DESCRIPTION, properties.get(SLING_DESCRIPTION).toString());
                 }
             }
-
         }
     }
 }

Added: sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1506498&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/metatype.properties (added)
+++ sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/metatype.properties Wed Jul 24 11:19:16 2013
@@ -0,0 +1,29 @@
+#
+#  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.
+#
+
+
+settings.name = Apache Sling Settings Service
+settings.description = The settings service manages some basic settings of Sling \
+ like run modes or information about the current instance.
+
+sling.name.name = Instance Name
+sling.name.description = A human readable name for the current instance.
+
+sling.description.name = Instance Description
+sling.description.description = A human readable description for the current instance.
\ No newline at end of file

Propchange: sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
    svn:keywords = Id

Added: sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml?rev=1506498&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml (added)
+++ sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml Wed Jul 24 11:19:16 2013
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?><metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0" 
+localization="OSGI-INF/metatype/metatype">
+    <OCD id="org.apache.sling.settings.impl.SlingSettingsServiceImpl" name="%settings.name" description="%settings.description">
+        <AD id="sling.name" type="String" name="%sling.name.name" description="%sling.name.description"/>
+        <AD id="sling.description" type="String" name="%sling.description.name" description="%sling.description.description"/>
+    </OCD>
+    <Designate pid="org.apache.sling.settings.impl.SlingSettingsServiceImpl">
+        <Object ocdref="org.apache.sling.settings.impl.SlingSettingsServiceImpl"/>
+    </Designate>
+</metatype:MetaData>
\ No newline at end of file

Propchange: sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/settings/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml
------------------------------------------------------------------------------
    svn:keywords = Id