KickJava   Java API By Example, From Geeks To Geeks.

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


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.geronimo.kernel.config;
18
19 import java.util.Collection JavaDoc;
20
21 import org.apache.geronimo.kernel.Kernel;
22 import org.apache.geronimo.kernel.GBeanNotFoundException;
23 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
24 import org.apache.geronimo.kernel.repository.Artifact;
25 import org.apache.geronimo.kernel.repository.ArtifactResolver;
26 import org.apache.geronimo.kernel.repository.ArtifactManager;
27 import org.apache.geronimo.kernel.management.State;
28 import org.apache.geronimo.gbean.GBeanData;
29 import org.apache.geronimo.gbean.GBeanInfo;
30 import org.apache.geronimo.gbean.GBeanInfoBuilder;
31 import org.apache.geronimo.gbean.AbstractName;
32
33 /**
34  * Standard implementation of an editable ConfigurationManager.
35  *
36  * @version $Rev:386276 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class EditableKernelConfigurationManager extends KernelConfigurationManager implements EditableConfigurationManager {
39     public EditableKernelConfigurationManager(Kernel kernel,
40             Collection JavaDoc stores,
41             ManageableAttributeStore attributeStore,
42             PersistentConfigurationList configurationList,
43             ArtifactManager artifactManager,
44             ArtifactResolver artifactResolver,
45             Collection JavaDoc repositories,
46             Collection JavaDoc watchers,
47             ClassLoader JavaDoc classLoader) {
48         super(kernel, stores, attributeStore, configurationList, artifactManager, artifactResolver, repositories, watchers, classLoader);
49     }
50
51     public void addGBeanToConfiguration(Artifact configurationId, GBeanData gbean, boolean start) throws InvalidConfigException {
52         Configuration configuration = getConfiguration(configurationId);
53
54         try {
55             // add the gbean to the configuration
56
configuration.addGBean(gbean);
57         } catch (GBeanAlreadyExistsException e) {
58             throw new InvalidConfigException("Cound not add GBean " + gbean.getAbstractName() + " to configuration " + configurationId, e);
59         }
60
61         addGBeanToConfiguration(configuration, gbean, start);
62     }
63
64     public void addGBeanToConfiguration(Artifact configurationId, String JavaDoc name, GBeanData gbean, boolean start) throws InvalidConfigException {
65         Configuration configuration = getConfiguration(configurationId);
66
67         try {
68             // add the gbean to the configuration
69
configuration.addGBean(name, gbean);
70         } catch (GBeanAlreadyExistsException e) {
71             throw new InvalidConfigException("Cound not add GBean " + gbean.getAbstractName() + " to configuration " + configurationId, e);
72         }
73
74         addGBeanToConfiguration(configuration, gbean, start);
75     }
76
77     private void addGBeanToConfiguration(Configuration configuration, GBeanData gbean, boolean start) throws InvalidConfigException {
78         ClassLoader JavaDoc configurationClassLoader = configuration.getConfigurationClassLoader();
79         ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
80         try {
81             Thread.currentThread().setContextClassLoader(configurationClassLoader);
82
83             log.trace("Registering GBean " + gbean.getAbstractName());
84
85
86             // preprocess the gbean data before loading it into the kernel
87
ConfigurationUtil.preprocessGBeanData(configuration.getAbstractName(), configuration, gbean);
88
89             // register the bean with the kernel
90
kernel.loadGBean(gbean, configurationClassLoader);
91
92             // start the configuration
93
if (start) {
94                 try {
95                     kernel.startRecursiveGBean(gbean.getAbstractName());
96                 } catch (GBeanNotFoundException e) {
97                     throw new InvalidConfigException("How could we not find a GBean that we just loaded ('" + gbean.getAbstractName() + "')?");
98                 }
99             }
100
101         } catch(Exception JavaDoc e) {
102             // clean up failed gbean
103
try {
104                 configuration.removeGBean(gbean.getAbstractName());
105             } catch (GBeanNotFoundException e1) {
106                 // this is good
107
}
108             try {
109                 kernel.stopGBean(gbean.getAbstractName());
110             } catch (GBeanNotFoundException e1) {
111                 // this is good
112
}
113             try {
114                 kernel.unloadGBean(gbean.getAbstractName());
115             } catch (GBeanNotFoundException e1) {
116                 // this is good
117
}
118
119             if (e instanceof InvalidConfigException) {
120                 throw (InvalidConfigException) e;
121             }
122             throw new InvalidConfigException("Cound not add GBean " + gbean.getAbstractName() + " to configuration " + configuration.getId(), e);
123         } finally {
124             Thread.currentThread().setContextClassLoader(oldCl);
125         }
126
127         if (attributeStore != null) {
128             attributeStore.addGBean(configuration.getId(), gbean);
129         }
130     }
131
132     public void removeGBeanFromConfiguration(Artifact configurationId, AbstractName gbeanName) throws GBeanNotFoundException, InvalidConfigException {
133         Configuration configuration = getConfiguration(configurationId);
134         if (!configuration.containsGBean(gbeanName)) {
135             throw new GBeanNotFoundException(gbeanName);
136         }
137         configuration.removeGBean(gbeanName);
138
139         try {
140             if (kernel.getGBeanState(gbeanName) == State.RUNNING_INDEX) {
141                 kernel.stopGBean(gbeanName);
142             }
143             kernel.unloadGBean(gbeanName);
144         } catch (GBeanNotFoundException e) {
145             // Bean is no longer loaded
146
}
147
148         // Make sure it's not loaded next time the configuration is loaded
149
if (attributeStore != null) {
150             attributeStore.setShouldLoad(configurationId, gbeanName, false);
151         }
152     }
153
154     public static final GBeanInfo GBEAN_INFO;
155
156     static {
157         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(EditableKernelConfigurationManager.class, KernelConfigurationManager.GBEAN_INFO, "ConfigurationManager");
158         infoFactory.addInterface(EditableConfigurationManager.class);
159         infoFactory.setConstructor(new String JavaDoc[]{"kernel", "Stores", "AttributeStore", "PersistentConfigurationList", "ArtifactManager", "ArtifactResolver", "Repositories", "Watchers", "classLoader"});
160         GBEAN_INFO = infoFactory.getBeanInfo();
161     }
162
163     public static GBeanInfo getGBeanInfo() {
164         return GBEAN_INFO;
165     }
166 }
167
Popular Tags