View Javadoc

1   /**
2    *
3    * Copyright 2003-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.gbean.runtime;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.geronimo.gbean.AbstractName;
23  import org.apache.geronimo.gbean.GReferenceInfo;
24  import org.apache.geronimo.gbean.InvalidConfigurationException;
25  import org.apache.geronimo.gbean.ReferencePatterns;
26  import org.apache.geronimo.kernel.Kernel;
27  import org.apache.geronimo.kernel.GBeanNotFoundException;
28  
29  /**
30   * @version $Rev: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
31   */
32  public class GBeanSingleReference extends AbstractGBeanReference {
33      private static final Log log = LogFactory.getLog(GBeanSingleReference.class);
34  
35      /**
36       * The object to which the proxy is bound
37       */
38      private final AbstractName targetName;
39  
40      public GBeanSingleReference(GBeanInstance gbeanInstance, GReferenceInfo referenceInfo, Kernel kernel, ReferencePatterns referencePatterns) throws InvalidConfigurationException {
41          super(gbeanInstance, referenceInfo, kernel, referencePatterns != null && referencePatterns.getAbstractName() != null);
42          targetName = referencePatterns != null? referencePatterns.getAbstractName(): null;
43      }
44  
45      public AbstractName getTargetName() {
46          return targetName;
47      }
48  
49      public final synchronized void online() {
50      }
51  
52      public final synchronized void offline() {
53          stop();
54      }
55  
56  
57      public synchronized boolean start() {
58          // We only need to start if there are patterns and we don't already have a proxy
59          if (targetName == null) {
60              return true;
61          }
62  
63          // assure the gbean is running
64          AbstractName abstractName = getGBeanInstance().getAbstractName();
65          if (!isRunning(getKernel(), targetName)) {
66              log.debug("Waiting to start " + abstractName + " because no targets are running for reference " + getName() +" matching the patterns " + targetName);
67              return false;
68          }
69  
70          if (getProxy() != null) {
71              return true;
72          }
73  
74          if (NO_PROXY) {
75              try {
76                  setProxy(getKernel().getGBean(targetName));
77              } catch (GBeanNotFoundException e) {
78                  // gbean disappeard on us
79                  log.debug("Waiting to start " + abstractName + " because no targets are running for reference " + getName() +" matching the patterns " + targetName);
80              }
81          } else {
82              setProxy(getKernel().getProxyManager().createProxy(targetName, getReferenceType()));
83          }
84  
85          return true;
86      }
87  
88      public synchronized void stop() {
89          Object proxy = getProxy();
90          if (proxy != null) {
91              getKernel().getProxyManager().destroyProxy(proxy);
92              setProxy(null);
93          }
94      }
95  
96  
97  }