KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > bean > BeanFactory


1 package org.sapia.regis.bean;
2
3 import java.lang.reflect.Proxy JavaDoc;
4
5 import org.sapia.regis.Node;
6 import org.sapia.regis.Registry;
7 import org.sapia.regis.RegistryProvider;
8
9 /**
10  * An instance of this class allows wrapping a <code>Node</code> in a dynamic proxy
11  * that implements a given interface. The interface must have getter methods that correspond
12  * to the name of the properties on the given node.
13  *
14  * @author yduchesne
15  *
16  */

17 public class BeanFactory {
18   
19   /**
20    * @param reg the <code>Registry</code> from which the given node originates.
21    * @param node a <code>Node</code>.
22    * @param interf a user-defined interface.
23    * @return
24    */

25   public static Object JavaDoc newBeanInstanceFor(Registry reg, Node node, Class JavaDoc interf){
26     NodeInvocationHandler handler = new NodeInvocationHandler(reg, node, interf);
27     return newBeanInstanceFor(node, interf, handler);
28   }
29   
30   static Object JavaDoc newBeanInstanceFor(Node node, Class JavaDoc interf, NodeInvocationHandler handler){
31     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
32     if(loader == null){
33       loader = BeanFactory.class.getClassLoader();
34     }
35     return Proxy.newProxyInstance(loader, new Class JavaDoc[]{interf, RegistryProvider.class}, handler);
36   }
37
38 }
39
Popular Tags