KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > InjectScriptWorker


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.enhance;
16
17 import java.lang.reflect.Modifier JavaDoc;
18
19 import org.apache.hivemind.Location;
20 import org.apache.hivemind.Resource;
21 import org.apache.hivemind.service.MethodSignature;
22 import org.apache.hivemind.util.Defense;
23 import org.apache.tapestry.IScript;
24 import org.apache.tapestry.engine.IScriptSource;
25 import org.apache.tapestry.spec.InjectSpecification;
26
27 /**
28  * Injects {@link org.apache.tapestry.IScript} instances directly into pages or components.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class InjectScriptWorker implements InjectEnhancementWorker
34 {
35     private IScriptSource _source;
36
37     public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
38     {
39         String JavaDoc propertyName = spec.getProperty();
40         String JavaDoc scriptName = spec.getObject();
41         Location location = spec.getLocation();
42
43         injectScript(op, propertyName, scriptName, location);
44     }
45
46     /**
47      * Injects a compiled script.
48      *
49      * @param op
50      * the enhancement operation
51      * @param propertyName
52      * the name of the property to inject
53      * @param scriptName
54      * the name of the script (relative to the location)
55      * @param location
56      * the location of the specification; primarily used as the base location for finding
57      * the script.
58      */

59
60     public void injectScript(EnhancementOperation op, String JavaDoc propertyName, String JavaDoc scriptName,
61             Location location)
62     {
63         Defense.notNull(op, "op");
64         Defense.notNull(propertyName, "propertyName");
65         Defense.notNull(scriptName, "scriptName");
66         Defense.notNull(location, "location");
67
68         op.claimProperty(propertyName);
69
70         Class JavaDoc propertyType = EnhanceUtils.verifyPropertyType(op, propertyName, IScript.class);
71
72         // PropertyType will likely be either java.lang.Object or IScript
73

74         String JavaDoc methodName = op.getAccessorMethodName(propertyName);
75
76         Resource resource = location.getResource().getRelativeResource(scriptName);
77
78         DeferredScript script = new DeferredScriptImpl(resource, _source, location);
79
80         String JavaDoc fieldName = op.addInjectedField("_$script", DeferredScript.class, script);
81
82         MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
83
84         op.addMethod(Modifier.PUBLIC, sig, "return " + fieldName + ".getScript();");
85     }
86
87     public void setSource(IScriptSource source)
88     {
89         _source = source;
90     }
91 }
92
Popular Tags