1 17 18 package org.apache.geronimo.kernel.config; 19 20 import java.io.IOException ; 21 import java.net.URI ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 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 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 stores; 48 private final ShutdownHook shutdownHook; 49 50 public ConfigurationManagerImpl(Kernel kernel, Collection stores) { 51 this.kernel = kernel; 52 this.stores = stores; 53 shutdownHook = new ShutdownHook(kernel); 54 } 55 56 public List listStores() { 57 List storeSnapshot = getStores(); 58 List result = new ArrayList (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 listConfigurations(ObjectName storeName) throws NoSuchStoreException { 67 List 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 configID) { 78 try { 79 ObjectName name = Configuration.getConfigurationObjectName(configID); 80 return kernel.isLoaded(name); 81 } catch (MalformedObjectNameException e) { 82 return false; 83 } 84 } 85 86 public ObjectName load(URI configID) throws NoSuchConfigException, IOException , InvalidConfigException { 87 List 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 loadRecursive(URI configID) throws NoSuchConfigException, IOException , InvalidConfigException { 99 try { 100 LinkedList ancestors = new LinkedList (); 101 while (configID != null && !isLoaded(configID)) { 102 ObjectName name = load(configID); 103 ancestors.addFirst(name); 104 configID = (URI ) kernel.getAttribute(name, "parentId"); 105 } 106 return ancestors; 107 } catch (NoSuchConfigException e) { 108 throw e; 109 } catch (IOException e) { 110 throw e; 111 } catch (InvalidConfigException e) { 112 throw e; 113 } catch (Exception e) { 114 throw new InvalidConfigException(e); 115 } 116 } 117 118 public void unload(URI configID) throws NoSuchConfigException { 119 ObjectName configName; 120 try { 121 configName = Configuration.getConfigurationObjectName(configID); 122 } catch (MalformedObjectNameException 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 getStores() { 134 return new ArrayList (stores); 135 } 136 137 public void doStart() { 138 kernel.registerShutdownHook(shutdownHook); 139 } 140 141 private static final ObjectName 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 []{"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 { 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 configs = kernel.listGBeans(CONFIG_QUERY); 176 if (configs.isEmpty()) { 177 return; 178 } 179 for (Iterator i = configs.iterator(); i.hasNext();) { 180 ObjectName configName = (ObjectName ) i.next(); 181 if (kernel.isLoaded(configName)) { 182 try { 183 kernel.stopGBean(configName); 184 } catch (GBeanNotFoundException e) { 185 } catch (InternalKernelException e) { 187 log.warn("Could not stop configuration: " + configName, e); 188 } 189 try { 190 kernel.unloadGBean(configName); 191 } catch (GBeanNotFoundException e) { 192 } 194 } 195 } 196 } 197 } 198 } 199 } 200 | Popular Tags |