1
15
16 package gate.creole;
17
18 import java.beans.*;
19 import java.io.Serializable;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.*;
23
24 import gate.*;
25 import gate.FeatureMap;
26 import gate.Resource;
27 import gate.util.*;
28
29
30
32 abstract public class AbstractResource
33 extends AbstractFeatureBearer implements Resource, Serializable
34 {
35 static final long serialVersionUID = -9196293927841163321L;
36
37
38 public Resource init() throws ResourceInstantiationException {
39 return this;
40 }
42
43 public void setName(String name){
44 this.name = name;
45 }
46
47
48 public String getName(){
49 return name;
50 }
51
52 protected String name;
53
56 public void cleanup(){
57 }
58
59
67 public static Object getParameterValue(Resource resource,
68 String paramaterName)
69 throws ResourceInstantiationException{
70 BeanInfo resBeanInf = null;
72 try {
73 resBeanInf = getBeanInfo(resource.getClass());
74 } catch(Exception e) {
75 throw new ResourceInstantiationException(
76 "Couldn't get bean info for resource " + resource.getClass().getName()
77 + Strings.getNl() + "Introspector exception was: " + e
78 );
79 }
80 PropertyDescriptor[] properties = resBeanInf.getPropertyDescriptors();
81
82 if(properties == null){
84 throw new ResourceInstantiationException(
85 "Couldn't get properties info for resource " +
86 resource.getClass().getName());
87 }
88 boolean done = false;
89 int i = 0;
90 Object value = null;
91 while(!done && i < properties.length){
92 PropertyDescriptor prop = properties[i];
93 if(prop.getName().equals(paramaterName)){
94 Method getMethod = prop.getReadMethod();
95 if(getMethod == null){
96 throw new ResourceInstantiationException(
97 "Couldn't get read accessor method for parameter " + paramaterName +
98 " in " + resource.getClass().getName());
99 }
100 Object[] args = new Object[0];
102 try {
103 value = getMethod.invoke(resource, args);
104 } catch(Exception e) {
105 throw new ResourceInstantiationException(
106 "couldn't invoke get method: " + e
107 );
108 }
109 done = true;
110 } i++;
112 } if(done) return value;
114 else throw new ResourceInstantiationException(
115 "Couldn't find parameter named " + paramaterName +
116 " in " + resource.getClass().getName());
117 }
118
119
126 public static void setParameterValue(Resource resource, BeanInfo resBeanInf,
127 String paramaterName,
128 Object parameterValue)
129 throws ResourceInstantiationException{
130 PropertyDescriptor[] properties = resBeanInf.getPropertyDescriptors();
131 if(properties == null){
133 throw new ResourceInstantiationException(
134 "Couldn't get properties info for resource " +
135 resource.getClass().getName());
136 }
137 boolean done = false;
138 int i = 0;
139 while(!done && i < properties.length){
140 PropertyDescriptor prop = properties[i];
141 if(prop.getName().equals(paramaterName)){
142 Method setMethod = prop.getWriteMethod();
143 if(setMethod == null){
144 throw new ResourceInstantiationException(
145 "Couldn't get write accessor method for parameter " +
146 paramaterName + " in " + resource.getClass().getName());
147 }
148
149 if(parameterValue != null){
151 Class propertyType = prop.getPropertyType();
152 Class paramType = parameterValue.getClass();
153 if(!propertyType.isAssignableFrom(paramType)) {
154 try {
156 parameterValue = propertyType.getConstructor(
157 new Class[]{paramType}
158 ).newInstance( new Object[]{parameterValue} );
159 } catch(Exception e) {
160 if(String.class.isAssignableFrom(paramType)){
164 ResourceData rData = (ResourceData)Gate.getCreoleRegister().
165 get(resource.getClass().getName());
166 ParameterList pList = rData.getParameterList();
167 Parameter param = null;
168 Iterator disjIter = pList.getInitimeParameters().iterator();
169 while(param == null && disjIter.hasNext()){
170 Iterator paramIter = ((List)disjIter.next()).iterator();
171 while(param == null && paramIter.hasNext()){
172 Parameter aParam = (Parameter)paramIter.next();
173 if(aParam.getName().equals(paramaterName)) param = aParam;
174 }
175 }
176 disjIter = pList.getRuntimeParameters().iterator();
177 while(param == null && disjIter.hasNext()){
178 Iterator paramIter = ((List)disjIter.next()).iterator();
179 while(param == null && paramIter.hasNext()){
180 Parameter aParam = (Parameter)paramIter.next();
181 if(aParam.getName().equals(paramaterName)) param = aParam;
182 }
183 }
184 if(param != null){
185 try{
186 parameterValue = param.calculateValueFromString(
187 (String)parameterValue);
188 }catch(ParameterException pe){
189 throw new ResourceInstantiationException(pe);
190 }
191 }else{
192 throw new LuckyException("Unknown parameter " + paramaterName +
194 " for resource " + resource.getClass().getName() +
195 "!");
196 }
197 }else{
198 throw new ResourceInstantiationException(
199 "Error converting " + parameterValue.getClass() +
200 " to " + propertyType + ": " + e.toString()
201 );
202 }
203 }
204 }
205 }
207 Object[] args = new Object[1];
209 args[0] = parameterValue;
210 try {
211 setMethod.invoke(resource, args);
212 } catch(Exception e) {
213 e.printStackTrace(Err.getPrintWriter());
214 throw new ResourceInstantiationException(
215 "couldn't invoke set method for " + paramaterName +
216 " on " + resource.getClass().getName() + ": " + e);
217 }
218 done = true;
219 } i++;
221 } if(!done) throw new ResourceInstantiationException(
223 "Couldn't find parameter named " + paramaterName +
224 " in " + resource.getClass().getName());
225 }
227
228
234 public static void setParameterValues(Resource resource,
235 FeatureMap parameters)
236 throws ResourceInstantiationException{
237 BeanInfo resBeanInf = null;
239 try {
240 resBeanInf = getBeanInfo(resource.getClass());
241 } catch(Exception e) {
242 throw new ResourceInstantiationException(
243 "Couldn't get bean info for resource " + resource.getClass().getName()
244 + Strings.getNl() + "Introspector exception was: " + e
245 );
246 }
247
248 Iterator parnameIter = parameters.keySet().iterator();
249 while(parnameIter.hasNext()){
250 String parName = (String)parnameIter.next();
251 setParameterValue(resource, resBeanInf, parName, parameters.get(parName));
252 }
253 }
254
255
256
263 public static void setResourceListeners(Resource resource, Map listeners)
264 throws
265 IntrospectionException, InvocationTargetException,
266 IllegalAccessException, GateException
267 {
268 BeanInfo resBeanInfo = getBeanInfo(resource.getClass());
270
271 EventSetDescriptor[] events = resBeanInfo.getEventSetDescriptors();
273
274 if (events != null) {
276 EventSetDescriptor event;
277 for(int i = 0; i < events.length; i++) {
278 event = events[i];
279
280 Object listener =
282 listeners.get(event.getListenerType().getName());
283 if(listener != null) {
284 Method addListener = event.getAddListenerMethod();
285
286 Object[] args = new Object[1];
288 args[0] = listener;
289 addListener.invoke(resource, args);
290 }
291 } } }
295
302 public static void removeResourceListeners(Resource resource, Map listeners)
303 throws IntrospectionException, InvocationTargetException,
304 IllegalAccessException, GateException{
305
306 BeanInfo resBeanInfo = getBeanInfo(resource.getClass());
308
309 EventSetDescriptor[] events = resBeanInfo.getEventSetDescriptors();
311
312 if(events != null) {
314 EventSetDescriptor event;
315 for(int i = 0; i < events.length; i++) {
316 event = events[i];
317
318 Object listener =
320 listeners.get(event.getListenerType().getName());
321 if(listener != null) {
322 Method removeListener = event.getRemoveListenerMethod();
323
324 Object[] args = new Object[1];
326 args[0] = listener;
327 removeListener.invoke(resource, args);
328 }
329 } } }
333
348 public static boolean checkParameterValues(Resource resource,
349 List parameters)
350 throws ResourceInstantiationException{
351 Iterator disIter = parameters.iterator();
352 while(disIter.hasNext()){
353 List disjunction = (List)disIter.next();
354 boolean required = !((Parameter)disjunction.get(0)).isOptional();
355 if(required){
356 boolean valueSet = false;
358 Iterator parIter = disjunction.iterator();
359 while(!valueSet && parIter.hasNext()){
360 Parameter par = (Parameter)parIter.next();
361 valueSet = (resource.getParameterValue(par.getName()) != null);
362 }
363 if(!valueSet) return false;
364 }
365 }
366 return true;
367 }
368
369
370
371
376 public Object getParameterValue(String paramaterName)
377 throws ResourceInstantiationException{
378 return getParameterValue(this, paramaterName);
379 }
380
381
387 public void setParameterValue(String paramaterName, Object parameterValue)
388 throws ResourceInstantiationException{
389 BeanInfo resBeanInf = null;
391 try {
392 resBeanInf = getBeanInfo(this.getClass());
393 } catch(Exception e) {
394 throw new ResourceInstantiationException(
395 "Couldn't get bean info for resource " + this.getClass().getName()
396 + Strings.getNl() + "Introspector exception was: " + e
397 );
398 }
399 setParameterValue(this, resBeanInf, paramaterName, parameterValue);
400 }
401
402
408 public void setParameterValues(FeatureMap parameters)
409 throws ResourceInstantiationException{
410 setParameterValues(this, parameters);
411 }
412
413 private static int beanCount = 0;
414 private static Hashtable beanInfoCache = new Hashtable();
415
416 public static BeanInfo getBeanInfo (Class c) throws IntrospectionException
417 {
418 beanCount = beanCount + 1;
419 BeanInfo r = ((BeanInfo) beanInfoCache.get(c));
420 if (r == null) {
421 r = Introspector.getBeanInfo(c, Object.class);
422 beanInfoCache.put(c, r);
423 }
424 return r;
425 }
426
427 }