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 | 0 | 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 | 0 | String propertyName = spec.getProperty(); |
44 | 0 | String scriptName = spec.getObject(); |
45 | 0 | Location location = spec.getLocation(); |
46 | |
|
47 | 0 | injectScript(op, propertyName, scriptName, location); |
48 | 0 | } |
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 | 0 | Defense.notNull(op, "op"); |
68 | 0 | Defense.notNull(propertyName, "propertyName"); |
69 | 0 | Defense.notNull(scriptName, "scriptName"); |
70 | 0 | Defense.notNull(location, "location"); |
71 | |
|
72 | 0 | op.claimReadonlyProperty(propertyName); |
73 | |
|
74 | 0 | Class propertyType = EnhanceUtils.verifyPropertyType(op, propertyName, IScript.class); |
75 | |
|
76 | |
|
77 | |
|
78 | 0 | String methodName = op.getAccessorMethodName(propertyName); |
79 | |
|
80 | 0 | Resource resource = location.getResource().getRelativeResource(scriptName); |
81 | |
|
82 | |
|
83 | |
|
84 | 0 | 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 | 0 | DeferredScript script = new DeferredScriptImpl(resource, _source, location); |
96 | |
|
97 | 0 | String fieldName = op.addInjectedField("_$script", DeferredScript.class, script); |
98 | |
|
99 | 0 | MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); |
100 | |
|
101 | 0 | op.addMethod(Modifier.PUBLIC, sig, "return " + fieldName + ".getScript();", location); |
102 | 0 | } |
103 | |
|
104 | |
public void setSource(IScriptSource source) |
105 | |
{ |
106 | 0 | _source = source; |
107 | 0 | } |
108 | |
|
109 | |
public void setAssetSource(AssetSource assetSource) |
110 | |
{ |
111 | 0 | _assetSource = assetSource; |
112 | 0 | } |
113 | |
} |