[spring-extensions] branch master updated: JSE-25 Add RunnableResource and ScriptedRunnable
Rod Widdowson
rdw at steadingsoftware.com
Mon Aug 7 06:00:36 EDT 2017
This is an automated email from the git hooks/post-receive script.
rdw pushed a commit to branch master
in repository spring-extensions.
View the commit online:
http://git.shibboleth.net/view/?p=spring-extensions.git;a=commit;h=ca2681e70403b20e5515e958bfbefaadaec57969
The following commit(s) were added to refs/heads/master by this push:
new ca2681e JSE-25 Add RunnableResource and ScriptedRunnable
ca2681e is described below
commit ca2681e70403b20e5515e958bfbefaadaec57969
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Mon Aug 7 10:55:37 2017 +0100
JSE-25 Add RunnableResource and ScriptedRunnable
https://issues.shibboleth.net/jira/browse/JSE-25
Add a new Resource class which is but a thin veneer on the Spring
FileSystem one. At initialization, when "lastModified", "exists"
and "getInputStream" are called a java.lang.Runnable is run.
This means that the runnable can be used to provoke any changes
(for instance running "svn update").
Add a ScriptedRunnable to allow scripting of this. Plus tests.
---
.../resource/RunnableFileSystemResource.java | 130 ++++++++++++++
.../ext/spring/resource/ScriptedRunnable.java | 143 +++++++++++++++
.../ext/spring/resource/RunnableResourceTest.java | 200 +++++++++++++++++++++
3 files changed, 473 insertions(+)
diff --git a/src/main/java/net/shibboleth/ext/spring/resource/RunnableFileSystemResource.java b/src/main/java/net/shibboleth/ext/spring/resource/RunnableFileSystemResource.java
new file mode 100644
index 0000000..cd57c53
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/resource/RunnableFileSystemResource.java
@@ -0,0 +1,130 @@
+/*
+ * 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.ext.spring.resource;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.annotation.Nonnull;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.BeanCreationException;
+import org.springframework.core.io.FileSystemResource;
+
+import net.shibboleth.utilities.java.support.annotation.ParameterName;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.resource.Resource;
+
+/**
+ * A file backed resource which calls out to a provided {@link Runnable} as appropriate such that the resource becomes
+ * reloadable.
+ */
+public class RunnableFileSystemResource extends FileSystemResource
+ implements Resource, org.springframework.core.io.Resource {
+
+ /** What to run at the appropriate time. */
+ @Nonnull private final Runnable theRunnable;
+
+ /** The log. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(RunnableFileSystemResource.class);
+
+ /** Log prefix. */
+ @Nonnull @NotEmpty private final String thePrefix;
+
+ /**
+ * Constructor.
+ *
+ * @param file The file to back.
+ * @param runnable a {@link Runnable} to call at appropriate times
+ */
+ public RunnableFileSystemResource(@Nonnull @ParameterName(name = "file") final File file,
+ @Nonnull @ParameterName(name = "runnable") final Runnable runnable) {
+ super(Constraint.isNotNull(file, "File parameter to RunnableFileSystemResource cannot be null"));
+ theRunnable = Constraint.isNotNull(runnable, "Runnable parameter to RunnableFileSystemResource cannot be null");
+ thePrefix = "RunnableResource [" + getPath() + "]";
+
+ try {
+ callRunnable();
+ } catch (final IOException ex) {
+ throw new BeanCreationException(ex.getMessage());
+ }
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param path the path to the file to look at.
+ * @param runnable a {@link Runnable} to call at appropriate times
+ */
+ public RunnableFileSystemResource(@Nonnull @NotEmpty @ParameterName(name = "path") final String path,
+ @Nonnull @ParameterName(name = "runnable") final Runnable runnable) {
+ super(Constraint.isNotEmpty(path, "Path parameter to RunnableFileSystemResource cannot be null"));
+ theRunnable = Constraint.isNotNull(runnable, "Runnable parameter to RunnableFileSystemResource cannot be null");
+ thePrefix = "RunnableResource [" + getPath() + "]";
+
+ try {
+ callRunnable();
+ } catch (final IOException ex) {
+ throw new BeanCreationException(ex.getMessage());
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override public RunnableFileSystemResource createRelativeResource(final String relativePath) throws IOException {
+ return new RunnableFileSystemResource(super.createRelative(relativePath).getFile(), theRunnable);
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean exists() {
+ try {
+ callRunnable();
+ } catch (final IOException e) {
+ return false;
+ }
+ return super.exists();
+ }
+
+ /** {@inheritDoc} */
+ @Override public InputStream getInputStream() throws IOException {
+ callRunnable();
+ return super.getInputStream();
+ }
+
+ /** {@inheritDoc} */
+ @Override public long lastModified() throws IOException {
+ callRunnable();
+ return super.lastModified();
+ }
+
+ /**
+ * Call the runnable and catch every event thrown.
+ *
+ * @throws IOException if anything bad happens
+ */
+ protected void callRunnable() throws IOException {
+ try {
+ theRunnable.run();
+ } catch (final Exception ex) {
+ log.error("{} : Runnable failed", thePrefix, ex);
+ throw new IOException(ex);
+ }
+ }
+}
diff --git a/src/main/java/net/shibboleth/ext/spring/resource/ScriptedRunnable.java b/src/main/java/net/shibboleth/ext/spring/resource/ScriptedRunnable.java
new file mode 100644
index 0000000..5fcc33d
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/resource/ScriptedRunnable.java
@@ -0,0 +1,143 @@
+/*
+ * 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.ext.spring.resource;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.script.ScriptContext;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.component.UnmodifiableComponent;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator;
+import net.shibboleth.utilities.java.support.scripting.EvaluableScript;
+
+/**
+ * A Runnable which executes a script.
+ */
+public class ScriptedRunnable extends AbstractIdentifiableInitializableComponent
+ implements Runnable, UnmodifiableComponent {
+
+ /** What is run. */
+ @NonnullAfterInit private EvaluableScript script;
+
+ /** Evaluator. */
+ @NonnullAfterInit private RunnableScriptEvaluator scriptEvaluator;
+
+ /** The log. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedRunnable.class);
+
+ /** Custom object for script. */
+ @Nullable private Object customObject;
+
+ /** {@inheritDoc} */
+ @Override protected void doInitialize() throws ComponentInitializationException {
+ super.doInitialize();
+
+ if (null == script) {
+ throw new ComponentInitializationException("No script has been provided");
+ }
+
+ scriptEvaluator = new RunnableScriptEvaluator(script);
+ scriptEvaluator.setCustomObject(customObject);
+
+ final StringBuilder builder = new StringBuilder("ScriptedRunnable '").append(getId()).append("':");
+ scriptEvaluator.setLogPrefix(builder.toString());
+ }
+
+ /**
+ * Return the custom (externally provided) object.
+ *
+ * @return the custom object
+ */
+ @Nullable public Object getCustomObject() {
+ return customObject;
+ }
+
+ /**
+ * Set the custom (externally provided) object.
+ *
+ * @param object the custom object
+ */
+ public void setCustomObject(@Nullable final Object object) {
+ ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+ ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+
+ customObject = object;
+ }
+
+ /**
+ * Gets the script to be evaluated.
+ *
+ * @return the script to be evaluated
+ */
+ @NonnullAfterInit public EvaluableScript getScript() {
+ return script;
+ }
+
+ /**
+ * Sets the script to be evaluated.
+ *
+ * @param matcherScript the script to be evaluated
+ */
+ public void setScript(@Nonnull final EvaluableScript matcherScript) {
+ ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+ ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+
+ script = Constraint.isNotNull(matcherScript, "Attribute value matching script cannot be null");
+ }
+
+ /** {@inheritDoc} */
+ @Override public void run() {
+ scriptEvaluator.execute();
+ }
+
+ /**
+ * The thing that runs the script.
+ */
+ private class RunnableScriptEvaluator extends AbstractScriptEvaluator {
+
+ /**
+ * Constructor.
+ *
+ * @param theScript the script we will evaluate.
+ */
+ public RunnableScriptEvaluator(@Nonnull final EvaluableScript theScript) {
+ super(theScript);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void prepareContext(final ScriptContext scriptContext, final Object... input) {
+ // Nothing to do
+ }
+
+ /**
+ * Run the script. Logging as appropriate.
+ */
+ public void execute() {
+ log.debug("{}: running script", getLogPrefix());
+ evaluate((Object[]) null);
+ }
+ }
+}
diff --git a/src/test/java/net/shibboleth/ext/spring/resource/RunnableResourceTest.java b/src/test/java/net/shibboleth/ext/spring/resource/RunnableResourceTest.java
new file mode 100644
index 0000000..f94ce34
--- /dev/null
+++ b/src/test/java/net/shibboleth/ext/spring/resource/RunnableResourceTest.java
@@ -0,0 +1,200 @@
+/*
+ * 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.ext.spring.resource;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.script.ScriptException;
+
+import org.springframework.core.io.Resource;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.scripting.EvaluableScript;
+
+/**
+ *
+ */
+public class RunnableResourceTest {
+
+ private String fileName;
+
+ private CustomObject object;
+
+ @BeforeClass public void setupClient() throws Exception {
+ final File file = File.createTempFile("RunnableResourceTest", ".xml");
+ fileName = file.getAbsolutePath();
+ object = new CustomObject();
+ }
+
+ @AfterClass public void deleteFile() {
+ final File f = new File(fileName);
+ if (f.exists()) {
+ f.delete();
+ }
+ }
+
+ private byte getValue(final Resource resource) throws IOException {
+ InputStream io = null;
+ try {
+ io = resource.getInputStream();
+ return (byte) io.read();
+ }
+ finally {
+ if ( null != io) {
+ io.close();
+ }
+ }
+ }
+
+ @BeforeMethod public void reset() {
+ object.reset();
+ }
+
+ @Test public void testCustomObject() throws IOException {
+ Assert.assertFalse(object.wasUppdated());
+ Assert.assertFalse(object.wasUppdated());
+ Assert.assertTrue(object.isValid((byte) 0));
+ Assert.assertFalse(object.isValid((byte) 1));
+ object.update();
+ Assert.assertTrue(object.wasUppdated());
+ Assert.assertTrue(object.isValid((byte) 0));
+ Assert.assertTrue(object.isValid((byte) 1));
+ Assert.assertFalse(object.isValid((byte) 0));
+ }
+
+
+ @Test public void testRunnable() throws ScriptException, ComponentInitializationException {
+ final EvaluableScript script = new EvaluableScript("custom.update();");
+ final ScriptedRunnable runnable = new ScriptedRunnable();
+ runnable.setCustomObject(object);
+ runnable.setScript(script);
+ runnable.setId("Runnable");
+ runnable.initialize();
+
+ Assert.assertFalse(object.wasUppdated());
+ Assert.assertFalse(object.wasUppdated());
+ Assert.assertTrue(object.isValid((byte) 0));
+ Assert.assertFalse(object.isValid((byte) 1));
+ runnable.run();
+ Assert.assertTrue(object.wasUppdated());
+ Assert.assertTrue(object.isValid((byte) 0));
+ Assert.assertTrue(object.isValid((byte) 1));
+ Assert.assertFalse(object.isValid((byte) 0));
+
+ }
+
+ @Test public void testResource() throws ScriptException, ComponentInitializationException, IOException, InterruptedException {
+
+ final long now = System.currentTimeMillis();
+ final EvaluableScript script = new EvaluableScript("custom.update();");
+ final ScriptedRunnable runnable = new ScriptedRunnable();
+ runnable.setCustomObject(object);
+ runnable.setScript(script);
+ runnable.setId("Runnable");
+ runnable.initialize();
+
+ Assert.assertFalse(object.wasUppdated());
+ Assert.assertFalse(object.wasUppdated());
+ Assert.assertTrue(object.isValid((byte) 0));
+
+ final Resource resource = new RunnableFileSystemResource(fileName, runnable);
+ Assert.assertTrue(object.wasUppdated());
+ Assert.assertTrue(object.isValid((byte) 1));
+ Assert.assertTrue(object.isValid(getValue(resource))); // GetValue increments
+ Assert.assertTrue(object.isValid((byte) 2));
+
+ Assert.assertTrue(resource.exists()); // exists increments
+ Assert.assertTrue(object.wasUppdated());
+ Assert.assertTrue(object.isValid(getValue(resource))); // GetValue increments
+ Assert.assertTrue(object.isValid((byte) 4));
+
+ Thread.sleep(10);
+ final long modified = resource.lastModified(); // lastModified Increments
+ Assert.assertTrue(object.wasUppdated());
+ Assert.assertTrue(object.isValid(getValue(resource)));// GetValue increments
+ Assert.assertTrue(object.isValid((byte) 6));
+
+ Assert.assertTrue(modified > now);
+ Thread.sleep(10);
+ Assert.assertTrue(modified < System.currentTimeMillis());
+
+ }
+
+
+
+ public class CustomObject {
+
+ private final File theFile;
+
+ private byte count, lastCheck;
+
+ private boolean updated;
+
+ public CustomObject() {
+ theFile = new File(fileName);
+ }
+
+ public void update() throws IOException {
+ FileOutputStream io = null;
+ try {
+ io = new FileOutputStream(theFile);
+
+ io.write(count);
+
+ if (count == 127 ) {
+ throw new IOException("Count wrapped");
+ }
+ count++;
+ updated = true;
+ } finally {
+ if (null != io) {
+ io.close();
+ }
+ }
+ }
+
+ public boolean isValid(final byte what) {
+ if(what >= lastCheck && what <= count) {
+ lastCheck = what;
+ return true;
+ }
+ return false;
+ }
+
+ public boolean wasUppdated() {
+ final boolean result = updated;
+ updated = false;
+ return result;
+ }
+
+ public void reset() {
+ updated = false;
+ count = 0;
+ lastCheck = 0;
+ }
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list