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 */
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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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 return false;
081 }
082 } else {
083 setProxy(getKernel().getProxyManager().createProxy(targetName, getReferenceType()));
084 }
085 log.debug("Started " + abstractName);
086 return true;
087 }
088
089 public synchronized void stop() {
090 Object proxy = getProxy();
091 if (proxy != null) {
092 getKernel().getProxyManager().destroyProxy(proxy);
093 setProxy(null);
094 }
095 }
096
097
098 }