incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1086523 - in /sling/trunk/installer/providers/jcr: ./ src/main/java/org/apache/sling/installer/provider/jcr/impl/
Date Tue, 29 Mar 2011 09:06:24 GMT
Author: cziegeler
Date: Tue Mar 29 09:06:23 2011
New Revision: 1086523

URL: http://svn.apache.org/viewvc?rev=1086523&view=rev
Log:
SLING-1971 : Persist configuration changes not made through the installer

Modified:
    sling/trunk/installer/providers/jcr/pom.xml
    sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/FileNodeConverter.java
    sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java
    sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrUtil.java

Modified: sling/trunk/installer/providers/jcr/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/pom.xml?rev=1086523&r1=1086522&r2=1086523&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/pom.xml (original)
+++ sling/trunk/installer/providers/jcr/pom.xml Tue Mar 29 09:06:23 2011
@@ -63,7 +63,10 @@
                         <Private-Package>org.apache.sling.installer.provider.jcr.impl.*</Private-Package>
                         <Sling-Nodetypes>SLING-INF/nodetypes/osgiconfig.cnd</Sling-Nodetypes>
                         <Sling-Namespaces>sling=http://sling.apache.org/jcr/sling/1.0</Sling-Namespaces>
-                        <Embed-Dependency>*;artifactId=org.apache.sling.commons.osgi</Embed-Dependency>
+                        <Embed-Dependency>
+                            org.apache.felix.configadmin;inline="org/apache/felix/cm/file/ConfigurationHandler.*",
+                            org.apache.sling.commons.osgi
+                        </Embed-Dependency>
                     </instructions>
                 </configuration>
             </plugin>
@@ -107,6 +110,13 @@
             <version>2.0.6</version>
             <scope>provided</scope>
         </dependency>
+      <!-- We use a class from the config admin implementation to read config files -->
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.configadmin</artifactId>
+            <version>1.2.8</version>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>

Modified: sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/FileNodeConverter.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/FileNodeConverter.java?rev=1086523&r1=1086522&r2=1086523&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/FileNodeConverter.java
(original)
+++ sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/FileNodeConverter.java
Tue Mar 29 09:06:23 2011
@@ -38,7 +38,7 @@ public class FileNodeConverter implement
     private static final String JCR_LAST_MODIFIED = "jcr:lastModified";
     private static final String JCR_CONTENT_LAST_MODIFIED = JCR_CONTENT + "/" + JCR_LAST_MODIFIED;
 
-    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Logger logger = LoggerFactory.getLogger(getClass());
 
 	/**
 	 * @see org.apache.sling.installer.provider.jcr.impl.JcrInstaller.NodeConverter#convertNode(javax.jcr.Node,
int)
@@ -47,17 +47,17 @@ public class FileNodeConverter implement
 	        final Node n,
 	        final int priority)
 	throws RepositoryException {
-		InstallableResource result = null;
+	    final String nodePath = n.getPath();
 		if (n.hasProperty(JCR_CONTENT_DATA) && n.hasProperty(JCR_CONTENT_LAST_MODIFIED))
{
 			try {
-				result = convert(n, n.getPath(), priority);
-			} catch(IOException ioe) {
-				log.info("Conversion failed, node {} ignored ({})", n.getPath(), ioe);
+				return convert(n, nodePath, priority);
+			} catch (final IOException ioe) {
+			    logger.info("Conversion failed, node {} ignored ({})", nodePath, ioe);
 			}
-			return result;
-		}
-		log.debug("Node {} has no {} properties, ignored", n.getPath(),
-				JCR_CONTENT_DATA + " or " + JCR_CONTENT_LAST_MODIFIED);
+		} else {
+		    logger.debug("Node {} has no {} properties, ignored", nodePath,
+    				JCR_CONTENT_DATA + " or " + JCR_CONTENT_LAST_MODIFIED);
+	    }
 		return null;
 	}
 

Modified: sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java?rev=1086523&r1=1086522&r2=1086523&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java
(original)
+++ sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java
Tue Mar 29 09:06:23 2011
@@ -18,8 +18,12 @@
  */
 package org.apache.sling.installer.provider.jcr.impl;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Dictionary;
 import java.util.LinkedList;
@@ -34,6 +38,7 @@ import javax.jcr.observation.Event;
 import javax.jcr.observation.EventIterator;
 import javax.jcr.observation.EventListener;
 
+import org.apache.felix.cm.file.ConfigurationHandler;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
@@ -60,7 +65,7 @@ import org.slf4j.LoggerFactory;
  */
 @Component(label="%jcrinstall.name", description="%jcrinstall.description", immediate=true,
metatype=true)
 @Properties({
-    @Property(name="service.description", value="Sling Jcrinstall Service"),
+    @Property(name="service.description", value="Sling JCR Install Service"),
     @Property(name="service.vendor", value="The Apache Software Foundation"),
     @Property(name=UpdateHandler.PROPERTY_SCHEMES, value=JcrInstaller.URL_SCHEME),
     @Property(name="service.ranking", intValue=100)
@@ -594,47 +599,63 @@ public class JcrInstaller implements Eve
             return null;
         }
 
-        final String path;
-        boolean resourceIsMoved = true;
-        if ( url != null ) {
-            // update
-            final int pos = url.indexOf(':');
-            final String oldPath = url.substring(pos + 1);
-            path = getPathWithHighestPrio(oldPath);
-            resourceIsMoved = path.equals(oldPath);
-            logger.debug("Update of {} at {}", resourceType, path);
-        } else {
-            // check for path hint
-            String hint = null;
-            if ( attributes != null ) {
-                hint = (String)attributes.get(InstallableResource.INSTALLATION_HINT);
-                if ( hint != null && hint.startsWith(URL_SCHEME + ':')) {
-                    hint = hint.substring(URL_SCHEME.length() + 1);
-                    final int lastSlash = hint.lastIndexOf('/');
-                    if ( lastSlash < 1 ) {
-                        hint = null;
-                    } else {
-                        int slashPos = hint.lastIndexOf('/', lastSlash - 1);
-                        final String dirName = hint.substring(slashPos + 1, lastSlash);
-                        if ( "install".equals(dirName) ) {
-                            hint = this.getPathWithHighestPrio(hint.substring(0, slashPos
+ 1) + "config/");
-                        } else {
+        Session session = null;
+        try {
+            session = this.repository.loginAdministrative(null);
+
+            final String path;
+            boolean resourceIsMoved = true;
+            if ( url != null ) {
+                // update
+                final int pos = url.indexOf(':');
+                final String oldPath = url.substring(pos + 1);
+                final String nodePath = getPathWithHighestPrio(oldPath);
+                if ( !nodePath.endsWith(".config") && session.itemExists(nodePath)
) {
+                    session.getItem(nodePath).remove();
+                    path = nodePath + ".config";
+                } else {
+                    path = nodePath;
+                }
+                resourceIsMoved = nodePath.equals(oldPath);
+                logger.debug("Update of {} at {}", resourceType, path);
+            } else {
+                // check for path hint
+                String hint = null;
+                if ( attributes != null ) {
+                    hint = (String)attributes.get(InstallableResource.INSTALLATION_HINT);
+                    if ( hint != null && hint.startsWith(URL_SCHEME + ':')) {
+                        hint = hint.substring(URL_SCHEME.length() + 1);
+                        final int lastSlash = hint.lastIndexOf('/');
+                        if ( lastSlash < 1 ) {
                             hint = null;
+                        } else {
+                            int slashPos = hint.lastIndexOf('/', lastSlash - 1);
+                            final String dirName = hint.substring(slashPos + 1, lastSlash);
+                            if ( "install".equals(dirName) ) {
+                                hint = this.getPathWithHighestPrio(hint.substring(0, slashPos
+ 1) + "config/");
+                            } else {
+                                hint = null;
+                            }
                         }
                     }
                 }
-            }
-            // add
-            path = (hint != null ? hint : this.newConfigPath) + id;
-            logger.debug("Add of {} at {}", resourceType, path);
-        }
+                // add
+                path = (hint != null ? hint : this.newConfigPath) + id + ".config";
+                logger.debug("Add of {} at {}", resourceType, path);
+            }
+
+            // write to a byte array stream
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            ConfigurationHandler.write(baos, dict);
+            baos.close();
+
+            // get or create file node
+            JcrUtil.createPath(session, path, "nt:file");
+            // get or create resource node
+            final Node dataNode = JcrUtil.createPath(session, path + "/jcr:content", "nt:resource");
 
-        Session session = null;
-        try {
-            session = this.repository.loginAdministrative(null);
-            final Node configNode = JcrUtil.createPath(session, path, ConfigNodeConverter.CONFIG_NODE_TYPE);
-            JcrUtil.removeAllProperties(configNode);
-            JcrUtil.saveProperties(configNode, dict);
+            dataNode.setProperty("jcr:data", new ByteArrayInputStream(baos.toByteArray()));
+            dataNode.setProperty("jcr:lastModified", Calendar.getInstance());
             session.save();
 
             final UpdateResult result = new UpdateResult(JcrInstaller.URL_SCHEME + ':' +
path);
@@ -643,7 +664,10 @@ public class JcrInstaller implements Eve
             result.setResourceIsMoved(resourceIsMoved);
             return result;
         } catch (final RepositoryException re) {
-            logger.error("Unable to remove resource from " + path, re);
+            logger.error("Unable to add/update resource " + resourceType + ':' + id, re);
+            return null;
+        } catch (final IOException e) {
+            logger.error("Unable to add/update resource " + resourceType + ':' + id, e);
             return null;
         } finally {
             if ( session != null ) {

Modified: sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrUtil.java?rev=1086523&r1=1086522&r2=1086523&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrUtil.java
(original)
+++ sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrUtil.java
Tue Mar 29 09:06:23 2011
@@ -18,18 +18,11 @@
  */
 package org.apache.sling.installer.provider.jcr.impl;
 
-import java.io.InputStream;
-import java.util.Calendar;
-import java.util.Dictionary;
-import java.util.Enumeration;
 import java.util.StringTokenizer;
 
 import javax.jcr.Node;
-import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
-import javax.jcr.Value;
-import javax.jcr.ValueFactory;
 
 /**
  * The <code>JcrUtil</code> class provides helper methods used
@@ -40,69 +33,6 @@ public abstract class JcrUtil {
     private static final String FOLDER_NODE_TYPE = "sling:Folder";
 
     /**
-     * Creates a {@link javax.jcr.Value JCR Value} for the given object with
-     * the given Session.
-     * Selects the the {@link javax.jcr.PropertyType PropertyType} according
-     * the instance of the object's Class
-     *
-     * @param value object
-     * @param session to create value for
-     * @return the value or null if not convertible to a valid PropertyType
-     * @throws RepositoryException in case of error, accessing the Repository
-     */
-    public static Value createValue(Object value, Session session)
-            throws RepositoryException {
-        Value val;
-        ValueFactory fac = session.getValueFactory();
-        if(value instanceof Calendar) {
-            val = fac.createValue((Calendar)value);
-        } else if (value instanceof InputStream) {
-            val = fac.createValue((InputStream)value);
-        } else if (value instanceof Node) {
-            val = fac.createValue((Node)value);
-        } else if (value instanceof Long) {
-            val = fac.createValue((Long)value);
-        } else if (value instanceof Integer) {
-            val = fac.createValue(((Integer)value));
-        } else if (value instanceof Number) {
-            val = fac.createValue(((Number)value).doubleValue());
-        } else if (value instanceof Boolean) {
-            val = fac.createValue((Boolean) value);
-        } else if ( value instanceof String ){
-            val = fac.createValue((String)value);
-        } else {
-            val = null;
-        }
-        return val;
-    }
-
-    /**
-     * Sets the value of the property.
-     * Selects the {@link javax.jcr.PropertyType PropertyType} according
-     * to the instance of the object's class.
-     * @param node         The node where the property will be set on.
-     * @param propertyName The name of the property.
-     * @param propertyValue The value for the property.
-     */
-    public static void setProperty(final Node node,
-                                   final String propertyName,
-                                   final Object propertyValue)
-    throws RepositoryException {
-        if ( propertyValue == null ) {
-            node.setProperty(propertyName, (String)null);
-        } else if ( propertyValue.getClass().isArray() ) {
-            final Object[] values = (Object[])propertyValue;
-            final Value[] setValues = new Value[values.length];
-            for(int i=0; i<values.length; i++) {
-                setValues[i] = createValue(values[i], node.getSession());
-            }
-            node.setProperty(propertyName, setValues);
-        } else {
-            node.setProperty(propertyName, createValue(propertyValue, node.getSession()));
-        }
-    }
-
-    /**
      * Creates or gets the {@link javax.jcr.Node Node} at the given Path.
      *
      * @param session The session to use for node creation
@@ -143,32 +73,4 @@ public abstract class JcrUtil {
         }
         return parentNode.getNode(relativePath);
     }
-
-    /**
-     * Remove all properties from the node.
-     * All properties without a namespace are removed.
-     * @param node The node
-     * @throws RepositoryException
-     */
-    public static void removeAllProperties(final Node node)
-    throws RepositoryException {
-        final PropertyIterator pI = node.getProperties();
-        while ( pI.hasNext() ) {
-            final javax.jcr.Property prop = pI.nextProperty();
-            if ( !prop.getName().contains(":") ) {
-                prop.remove();
-            }
-        }
-    }
-
-    public static void saveProperties(final Node configNode,
-                                      final Dictionary<String, Object> dict)
-    throws RepositoryException {
-        final Enumeration<String> keys = dict.keys();
-        while ( keys.hasMoreElements() ) {
-            final String key = keys.nextElement();
-
-            JcrUtil.setProperty(configNode, key, dict.get(key));
-        }
-    }
 }



Mime
View raw message