1 17 package org.apache.geronimo.system.configuration; 18 19 import java.io.BufferedReader ; 20 import java.io.BufferedWriter ; 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileReader ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.net.URI ; 27 import java.net.URISyntaxException ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import javax.management.ObjectName ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.geronimo.gbean.GBeanInfo; 37 import org.apache.geronimo.gbean.GBeanInfoBuilder; 38 import org.apache.geronimo.gbean.GBeanLifecycle; 39 import org.apache.geronimo.kernel.Kernel; 40 import org.apache.geronimo.kernel.config.ConfigurationInfo; 41 import org.apache.geronimo.kernel.config.NoSuchStoreException; 42 import org.apache.geronimo.kernel.config.PersistentConfigurationList; 43 import org.apache.geronimo.kernel.config.ConfigurationUtil; 44 import org.apache.geronimo.kernel.config.ConfigurationManager; 45 import org.apache.geronimo.kernel.management.State; 46 import org.apache.geronimo.system.serverinfo.ServerInfo; 47 48 54 public class FileConfigurationList implements GBeanLifecycle, PersistentConfigurationList { 55 private static final Log log = LogFactory.getLog(PersistentConfigurationList.class); 56 57 60 private final Kernel kernel; 61 62 65 private final ConfigurationManager configurationManager; 66 67 70 private final ServerInfo serverInfo; 71 72 76 private final String configFile; 77 78 83 private boolean kernelFullyStarted = false; 84 85 88 private File configList; 89 90 93 private Runnable hook; 94 95 public FileConfigurationList(Kernel kernel, ServerInfo serverInfo, String configDir) { 96 this.kernel = kernel; 97 configurationManager = ConfigurationUtil.getConfigurationManager(kernel); 98 this.serverInfo = serverInfo; 99 this.configFile = configDir; 100 } 101 102 public void doStart() throws Exception { 103 configList = serverInfo.resolve(configFile); 104 File parent = configList.getParentFile(); 105 if (!parent.isDirectory()) { 106 if (!parent.mkdirs()) { 107 throw new IOException ("Unable to create directory for list:" + parent); 108 } 109 } 110 hook = new Runnable () { 111 public void run() { 112 try { 113 save(); 114 } catch (IOException e) { 115 log.error("Unable to save configuration on shutdown", e); 116 } 117 } 118 }; 119 kernel.registerShutdownHook(hook); 120 } 121 122 public void doStop() throws Exception { 123 doFail(); 124 } 125 126 public void doFail() { 127 kernel.unregisterShutdownHook(hook); 128 hook = null; 129 configList = null; 130 } 131 132 public synchronized boolean isKernelFullyStarted() { 133 return kernelFullyStarted; 134 } 135 136 public synchronized void setKernelFullyStarted(boolean kernelFullyStarted) { 137 this.kernelFullyStarted = kernelFullyStarted; 138 } 139 140 public synchronized void save() throws IOException { 141 if (!kernelFullyStarted) { 142 log.info("Configuration list was not saved. Kernel was never fully started."); 143 return; 144 } 145 146 BufferedWriter writer = new BufferedWriter (new FileWriter (configList)); 147 try { 148 List stores = configurationManager.listStores(); 149 for (Iterator i = stores.iterator(); i.hasNext();) { 150 ObjectName storeName = (ObjectName ) i.next(); 151 List configList = configurationManager.listConfigurations(storeName); 152 for (Iterator j = configList.iterator(); j.hasNext();) { 153 ConfigurationInfo info = (ConfigurationInfo) j.next(); 154 if (info.getState() == State.RUNNING) { 155 writer.write(info.getConfigID().toString()); 156 writer.newLine(); 157 } 158 } 159 } 160 writer.close(); 161 } catch (NoSuchStoreException e) { 162 writer.close(); 163 configList.delete(); 164 } 165 log.info("Saved running configuration list"); 166 } 167 168 public List restore() throws IOException { 169 FileReader fileReader; 170 try { 171 fileReader = new FileReader (configList); 172 } catch (FileNotFoundException e) { 173 return Collections.EMPTY_LIST; 174 } 175 BufferedReader reader = new BufferedReader (fileReader); 176 try { 177 List configs = new ArrayList (); 178 String line; 179 while ((line = reader.readLine()) != null) { 180 try { 181 configs.add(new URI (line)); 182 } catch (URISyntaxException e) { 183 throw new IOException ("Invalid URI in config list: " + line); 184 } 185 } 186 return configs; 187 } finally { 188 reader.close(); 189 } 190 } 191 192 public static final GBeanInfo GBEAN_INFO; 193 194 static { 195 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(FileConfigurationList.class, "PersistentConfigurationList"); 196 infoFactory.addInterface(PersistentConfigurationList.class); 197 infoFactory.addAttribute("kernel", Kernel.class, false); 198 infoFactory.addAttribute("kernelFullyStarted", boolean.class, false); 199 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean"); 200 infoFactory.addAttribute("configFile", String .class, true); 201 infoFactory.setConstructor(new String []{"kernel", "ServerInfo", "configFile"}); 202 GBEAN_INFO = infoFactory.getBeanInfo(); 203 } 204 205 public static GBeanInfo getGBeanInfo() { 206 return GBEAN_INFO; 207 } 208 } 209 | Popular Tags |