1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.geronimo.kernel.config;
19
20 import java.util.Collection;
21
22 import org.apache.geronimo.kernel.Kernel;
23 import org.apache.geronimo.kernel.GBeanNotFoundException;
24 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
25 import org.apache.geronimo.kernel.repository.Artifact;
26 import org.apache.geronimo.kernel.repository.ArtifactResolver;
27 import org.apache.geronimo.kernel.repository.ArtifactManager;
28 import org.apache.geronimo.kernel.management.State;
29 import org.apache.geronimo.gbean.GBeanData;
30 import org.apache.geronimo.gbean.GBeanInfo;
31 import org.apache.geronimo.gbean.GBeanInfoBuilder;
32 import org.apache.geronimo.gbean.AbstractName;
33
34 /**
35 * Standard implementation of an editable ConfigurationManager.
36 *
37 * @version $Rev:386276 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
38 */
39 public class EditableKernelConfigurationManager extends KernelConfigurationManager implements EditableConfigurationManager {
40 public EditableKernelConfigurationManager(Kernel kernel,
41 Collection stores,
42 ManageableAttributeStore attributeStore,
43 PersistentConfigurationList configurationList,
44 ArtifactManager artifactManager,
45 ArtifactResolver artifactResolver,
46 Collection repositories,
47 Collection watchers,
48 ClassLoader classLoader) {
49 super(kernel, stores, attributeStore, configurationList, artifactManager, artifactResolver, repositories, watchers, classLoader);
50 }
51
52 public void addGBeanToConfiguration(Artifact configurationId, GBeanData gbean, boolean start) throws InvalidConfigException {
53 Configuration configuration = getConfiguration(configurationId);
54
55 try {
56
57 configuration.addGBean(gbean);
58 } catch (GBeanAlreadyExistsException e) {
59 throw new InvalidConfigException("Cound not add GBean " + gbean.getAbstractName() + " to configuration " + configurationId, e);
60 }
61
62 addGBeanToConfiguration(configuration, gbean, start);
63 }
64
65 public void addGBeanToConfiguration(Artifact configurationId, String name, GBeanData gbean, boolean start) throws InvalidConfigException {
66 Configuration configuration = getConfiguration(configurationId);
67
68 try {
69
70 configuration.addGBean(name, gbean);
71 } catch (GBeanAlreadyExistsException e) {
72 throw new InvalidConfigException("Cound not add GBean " + gbean.getAbstractName() + " to configuration " + configurationId, e);
73 }
74
75 addGBeanToConfiguration(configuration, gbean, start);
76 }
77
78 private void addGBeanToConfiguration(Configuration configuration, GBeanData gbean, boolean start) throws InvalidConfigException {
79 ClassLoader configurationClassLoader = configuration.getConfigurationClassLoader();
80 ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
81 try {
82 Thread.currentThread().setContextClassLoader(configurationClassLoader);
83
84 log.trace("Registering GBean " + gbean.getAbstractName());
85
86
87
88 ConfigurationUtil.preprocessGBeanData(configuration.getAbstractName(), configuration, gbean);
89
90
91 kernel.loadGBean(gbean, configurationClassLoader);
92
93
94 if (start) {
95 try {
96 kernel.startRecursiveGBean(gbean.getAbstractName());
97 } catch (GBeanNotFoundException e) {
98 throw new InvalidConfigException("How could we not find a GBean that we just loaded ('" + gbean.getAbstractName() + "')?");
99 }
100 }
101
102 } catch(Exception e) {
103
104 try {
105 configuration.removeGBean(gbean.getAbstractName());
106 } catch (GBeanNotFoundException e1) {
107
108 }
109 try {
110 kernel.stopGBean(gbean.getAbstractName());
111 } catch (GBeanNotFoundException e1) {
112
113 }
114 try {
115 kernel.unloadGBean(gbean.getAbstractName());
116 } catch (GBeanNotFoundException e1) {
117
118 }
119
120 if (e instanceof InvalidConfigException) {
121 throw (InvalidConfigException) e;
122 }
123 throw new InvalidConfigException("Cound not add GBean " + gbean.getAbstractName() + " to configuration " + configuration.getId(), e);
124 } finally {
125 Thread.currentThread().setContextClassLoader(oldCl);
126 }
127
128 if (attributeStore != null) {
129 attributeStore.addGBean(configuration.getId(), gbean);
130 }
131 }
132
133 public void removeGBeanFromConfiguration(Artifact configurationId, AbstractName gbeanName) throws GBeanNotFoundException, InvalidConfigException {
134 Configuration configuration = getConfiguration(configurationId);
135 if (!configuration.containsGBean(gbeanName)) {
136 throw new GBeanNotFoundException(gbeanName);
137 }
138 configuration.removeGBean(gbeanName);
139
140 try {
141 if (kernel.getGBeanState(gbeanName) == State.RUNNING_INDEX) {
142 kernel.stopGBean(gbeanName);
143 }
144 kernel.unloadGBean(gbeanName);
145 } catch (GBeanNotFoundException e) {
146
147 }
148
149
150 if (attributeStore != null) {
151 attributeStore.setShouldLoad(configurationId, gbeanName, false);
152 }
153 }
154
155 public static final GBeanInfo GBEAN_INFO;
156
157 static {
158 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(EditableKernelConfigurationManager.class, KernelConfigurationManager.GBEAN_INFO, "ConfigurationManager");
159 infoFactory.addInterface(EditableConfigurationManager.class);
160 infoFactory.setConstructor(new String[]{"kernel", "Stores", "AttributeStore", "PersistentConfigurationList", "ArtifactManager", "ArtifactResolver", "Repositories", "Watchers", "classLoader"});
161 GBEAN_INFO = infoFactory.getBeanInfo();
162 }
163
164 public static GBeanInfo getGBeanInfo() {
165 return GBEAN_INFO;
166 }
167 }