Hi,
 
I tried to create simple prototype to see how much effort would go into 
creating the proxy implementation.
The project dynclass.sourceforge.net makes it very simple (the project isn't 
maitained actively, so we should possible find an alternative or maintain it 
within mmbase).
 
Besides making some classes public in the dynclass project, you will have to 
create a method, to define a class definition for your dynamic class that 
represents your node. This is a start of the method that only exposes the 
fields, but you could also expose relations and other usefull stuff.
 
public class MMBeanCreator {
    public static Class createClassForNode(NodeManager mgr) {
     FieldList fl = mgr.getFields();
        FieldIterator iter = fl.fieldIterator();
        List methods = new ArrayList(fl.size() * 2);
        boolean needsBeanInfo = false;
        while (iter.hasNext()) {
         Field f = iter.nextField();
            String name = (String) f.getName();
            String suffix = PropertyMethodNameTx.encodeProperty(name);
            boolean isStandardProperty = suffix.length() == name.length();
            
            String getterPrefix = (isStandardProperty? "get" : "g$t");
            methods.add(new MethodDefinition(getterPrefix + suffix,
                                             Object.class,
                                             null));
            String setterPrefix = (isStandardProperty? "set" : "s$t");
            methods.add(new MethodDefinition(setterPrefix + suffix,
                                             Void.TYPE,
                                             new Class[] { Object.class }));
            needsBeanInfo |= !isStandardProperty;
        }
        
        MethodDefinition[] ml = (MethodDefinition[])
            methods.toArray(new MethodDefinition[methods.size()]);
        ClassDefinition classDef = new ClassDefinition(Object.class, null, ml);
        
        Class cls = ClassCreator.defineClass(classDef);
        if (needsBeanInfo) {
            ClassDefinition infoClass = new 
ClassDefinition(DynBeanInfoBase.class);
            DynClassLoader cl = (DynClassLoader) cls.getClassLoader();
            cl.defineClass(infoClass, cls.getName() + "BeanInfo");
        }
        return cls;
    }
}

 
Create a class that implements InvocationHandler that will translate the method 
calls on the proxy to the actual methods on the node. Very simple 
implementation that only works for getting value for field, while a lot of 
other things are possible.
 
public class MMNodeInvocationHandler implements InvocationHandler {
 private Node node;
 
 public MMNodeInvocationHandler(Node n) {
  this.node = n;
 }
 
 public Object invoke(Object arg0, Method arg1, Object[] arg2)
   throws Throwable {
  System.out.println("in invoke on " + arg0.getClass().getName());
  return node.getObjectValue(arg1.getName());
 }
}

 
The following call creates the class that is the counterpart of the nodemanager
 
  Class nodeBeanClass = MMBeanCreator.createClassForNode(node.getNodeManager());

The following call wraps the node with an instance of the class that is usefull 
for environments that understand java beans. EL and jxpath for example.
 
  Object nodeBean = ClassCreator.newInstance(nodeBeanClass, new 
MMNodeInvocationHandler(node));

 
No test the bean with your favorite tooling.
 
 
HTH
Michael

Disclaimer

Dit bericht met eventuele bijlagen is vertrouwelijk en uitsluitend bestemd voor 
de geadresseerde. Indien u niet de bedoelde ontvanger bent, wordt u verzocht de 
afzender te waarschuwen en dit bericht met eventuele bijlagen direct te 
verwijderen en/of te vernietigen. Het is niet toegestaan dit bericht en 
eventuele bijlagen te vermenigvuldigen, door te sturen, openbaar te maken, op 
te slaan of op andere wijze te gebruiken. Ordina N.V. en/of haar 
groepsmaatschappijen accepteren geen verantwoordelijkheid of aansprakelijkheid 
voor schade die voortvloeit uit (de inhoud van) de verzending van dit bericht.

This e-mail and any attachments are confidential and is solely intended for the 
addressee only. If you are not the intended recipient, please notify the sender 
and delete and/or destroy this message and any attachments immediately. It is 
prohibited to copy, to distribute, to disclose or to use this e-mail and any 
attachments in any other way. Ordina N.V. and/or its group companies do not 
accept any responsibility nor liability for any damage resulting from (the 
content of) the transmission of this message.

<<winmail.dat>>

_______________________________________________
Developers mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/developers

Reply via email to