[java-support] branch master updated: Add a generic ScriptedPredicate.
Scott Cantor
cantor.2 at osu.edu
Mon May 8 14:16:31 EDT 2017
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch master
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=59f81574f97d0b1c49e2b197164e8b5c5b3216de
The following commit(s) were added to refs/heads/master by this push:
new 59f8157 Add a generic ScriptedPredicate.
59f8157 is described below
commit 59f81574f97d0b1c49e2b197164e8b5c5b3216de
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon May 8 14:16:30 2017 -0400
Add a generic ScriptedPredicate.
---
.../java/support/logic/ScriptedPredicate.java | 153 +++++++++++++++++++++
1 file changed, 153 insertions(+)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedPredicate.java b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedPredicate.java
new file mode 100644
index 0000000..2bb4a3f
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedPredicate.java
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.utilities.java.support.logic;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.script.ScriptContext;
+import javax.script.ScriptException;
+
+import net.shibboleth.utilities.java.support.annotation.ParameterName;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator;
+import net.shibboleth.utilities.java.support.scripting.EvaluableScript;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.Resource;
+
+import com.google.common.base.Predicate;
+
+/**
+ * A {@link Predicate} which calls out to a supplied script.
+ *
+ * @param <T> input type
+ */
+public class ScriptedPredicate<T> extends AbstractScriptEvaluator implements Predicate<T> {
+
+ /** Class logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedPredicate.class);
+
+ /**
+ * Constructor.
+ *
+ * @param theScript the script we will evaluate.
+ * @param extraInfo debugging information.
+ */
+ protected ScriptedPredicate(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
+ @Nullable @NotEmpty @ParameterName(name="extraInfo") final String extraInfo) {
+ super(theScript);
+ setOutputType(Boolean.class);
+ setReturnOnError(false);
+ setLogPrefix("Scripted Predicate from " + extraInfo + ":");
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param theScript the script we will evaluate.
+ */
+ protected ScriptedPredicate(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
+ super(theScript);
+ setLogPrefix("Anonymous Scripted Predicate:");
+ setOutputType(Boolean.class);
+ setReturnOnError(false);
+ }
+
+ /**
+ * Set value to return if an error occurs.
+ *
+ * @param flag value to return
+ */
+ public void setReturnOnError(final boolean flag) {
+ setReturnOnError(Boolean.valueOf(flag));
+ }
+
+ /** {@inheritDoc} */
+ public boolean apply(@Nullable final T input) {
+
+ final Object result = evaluate(input);
+ return (boolean) (result != null ? result : getReturnOnError());
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected void prepareContext(@Nonnull final ScriptContext scriptContext, @Nullable final Object... input) {
+ scriptContext.setAttribute("input", input[0], ScriptContext.ENGINE_SCOPE);
+ }
+
+ /**
+ * Factory to create {@link ScriptedPredicate} from a {@link Resource}.
+ *
+ * @param resource the resource to look at
+ * @param engineName the language
+ * @return the predicate
+ * @throws ScriptException if the compile fails
+ * @throws IOException if the file doesn't exist.
+ */
+ static ScriptedPredicate resourceScript(@Nonnull @NotEmpty final String engineName,
+ @Nonnull final Resource resource) throws ScriptException, IOException {
+ try (final InputStream is = resource.getInputStream()) {
+ final EvaluableScript script = new EvaluableScript(engineName, is);
+ return new ScriptedPredicate(script, resource.getDescription());
+ }
+ }
+
+ /**
+ * Factory to create {@link ScriptedPredicate} from a {@link Resource}.
+ *
+ * @param resource the resource to look at
+ * @return the predicate
+ * @throws ScriptException if the compile fails
+ * @throws IOException if the file doesn't exist.
+ */
+ static ScriptedPredicate resourceScript(final Resource resource) throws ScriptException, IOException {
+ return resourceScript(DEFAULT_ENGINE, resource);
+ }
+
+ /**
+ * Factory to create {@link ScriptedPredicate} from inline data.
+ *
+ * @param scriptSource the script, as a string
+ * @param engineName the language
+ * @return the predicate
+ * @throws ScriptException if the compile fails
+ */
+ static ScriptedPredicate inlineScript(@Nonnull @NotEmpty final String engineName,
+ @Nonnull @NotEmpty final String scriptSource) throws ScriptException {
+ final EvaluableScript script = new EvaluableScript(engineName, scriptSource);
+ return new ScriptedPredicate(script, "Inline");
+ }
+
+ /**
+ * Factory to create {@link ScriptedPredicate} from inline data.
+ *
+ * @param scriptSource the script, as a string
+ * @return the predicate
+ * @throws ScriptException if the compile fails
+ */
+ static ScriptedPredicate inlineScript(@Nonnull @NotEmpty final String scriptSource) throws ScriptException {
+ final EvaluableScript script = new EvaluableScript(DEFAULT_ENGINE, scriptSource);
+ return new ScriptedPredicate(script, "Inline");
+ }
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list