Reference.java

biz/hammurapi/config/Reference.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Undocumented method 2 20:9
Java Inspector 089 Undocumented method 2 26:9
Java Inspector 089 Undocumented method 2 30:9
Java Inspector 089 Undocumented method 2 41:9
Java Inspector 089 Parameter type is not documented 2 51:9
Java Inspector 089 Undocumented method 2 54:25
Java Inspector 089 Parameter path is not documented 2 72:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 34:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 37:58
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 56:73

Source code

1package biz.hammurapi.config;
2
3import java.lang.reflect.InvocationHandler;
4import java.lang.reflect.Method;
5import java.lang.reflect.Proxy;
6
7/**
8 * This is a dynamic proxy wrapper for one component to obtain a reference to another.
9 * @author Pavel Vlasov
10 */
11public class Reference implements Wrapper, Component {
12
13 private Object master;
14
15 /**
16 * Proxy to the master. Master may not exist at the time of proxy creation.
17 */
18 private Object proxy;
19
20 public Object getMaster() {
21 return proxy;
22 }
23
24 private Object owner;
25
26 public void setOwner(Object owner) {
27 this.owner = owner;
28 }
29
30 public void start() throws ConfigurationException {
31 if (owner instanceof Context) {
32 master = ((Context) owner).get(path);
33 if (master==null) {
34 throw new ConfigurationException("Master is null, path: "+path);
35 }
36 } else {
37 throw new ConfigurationException("Owner does not implement "+Context.class.getName());
38 }
39 }
40
41 public void stop() throws ConfigurationException {
42 // TODO Auto-generated method stub
43
44 }
45
46 /**
47 * Interface which dynamic proxy shall implement.
48 * @param type
49 * @throws ClassNotFoundException
50 */
51 public void setType(String type) throws ClassNotFoundException {
52 InvocationHandler ih = new InvocationHandler() {
53
54 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
55 if (master==null) {
56 throw new IllegalStateException("Master is null, path: "+path);
57 }
58 return method.invoke(master, args);
59 }
60
61 };
62
63 proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {Class.forName(type)}, ih);
64 }
65
66 private String path;
67
68 /**
69 * Path to the referenced component
70 * @param path
71 */
72 public void setPath(String path) {
73 this.path = path;
74 }
75
76}
77