You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2022/03/20 13:32:32 UTC

[sling-org-apache-sling-jcr-repoinit] 01/01: SLING-8843 repoinit developer mode

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

joerghoh pushed a commit to branch improvement/SLING-8843-developer-mode
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git

commit 828cad9c1fb51ecd9f7836543502fa46679551b2
Author: Jörg Hoh <jo...@joerghoh.de>
AuthorDate: Sun Mar 20 14:27:57 2022 +0100

    SLING-8843 repoinit developer mode
    
    when providing the system property
    "org.apache.sling.jcr.repoinit.developermode" with the value "true"
    repoinit will not throw exception and terminate the startup of the
    repository, but instead logs an message pointing this out.
    Thus it's easier for a developer to fix the script without the need to
    make the repository going first.
    
    To clearly point out potential mis-use (having it configured in
    production environments), this log message is written on loglevel ERROR
    and also the on startup the "developer mode active" INFO message is
    written.
---
 .../impl/RepositoryInitializerFactory.java         | 83 ++++++++++++++--------
 .../impl/RepositoryInitializerFactoryTest.java     | 36 +++++++---
 src/test/resources/log4j.properties                |  8 +--
 3 files changed, 86 insertions(+), 41 deletions(-)

diff --git a/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java b/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java
index 8d30330..c239857 100644
--- a/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java
+++ b/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java
@@ -73,6 +73,8 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer
             String[] scripts() default {};
     }
 
+    protected static final String PROPERTY_DEVELOPER_MODE = "org.apache.sling.jcr.repoinit.developermode";
+    
     private final Logger log = LoggerFactory.getLogger(getClass());
 
 
@@ -87,7 +89,11 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer
     @Activate
     public void activate(final RepositoryInitializerFactory.Config config) {
         this.config = config;
-        log.debug("Activated: {}", this.toString());
+        if (isDeveloperModeEnabled()) {
+            log.info("Activated: {} (developer mode active)", this.toString());
+        } else {
+            log.debug("Activated: {}", this.toString());
+        }
     }
 
     @Override
@@ -99,43 +105,54 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer
 
     @Override
     public void processRepository(final SlingRepository repo) throws Exception {
-        if ( (config.references() != null && config.references().length > 0)
-           || (config.scripts() != null && config.scripts().length > 0 )) {
+        // loginAdministrative is ok here, definitely an admin operation
+        final Session s = repo.loginAdministrative(null);
+        try {
+            executeScripts(s, config);
+        } catch (Exception e) {
+            if (isDeveloperModeEnabled()) {
+                log.error("Eror in the repoinit scripts, which is ignored because the developer mode is active. "
+                        + "If not fixed it will prevent the startup of non-developer systems");
+            } else {
+                throw (e);
+            }
+        } finally {
+            s.logout();
+        }
+    }
 
-            // loginAdministrative is ok here, definitely an admin operation
-            final Session s = repo.loginAdministrative(null);
-            try {
-                if ( config.references() != null ) {
-                    final RepoinitTextProvider p = new RepoinitTextProvider();
-                    for(final String reference : config.references()) {
-                        if(reference == null || reference.trim().length() == 0) {
-                            continue;
-                        }
-                        final String repoinitText = p.getRepoinitText("raw:" + reference);
-                        final List<Operation> ops = parser.parse(new StringReader(repoinitText));
-                        String msg = String.format("Executing %s repoinit operations", ops.size());
-                        log.info(msg);
-                        applyOperations(s,ops,msg);
+
+    
+    protected void executeScripts (Session session, RepositoryInitializerFactory.Config config) throws Exception {
+        if ( (config.references() != null && config.references().length > 0)
+                || (config.scripts() != null && config.scripts().length > 0 )) {
+            if ( config.references() != null ) {
+                final RepoinitTextProvider p = new RepoinitTextProvider();
+                for(final String reference : config.references()) {
+                    if(reference == null || reference.trim().length() == 0) {
+                        continue;
                     }
+                    final String repoinitText = p.getRepoinitText("raw:" + reference);
+                    final List<Operation> ops = parser.parse(new StringReader(repoinitText));
+                    String msg = String.format("Executing %s repoinit operations", ops.size());
+                    log.info(msg);
+                    applyOperations(session,ops,msg);
                 }
-                if ( config.scripts() != null ) {
-                    for(final String script : config.scripts()) {
-                        if(script == null || script.trim().length() == 0) {
-                            continue;
-                        }
-                        final List<Operation> ops = parser.parse(new StringReader(script));
-                        String msg = String.format("Executing %s repoinit operations", ops.size());
-                        log.info(msg);
-                        applyOperations(s,ops,msg);
+            }
+            if ( config.scripts() != null ) {
+                for(final String script : config.scripts()) {
+                    if(script == null || script.trim().length() == 0) {
+                        continue;
                     }
+                    final List<Operation> ops = parser.parse(new StringReader(script));
+                    String msg = String.format("Executing %s repoinit operations", ops.size());
+                    log.info(msg);
+                    applyOperations(session,ops,msg);
                 }
-            } finally {
-                s.logout();
             }
         }
     }
 
-
     /**
      * Apply the operations within a session, support retries
      * @param session the JCR session to use
@@ -177,5 +194,13 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer
         }
     }
 
+    
+    protected static boolean isDeveloperModeEnabled() {
+        String dm = System.getProperty(PROPERTY_DEVELOPER_MODE);
+        if (dm == null) {
+            return false;
+        }
+        return dm.toLowerCase().equals("true");
+    }
 
 }
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java b/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java
index ac69d7c..24dc999 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java
@@ -16,25 +16,30 @@
  */
 package org.apache.sling.jcr.repoinit.impl;
 
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+
+import java.util.Properties;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.jcr.repoinit.JcrRepoInitOpsProcessor;
 import org.apache.sling.repoinit.parser.RepoInitParser;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-
 import org.mockito.ArgumentMatchers;
-
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.mock;
+import org.mockito.Mockito;
 
 public class RepositoryInitializerFactoryTest {
 
     @Rule
-    public SlingContext context = new SlingContext();
+    public SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK);
 
     RepositoryInitializerFactory sut;
     JcrRepoInitOpsProcessor processor;
@@ -56,4 +61,19 @@ public class RepositoryInitializerFactoryTest {
             .when(processor).apply(ArgumentMatchers.any(), ArgumentMatchers.any());
         sut.applyOperations(mock(Session.class), null, null); 
     }
+    
+    @Test
+    public void validateDeveloperMode() throws Exception {
+        Properties oldProps = System.getProperties();
+        try {
+            System.setProperty(RepositoryInitializerFactory.PROPERTY_DEVELOPER_MODE, "true");
+            RepositoryInitializerFactory spy = Mockito.spy(sut);
+            doThrow(new RepositoryException("reason")).when(spy).executeScripts(any(Session.class),any(RepositoryInitializerFactory.Config.class));
+            
+            spy.processRepository(context.getService(SlingRepository.class));
+        } finally {
+            System.clearProperty(RepositoryInitializerFactory.PROPERTY_DEVELOPER_MODE);
+        }
+    }
+
 }
diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties
index 3060198..480c030 100644
--- a/src/test/resources/log4j.properties
+++ b/src/test/resources/log4j.properties
@@ -16,9 +16,9 @@
 # under the License.
 
 log4j.rootLogger=INFO, file
-log4j.appender.file=org.apache.log4j.FileAppender
-log4j.appender.file.File=target/testing.log
+log4j.appender.file=org.apache.log4j.ConsoleAppender
 log4j.appender.file.layout=org.apache.log4j.PatternLayout
-log4j.appender.file.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss} *%-5p* [%t] %c{1}: %m (%F, line %L)\n
+log4j.appender.file.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss:SSS} *%-5p* [%t] %c{1}: %m (%F, line %L)\n
 
-log4j.logger.org.apache.jackrabbit=WARN
\ No newline at end of file
+log4j.logger.org.apache.jackrabbit=WARN
+log4j.logger.org.apache.sling.jcr.resource=WARN
\ No newline at end of file