001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean.runtime;
017:
018: import org.apache.geronimo.gbean.AbstractName;
019: import org.apache.geronimo.gbean.GReferenceInfo;
020: import org.apache.geronimo.gbean.InvalidConfigurationException;
021: import org.apache.geronimo.kernel.ClassLoading;
022: import org.apache.geronimo.kernel.GBeanNotFoundException;
023: import org.apache.geronimo.kernel.Kernel;
024: import org.apache.geronimo.kernel.management.State;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import java.lang.reflect.Method;
029: import java.lang.reflect.Modifier;
030:
031: /**
032: * @version $Rev: 614255 $ $Date: 2008-01-22 08:53:48 -0800 (Tue, 22 Jan 2008) $
033: */
034: public abstract class AbstractGBeanReference implements GBeanReference {
035: /**
036: * Should we proxy references.
037: */
038: protected static final boolean NO_PROXY;
039: static {
040: Log log = LogFactory.getLog(AbstractGBeanReference.class);
041: String no_proxy = System.getProperty(
042: "Xorg.apache.geronimo.gbean.NoProxy", "true");
043: NO_PROXY = no_proxy.equalsIgnoreCase("true");
044: if (NO_PROXY) {
045: log.info("GBean references are not using proxies");
046: } else {
047: log.info("GBean references are using proxies");
048: }
049: }
050:
051: /**
052: * Name of this reference.
053: */
054: private final String name;
055:
056: /**
057: * Interface this GBeanInstance uses to refer to the other.
058: */
059: private final Class referenceType;
060:
061: /**
062: * Proxy type which is injected into the GBeanInstance.
063: */
064: private final Class proxyType;
065:
066: /**
067: * The GBeanInstance to which this reference belongs.
068: */
069: private final GBeanInstance gbeanInstance;
070:
071: /**
072: * The method that will be called to set the attribute value. If null, the value will be set with
073: * a constructor argument
074: */
075: private final MethodInvoker setInvoker;
076:
077: private final boolean hasTargets;
078:
079: /**
080: * The metadata for this reference
081: */
082: private final GReferenceInfo referenceInfo;
083:
084: /**
085: * The kernel to which the reference is bound.
086: */
087: private final Kernel kernel;
088:
089: /**
090: * Proxy for this reference
091: */
092: private Object proxy;
093:
094: public AbstractGBeanReference(GBeanInstance gbeanInstance,
095: GReferenceInfo referenceInfo, Kernel kernel,
096: boolean hasTargets) throws InvalidConfigurationException {
097: this .gbeanInstance = gbeanInstance;
098: this .referenceInfo = referenceInfo;
099: this .kernel = kernel;
100: this .hasTargets = hasTargets;
101:
102: this .name = referenceInfo.getName();
103: try {
104: this .referenceType = ClassLoading.loadClass(referenceInfo
105: .getReferenceType(), gbeanInstance.getType()
106: .getClassLoader());
107: } catch (ClassNotFoundException e) {
108: throw new InvalidConfigurationException(
109: "Could not load Reference Type: "
110: + getDescription(), e);
111: }
112: if (Modifier.isFinal(referenceType.getModifiers())) {
113: throw new IllegalArgumentException(
114: "Proxy interface cannot be a final class: "
115: + referenceType.getName());
116: }
117: try {
118: this .proxyType = ClassLoading.loadClass(referenceInfo
119: .getProxyType(), gbeanInstance.getType()
120: .getClassLoader());
121: } catch (ClassNotFoundException e) {
122: throw new InvalidConfigurationException(
123: "Could not load Proxy Type:" + getDescription(), e);
124: }
125:
126: if (referenceInfo.getSetterName() != null) {
127: try {
128: String setterName = referenceInfo.getSetterName();
129: Method setterMethod = gbeanInstance.getType()
130: .getMethod(setterName,
131: new Class[] { proxyType });
132: if (NO_PROXY) {
133: setInvoker = new ReflectionMethodInvoker(
134: setterMethod);
135: } else {
136: setInvoker = new FastMethodInvoker(setterMethod);
137: }
138: } catch (NoSuchMethodException e) {
139: throw new InvalidConfigurationException(
140: "Setter method not found " + getDescription(),
141: e);
142: }
143: } else {
144: setInvoker = null;
145: }
146:
147: }
148:
149: protected final Kernel getKernel() {
150: return kernel;
151: }
152:
153: public final GBeanInstance getGBeanInstance() {
154: return gbeanInstance;
155: }
156:
157: public final String getName() {
158: return name;
159: }
160:
161: public final GReferenceInfo getReferenceInfo() {
162: return referenceInfo;
163: }
164:
165: public final Class getReferenceType() {
166: return referenceType;
167: }
168:
169: public final Class getProxyType() {
170: return proxyType;
171: }
172:
173: public final Object getProxy() {
174: return proxy;
175: }
176:
177: protected final void setProxy(Object proxy) {
178: this .proxy = proxy;
179: }
180:
181: /**
182: * Is the component in the Running state
183: *
184: * @param abstractName name of the component to check
185: * @return true if the component is running; false otherwise
186: */
187: protected boolean isRunning(Kernel kernel, AbstractName abstractName) {
188: try {
189: final int state = kernel.getGBeanState(abstractName);
190: return state == State.RUNNING_INDEX;
191: } catch (GBeanNotFoundException e) {
192: // mbean is no longer registerd
193: return false;
194: } catch (Exception e) {
195: // problem getting the attribute, mbean has most likely failed
196: return false;
197: }
198: }
199:
200: protected final String getDescription() {
201: return "\n GBeanInstance: " + gbeanInstance.getName()
202: + "\n Reference Name: " + getName()
203: + "\n Reference Type: "
204: + referenceInfo.getReferenceType()
205: + "\n Proxy Type: " + referenceInfo.getProxyType();
206: }
207:
208: public final synchronized void inject(Object target)
209: throws Exception {
210: // set the proxy into the instance
211: if (setInvoker != null && hasTargets) {
212: setInvoker.invoke(target, new Object[] { getProxy() });
213: }
214: }
215: }
|