KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > StoreConfig


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

16 package org.apache.catalina.storeconfig;
17
18 import java.io.PrintWriter JavaDoc;
19
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.MalformedObjectNameException JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
24 import org.apache.catalina.Context;
25 import org.apache.catalina.Host;
26 import org.apache.catalina.Server;
27 import org.apache.catalina.ServerFactory;
28 import org.apache.catalina.Service;
29 import org.apache.catalina.core.StandardContext;
30 import org.apache.catalina.mbeans.MBeanUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * Store Server/Service/Host/Context at file or PrintWriter. Default server.xml
36  * is at $catalina.base/conf/server.xml
37  *
38  * @author Peter Rossbach
39  *
40  */

41 public class StoreConfig implements IStoreConfig {
42     private static Log log = LogFactory.getLog(StoreConfig.class);
43
44     private String JavaDoc serverFilename = "conf/server.xml";
45
46     private StoreRegistry registry;
47
48     /**
49      * get server.xml location
50      *
51      * @return
52      */

53     public String JavaDoc getServerFilename() {
54         return serverFilename;
55     }
56
57     /**
58      * set new server.xml location
59      *
60      * @param string
61      */

62     public void setServerFilename(String JavaDoc string) {
63         serverFilename = string;
64     }
65
66     /*
67      * Get the StoreRegistry with all factory to generate the
68      * server.xml/context.xml files
69      *
70      * @see org.apache.catalina.config.IStoreConfig#getRegistry()
71      */

72     public StoreRegistry getRegistry() {
73         return registry;
74     }
75
76     /*
77      * set StoreRegistry
78      *
79      * @see org.apache.catalina.config.IStoreConfig#setRegistry(org.apache.catalina.config.ConfigurationRegistry)
80      */

81     public void setRegistry(StoreRegistry aRegistry) {
82         registry = aRegistry;
83     }
84
85     /**
86      * Store current Server
87      *
88      * @see org.apache.catalina.ServerFactory#getServer()
89      */

90     public synchronized void storeConfig() {
91         store(ServerFactory.getServer());
92     }
93
94     /**
95      * Store Server from Object Name (Catalina:type=Server)
96      *
97      * @param aServerName
98      * Server ObjectName
99      * @param backup
100      * @param externalAllowed
101      * s *
102      * @throws MalformedObjectNameException
103      */

104     public synchronized void storeServer(String JavaDoc aServerName, boolean backup,
105             boolean externalAllowed) throws MalformedObjectNameException JavaDoc {
106         if (aServerName == null || aServerName.length() == 0) {
107             if (log.isErrorEnabled())
108                 log.error("Please, call with a correct server ObjectName!");
109             return;
110         }
111         MBeanServer JavaDoc mserver = MBeanUtils.createServer();
112         ObjectName JavaDoc objectName = new ObjectName JavaDoc(aServerName);
113         if (mserver.isRegistered(objectName)) {
114             try {
115                 Server JavaDoc aServer = (Server JavaDoc) mserver.getAttribute(objectName,
116                         "managedResource");
117                 StoreDescription desc = null;
118                 desc = getRegistry().findDescription(StandardContext.class);
119                 if (desc != null) {
120                     boolean oldSeparate = desc.isStoreSeparate();
121                     boolean oldBackup = desc.isBackup();
122                     boolean oldExternalAllowed = desc.isExternalAllowed();
123                     try {
124                         desc.setStoreSeparate(true);
125                         desc.setBackup(backup);
126                         desc.setExternalAllowed(externalAllowed);
127                         store((Server JavaDoc) aServer);
128                     } finally {
129                         desc.setStoreSeparate(oldSeparate);
130                         desc.setBackup(oldBackup);
131                         desc.setExternalAllowed(oldExternalAllowed);
132                     }
133                 } else
134                     store((Server JavaDoc) aServer);
135             } catch (Exception JavaDoc e) {
136                 if (log.isInfoEnabled())
137                     log.info("Object " + aServerName
138                             + " is no a Server instance or store exception", e);
139             }
140         } else if (log.isInfoEnabled())
141             log.info("Server " + aServerName + " not found!");
142     }
143
144     /**
145      * Store a Context from ObjectName
146      *
147      * @param aContextName
148      * MBean ObjectName
149      * @param backup
150      * @param externalAllowed
151      * @throws MalformedObjectNameException
152      */

153     public synchronized void storeContext(String JavaDoc aContextName, boolean backup,
154             boolean externalAllowed) throws MalformedObjectNameException JavaDoc {
155         if (aContextName == null || aContextName.length() == 0) {
156             if (log.isErrorEnabled())
157                 log.error("Please, call with a correct context ObjectName!");
158             return;
159         }
160         MBeanServer JavaDoc mserver = MBeanUtils.createServer();
161         ObjectName JavaDoc objectName = new ObjectName JavaDoc(aContextName);
162         if (mserver.isRegistered(objectName)) {
163             try {
164                 Context aContext = (Context) mserver.getAttribute(objectName,
165                         "managedResource");
166                 String JavaDoc configFile = aContext.getConfigFile();
167                 if (configFile != null) {
168                     try {
169                         StoreDescription desc = null;
170                         desc = getRegistry().findDescription(
171                                 aContext.getClass());
172                         if (desc != null) {
173                             boolean oldSeparate = desc.isStoreSeparate();
174                             boolean oldBackup = desc.isBackup();
175                             boolean oldExternalAllowed = desc
176                                     .isExternalAllowed();
177                             try {
178                                 desc.setStoreSeparate(true);
179                                 desc.setBackup(backup);
180                                 desc.setExternalAllowed(externalAllowed);
181                                 desc.getStoreFactory()
182                                         .store(null, -2, aContext);
183                             } finally {
184                                 desc.setStoreSeparate(oldSeparate);
185                                 desc.setBackup(oldBackup);
186                                 desc.setBackup(oldExternalAllowed);
187                             }
188                         }
189                     } catch (Exception JavaDoc e) {
190                         log.error(e);
191                     }
192                 } else
193                     log.error("Missing configFile at Context "
194                             + aContext.getPath() + " to store!");
195             } catch (Exception JavaDoc e) {
196                 if (log.isInfoEnabled())
197                     log
198                             .info(
199                                     "Object "
200                                             + aContextName
201                                             + " is no a context instance or store exception",
202                                     e);
203             }
204         } else if (log.isInfoEnabled())
205             log.info("Context " + aContextName + " not found!");
206     }
207
208     /**
209      * Write the configuration information for this entire <code>Server</code>
210      * out to the server.xml configuration file.
211      *
212      */

213     public synchronized void store(Server JavaDoc aServer) {
214
215         StoreFileMover mover = new StoreFileMover(System
216                 .getProperty("catalina.base"), getServerFilename(),
217                 getRegistry().getEncoding());
218         // Open an output writer for the new configuration file
219
try {
220             PrintWriter JavaDoc writer = mover.getWriter();
221
222             try {
223                 store(writer, -2, aServer);
224             } finally {
225                 // Flush and close the output file
226
try {
227                     writer.flush();
228                 } catch (Exception JavaDoc e) {
229                     log.error(e);
230                 }
231                 try {
232                     writer.close();
233                 } catch (Exception JavaDoc e) {
234                     throw (e);
235                 }
236             }
237             mover.move();
238         } catch (Exception JavaDoc e) {
239             log.error(e);
240         }
241     }
242
243     /*
244      * (non-Javadoc)
245      *
246      * @see org.apache.catalina.config.IStoreConfig#store(org.apache.catalina.Context)
247      */

248     public synchronized void store(Context aContext) {
249         String JavaDoc configFile = aContext.getConfigFile();
250         if (configFile != null) {
251             try {
252                 StoreDescription desc = null;
253                 desc = getRegistry().findDescription(aContext.getClass());
254                 if (desc != null) {
255                     boolean old = desc.isStoreSeparate();
256                     try {
257                         desc.setStoreSeparate(true);
258                         desc.getStoreFactory().store(null, -2, aContext);
259                     } finally {
260                         desc.setStoreSeparate(old);
261                     }
262                 }
263             } catch (Exception JavaDoc e) {
264                 log.error(e);
265             }
266         } else
267             log.error("Missing configFile at Context " + aContext.getPath());
268
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.apache.catalina.config.IStoreConfig#store(java.io.PrintWriter,
275      * int, org.apache.catalina.Context)
276      */

277     public synchronized void store(PrintWriter JavaDoc aWriter, int indent,
278             Context aContext) {
279         boolean oldSeparate = true;
280         StoreDescription desc = null;
281         try {
282             desc = getRegistry().findDescription(aContext.getClass());
283             oldSeparate = desc.isStoreSeparate();
284             desc.setStoreSeparate(false);
285             desc.getStoreFactory().store(aWriter, indent, aContext);
286         } catch (Exception JavaDoc e) {
287             log.error(e);
288         } finally {
289             if (desc != null)
290                 desc.setStoreSeparate(oldSeparate);
291         }
292     }
293
294     /*
295      * (non-Javadoc)
296      *
297      * @see org.apache.catalina.config.IStoreConfig#store(java.io.PrintWriter,
298      * int, org.apache.catalina.Host)
299      */

300     public synchronized void store(PrintWriter JavaDoc aWriter, int indent, Host aHost) {
301         try {
302             StoreDescription desc = getRegistry().findDescription(
303                     aHost.getClass());
304             desc.getStoreFactory().store(aWriter, indent, aHost);
305         } catch (Exception JavaDoc e) {
306             log.error(e);
307         }
308     }
309
310     /*
311      * (non-Javadoc)
312      *
313      * @see org.apache.catalina.config.IStoreConfig#store(java.io.PrintWriter,
314      * int, org.apache.catalina.Service)
315      */

316     public synchronized void store(PrintWriter JavaDoc aWriter, int indent,
317             Service aService) {
318         try {
319             StoreDescription desc = getRegistry().findDescription(
320                     aService.getClass());
321             desc.getStoreFactory().store(aWriter, indent, aService);
322         } catch (Exception JavaDoc e) {
323             log.error(e);
324         }
325     }
326
327     /**
328      * Store the state of this Server MBean (which will recursively store
329      * everything)
330      *
331      * @param writer
332      * @param indent
333      * @param aServer
334      */

335     public synchronized void store(PrintWriter JavaDoc writer, int indent,
336             Server JavaDoc aServer) {
337         try {
338             StoreDescription desc = getRegistry().findDescription(
339                     aServer.getClass());
340             desc.getStoreFactory().store(writer, indent, aServer);
341         } catch (Exception JavaDoc e) {
342             log.error(e);
343         }
344     }
345
346 }
Popular Tags