diff --git a/pom.xml b/pom.xml index 3c24657..6aab071 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,17 @@ https://github.com/Netcentric/vault-upgrade-hook/issues + + + + org.assertj + assertj-core + 2.8.0 + test + + + + clean install diff --git a/vault-upgrade-hook/pom.xml b/vault-upgrade-hook/pom.xml index 595e65a..ed0c0da 100644 --- a/vault-upgrade-hook/pom.xml +++ b/vault-upgrade-hook/pom.xml @@ -177,8 +177,6 @@ org.assertj assertj-core - 2.8.0 - test \ No newline at end of file diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeInfo.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeInfo.java index ed67ead..30003d2 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeInfo.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeInfo.java @@ -23,11 +23,9 @@ import org.apache.jackrabbit.vault.packaging.InstallContext; import org.apache.jackrabbit.vault.packaging.InstallContext.Phase; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; import biz.netcentric.vlt.upgrade.handler.UpgradeHandler; import biz.netcentric.vlt.upgrade.handler.UpgradeType; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; /** * This class represents the wrapper to execute {@link UpgradeAction}s. It loads @@ -35,7 +33,7 @@ */ public class UpgradeInfo { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UpgradeInfo.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(UpgradeInfo.class); /** * This property configures the handler to execute this {@link UpgradeInfo}s @@ -78,15 +76,14 @@ public UpgradeInfo(InstallContext ctx, UpgradeStatus status, Node node) throws R installationMode = InstallationMode.valueOf(JcrUtils.getStringProperty(node, PN_INSTALLATION_MODE, DEFAULT_INSTALLATION_MODE).toUpperCase()); defaultPhase = Phase.valueOf(JcrUtils.getStringProperty(node, PN_DEFAULT_PHASE, DEFAULT_PHASE).toUpperCase()); handler = UpgradeType.create(ctx, JcrUtils.getStringProperty(node, PN_HANDLER, DEFAULT_HANDLER)); - loadActions(ctx); LOG.debug(ctx, "UpgradeInfo loaded [{}]", this); } - private void loadActions(InstallContext ctx) throws RepositoryException { + public void loadActions(InstallContext ctx) throws RepositoryException { for (Phase availablePhase : Phase.values()) { actions.put(availablePhase, new ArrayList()); } - for (UpgradeAction action : handler.create(ctx, new UpgradeActionInfo(node, defaultPhase))) { + for (UpgradeAction action : handler.create(ctx, this)) { actions.get(action.getPhase()).add(action); } for (Phase availablePhase : Phase.values()) { diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeProcessor.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeProcessor.java index b4823b2..7f088b2 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeProcessor.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeProcessor.java @@ -26,7 +26,6 @@ import biz.netcentric.vlt.upgrade.UpgradeInfo.InstallationMode; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; /** * This class is the main entry point for the vault-upgrade-hook @@ -39,7 +38,7 @@ */ public class UpgradeProcessor implements InstallHook { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UpgradeProcessor.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(UpgradeProcessor.class); /** * Absolute path where information of the {@link UpgradeInfo} execution are @@ -160,6 +159,7 @@ protected void loadInfos(final InstallContext ctx) throws RepositoryException { while (nodes.hasNext()) { Node child = nodes.nextNode(); final UpgradeInfo info = new UpgradeInfo(ctx, status, child); + info.loadActions(ctx); LOG.debug(ctx, "info [{}]", info); infos.add(info); } diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeStatus.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeStatus.java index 89c13e9..0369e5f 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeStatus.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeStatus.java @@ -22,7 +22,6 @@ import org.apache.jackrabbit.vault.packaging.Version; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; /** * This class represents a previous upgrade execution and has methods to compare @@ -30,7 +29,7 @@ */ public class UpgradeStatus { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UpgradeStatus.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(UpgradeStatus.class); public static final String PN_UPGRADE_TIME = "time"; public static final String PN_VERSION = "version"; diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeActionInfo.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeActionInfo.java deleted file mode 100644 index 2d16944..0000000 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeActionInfo.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * (C) Copyright 2016 Netcentric AG. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package biz.netcentric.vlt.upgrade.handler; - -import javax.jcr.Node; - -import org.apache.jackrabbit.vault.packaging.InstallContext.Phase; - -public class UpgradeActionInfo { - - private Node node; - private Phase defaultPhase; - - public UpgradeActionInfo(Node node, Phase defaultPhase) { - this.node = node; - this.defaultPhase = defaultPhase; - } - - public Node getNode() { - return node; - } - - public Phase getDefaultPhase() { - return defaultPhase; - } -} diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeHandler.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeHandler.java index c8aa8d9..753369a 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeHandler.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeHandler.java @@ -39,6 +39,6 @@ public interface UpgradeHandler { * @return * @throws RepositoryException */ - Iterable create(InstallContext ctx, UpgradeActionInfo info) throws RepositoryException; + Iterable create(InstallContext ctx, UpgradeInfo info) throws RepositoryException; } diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandler.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandler.java index 0294ab6..c392933 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandler.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandler.java @@ -22,10 +22,8 @@ import biz.netcentric.vlt.upgrade.UpgradeAction; import biz.netcentric.vlt.upgrade.UpgradeInfo; import biz.netcentric.vlt.upgrade.handler.OsgiUtil; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; import biz.netcentric.vlt.upgrade.handler.UpgradeHandler; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; /** * This handler creates {@link GroovyScript} instances which are executed via @@ -35,7 +33,7 @@ */ public class GroovyConsoleHandler implements UpgradeHandler { - static PackageInstallLogger LOG = PackageInstallLoggerImpl.create(GroovyConsoleHandler.class); + static PackageInstallLogger log = PackageInstallLogger.create(GroovyConsoleHandler.class); OsgiUtil osgi = new OsgiUtil(); @@ -43,7 +41,7 @@ public class GroovyConsoleHandler implements UpgradeHandler { public boolean isAvailable(InstallContext ctx) { boolean available = osgi.hasService(GroovyConsoleService.class); if (!available) { - LOG.warn(ctx, "Could not load GroovyConsoleService."); + log.warn(ctx, "Could not load GroovyConsoleService."); } return available; @@ -51,7 +49,7 @@ public boolean isAvailable(InstallContext ctx) { @Override - public List create(InstallContext ctx, UpgradeActionInfo info) throws RepositoryException { + public List create(InstallContext ctx, UpgradeInfo info) throws RepositoryException { List scripts = new ArrayList<>(); NodeIterator nodes = info.getNode().getNodes(); diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyScript.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyScript.java index 3b18781..85e1309 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyScript.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyScript.java @@ -25,11 +25,10 @@ import biz.netcentric.vlt.upgrade.handler.OsgiUtil.ServiceWrapper; import biz.netcentric.vlt.upgrade.handler.SlingUtils; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; public class GroovyScript extends UpgradeAction { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(GroovyScript.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(GroovyScript.class); SlingUtils sling = new SlingUtils(); private final Node script; diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/Script.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/Script.java index 5b41d2a..83afde2 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/Script.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/Script.java @@ -13,11 +13,10 @@ import biz.netcentric.vlt.upgrade.UpgradeAction; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; public class Script extends UpgradeAction { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(Script.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(Script.class); private final SlingScript delegate; diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/ScriptHandler.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/ScriptHandler.java index bb8c2eb..2639a64 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/ScriptHandler.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/script/ScriptHandler.java @@ -11,15 +11,14 @@ import org.apache.sling.api.scripting.SlingScript; import biz.netcentric.vlt.upgrade.UpgradeAction; +import biz.netcentric.vlt.upgrade.UpgradeInfo; import biz.netcentric.vlt.upgrade.handler.SlingUtils; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; import biz.netcentric.vlt.upgrade.handler.UpgradeHandler; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; public class ScriptHandler implements UpgradeHandler { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(ScriptHandler.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(ScriptHandler.class); SlingUtils sling = new SlingUtils(); @@ -29,7 +28,7 @@ public boolean isAvailable(final InstallContext ctx) { } @Override - public Iterable create(final InstallContext ctx, final UpgradeActionInfo info) throws RepositoryException { + public Iterable create(final InstallContext ctx, final UpgradeInfo info) throws RepositoryException { final List scripts = new ArrayList<>(); final Resource root = sling.getResourceResolver(ctx.getSession()).getResource(info.getNode().getPath()); diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipe.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipe.java index 28457cf..05ea28d 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipe.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipe.java @@ -19,11 +19,10 @@ import biz.netcentric.vlt.upgrade.handler.OsgiUtil; import biz.netcentric.vlt.upgrade.handler.OsgiUtil.ServiceWrapper; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; public class SlingPipe extends UpgradeAction { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(SlingPipe.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(SlingPipe.class); OsgiUtil osgi = new OsgiUtil(); diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipesHandler.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipesHandler.java index 401f4e8..d566f21 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipesHandler.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/slingpipes/SlingPipesHandler.java @@ -18,15 +18,14 @@ import org.apache.sling.pipes.Plumber; import biz.netcentric.vlt.upgrade.UpgradeAction; +import biz.netcentric.vlt.upgrade.UpgradeInfo; import biz.netcentric.vlt.upgrade.handler.SlingUtils; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; import biz.netcentric.vlt.upgrade.handler.UpgradeHandler; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; public class SlingPipesHandler implements UpgradeHandler { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(SlingPipesHandler.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(SlingPipesHandler.class); SlingUtils sling = new SlingUtils(); @@ -40,7 +39,7 @@ public boolean isAvailable(InstallContext ctx) { } @Override - public Iterable create(InstallContext ctx, UpgradeActionInfo info) throws RepositoryException { + public Iterable create(InstallContext ctx, UpgradeInfo info) throws RepositoryException { Collection pipes = new ArrayList<>(); for (Resource child : sling.getResourceResolver(info.getNode().getSession()) .getResource(info.getNode().getPath()).getChildren()) { diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesHandler.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesHandler.java index 436a1fd..a6ba75b 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesHandler.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesHandler.java @@ -14,16 +14,15 @@ import org.apache.jackrabbit.vault.packaging.InstallContext; import biz.netcentric.vlt.upgrade.UpgradeAction; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; +import biz.netcentric.vlt.upgrade.UpgradeInfo; import biz.netcentric.vlt.upgrade.handler.UpgradeHandler; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; public class UserPreferencesHandler implements UpgradeHandler { private static final String PN_USER_IDS = "handler.userIds"; private static final String NAME_SUFFIX_USER_PREFERENCES = "user.preferences"; - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UserPreferencesHandler.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(UserPreferencesHandler.class); @Override public boolean isAvailable(InstallContext ctx) { @@ -32,7 +31,7 @@ public boolean isAvailable(InstallContext ctx) { } @Override - public Iterable create(InstallContext ctx, UpgradeActionInfo info) throws RepositoryException { + public Iterable create(InstallContext ctx, UpgradeInfo info) throws RepositoryException { List actions = new ArrayList<>(); // extract user id from upgrade info node diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesUpgradeAction.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesUpgradeAction.java index b3e652a..a0142b9 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesUpgradeAction.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/userpreferences/UserPreferencesUpgradeAction.java @@ -11,11 +11,10 @@ import biz.netcentric.vlt.upgrade.UpgradeAction; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -import biz.netcentric.vlt.upgrade.util.PackageInstallLoggerImpl; public class UserPreferencesUpgradeAction extends UpgradeAction { - private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UserPreferencesUpgradeAction.class); + private static final PackageInstallLogger LOG = PackageInstallLogger.create(UserPreferencesUpgradeAction.class); private final Node xmlFileNode; private final String userId; diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/PackageInstallLogger.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/PackageInstallLogger.java index ed18d8d..807e3c9 100644 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/PackageInstallLogger.java +++ b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/PackageInstallLogger.java @@ -1,15 +1,90 @@ +/* + * (C) Copyright 2016 Netcentric AG. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ package biz.netcentric.vlt.upgrade.util; +import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener; +import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener.Mode; import org.apache.jackrabbit.vault.packaging.InstallContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.helpers.FormattingTuple; +import org.slf4j.helpers.MessageFormatter; -public interface PackageInstallLogger { - void debug(InstallContext ctx, String format, Object... arguments); +/** + * Helper class to log during content package installation. + */ +public class PackageInstallLogger { + private final Logger base; - void info(InstallContext ctx, String format, Object... arguments); + public PackageInstallLogger(Logger log) { + base = log; + } - void status(InstallContext ctx, String format, String path, Object... arguments); + public static PackageInstallLogger create(Class clazz) { + return new PackageInstallLogger(LoggerFactory.getLogger(clazz)); + } - void warn(InstallContext ctx, String format, Object... arguments); + public void debug(InstallContext ctx, String format, Object... arguments) { + if (getBase().isDebugEnabled()) { + FormattingTuple message = build(format, arguments); + getBase().debug(message.getMessage(), message.getThrowable()); + } + } + + public void info(InstallContext ctx, String format, Object... arguments) { + FormattingTuple message = build(format, arguments); + getBase().info(message.getMessage(), message.getThrowable()); + } + + public void status(InstallContext ctx, String format, String path, Object... arguments) { + info(ctx, format + " - " + path, arguments); + progressLog(ctx, build(format, arguments), path); + } + + public void warn(InstallContext ctx, String format, Object... arguments) { + FormattingTuple message = build(format, arguments); + getBase().warn(message.getMessage(), message.getThrowable()); + progressLog(ctx, message, null); + } + + public void error(InstallContext ctx, String format, Object... arguments) { + FormattingTuple message = build(format, arguments); + getBase().error(message.getMessage(), message.getThrowable()); + progressLog(ctx, message, null); + } + + protected FormattingTuple build(String format, Object[] arguments) { + return MessageFormatter.arrayFormat(format, arguments); + } + + protected void progressLog(InstallContext ctx, FormattingTuple message, String path) { + ProgressTrackerListener l = ctx.getOptions().getListener(); + if (l != null) { + if (message.getThrowable() != null) { + l.onError(Mode.TEXT, message.getMessage(), toException(message.getThrowable())); + } else { + l.onMessage(path == null ? Mode.TEXT : Mode.PATHS, "Upgrade " + message.getMessage(), + path == null ? "" : path); + } + } + } + + protected Exception toException(Throwable throwable) { + if (throwable instanceof Exception) { + return (Exception) throwable; + } else { + return new Exception(throwable); + } + } + + public Logger getBase() { + return base; + } - void error(InstallContext ctx, String format, Object... arguments); } diff --git a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/PackageInstallLoggerImpl.java b/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/PackageInstallLoggerImpl.java deleted file mode 100644 index 46f85f3..0000000 --- a/vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/PackageInstallLoggerImpl.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * (C) Copyright 2016 Netcentric AG. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package biz.netcentric.vlt.upgrade.util; - -import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener; -import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener.Mode; -import org.apache.jackrabbit.vault.packaging.InstallContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.helpers.FormattingTuple; -import org.slf4j.helpers.MessageFormatter; - -/** - * Helper class to log during content package installation. - */ -public class PackageInstallLoggerImpl implements PackageInstallLogger { - private final Logger base; - - public PackageInstallLoggerImpl(Logger log) { - base = log; - } - - public static PackageInstallLoggerImpl create(Class clazz) { - return new PackageInstallLoggerImpl(LoggerFactory.getLogger(clazz)); - } - - @Override - public void debug(InstallContext ctx, String format, Object... arguments) { - if (getBase().isDebugEnabled()) { - FormattingTuple message = build(format, arguments); - getBase().debug(message.getMessage(), message.getThrowable()); - } - } - - @Override - public void info(InstallContext ctx, String format, Object... arguments) { - FormattingTuple message = build(format, arguments); - getBase().info(message.getMessage(), message.getThrowable()); - } - - @Override - public void status(InstallContext ctx, String format, String path, Object... arguments) { - info(ctx, format + " - " + path, arguments); - progressLog(ctx, build(format, arguments), path); - } - - @Override - public void warn(InstallContext ctx, String format, Object... arguments) { - FormattingTuple message = build(format, arguments); - getBase().warn(message.getMessage(), message.getThrowable()); - progressLog(ctx, message, null); - } - - @Override - public void error(InstallContext ctx, String format, Object... arguments) { - FormattingTuple message = build(format, arguments); - getBase().error(message.getMessage(), message.getThrowable()); - progressLog(ctx, message, null); - } - - protected FormattingTuple build(String format, Object[] arguments) { - return MessageFormatter.arrayFormat(format, arguments); - } - - protected void progressLog(InstallContext ctx, FormattingTuple message, String path) { - ProgressTrackerListener l = ctx.getOptions().getListener(); - if (l != null) { - if (message.getThrowable() != null) { - l.onError(Mode.TEXT, message.getMessage(), toException(message.getThrowable())); - } else { - l.onMessage(path == null ? Mode.TEXT : Mode.PATHS, "Upgrade " + message.getMessage(), - path == null ? "" : path); - } - } - } - - protected Exception toException(Throwable throwable) { - if (throwable instanceof Exception) { - return (Exception) throwable; - } else { - return new Exception(throwable); - } - } - - public Logger getBase() { - return base; - } - -} diff --git a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeInfoTest.java b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeInfoTest.java index 2a587d6..89877c2 100644 --- a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeInfoTest.java +++ b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeInfoTest.java @@ -29,7 +29,6 @@ import org.mockito.runners.MockitoJUnitRunner; import biz.netcentric.vlt.upgrade.UpgradeInfo.InstallationMode; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; import biz.netcentric.vlt.upgrade.handler.UpgradeHandler; @RunWith(MockitoJUnitRunner.class) @@ -59,7 +58,9 @@ public void testConstructorDefaults() throws Exception { try { sling.build().resource("/test"); Node node = session.getNode("/test"); - new UpgradeInfo(ctx, status, node); + UpgradeInfo upgradeInfo = new UpgradeInfo(ctx, status, node); + upgradeInfo.loadActions(ctx); + Assert.fail("Exception expected"); } catch (IllegalStateException e) { Assert.assertNotNull(e); @@ -70,6 +71,7 @@ public void testConstructorDefaults() throws Exception { Node node = session.getNode("/test"); Mockito.when(ctx.getPackage().getId().getVersionString()).thenReturn("1.0.0"); UpgradeInfo info = new UpgradeInfo(ctx, status, node); + info.loadActions(ctx); Assert.assertSame(node, info.getNode()); Assert.assertSame(status, info.getStatus()); Assert.assertEquals(UpgradeInfo.DEFAULT_INSTALLATION_MODE, info.getInstallationMode().toString()); @@ -89,6 +91,7 @@ public void testConstructor() throws Exception { UpgradeInfo.PN_HANDLER, TEST_HANDLER); Node node = session.getNode("/test"); UpgradeInfo info = new UpgradeInfo(ctx, status, node); + info.loadActions(ctx); Assert.assertEquals(InstallationMode.ALWAYS, info.getInstallationMode()); Assert.assertEquals(Phase.PREPARE, info.getDefaultPhase()); @@ -105,7 +108,7 @@ public boolean isAvailable(InstallContext ctx) { } @Override - public Iterable create(InstallContext ctx, UpgradeActionInfo info) { + public Iterable create(InstallContext ctx, UpgradeInfo info) { UpgradeAction action = Mockito.mock(UpgradeAction.class); Mockito.when(action.getPhase()).thenReturn(PHASE); return Arrays.asList(action); diff --git a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeProcessorTest.java b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeProcessorTest.java index 019dcc0..ef29cdc 100644 --- a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeProcessorTest.java +++ b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeProcessorTest.java @@ -35,7 +35,6 @@ import org.mockito.runners.MockitoJUnitRunner; import biz.netcentric.vlt.upgrade.UpgradeInfo.InstallationMode; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; import biz.netcentric.vlt.upgrade.handler.UpgradeHandler; @RunWith(MockitoJUnitRunner.class) @@ -183,7 +182,7 @@ public boolean isAvailable(InstallContext ctx) { } @Override - public Iterable create(InstallContext ctx, UpgradeActionInfo info) { + public Iterable create(InstallContext ctx, UpgradeInfo info) { return Arrays.asList(action); } diff --git a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandlerTest.java b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandlerTest.java index 5b5be4d..e50bf9b 100644 --- a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandlerTest.java +++ b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandlerTest.java @@ -32,8 +32,8 @@ import com.icfolson.aem.groovy.console.GroovyConsoleService; import biz.netcentric.vlt.upgrade.UpgradeAction; +import biz.netcentric.vlt.upgrade.UpgradeInfo; import biz.netcentric.vlt.upgrade.handler.OsgiUtil; -import biz.netcentric.vlt.upgrade.handler.UpgradeActionInfo; import biz.netcentric.vlt.upgrade.testUtils.LoggerStub; @RunWith(Enclosed.class) @@ -63,13 +63,13 @@ public static final class Create extends GroovyConsoleHandlerAbstractTest { @Test public void shouldReturnGroovyScriptActions() throws RepositoryException { - List upgradeActions = groovyConsoleHandler.create(installContext, new UpgradeActionInfo(node, null)); + List upgradeActions = groovyConsoleHandler.create(installContext, info); assertThat(upgradeActions).hasSize(2); } @Test public void shouldReturnOnlyGroovyScripts() throws RepositoryException { - List upgradeActions = groovyConsoleHandler.create(installContext, new UpgradeActionInfo(node, null)); + List upgradeActions = groovyConsoleHandler.create(installContext, info); for (UpgradeAction action : upgradeActions) { assertThat(action.getName()).endsWith(".groovy"); @@ -86,11 +86,12 @@ public static abstract class GroovyConsoleHandlerAbstractTest { OsgiUtil osgi; InstallContextImpl installContext; Node node; + UpgradeInfo info; @Before public void setUp() throws RepositoryException, UnsupportedEncodingException { groovyConsoleHandler = new GroovyConsoleHandler(); - GroovyConsoleHandler.LOG = new LoggerStub(); + GroovyConsoleHandler.log = new LoggerStub(); osgi = Mockito.spy(new OsgiUtil()); groovyConsoleHandler.osgi = osgi; @@ -106,6 +107,7 @@ public void setUp() throws RepositoryException, UnsupportedEncodingException { node = sling.resourceResolver().getResource("/some/resource").adaptTo(Node.class); installContext = new InstallContextImpl(node, null, null, null); + info = new UpgradeInfo(installContext, null, node); } } } diff --git a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/testUtils/LoggerStub.java b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/testUtils/LoggerStub.java index a5ed446..949eea1 100644 --- a/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/testUtils/LoggerStub.java +++ b/vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/testUtils/LoggerStub.java @@ -1,10 +1,23 @@ +/* + * (C) Copyright 2016 Netcentric AG. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ package biz.netcentric.vlt.upgrade.testUtils; import org.apache.jackrabbit.vault.packaging.InstallContext; import biz.netcentric.vlt.upgrade.util.PackageInstallLogger; -public class LoggerStub implements PackageInstallLogger { +public class LoggerStub extends PackageInstallLogger { + + public LoggerStub() { + super(null); + } + @Override public void debug(InstallContext ctx, String format, Object... arguments) {