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.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.gbean.AbstractName;
021: import org.apache.geronimo.gbean.GReferenceInfo;
022: import org.apache.geronimo.gbean.InvalidConfigurationException;
023: import org.apache.geronimo.gbean.ReferencePatterns;
024: import org.apache.geronimo.kernel.Kernel;
025: import org.apache.geronimo.kernel.GBeanNotFoundException;
026:
027: /**
028: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
029: */
030: public class GBeanSingleReference extends AbstractGBeanReference {
031: private static final Log log = LogFactory
032: .getLog(GBeanSingleReference.class);
033:
034: /**
035: * The object to which the proxy is bound
036: */
037: private final AbstractName targetName;
038:
039: public GBeanSingleReference(GBeanInstance gbeanInstance,
040: GReferenceInfo referenceInfo, Kernel kernel,
041: ReferencePatterns referencePatterns)
042: throws InvalidConfigurationException {
043: super (gbeanInstance, referenceInfo, kernel,
044: referencePatterns != null
045: && referencePatterns.getAbstractName() != null);
046: targetName = referencePatterns != null ? referencePatterns
047: .getAbstractName() : null;
048: }
049:
050: public AbstractName getTargetName() {
051: return targetName;
052: }
053:
054: public final synchronized void online() {
055: }
056:
057: public final synchronized void offline() {
058: stop();
059: }
060:
061: public synchronized boolean start() {
062: // We only need to start if there are patterns and we don't already have a proxy
063: if (targetName == null) {
064: return true;
065: }
066:
067: // assure the gbean is running
068: AbstractName abstractName = getGBeanInstance()
069: .getAbstractName();
070: if (!isRunning(getKernel(), targetName)) {
071: log.debug("Waiting to start " + abstractName
072: + " because no targets are running for reference "
073: + getName() + " matching the patterns "
074: + targetName);
075: return false;
076: }
077:
078: if (getProxy() != null) {
079: return true;
080: }
081:
082: if (NO_PROXY) {
083: try {
084: setProxy(getKernel().getGBean(targetName));
085: } catch (GBeanNotFoundException e) {
086: // gbean disappeard on us
087: log
088: .debug("Waiting to start "
089: + abstractName
090: + " because no targets are running for reference "
091: + getName() + " matching the patterns "
092: + targetName);
093: return false;
094: }
095: } else {
096: setProxy(getKernel().getProxyManager().createProxy(
097: targetName, getReferenceType()));
098: }
099: log.debug("Started " + abstractName);
100: return true;
101: }
102:
103: public synchronized void stop() {
104: Object proxy = getProxy();
105: if (proxy != null) {
106: getKernel().getProxyManager().destroyProxy(proxy);
107: setProxy(null);
108: }
109: }
110:
111: }
|