KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > config > Configurator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.kernel.plugins.config;
23
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.jboss.beans.info.spi.BeanInfo;
31 import org.jboss.beans.info.spi.PropertyInfo;
32 import org.jboss.beans.metadata.spi.BeanMetaData;
33 import org.jboss.beans.metadata.spi.ClassLoaderMetaData;
34 import org.jboss.beans.metadata.spi.ConstructorMetaData;
35 import org.jboss.beans.metadata.spi.ParameterMetaData;
36 import org.jboss.beans.metadata.spi.PropertyMetaData;
37 import org.jboss.beans.metadata.spi.ValueMetaData;
38 import org.jboss.joinpoint.plugins.Config;
39 import org.jboss.joinpoint.spi.ConstructorJoinpoint;
40 import org.jboss.joinpoint.spi.Joinpoint;
41 import org.jboss.joinpoint.spi.JoinpointException;
42 import org.jboss.joinpoint.spi.JoinpointFactory;
43 import org.jboss.joinpoint.spi.MethodJoinpoint;
44 import org.jboss.joinpoint.spi.TargettedJoinpoint;
45 import org.jboss.kernel.spi.config.KernelConfig;
46 import org.jboss.reflect.spi.ConstructorInfo;
47 import org.jboss.reflect.spi.MethodInfo;
48 import org.jboss.reflect.spi.TypeInfo;
49
50 /**
51  * Configuration utilities.
52  *
53  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
54  * @version $Revision: 57320 $
55  */

56 public class Configurator extends Config
57 {
58    /**
59     * Instantiate and configure a bean
60     *
61     * @param config the confg
62     * @param info the bean info
63     * @param metaData the bean metadata
64     * @return the instantiated and configured object
65     * @throws Throwable for any error
66     */

67    public static Object JavaDoc instantiateAndConfigure(KernelConfig config, BeanInfo info, BeanMetaData metaData) throws Throwable JavaDoc
68    {
69       Object JavaDoc result = instantiate(config, info, metaData);
70       if (metaData != null)
71          configure(result, info, metaData);
72       return result;
73    }
74    
75    /**
76     * Instantiate a bean
77     *
78     * @param config the kernel config
79     * @param info the bean info
80     * @param metaData the bean metadata
81     * @return the instantiated object
82     * @throws Throwable for any error
83     */

84    public static Object JavaDoc instantiate(KernelConfig config, BeanInfo info, BeanMetaData metaData) throws Throwable JavaDoc
85    {
86       boolean trace = log.isTraceEnabled();
87       if (trace)
88          log.trace("Instantiating info=" + info + " metaData=" + metaData);
89
90       ConstructorMetaData constructor = null;
91       if (metaData != null)
92          constructor = metaData.getConstructor();
93       Joinpoint joinPoint = getConstructorJoinPoint(config, info, constructor, metaData);
94       return joinPoint.dispatch();
95    }
96    
97    /**
98     * Get a constructor joinpoint
99     *
100     * @param config the kernel config
101     * @param info the bean info
102     * @param metaData the constructor metadata
103     * @param beanMetaData
104     * @return the joinpoint
105     * @throws Throwable for any error
106     */

107    public static Joinpoint getConstructorJoinPoint(KernelConfig config, BeanInfo info, ConstructorMetaData metaData, BeanMetaData beanMetaData)
108       throws Throwable JavaDoc
109    {
110       boolean trace = log.isTraceEnabled();
111       
112       if (trace)
113          log.trace("Get constructor joinpoint info=" + info + " constructor=" + metaData);
114
115       if (config == null)
116          throw new IllegalArgumentException JavaDoc("Null config");
117
118       if (metaData != null)
119       {
120          ClassLoader JavaDoc cl = getClassLoader(beanMetaData);
121
122          ValueMetaData vmd = metaData.getValue();
123          if (vmd != null)
124          {
125             TypeInfo typeInfo = null;
126             if (info != null)
127                typeInfo = info.getClassInfo();
128             return new ValueJoinpoint(vmd, typeInfo, cl);
129          }
130          
131          vmd = metaData.getFactory();
132          if (vmd != null)
133          {
134             // Get the factory
135
Object JavaDoc factory = vmd.getValue(null, cl);
136
137             // Get the parameters
138
List JavaDoc parameters = metaData.getParameters();
139
140             // Describe the factory
141
BeanInfo factoryInfo = config.getBeanInfo(factory.getClass());
142             
143             // Find the method
144
MethodJoinpoint joinPoint = findMethod(trace, factoryInfo, cl, metaData.getFactoryMethod(), parameters, false, true);
145             joinPoint.setTarget(factory);
146             MethodInfo minfo = joinPoint.getMethodInfo();
147
148             // Set the parameters
149
if (minfo != null)
150             {
151                TypeInfo[] pinfos = minfo.getParameterTypes();
152                Object JavaDoc[] params = getParameters(trace, cl, pinfos, parameters);
153                joinPoint.setArguments(params);
154             }
155             return joinPoint;
156          }
157          
158          String JavaDoc factoryClassName = metaData.getFactoryClass();
159          if (factoryClassName != null)
160          {
161             // Get the parameters
162
List JavaDoc parameters = metaData.getParameters();
163
164             BeanInfo factoryInfo = config.getBeanInfo(factoryClassName, cl);
165
166             // Find the method
167
MethodJoinpoint joinPoint = findMethod(trace, factoryInfo, cl, metaData.getFactoryMethod(), parameters, true, true);
168             MethodInfo minfo = joinPoint.getMethodInfo();
169
170             // Set the parameters
171
if (minfo != null)
172             {
173                TypeInfo[] pinfos = minfo.getParameterTypes();
174                Object JavaDoc[] params = getParameters(trace, cl, pinfos, parameters);
175                joinPoint.setArguments(params);
176             }
177             return joinPoint;
178          }
179          
180          // Find the constructor
181
ConstructorJoinpoint joinPoint = findConstructor(trace, info, metaData, beanMetaData);
182          ConstructorInfo cinfo = joinPoint.getConstructorInfo();
183          
184          // Set the parameters
185
if (cinfo != null)
186          {
187             TypeInfo[] pinfos = cinfo.getParameterTypes();
188             Object JavaDoc[] params = getParameters(trace, cl, pinfos, metaData.getParameters());
189             joinPoint.setArguments(params);
190          }
191          return joinPoint;
192       }
193       
194       // Default constructor
195
return findConstructor(trace, info, metaData, beanMetaData);
196    }
197    
198    /**
199     * Find a constructor
200     *
201     * @param trace whether trace is enabled
202     * @param info the bean info
203     * @param metaData the constructor metadata
204     * @param beanMetaData
205     * @return the constructor join point
206     * @throws Exception for any error
207     */

208    public static ConstructorJoinpoint findConstructor(boolean trace, BeanInfo info, ConstructorMetaData metaData, BeanMetaData beanMetaData) throws Exception JavaDoc
209    {
210       ConstructorInfo cinfo = resolveConstructor(trace, info, metaData);
211       JoinpointFactory jpf = info.getJoinpointFactory();
212       return jpf.getConstructorJoinpoint(cinfo);
213    }
214    
215    /**
216     * Resolve a constructor
217     *
218     * @param trace whether trace is enabled
219     * @param info the bean info
220     * @param metaData the constructor metadata
221     * @return the constructor info
222     */

223    public static ConstructorInfo resolveConstructor(boolean trace, BeanInfo info, ConstructorMetaData metaData)
224    {
225       if (info == null)
226          throw new IllegalArgumentException JavaDoc("Null bean info");
227
228       List JavaDoc params = Collections.EMPTY_LIST;
229       if (metaData != null && metaData.getParameters() != null)
230          params = metaData.getParameters();
231       String JavaDoc[] paramTypes = new String JavaDoc[params.size()];
232       if (params.isEmpty() == false)
233       {
234          int x = 0;
235          for (Iterator JavaDoc i = params.iterator(); i.hasNext();)
236          {
237             ParameterMetaData pdata = (ParameterMetaData) i.next();
238             paramTypes[x++] = pdata.getType();
239          }
240       }
241       return findConstructorInfo(info.getClassInfo(), paramTypes);
242    }
243
244    /**
245     * Configure a bean
246     *
247     * @param object the object to configure
248     * @param info the bean info
249     * @param metaData the bean metadata
250     * @throws Throwable for any error
251     */

252    public static void configure(Object JavaDoc object, BeanInfo info, BeanMetaData metaData) throws Throwable JavaDoc
253    {
254       boolean trace = log.isTraceEnabled();
255
256       if (object == null)
257          throw new IllegalArgumentException JavaDoc("Null object");
258       if (info == null)
259          throw new IllegalArgumentException JavaDoc("Null bean info");
260       if (metaData == null)
261          throw new IllegalArgumentException JavaDoc("Null bean metadata");
262
263       Set JavaDoc properties = metaData.getProperties();
264       if (properties != null && properties.isEmpty() == false)
265       {
266          ClassLoader JavaDoc cl = getClassLoader(metaData);
267          
268          for (Iterator JavaDoc i = metaData.getProperties().iterator(); i.hasNext();)
269          {
270             PropertyMetaData property = (PropertyMetaData) i.next();
271             configure(trace, object, info, cl, property);
272          }
273       }
274    }
275
276    /**
277     * Configure a bean property
278     *
279     * @param object the object to configure
280     * @param info the bean info
281     * @param cl the classloader
282     * @param metaData the property metadata
283     * @throws Throwable for any error
284     */

285    public static void configure(Object JavaDoc object, BeanInfo info, ClassLoader JavaDoc cl, PropertyMetaData metaData) throws Throwable JavaDoc
286    {
287       boolean trace = log.isTraceEnabled();
288       configure(trace, object, info, cl, metaData);
289    }
290
291    /**
292     * Configure a bean property
293     *
294     * @param trace whether trace is enabled
295     * @param object the object to configure
296     * @param info the bean info
297     * @param cl the classloader
298     * @param metaData the property metadata
299     * @throws Throwable for any error
300     */

301    public static void configure(boolean trace, Object JavaDoc object, BeanInfo info, ClassLoader JavaDoc cl, PropertyMetaData metaData) throws Throwable JavaDoc
302    {
303       PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName());
304       configure(trace, object, ainfo, cl, metaData);
305    }
306
307    /**
308     * Configure a bean property
309     *
310     * @param object the object to configure
311     * @param info the property info
312     * @param cl the classloader
313     * @param metaData the property metadata
314     * @throws Throwable for any error
315     */

316    public static void configure(Object JavaDoc object, PropertyInfo info, ClassLoader JavaDoc cl, PropertyMetaData metaData) throws Throwable JavaDoc
317    {
318       boolean trace = log.isTraceEnabled();
319       configure(trace, object, info, cl, metaData);
320    }
321
322    /**
323     * Configure a bean property
324     *
325     * @param trace whether trace is enabled
326     * @param object the object to configure
327     * @param info the property info
328     * @param cl the classloader
329     * @param metaData the property metadata
330     * @throws Throwable for any error
331     */

332    public static void configure(boolean trace, Object JavaDoc object, PropertyInfo info, ClassLoader JavaDoc cl, PropertyMetaData metaData) throws Throwable JavaDoc
333    {
334       if (trace)
335          log.trace("Configuring info=" + info + " metaData=" + metaData);
336       
337       TargettedJoinpoint joinPoint = getPropertySetterJoinPoint(trace, info, cl, metaData.getValue());
338       joinPoint.setTarget(object);
339       
340       if (trace)
341          log.trace("Setting property " + joinPoint);
342       
343       joinPoint.dispatch();
344    }
345
346    /**
347     * Get property getter for an property
348     *
349     * @param info the bean info
350     * @param property the property name
351     * @return the joinpoint
352     * @throws Throwable for any error
353     */

354    public static TargettedJoinpoint getPropertyGetterJoinPoint(BeanInfo info, String JavaDoc property) throws Throwable JavaDoc
355    {
356       boolean trace = log.isTraceEnabled();
357       PropertyInfo ainfo = resolveProperty(trace, info, property);
358       return getPropertyGetterJoinPoint(trace, ainfo);
359    }
360
361    /**
362     * Get an property getter joinpoint
363     *
364     * @param trace whether trace is enabled
365     * @param info the property info
366     * @return the joinpoint
367     * @throws Throwable for any error
368     */

369    public static TargettedJoinpoint getPropertyGetterJoinPoint(boolean trace, PropertyInfo info) throws Throwable JavaDoc
370    {
371       if (trace)
372          log.trace("Get property setter join point info=" + info);
373
374       if (info == null)
375          throw new IllegalArgumentException JavaDoc("Null property info");
376       
377       JoinpointFactory jpf = info.getBeanInfo().getJoinpointFactory();
378       MethodInfo minfo = info.getGetter();
379       return getMethodJoinpoint(null, jpf, minfo.getName(), null, null);
380    }
381
382    /**
383     * Get the property setters for a bean
384     *
385     * @param info the bean info
386     * @param metaData the bean metadata
387     * @return the property setters
388     * @throws Throwable for any error
389     */

390    public static Set JavaDoc<TargettedJoinpoint> getPropertySetterJoinPoints(BeanInfo info, BeanMetaData metaData) throws Throwable JavaDoc
391    {
392       boolean trace = log.isTraceEnabled();
393
394       if (info == null)
395          throw new IllegalArgumentException JavaDoc("Null bean info");
396       if (metaData == null)
397          throw new IllegalArgumentException JavaDoc("Null bean metadata");
398       
399       Set JavaDoc<TargettedJoinpoint> result = new HashSet JavaDoc<TargettedJoinpoint>();
400       Set JavaDoc<PropertyMetaData> propertys = metaData.getProperties();
401       if (propertys != null && propertys.isEmpty() == false)
402       {
403          ClassLoader JavaDoc cl = getClassLoader(metaData);
404          
405          for (Iterator JavaDoc i = metaData.getProperties().iterator(); i.hasNext();)
406          {
407             PropertyMetaData property = (PropertyMetaData) i.next();
408             TargettedJoinpoint joinPoint = getPropertySetterJoinPoint(trace, info, cl, property);
409             result.add(joinPoint);
410          }
411       }
412       
413       return result;
414    }
415
416    /**
417     * Get property setter for an property
418     *
419     * @param info the bean info
420     * @param cl the classloader
421     * @param metaData the property metadata
422     * @return the joinpoint
423     * @throws Throwable for any error
424     */

425    public static TargettedJoinpoint getPropertySetterJoinPoint(BeanInfo info, ClassLoader JavaDoc cl, PropertyMetaData metaData) throws Throwable JavaDoc
426    {
427       boolean trace = log.isTraceEnabled();
428       return getPropertySetterJoinPoint(trace, info, cl, metaData);
429    }
430
431    /**
432     * Get property setter for an property
433     *
434     * @param trace whether trace is enabled
435     * @param info the bean info
436     * @param cl the classloader
437     * @param metaData the property metadata
438     * @return the joinpoint
439     * @throws Throwable for any error
440     */

441    public static TargettedJoinpoint getPropertySetterJoinPoint(boolean trace, BeanInfo info, ClassLoader JavaDoc cl, PropertyMetaData metaData) throws Throwable JavaDoc
442    {
443       PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName());
444       return getPropertySetterJoinPoint(trace, ainfo, cl, metaData.getValue());
445    }
446
447    /**
448     * Get property setter for an property
449     *
450     * @param info the property info
451     * @param cl the classloader
452     * @param metaData the property metadata
453     * @return the joinpoint
454     * @throws Throwable for any error
455     */

456    public static TargettedJoinpoint getPropertySetterJoinPoint(PropertyInfo info, ClassLoader JavaDoc cl, PropertyMetaData metaData) throws Throwable JavaDoc
457    {
458       boolean trace = log.isTraceEnabled();
459       return getPropertySetterJoinPoint(trace, info, cl, metaData.getValue());
460    }
461
462    /**
463     * Get property setter for an property
464     *
465     * @param info the bean info
466     * @param property the property name
467     * @param cl the classloader
468     * @param vmd the value meta data
469     * @return the joinpoint
470     * @throws Throwable for any error
471     */

472    public static TargettedJoinpoint getPropertySetterJoinPoint(BeanInfo info, String JavaDoc property, ClassLoader JavaDoc cl, ValueMetaData vmd) throws Throwable JavaDoc
473    {
474       boolean trace = log.isTraceEnabled();
475       PropertyInfo ainfo = resolveProperty(trace, info, property);
476       return getPropertySetterJoinPoint(trace, ainfo, cl, vmd);
477    }
478
479    /**
480     * Get an property setter joinpoint
481     *
482     * @param trace whether trace is enabled
483     * @param info the property info
484     * @param cl the classloader
485     * @param metaData the value metadata
486     * @return the joinpoint
487     * @throws Throwable for any error
488     */

489    public static TargettedJoinpoint getPropertySetterJoinPoint(boolean trace, PropertyInfo info, ClassLoader JavaDoc cl, ValueMetaData metaData) throws Throwable JavaDoc
490    {
491       if (trace)
492          log.trace("Get property setter join point info=" + info + " metaData=" + metaData);
493
494       if (info == null)
495          throw new IllegalArgumentException JavaDoc("Null property info");
496       if (metaData == null)
497          throw new IllegalArgumentException JavaDoc("Null value metadata");
498       
499       TypeInfo type = info.getType();
500       Object JavaDoc value = metaData.getValue(type, cl);
501       JoinpointFactory jpf = info.getBeanInfo().getJoinpointFactory();
502       MethodInfo minfo = info.getSetter();
503       if (minfo == null)
504          throw new IllegalArgumentException JavaDoc("No setter configured for property: " + info);
505       String JavaDoc[] parameterTypes = getParameterTypes(trace, minfo.getParameterTypes());
506       return getMethodJoinpoint(null, jpf, minfo.getName(), parameterTypes, new Object JavaDoc[] { value });
507    }
508    
509    /**
510     * Unconfigure a bean
511     *
512     * @param object the object to unconfigure
513     * @param info the bean info
514     * @param metaData the bean metadata
515     * @throws Throwable for any error
516     */

517    public static void unconfigure(Object JavaDoc object, BeanInfo info, BeanMetaData metaData) throws Throwable JavaDoc
518    {
519       if (object == null)
520          throw new IllegalArgumentException JavaDoc("Null object");
521       if (info == null)
522          throw new IllegalArgumentException JavaDoc("Null bean info");
523       if (metaData == null)
524          throw new IllegalArgumentException JavaDoc("Null bean metadata");
525
526       Set JavaDoc propertys = metaData.getProperties();
527       if (propertys != null && propertys.isEmpty() == false)
528       {
529          for (Iterator JavaDoc i = metaData.getProperties().iterator(); i.hasNext();)
530          {
531             PropertyMetaData property = (PropertyMetaData) i.next();
532             unconfigure(object, info, property);
533          }
534       }
535    }
536
537    /**
538     * Unconfigure a bean property
539     *
540     * @param object the object to unconfigure
541     * @param info the bean info
542     * @param metaData the property metadata
543     * @throws Throwable for any error
544     */

545    public static void unconfigure(Object JavaDoc object, BeanInfo info, PropertyMetaData metaData) throws Throwable JavaDoc
546    {
547       boolean trace = log.isTraceEnabled();
548       PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName());
549       unconfigure(trace, object, ainfo, metaData);
550    }
551
552    /**
553     * UnConfigure a bean property
554     *
555     * @param trace whether trace is enabled
556     * @param object the object to configure
557     * @param info the property info
558     * @param metaData the property metadata
559     * @throws Throwable for any error
560     */

561    public static void unconfigure(boolean trace, Object JavaDoc object, PropertyInfo info, PropertyMetaData metaData) throws Throwable JavaDoc
562    {
563       if (trace)
564          log.trace("Unconfiguring info=" + info + " metaData=" + metaData);
565       
566       TargettedJoinpoint joinPoint = getPropertyNullerJoinPoint(info, metaData);
567       joinPoint.setTarget(object);
568       
569       if (trace)
570          log.trace("Unsetting property " + joinPoint);
571       
572       joinPoint.dispatch();
573    }
574    
575    /**
576     * Get property nuller joinpoints for a bean
577     *
578     * @param info the bean info
579     * @param metaData the bean metadata
580     * @return the join points
581     * @throws Throwable for any error
582     */

583    public static Set JavaDoc<TargettedJoinpoint> getPropertyNullerJoinPoints(BeanInfo info, BeanMetaData metaData) throws Throwable JavaDoc
584    {
585       if (info == null)
586          throw new IllegalArgumentException JavaDoc("Null bean info");
587       if (metaData == null)
588          throw new IllegalArgumentException JavaDoc("Null bean metadata");
589       
590       Set JavaDoc<TargettedJoinpoint> result = new HashSet JavaDoc<TargettedJoinpoint>();
591       Set JavaDoc<PropertyMetaData> propertys = metaData.getProperties();
592       if (propertys != null && propertys.isEmpty() == false)
593       {
594          for (Iterator JavaDoc i = metaData.getProperties().iterator(); i.hasNext();)
595          {
596             PropertyMetaData property = (PropertyMetaData) i.next();
597             TargettedJoinpoint joinPoint = getPropertyNullerJoinPoint(info, property);
598             result.add(joinPoint);
599          }
600       }
601       return result;
602    }
603
604    /**
605     * Get property nuller joinpoint for a property
606     *
607     * @param info the bean info
608     * @param metaData the property metadata
609     * @return the join point
610     * @throws Throwable for any error
611     */

612    public static TargettedJoinpoint getPropertyNullerJoinPoint(BeanInfo info, PropertyMetaData metaData) throws Throwable JavaDoc
613    {
614       boolean trace = log.isTraceEnabled();
615       PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName());
616       return getPropertyNullerJoinPoint(ainfo, metaData);
617    }
618
619    /**
620     * Get property nuller joinpoint for a property
621     *
622     * @param info the property info
623     * @param metaData the property metadata
624     * @return the join point
625     * @throws Throwable for any error
626     */

627    public static TargettedJoinpoint getPropertyNullerJoinPoint(PropertyInfo info, PropertyMetaData metaData) throws Throwable JavaDoc
628    {
629       boolean trace = log.isTraceEnabled();
630       if (trace)
631          log.trace("Get property nuller join point info=" + info + " metaData=" + metaData);
632       
633       if (info == null)
634          throw new IllegalArgumentException JavaDoc("Null property info");
635       
636       JoinpointFactory jpf = info.getBeanInfo().getJoinpointFactory();
637       MethodInfo minfo = info.getSetter();
638       String JavaDoc[] parameterTypes = getParameterTypes(trace, minfo.getParameterTypes());
639       return getMethodJoinpoint(null, jpf, minfo.getName(), parameterTypes, new Object JavaDoc[] { null });
640    }
641    
642    /**
643     * Get the property info
644     *
645     * @param trace whether trace is enabled
646     * @param info the bean info
647     * @param name the property name
648     * @return the property info
649     * @throws Throwable for any error
650     */

651    public static PropertyInfo resolveProperty(boolean trace, BeanInfo info, String JavaDoc name) throws Throwable JavaDoc
652    {
653       if (info == null)
654          throw new IllegalArgumentException JavaDoc("Null bean info");
655       if (name == null)
656          throw new IllegalArgumentException JavaDoc("Null name");
657       
658       Set JavaDoc properties = info.getProperties();
659       if (properties != null && properties.size() > 0)
660       {
661          for (Iterator JavaDoc i = properties.iterator(); i.hasNext();)
662          {
663             PropertyInfo ainfo = (PropertyInfo) i.next();
664             if (name.equals(ainfo.getName()))
665                return ainfo;
666          }
667       }
668       
669       throw new JoinpointException("Property " + name + " not found for " + info);
670    }
671    
672    /**
673     * Find a method
674     *
675     * @param info the bean info
676     * @param cl the classloader
677     * @param name the method name
678     * @param parameters the parameter metadata
679     * @param isStatic whether the method is static
680     * @param isPublic whether the method is public
681     * @return the method join point
682     * @throws Throwable for any error
683     */

684    public static MethodJoinpoint findMethod(BeanInfo info, ClassLoader JavaDoc cl, String JavaDoc name, List JavaDoc parameters, boolean isStatic, boolean isPublic) throws Throwable JavaDoc
685    {
686       boolean trace = log.isTraceEnabled();
687       return findMethod(trace, info, cl, name, parameters, isStatic, isPublic);
688    }
689    
690    /**
691     * Find a method
692     *
693     * @param trace whether trace is enabled
694     * @param info the bean info
695     * @param cl the classloader
696     * @param name the method name
697     * @param parameters the parameter metadata
698     * @param isStatic whether the method is static
699     * @param isPublic whether the method is public
700     * @return the method join point
701     * @throws Throwable for any error
702     */

703    public static MethodJoinpoint findMethod(boolean trace, BeanInfo info, ClassLoader JavaDoc cl, String JavaDoc name, List JavaDoc parameters, boolean isStatic, boolean isPublic) throws Throwable JavaDoc
704    {
705       if (info == null)
706          throw new IllegalArgumentException JavaDoc("Null bean info");
707       if (name == null)
708          throw new IllegalArgumentException JavaDoc("Null name");
709
710       String JavaDoc[] paramTypes = getParameterTypes(trace, parameters);
711       MethodInfo minfo = findMethodInfo(info.getClassInfo(), name, paramTypes, isStatic, isPublic);
712       JoinpointFactory jpf = info.getJoinpointFactory();
713       MethodJoinpoint joinPoint = jpf.getMethodJoinpoint(minfo);
714
715       // Set the parameters
716
if (minfo != null)
717       {
718          TypeInfo[] pinfos = minfo.getParameterTypes();
719          Object JavaDoc[] params = getParameters(trace, cl, pinfos, parameters);
720          joinPoint.setArguments(params);
721       }
722       
723       return joinPoint;
724    }
725    
726    /**
727     * Get the parameters types
728     *
729     * @param trace whether trace is enabled
730     * @param parameters the parameter metadata
731     * @return an array of parameter types
732     * @throws Throwable for any error
733     */

734    public static String JavaDoc[] getParameterTypes(boolean trace, List JavaDoc parameters) throws Throwable JavaDoc
735    {
736       if (parameters == null)
737          return null;
738       
739       String JavaDoc[] paramTypes = new String JavaDoc[parameters.size()];
740       int x = 0;
741       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
742       {
743          ParameterMetaData pmd = (ParameterMetaData) i.next();
744          paramTypes[x++] = pmd.getType();
745       }
746       return paramTypes;
747    }
748    
749    /**
750     * Get the parameters types
751     *
752     * @param trace whether trace is enabled
753     * @param parameters the parameter types
754     * @return an array of parameter types
755     * @throws Throwable for any error
756     */

757    public static String JavaDoc[] getParameterTypes(boolean trace, TypeInfo[] parameters) throws Throwable JavaDoc
758    {
759       if (parameters == null)
760          return null;
761       
762       String JavaDoc[] paramTypes = new String JavaDoc[parameters.length];
763       int x = 0;
764       for (int i = 0; i < parameters.length; ++i)
765          paramTypes[x++] = parameters[i].getName();
766       return paramTypes;
767    }
768    
769    /**
770     * Get the parameters
771     *
772     * @param trace whether trace is enabled
773     * @param cl the classloader
774     * @param pinfos the parameter infos
775     * @param parameters the parameter metadata
776     * @return an array of parameters
777     * @throws Throwable for any error
778     */

779    public static Object JavaDoc[] getParameters(boolean trace, ClassLoader JavaDoc cl, TypeInfo[] pinfos, List JavaDoc parameters) throws Throwable JavaDoc
780    {
781       if (parameters == null)
782          return null;
783       
784       Object JavaDoc[] params = new Object JavaDoc[parameters.size()];
785       int x = 0;
786       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
787       {
788          ParameterMetaData pdata = (ParameterMetaData) i.next();
789          ValueMetaData vmd = pdata.getValue();
790          params[x] = vmd.getValue(pinfos[x], cl);
791          x++;
792       }
793       return params;
794    }
795
796    /**
797     * Get the classloader for some BeanMetaData
798     *
799     * @param metaData the metaData
800     * @return the classloader
801     * @throws Throwable for any error
802     */

803    public static ClassLoader JavaDoc getClassLoader(BeanMetaData metaData) throws Throwable JavaDoc
804    {
805       ClassLoaderMetaData clmd = null;
806       if (metaData != null)
807          clmd = metaData.getClassLoader();
808       return getClassLoader(clmd);
809    }
810
811    /**
812     * Get the classloader for some ClassLoaderMetaData
813     *
814     * @param metaData the metaData
815     * @return the classloader
816     * @throws Throwable for any error
817     */

818    public static ClassLoader JavaDoc getClassLoader(ClassLoaderMetaData metaData) throws Throwable JavaDoc
819    {
820       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
821       ClassLoader JavaDoc cl = null;
822       if (metaData != null)
823       {
824             ValueMetaData clVMD = metaData.getClassLoader();
825             if (clVMD != null)
826             {
827                Object JavaDoc object = clVMD.getValue(null, tcl);
828                if (object != null && object instanceof ClassLoader JavaDoc == false)
829                   throw new IllegalArgumentException JavaDoc("Configured object is not a classloader " + metaData);
830                cl = (ClassLoader JavaDoc) object;
831             }
832       }
833       if (cl == null)
834          cl = tcl;
835       return cl;
836    }
837    
838    /**
839     * ValueJoinpoint.
840     */

841    private static class ValueJoinpoint implements Joinpoint
842    {
843       /** The value metadata */
844       private ValueMetaData vmd;
845       
846       /** The type info */
847       private TypeInfo info;
848       
849       /** The classloader */
850       private ClassLoader JavaDoc cl;
851       
852       /**
853        * Create a new ValueJoinpoint.
854        *
855        * @param vmd the value metadata
856        * @param info the type info
857        * @param cl the classloader
858        */

859       public ValueJoinpoint(ValueMetaData vmd, TypeInfo info, ClassLoader JavaDoc cl)
860       {
861          this.vmd = vmd;
862          this.info = info;
863          this.cl = cl;
864       }
865       
866       public Object JavaDoc dispatch() throws Throwable JavaDoc
867       {
868          return vmd.getValue(info, cl);
869       }
870
871       public String JavaDoc toHumanReadableString()
872       {
873          return vmd.toShortString();
874       }
875
876       public Object JavaDoc clone()
877       {
878          try
879          {
880             return super.clone();
881          }
882          catch (CloneNotSupportedException JavaDoc e)
883          {
884             throw new Error JavaDoc(e);
885          }
886       }
887       
888    }
889 }
890
Popular Tags