KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > reference > ResourceReference


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.naming.reference;
19
20 import org.apache.geronimo.gbean.AbstractName;
21 import org.apache.geronimo.gbean.AbstractNameQuery;
22 import org.apache.geronimo.kernel.GBeanNotFoundException;
23 import org.apache.geronimo.kernel.Kernel;
24 import org.apache.geronimo.kernel.repository.Artifact;
25
26 import javax.naming.NameNotFoundException JavaDoc;
27
28 /**
29  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
30  */

31 public class ResourceReference extends ConfigurationAwareReference {
32     private final Class JavaDoc iface;
33
34     /**
35      *
36      * @param configId the configId of the configuration that holds the reference, not the resource adapter.
37      * @param abstractNameQuery query for name of the resource adapter.
38      * @param iface
39      */

40     public ResourceReference(Artifact configId, AbstractNameQuery abstractNameQuery, Class JavaDoc iface) {
41         super(configId, abstractNameQuery);
42         this.iface = iface;
43     }
44
45     public String JavaDoc getClassName() {
46         return iface.getName();
47     }
48
49     public Object JavaDoc getContent() throws NameNotFoundException JavaDoc {
50         Kernel kernel = getKernel();
51
52         AbstractName target;
53         try {
54             target = resolveTargetName();
55         } catch (GBeanNotFoundException e) {
56             throw (NameNotFoundException JavaDoc) new NameNotFoundException JavaDoc("Could not resolve name query: " + abstractNameQueries).initCause(e);
57         }
58
59         Object JavaDoc proxy;
60         try {
61             proxy = kernel.invoke(target, "$getResource");
62         } catch (Exception JavaDoc e) {
63             throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("Could not get proxy").initCause(e);
64         }
65         if (proxy == null) {
66             throw new IllegalStateException JavaDoc("Proxy not returned. Target " + target + " not started");
67         }
68         if (!iface.isAssignableFrom(proxy.getClass())) {
69             Class JavaDoc proxyClass = proxy.getClass();
70             Class JavaDoc[] interfaces = proxyClass.getInterfaces();
71             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
72             boolean namesMatch = false;
73             for (int i = 0; i < interfaces.length; i++) {
74                 Class JavaDoc anInterface = interfaces[i];
75                 if (iface.getName().equals(anInterface.getName())) {
76                     namesMatch = true;
77                     message.append("Proxy implements correct interface: ").append(iface.getName()).append(", but classloaders differ\n");
78                     message.append("lookup interface classloader: ").append(iface.getClassLoader().toString()).append("\n");
79                     message.append("target interface classloader: ").append(anInterface.getClassLoader().toString()).append("\n");
80                     message.append("target proxy classloader: ").append(proxy.getClass().getClassLoader());
81                     break;
82                 }
83             }
84             if (!namesMatch) {
85                 message.append("Proxy does not implement an interface named: ").append(iface.getName());
86             }
87             throw new ClassCastException JavaDoc(message.toString());
88         }
89         return proxy;
90
91     }
92 }
93
Popular Tags