KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > ConnectorMethodInterceptor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector;
19
20 import java.io.Serializable JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22
23 import net.sf.cglib.proxy.MethodInterceptor;
24 import net.sf.cglib.proxy.MethodProxy;
25 import org.apache.geronimo.kernel.KernelRegistry;
26 import org.apache.geronimo.kernel.Kernel;
27 import org.apache.geronimo.kernel.proxy.DeadProxyException;
28 import org.apache.geronimo.gbean.AbstractName;
29
30 /**
31  * MethodInterceptor used by various Proxies. The important part of this class is the
32  * deserialization in the readResolve method.
33  *
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  *
36  * */

37 public class ConnectorMethodInterceptor implements MethodInterceptor, Serializable JavaDoc {
38     private static final long serialVersionUID = -3062915319296676033L;
39     private final String JavaDoc kernelName;
40     private final AbstractName targetName;
41
42     private transient Object JavaDoc internalProxy;
43
44     public ConnectorMethodInterceptor(String JavaDoc kernelName, AbstractName targetName) {
45         this.kernelName = kernelName;
46         this.targetName = targetName;
47     }
48
49     public Object JavaDoc intercept(final Object JavaDoc o, final Method JavaDoc method, Object JavaDoc[] objects, final MethodProxy methodProxy) throws Throwable JavaDoc {
50         if (internalProxy == null) {
51             connectInternalProxy();
52         }
53         try {
54             return methodProxy.invoke(internalProxy, objects);
55         } catch (DeadProxyException e) {
56             connectInternalProxy();
57             return methodProxy.invoke(internalProxy, objects);
58         }
59     }
60
61     public void setInternalProxy(final Object JavaDoc internalProxy) {
62         this.internalProxy = internalProxy;
63     }
64
65     private void connectInternalProxy() throws Throwable JavaDoc {
66         Kernel kernel = KernelRegistry.getKernel(kernelName);
67         try {
68             internalProxy = kernel.invoke(targetName, "$getConnectionFactory");
69         } catch (Exception JavaDoc e) {
70             throw new IllegalStateException JavaDoc("Could not connect proxy to ManagedConnectionFactoryWrapper").initCause(e);
71         }
72     }
73 }
74
Popular Tags