Skip to content

Commit 7ff721c

Browse files
committed
SWITCHYARD-979: Implementation of script component
1 parent 37ff709 commit 7ff721c

25 files changed

+1273
-0
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<module>hornetq</module>
6868
<module>jca</module>
6969
<module>tools/forge</module>
70+
<module>script</module>
7071
</modules>
7172

7273
<repositories>

script/Readme.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Switchyard Script Component
2+
This project provides an implementation.script enabling the usage of the any JVM scripting languge with JSR-223 support in a SwitchYard service implementation.
3+
4+
_ _ _
5+
6+
## Using script "inlined" in SwitchYard
7+
<sd:switchyard
8+
xmlns="urn:switchyard-component-script:config:1.0"
9+
xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"
10+
xmlns:sd="urn:switchyard-config:switchyard:1.0"
11+
xmlns:bean="urn:switchyard-component-bean:config:1.0">
12+
13+
<sca:composite>
14+
15+
<sca:component name="ScriptComponent">
16+
<sca:service name="OrderService" >
17+
<sca:interface.java interface="org.switchyard.component.script.deploy.support.OrderService"/>
18+
</sca:service>
19+
20+
<implementation.script language="JavaScript">
21+
<code>
22+
'Hello ' + exchange.message.content
23+
</code>
24+
</implementation.script>
25+
</sca:component>
26+
27+
</sca:composite>
28+
29+
</sd:switchyard>
30+
31+
The script languge is defined by the attribute *language* of *implementation.script*
32+
33+
Calling this service is done in the same way as calling any other SwitchYard service, for example:
34+
35+
String title = (String) newInvoker("OrderService").operation("getTitleForItem").sendInOut(10).getContent(String.class);
36+
37+
_ _ _
38+
39+
## Using external script
40+
<sd:switchyard
41+
xmlns="urn:switchyard-component-script:config:1.0"
42+
xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"
43+
xmlns:sd="urn:switchyard-config:switchyard:1.0"
44+
xmlns:bean="urn:switchyard-component-bean:config:1.0">
45+
46+
<sca:composite>
47+
48+
<sca:component name="scriptComponent">
49+
<sca:service name="OrderService" >
50+
<sca:interface.java interface="org.switchyard.component.script.deploy.support.OrderService"/>
51+
</sca:service>
52+
53+
<implementation.script scriptFile="script.js"/>
54+
</sca:component>
55+
56+
</sca:composite>
57+
58+
</sd:switchyard>
59+
60+
The script can be located on the classpath or in an external file.
61+
The scripting language can be defined by the *language* attribute or is devised from an extension of a scripting file
62+
_ _ _
63+
64+
## Message content/Exchange injection
65+
The invoked script has binded one of two variables
66+
67+
* exchange - representing SwitchYard exchange and available if *injectExchange* attribute of *implementation.script* set to true
68+
* content - content of the incoming message otherwise
69+
<implementation.script injectExchange="true" scriptFile="file://sample.js"/>
70+
71+
_ _ _
72+

script/pom.xml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.switchyard.components</groupId>
7+
<artifactId>switchyard-components-parent</artifactId>
8+
<version>0.6.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>switchyard-component-script</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<name>SwitchYard: Script Component</name>
16+
<url>http://switchyard.org</url>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-resources-plugin</artifactId>
23+
<configuration>
24+
<!-- Avoid processing @ delimeter, we use that in license headers -->
25+
<useDefaultDelimiters>false</useDefaultDelimiters>
26+
<delimiters>
27+
<delimiter>${*}</delimiter>
28+
</delimiters>
29+
</configuration>
30+
</plugin>
31+
</plugins>
32+
<testResources>
33+
<testResource>
34+
<directory>src/test/resources</directory>
35+
<includes>
36+
<include>**/*.xml</include>
37+
<include>**/*.js</include>
38+
</includes>
39+
</testResource>
40+
</testResources>
41+
</build>
42+
43+
<dependencies>
44+
45+
<dependency>
46+
<groupId>org.switchyard</groupId>
47+
<artifactId>switchyard-config</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.switchyard.components</groupId>
51+
<artifactId>switchyard-component-bean</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.switchyard</groupId>
56+
<artifactId>switchyard-test</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.hamcrest</groupId>
61+
<artifactId>hamcrest-all</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.mockito</groupId>
66+
<artifactId>mockito-all</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
</dependencies>
71+
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
3+
* LLC, and individual contributors by the @authors tag. See the copyright.txt
4+
* in the distribution for a full listing of individual contributors.
5+
*
6+
* This is free software; you can redistribute it and/or modify it under the
7+
* terms of the GNU Lesser General Public License as published by the Free
8+
* Software Foundation; either version 2.1 of the License, or (at your option)
9+
* any later version.
10+
*
11+
* This software is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this software; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
19+
* site: http://www.fsf.org.
20+
*/
21+
package org.switchyard.component.script.config.model;
22+
23+
import org.switchyard.config.model.NamedModel;
24+
25+
/**
26+
* Configuration model for a 'code' element containing an in-lined JSR-223 script.
27+
*
28+
* @author Jiri Pechanec
29+
* @author Daniel Bevenius
30+
*
31+
*/
32+
public interface CodeModel extends NamedModel {
33+
34+
/**
35+
* The script element name.
36+
*/
37+
String CODE = "code";
38+
39+
/**
40+
* Gets the script content from the 'code' element.
41+
*
42+
* @return String The script.
43+
*/
44+
public abstract String getCode();
45+
46+
/**
47+
* Sets the script code.
48+
*
49+
* @param code The script code to set.
50+
* @return {@link V1CodeModel} this object ref to support method chaining.
51+
*/
52+
public abstract CodeModel setCode(final String code);
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
3+
* LLC, and individual contributors by the @authors tag. See the copyright.txt
4+
* in the distribution for a full listing of individual contributors.
5+
*
6+
* This is free software; you can redistribute it and/or modify it under the
7+
* terms of the GNU Lesser General Public License as published by the Free
8+
* Software Foundation; either version 2.1 of the License, or (at your option)
9+
* any later version.
10+
*
11+
* This software is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this software; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
19+
* site: http://www.fsf.org.
20+
*/
21+
package org.switchyard.component.script.config.model;
22+
23+
import org.switchyard.config.model.composite.ComponentImplementationModel;
24+
25+
/**
26+
* A definition of an 'implementation.script' element.
27+
*
28+
* @author Jiri Pechanec
29+
* @author Daniel Bevenius
30+
*
31+
*/
32+
public interface ScriptComponentImplementationModel extends ComponentImplementationModel {
33+
34+
/**
35+
* The 'script' namespace.
36+
*/
37+
public static final String DEFAULT_NAMESPACE = "urn:switchyard-component-script:config:1.0";
38+
39+
/**
40+
* The 'script' implementation type.
41+
*/
42+
String SCRIPT = "script";
43+
44+
/**
45+
* The 'code' element name.
46+
*/
47+
String CODE = "code";
48+
49+
/**
50+
* The 'scriptFile' attribute name.
51+
*/
52+
String SCRIPT_FILE = "scriptFile";
53+
54+
/**
55+
* The 'injectExchange' attribute name.
56+
*/
57+
String INJECT_EXCHANGE = "injectExchange";
58+
59+
/**
60+
* The 'language' attribute name.
61+
*/
62+
String LANGUAGE = "language";
63+
64+
/**
65+
* Gets the JSR-223 script that is the actual script code.
66+
* @return {@link CodeModel} the code configuration model.
67+
*/
68+
CodeModel getCodeModel();
69+
70+
/**
71+
* Sets the in-line script model.
72+
* @param codeModel The code configuration model.
73+
* @return {@link ScriptComponentImplementationModel} to enable method chaining.
74+
*/
75+
ScriptComponentImplementationModel setScriptModel(final CodeModel codeModel);
76+
77+
/**
78+
* Gets the file that is the actual script code.
79+
* @return String the filename of a script to execute.
80+
*/
81+
String getScriptFile();
82+
83+
/**
84+
* Sets the scriptFile.
85+
*
86+
* @param scriptFile The script file.
87+
* @return {@link ScriptComponentImplementationModel} to enable method chaining.
88+
*/
89+
ScriptComponentImplementationModel setScriptFile(final String scriptFile);
90+
91+
/**
92+
* Determines whether the complete Exchange should be injected into the script.
93+
*
94+
* @return true If the Exchange should be injected.
95+
*/
96+
Boolean injectExchange();
97+
98+
/**
99+
* Sets the 'injectExchange' property whether the complete Exchange should be injected into the script.
100+
*
101+
* @param enable The value to 'injectExchange to.
102+
* @return {@link ScriptComponentImplementationModel} to enable method chaining.
103+
*/
104+
ScriptComponentImplementationModel setInjectExchange(final Boolean enable);
105+
106+
/**
107+
* Gets the name of script language.
108+
* @return String the language name.
109+
*/
110+
String getLanguage();
111+
112+
/**
113+
* Sets the script language.
114+
*
115+
* @param language The script language.
116+
* @return {@link ScriptComponentImplementationModel} to enable method chaining.
117+
*/
118+
ScriptComponentImplementationModel setLanguage(final String language);
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.switchyard.component.script.config.model.v1;
2+
3+
import javax.xml.namespace.QName;
4+
5+
import org.switchyard.component.script.config.model.ScriptComponentImplementationModel;
6+
import org.switchyard.component.script.config.model.CodeModel;
7+
import org.switchyard.config.Configuration;
8+
import org.switchyard.config.model.BaseNamedModel;
9+
import org.switchyard.config.model.Descriptor;
10+
11+
/**
12+
* A version 1 implementation of a CodeModel.
13+
*
14+
* @author Jiri Pechanec
15+
* @author Daniel Bevenius
16+
*
17+
*/
18+
public class V1CodeModel extends BaseNamedModel implements CodeModel {
19+
20+
private String _code;
21+
22+
/**
23+
* No-args constructor.
24+
*/
25+
public V1CodeModel() {
26+
super(new QName(ScriptComponentImplementationModel.DEFAULT_NAMESPACE, CODE));
27+
}
28+
29+
/**
30+
* Constructor.
31+
*
32+
* @param config The configuration model.
33+
* @param desc The descriptor for this model.
34+
*/
35+
public V1CodeModel(Configuration config, Descriptor desc) {
36+
super(config, desc);
37+
}
38+
39+
@Override
40+
public String getCode() {
41+
if (_code != null) {
42+
return _code;
43+
}
44+
45+
_code = getModelValue();
46+
return _code;
47+
}
48+
49+
@Override
50+
public CodeModel setCode(final String code) {
51+
setModelValue(code);
52+
_code = code;
53+
return this;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)