Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWITCHYARD-979: Implementation of script component #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<module>hornetq</module>
<module>jca</module>
<module>tools/forge</module>
<module>script</module>
</modules>

<repositories>
Expand Down
72 changes: 72 additions & 0 deletions script/Readme.md
Original file line number Diff line number Diff line change
@@ -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
<sd:switchyard
xmlns="urn:switchyard-component-script:config:1.0"
xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:sd="urn:switchyard-config:switchyard:1.0"
xmlns:bean="urn:switchyard-component-bean:config:1.0">

<sca:composite>

<sca:component name="ScriptComponent">
<sca:service name="OrderService" >
<sca:interface.java interface="org.switchyard.component.script.deploy.support.OrderService"/>
</sca:service>

<implementation.script language="JavaScript">
<code>
'Hello ' + exchange.message.content
</code>
</implementation.script>
</sca:component>

</sca:composite>

</sd:switchyard>

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
<sd:switchyard
xmlns="urn:switchyard-component-script:config:1.0"
xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:sd="urn:switchyard-config:switchyard:1.0"
xmlns:bean="urn:switchyard-component-bean:config:1.0">

<sca:composite>

<sca:component name="scriptComponent">
<sca:service name="OrderService" >
<sca:interface.java interface="org.switchyard.component.script.deploy.support.OrderService"/>
</sca:service>

<implementation.script scriptFile="script.js"/>
</sca:component>

</sca:composite>

</sd:switchyard>

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
<implementation.script injectExchange="true" scriptFile="file://sample.js"/>

_ _ _

72 changes: 72 additions & 0 deletions script/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-components-parent</artifactId>
<version>0.6.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>switchyard-component-script</artifactId>
<packaging>jar</packaging>

<name>SwitchYard: Script Component</name>
<url>http://switchyard.org</url>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<!-- Avoid processing @ delimeter, we use that in license headers -->
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
</configuration>
</plugin>
</plugins>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.js</include>
</includes>
</testResource>
</testResources>
</build>

<dependencies>

<dependency>
<groupId>org.switchyard</groupId>
<artifactId>switchyard-config</artifactId>
</dependency>
<dependency>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-bean</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.switchyard</groupId>
<artifactId>switchyard-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Loading