Author: cziegeler
Date: Fri May 16 03:17:37 2008
New Revision: 657002

URL: http://svn.apache.org/viewvc?rev=657002&view=rev
Log:
#SLING-414: Move generation and managing of the sling id to own service in the 
engine.

Added:
    
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/SlingSettingsService.java
   (with props)
    
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/SlingSettingsServiceImpl.java
   (with props)
Modified:
    incubator/sling/trunk/engine/pom.xml
    
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
    incubator/sling/trunk/extensions/event/pom.xml
    
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
    
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
    incubator/sling/trunk/jcr/contentloader/pom.xml
    
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java
    
incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launcher/app/Sling.java

Modified: incubator/sling/trunk/engine/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/engine/pom.xml?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- incubator/sling/trunk/engine/pom.xml (original)
+++ incubator/sling/trunk/engine/pom.xml Fri May 16 03:17:37 2008
@@ -73,6 +73,9 @@
                             javax.portlet;resolution:=optional,
                             *
                         </Import-Package>
+                        <Bundle-Activator>
+                            
org.apache.sling.engine.impl.SlingSettingsServiceImpl
+                        </Bundle-Activator>
                         <Embed-Dependency>
                             commons-fileupload
                         </Embed-Dependency>

Modified: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/EngineConstants.java?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
 (original)
+++ 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
 Fri May 16 03:17:37 2008
@@ -55,17 +55,6 @@
     public static final String SLING_HOME_URL = "sling.home.url";
 
     /**
-     * The name of the framework property containing the identifier of the
-     * running Sling instance (value is "sling.id"). This value of this 
property
-     * is managed by this class and cannot be overwritten by the configuration
-     * file(s).
-     * <p>
-     * This property is available calling the
-     * <code>BundleContext.getProperty(String)</code> method.
-     */
-    public static final String SLING_ID = "sling.id";
-
-    /**
      * The name of the request attribute providing the name of the currently
      * executing servlet (value is "sling.core.current.servletName"). This
      * attribute is set immediately before calling the

Added: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/SlingSettingsService.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/SlingSettingsService.java?rev=657002&view=auto
==============================================================================
--- 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/SlingSettingsService.java
 (added)
+++ 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/SlingSettingsService.java
 Fri May 16 03:17:37 2008
@@ -0,0 +1,30 @@
+/*
+ * 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.engine;
+
+/**
+ * The <code>SlingSettingsService</code> provides basic Sling settings.
+ */
+public interface SlingSettingsService {
+
+    /**
+     * The identifier of the running Sling instance.
+     */
+    String getSlingId();
+}

Propchange: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/SlingSettingsService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/SlingSettingsService.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/SlingSettingsServiceImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/SlingSettingsServiceImpl.java?rev=657002&view=auto
==============================================================================
--- 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/SlingSettingsServiceImpl.java
 (added)
+++ 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/SlingSettingsServiceImpl.java
 Fri May 16 03:17:37 2008
@@ -0,0 +1,123 @@
+/*
+ * 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.engine.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.UUID;
+
+import org.apache.sling.engine.SlingSettingsService;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This is the basic implementation of the sling settings service.
+ */
+public class SlingSettingsServiceImpl implements SlingSettingsService, 
BundleActivator {
+
+    /** The logger */
+    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    /** The sling instance id. */
+    private String slingId;
+
+    /** The service registration */
+    private ServiceRegistration serviceRegistration;
+
+    /**
+     * @see org.apache.sling.engine.SlingSettingsService#getSlingId()
+     */
+    public String getSlingId() {
+        return this.slingId;
+    }
+
+    /**
+     * Read/Create the sling instance id.
+     * @see 
org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+     */
+    public void start(BundleContext context) throws Exception {
+        // try to read the id from the id file first
+        File idFile = context.getDataFile("sling.id.file");
+        if ( idFile == null ) {
+            // the osgi framework does not support storing something in the 
file system
+            throw new RuntimeException("Unable to read from bundle data 
file.");
+        }
+        if (idFile.exists() && idFile.length() >= 36) {
+            FileInputStream fin = null;
+            try {
+                fin = new FileInputStream(idFile);
+                byte[] rawBytes = new byte[36];
+                if (fin.read(rawBytes) == 36) {
+                    String rawString = new String(rawBytes, "ISO-8859-1");
+
+                    // roundtrip to ensure correct format of UUID value
+                    slingId = UUID.fromString(rawString).toString();
+                }
+            } catch (Throwable t) {
+                logger.error("Failed reading UUID from id file " + idFile
+                        + ", creating new id", t);
+            } finally {
+                if (fin != null) {
+                    try {
+                        fin.close();
+                    } catch (IOException ignore) {
+                    }
+                }
+            }
+        }
+
+        // no sling id yet or failure to read file: create an id and store
+        if (slingId == null) {
+            slingId = UUID.randomUUID().toString();
+
+            idFile.delete();
+            idFile.getParentFile().mkdirs();
+            FileOutputStream fout = null;
+            try {
+                fout = new FileOutputStream(idFile);
+                fout.write(slingId.getBytes("ISO-8859-1"));
+                fout.flush();
+            } catch (Throwable t) {
+                logger.error("Failed writing UUID to id file " + idFile, t);
+            } finally {
+                if (fout != null) {
+                    try {
+                        fout.close();
+                    } catch (IOException ignore) {
+                    }
+                }
+            }
+        }
+        serviceRegistration = 
context.registerService(SlingSettingsService.class.getName(), this, null);
+    }
+
+    /**
+     * @see 
org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+     */
+    public void stop(BundleContext context) throws Exception {
+        if ( serviceRegistration != null ) {
+            serviceRegistration.unregister();
+            serviceRegistration = null;
+        }
+    }
+}

Propchange: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/SlingSettingsServiceImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/SlingSettingsServiceImpl.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Modified: incubator/sling/trunk/extensions/event/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/pom.xml?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/pom.xml (original)
+++ incubator/sling/trunk/extensions/event/pom.xml Fri May 16 03:17:37 2008
@@ -99,6 +99,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.engine</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.jcr.api</artifactId>
             <version>2.0.0-incubator-SNAPSHOT</version>
         </dependency>

Modified: 
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- 
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
 (original)
+++ 
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
 Fri May 16 03:17:37 2008
@@ -38,6 +38,7 @@
 import org.apache.sling.commons.threads.ThreadPool;
 import org.apache.sling.commons.threads.ThreadPoolConfig;
 import org.apache.sling.commons.threads.ThreadPoolManager;
+import org.apache.sling.engine.SlingSettingsService;
 import org.apache.sling.event.EventUtil;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.osgi.service.component.ComponentContext;
@@ -56,12 +57,6 @@
 public abstract class AbstractRepositoryEventHandler
     implements EventHandler, EventListener {
 
-    /** FIXME - This is a copy from the sling core constants to avoid
-     * a dependency just for the constant. We will move this into an
-     * OSGi helper bundle
-     */
-    public static final String SLING_ID = "sling.id";
-
     /** Default log. */
     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -98,6 +93,10 @@
     /** Our thread pool. */
     protected ThreadPool threadPool;
 
+    /** @scr.reference
+     *  Sling settings service. */
+    protected SlingSettingsService settingsService;
+
     /**
      * Activate this component.
      * @param context
@@ -105,7 +104,7 @@
      */
     protected void activate(final ComponentContext context)
     throws Exception {
-        this.applicationId = context.getBundleContext().getProperty(SLING_ID);
+        this.applicationId = this.settingsService.getSlingId();
         this.repositoryPath = 
(String)context.getProperties().get(CONFIG_PROPERTY_REPO_PATH);
 
         // start background threads

Modified: 
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- 
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
 (original)
+++ 
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
 Fri May 16 03:17:37 2008
@@ -35,6 +35,7 @@
 import org.apache.sling.commons.threads.ThreadPool;
 import org.apache.sling.commons.threads.ThreadPoolConfig;
 import org.apache.sling.commons.threads.ThreadPoolManager;
+import org.apache.sling.engine.SlingSettingsService;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
@@ -86,6 +87,13 @@
             allowing(eventAdmin).sendEvent(with(any(Event.class)));
         }});
 
+        // sling settings service
+        this.handler.settingsService = new SlingSettingsService() {
+            public String getSlingId() {
+                return SLING_ID;
+            }
+        };
+
         // we need a thread pool manager
         this.handler.threadPoolManager = 
this.getMockery().mock(ThreadPoolManager.class);
         final ThreadPool pool = new ThreadPoolImpl();
@@ -96,12 +104,8 @@
             will(returnValue(null));
         }});
 
-        // lets set up the bundle context with the sling id
+        // lets set up the bundle context
         final BundleContext bundleContext = 
this.getMockery().mock(BundleContext.class);
-        this.getMockery().checking(new Expectations() {{
-            
allowing(bundleContext).getProperty(AbstractRepositoryEventHandler.SLING_ID);
-            will(returnValue(SLING_ID));
-        }});
 
         // lets set up the component configuration
         final Dictionary<String, Object> componentConfig = 
this.getComponentConfig();

Modified: incubator/sling/trunk/jcr/contentloader/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/pom.xml?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/pom.xml (original)
+++ incubator/sling/trunk/jcr/contentloader/pom.xml Fri May 16 03:17:37 2008
@@ -103,6 +103,11 @@
             <version>2.0.0-incubator-SNAPSHOT</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.engine</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>

Modified: 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java
 (original)
+++ 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java
 Fri May 16 03:17:37 2008
@@ -31,6 +31,7 @@
 import javax.jcr.lock.LockException;
 
 import org.apache.sling.commons.mime.MimeTypeService;
+import org.apache.sling.engine.SlingSettingsService;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleEvent;
@@ -97,6 +98,10 @@
      */
     private final Set<String> updatedBundles = new HashSet<String>();
 
+    /** @scr.reference
+     *  Sling settings service. */
+    protected SlingSettingsService settingsService;
+
     // ---------- BundleListener 
-----------------------------------------------
 
     /**
@@ -188,7 +193,7 @@
 
     /** Activates this component, called by SCR before registering as a 
service */
     protected void activate(ComponentContext componentContext) {
-        this.slingId = 
componentContext.getBundleContext().getProperty("sling.id");
+        this.slingId = this.settingsService.getSlingId();
         this.initialContentLoader = new Loader(this);
 
         componentContext.getBundleContext().addBundleListener(this);

Modified: 
incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launcher/app/Sling.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launcher/app/Sling.java?rev=657002&r1=657001&r2=657002&view=diff
==============================================================================
--- 
incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launcher/app/Sling.java
 (original)
+++ 
incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launcher/app/Sling.java
 Fri May 16 03:17:37 2008
@@ -36,7 +36,6 @@
 import java.util.StringTokenizer;
 import java.util.TreeMap;
 import java.util.TreeSet;
-import java.util.UUID;
 import java.util.Map.Entry;
 
 import org.apache.felix.framework.Felix;
@@ -112,20 +111,6 @@
     public static final String SLING_HOME_URL = "sling.home.url";
 
     /**
-     * The name of the framework property containing the identifier of the
-     * running Sling instance (value is "sling.id"). This value of this 
property
-     * is managed by this class and cannot be overwritten by the configuration
-     * file(s).
-     */
-    public static final String SLING_ID = "sling.id";
-
-    /**
-     * The name of the file used to persist the value of the
-     * [EMAIL PROTECTED] #SLING_ID Sling instance identifier} (value is 
"sling.id").
-     */
-    public static final String SLING_ID_FILE = SLING_ID;
-
-    /**
      * The name of the configuration property defining a properties file
      * defining a list of bundles, which are installed into the framework when
      * it has been launched (value is "org.apache.osgi.bundles").
@@ -202,9 +187,6 @@
         // ensure execution environment
         this.setExecutionEnvironment(props);
 
-        // ensure the instance ID
-        props.put(SLING_ID, getInstanceId(props));
-
         // make sure Felix does not exit the VM when terminating ...
         props.put(EMBEDDED_EXECUTION_PROP, "true");
 
@@ -221,8 +203,7 @@
         this.felix = tmpFelix;
 
         // log sucess message
-        this.logger.log(Logger.LOG_INFO, "Sling Instance "
-            + props.get(SLING_ID) + " started");
+        this.logger.log(Logger.LOG_INFO, "Sling started");
     }
 
     /**
@@ -232,11 +213,9 @@
     public final void destroy() {
         // shutdown the Felix container
         if (felix != null) {
-            String label = "Sling Instance "
-                + felix.getBundleContext().getProperty(SLING_ID);
-            logger.log(Logger.LOG_INFO, "Shutting down " + label);
+            logger.log(Logger.LOG_INFO, "Shutting down Sling");
             felix.stopAndWait();
-            logger.log(Logger.LOG_INFO, label + " stopped");
+            logger.log(Logger.LOG_INFO, "Sling stopped");
             felix = null;
         }
     }
@@ -595,71 +574,6 @@
         }
     }
 
-    /**
-     * Returns the Sling Instance Identifier. The value is read from the
-     * <code>${sling.home}/sling.id</code> file if existing and valid.
-     * Otherwise a new id is created as a random UUID and then stored in the
-     * file.
-     *
-     * @return the Sling Instance Identifier
-     */
-    private String getInstanceId(Map<String, String> props) {
-        String slingId = null;
-
-        // try to read the id from the id file first
-        File idFile = new File(props.get(SLING_HOME), SLING_ID_FILE);
-        if (idFile.exists() && idFile.length() >= 36) {
-            FileInputStream fin = null;
-            try {
-                fin = new FileInputStream(idFile);
-                byte[] rawBytes = new byte[36];
-                if (fin.read(rawBytes) == 36) {
-                    String rawString = new String(rawBytes, "ISO-8859-1");
-
-                    // roundtrip to ensure correct format of UUID value
-                    slingId = UUID.fromString(rawString).toString();
-                }
-            } catch (Throwable t) {
-                logger.log(Logger.LOG_ERROR,
-                    "Failed reading UUID from id file " + idFile
-                        + ", creating new id", t);
-            } finally {
-                if (fin != null) {
-                    try {
-                        fin.close();
-                    } catch (IOException ignore) {
-                    }
-                }
-            }
-        }
-
-        // no sling id yet or failure to read file: create an id and store
-        if (slingId == null) {
-            slingId = UUID.randomUUID().toString();
-
-            idFile.delete();
-            idFile.getParentFile().mkdirs();
-            FileOutputStream fout = null;
-            try {
-                fout = new FileOutputStream(idFile);
-                fout.write(slingId.getBytes("ISO-8859-1"));
-                fout.flush();
-            } catch (Throwable t) {
-                logger.log(Logger.LOG_ERROR, "Failed writing UUID to id file "
-                    + idFile, t);
-            } finally {
-                if (fout != null) {
-                    try {
-                        fout.close();
-                    } catch (IOException ignore) {
-                    }
-                }
-            }
-        }
-
-        return slingId;
-    }
-
     // ---------- Extension support 
--------------------------------------------
 
     /**


Reply via email to