KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > schema > rules > InvokeParentRule


1 // Copyright 2004, 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.hivemind.schema.rules;
16
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.Element;
23 import org.apache.hivemind.schema.SchemaProcessor;
24
25 /**
26  * Rule used to connect a child object to its parent by invoking a method on the parent, passing the
27  * child. The child object is the top object on the stack and the parent object is the next object
28  * down on the stack. Created from the <code>&lt;invoke-parent&gt;</code> element. Generally, this
29  * is the last rule in a sequence of rules.
30  *
31  * @author Howard Lewis Ship
32  */

33 public class InvokeParentRule extends BaseRule
34 {
35     private String JavaDoc _methodName;
36
37     private int _depth = 1;
38     private int _parameterCount = 1;
39
40     public InvokeParentRule()
41     {
42
43     }
44
45     public InvokeParentRule(String JavaDoc methodName)
46     {
47         _methodName = methodName;
48     }
49
50     /**
51      * Invokes the named method on the parent object (using reflection).
52      */

53     public void begin(SchemaProcessor processor, Element element)
54     {
55         Object JavaDoc[] parameters = new Object JavaDoc[_parameterCount];
56         Class JavaDoc[] parameterTypes = new Class JavaDoc[_parameterCount];
57         // Get all parameters from the stack in the order they where pushed on it
58
for (int i = 0; i < parameters.length; i++)
59         {
60             parameters[i] = processor.peek(_parameterCount - i - 1);
61             if (parameterTypes[i] != null)
62                 parameterTypes[i] = parameters[i].getClass();
63         }
64
65         String JavaDoc methodName = _methodName;
66         Object JavaDoc parent = processor.peek(_parameterCount + _depth - 1);
67         if (processor.isInBackwardCompatibilityModeForMaps() && _parameterCount == 1
68                 && ("addElement".equals(_methodName) || "add".equals(_methodName))) {
69             methodName = "addKeyedElement";
70             // We can't call the map (parent) directly, since we must calculate the key attribute
71
// value, but don't have access to its definition.
72
// Let the processor do the work. It knows the current element-model and its attributes
73
parent = processor;
74         } else if (parent instanceof List JavaDoc && "addElement".equals(_methodName) ) {
75             // For Backward compatiblity: If parent is a list and the method is 'addElement'
76
// then change it to add.
77
methodName = "add";
78         }
79         
80         try
81         {
82             Method JavaDoc m = findMethod(parent, methodName, parameterTypes);
83
84             m.invoke(parent, parameters);
85         }
86         catch (InvocationTargetException JavaDoc ex) {
87             throw new ApplicationRuntimeException(RulesMessages.errorInvokingMethod(
88                     methodName,
89                     parent,
90                     getLocation(),
91                     ex.getTargetException()), getLocation(), ex.getTargetException());
92         } catch (Exception JavaDoc ex)
93         {
94             throw new ApplicationRuntimeException(RulesMessages.errorInvokingMethod(
95                     methodName,
96                     parent,
97                     getLocation(),
98                     ex), getLocation(), ex);
99         }
100     }
101
102     public String JavaDoc getMethodName()
103     {
104         return _methodName;
105     }
106
107     public void setMethodName(String JavaDoc string)
108     {
109         _methodName = string;
110     }
111
112     /**
113      * @since 1.1
114      */

115     public int getDepth()
116     {
117         return _depth;
118     }
119
120     /**
121      * Sets the depth of the parent object. The default is 1.
122      */

123     public void setDepth(int i)
124     {
125         _depth = i;
126     }
127
128     /**
129      * Searches for the *first* public method the has the right name, and takes a single parameter
130      * that is compatible with the parameter type.
131      *
132      * @throws NoSuchMethodException
133      * if a method can't be found
134      */

135     private Method JavaDoc findMethod(Object JavaDoc target, String JavaDoc name, Class JavaDoc[] parameterTypes)
136             throws NoSuchMethodException JavaDoc
137     {
138         Method JavaDoc[] methods = target.getClass().getMethods();
139
140         for (int i = 0; i < methods.length; i++)
141         {
142             Method JavaDoc m = methods[i];
143             Class JavaDoc[] actualParameterTypes = m.getParameterTypes();
144
145             if (actualParameterTypes.length != parameterTypes.length)
146                 continue;
147
148             if (!m.getName().equals(name))
149                 continue;
150
151             for (int j = 0; j < actualParameterTypes.length; j++)
152             {
153                 Class JavaDoc actualParameterType = actualParameterTypes[j];
154                 Class JavaDoc expectedParameterType = parameterTypes[j];
155                 if ((expectedParameterType != null && actualParameterType.isAssignableFrom(expectedParameterType))
156                         || (expectedParameterType == null && !actualParameterType.isPrimitive()))
157                     return m;
158                 
159             }
160         }
161
162         throw new NoSuchMethodException JavaDoc(name);
163     }
164
165     public int getParameterCount()
166     {
167         return _parameterCount;
168     }
169
170     /**
171      * Sets the number of parameters that are passed to the invoked method. The default is 1.
172      *
173      * @param parameterCount
174      */

175     public void setParameterCount(int parameterCount)
176     {
177         _parameterCount = parameterCount;
178     }
179
180 }
181
Popular Tags