Skip to content

Commit

Permalink
Simplifying CmdConnection#execute
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Oct 22, 2018
1 parent 1d24a98 commit f1e7a4c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

public class CmdConfiguration extends AbstractConfiguration {

public static final String OBJECT_CLASS = "OBJECT_CLASS";

private String createCmdPath = "";

private String updateCmdPath = "";
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/net/tirasa/connid/bundles/cmd/CmdConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package net.tirasa.connid.bundles.cmd;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.identityconnectors.common.Pair;
import org.identityconnectors.common.logging.Log;

public class CmdConnection {
Expand All @@ -35,18 +36,13 @@ public static CmdConnection openConnection() {
private CmdConnection() {
}

public Process execute(final String path, final String[] envp) throws IOException {
LOG.info("Execute script {0} {1}", path, Arrays.asList(envp == null ? new String[0] : envp));
public Process execute(final String path, final List<Pair<String, String>> env) throws IOException {
LOG.info("Execute script {0} {1}", path, env);

ProcessBuilder builder = new ProcessBuilder(path.split(" "));
if (envp != null) {
for (String env : envp) {
String[] split = env.split("=");
if (split == null || split.length < 2) {
LOG.error("Could not parse {}", env);
} else {
builder.environment().put(split[0], split[1]);
}
if (env != null) {
for (Pair<String, String> entry : env) {
builder.environment().put(entry.first, entry.second);
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/main/java/net/tirasa/connid/bundles/cmd/methods/CmdDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package net.tirasa.connid.bundles.cmd.methods;

import java.util.ArrayList;
import java.util.List;
import net.tirasa.connid.bundles.cmd.CmdConfiguration;
import org.identityconnectors.common.Pair;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.framework.common.objects.ObjectClass;
import org.identityconnectors.framework.common.objects.Uid;
Expand All @@ -36,15 +40,18 @@ public CmdDelete(final ObjectClass oc, final String path, final Uid uid) {

public void execDeleteCmd() {
LOG.info("Executing deletion for {0}", uid);

waitFor(exec(scriptPath, createEnv()));
}

private String[] createEnv() {
private List<Pair<String, String>> createEnv() {
LOG.ok("Creating environment for deletion with:");
LOG.ok("ObjectClass: {0}" , oc.getObjectClassValue());
LOG.ok("Environment variable {0}: {1}" , uid.getName(), uid.getUidValue());

return new String[] {"OBJECT_CLASS=" + oc.getObjectClassValue(), uid.getName() + "=" + uid.getUidValue()};
LOG.ok("ObjectClass: {0}", oc.getObjectClassValue());
LOG.ok("Environment variable {0}: {1}", uid.getName(), uid.getUidValue());

List<Pair<String, String>> env = new ArrayList<Pair<String, String>>();
env.add(new Pair<String, String>(CmdConfiguration.OBJECT_CLASS, oc.getObjectClassValue()));
env.add(new Pair<String, String>(uid.getName(), uid.getUidValue()));
return env;
}
}
26 changes: 15 additions & 11 deletions src/main/java/net/tirasa/connid/bundles/cmd/methods/CmdExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.util.List;
import java.util.Set;
import net.tirasa.connid.bundles.cmd.CmdConnection;
import net.tirasa.connid.bundles.cmd.CmdConfiguration;
import org.identityconnectors.common.IOUtil;
import org.identityconnectors.common.Pair;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.common.security.GuardedString;
import org.identityconnectors.framework.common.exceptions.ConnectorException;
Expand All @@ -39,7 +41,7 @@ public CmdExec(final ObjectClass oc) {
this.oc = oc;
}

protected Process exec(final String path, final String[] env) {
protected Process exec(final String path, final List<Pair<String, String>> env) {
try {
return CmdConnection.openConnection().execute(path, env);
} catch (Exception e) {
Expand All @@ -48,20 +50,20 @@ protected Process exec(final String path, final String[] env) {
}
}

protected String[] createEnv(final Set<Attribute> attrs) {
protected List<Pair<String, String>> createEnv(final Set<Attribute> attrs) {
return createEnv(attrs, null);
}

protected String[] createEnv(final Set<Attribute> attrs, final Uid uid) {
final List<String> res = new ArrayList<String>();
protected List<Pair<String, String>> createEnv(final Set<Attribute> attrs, final Uid uid) {
final List<Pair<String, String>> env = new ArrayList<Pair<String, String>>();

LOG.ok("Creating environment with:");
if (oc != null) {
LOG.ok("OBJECT_CLASS: {0}", oc.getObjectClassValue());
res.add("OBJECT_CLASS=" + oc.getObjectClassValue());
LOG.ok(CmdConfiguration.OBJECT_CLASS + ": {0}", oc.getObjectClassValue());
env.add(new Pair<String, String>(CmdConfiguration.OBJECT_CLASS, oc.getObjectClassValue()));
}

for (final Attribute attr : attrs) {
for (Attribute attr : attrs) {
if (attr.getValue() != null && !attr.getValue().isEmpty()) {
LOG.ok("Environment variable {0}: {1}", attr.getName(), attr.getValue().get(0));

Expand All @@ -72,22 +74,24 @@ protected String[] createEnv(final Set<Attribute> attrs, final Uid uid) {

@Override
public void access(char[] clearChars) {
res.add(OperationalAttributes.PASSWORD_NAME + "=" + new String(clearChars));
env.add(new Pair<String, String>(
OperationalAttributes.PASSWORD_NAME, new String(clearChars)));
}
});
}
} else {
res.add(attr.getName() + "=" + IOUtil.join(attr.getValue().toArray(), ','));
env.add(new Pair<String, String>(
attr.getName(), IOUtil.join(attr.getValue().toArray(), ',')));
}
}
}

if (uid != null && AttributeUtil.find(Uid.NAME, attrs) == null) {
LOG.ok("Environment variable {0}: {1}", Uid.NAME, uid.getUidValue());
res.add(Uid.NAME + "=" + uid.getUidValue());
env.add(new Pair<String, String>(Uid.NAME, uid.getUidValue()));
}

return res.toArray(new String[res.size()]);
return env;
}

protected void waitFor(final Process proc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import net.tirasa.connid.bundles.cmd.CmdConfiguration;
import net.tirasa.connid.bundles.cmd.search.Operand;
import org.identityconnectors.common.Pair;
import org.identityconnectors.common.StringUtil;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.common.security.GuardedString;
Expand Down Expand Up @@ -88,17 +90,17 @@ public void execQuery() throws ConnectException {
waitFor(proc);
}

private String[] createEnv() {
final List<String> attributes = new ArrayList<String>();
private List<Pair<String, String>> createEnv() {
List<Pair<String, String>> attributes = new ArrayList<Pair<String, String>>();

LOG.ok("Creating environment for search with:");
LOG.ok("OBJECT_CLASS: {0}", oc.getObjectClassValue());
LOG.ok(CmdConfiguration.OBJECT_CLASS + ": {0}", oc.getObjectClassValue());
LOG.ok("Query filter {0}= {1}", filter.getAttributeName(), filter.getAttributeValue());

attributes.add(filter.getAttributeName() + "=" + filter.getAttributeValue());
attributes.add("OBJECT_CLASS=" + oc.getObjectClassValue());
attributes.add(new Pair<String, String>(filter.getAttributeName(), filter.getAttributeValue()));
attributes.add(new Pair<String, String>(CmdConfiguration.OBJECT_CLASS, oc.getObjectClassValue()));

return attributes.toArray(new String[attributes.size()]);
return attributes;
}

private void fillUserHandler(final String searchScriptOutput) throws ConnectException {
Expand Down

0 comments on commit f1e7a4c

Please sign in to comment.