KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > CglibBeanInterfaceFactory


1 // Copyright 2006 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.service.impl;
16
17 import java.lang.reflect.Method JavaDoc;
18
19 import net.sf.cglib.proxy.Callback;
20 import net.sf.cglib.proxy.CallbackFilter;
21 import net.sf.cglib.proxy.Enhancer;
22 import net.sf.cglib.proxy.FixedValue;
23 import net.sf.cglib.proxy.NoOp;
24
25 import org.apache.hivemind.Location;
26 import org.apache.hivemind.definition.ImplementationConstructionContext;
27 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor;
28
29 /**
30  *
31  * @author James Carman
32  */

33 public class CglibBeanInterfaceFactory extends AbstractServiceImplementationConstructor
34 {
35
36     public CglibBeanInterfaceFactory(Location location, String JavaDoc contributingModuleId)
37     {
38         super(location);
39     }
40
41     public Object JavaDoc constructCoreServiceImplementation(ImplementationConstructionContext context)
42     {
43         return createCglibBean();
44     }
45
46
47     /**
48      * @return
49      */

50     public static BeanInterface createCglibBean() {
51         Enhancer enhancer = new Enhancer();
52         enhancer.setClassLoader( BeanInterface.class.getClassLoader() );
53         enhancer.setInterfaces( new Class JavaDoc[] { BeanInterface.class } );
54         enhancer.setCallbackFilter( new InterfaceMethodFilter() );
55         enhancer.setCallbacks( new Callback[] { new FixedValue()
56                 {
57                     public Object JavaDoc loadObject() throws Exception JavaDoc {
58
59                         return "Hello, World!";
60                     }
61             
62                 }, NoOp.INSTANCE } );
63         
64         return (BeanInterface)enhancer.create();
65     }
66     
67     
68     private static class InterfaceMethodFilter implements CallbackFilter
69     {
70
71         public int accept(Method JavaDoc method)
72         {
73             if( method.getName().equals( "interfaceMethod" ) )
74             {
75                 return 0;
76             }
77             return 1;
78         }
79         
80     }
81
82 }
83
Popular Tags