-
Notifications
You must be signed in to change notification settings - Fork 12
SLING-8843 repoinit developer mode #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
828cad9
de4a3a5
9e33541
2f0c463
3c385ed
da507a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| */ | ||
| package org.apache.sling.jcr.repoinit.impl; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.StringReader; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
@@ -28,6 +29,7 @@ | |
| import org.apache.sling.jcr.api.SlingRepositoryInitializer; | ||
| import org.apache.sling.jcr.repoinit.JcrRepoInitOpsProcessor; | ||
| import org.apache.sling.repoinit.parser.RepoInitParser; | ||
| import org.apache.sling.repoinit.parser.RepoInitParsingException; | ||
| import org.apache.sling.repoinit.parser.operations.Operation; | ||
| import org.osgi.framework.Constants; | ||
| import org.osgi.service.component.annotations.Activate; | ||
|
|
@@ -73,6 +75,8 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer | |
| String[] scripts() default {}; | ||
| } | ||
|
|
||
| protected static final String PROPERTY_FAIL_ON_ERROR = "org.apache.sling.jcr.repoinit.failOnError"; | ||
|
|
||
| private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
|
||
|
|
||
|
|
@@ -87,7 +91,11 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer | |
| @Activate | ||
| public void activate(final RepositoryInitializerFactory.Config config) { | ||
| this.config = config; | ||
| log.debug("Activated: {}", this.toString()); | ||
| if (continueOnError()) { | ||
| log.info("Activated: {} (continue on repoinit errors)", this); | ||
| } else { | ||
| log.debug("Activated: {}", this.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -99,43 +107,50 @@ public String toString() { | |
|
|
||
| @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 (continueOnError()) { | ||
| log.error("Repoinit error, won't stop execution because {} is set to non-true. Without this " | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "set to non-true" takes more mental bandwidth than "false" or "set to false", IMHO ;-) |
||
| + "setting the startup would fail.",PROPERTY_FAIL_ON_ERROR,e); | ||
| } 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 IOException, RepositoryException, RepoInitParsingException { | ||
| if (config.references() != null) { | ||
| final RepoinitTextProvider p = new RepoinitTextProvider(); | ||
| for (final String reference : config.references()) { | ||
| if (reference == null || reference.trim().length() == 0) { | ||
| continue; | ||
| } | ||
| 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); | ||
| } | ||
| 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; | ||
| } | ||
| } finally { | ||
| s.logout(); | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Apply the operations within a session, support retries | ||
| * @param session the JCR session to use | ||
|
|
@@ -177,5 +192,10 @@ protected void applyOperations(Session session, List<Operation> ops, String logM | |
| } | ||
| } | ||
|
|
||
|
|
||
| protected boolean continueOnError() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick, now that the property is named "fail on error" I'd use the same terminology here and call the method failOnError |
||
| String failOnError = System.getProperty(PROPERTY_FAIL_ON_ERROR,"true"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let us please use the OSGi way in order to allow using framework properties as well: https://docs.osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html#getProperty(java.lang.String) |
||
| return !failOnError.equals("true"); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * 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.jcr.repoinit.impl.util; | ||
|
|
||
| import static org.junit.Assert.fail; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import ch.qos.logback.classic.Level; | ||
| import ch.qos.logback.classic.Logger; | ||
| import ch.qos.logback.classic.LoggerContext; | ||
| import ch.qos.logback.classic.spi.ILoggingEvent; | ||
| import ch.qos.logback.core.read.ListAppender; | ||
|
|
||
| /** Capture logs for testing */ | ||
| public class LogCapture extends ListAppender<ILoggingEvent> implements Closeable { | ||
| private final boolean verboseFailure; | ||
|
|
||
| /** Setup the capture and start it */ | ||
| public LogCapture(String loggerName, boolean verboseFailure) { | ||
| this.verboseFailure = verboseFailure; | ||
| Logger logger = (Logger) LoggerFactory.getLogger(loggerName); | ||
| logger.setLevel(Level.ALL); | ||
| setContext((LoggerContext) LoggerFactory.getILoggerFactory()); | ||
| logger.addAppender(this); | ||
| start(); | ||
| } | ||
|
|
||
| public boolean anyMatch(Predicate<ILoggingEvent> p) { | ||
| return this.list.stream().anyMatch(p); | ||
| } | ||
|
|
||
| public void assertContains(Level atLevel, String ... substrings) { | ||
| Stream.of(substrings).forEach(substring -> { | ||
| if(!anyMatch(event -> event.getLevel() == atLevel && event.getFormattedMessage().contains(substring))) { | ||
| if(verboseFailure) { | ||
| fail(String.format("No log message contains [%s] in log\n%s", substring, this.list.toString())); | ||
| } else { | ||
| fail(String.format("No log message contains [%s]", substring)); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| stop(); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <configuration> | ||
| <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
| <encoder> | ||
| <Pattern> | ||
| %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n | ||
| </Pattern> | ||
| </encoder> | ||
| </appender> | ||
|
|
||
| <root level="INFO"> | ||
| <appender-ref ref="CONSOLE" /> | ||
| </root> | ||
|
|
||
| <logger name="org.apache.jackrabbit" level="WARN"/> | ||
| <logger name="org.apache.sling.jcr.resource" level="WARN"/> | ||
| <logger name="ch.qos.logback.core.joran.action.AppenderAction" level="WARN"/> | ||
| </configuration> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't slf4j-simple enough for the tests?