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 10:15:53 UTC

[sling-org-apache-sling-settings] 02/11: SLING-1651 - Integrate RunMode module into new Settings Module SLING-983 - Add sling.properties file to configuration status page

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.settings-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-settings.git

commit 2f625ec81ff7a4cbf24a4d2813517f70e42063a8
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Aug 13 11:30:39 2010 +0000

    SLING-1651 - Integrate RunMode module into new Settings Module
    SLING-983 - Add sling.properties file to configuration status page
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/settings@985159 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |   4 +-
 .../sling/settings/SlingSettingsService.java       |  22 ++++
 .../org/apache/sling/settings/impl/Activator.java  |  32 ++++-
 .../apache/sling/settings/impl/RunModeCommand.java | 101 +++++++++++++++
 .../settings/impl/SlingPropertiesPrinter.java      | 144 +++++++++++++++++++++
 .../sling/settings/impl/SlingSettingsPrinter.java  |  96 ++++++++++++++
 .../settings/impl/SlingSettingsServiceImpl.java    |  61 ++++++++-
 7 files changed, 454 insertions(+), 6 deletions(-)

diff --git a/pom.xml b/pom.xml
index 91b28c7..6be3d20 100644
--- a/pom.xml
+++ b/pom.xml
@@ -89,11 +89,13 @@
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.shell</artifactId>
             <version>1.0.0</version>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.webconsole</artifactId>
-            <version>1.2.0</version>
+            <version>3.0.0</version>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
diff --git a/src/main/java/org/apache/sling/settings/SlingSettingsService.java b/src/main/java/org/apache/sling/settings/SlingSettingsService.java
index 97c8fcc..f8b6904 100644
--- a/src/main/java/org/apache/sling/settings/SlingSettingsService.java
+++ b/src/main/java/org/apache/sling/settings/SlingSettingsService.java
@@ -19,10 +19,16 @@
 package org.apache.sling.settings;
 
 import java.net.URL;
+import java.util.Set;
 
 /**
  * The <code>SlingSettingsService</code> provides basic Sling settings.
+ * - Sling home : If the Sling launchpad is used
+ * - Sling Id : A unique id of the installation
  *
+ * Run Mode Support
+ * A run mode is simply a string like "author", "test", "development",...
+ * The server can have a set of active run modes.
  */
 public interface SlingSettingsService {
 
@@ -55,6 +61,14 @@ public interface SlingSettingsService {
     String SLING_HOME_URL = "sling.home.url";
 
     /**
+     * The name of the framework property defining the set of used
+     * run modes.
+     * The value is a comma separated list of run modes.
+     */
+    String RUN_MODES_PROPERTY = "sling.run.modes";
+
+
+    /**
      * The identifier of the running Sling instance.
      */
     String getSlingId();
@@ -70,4 +84,12 @@ public interface SlingSettingsService {
      * property.
      */
     URL getSlingHome();
+
+    /**
+     * Return the set of activate run modes.
+     * This set might be empty.
+     * @return A non modifiable set of run modes.
+     */
+    Set<String> getRunModes();
+
 }
diff --git a/src/main/java/org/apache/sling/settings/impl/Activator.java b/src/main/java/org/apache/sling/settings/impl/Activator.java
index cb3b522..8047867 100644
--- a/src/main/java/org/apache/sling/settings/impl/Activator.java
+++ b/src/main/java/org/apache/sling/settings/impl/Activator.java
@@ -41,7 +41,7 @@ public class Activator implements BundleActivator {
      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
      */
     public void start(BundleContext context) throws Exception {
-        final Object service = new SlingSettingsServiceImpl(context);
+        final SlingSettingsService service = new SlingSettingsServiceImpl(context);
         final Dictionary<String, String> props = new Hashtable<String, String>();
         props.put(Constants.SERVICE_PID, service.getClass().getName());
         props.put(Constants.SERVICE_DESCRIPTION,
@@ -50,12 +50,42 @@ public class Activator implements BundleActivator {
         serviceRegistration = context.registerService(new String[] {
                                                SlingSettingsService.class.getName()},
                                            service, props);
+        try {
+            SlingPropertiesPrinter.initPlugin(context);
+        } catch (Throwable ignore) {
+            // we just ignore this
+        }
+        try {
+            SlingSettingsPrinter.initPlugin(context, service);
+        } catch (Throwable ignore) {
+            // we just ignore this
+        }
+        try {
+            RunModeCommand.initPlugin(context, service.getRunModes());
+        } catch (Throwable ignore) {
+            // we just ignore this
+        }
     }
 
     /**
      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
      */
     public void stop(BundleContext context) throws Exception {
+        try {
+            RunModeCommand.destroyPlugin();
+        } catch (Throwable ignore) {
+            // we just ignore this
+        }
+        try {
+            SlingSettingsPrinter.destroyPlugin();
+        } catch (Throwable ignore) {
+            // we just ignore this
+        }
+        try {
+            SlingPropertiesPrinter.destroyPlugin();
+        } catch (Throwable ignore) {
+            // we just ignore this
+        }
         if ( serviceRegistration != null ) {
             serviceRegistration.unregister();
             serviceRegistration = null;
diff --git a/src/main/java/org/apache/sling/settings/impl/RunModeCommand.java b/src/main/java/org/apache/sling/settings/impl/RunModeCommand.java
new file mode 100644
index 0000000..1aa93cf
--- /dev/null
+++ b/src/main/java/org/apache/sling/settings/impl/RunModeCommand.java
@@ -0,0 +1,101 @@
+/*
+ * 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.io.PrintStream;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Set;
+
+import org.apache.felix.shell.Command;
+import org.apache.felix.webconsole.ConfigurationPrinter;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * Run mode command for the shell.
+ */
+public class RunModeCommand implements Command {
+
+    private static ServiceRegistration pluginReg;
+
+    public static void initPlugin(final BundleContext bundleContext,
+            final Set<String> modes) {
+        final RunModeCommand command = new RunModeCommand(modes);
+
+        final Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put(Constants.SERVICE_DESCRIPTION,
+            "Apache Sling Sling Run Mode Shell Command");
+        props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+
+        pluginReg = bundleContext.registerService(ConfigurationPrinter.class.getName(),
+                command,
+                props);
+    }
+
+    public static void destroyPlugin() {
+        if ( pluginReg != null) {
+            pluginReg.unregister();
+            pluginReg = null;
+        }
+    }
+
+    private static final String CMD_NAME = "runmodes";
+
+    /** @scr.reference */
+    private Set<String> modes;
+
+    public RunModeCommand(final Set<String> modes) {
+        this.modes = modes;
+    }
+
+    /**
+     * @see org.apache.felix.shell.Command#getName()
+     */
+    public String getName() {
+        return CMD_NAME;
+    }
+
+    /**
+     * @see org.apache.felix.shell.Command#getShortDescription()
+     */
+    public String getShortDescription() {
+        return "lists current run modes";
+    }
+
+    /**
+     * @see org.apache.felix.shell.Command#getUsage()
+     */
+    public String getUsage() {
+        return CMD_NAME;
+    }
+
+    /**
+     * @see org.apache.felix.shell.Command#execute(java.lang.String, java.io.PrintStream, java.io.PrintStream)
+     */
+    public void execute(String command, PrintStream out, PrintStream err) {
+        out.print("Current Run Modes: ");
+        if (modes == null || modes.size() == 0) {
+            out.println("-");
+        } else {
+            out.println(modes);
+        }
+    }
+}
diff --git a/src/main/java/org/apache/sling/settings/impl/SlingPropertiesPrinter.java b/src/main/java/org/apache/sling/settings/impl/SlingPropertiesPrinter.java
new file mode 100644
index 0000000..fd3fbdc
--- /dev/null
+++ b/src/main/java/org/apache/sling/settings/impl/SlingPropertiesPrinter.java
@@ -0,0 +1,144 @@
+/*
+ * 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.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.apache.felix.webconsole.ConfigurationPrinter;
+import org.apache.felix.webconsole.ModeAwareConfigurationPrinter;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This is a configuration printer for the web console which
+ * prints out the Sling properties from Launchpad if available.
+ *
+ */
+public class SlingPropertiesPrinter implements ModeAwareConfigurationPrinter {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(SlingPropertiesPrinter.class);
+
+    private static ServiceRegistration propertiesPlugin;
+
+    public static void initPlugin(final BundleContext bundleContext) {
+        // if the properties are available, we register the sling properties plugin
+        final String propUrl = bundleContext.getProperty("sling.properties.url");
+        if ( propUrl != null ) {
+            // try to read properties
+            Properties props = null;
+            try {
+                final URL url = new URL(propUrl);
+                final InputStream is = url.openStream();
+                final Properties tmp = new Properties();
+                tmp.load(is);
+                props = tmp;
+            } catch (IOException ioe) {
+                LOGGER.warn("Unable to read sling properties from " + propUrl, ioe);
+            }
+            if ( props != null ) {
+                final SlingPropertiesPrinter propertiesPrinter = new SlingPropertiesPrinter(props);
+                final Dictionary<String, String> serviceProps2 = new Hashtable<String, String>();
+                serviceProps2.put(Constants.SERVICE_DESCRIPTION,
+                    "Apache Sling Sling Properties Configuration Printer");
+                serviceProps2.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+
+                propertiesPlugin = bundleContext.registerService(ConfigurationPrinter.class.getName(),
+                        propertiesPrinter,
+                        serviceProps2);
+            }
+        }
+    }
+
+    public static void destroyPlugin() {
+        if ( propertiesPlugin != null) {
+            propertiesPlugin.unregister();
+            propertiesPlugin = null;
+        }
+    }
+
+    private static String HEADLINE = "Apache Sling Launchpad Properties";
+
+    private final Properties props;
+
+    public SlingPropertiesPrinter(final Properties props) {
+        this.props = props;
+    }
+
+    /**
+     * @see org.apache.felix.webconsole.ConfigurationPrinter#getTitle()
+     */
+    public String getTitle() {
+        return "Sling Properties";
+    }
+
+    /**
+     * Print out the servlet filter chains.
+     * @see org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter)
+     */
+    public void printConfiguration(PrintWriter pw) {
+        pw.println(HEADLINE);
+        pw.println();
+        SortedSet<Object> keys = new TreeSet<Object>( props.keySet() );
+        for ( Iterator<Object> ki = keys.iterator(); ki.hasNext(); ) {
+            final Object key = ki.next();
+            pw.print( key );
+            pw.print(" = ");
+            final Object value = props.get(key);
+            if ( value != null ) {
+                pw.print(value.toString());
+            }
+            pw.println();
+        }
+    }
+
+    /**
+     * @see org.apache.felix.webconsole.ModeAwareConfigurationPrinter#printConfiguration(java.io.PrintWriter, java.lang.String)
+     */
+    public void printConfiguration(PrintWriter printWriter, String mode) {
+        if ( mode != ConfigurationPrinter.MODE_ZIP ) {
+            this.printConfiguration(printWriter);
+        } else {
+            // write into byte array first
+            String contents = null;
+            try {
+                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                this.props.store(baos , HEADLINE);
+                contents = baos.toString("8859_1");
+            } catch (IOException ioe) {
+                // if something goes wrong here we default to text output
+                this.printConfiguration(printWriter);
+                return;
+            }
+            printWriter.write(contents);
+        }
+    }
+}
diff --git a/src/main/java/org/apache/sling/settings/impl/SlingSettingsPrinter.java b/src/main/java/org/apache/sling/settings/impl/SlingSettingsPrinter.java
new file mode 100644
index 0000000..d45b9a4
--- /dev/null
+++ b/src/main/java/org/apache/sling/settings/impl/SlingSettingsPrinter.java
@@ -0,0 +1,96 @@
+/*
+ * 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.io.PrintWriter;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.felix.webconsole.ConfigurationPrinter;
+import org.apache.sling.settings.SlingSettingsService;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * This is a configuration printer for the web console which
+ * prints out the sling settings.
+ *
+ */
+public class SlingSettingsPrinter implements ConfigurationPrinter {
+
+    private static ServiceRegistration pluginReg;
+
+    public static void initPlugin(final BundleContext bundleContext,
+            final SlingSettingsService service) {
+        final SlingSettingsPrinter printer = new SlingSettingsPrinter(service);
+
+        final Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put(Constants.SERVICE_DESCRIPTION,
+            "Apache Sling Sling Settings Configuration Printer");
+        props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+
+        pluginReg = bundleContext.registerService(ConfigurationPrinter.class.getName(),
+                printer,
+                props);
+    }
+
+    public static void destroyPlugin() {
+        if ( pluginReg != null) {
+            pluginReg.unregister();
+            pluginReg = null;
+        }
+    }
+
+    private static String HEADLINE = "Apache Sling Settings";
+
+    private final SlingSettingsService settings;
+
+    public SlingSettingsPrinter(final SlingSettingsService settings) {
+        this.settings = settings;
+    }
+
+    /**
+     * @see org.apache.felix.webconsole.ConfigurationPrinter#getTitle()
+     */
+    public String getTitle() {
+        return "Sling Settings";
+    }
+
+    /**
+     * Print out the servlet filter chains.
+     * @see org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter)
+     */
+    public void printConfiguration(PrintWriter pw) {
+        pw.println(HEADLINE);
+        pw.println();
+        pw.print("Sling ID = ");
+        pw.print(this.settings.getSlingId());
+        pw.println();
+        pw.print("Sling Home = ");
+        pw.print(this.settings.getSlingHomePath());
+        pw.println();
+        pw.print("Sling Home URL = ");
+        pw.print(this.settings.getSlingHome());
+        pw.println();
+        pw.print("Run Modes = ");
+        pw.print(this.settings.getRunModes());
+        pw.println();
+    }
+}
diff --git a/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java b/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
index b8fdf7f..2bcb200 100644
--- a/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
+++ b/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
@@ -24,6 +24,9 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.UUID;
 
 import org.apache.sling.settings.SlingSettingsService;
@@ -45,20 +48,33 @@ public class SlingSettingsServiceImpl
     private String slingId;
 
     /** The sling home */
-    private final String slingHome;
+    private String slingHome;
 
     /** The sling home url */
     private URL slingHomeUrl;
 
+    private Set<String> runModes;
+
+    /** The name of the data file holding the sling id. */
     private static final String DATA_FILE = "sling.id.file";
 
     /**
      * Create the service and search the Sling home urls and
      * get/create a sling id.
+     * Setup run modes
      * @param context The bundle context
      */
     public SlingSettingsServiceImpl(final BundleContext context) {
-        // get sling home and sling home url
+        this.setupSlingHome(context);
+        this.setupSlingId(context);
+        this.setupRunModes(context);
+
+    }
+
+    /**
+     * Get sling home and sling home url
+     */
+    private void setupSlingHome(final BundleContext context) {
         this.slingHome = context.getProperty(SLING_HOME);
         final String url = context.getProperty(SLING_HOME_URL);
         if ( url != null ) {
@@ -68,7 +84,12 @@ public class SlingSettingsServiceImpl
                 logger.error("Sling home url is not a url: {}", url);
             }
         }
+    }
 
+    /**
+     * Get / create sling id
+     */
+    private void setupSlingId(final BundleContext context) {
         // try to read the id from the id file first
         final File idFile = context.getDataFile(DATA_FILE);
         if ( idFile == null ) {
@@ -98,7 +119,30 @@ public class SlingSettingsServiceImpl
         }
     }
 
-    /** Read the id from a file. */
+    /**
+     * Set up run modes.
+     */
+    private void setupRunModes(final BundleContext context) {
+        final String prop = context.getProperty(RUN_MODES_PROPERTY);
+        if (prop == null || prop.trim().length() == 0) {
+            this.runModes = Collections.emptySet();
+        } else {
+            final Set<String> modesSet = new HashSet<String>();
+            final String[] modes = prop.split(",");
+            for(int i=0; i < modes.length; i++) {
+                modesSet.add(modes[i].trim());
+            }
+            // make the set unmodifiable and synced
+            // we propably don't need a synced set as it is read only
+            this.runModes = Collections.synchronizedSet(Collections.unmodifiableSet(modesSet));
+            logger.info("Active run modes {}", this.runModes);
+        }
+    }
+
+
+    /**
+     * Read the id from a file.
+     */
     private String readSlingId(final File idFile) {
         if (idFile.exists() && idFile.length() >= 36) {
             FileInputStream fin = null;
@@ -129,7 +173,9 @@ public class SlingSettingsServiceImpl
         return null;
     }
 
-    /** Write the sling id file. */
+    /**
+     * Write the sling id file.
+     */
     private void writeSlingId(final File idFile, final String id) {
         idFile.delete();
         idFile.getParentFile().mkdirs();
@@ -181,4 +227,11 @@ public class SlingSettingsServiceImpl
     public String getSlingHomePath() {
         return this.slingHome;
     }
+
+    /**
+     * @see org.apache.sling.settings.SlingSettingsService#getRunModes()
+     */
+    public Set<String> getRunModes() {
+        return this.runModes;
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.