diff --git a/pom.xml b/pom.xml index c9dd7036d..06bfc75a6 100755 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,7 @@ hornetq jca tools/forge + script diff --git a/script/Readme.md b/script/Readme.md new file mode 100644 index 000000000..36103073e --- /dev/null +++ b/script/Readme.md @@ -0,0 +1,72 @@ +# Switchyard Script Component +This project provides an implementation.script enabling the usage of the any JVM scripting languge with JSR-223 support in a SwitchYard service implementation. + +_ _ _ + +## Using script "inlined" in SwitchYard + + + + + + + + + + + + 'Hello ' + exchange.message.content + + + + + + + + +The script languge is defined by the attribute *language* of *implementation.script* + +Calling this service is done in the same way as calling any other SwitchYard service, for example: + + String title = (String) newInvoker("OrderService").operation("getTitleForItem").sendInOut(10).getContent(String.class); + +_ _ _ + +## Using external script + + + + + + + + + + + + + + + + +The script can be located on the classpath or in an external file. +The scripting language can be defined by the *language* attribute or is devised from an extension of a scripting file +_ _ _ + +## Message content/Exchange injection +The invoked script has binded one of two variables + +* exchange - representing SwitchYard exchange and available if *injectExchange* attribute of *implementation.script* set to true +* content - content of the incoming message otherwise + + +_ _ _ + diff --git a/script/pom.xml b/script/pom.xml new file mode 100644 index 000000000..4282d0a0d --- /dev/null +++ b/script/pom.xml @@ -0,0 +1,72 @@ + + 4.0.0 + + + org.switchyard.components + switchyard-components-parent + 0.6.0-SNAPSHOT + ../pom.xml + + + switchyard-component-script + jar + + SwitchYard: Script Component + http://switchyard.org + + + + + org.apache.maven.plugins + maven-resources-plugin + + + false + + ${*} + + + + + + + src/test/resources + + **/*.xml + **/*.js + + + + + + + + + org.switchyard + switchyard-config + + + org.switchyard.components + switchyard-component-bean + test + + + org.switchyard + switchyard-test + test + + + org.hamcrest + hamcrest-all + test + + + org.mockito + mockito-all + test + + + + + diff --git a/script/src/main/java/org/switchyard/component/script/config/model/CodeModel.java b/script/src/main/java/org/switchyard/component/script/config/model/CodeModel.java new file mode 100644 index 000000000..d6c1e6824 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/config/model/CodeModel.java @@ -0,0 +1,54 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.config.model; + +import org.switchyard.config.model.NamedModel; + +/** + * Configuration model for a 'code' element containing an in-lined JSR-223 script. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public interface CodeModel extends NamedModel { + + /** + * The script element name. + */ + String CODE = "code"; + + /** + * Gets the script content from the 'code' element. + * + * @return String The script. + */ + public abstract String getCode(); + + /** + * Sets the script code. + * + * @param code The script code to set. + * @return {@link V1CodeModel} this object ref to support method chaining. + */ + public abstract CodeModel setCode(final String code); + +} diff --git a/script/src/main/java/org/switchyard/component/script/config/model/ScriptComponentImplementationModel.java b/script/src/main/java/org/switchyard/component/script/config/model/ScriptComponentImplementationModel.java new file mode 100644 index 000000000..89c1bfb55 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/config/model/ScriptComponentImplementationModel.java @@ -0,0 +1,119 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.config.model; + +import org.switchyard.config.model.composite.ComponentImplementationModel; + +/** + * A definition of an 'implementation.script' element. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public interface ScriptComponentImplementationModel extends ComponentImplementationModel { + + /** + * The 'script' namespace. + */ + public static final String DEFAULT_NAMESPACE = "urn:switchyard-component-script:config:1.0"; + + /** + * The 'script' implementation type. + */ + String SCRIPT = "script"; + + /** + * The 'code' element name. + */ + String CODE = "code"; + + /** + * The 'scriptFile' attribute name. + */ + String SCRIPT_FILE = "scriptFile"; + + /** + * The 'injectExchange' attribute name. + */ + String INJECT_EXCHANGE = "injectExchange"; + + /** + * The 'language' attribute name. + */ + String LANGUAGE = "language"; + + /** + * Gets the JSR-223 script that is the actual script code. + * @return {@link CodeModel} the code configuration model. + */ + CodeModel getCodeModel(); + + /** + * Sets the in-line script model. + * @param codeModel The code configuration model. + * @return {@link ScriptComponentImplementationModel} to enable method chaining. + */ + ScriptComponentImplementationModel setScriptModel(final CodeModel codeModel); + + /** + * Gets the file that is the actual script code. + * @return String the filename of a script to execute. + */ + String getScriptFile(); + + /** + * Sets the scriptFile. + * + * @param scriptFile The script file. + * @return {@link ScriptComponentImplementationModel} to enable method chaining. + */ + ScriptComponentImplementationModel setScriptFile(final String scriptFile); + + /** + * Determines whether the complete Exchange should be injected into the script. + * + * @return true If the Exchange should be injected. + */ + Boolean injectExchange(); + + /** + * Sets the 'injectExchange' property whether the complete Exchange should be injected into the script. + * + * @param enable The value to 'injectExchange to. + * @return {@link ScriptComponentImplementationModel} to enable method chaining. + */ + ScriptComponentImplementationModel setInjectExchange(final Boolean enable); + + /** + * Gets the name of script language. + * @return String the language name. + */ + String getLanguage(); + + /** + * Sets the script language. + * + * @param language The script language. + * @return {@link ScriptComponentImplementationModel} to enable method chaining. + */ + ScriptComponentImplementationModel setLanguage(final String language); +} diff --git a/script/src/main/java/org/switchyard/component/script/config/model/v1/V1CodeModel.java b/script/src/main/java/org/switchyard/component/script/config/model/v1/V1CodeModel.java new file mode 100644 index 000000000..f22427d55 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/config/model/v1/V1CodeModel.java @@ -0,0 +1,56 @@ +package org.switchyard.component.script.config.model.v1; + +import javax.xml.namespace.QName; + +import org.switchyard.component.script.config.model.ScriptComponentImplementationModel; +import org.switchyard.component.script.config.model.CodeModel; +import org.switchyard.config.Configuration; +import org.switchyard.config.model.BaseNamedModel; +import org.switchyard.config.model.Descriptor; + +/** + * A version 1 implementation of a CodeModel. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public class V1CodeModel extends BaseNamedModel implements CodeModel { + + private String _code; + + /** + * No-args constructor. + */ + public V1CodeModel() { + super(new QName(ScriptComponentImplementationModel.DEFAULT_NAMESPACE, CODE)); + } + + /** + * Constructor. + * + * @param config The configuration model. + * @param desc The descriptor for this model. + */ + public V1CodeModel(Configuration config, Descriptor desc) { + super(config, desc); + } + + @Override + public String getCode() { + if (_code != null) { + return _code; + } + + _code = getModelValue(); + return _code; + } + + @Override + public CodeModel setCode(final String code) { + setModelValue(code); + _code = code; + return this; + } + +} diff --git a/script/src/main/java/org/switchyard/component/script/config/model/v1/V1ScriptComponentImplementationModel.java b/script/src/main/java/org/switchyard/component/script/config/model/v1/V1ScriptComponentImplementationModel.java new file mode 100644 index 000000000..aae67b7e0 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/config/model/v1/V1ScriptComponentImplementationModel.java @@ -0,0 +1,130 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.config.model.v1; + +import org.switchyard.component.script.config.model.ScriptComponentImplementationModel; +import org.switchyard.component.script.config.model.CodeModel; +import org.switchyard.config.Configuration; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.composite.v1.V1ComponentImplementationModel; + +/** + * Version 1 implementation of a {@link ScriptComponentImplementationModel}. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public class V1ScriptComponentImplementationModel extends V1ComponentImplementationModel implements ScriptComponentImplementationModel { + + private Boolean _injectExchange; + private String _scriptFile; + private String _language; + private CodeModel _scriptModel; + + /** + * No args constructor that uses the default namespace when constructing + * this model. + */ + public V1ScriptComponentImplementationModel() { + super(SCRIPT, DEFAULT_NAMESPACE); + } + + /** + * Constructor. + * + * @param config The configuration model. + * @param desc The descriptor for the model. + */ + public V1ScriptComponentImplementationModel(final Configuration config, final Descriptor desc) { + super(config, desc); + } + + @Override + public CodeModel getCodeModel() { + if (_scriptModel != null) { + return _scriptModel; + } + + _scriptModel = (CodeModel) getFirstChildModel(CODE); + return _scriptModel; + } + + @Override + public V1ScriptComponentImplementationModel setScriptModel(final CodeModel scriptModel) { + setChildModel(scriptModel); + _scriptModel = scriptModel; + return this; + } + + @Override + public String getScriptFile() { + if (_scriptFile != null) { + return _scriptFile; + } + + _scriptFile = getModelAttribute(SCRIPT_FILE); + return _scriptFile; + } + + @Override + public V1ScriptComponentImplementationModel setScriptFile(final String scriptFile) { + setModelAttribute(SCRIPT_FILE, scriptFile); + _scriptFile = scriptFile; + return this; + } + + @Override + public Boolean injectExchange() { + if (_injectExchange != null) { + return _injectExchange; + } + + _injectExchange = Boolean.valueOf(getModelAttribute(INJECT_EXCHANGE)); + return _injectExchange; + } + + @Override + public ScriptComponentImplementationModel setInjectExchange(final Boolean enable) + { + setModelAttribute(INJECT_EXCHANGE, enable.toString()); + _injectExchange = enable; + return this; + } + + @Override + public String getLanguage() { + if (_language != null) { + return _language; + } + + _language = getModelAttribute(LANGUAGE); + return _language; + } + + @Override + public V1ScriptComponentImplementationModel setLanguage(final String language) { + setModelAttribute(LANGUAGE, language); + _language = language; + return this; + } + +} diff --git a/script/src/main/java/org/switchyard/component/script/config/model/v1/V1ScriptModelMarshaller.java b/script/src/main/java/org/switchyard/component/script/config/model/v1/V1ScriptModelMarshaller.java new file mode 100644 index 000000000..9a6680f86 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/config/model/v1/V1ScriptModelMarshaller.java @@ -0,0 +1,42 @@ +package org.switchyard.component.script.config.model.v1; + +import org.switchyard.component.script.config.model.ScriptComponentImplementationModel; +import org.switchyard.config.Configuration; +import org.switchyard.config.model.BaseMarshaller; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.Model; +import org.switchyard.config.model.composite.ComponentImplementationModel; + +/** + * Version 1 marshaller for a Script model. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public class V1ScriptModelMarshaller extends BaseMarshaller { + + /** + * Sole constructor. + * + * @param desc The descriptor for this model. + */ + public V1ScriptModelMarshaller(Descriptor desc) { + super(desc); + } + + @Override + public Model read(final Configuration config) { + final String name = config.getName(); + if (name.startsWith(ComponentImplementationModel.IMPLEMENTATION)) { + return new V1ScriptComponentImplementationModel(config, getDescriptor()); + } + + if (name.startsWith(ScriptComponentImplementationModel.CODE)) { + return new V1CodeModel(config, getDescriptor()); + } + + return null; + } + +} diff --git a/script/src/main/java/org/switchyard/component/script/deploy/ScriptActivator.java b/script/src/main/java/org/switchyard/component/script/deploy/ScriptActivator.java new file mode 100644 index 000000000..98a494966 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/deploy/ScriptActivator.java @@ -0,0 +1,57 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.deploy; + +import javax.xml.namespace.QName; + +import org.switchyard.component.script.config.model.ScriptComponentImplementationModel; +import org.switchyard.config.model.composite.ComponentModel; +import org.switchyard.deploy.BaseActivator; +import org.switchyard.deploy.ServiceHandler; + +/** + * Activator for implemenations.script. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public class ScriptActivator extends BaseActivator { + + private static final String[] TYPES = new String[] {"script"}; + + /** + * Sole constructor . + */ + public ScriptActivator() { + super(TYPES); + } + + @Override + public ServiceHandler activateService(QName name, ComponentModel config) { + return new ScriptHandler((ScriptComponentImplementationModel)config.getImplementation()); + } + + @Override + public void deactivateService(QName name, ServiceHandler handler) { + // Nothing to do here + } +} diff --git a/script/src/main/java/org/switchyard/component/script/deploy/ScriptComponent.java b/script/src/main/java/org/switchyard/component/script/deploy/ScriptComponent.java new file mode 100644 index 000000000..53ecd2e29 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/deploy/ScriptComponent.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.switchyard.component.script.deploy; + +import org.switchyard.ServiceDomain; +import org.switchyard.deploy.Activator; +import org.switchyard.deploy.BaseComponent; + +/** + * An implementation of Script component. + * + * @author Jiri Pechanec + * @author Magesh Kumar B (C) 2011 Red Hat Inc. + */ +public class ScriptComponent extends BaseComponent { + + /** + * Default constructor. + */ + public ScriptComponent() { + setName("ScriptComponent"); + } + + /* (non-Javadoc) + * @see org.switchyard.deploy.Component#createActivator(org.switchyard.ServiceDomain) + */ + @Override + public Activator createActivator(ServiceDomain domain) { + ScriptActivator activator = new ScriptActivator(); + activator.setServiceDomain(domain); + return activator; + } + +} diff --git a/script/src/main/java/org/switchyard/component/script/deploy/ScriptHandler.java b/script/src/main/java/org/switchyard/component/script/deploy/ScriptHandler.java new file mode 100644 index 000000000..0445e46a8 --- /dev/null +++ b/script/src/main/java/org/switchyard/component/script/deploy/ScriptHandler.java @@ -0,0 +1,179 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.deploy; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +import org.switchyard.Exchange; +import org.switchyard.HandlerException; +import org.switchyard.common.type.Classes; +import org.switchyard.component.script.config.model.CodeModel; +import org.switchyard.component.script.config.model.ScriptComponentImplementationModel; +import org.switchyard.deploy.ServiceHandler; +import org.switchyard.exception.SwitchYardException; + +/** + * An ExchangeHandle that can load and invoke a script using Java Scripting API. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public class ScriptHandler implements ServiceHandler { + + private final ScriptComponentImplementationModel _implModel; + private ScriptEngine _engine; + private CompiledScript _compiledScript; + private String _scriptCode; + + /** + * Sole constructor. + * + * @param implModel The configuration model. + */ + public ScriptHandler(final ScriptComponentImplementationModel implModel) { + _implModel = implModel; + } + + /** + * Initializes script engine and loads the script. + */ + public void start() { + try { + final String language = _implModel.getLanguage(); + final String scriptFile = _implModel.getScriptFile(); + final CodeModel scriptModel = _implModel.getCodeModel(); + + if (scriptFile == null && scriptModel == null) { + throw new SwitchYardException( + "Neither script file nor in-line script were provided"); + } + if (scriptFile != null && scriptModel != null) { + throw new SwitchYardException( + "Either script file or in-line script may be provided but not both"); + } + if (scriptModel != null && language == null) { + throw new SwitchYardException( + "Missing language for in-line script"); + } + final ScriptEngineManager manager = new ScriptEngineManager( + Classes.getClassLoader()); + if (language != null) { + _engine = manager.getEngineByName(language); + if (_engine == null) { + throw new SwitchYardException("Unknown script language '" + + language + "'"); + } + } else { + // We tested previously that scriptFile is not null when + // language is null, we fill find an extension of script + // filename + final int dotIndex = scriptFile.lastIndexOf('.'); + if (dotIndex == -1) { + throw new SwitchYardException("No extension in filename '" + + scriptFile + "'"); + } + final String extension = scriptFile.substring(dotIndex + 1); + _engine = manager.getEngineByExtension(extension); + if (_engine == null) { + throw new SwitchYardException("Unknown script extension '" + + extension + "'"); + } + } + + if (_engine instanceof Compilable) { + _compiledScript = (scriptModel != null) ? ((Compilable) _engine) + .compile(scriptModel.getCode()) + : ((Compilable) _engine) + .compile(loadInputStream(_implModel + .getScriptFile())); + } else { + _scriptCode = (scriptModel != null) ? scriptModel.getCode() + : streamToString(loadInputStream(_implModel + .getScriptFile())); + } + } catch (final SwitchYardException e) { + throw e; + } catch (final Exception e) { + throw new SwitchYardException(e); + } + } + + @Override + public void stop() { + // Nothing to do here + } + + private InputStreamReader loadInputStream(final String scriptFile) throws IOException { + final InputStream in = Classes.getResourceAsStream(scriptFile); + if (in != null) { + return new InputStreamReader(in); + } else { + return new InputStreamReader(new FileInputStream(scriptFile)); + } + } + + private String streamToString(final InputStreamReader inputStream) throws IOException { + final BufferedReader in = new BufferedReader(inputStream); + char[] buffer = new char[1024]; + StringBuilder sb = new StringBuilder(); + int count; + while ((count = in.read(buffer)) != -1) { + sb.append(buffer, 0, count); + } + return sb.toString(); + } + + @Override + public void handleMessage(final Exchange exchange) throws HandlerException { + try { + final Bindings bindings = _engine.createBindings(); + if (_implModel.injectExchange()) { + bindings.put("exchange", exchange); + } else { + bindings.put("content", exchange.getMessage().getContent()); + } + final Object response = (_compiledScript != null) ? _compiledScript + .eval(bindings) : _engine.eval(_scriptCode, bindings); + if (response != null) { + exchange.getMessage().setContent(response); + exchange.send(exchange.getMessage()); + } + } catch (final Exception e) { + throw new HandlerException(e); + } + } + + @Override + public void handleFault(final Exchange exchange) { + } + +} diff --git a/script/src/main/resources/META-INF/services/org.switchyard.deploy.Component b/script/src/main/resources/META-INF/services/org.switchyard.deploy.Component new file mode 100644 index 000000000..1c29753d1 --- /dev/null +++ b/script/src/main/resources/META-INF/services/org.switchyard.deploy.Component @@ -0,0 +1 @@ +org.switchyard.component.script.deploy.ScriptComponent \ No newline at end of file diff --git a/script/src/main/resources/org/switchyard/component/script/config/model/v1/script-v1.xsd b/script/src/main/resources/org/switchyard/component/script/config/model/v1/script-v1.xsd new file mode 100644 index 000000000..8b47725df --- /dev/null +++ b/script/src/main/resources/org/switchyard/component/script/config/model/v1/script-v1.xsd @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/script/src/main/resources/org/switchyard/config/model/descriptor.properties b/script/src/main/resources/org/switchyard/config/model/descriptor.properties new file mode 100644 index 000000000..e49ebb0fe --- /dev/null +++ b/script/src/main/resources/org/switchyard/config/model/descriptor.properties @@ -0,0 +1,21 @@ +# JBoss, Home of Professional Open Source +# Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors +# as indicated by the @authors tag. All rights reserved. +# See the copyright.txt in the distribution for a +# full listing of individual contributors. +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions +# of the GNU Lesser General Public License, v. 2.1. +# This program is distributed in the hope that it will be useful, but WITHOUT A +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +# You should have received a copy of the GNU Lesser General Public License, +# v.2.1 along with this distribution; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +script1_0.namespace=urn:switchyard-component-script:config:1.0 +script1_0.schema=script-v1.xsd +script1_0.location=/org/switchyard/component/script/config/model/v1/ +script1_0.marshaller=org.switchyard.component.script.config.model.v1.V1ScriptModelMarshaller diff --git a/script/src/test/java/org/switchyard/component/script/config/model/v1/V1ScriptComponentImplementationModelTest.java b/script/src/test/java/org/switchyard/component/script/config/model/v1/V1ScriptComponentImplementationModelTest.java new file mode 100644 index 000000000..bf6bd9a1e --- /dev/null +++ b/script/src/test/java/org/switchyard/component/script/config/model/v1/V1ScriptComponentImplementationModelTest.java @@ -0,0 +1,98 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.config.model.v1; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; +import org.switchyard.common.type.Classes; +import org.switchyard.component.script.config.model.v1.V1ScriptComponentImplementationModel; +import org.switchyard.component.script.config.model.v1.V1CodeModel; +import org.switchyard.config.model.ModelPuller; +import org.switchyard.config.model.Validation; +import org.switchyard.config.model.composite.ComponentImplementationModel; +import org.switchyard.config.model.composite.ComponentModel; +import org.switchyard.config.model.switchyard.SwitchYardModel; + +/** + * Unit test for {@link V1ScriptComponentImplementationModel}. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +public class V1ScriptComponentImplementationModelTest { + + @Test + public void inlineScript() throws Exception { + final V1ScriptComponentImplementationModel implModel = getImplModel("switchyard-script-impl.xml"); + final Validation validateModel = implModel.validateModel(); + + assertThat(validateModel.isValid(), is(true)); + final String script = implModel.getCodeModel().getCode(); + assertThat(script, is(equalTo("java.lang.System.out.println(content)"))); + } + + @Test + public void externalFileScript() throws Exception { + final V1ScriptComponentImplementationModel implModel = getImplModel("switchyard-script-impl-file.xml"); + final Validation validateModel = implModel.validateModel(); + + assertThat(validateModel.isValid(), is(true)); + assertThat(implModel.injectExchange(), is(true)); + assertThat(implModel.getScriptFile(), is(equalTo("sample.js"))); + assertThat(Classes.getResourceAsStream(implModel.getScriptFile(), getClass()), is(notNullValue())); + } + + @Test + public void programmaticCreationWithInlineScript() { + final V1ScriptComponentImplementationModel implModel = new V1ScriptComponentImplementationModel(); + implModel.setInjectExchange(true); + implModel.setLanguage("JavaScript"); + final V1CodeModel scriptModel = new V1CodeModel(); + scriptModel.setCode("bogus script"); + implModel.setScriptModel(scriptModel); + + assertThat(implModel.getCodeModel().getCode(), is(equalTo("bogus script"))); + assertThat(implModel.injectExchange(), is(true)); + assertThat(implModel.getLanguage(), is("JavaScript")); + } + + @Test + public void programmaticCreationWithScriptFile() { + final V1ScriptComponentImplementationModel implModel = new V1ScriptComponentImplementationModel(); + implModel.setScriptFile("bogusScript.js"); + + assertThat(implModel.getScriptFile(), is(equalTo("bogusScript.js"))); + assertThat(implModel.injectExchange(), is(false)); + } + + private V1ScriptComponentImplementationModel getImplModel(final String config) throws Exception { + final SwitchYardModel model = new ModelPuller().pull(config, getClass()); + final ComponentModel componentModel = model.getComposite().getComponents().get(0); + final ComponentImplementationModel implementation = componentModel.getImplementation(); + return (V1ScriptComponentImplementationModel) implementation; + } + +} diff --git a/script/src/test/java/org/switchyard/component/script/deploy/ScriptActivatorTest.java b/script/src/test/java/org/switchyard/component/script/deploy/ScriptActivatorTest.java new file mode 100644 index 000000000..8d58aaa44 --- /dev/null +++ b/script/src/test/java/org/switchyard/component/script/deploy/ScriptActivatorTest.java @@ -0,0 +1,56 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.deploy; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.switchyard.test.Invoker; +import org.switchyard.test.ServiceOperation; +import org.switchyard.test.SwitchYardRunner; +import org.switchyard.test.SwitchYardTestCaseConfig; +import org.switchyard.test.mixins.CDIMixIn; + +/** + * Functional test for ScriptActivator. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +@RunWith(SwitchYardRunner.class) +@SwitchYardTestCaseConfig(config = "switchyard-script.xml", mixins = CDIMixIn.class) +public class ScriptActivatorTest { + + @ServiceOperation("OrderService.getTitleForItem") + private Invoker _getTitleForItem; + + @Test + public void activator() { + final String title = _getTitleForItem.sendInOut(10).getContent(String.class); + assertThat(title, is(equalTo("10:Fletch"))); + } + + +} diff --git a/script/src/test/java/org/switchyard/component/script/deploy/ScriptFileTest.java b/script/src/test/java/org/switchyard/component/script/deploy/ScriptFileTest.java new file mode 100644 index 000000000..ef4add9cf --- /dev/null +++ b/script/src/test/java/org/switchyard/component/script/deploy/ScriptFileTest.java @@ -0,0 +1,56 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.deploy; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.switchyard.test.Invoker; +import org.switchyard.test.ServiceOperation; +import org.switchyard.test.SwitchYardRunner; +import org.switchyard.test.SwitchYardTestCaseConfig; +import org.switchyard.test.mixins.CDIMixIn; + +/** + * Functional test for ScriptActivator. + * + * @author Jiri Pechanec + * @author Daniel Bevenius + * + */ +@RunWith(SwitchYardRunner.class) +@SwitchYardTestCaseConfig(config = "switchyard-script-scriptFile.xml", mixins = CDIMixIn.class) +public class ScriptFileTest { + + @ServiceOperation("OrderService.getTitleForItem") + private Invoker _getTitleForItem; + + @Test + public void executeScriptFromFileOnClasspath() { + final String title = _getTitleForItem.sendInOut(10).getContent(String.class); + assertThat(title, is(equalTo("10:Fletch"))); + } + + +} diff --git a/script/src/test/java/org/switchyard/component/script/deploy/support/OrderService.java b/script/src/test/java/org/switchyard/component/script/deploy/support/OrderService.java new file mode 100644 index 000000000..ffa9d6de4 --- /dev/null +++ b/script/src/test/java/org/switchyard/component/script/deploy/support/OrderService.java @@ -0,0 +1,33 @@ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware + * LLC, and individual contributors by the @authors tag. See the copyright.txt + * in the distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.switchyard.component.script.deploy.support; + +/** + * Simple interface intended for testing. + * + * @author Daniel Bevenius + * + */ +public interface OrderService { + + public String getTitleForItem(String itemId); + +} diff --git a/script/src/test/resources/META-INF/beans.xml b/script/src/test/resources/META-INF/beans.xml new file mode 100644 index 000000000..e69de29bb diff --git a/script/src/test/resources/org/switchyard/component/script/config/model/v1/sample.js b/script/src/test/resources/org/switchyard/component/script/config/model/v1/sample.js new file mode 100644 index 000000000..bf39c63bd --- /dev/null +++ b/script/src/test/resources/org/switchyard/component/script/config/model/v1/sample.js @@ -0,0 +1 @@ +exchange.message.content + ':Fletch' \ No newline at end of file diff --git a/script/src/test/resources/org/switchyard/component/script/config/model/v1/switchyard-script-impl-file.xml b/script/src/test/resources/org/switchyard/component/script/config/model/v1/switchyard-script-impl-file.xml new file mode 100644 index 000000000..96a3fda46 --- /dev/null +++ b/script/src/test/resources/org/switchyard/component/script/config/model/v1/switchyard-script-impl-file.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + diff --git a/script/src/test/resources/org/switchyard/component/script/config/model/v1/switchyard-script-impl.xml b/script/src/test/resources/org/switchyard/component/script/config/model/v1/switchyard-script-impl.xml new file mode 100644 index 000000000..ce8135d4c --- /dev/null +++ b/script/src/test/resources/org/switchyard/component/script/config/model/v1/switchyard-script-impl.xml @@ -0,0 +1,32 @@ + + + + + + + + java.lang.System.out.println(content) + + + + + + + + + diff --git a/script/src/test/resources/org/switchyard/component/script/deploy/sample.js b/script/src/test/resources/org/switchyard/component/script/deploy/sample.js new file mode 100644 index 000000000..bf39c63bd --- /dev/null +++ b/script/src/test/resources/org/switchyard/component/script/deploy/sample.js @@ -0,0 +1 @@ +exchange.message.content + ':Fletch' \ No newline at end of file diff --git a/script/src/test/resources/org/switchyard/component/script/deploy/switchyard-script-scriptFile.xml b/script/src/test/resources/org/switchyard/component/script/deploy/switchyard-script-scriptFile.xml new file mode 100644 index 000000000..79614aa59 --- /dev/null +++ b/script/src/test/resources/org/switchyard/component/script/deploy/switchyard-script-scriptFile.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + diff --git a/script/src/test/resources/org/switchyard/component/script/deploy/switchyard-script.xml b/script/src/test/resources/org/switchyard/component/script/deploy/switchyard-script.xml new file mode 100644 index 000000000..a97258fb8 --- /dev/null +++ b/script/src/test/resources/org/switchyard/component/script/deploy/switchyard-script.xml @@ -0,0 +1,36 @@ + + + + + + + + + + exchange.message.content + ':Fletch' + + + + + + + + + + +