blob: c6b58a25c657f879d4ac3f22ca250ad95776ffa4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package org.apache.tuscany.container.js.assembly.impl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.tuscany.common.resource.ResourceLoader;
import org.apache.tuscany.container.js.assembly.JavaScriptImplementation;
import org.apache.tuscany.model.assembly.AssemblyModelContext;
import org.apache.tuscany.model.assembly.ComponentType;
import org.apache.tuscany.model.assembly.ModelInitException;
import org.apache.tuscany.model.assembly.impl.ComponentImplementationImpl;
/**
* Default implementation of a JavScript component implementation type
*
* @version $Rev$ $Date$
*/
public class JavaScriptImplementationImpl extends ComponentImplementationImpl implements JavaScriptImplementation {
private ResourceLoader resourceLoader;
public JavaScriptImplementationImpl() {
super();
}
public ResourceLoader getResourceLoader() {
return null;
}
public void initialize(AssemblyModelContext modelContext) throws ModelInitException {
if (isInitialized()) {
return;
}
this.resourceLoader = modelContext.getApplicationResourceLoader();
if(resourceLoader == null){
throw new ModelInitException("No resource loader set on model context");
}
getScriptFile();
// Initialize the component type
ComponentType componentType = getComponentType();
if (componentType == null) {
try {
componentType = createComponentType(modelContext);
} catch (IOException e) {
throw new ModelInitException("Error retrieving component type file",e);
}
setComponentType(componentType);
}
super.initialize(modelContext);
}
String script;
public String getScriptFile() {
return script;
}
public void setScriptFile(String fn) {
script = fn;
}
private String scriptCode;
public String getScript() throws ModelInitException {
if (scriptCode != null) {
return scriptCode;
}
try {
URL url = resourceLoader.getResource(getScriptFile());
if (url == null) {
ModelInitException ce = new ModelInitException("Script not found");
ce.setIdentifier(getScriptFile());
throw ce;
}
InputStream inputStream = url.openStream();
try {
StringBuffer sb = new StringBuffer();
int n = 0;
while ((n = inputStream.read()) != -1) {
sb.append((char) n);
}
scriptCode = sb.toString();
return scriptCode;
} finally {
inputStream.close();
}
} catch (IOException e) {
ModelInitException ce = new ModelInitException("Error reading script file",e);
ce.setIdentifier(getScriptFile());
throw ce;
}
}
/**
* Create the component type
*
* @param modelContext
* @param implementationClass
*/
private ComponentType createComponentType(AssemblyModelContext modelContext) throws IOException{
String prefix = script.substring(0,script.lastIndexOf('.'));
URL componentTypeFile = resourceLoader.getResource(prefix + ".componentType");
if (componentTypeFile != null) {
return modelContext.getAssemblyLoader().loadComponentType(componentTypeFile.toString());
} else {
// TODO we could introspect the JavaScript source
return modelContext.getAssemblyFactory().createComponentType();
}
}
}
|