CompositeProxyFactory.java

biz/hammurapi/util/CompositeProxyFactory.java

Violations

Inspector Message Severity Location
Java Inspector 002 Empty catch block. 1 90:59
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Type is not documented 2 44:9
Java Inspector 089 Undocumented parameter classLoader 2 73:9
Java Inspector 089 Undocumented method 2 78:9
Java Inspector 089 Undocumented method 2 83:41
Java Inspector 089 Undocumented method 2 101:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 95:81
Java Inspector 046 Empty statements 3 90:91

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.lang.reflect.InvocationHandler;
26import java.lang.reflect.Method;
27import java.lang.reflect.Proxy;
28
29/**
30 * Creates a proxy which combines several classes and sequentially searches for
31 * matching method to invoke. This allows to implement lazy instantiation for
32 * situations where you have part of object data in cheaply accessible storage (cache or DB)
33 * and the other part in expensively accessible storage (e.g. XML file shall be
34 * parsed to access all object data).
35 * @author Pavel Vlasov
36 * @version $Revision: 1.1 $
37 */
38public class CompositeProxyFactory {
39
40 /**
41 * @author Pavel Vlasov
42 * @version $Revision: 1.1 $
43 */
44 public interface TargetFactory {
45
46 /**
47 * @return target class for the proxy to know which methods it implements.
48 * You might want to return only one of class interfaces or base classes
49 * to limit number of exposed methods.
50 */
51 Class getTargetClass();
52
53 /**
54 * @return target itself to perform invocation.
55 */
56 Object getTarget();
57 }
58
59 private ClassLoader classLoader;
60
61 /**
62 * Creates factory which uses default classloader.
63 */
64 public CompositeProxyFactory() {
65 super();
66 classLoader=getClass().getClassLoader();
67 }
68
69 /**
70 * Creates factory whith specified classloader which will be
71 * passed to createProxy(ClassLoader, Class[], TargetFactory[])
72 */
73 public CompositeProxyFactory(ClassLoader classLoader) {
74 super();
75 this.classLoader=classLoader;
76 }
77
78 public static Object createProxy(ClassLoader classLoader, Class[] interfaces, final TargetFactory[] targetFactories) {
79 return Proxy.newProxyInstance(
80 classLoader,
81 interfaces,
82 new InvocationHandler() {
83 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
84 for (int i=0; i<targetFactories.length; i++) {
85 try {
86 return targetFactories[i]
87 .getTargetClass()
88 .getMethod(method.getName(), method.getParameterTypes())
89 .invoke(targetFactories[i].getTarget(), args);
90 } catch (NoSuchMethodException e) {
91 // Ignore it.
92 }
93 }
94
95 throw new NoSuchMethodException("Method not found in targets: "+method);
96 }
97 });
98
99 }
100
101 public Object createProxy(Class[] interfaces, TargetFactory[] targetFactories) {
102 return createProxy(classLoader, interfaces, targetFactories);
103 }
104}
105