KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > runtime > GBeanSingleReference


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 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                 return false;
81             }
82         } else {
83             setProxy(getKernel().getProxyManager().createProxy(targetName, getReferenceType()));
84         }
85         log.debug("Started " + abstractName);
86         return true;
87     }
88
89     public synchronized void stop() {
90         Object JavaDoc proxy = getProxy();
91         if (proxy != null) {
92             getKernel().getProxyManager().destroyProxy(proxy);
93             setProxy(null);
94         }
95     }
96
97
98 }
99
Popular Tags