-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-2401: Introduced ScriptFileResource
- Loading branch information
Showing
6 changed files
with
298 additions
and
1 deletion.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
script/src/main/java/org/jboss/forge/addon/script/resource/ScriptFileResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.script.resource; | ||
|
||
import javax.script.ScriptContext; | ||
import javax.script.ScriptException; | ||
|
||
import org.jboss.forge.addon.resource.FileResource; | ||
|
||
/** | ||
* Handles a JBoss Forge script and provides operations related to it | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public interface ScriptFileResource extends FileResource<ScriptFileResource> | ||
{ | ||
/** | ||
* Executes this script | ||
* | ||
* @return The value returned from the execution of the script. | ||
*/ | ||
Object eval() throws ScriptException; | ||
|
||
/** | ||
* Executes this script using the provided {@link ScriptContext} | ||
* | ||
* @return The value returned from the execution of the script. | ||
*/ | ||
Object eval(ScriptContext context) throws ScriptException; | ||
|
||
/** | ||
* @return the short name of the scripting language | ||
*/ | ||
String getEngineName(); | ||
|
||
/** | ||
* @return the scripting language | ||
*/ | ||
String getEngineLanguageName(); | ||
|
||
/** | ||
* @return the scripting language version | ||
*/ | ||
String getEngineLanguageVersion(); | ||
} |
75 changes: 75 additions & 0 deletions
75
...src/main/java/org/jboss/forge/addon/script/resource/impl/ScriptFileResourceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.script.resource.impl; | ||
|
||
import java.io.File; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
|
||
import org.jboss.forge.addon.resource.Resource; | ||
import org.jboss.forge.addon.resource.ResourceFactory; | ||
import org.jboss.forge.addon.resource.ResourceGenerator; | ||
import org.jboss.forge.addon.script.resource.ScriptFileResource; | ||
|
||
/** | ||
* Generates {@link ScriptFileResource} objects | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class ScriptFileResourceGenerator implements ResourceGenerator<ScriptFileResource, File> | ||
{ | ||
|
||
private ScriptEngineManager manager; | ||
|
||
public ScriptFileResourceGenerator() | ||
{ | ||
manager = new ScriptEngineManager(getClass().getClassLoader()); | ||
} | ||
|
||
@Override | ||
public boolean handles(Class<?> type, Object resource) | ||
{ | ||
boolean result = false; | ||
if (resource instanceof File) | ||
{ | ||
String extension = getFileExtension((File) resource); | ||
result = (extension != null && manager.getEngineByExtension(extension.toLowerCase()) != null); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T extends Resource<File>> T getResource(ResourceFactory factory, Class<ScriptFileResource> type, | ||
File resource) | ||
{ | ||
String fileExtension = getFileExtension(resource); | ||
ScriptEngine engine = manager.getEngineByExtension(fileExtension.toLowerCase()); | ||
return (T) new ScriptFileResourceImpl(factory, resource, engine); | ||
} | ||
|
||
@Override | ||
public <T extends Resource<File>> Class<?> getResourceType(ResourceFactory factory, Class<ScriptFileResource> type, | ||
File resource) | ||
{ | ||
return ScriptFileResource.class; | ||
} | ||
|
||
private String getFileExtension(File file) | ||
{ | ||
String name = file.getName(); | ||
int idx = name.lastIndexOf('.'); | ||
if (idx > -1) | ||
{ | ||
return name.substring(idx + 1); | ||
} | ||
return null; | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
script/src/main/java/org/jboss/forge/addon/script/resource/impl/ScriptFileResourceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.script.resource.impl; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.script.ScriptContext; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptException; | ||
|
||
import org.jboss.forge.addon.resource.AbstractFileResource; | ||
import org.jboss.forge.addon.resource.Resource; | ||
import org.jboss.forge.addon.resource.ResourceFactory; | ||
import org.jboss.forge.addon.script.resource.ScriptFileResource; | ||
|
||
/** | ||
* Default implementation of {@link ScriptFileResource} | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class ScriptFileResourceImpl extends AbstractFileResource<ScriptFileResource> | ||
implements ScriptFileResource | ||
{ | ||
private final ScriptEngine engine; | ||
|
||
public ScriptFileResourceImpl(ResourceFactory factory, File file, ScriptEngine engine) | ||
{ | ||
super(factory, file); | ||
this.engine = engine; | ||
} | ||
|
||
@Override | ||
public Object eval() throws ScriptException | ||
{ | ||
return engine.eval(getContents()); | ||
} | ||
|
||
@Override | ||
public Object eval(ScriptContext context) throws ScriptException | ||
{ | ||
return engine.eval(getContents(), context); | ||
} | ||
|
||
@Override | ||
public String getEngineName() | ||
{ | ||
return engine.getFactory().getEngineName(); | ||
} | ||
|
||
@Override | ||
public String getEngineLanguageName() | ||
{ | ||
return engine.getFactory().getLanguageName(); | ||
} | ||
|
||
@Override | ||
public String getEngineLanguageVersion() | ||
{ | ||
return engine.getFactory().getLanguageVersion(); | ||
} | ||
|
||
@Override | ||
public Resource<File> createFrom(File file) | ||
{ | ||
return new ScriptFileResourceImpl(getResourceFactory(), file, engine); | ||
} | ||
|
||
@Override | ||
protected List<Resource<?>> doListResources() | ||
{ | ||
return Collections.emptyList(); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
script/src/main/resources/META-INF/services/org.jboss.forge.furnace.container.simple.Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.jboss.forge.addon.script.ui.RunCommand | ||
org.jboss.forge.addon.script.impl.ForgeScriptEngineFactory | ||
org.jboss.forge.addon.script.impl.ForgeScriptEngineFactory | ||
org.jboss.forge.addon.script.resource.impl.ScriptFileResourceGenerator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
script/src/test/java/org/jboss/forge/addon/script/resource/ScriptFileResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.script.resource; | ||
|
||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
|
||
import javax.inject.Inject; | ||
import javax.script.ScriptContext; | ||
|
||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.forge.addon.resource.DirectoryResource; | ||
import org.jboss.forge.addon.resource.Resource; | ||
import org.jboss.forge.addon.resource.ResourceFactory; | ||
import org.jboss.forge.addon.script.ScriptContextBuilder; | ||
import org.jboss.forge.addon.script.impl.ForgeScriptEngineFactory; | ||
import org.jboss.forge.furnace.util.OperatingSystemUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class ScriptFileResourceTest | ||
{ | ||
@Inject | ||
private ResourceFactory resourceFactory; | ||
|
||
@Inject | ||
private ForgeScriptEngineFactory engineFactory; | ||
|
||
@Test | ||
public void testScriptFileResource() throws Exception | ||
{ | ||
File file = File.createTempFile("forgescript", ".fsh"); | ||
file.deleteOnExit(); | ||
Resource<File> resource = resourceFactory.create(file); | ||
Assert.assertThat(resource, instanceOf(ScriptFileResource.class)); | ||
ScriptFileResource scriptResource = (ScriptFileResource) resource; | ||
Assert.assertEquals(engineFactory.getEngineName(), scriptResource.getEngineName()); | ||
Assert.assertEquals(engineFactory.getLanguageName(), scriptResource.getEngineLanguageName()); | ||
Assert.assertEquals(engineFactory.getLanguageVersion(), scriptResource.getEngineLanguageVersion()); | ||
} | ||
|
||
@Test | ||
public void testScriptFileResourceExecution() throws Exception | ||
{ | ||
File file = File.createTempFile("forgescript", ".fsh"); | ||
Files.write(file.toPath(), "touch foo.txt".getBytes()); | ||
file.deleteOnExit(); | ||
|
||
Resource<File> resource = resourceFactory.create(file); | ||
DirectoryResource tmpDir = resourceFactory.create(OperatingSystemUtils.createTempDir()) | ||
.reify(DirectoryResource.class); | ||
tmpDir.deleteOnExit(); | ||
Assert.assertThat(resource, instanceOf(ScriptFileResource.class)); | ||
ScriptFileResource scriptResource = (ScriptFileResource) resource; | ||
ScriptContext context = ScriptContextBuilder.create().currentResource(tmpDir).build(); | ||
Assert.assertThat(tmpDir.getChild("foo.txt").exists(), is(false)); | ||
scriptResource.eval(context); | ||
Assert.assertThat(tmpDir.getChild("foo.txt").exists(), is(true)); | ||
} | ||
|
||
@Test | ||
public void testScriptFileResourceJavascript() throws Exception | ||
{ | ||
File file = File.createTempFile("script", ".js"); | ||
Files.write(file.toPath(), "var a = 1;".getBytes()); | ||
file.deleteOnExit(); | ||
|
||
Resource<File> resource = resourceFactory.create(file); | ||
Assert.assertThat(resource, instanceOf(ScriptFileResource.class)); | ||
ScriptFileResource scriptResource = (ScriptFileResource) resource; | ||
ScriptContext context = ScriptContextBuilder.create().build(); | ||
scriptResource.eval(context); | ||
Assert.assertEquals(1.0D, context.getAttribute("a")); | ||
} | ||
|
||
} |