1 17 package org.apache.geronimo.kernel.config; 18 19 import java.util.Collection ; 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 38 public class EditableKernelConfigurationManager extends KernelConfigurationManager implements EditableConfigurationManager { 39 public EditableKernelConfigurationManager(Kernel kernel, 40 Collection stores, 41 ManageableAttributeStore attributeStore, 42 PersistentConfigurationList configurationList, 43 ArtifactManager artifactManager, 44 ArtifactResolver artifactResolver, 45 Collection repositories, 46 Collection watchers, 47 ClassLoader 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 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 name, GBeanData gbean, boolean start) throws InvalidConfigException { 65 Configuration configuration = getConfiguration(configurationId); 66 67 try { 68 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 configurationClassLoader = configuration.getConfigurationClassLoader(); 79 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 80 try { 81 Thread.currentThread().setContextClassLoader(configurationClassLoader); 82 83 log.trace("Registering GBean " + gbean.getAbstractName()); 84 85 86 ConfigurationUtil.preprocessGBeanData(configuration.getAbstractName(), configuration, gbean); 88 89 kernel.loadGBean(gbean, configurationClassLoader); 91 92 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 e) { 102 try { 104 configuration.removeGBean(gbean.getAbstractName()); 105 } catch (GBeanNotFoundException e1) { 106 } 108 try { 109 kernel.stopGBean(gbean.getAbstractName()); 110 } catch (GBeanNotFoundException e1) { 111 } 113 try { 114 kernel.unloadGBean(gbean.getAbstractName()); 115 } catch (GBeanNotFoundException e1) { 116 } 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 } 147 148 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 []{"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 |