1 /**
2 *
3 * Copyright 2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
27
28 /**
29 * @version $Rev: 406864 $ $Date: 2006-05-16 00:36:42 -0700 (Tue, 16 May 2006) $
30 */
31 public class ResourceReference extends ConfigurationAwareReference {
32 private final Class 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 iface) {
41 super(configId, abstractNameQuery);
42 this.iface = iface;
43 }
44
45 public String getClassName() {
46 return iface.getName();
47 }
48
49 public Object getContent() throws NameNotFoundException {
50 Kernel kernel = getKernel();
51
52 AbstractName target;
53 try {
54 target = resolveTargetName();
55 } catch (GBeanNotFoundException e) {
56 throw (NameNotFoundException) new NameNotFoundException("Could not resolve name query: " + abstractNameQueries).initCause(e);
57 }
58
59 Object proxy;
60 try {
61 proxy = kernel.invoke(target, "$getResource");
62 } catch (Exception e) {
63 throw (IllegalStateException) new IllegalStateException("Could not get proxy").initCause(e);
64 }
65 if (proxy == null) {
66 throw new IllegalStateException("Proxy not returned. Target " + target + " not started");
67 }
68 if (!iface.isAssignableFrom(proxy.getClass())) {
69 Class proxyClass = proxy.getClass();
70 Class[] interfaces = proxyClass.getInterfaces();
71 StringBuffer message = new StringBuffer();
72 boolean namesMatch = false;
73 for (int i = 0; i < interfaces.length; i++) {
74 Class 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(message.toString());
88 }
89 return proxy;
90
91 }
92 }