incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r695882 - in /incubator/sling/trunk/extensions/jcrinstall/src: main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/ test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/
Date Tue, 16 Sep 2008 13:48:19 GMT
Author: bdelacretaz
Date: Tue Sep 16 06:48:18 2008
New Revision: 695882

URL: http://svn.apache.org/viewvc?rev=695882&view=rev
Log:
SLING-658 - avoid uninstalling resources in the activate() method

Modified:
    incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RegexpFilter.java
    incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
    incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/MockRepositoryObserver.java
    incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/ResourceDetectionTest.java

Modified: incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RegexpFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RegexpFilter.java?rev=695882&r1=695881&r2=695882&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RegexpFilter.java
(original)
+++ incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RegexpFilter.java
Tue Sep 16 06:48:18 2008
@@ -24,12 +24,18 @@
 /** Simple regexp-based filter for folder and file names */ 
 class RegexpFilter {
     private final Pattern pattern;
+    private final String regexp;
     
     RegexpFilter(String regexp) throws PatternSyntaxException {
+        this.regexp = regexp;
         pattern = Pattern.compile(regexp);
     }
     
     boolean accept(String path) {
         return pattern.matcher(path).matches();
     }
+    
+    String getRegexp() {
+        return regexp;
+    }
 }

Modified: incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java?rev=695882&r1=695881&r2=695882&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
(original)
+++ incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
Tue Sep 16 06:48:18 2008
@@ -73,6 +73,7 @@
     protected SlingRepository repository;
     
     private Session session;
+    private File serviceDataFile;
     
     private List<WatchedFolderCreationListener> listeners = new LinkedList<WatchedFolderCreationListener>();
     
@@ -111,27 +112,7 @@
             log.info("Using folder name regexp '{}' from context property '{}'", folderNameRegexp,
FOLDER_NAME_REGEXP_PROPERTY);
     	}
         folderNameFilter = new RegexpFilter(folderNameRegexp);
-        
-        // If regexp has changed, uninstall everything - it's a bit hard
-        // to know what might have changed otherwise
-        // (null context happens during testing only)
-        if(context != null) { 
-            final File f = context.getBundleContext().getDataFile(DATA_FILE);
-            final Properties props = loadProperties(f);
-            final String oldRegexp = props.getProperty(DATA_LAST_FOLDER_REGEXP);
-            if(oldRegexp != null && !oldRegexp.equals(folderNameRegexp)) {
-                log.info("Folder name regexp has changed, uninstalling all resources ({}
-> {}", oldRegexp, folderNameRegexp);
-                for(String uri : osgiController.getInstalledUris()) {
-                    try {
-                        osgiController.uninstall(uri);
-                    } catch (JcrInstallException e) {
-                        log.warn("Exception during 'uninstall all'", e);
-                    }
-                }
-            }
-            props.setProperty(DATA_LAST_FOLDER_REGEXP, folderNameRegexp);
-            saveProperties(props, f);
-        }
+        serviceDataFile = getServiceDataFile(context);
         
         // Listen for any new WatchedFolders created after activation
         session = repository.loginAdministrative(repository.getDefaultWorkspace());
@@ -145,17 +126,6 @@
                     isDeep, null, null, noLocal);
         }
         
-        // Check if any deletions happened while this
-        // service was inactive: create a fake WatchFolder 
-        // on / and use it to check for any deletions, even 
-        // if the corresponding WatchFolders are gone
-        try {
-            final WatchedFolder rootWf = new WatchedFolder(repository, "/", osgiController,
filenameFilter, 0L);
-            rootWf.checkDeletions(osgiController.getInstalledUris());
-        } catch(Exception e) {
-            log.warn("Exception in root WatchFolder.checkDeletions call", e);
-        }
-
         // Find folders to watch
         folders = new HashSet<WatchedFolder>();
         for(String root : roots) {
@@ -169,6 +139,10 @@
         t.start();
     }
     
+    protected File getServiceDataFile(ComponentContext context) {
+        return context.getBundleContext().getDataFile(DATA_FILE);
+    }
+    
     /** Get a property value from the component context or bundle context */
     protected String getPropertyValue(ComponentContext ctx, String name) {
         String result = (String)ctx.getProperties().get(name);
@@ -264,12 +238,48 @@
         return path;
     }
     
+    /** Uninstall resources as needed when starting up */
+    void handleInitialUninstalls() {
+        // If regexp has changed, uninstall everything - it's a bit hard
+        // to know what might have changed otherwise
+        // (null context happens during testing only)
+        final Properties props = loadProperties(serviceDataFile);
+        final String oldRegexp = props.getProperty(DATA_LAST_FOLDER_REGEXP);
+        if(oldRegexp != null && !oldRegexp.equals(folderNameFilter.getRegexp()))
{
+            log.info("Folder name regexp has changed, uninstalling all resources ( {} ->
{} )", 
+                    oldRegexp, folderNameFilter.getRegexp());
+            for(String uri : osgiController.getInstalledUris()) {
+                try {
+                    osgiController.uninstall(uri);
+                } catch (JcrInstallException e) {
+                    log.warn("Exception during 'uninstall all'", e);
+                }
+            }
+        }
+        props.setProperty(DATA_LAST_FOLDER_REGEXP, folderNameFilter.getRegexp());
+        saveProperties(props, serviceDataFile);
+        
+        // Check if any deletions happened while this
+        // service was inactive: create a fake WatchFolder 
+        // on / and use it to check for any deletions, even 
+        // if the corresponding WatchFolders are gone
+        try {
+            final WatchedFolder rootWf = new WatchedFolder(repository, "/", osgiController,
filenameFilter, 0L);
+            rootWf.checkDeletions(osgiController.getInstalledUris());
+        } catch(Exception e) {
+            log.warn("Exception in root WatchFolder.checkDeletions call", e);
+        }
+
+    }
+    
     /**
      * Scan WatchFolders once their timer expires
      */
     public void run() {
         log.info("{} thread {} starts", getClass().getSimpleName(), Thread.currentThread().getName());
 
+        handleInitialUninstalls();
+        
         // We could use the scheduler service but that makes things harder to test
         while (running) {
             try {

Modified: incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/MockRepositoryObserver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/MockRepositoryObserver.java?rev=695882&r1=695881&r2=695882&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/MockRepositoryObserver.java
(original)
+++ incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/MockRepositoryObserver.java
Tue Sep 16 06:48:18 2008
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.jcr.jcrinstall.jcr.impl;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.Properties;
 import java.util.Set;
 
@@ -30,11 +32,17 @@
  */
 public class MockRepositoryObserver extends RepositoryObserver {
     private Properties props;
+    private final File serviceDataFile;
     
     MockRepositoryObserver(SlingRepository repo, final OsgiController c) {
+        this(repo, c, null);
+    }
+    
+    MockRepositoryObserver(SlingRepository repo, final OsgiController c, File serviceDataFile)
{
         repository = repo;
         osgiController = c;
         scanDelayMsec = 0;
+        this.serviceDataFile = serviceDataFile;
     }
     
     public void run() {
@@ -63,4 +71,18 @@
     protected String getPropertyValue(ComponentContext ctx, String name) {
         return (props == null ? null : props.getProperty(name));
     }
+    
+    protected File getServiceDataFile(ComponentContext context) {
+        if(serviceDataFile != null) {
+            return serviceDataFile;
+        }
+        
+        try {
+            final File f = File.createTempFile(getClass().getSimpleName(), ".properties");
+            f.deleteOnExit();
+            return f;
+        } catch(IOException ioe) {
+            throw new Error("IOException", ioe);
+        }
+    }
 }

Modified: incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/ResourceDetectionTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/ResourceDetectionTest.java?rev=695882&r1=695881&r2=695882&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/ResourceDetectionTest.java
(original)
+++ incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/jcr/impl/ResourceDetectionTest.java
Tue Sep 16 06:48:18 2008
@@ -237,9 +237,8 @@
             one(c).uninstall("/libs/watchfolder-is-gone/install/gone.cfg");
         }});
         
-        // Activating with installed resources that are not in
-        // the repository must cause them to be uninstalled
         ro.activate(null);
+        ro.handleInitialUninstalls();
         mockery.assertIsSatisfied();
     }
     
@@ -269,6 +268,7 @@
         // Activating with installed resources that are not in
         // the repository must cause them to be uninstalled
         ro.activate(null);
+        ro.handleInitialUninstalls();
         mockery.assertIsSatisfied();
     }
     
@@ -310,6 +310,8 @@
     
     /** Verify that resources are correctly uninstalled if the folder name regexp changes
*/
     public void testFolderRegexpChange() throws Exception {
+        final File serviceDataFile = File.createTempFile(getClass().getName(), ".properties");
+        serviceDataFile.deleteOnExit();
         contentHelper.setupContent();
         
        final String [] resources = {
@@ -329,44 +331,42 @@
         final Set<String> installedUri = new HashSet<String>();
         final OsgiController c = mockery.mock(OsgiController.class);
         final Properties props = new Properties();
-        final ComponentContext ctx = mockery.mock(ComponentContext.class);
-        final BundleContext bc = mockery.mock(BundleContext.class);
-        final File f = File.createTempFile(getClass().getSimpleName(), "properties");
-        f.deleteOnExit();
         
         // Test with first regexp
         mockery.checking(new Expectations() {{
-            allowing(ctx).getBundleContext(); will(returnValue(bc));
-            allowing(bc).getDataFile("service.properties"); will(returnValue(f));
             allowing(c).getInstalledUris(); will(returnValue(installedUri));
             allowing(c).getLastModified(with(any(String.class))); will(returnValue(-1L));

             one(c).installOrUpdate(with(equal(resources[0])), with(equal(lastModifiedA)),
with(any(InputStream.class)));
             one(c).installOrUpdate(with(equal(resources[2])), with(equal(lastModifiedA)),
with(any(InputStream.class)));
         }});
         
-        final MockRepositoryObserver ro = new MockRepositoryObserver(repo, c);
+        final MockRepositoryObserver ro = new MockRepositoryObserver(repo, c, serviceDataFile);
         ro.setProperties(props);
         props.setProperty(RepositoryObserver.FOLDER_NAME_REGEXP_PROPERTY, ".*foo/bar/install$");
-        ro.activate(ctx);
+        ro.activate(null);
+        ro.handleInitialUninstalls();
         for(String file : resources) {
             contentHelper.createOrUpdateFile(file, data, lastModifiedA);
         }
         eventHelper.waitForEvents(5000L);
         ro.runOneCycle();
         mockery.assertIsSatisfied();
+        installedUri.add(resources[0]);
+        installedUri.add(resources[2]);
         
         // Test with a different regexp, install.A resources must be uninstalled
         mockery.checking(new Expectations() {{
-            allowing(ctx).getBundleContext(); will(returnValue(bc));
-            allowing(bc).getDataFile("service.properties"); will(returnValue(f));
             allowing(c).getInstalledUris(); will(returnValue(installedUri));
             allowing(c).getLastModified(with(any(String.class))); will(returnValue(-1L));

+            one(c).uninstall(resources[0]);
+            one(c).uninstall(resources[2]);
             one(c).installOrUpdate(with(equal(resources[1])), with(equal(lastModifiedA)),
with(any(InputStream.class)));
             one(c).installOrUpdate(with(equal(resources[3])), with(equal(lastModifiedA)),
with(any(InputStream.class)));
         }});
         
         props.setProperty(RepositoryObserver.FOLDER_NAME_REGEXP_PROPERTY, ".*foo/wii/install$");
-        ro.activate(ctx);
+        ro.activate(null);
+        ro.handleInitialUninstalls();
         for(String file : resources) {
             contentHelper.createOrUpdateFile(file, data, lastModifiedA);
         }



Mime
View raw message