001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 */
017
018 package org.apache.geronimo.gbean.runtime;
019
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022 import org.apache.geronimo.gbean.AbstractName;
023 import org.apache.geronimo.gbean.GReferenceInfo;
024 import org.apache.geronimo.gbean.InvalidConfigurationException;
025 import org.apache.geronimo.gbean.ReferencePatterns;
026 import org.apache.geronimo.kernel.Kernel;
027 import org.apache.geronimo.kernel.GBeanNotFoundException;
028
029 /**
030 * @version $Rev: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
031 */
032 public class GBeanSingleReference extends AbstractGBeanReference {
033 private static final Log log = LogFactory.getLog(GBeanSingleReference.class);
034
035 /**
036 * The object to which the proxy is bound
037 */
038 private final AbstractName targetName;
039
040 public GBeanSingleReference(GBeanInstance gbeanInstance, GReferenceInfo referenceInfo, Kernel kernel, ReferencePatterns referencePatterns) throws InvalidConfigurationException {
041 super(gbeanInstance, referenceInfo, kernel, referencePatterns != null && referencePatterns.getAbstractName() != null);
042 targetName = referencePatterns != null? referencePatterns.getAbstractName(): null;
043 }
044
045 public AbstractName getTargetName() {
046 return targetName;
047 }
048
049 public final synchronized void online() {
050 }
051
052 public final synchronized void offline() {
053 stop();
054 }
055
056
057 public synchronized boolean start() {
058 // We only need to start if there are patterns and we don't already have a proxy
059 if (targetName == null) {
060 return true;
061 }
062
063 // assure the gbean is running
064 AbstractName abstractName = getGBeanInstance().getAbstractName();
065 if (!isRunning(getKernel(), targetName)) {
066 log.debug("Waiting to start " + abstractName + " because no targets are running for reference " + getName() +" matching the patterns " + targetName);
067 return false;
068 }
069
070 if (getProxy() != null) {
071 return true;
072 }
073
074 if (NO_PROXY) {
075 try {
076 setProxy(getKernel().getGBean(targetName));
077 } catch (GBeanNotFoundException e) {
078 // gbean disappeard on us
079 log.debug("Waiting to start " + abstractName + " because no targets are running for reference " + getName() +" matching the patterns " + targetName);
080 }
081 } else {
082 setProxy(getKernel().getProxyManager().createProxy(targetName, getReferenceType()));
083 }
084
085 return true;
086 }
087
088 public synchronized void stop() {
089 Object proxy = getProxy();
090 if (proxy != null) {
091 getKernel().getProxyManager().destroyProxy(proxy);
092 setProxy(null);
093 }
094 }
095
096
097 }