1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.enhance; |
16 |
|
|
17 |
|
import org.apache.hivemind.Location; |
18 |
|
import org.apache.hivemind.Resource; |
19 |
|
import org.apache.hivemind.service.MethodSignature; |
20 |
|
import org.apache.hivemind.util.Defense; |
21 |
|
import org.apache.tapestry.IAsset; |
22 |
|
import org.apache.tapestry.IScript; |
23 |
|
import org.apache.tapestry.asset.AssetSource; |
24 |
|
import org.apache.tapestry.engine.IScriptSource; |
25 |
|
import org.apache.tapestry.spec.InjectSpecification; |
26 |
|
|
27 |
|
import java.lang.reflect.Modifier; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
1 |
public class InjectScriptWorker implements InjectEnhancementWorker |
36 |
|
{ |
37 |
|
private IScriptSource _source; |
38 |
|
|
39 |
|
private AssetSource _assetSource; |
40 |
|
|
41 |
|
public void performEnhancement(EnhancementOperation op, InjectSpecification spec) |
42 |
|
{ |
43 |
1 |
String propertyName = spec.getProperty(); |
44 |
1 |
String scriptName = spec.getObject(); |
45 |
1 |
Location location = spec.getLocation(); |
46 |
|
|
47 |
1 |
injectScript(op, propertyName, scriptName, location); |
48 |
1 |
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
public void injectScript(EnhancementOperation op, String propertyName, |
65 |
|
String scriptName, Location location) |
66 |
|
{ |
67 |
1 |
Defense.notNull(op, "op"); |
68 |
1 |
Defense.notNull(propertyName, "propertyName"); |
69 |
1 |
Defense.notNull(scriptName, "scriptName"); |
70 |
1 |
Defense.notNull(location, "location"); |
71 |
|
|
72 |
1 |
op.claimReadonlyProperty(propertyName); |
73 |
|
|
74 |
3 |
Class propertyType = EnhanceUtils.verifyPropertyType(op, propertyName, IScript.class); |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
1 |
String methodName = op.getAccessorMethodName(propertyName); |
79 |
|
|
80 |
1 |
Resource resource = location.getResource().getRelativeResource(scriptName); |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
1 |
if (resource.getResourceURL() == null) |
85 |
|
{ |
86 |
0 |
IAsset scriptAsset = _assetSource.findAsset(location.getResource(), op.getSpecification(), |
87 |
|
scriptName, null, location); |
88 |
|
|
89 |
0 |
if (scriptAsset != null) |
90 |
|
{ |
91 |
0 |
resource = scriptAsset.getResourceLocation(); |
92 |
|
} |
93 |
|
} |
94 |
|
|
95 |
1 |
DeferredScript script = new DeferredScriptImpl(resource, _source, location); |
96 |
|
|
97 |
1 |
String fieldName = op.addInjectedField("_$script", DeferredScript.class, script); |
98 |
|
|
99 |
1 |
MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); |
100 |
|
|
101 |
1 |
op.addMethod(Modifier.PUBLIC, sig, "return " + fieldName + ".getScript();", location); |
102 |
1 |
} |
103 |
|
|
104 |
|
public void setSource(IScriptSource source) |
105 |
|
{ |
106 |
1 |
_source = source; |
107 |
1 |
} |
108 |
|
|
109 |
|
public void setAssetSource(AssetSource assetSource) |
110 |
|
{ |
111 |
0 |
_assetSource = assetSource; |
112 |
0 |
} |
113 |
|
} |