Restructured the project creating a router directory for the router bundle implementa...
[xmlrouter] / router / impl / src / main / java / org / wamblee / xmlrouter / impl / IdentifiablePrefixProxyFactory.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Proxy;
22
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.Identifiable;
25
26 /**
27  * This proxy factory creates proxies for identifiable objects that add a prefix
28  * to the ids.
29  * 
30  * @author Erik Brakkee
31  * 
32  * @param <T>
33  */
34 public class IdentifiablePrefixProxyFactory<T> {
35
36     public static final class IdPrefixInvocationHandler<T> implements
37         InvocationHandler {
38
39         private String prefix;
40         private T service;
41
42         public IdPrefixInvocationHandler(String aPrefix, T aService) {
43             prefix = aPrefix;
44             service = aService;
45         }
46
47         @Override
48         public Object invoke(Object aProxy, final Method aMethod,
49             final Object[] aArgs) throws Throwable {
50             try {
51                 if (aMethod.getDeclaringClass().equals(Identifiable.class)) {
52                     if (!aMethod.getName().equals("getId")) {
53                         throw new RuntimeException(
54                             "Programming error, Identifiable interface was modified");
55                     }
56                     Id id = (Id) aMethod.invoke(service, aArgs);
57                     return new Id(prefix + id.getId());
58                 }
59                 return aMethod.invoke(service, aArgs);
60             } catch (InvocationTargetException e) {
61                 throw e.getCause();
62             }
63         }
64     }
65
66     public static <T> T getProxy(String aPrefix, T aService,
67         Class... aInterfaces) {
68         InvocationHandler handler = new IdPrefixInvocationHandler<T>(aPrefix,
69             aService);
70         Class proxyClass = Proxy.getProxyClass(aService.getClass()
71             .getClassLoader(), aInterfaces);
72         T proxy;
73         try {
74             proxy = (T) proxyClass.getConstructor(
75                 new Class[] { InvocationHandler.class }).newInstance(
76                 new Object[] { handler });
77             return proxy;
78         } catch (Exception e) {
79             throw new RuntimeException("Could not create proxy for " +
80                 aService.getClass().getName(), e);
81         }
82     }
83 }