Skip to content

Commit

Permalink
Netcentric#9: extract logger to interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
otarsko committed Oct 19, 2017
1 parent 1598f64 commit 9eab556
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 92 deletions.
6 changes: 6 additions & 0 deletions vault-upgrade-hook/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,11 @@
<version>1.7.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
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
* and holds the configuration.
*/
public class UpgradeInfo {

private static final PackageInstallLogger LOG = PackageInstallLogger.create(UpgradeInfo.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UpgradeInfo.class);

/**
* This property configures the handler to execute this {@link UpgradeInfo}s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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 <b>vault-upgrade-hook</b>
Expand All @@ -38,7 +39,7 @@
*/
public class UpgradeProcessor implements InstallHook {

private static final PackageInstallLogger LOG = PackageInstallLogger.create(UpgradeProcessor.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UpgradeProcessor.class);

/**
* Absolute path where information of the {@link UpgradeInfo} execution are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
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
* the current execution with the former.
*/
public class UpgradeStatus {

private static final PackageInstallLogger LOG = PackageInstallLogger.create(UpgradeStatus.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UpgradeStatus.class);

public static final String PN_UPGRADE_TIME = "time";
public static final String PN_VERSION = "version";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import biz.netcentric.vlt.upgrade.handler.OsgiUtil;
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
Expand All @@ -33,7 +34,7 @@
*/
public class GroovyConsoleHandler implements UpgradeHandler {

private static final PackageInstallLogger LOG = PackageInstallLogger.create(GroovyConsoleHandler.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(GroovyConsoleHandler.class);

OsgiUtil osgi = new OsgiUtil();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
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 = PackageInstallLogger.create(GroovyScript.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(GroovyScript.class);

SlingUtils sling = new SlingUtils();
private final Node script;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

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 = PackageInstallLogger.create(Script.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(Script.class);

private final SlingScript delegate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import biz.netcentric.vlt.upgrade.handler.SlingUtils;
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 = PackageInstallLogger.create(ScriptHandler.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(ScriptHandler.class);

SlingUtils sling = new SlingUtils();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
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 = PackageInstallLogger.create(SlingPipe.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(SlingPipe.class);

OsgiUtil osgi = new OsgiUtil();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import biz.netcentric.vlt.upgrade.handler.SlingUtils;
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 = PackageInstallLogger.create(SlingPipesHandler.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(SlingPipesHandler.class);

SlingUtils sling = new SlingUtils();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
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 = PackageInstallLogger.create(UserPreferencesHandler.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UserPreferencesHandler.class);

@Override
public boolean isAvailable(InstallContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

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 = PackageInstallLogger.create(UserPreferencesUpgradeAction.class);
private static final PackageInstallLogger LOG = PackageInstallLoggerImpl.create(UserPreferencesUpgradeAction.class);

private final Node xmlFileNode;
private final String userId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,90 +1,15 @@
/*
* (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 PackageInstallLogger {
private final Logger base;
public interface PackageInstallLogger {
void debug(InstallContext ctx, String format, Object... arguments);

public PackageInstallLogger(Logger log) {
base = log;
}
void info(InstallContext ctx, String format, Object... arguments);

public static PackageInstallLogger create(Class<?> clazz) {
return new PackageInstallLogger(LoggerFactory.getLogger(clazz));
}
void status(InstallContext ctx, String format, String path, 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 warn(InstallContext ctx, String format, Object... arguments);

void error(InstallContext ctx, String format, Object... arguments);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* (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;
}

}

0 comments on commit 9eab556

Please sign in to comment.