KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > ConfigurationManagerImpl


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.kernel.config;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.geronimo.gbean.GBeanInfo;
34 import org.apache.geronimo.gbean.GBeanInfoBuilder;
35 import org.apache.geronimo.gbean.GBeanLifecycle;
36 import org.apache.geronimo.kernel.GBeanNotFoundException;
37 import org.apache.geronimo.kernel.InternalKernelException;
38 import org.apache.geronimo.kernel.Kernel;
39 import org.apache.geronimo.kernel.jmx.JMXUtil;
40
41 /**
42  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
43  */

44 public class ConfigurationManagerImpl implements ConfigurationManager, GBeanLifecycle {
45     private static final Log log = LogFactory.getLog(ConfigurationManagerImpl.class);
46     private final Kernel kernel;
47     private final Collection JavaDoc stores;
48     private final ShutdownHook shutdownHook;
49
50     public ConfigurationManagerImpl(Kernel kernel, Collection JavaDoc stores) {
51         this.kernel = kernel;
52         this.stores = stores;
53         shutdownHook = new ShutdownHook(kernel);
54     }
55
56     public List JavaDoc listStores() {
57         List JavaDoc storeSnapshot = getStores();
58         List JavaDoc result = new ArrayList JavaDoc(storeSnapshot.size());
59         for (int i = 0; i < storeSnapshot.size(); i++) {
60             ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i);
61             result.add(JMXUtil.getObjectName(store.getObjectName()));
62         }
63         return result;
64     }
65
66     public List JavaDoc listConfigurations(ObjectName JavaDoc storeName) throws NoSuchStoreException {
67         List JavaDoc storeSnapshot = getStores();
68         for (int i = 0; i < storeSnapshot.size(); i++) {
69             ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i);
70             if (storeName.equals(JMXUtil.getObjectName(store.getObjectName()))) {
71                 return store.listConfiguations();
72             }
73         }
74         throw new NoSuchStoreException("No such store: " + storeName);
75     }
76
77     public boolean isLoaded(URI JavaDoc configID) {
78         try {
79             ObjectName JavaDoc name = Configuration.getConfigurationObjectName(configID);
80             return kernel.isLoaded(name);
81         } catch (MalformedObjectNameException JavaDoc e) {
82             return false;
83         }
84     }
85
86     public ObjectName JavaDoc load(URI JavaDoc configID) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
87         List JavaDoc storeSnapshot = getStores();
88
89         for (int i = 0; i < storeSnapshot.size(); i++) {
90             ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i);
91             if (store.containsConfiguration(configID)) {
92                 return store.loadConfiguration(configID);
93             }
94         }
95         throw new NoSuchConfigException("No configuration with id: " + configID);
96     }
97
98     public List JavaDoc loadRecursive(URI JavaDoc configID) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
99         try {
100             LinkedList JavaDoc ancestors = new LinkedList JavaDoc();
101             while (configID != null && !isLoaded(configID)) {
102                 ObjectName JavaDoc name = load(configID);
103                 ancestors.addFirst(name);
104                 configID = (URI JavaDoc) kernel.getAttribute(name, "parentId");
105             }
106             return ancestors;
107         } catch (NoSuchConfigException e) {
108             throw e;
109         } catch (IOException JavaDoc e) {
110             throw e;
111         } catch (InvalidConfigException e) {
112             throw e;
113         } catch (Exception JavaDoc e) {
114             throw new InvalidConfigException(e);
115         }
116     }
117
118     public void unload(URI JavaDoc configID) throws NoSuchConfigException {
119         ObjectName JavaDoc configName;
120         try {
121             configName = Configuration.getConfigurationObjectName(configID);
122         } catch (MalformedObjectNameException JavaDoc e) {
123             throw new NoSuchConfigException("Cannot convert ID to ObjectName: ", e);
124         }
125         try {
126             kernel.unloadGBean(configName);
127         } catch (GBeanNotFoundException e) {
128             throw new NoSuchConfigException("No config registered: " + configName, e);
129         }
130         log.info("Unloaded Configuration " + configName);
131     }
132
133     private List JavaDoc getStores() {
134         return new ArrayList JavaDoc(stores);
135     }
136
137     public void doStart() {
138         kernel.registerShutdownHook(shutdownHook);
139     }
140
141     private static final ObjectName JavaDoc CONFIG_QUERY = JMXUtil.getObjectName("geronimo.config:*");
142
143     public void doStop() {
144         kernel.unregisterShutdownHook(shutdownHook);
145     }
146
147     public void doFail() {
148         log.error("Cofiguration manager failed");
149     }
150
151     public static final GBeanInfo GBEAN_INFO;
152
153     static {
154         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(ConfigurationManagerImpl.class, "ConfigurationManager");
155         infoFactory.addAttribute("kernel", Kernel.class, false);
156         infoFactory.addReference("Stores", ConfigurationStore.class, "ConfigurationStore");
157         infoFactory.addInterface(ConfigurationManager.class);
158         infoFactory.setConstructor(new String JavaDoc[]{"kernel", "Stores"});
159         GBEAN_INFO = infoFactory.getBeanInfo();
160     }
161
162     public static GBeanInfo getGBeanInfo() {
163         return GBEAN_INFO;
164     }
165
166     private static class ShutdownHook implements Runnable JavaDoc {
167         private final Kernel kernel;
168
169         public ShutdownHook(Kernel kernel) {
170             this.kernel = kernel;
171         }
172
173         public void run() {
174             while (true) {
175                 Set JavaDoc configs = kernel.listGBeans(CONFIG_QUERY);
176                 if (configs.isEmpty()) {
177                     return;
178                 }
179                 for (Iterator JavaDoc i = configs.iterator(); i.hasNext();) {
180                     ObjectName JavaDoc configName = (ObjectName JavaDoc) i.next();
181                     if (kernel.isLoaded(configName)) {
182                         try {
183                             kernel.stopGBean(configName);
184                         } catch (GBeanNotFoundException e) {
185                             // ignore
186
} catch (InternalKernelException e) {
187                             log.warn("Could not stop configuration: " + configName, e);
188                         }
189                         try {
190                             kernel.unloadGBean(configName);
191                         } catch (GBeanNotFoundException e) {
192                             // ignore
193
}
194                     }
195                 }
196             }
197         }
198     }
199 }
200
Popular Tags