diff --git a/pom.xml b/pom.xml index 7a0f41e9..02b3b0f5 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,7 @@ ${project.build.directory}/staging 1.5 22.3.1 + 2.9.6 ${project.build.directory}/compiler @@ -872,20 +873,10 @@ xml-apis 1.4.01 - - com.fasterxml.jackson.core - jackson-core - 2.9.6 - - - com.fasterxml.jackson.core - jackson-annotations - 2.9.6 - com.fasterxml.jackson.core jackson-databind - 2.9.6 + ${jackson.version} diff --git a/src/main/java/org/lsc/Hooks.java b/src/main/java/org/lsc/Hooks.java index 16794985..25339d12 100644 --- a/src/main/java/org/lsc/Hooks.java +++ b/src/main/java/org/lsc/Hooks.java @@ -46,9 +46,9 @@ import java.lang.ProcessBuilder; import java.io.OutputStream; import java.io.IOException; -import java.io.StringWriter; -import java.io.PrintWriter; +import java.util.Optional; import org.lsc.utils.output.LdifLayout; +import org.lsc.beans.syncoptions.ISyncOptions.OutputFormat; import com.fasterxml.jackson.databind.ObjectMapper; // For encoding object to JSON import com.fasterxml.jackson.databind.ObjectWriter; @@ -58,62 +58,33 @@ */ public class Hooks { - static final Logger LOGGER = LoggerFactory.getLogger(AbstractSynchronize.class); + static final Logger LOGGER = LoggerFactory.getLogger(Hooks.class); + + private static final ObjectMapper Mapper = new ObjectMapper(); /** * Method calling a postSyncHook if necessary * * return nothing */ - public final static void postSyncHook(final String hook, final String outputFormat, final LscModifications lm) { + public final void postSyncHook(final Optional hook, final OutputFormat outputFormat, final LscModifications lm) { - if( hook != null && ! hook.equals("") ) + if( hook != null && hook.isPresent() ) { - - String format = ""; - if( outputFormat.equals("json") ) { - format = "json"; - } - else - { - format = "ldif"; - } - - // Compute json/ldif modifications - String modifications = null; - switch (lm.getOperation()) { case CREATE_OBJECT: - if( format.equals("json") ) { - modifications = getJsonModifications(lm); - } - else { - modifications = LdifLayout.format(lm); - } - callHook("create", hook, lm.getMainIdentifier(), format, modifications); + callHook("create", hook.get(), lm.getMainIdentifier(), outputFormat, lm); break; case UPDATE_OBJECT: - if( format.equals("json") ) { - modifications = getJsonModifications(lm); - } - else { - modifications = LdifLayout.format(lm); - } - callHook("update", hook, lm.getMainIdentifier(), format, modifications); + callHook("update", hook.get(), lm.getMainIdentifier(), outputFormat, lm); break; case CHANGE_ID: - if( format.equals("json") ) { - modifications = getJsonModifications(lm); - } - else { - modifications = LdifLayout.format(lm); - } - callHook("changeId", hook, lm.getMainIdentifier(), format, modifications); + callHook("changeId", hook.get(), lm.getMainIdentifier(), outputFormat, lm); break; case DELETE_OBJECT: - callHook("delete", hook, lm.getMainIdentifier(), format, modifications); + callHook("delete", hook.get(), lm.getMainIdentifier(), outputFormat, lm); break; default: @@ -122,18 +93,30 @@ public final static void postSyncHook(final String hook, final String outputForm } } - /** - * Method calling the hook - * - * return nothing - */ public final static void callHook( String operationType, String hook, String identifier, - String format, - String modifications) { + OutputFormat outputFormat, + LscModifications lm) { + + LOGGER.info("Calling {} posthook {} with format {} for {}", + operationType, + hook, + outputFormat.toString(), + identifier); + + String modifications = null; + // Compute modifications only in a create / update / changeid operation + if( ! operationType.equals("delete") ) + { + if( outputFormat == OutputFormat.JSON ) { + modifications = getJsonModifications(lm); + } + else { + modifications = LdifLayout.format(lm); + } + } - LOGGER.info("Calling {} posthook {} with format {} for {}", operationType, hook, format, identifier); try { if( modifications != null ) { Process p = new ProcessBuilder( @@ -158,11 +141,12 @@ public final static void callHook( String operationType, } } catch(IOException e) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - LOGGER.error("Error while calling {} posthook {} with format {} for {}: {}", - operationType, hook, format, identifier, sw.toString()); + LOGGER.error("Error while calling {} posthook {} with format {} for {}", + operationType, + hook, + outputFormat.toString(), + identifier); + LOGGER.error("posthook error: ", e); } } @@ -172,16 +156,14 @@ public final static void callHook( String operationType, * @return modifications in a json String */ public final static String getJsonModifications(final LscModifications lm) { - ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + ObjectWriter ow = Mapper.writer().withDefaultPrettyPrinter(); String json = ""; try { json = ow.writeValueAsString(lm.getLscAttributeModifications()); } catch(Exception e) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - LOGGER.error("Error while encoding LSC modifications to json", sw.toString()); + LOGGER.error("Error while encoding LSC modifications to json"); + LOGGER.error("encoding error: ", e); } return json; } diff --git a/src/main/java/org/lsc/beans/syncoptions/ForceSyncOptions.java b/src/main/java/org/lsc/beans/syncoptions/ForceSyncOptions.java index e6c7d82c..920da0d7 100644 --- a/src/main/java/org/lsc/beans/syncoptions/ForceSyncOptions.java +++ b/src/main/java/org/lsc/beans/syncoptions/ForceSyncOptions.java @@ -47,6 +47,7 @@ import java.util.List; import java.util.Set; +import java.util.Optional; import org.lsc.LscModificationType; import org.lsc.configuration.PolicyType; @@ -128,28 +129,28 @@ public String getCondition(LscModificationType operation) { return DEFAULT_CONDITION; } - public String getPostHookOutputFormat() { - return ""; + public OutputFormat getPostHookOutputFormat() { + return OutputFormat.LDIF; } - public String getCreatePostHook() { - return ""; + public Optional getCreatePostHook() { + return Optional.ofNullable(null); } - public String getDeletePostHook() { - return ""; + public Optional getDeletePostHook() { + return Optional.ofNullable(null); } - public String getUpdatePostHook() { - return ""; + public Optional getUpdatePostHook() { + return Optional.ofNullable(null); } - public String getChangeIdPostHook() { - return ""; + public Optional getChangeIdPostHook() { + return Optional.ofNullable(null); } - public String getPostHook(LscModificationType operation) { - return ""; + public Optional getPostHook(LscModificationType operation) { + return Optional.ofNullable(null); } public String getDn() { diff --git a/src/main/java/org/lsc/beans/syncoptions/ISyncOptions.java b/src/main/java/org/lsc/beans/syncoptions/ISyncOptions.java index 76814359..69616755 100644 --- a/src/main/java/org/lsc/beans/syncoptions/ISyncOptions.java +++ b/src/main/java/org/lsc/beans/syncoptions/ISyncOptions.java @@ -47,6 +47,7 @@ import java.util.List; import java.util.Set; +import java.util.Optional; import org.lsc.LscModificationType; import org.lsc.configuration.PolicyType; @@ -60,6 +61,9 @@ public interface ISyncOptions { /** default condition if none is given */ public static final String DEFAULT_CONDITION = "true"; + + /** list of output formats */ + public enum OutputFormat { LDIF, JSON }; /** * Initialize the synchronization options policy. @@ -156,40 +160,40 @@ public interface ISyncOptions { /** * Returns the posthook output format * - * @return the posthook output format or "" if none is specified (default) + * @return the posthook output format (default = OutputFormat.LDIF) */ - String getPostHookOutputFormat(); + OutputFormat getPostHookOutputFormat(); /** * Returns the posthook for a creation * * @return the posthook or "" if none is specified (default) */ - String getCreatePostHook(); + Optional getCreatePostHook(); /** * Returns the posthook for an update * * @return the posthook or "" if none is specified (default) */ - String getUpdatePostHook(); + Optional getUpdatePostHook(); /** * Returns the posthook for a delete * * @return the posthook or "" if none is specified (default) */ - String getDeletePostHook(); + Optional getDeletePostHook(); /** * Returns the posthook for a id change * * @return the posthook or "" if none is specified (default) */ - String getChangeIdPostHook(); + Optional getChangeIdPostHook(); - String getPostHook(LscModificationType operation); + Optional getPostHook(LscModificationType operation); /** * Return the expression used to infer the new object DN diff --git a/src/main/java/org/lsc/beans/syncoptions/PropertiesBasedSyncOptions.java b/src/main/java/org/lsc/beans/syncoptions/PropertiesBasedSyncOptions.java index ba10878e..5ec763e5 100644 --- a/src/main/java/org/lsc/beans/syncoptions/PropertiesBasedSyncOptions.java +++ b/src/main/java/org/lsc/beans/syncoptions/PropertiesBasedSyncOptions.java @@ -49,6 +49,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.Optional; import org.lsc.LscModificationType; import org.lsc.configuration.DatasetType; @@ -197,43 +198,41 @@ public String getCondition(LscModificationType operation) { return result; } - public String getPostHookOutputFormat() { + public OutputFormat getPostHookOutputFormat() { + // default: returns LDIF format if (conf.getHooks() == null || conf.getHooks().getOutputFormat() == null) { - return ""; + return OutputFormat.LDIF; + } + switch(conf.getHooks().getOutputFormat()){ + case "json": + return OutputFormat.JSON; + default: + return OutputFormat.LDIF; } - return conf.getHooks().getOutputFormat(); } - public String getCreatePostHook() { - if (conf.getHooks() == null || conf.getHooks().getCreatePostHook() == null) { - return ""; - } - return conf.getHooks().getCreatePostHook(); + public Optional getCreatePostHook() { + Optional hook = Optional.ofNullable(conf.getHooks().getCreatePostHook()).filter(s -> !s.isEmpty()); + return hook; } - public String getDeletePostHook() { - if (conf.getHooks() == null || conf.getHooks().getDeletePostHook() == null) { - return ""; - } - return conf.getHooks().getDeletePostHook(); + public Optional getDeletePostHook() { + Optional hook = Optional.ofNullable(conf.getHooks().getDeletePostHook()).filter(s -> !s.isEmpty()); + return hook; } - public String getUpdatePostHook() { - if (conf.getHooks() == null || conf.getHooks().getUpdatePostHook() == null) { - return ""; - } - return conf.getHooks().getUpdatePostHook(); + public Optional getUpdatePostHook() { + Optional hook = Optional.ofNullable(conf.getHooks().getUpdatePostHook()).filter(s -> !s.isEmpty()); + return hook; } - public String getChangeIdPostHook() { - if (conf.getHooks() == null || conf.getHooks().getChangeIdPostHook() == null) { - return ""; - } - return conf.getHooks().getChangeIdPostHook(); + public Optional getChangeIdPostHook() { + Optional hook = Optional.ofNullable(conf.getHooks().getChangeIdPostHook()).filter(s -> !s.isEmpty()); + return hook; } - public String getPostHook(LscModificationType operation) { - String result = ""; + public Optional getPostHook(LscModificationType operation) { + Optional result = Optional.ofNullable(null); switch (operation) { case CREATE_OBJECT: result = this.getCreatePostHook(); diff --git a/src/main/java/org/lsc/runnable/CleanEntryRunner.java b/src/main/java/org/lsc/runnable/CleanEntryRunner.java index 61563291..0ed8c521 100644 --- a/src/main/java/org/lsc/runnable/CleanEntryRunner.java +++ b/src/main/java/org/lsc/runnable/CleanEntryRunner.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import javax.naming.CommunicationException; @@ -24,6 +25,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.lsc.Hooks; +import org.lsc.beans.syncoptions.ISyncOptions.OutputFormat; /** * @author sfroger @@ -110,9 +112,10 @@ public void run() { // do it! if (task.getDestinationService().apply(lm)) { // Retrieve posthook for the current operation - String hook = syncOptions.getDeletePostHook(); - String outputFormat = syncOptions.getPostHookOutputFormat(); - Hooks.postSyncHook(hook, outputFormat, lm); + Optional hook = syncOptions.getDeletePostHook(); + OutputFormat outputFormat = syncOptions.getPostHookOutputFormat(); + Hooks hookObject = new Hooks(); + hookObject.postSyncHook(hook, outputFormat, lm); counter.incrementCountCompleted(); abstractSynchronize.logAction(lm, id, task.getName()); } else { diff --git a/src/main/java/org/lsc/runnable/SynchronizeEntryRunner.java b/src/main/java/org/lsc/runnable/SynchronizeEntryRunner.java index a07457bb..d2c21ffc 100644 --- a/src/main/java/org/lsc/runnable/SynchronizeEntryRunner.java +++ b/src/main/java/org/lsc/runnable/SynchronizeEntryRunner.java @@ -3,6 +3,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import org.lsc.AbstractSynchronize; import org.lsc.beans.InfoCounter; @@ -17,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.lsc.Hooks; +import org.lsc.beans.syncoptions.ISyncOptions.OutputFormat; /** * @author sbahloul @@ -139,9 +141,10 @@ public boolean run(IBean entry) { // if we got here, we have a modification to apply - let's do it! if (task.getDestinationService().apply(lm)) { // Retrieve posthook for the current operation - String hook = task.getSyncOptions().getPostHook(modificationType); - String outputFormat = task.getSyncOptions().getPostHookOutputFormat(); - Hooks.postSyncHook(hook, outputFormat, lm); + Optional hook = task.getSyncOptions().getPostHook(modificationType); + OutputFormat outputFormat = task.getSyncOptions().getPostHookOutputFormat(); + Hooks hookObject = new Hooks(); + hookObject.postSyncHook(hook, outputFormat, lm); counter.incrementCountCompleted(); abstractSynchronize.logAction(lm, id, syncName); return true;