001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.kernel.config;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.geronimo.gbean.AbstractName;
025: import org.apache.geronimo.gbean.AbstractNameQuery;
026: import org.apache.geronimo.gbean.GBeanData;
027: import org.apache.geronimo.gbean.GBeanInfo;
028: import org.apache.geronimo.gbean.GBeanInfoBuilder;
029: import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
030: import org.apache.geronimo.kernel.GBeanNotFoundException;
031: import org.apache.geronimo.kernel.Kernel;
032: import org.apache.geronimo.kernel.management.State;
033: import org.apache.geronimo.kernel.repository.Artifact;
034: import org.apache.geronimo.kernel.repository.ArtifactManager;
035: import org.apache.geronimo.kernel.repository.ArtifactResolver;
036:
037: /**
038: * Standard implementation of an editable ConfigurationManager.
039: *
040: * @version $Rev:386276 $ $Date: 2007-11-21 00:39:04 -0800 (Wed, 21 Nov 2007) $
041: */
042: public class EditableKernelConfigurationManager extends
043: KernelConfigurationManager implements
044: EditableConfigurationManager {
045: private final AbstractNameQuery defaultStoreNameQuery;
046:
047: public EditableKernelConfigurationManager(Kernel kernel,
048: AbstractNameQuery defaultStoreNameQuery, Collection stores,
049: ManageableAttributeStore attributeStore,
050: PersistentConfigurationList configurationList,
051: ArtifactManager artifactManager,
052: ArtifactResolver artifactResolver, Collection repositories,
053: Collection watchers, ClassLoader classLoader) {
054: super (kernel, stores, attributeStore, configurationList,
055: artifactManager, artifactResolver, repositories,
056: watchers, classLoader);
057: this .defaultStoreNameQuery = defaultStoreNameQuery;
058: }
059:
060: @Override
061: protected List getStoreList() {
062: if (null == defaultStoreNameQuery) {
063: return super .getStoreList();
064: }
065:
066: List<ConfigurationStore> storeList = new ArrayList<ConfigurationStore>();
067: for (Iterator iterator = stores.iterator(); iterator.hasNext();) {
068: ConfigurationStore configurationStore = (ConfigurationStore) iterator
069: .next();
070: if (defaultStoreNameQuery.matches(configurationStore
071: .getAbstractName(), Collections.EMPTY_SET)) {
072: storeList.add(0, configurationStore);
073: } else {
074: storeList.add(configurationStore);
075: }
076: }
077: return storeList;
078: }
079:
080: public void addGBeanToConfiguration(Artifact configurationId,
081: GBeanData gbean, boolean start)
082: throws InvalidConfigException {
083: Configuration configuration = getConfiguration(configurationId);
084:
085: try {
086: // add the gbean to the configuration
087: configuration.addGBean(gbean);
088: } catch (GBeanAlreadyExistsException e) {
089: throw new InvalidConfigException("Cound not add GBean "
090: + gbean.getAbstractName() + " to configuration "
091: + configurationId, e);
092: }
093:
094: addGBeanToConfiguration(configuration, gbean, start);
095: }
096:
097: public void addGBeanToConfiguration(Artifact configurationId,
098: String name, GBeanData gbean, boolean start)
099: throws InvalidConfigException {
100: Configuration configuration = getConfiguration(configurationId);
101:
102: try {
103: // add the gbean to the configuration
104: configuration.addGBean(name, gbean);
105: } catch (GBeanAlreadyExistsException e) {
106: throw new InvalidConfigException("Cound not add GBean "
107: + gbean.getAbstractName() + " to configuration "
108: + configurationId, e);
109: }
110:
111: addGBeanToConfiguration(configuration, gbean, start);
112: }
113:
114: private void addGBeanToConfiguration(Configuration configuration,
115: GBeanData gbean, boolean start)
116: throws InvalidConfigException {
117: ClassLoader configurationClassLoader = configuration
118: .getConfigurationClassLoader();
119: ClassLoader oldCl = Thread.currentThread()
120: .getContextClassLoader();
121: try {
122: Thread.currentThread().setContextClassLoader(
123: configurationClassLoader);
124:
125: log.trace("Registering GBean " + gbean.getAbstractName());
126:
127: // preprocess the gbean data before loading it into the kernel
128: ConfigurationUtil.preprocessGBeanData(configuration
129: .getAbstractName(), configuration, gbean);
130:
131: // register the bean with the kernel
132: kernel.loadGBean(gbean, configurationClassLoader);
133:
134: // start the configuration
135: if (start) {
136: try {
137: kernel.startRecursiveGBean(gbean.getAbstractName());
138: } catch (GBeanNotFoundException e) {
139: throw new InvalidConfigException(
140: "How could we not find a GBean that we just loaded ('"
141: + gbean.getAbstractName() + "')?",
142: e);
143: }
144: }
145:
146: } catch (Exception e) {
147: // clean up failed gbean
148: try {
149: configuration.removeGBean(gbean.getAbstractName());
150: } catch (GBeanNotFoundException e1) {
151: // this is good
152: }
153: try {
154: kernel.stopGBean(gbean.getAbstractName());
155: } catch (GBeanNotFoundException e1) {
156: // this is good
157: }
158: try {
159: kernel.unloadGBean(gbean.getAbstractName());
160: } catch (GBeanNotFoundException e1) {
161: // this is good
162: }
163:
164: if (e instanceof InvalidConfigException) {
165: throw (InvalidConfigException) e;
166: }
167: throw new InvalidConfigException("Cound not add GBean "
168: + gbean.getAbstractName() + " to configuration "
169: + configuration.getId(), e);
170: } finally {
171: Thread.currentThread().setContextClassLoader(oldCl);
172: }
173:
174: if (attributeStore != null) {
175: attributeStore.addGBean(configuration.getId(), gbean,
176: configurationClassLoader);
177: }
178: }
179:
180: public void removeGBeanFromConfiguration(Artifact configurationId,
181: AbstractName gbeanName) throws GBeanNotFoundException,
182: InvalidConfigException {
183: Configuration configuration = getConfiguration(configurationId);
184: if (!configuration.containsGBean(gbeanName)) {
185: throw new GBeanNotFoundException(gbeanName);
186: }
187: configuration.removeGBean(gbeanName);
188:
189: try {
190: if (kernel.getGBeanState(gbeanName) == State.RUNNING_INDEX) {
191: kernel.stopGBean(gbeanName);
192: }
193: kernel.unloadGBean(gbeanName);
194: } catch (GBeanNotFoundException e) {
195: // Bean is no longer loaded
196: }
197:
198: // Make sure it's not loaded next time the configuration is loaded
199: if (attributeStore != null) {
200: attributeStore.setShouldLoad(configurationId, gbeanName,
201: false);
202: }
203: }
204:
205: public static final GBeanInfo GBEAN_INFO;
206:
207: public static final String GBEAN_ATTR_DEFAULT_STORE_NAME_QUERY = "defaultStoreNameQuery";
208:
209: static {
210: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
211: EditableKernelConfigurationManager.class,
212: KernelConfigurationManager.GBEAN_INFO,
213: "ConfigurationManager");
214:
215: infoFactory.addAttribute(GBEAN_ATTR_DEFAULT_STORE_NAME_QUERY,
216: AbstractNameQuery.class, true);
217:
218: infoFactory.addInterface(EditableConfigurationManager.class);
219:
220: infoFactory.setConstructor(new String[] { "kernel",
221: GBEAN_ATTR_DEFAULT_STORE_NAME_QUERY, "Stores",
222: "AttributeStore", "PersistentConfigurationList",
223: "ArtifactManager", "ArtifactResolver", "Repositories",
224: "Watchers", "classLoader" });
225:
226: GBEAN_INFO = infoFactory.getBeanInfo();
227: }
228:
229: public static GBeanInfo getGBeanInfo() {
230: return GBEAN_INFO;
231: }
232: }
|