Skip to content

Instantly share code, notes, and snippets.

@ilgrosso
Created October 10, 2016 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilgrosso/8e442026bc8cf40ad8d307bbef85c140 to your computer and use it in GitHub Desktop.
Save ilgrosso/8e442026bc8cf40ad8d307bbef85c140 to your computer and use it in GitHub Desktop.
package org.apache.syncope.core.provisioning.java.pushpull;
import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import org.apache.syncope.common.lib.patch.AnyPatch;
import org.apache.syncope.common.lib.to.AnyTO;
import org.apache.syncope.common.lib.to.EntityTO;
import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
import org.apache.syncope.core.persistence.api.dao.GroupDAO;
import org.apache.syncope.core.persistence.api.dao.UserDAO;
import org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException;
import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningProfile;
import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport;
import org.apache.syncope.core.provisioning.api.pushpull.PullActions;
import org.identityconnectors.framework.common.objects.SyncDelta;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public class JSPullActions implements PullActions {
protected static final Logger LOG = LoggerFactory.getLogger(JSPullActions.class);
@Autowired
protected AnyTypeDAO anyTypeDAO;
@Autowired
protected UserDAO userDAO;
@Autowired
protected GroupDAO groupDAO;
@Autowired
protected PullUtils pullUtils;
protected Invocable invocable;
public void setScriptEngine(final ScriptEngine engine) {
if (!(engine instanceof Invocable)) {
throw new IllegalArgumentException(
"The provided ScriptEngine is not Invocable :" + engine.getClass().getName());
}
Bindings bindings = new SimpleBindings();
bindings.put("LOG", LOG);
bindings.put("anyTypeDAO", anyTypeDAO);
bindings.put("userDAO", userDAO);
bindings.put("groupDAO", groupDAO);
bindings.put("pullUtils", pullUtils);
engine.getContext().setBindings(bindings, ScriptContext.ENGINE_SCOPE);
this.invocable = (Invocable) engine;
}
@Override
public void beforeAll(final ProvisioningProfile<?, ?> profile) throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
invocable.invokeFunction("beforeAll", profile);
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public <A extends AnyTO, P extends AnyPatch> SyncDelta beforeUpdate(
final ProvisioningProfile<?, ?> profile,
final SyncDelta delta,
final A any,
final P anyMod) throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeUpdate", profile, delta, any, anyMod));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public SyncDelta beforeDelete(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity)
throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeDelete", profile, delta, entity));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public SyncDelta beforeAssign(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity)
throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeAssign", profile, delta, entity));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public SyncDelta beforeProvision(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity)
throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeProvision", profile, delta, entity));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public SyncDelta beforeLink(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity)
throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeLink", profile, delta, entity));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public SyncDelta beforeUnassign(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity)
throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeUnassign", profile, delta, entity));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public SyncDelta beforeDeprovision(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity)
throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeDeprovision", profile, delta, entity));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public SyncDelta beforeUnlink(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity)
throws JobExecutionException {
if (invocable == null) {
throw new JobExecutionException("No ScriptEngine provided");
}
try {
return (SyncDelta) (invocable.invokeFunction("beforeUnlink", profile, delta, entity));
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public void after(
final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity,
final ProvisioningReport result)
throws JobExecutionException {
try {
invocable.invokeFunction("beforeAll", profile, delta, entity);
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public IgnoreProvisionException onError(
final ProvisioningProfile<?, ?> profile,
final SyncDelta delta,
final Exception exception) throws JobExecutionException {
try {
Object result = invocable.invokeFunction("onError", profile, delta, exception);
return result instanceof IgnoreProvisionException
? IgnoreProvisionException.class.cast(result)
: null;
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
@Override
public void afterAll(final ProvisioningProfile<?, ?> profile)
throws JobExecutionException {
try {
invocable.invokeFunction("afterAll", profile);
} catch (ScriptException | NoSuchMethodException e) {
throw new JobExecutionException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment