-
Notifications
You must be signed in to change notification settings - Fork 43
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
add a create/update/changeId/delete posthook (#193) #242
Changes from 2 commits
6d51dfe
2f2ecb6
68cd544
d9271d4
45d8a32
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 |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
**************************************************************************** | ||
* Ldap Synchronization Connector provides tools to synchronize | ||
* electronic identities from a list of data sources including | ||
* any database with a JDBC connector, another LDAP directory, | ||
* flat files... | ||
* | ||
* ==LICENSE NOTICE== | ||
* | ||
* Copyright (c) 2008 - 2011 LSC Project | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
|
||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the LSC Project nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* ==LICENSE NOTICE== | ||
* | ||
* (c) 2008 - 2011 LSC Project | ||
**************************************************************************** | ||
*/ | ||
package org.lsc; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import java.lang.ProcessBuilder; | ||
import java.io.OutputStream; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.PrintWriter; | ||
import org.lsc.utils.output.LdifLayout; | ||
import com.fasterxml.jackson.databind.ObjectMapper; // For encoding object to JSON | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
|
||
|
||
/** | ||
* This object is managing posthook scripts | ||
*/ | ||
public class Hooks { | ||
|
||
static final Logger LOGGER = LoggerFactory.getLogger(AbstractSynchronize.class); | ||
/** | ||
* Method calling a postSyncHook if necessary | ||
* | ||
* return nothing | ||
*/ | ||
public final static void postSyncHook(final String hook, final String outputFormat, final LscModifications lm) { | ||
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. style: in general in Java we don't use static methods. You can construct the object in the constructor of the caller. |
||
|
||
if( hook != null && ! hook.equals("") ) | ||
{ | ||
|
||
String format = ""; | ||
if( outputFormat.equals("json") ) { | ||
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. the parsing of the outputFormat should be done previously, here you should be able to manipulate only an Enum |
||
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); | ||
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. these lines can be easily factorized, and in this case easily integrated in the callHook method |
||
break; | ||
|
||
case UPDATE_OBJECT: | ||
if( format.equals("json") ) { | ||
modifications = getJsonModifications(lm); | ||
} | ||
else { | ||
modifications = LdifLayout.format(lm); | ||
} | ||
callHook("update", hook, lm.getMainIdentifier(), format, modifications); | ||
break; | ||
|
||
case CHANGE_ID: | ||
if( format.equals("json") ) { | ||
modifications = getJsonModifications(lm); | ||
} | ||
else { | ||
modifications = LdifLayout.format(lm); | ||
} | ||
callHook("changeId", hook, lm.getMainIdentifier(), format, modifications); | ||
break; | ||
|
||
case DELETE_OBJECT: | ||
callHook("delete", hook, lm.getMainIdentifier(), format, modifications); | ||
break; | ||
|
||
default: | ||
LOGGER.info("Error: unknown operation for posthook {}", hook); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Method calling the hook | ||
* | ||
* return nothing | ||
*/ | ||
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. I'm not sure such comments are really useful |
||
public final static void callHook( String operationType, | ||
String hook, | ||
String identifier, | ||
String format, | ||
String modifications) { | ||
|
||
LOGGER.info("Calling {} posthook {} with format {} for {}", operationType, hook, format, identifier); | ||
try { | ||
if( modifications != null ) { | ||
Process p = new ProcessBuilder( | ||
hook, | ||
identifier, | ||
operationType) | ||
.start(); | ||
|
||
// sends ldif modifications to stdin of hook script | ||
OutputStream stdin = p.getOutputStream(); | ||
stdin.write(modifications.getBytes()); | ||
stdin.write("\n".getBytes()); | ||
stdin.flush(); | ||
stdin.close(); | ||
} | ||
else { | ||
Process p = new ProcessBuilder( | ||
hook, | ||
identifier, | ||
operationType) | ||
.start(); | ||
} | ||
} | ||
catch(IOException e) { | ||
StringWriter sw = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(sw); | ||
e.printStackTrace(pw); | ||
LOGGER.error("Error while calling {} posthook {} with format {} for {}: {}", | ||
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. do you know that the LOGGER.error can directly take an exception as a parameter, and so log the stack trace without all this stuff? |
||
operationType, hook, format, identifier, sw.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Method computing modifications as json | ||
* | ||
* @return modifications in a json String | ||
*/ | ||
public final static String getJsonModifications(final LscModifications lm) { | ||
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); | ||
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. nits (perf): it's recommended to declare your ObjectMapper as static for performace reasons. |
||
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()); | ||
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. same remark about stack trace logging |
||
} | ||
return json; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,60 @@ public String getCondition(LscModificationType operation) { | |
} | ||
return result; | ||
} | ||
|
||
public String getPostHookOutputFormat() { | ||
if (conf.getHooks() == null || conf.getHooks().getOutputFormat() == null) { | ||
return ""; | ||
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. As said earlier, the result of this method should be an enum. |
||
} | ||
return conf.getHooks().getOutputFormat(); | ||
} | ||
|
||
public String getCreatePostHook() { | ||
if (conf.getHooks() == null || conf.getHooks().getCreatePostHook() == null) { | ||
return ""; | ||
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. style: I usually prefer returning an Optional to indicate there is a value or not. But here I did not check what are the consequences, it can be a little more complicated to handle if you are not used to. |
||
} | ||
return conf.getHooks().getCreatePostHook(); | ||
} | ||
|
||
public String getDeletePostHook() { | ||
if (conf.getHooks() == null || conf.getHooks().getDeletePostHook() == null) { | ||
return ""; | ||
} | ||
return conf.getHooks().getDeletePostHook(); | ||
} | ||
|
||
public String getUpdatePostHook() { | ||
if (conf.getHooks() == null || conf.getHooks().getUpdatePostHook() == null) { | ||
return ""; | ||
} | ||
return conf.getHooks().getUpdatePostHook(); | ||
} | ||
|
||
public String getChangeIdPostHook() { | ||
if (conf.getHooks() == null || conf.getHooks().getChangeIdPostHook() == null) { | ||
return ""; | ||
} | ||
return conf.getHooks().getChangeIdPostHook(); | ||
} | ||
|
||
public String getPostHook(LscModificationType operation) { | ||
String result = ""; | ||
switch (operation) { | ||
case CREATE_OBJECT: | ||
result = this.getCreatePostHook(); | ||
break; | ||
case UPDATE_OBJECT: | ||
result = this.getUpdatePostHook(); | ||
break; | ||
case DELETE_OBJECT: | ||
result = this.getDeletePostHook(); | ||
break; | ||
case CHANGE_ID: | ||
result = this.getChangeIdPostHook(); | ||
break; | ||
} | ||
return result; | ||
} | ||
|
||
public String getDelimiter(String name) { | ||
DatasetType dataset = LscConfiguration.getDataset(conf, name); | ||
|
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.
could you put the common version in a property?
(by the way do you really need these 3 dependencies?)