KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > ComponentRegistry


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 package org.apache.servicemix.jbi.framework;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedHashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.component.Component;
28
29 /**
30  * Registry for Components
31  *
32  * @version $Revision: 430746 $
33  */

34 public class ComponentRegistry {
35     
36     private Map JavaDoc idMap = new LinkedHashMap JavaDoc();
37     private boolean runningStateInitialized = false;
38     private Registry registry;
39     
40     
41     protected ComponentRegistry(Registry reg){
42         this.registry = reg;
43     }
44     /**
45      * Register a Component
46      * @param name
47      * @param description
48      * @param component
49      * @param dc
50      * @param binding
51      * @param service
52      * @return an associated ComponentConnector or null if it already exists
53      */

54     public synchronized ComponentMBeanImpl registerComponent(
55                     ComponentNameSpace name,
56                     String JavaDoc description,
57                     Component component,
58                     boolean binding,
59                     boolean service,
60                     String JavaDoc[] sharedLibraries) {
61         ComponentMBeanImpl result = null;
62         if (!idMap.containsKey(name)) {
63             result = new ComponentMBeanImpl(registry.getContainer(), name, description, component, binding, service, sharedLibraries);
64             idMap.put(name, result);
65         }
66         return result;
67     }
68     
69     /**
70      * start components
71      * @throws JBIException
72      */

73     public synchronized void start() throws JBIException{
74         if (!setInitialRunningStateFromStart()) {
75             for(Iterator JavaDoc i = getComponents().iterator(); i.hasNext();) {
76                 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next();
77                 lcc.doStart();
78             }
79         }
80     }
81
82     /**
83      * stop components
84      * @throws JBIException
85      *
86      * @throws JBIException
87      */

88     public synchronized void stop() throws JBIException {
89         for (Iterator JavaDoc i = getReverseComponents(). iterator();i.hasNext();) {
90             ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next();
91             lcc.doStop();
92         }
93         runningStateInitialized = false;
94     }
95     
96     /**
97      * shutdown all Components
98      *
99      * @throws JBIException
100      */

101     public synchronized void shutDown() throws JBIException {
102         for (Iterator JavaDoc i = getReverseComponents().iterator();i.hasNext();) {
103             ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next();
104             lcc.persistRunningState();
105             lcc.doShutDown();
106         }
107     }
108     
109     private Collection JavaDoc getReverseComponents() {
110         synchronized (idMap) {
111             ArrayList JavaDoc l = new ArrayList JavaDoc(idMap.values());
112             Collections.reverse(l);
113             return l;
114         }
115     }
116
117     
118     /**
119      * Deregister a Component
120      * @param component
121      * @return the deregistered component
122      */

123     public synchronized void deregisterComponent(ComponentMBeanImpl component) {
124         idMap.remove(component.getComponentNameSpace());
125     }
126     
127     /**
128      * Get a registered ComponentConnector from it's id
129      * @param id
130      * @return the ComponentConnector or null
131      */

132     public ComponentMBeanImpl getComponent(ComponentNameSpace id) {
133         synchronized (idMap) {
134             return (ComponentMBeanImpl) idMap.get(id);
135         }
136     }
137     
138     /**
139      *
140      * @return Collection of ComponentConnectors held by the registry
141      */

142     public Collection JavaDoc getComponents() {
143         synchronized (idMap) {
144             return new ArrayList JavaDoc(idMap.values());
145         }
146     }
147
148     private boolean setInitialRunningStateFromStart() throws JBIException{
149         boolean result = !runningStateInitialized;
150         if (!runningStateInitialized){
151             runningStateInitialized = true;
152             for (Iterator JavaDoc i = getComponents().iterator();i.hasNext();) {
153                 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next();
154                 if(!lcc.isPojo() && !registry.isContainerEmbedded()){
155                     lcc.setInitialRunningState();
156                 }else {
157                     lcc.doStart();
158                 }
159             }
160         }
161         return result;
162     }
163 }
Popular Tags