KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.catalina.storeconfig;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.naming.directory.DirContext JavaDoc;
28
29 import org.apache.catalina.Container;
30 import org.apache.catalina.Context;
31 import org.apache.catalina.Engine;
32 import org.apache.catalina.Host;
33 import org.apache.catalina.Lifecycle;
34 import org.apache.catalina.LifecycleListener;
35 import org.apache.catalina.Loader;
36 import org.apache.catalina.Manager;
37 import org.apache.catalina.Pipeline;
38 import org.apache.catalina.Realm;
39 import org.apache.catalina.Valve;
40 import org.apache.catalina.core.StandardContext;
41 import org.apache.catalina.deploy.ApplicationParameter;
42 import org.apache.catalina.deploy.NamingResources;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.apache.naming.resources.ProxyDirContext;
46
47 /**
48  * Store server.xml Context element with all childs
49  * <ul>
50  * <li>Store all context at server.xml</li>
51  * <li>Store existing app.xml context a conf/enginename/hostname/app.xml</li>
52  * <li>Store with backup</li>
53  * </ul>
54  *
55  * @author Peter Rossbach
56  */

57 public class StandardContextSF extends StoreFactoryBase {
58
59     private static Log log = LogFactory.getLog(StandardContextSF.class);
60
61     /*
62      * Store a Context as Separate file as configFile value from context exists.
63      * filename can be relative to catalina.base.
64      *
65      * @see org.apache.catalina.config.IStoreFactory#store(java.io.PrintWriter,
66      * int, java.lang.Object)
67      */

68     public void store(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc aContext)
69             throws Exception JavaDoc {
70
71         if (aContext instanceof StandardContext) {
72             StoreDescription desc = getRegistry().findDescription(
73                     aContext.getClass());
74             if (desc.isStoreSeparate()) {
75                 String JavaDoc configFile = ((StandardContext) aContext)
76                         .getConfigFile();
77                 if (configFile != null) {
78                     if (desc.isExternalAllowed()) {
79                         if (desc.isBackup())
80                             storeWithBackup((StandardContext) aContext);
81                         else
82                             storeContextSeparate(aWriter, indent,
83                                     (StandardContext) aContext);
84                         return;
85                     }
86                 }
87             }
88         }
89         super.store(aWriter, indent, aContext);
90
91     }
92
93     /**
94      * Store a Context without backup add separate file or when configFile =
95      * null a aWriter.
96      *
97      * @param aWriter
98      * @param indent
99      * @param aContext
100      * @throws Exception
101      */

102     protected void storeContextSeparate(PrintWriter JavaDoc aWriter, int indent,
103             StandardContext aContext) throws Exception JavaDoc {
104         String JavaDoc configFile = aContext.getConfigFile();
105         PrintWriter JavaDoc writer = null;
106         if (configFile != null) {
107             File JavaDoc config = new File JavaDoc(configFile);
108             if (!config.isAbsolute()) {
109                 config = new File JavaDoc(System.getProperty("catalina.base"),
110                         configFile);
111             }
112             if (log.isInfoEnabled())
113                 log.info("Store Context " + aContext.getPath()
114                         + " separate at file " + config);
115             try {
116                 writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(
117                         new FileOutputStream JavaDoc(config), getRegistry()
118                                 .getEncoding()));
119                 storeXMLHead(writer);
120                 super.store(writer, -2, aContext);
121             } finally {
122                 if (writer != null) {
123                     try {
124                         writer.flush();
125                     } catch (Exception JavaDoc e) {
126                         ;
127                     }
128                     try {
129                         writer.close();
130                     } catch (Throwable JavaDoc t) {
131                         ;
132                     }
133                 }
134             }
135         } else {
136             super.store(aWriter, indent, aContext);
137         }
138     }
139
140     /**
141      * Store the Context with a Backup
142      *
143      * @param aContext
144      * @throws Exception
145      */

146     protected void storeWithBackup(StandardContext aContext) throws Exception JavaDoc {
147         StoreFileMover mover = getConfigFileWriter((Context JavaDoc) aContext);
148         if (mover != null) {
149             if (log.isInfoEnabled())
150                 log.info("Store Context " + aContext.getPath()
151                         + " separate with backup (at file "
152                         + mover.getConfigSave() + " )");
153             PrintWriter JavaDoc writer = mover.getWriter();
154             try {
155                 storeXMLHead(writer);
156                 super.store(writer, -2, aContext);
157             } finally {
158                 // Flush and close the output file
159
try {
160                     writer.flush();
161                 } catch (Exception JavaDoc e) {
162                     log.error(e);
163                 }
164                 try {
165                     writer.close();
166                 } catch (Exception JavaDoc e) {
167                     throw (e);
168                 }
169             }
170             mover.move();
171         }
172     }
173
174     /**
175      * Get explizit writer for context (context.getConfigFile())
176      *
177      * @param context
178      * @return
179      * @throws IOException
180      */

181     protected StoreFileMover getConfigFileWriter(Context JavaDoc context)
182             throws IOException JavaDoc {
183         String JavaDoc configFile = context.getConfigFile();
184         PrintWriter JavaDoc writer = null;
185         StoreFileMover mover = null;
186         if (configFile != null) {
187             File JavaDoc config = new File JavaDoc(configFile);
188             if (!config.isAbsolute()) {
189                 config = new File JavaDoc(System.getProperty("catalina.base"),
190                         configFile);
191             }
192             // Open an output writer for the new configuration file
193
mover = new StoreFileMover("", config.getCanonicalPath(),
194                     getRegistry().getEncoding());
195         }
196         return mover;
197     }
198
199     /**
200      * Store the specified Host properties.
201      *
202      * @param aWriter
203      * PrintWriter to which we are storing
204      * @param indent
205      * Number of spaces to indent this element
206      * @param aContext
207      * Context whose properties are being stored
208      *
209      * @exception Exception
210      * if an exception occurs while storing
211      */

212     public void storeChilds(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc aContext,
213             StoreDescription parentDesc) throws Exception JavaDoc {
214         if (aContext instanceof StandardContext) {
215             StandardContext context = (StandardContext) aContext;
216             // Store nested <Listener> elements
217
if (context instanceof Lifecycle) {
218                 LifecycleListener listeners[] = context
219                         .findLifecycleListeners();
220                 storeElementArray(aWriter, indent, listeners);
221             }
222             // Store nested <Valve> elements
223
if (context instanceof Pipeline) {
224                 Valve valves[] = ((Pipeline) context).getValves();
225                 storeElementArray(aWriter, indent, valves);
226             }
227
228             // Store nested <Loader> elements
229
Loader loader = context.getLoader();
230             storeElement(aWriter, indent, loader);
231
232             // Store nested <Manager> elements
233
Manager manager = context.getManager();
234             storeElement(aWriter, indent, manager);
235
236             // Store nested <Realm> element
237
Realm realm = context.getRealm();
238             if (realm != null) {
239                 Realm parentRealm = null;
240                 // @TODO is this case possible?
241
if (context.getParent() != null) {
242                     parentRealm = context.getParent().getRealm();
243                 }
244                 if (realm != parentRealm) {
245                     storeElement(aWriter, indent, realm);
246
247                 }
248             }
249             // Store nested resources
250
DirContext JavaDoc resources = context.getResources();
251             if (resources instanceof ProxyDirContext)
252                 resources = ((ProxyDirContext) resources).getDirContext();
253             storeElement(aWriter, indent, resources);
254
255             // Store nested <InstanceListener> elements
256
String JavaDoc iListeners[] = context.findInstanceListeners();
257             getStoreAppender().printTagArray(aWriter, "InstanceListener",
258                     indent + 2, iListeners);
259
260             // Store nested <WrapperListener> elements
261
String JavaDoc wLifecycles[] = context.findWrapperLifecycles();
262             getStoreAppender().printTagArray(aWriter, "WrapperListener",
263                     indent + 2, wLifecycles);
264             // Store nested <WrapperLifecycle> elements
265
String JavaDoc wListeners[] = context.findWrapperListeners();
266             getStoreAppender().printTagArray(aWriter, "WrapperLifecycle",
267                     indent + 2, wListeners);
268
269             // Store nested <Parameter> elements
270
ApplicationParameter[] appParams = context
271                     .findApplicationParameters();
272             storeElementArray(aWriter, indent, appParams);
273
274             // Store nested naming resources elements (EJB,Resource,...)
275
NamingResources nresources = context.getNamingResources();
276             storeElement(aWriter, indent, nresources);
277
278             // Store nested watched resources <WatchedResource>
279
String JavaDoc[] wresources = context.findWatchedResources();
280             wresources = filterWatchedResources(context, wresources);
281             getStoreAppender().printTagArray(aWriter, "WatchedResource",
282                     indent + 2, wresources);
283         }
284     }
285
286     /**
287      * Return a File object representing the "configuration root" directory for
288      * our associated Host.
289      */

290     protected File JavaDoc configBase(Context JavaDoc context) {
291
292         File JavaDoc file = new File JavaDoc(System.getProperty("catalina.base"), "conf");
293         Container host = (Host) context.getParent();
294
295         if ((host != null) && (host instanceof Host)) {
296             Container engine = host.getParent();
297             if ((engine != null) && (engine instanceof Engine)) {
298                 file = new File JavaDoc(file, engine.getName());
299             }
300             file = new File JavaDoc(file, host.getName());
301             try {
302                 file = file.getCanonicalFile();
303             } catch (IOException JavaDoc e) {
304                 log.error(e);
305             }
306         }
307         return (file);
308
309     }
310
311     /**
312      * filter out the default watched resources
313      *
314      * @param context
315      * @param wresources
316      * @return
317      * @throws IOException
318      * TODO relative watchedresource TODO absolute handling
319      * configFile TODO Filename case handling for Windows? TODO
320      * digester variable subsitution $catalina.base, $catalina.home
321      */

322     protected String JavaDoc[] filterWatchedResources(StandardContext context,
323             String JavaDoc[] wresources) throws IOException JavaDoc {
324         File JavaDoc configBase = configBase(context);
325         String JavaDoc confContext = new File JavaDoc(System.getProperty("catalina.base"),
326                 "conf/context.xml").getCanonicalPath();
327         String JavaDoc confHostDefault = new File JavaDoc(configBase, "context.xml.default")
328                 .getCanonicalPath();
329         String JavaDoc configFile = context.getConfigFile();
330
331         List JavaDoc resource = new ArrayList JavaDoc();
332         for (int i = 0; i < wresources.length; i++) {
333
334             if (wresources[i].equals(confContext))
335                 continue;
336             if (wresources[i].equals(confHostDefault))
337                 continue;
338             if (wresources[i].equals(configFile))
339                 continue;
340             resource.add(wresources[i]);
341         }
342         return (String JavaDoc[]) resource.toArray(new String JavaDoc[resource.size()]);
343     }
344
345 }
Popular Tags