KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > FileConfigurationList


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.configuration;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.BufferedWriter JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.management.ObjectName JavaDoc;
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 /**
49  * GBean that saves a list of configurations, for example to allow
50  * a server to restart automatically.
51  *
52  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
53  */

54 public class FileConfigurationList implements GBeanLifecycle, PersistentConfigurationList {
55     private static final Log log = LogFactory.getLog(PersistentConfigurationList.class);
56
57     /**
58      * The kernel for which we are persisting the configuration list.
59      */

60     private final Kernel kernel;
61
62     /**
63      * The ConfigurationManager for the kernel
64      */

65     private final ConfigurationManager configurationManager;
66
67     /**
68      * Used to resolve the location of the configuration file.
69      */

70     private final ServerInfo serverInfo;
71
72     /**
73      * The file to which we are saving the configurations. This is relative to the
74      * server base directory in server info.
75      */

76     private final String JavaDoc configFile;
77
78     /**
79      * Is the kernel fully started? Until the kernel is fully started, we will
80      * not write out a new configuration list. This stops a crtl^c during start up
81      * from completely overwriting the startup file with no content.
82      */

83     private boolean kernelFullyStarted = false;
84
85     /**
86      * The acutal absolute file where we write the configuration list.
87      */

88     private File JavaDoc configList;
89
90     /**
91      * Our hook the kernel calls before shutting down.
92      */

93     private Runnable JavaDoc hook;
94
95     public FileConfigurationList(Kernel kernel, ServerInfo serverInfo, String JavaDoc 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 JavaDoc {
103         configList = serverInfo.resolve(configFile);
104         File JavaDoc parent = configList.getParentFile();
105         if (!parent.isDirectory()) {
106             if (!parent.mkdirs()) {
107                 throw new IOException JavaDoc("Unable to create directory for list:" + parent);
108             }
109         }
110         hook = new Runnable JavaDoc() {
111             public void run() {
112                 try {
113                     save();
114                 } catch (IOException JavaDoc 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 JavaDoc {
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 JavaDoc {
141         if (!kernelFullyStarted) {
142             log.info("Configuration list was not saved. Kernel was never fully started.");
143             return;
144         }
145
146         BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(configList));
147         try {
148             List JavaDoc stores = configurationManager.listStores();
149             for (Iterator JavaDoc i = stores.iterator(); i.hasNext();) {
150                 ObjectName JavaDoc storeName = (ObjectName JavaDoc) i.next();
151                 List JavaDoc configList = configurationManager.listConfigurations(storeName);
152                 for (Iterator JavaDoc 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 JavaDoc restore() throws IOException JavaDoc {
169         FileReader JavaDoc fileReader;
170         try {
171             fileReader = new FileReader JavaDoc(configList);
172         } catch (FileNotFoundException JavaDoc e) {
173             return Collections.EMPTY_LIST;
174         }
175         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(fileReader);
176         try {
177             List JavaDoc configs = new ArrayList JavaDoc();
178             String JavaDoc line;
179             while ((line = reader.readLine()) != null) {
180                 try {
181                     configs.add(new URI JavaDoc(line));
182                 } catch (URISyntaxException JavaDoc e) {
183                     throw new IOException JavaDoc("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 JavaDoc.class, true);
201         infoFactory.setConstructor(new String JavaDoc[]{"kernel", "ServerInfo", "configFile"});
202         GBEAN_INFO = infoFactory.getBeanInfo();
203     }
204
205     public static GBeanInfo getGBeanInfo() {
206         return GBEAN_INFO;
207     }
208 }
209
Popular Tags