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:16:53 UTC
[sling-org-apache-sling-settings] 07/09: SLING-2976 : Add support
for instance name and description
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.settings-1.3.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-settings.git
commit aeb6067fe68ed8eea663a29ad4658f8a2a7c93fb
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Jul 24 11:19:16 2013 +0000
SLING-2976 : Add support for instance name and description
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/settings@1506498 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 7 +-
.../sling/settings/SlingSettingsService.java | 14 ---
.../sling/settings/impl/ServicesListener.java | 32 +++++-
.../settings/impl/SettingsServiceConfigurator.java | 38 +++++++
.../settings/impl/SlingSettingsServiceImpl.java | 114 ++++++---------------
.../OSGI-INF/metatype/metatype.properties | 29 ++++++
...ling.settings.impl.SlingSettingsServiceImpl.xml | 10 ++
7 files changed, 144 insertions(+), 100 deletions(-)
diff --git a/pom.xml b/pom.xml
index 36ed3d0..10a8d10 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>
diff --git a/src/main/java/org/apache/sling/settings/SlingSettingsService.java b/src/main/java/org/apache/sling/settings/SlingSettingsService.java
index e9e1b08..563f7b5 100644
--- a/src/main/java/org/apache/sling/settings/SlingSettingsService.java
+++ b/src/main/java/org/apache/sling/settings/SlingSettingsService.java
@@ -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);
}
diff --git a/src/main/java/org/apache/sling/settings/impl/ServicesListener.java b/src/main/java/org/apache/sling/settings/impl/ServicesListener.java
index 77b8045..0494219 100644
--- a/src/main/java/org/apache/sling/settings/impl/ServicesListener.java
+++ b/src/main/java/org/apache/sling/settings/impl/ServicesListener.java
@@ -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();
diff --git a/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java b/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java
new file mode 100644
index 0000000..74228ae
--- /dev/null
+++ b/src/main/java/org/apache/sling/settings/impl/SettingsServiceConfigurator.java
@@ -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);
+ }
+}
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 c2c9be0..5fadb1e 100644
--- a/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
+++ b/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
@@ -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());
}
}
-
}
}
}
diff --git a/src/main/resources/OSGI-INF/metatype/metatype.properties b/src/main/resources/OSGI-INF/metatype/metatype.properties
new file mode 100644
index 0000000..8b9371f
--- /dev/null
+++ b/src/main/resources/OSGI-INF/metatype/metatype.properties
@@ -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
diff --git a/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml b/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml
new file mode 100644
index 0000000..f88fb82
--- /dev/null
+++ b/src/main/resources/OSGI-INF/metatype/org.apache.sling.settings.impl.SlingSettingsServiceImpl.xml
@@ -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
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.