KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import javax.print.DocPrintJob JavaDoc;
22
23 import org.apache.catalina.Container;
24 import org.apache.catalina.core.StandardContext;
25 import org.apache.catalina.core.StandardHost;
26
27 /**
28  * store StandardContext Attributes ... TODO DefaultContext Handling
29  *
30  * @author Peter Rossbach
31  *
32  */

33 public class StoreContextAppender extends StoreAppender {
34
35     /*
36      * Print Context Values. <ul><li> Spezial handling to default workDir.
37      * </li><li> Don't save path at external context.xml </li><li> Don't
38      * generate docBase for host.appBase webapps <LI></ul>
39      *
40      * @see org.apache.catalina.config.StoreAppender#isPrintValue(java.lang.Object,
41      * java.lang.Object, java.lang.String,
42      * org.apache.catalina.config.StoreDescription)
43      */

44     public boolean isPrintValue(Object JavaDoc bean, Object JavaDoc bean2, String JavaDoc attrName,
45             StoreDescription desc) {
46         boolean isPrint = super.isPrintValue(bean, bean2, attrName, desc);
47         if (isPrint) {
48             StandardContext context = ((StandardContext) bean);
49             if ("workDir".equals(attrName)) {
50                 String JavaDoc defaultWorkDir = getDefaultWorkDir(context);
51                 isPrint = !defaultWorkDir.equals(context.getWorkDir());
52             } else if ("path".equals(attrName)) {
53                 isPrint = desc.isStoreSeparate()
54                             && desc.isExternalAllowed()
55                             && context.getConfigFile() == null;
56             } else if ("docBase".equals(attrName)) {
57                 Container host = context.getParent();
58                 if (host instanceof StandardHost) {
59                     File JavaDoc appBase = getAppBase(((StandardHost) host));
60                     File JavaDoc docBase = getDocBase(context,appBase);
61                     isPrint = !appBase.equals(docBase.getParentFile());
62                 }
63             }
64         }
65         return isPrint;
66     }
67
68     protected File JavaDoc getAppBase(StandardHost host) {
69
70         File JavaDoc appBase;
71         File JavaDoc file = new File JavaDoc(host.getAppBase());
72         if (!file.isAbsolute())
73             file = new File JavaDoc(System.getProperty("catalina.base"), host
74                     .getAppBase());
75         try {
76             appBase = file.getCanonicalFile();
77         } catch (IOException JavaDoc e) {
78             appBase = file;
79         }
80         return (appBase);
81
82     }
83
84     protected File JavaDoc getDocBase(StandardContext context, File JavaDoc appBase) {
85
86         File JavaDoc docBase;
87         File JavaDoc file = new File JavaDoc(context.getDocBase());
88         if (!file.isAbsolute())
89             file = new File JavaDoc(appBase, context.getDocBase());
90         try {
91             docBase = file.getCanonicalFile();
92         } catch (IOException JavaDoc e) {
93             docBase = file;
94         }
95         return (docBase);
96
97     }
98
99     /**
100      * Make default Work Dir
101      *
102      * @param context
103      * @return
104      */

105     protected String JavaDoc getDefaultWorkDir(StandardContext context) {
106         String JavaDoc defaultWorkDir = null;
107         String JavaDoc contextPath = context.getPath().length() == 0 ? "_" : context
108                 .getPath().substring(1);
109         Container host = context.getParent();
110         if (host instanceof StandardHost) {
111             String JavaDoc hostWorkDir = ((StandardHost) host).getWorkDir();
112             if (hostWorkDir != null) {
113                 defaultWorkDir = hostWorkDir + File.separator + contextPath;
114             } else {
115                 String JavaDoc engineName = context.getParent().getParent().getName();
116                 String JavaDoc hostName = context.getParent().getName();
117                 defaultWorkDir = "work" + File.separator + engineName
118                         + File.separator + hostName + File.separator
119                         + contextPath;
120             }
121         }
122         return defaultWorkDir;
123     }
124
125     /*
126      * Generate a real default StandardContext TODO read and interpret the
127      * default context.xml and context.xml.default TODO Cache a Default
128      * StandardContext ( with reloading strategy) TODO remove really all
129      * elements, but detection is hard... To Listener or Valve from same class?>
130      *
131      * @see org.apache.catalina.storeconfig.StoreAppender#defaultInstance(java.lang.Object)
132      */

133     public Object JavaDoc defaultInstance(Object JavaDoc bean) throws InstantiationException JavaDoc,
134             IllegalAccessException JavaDoc {
135         if (bean instanceof StandardContext) {
136             StandardContext defaultContext = new StandardContext();
137             /*
138              * if (!((StandardContext) bean).getOverride()) {
139              * defaultContext.setParent(((StandardContext)bean).getParent());
140              * ContextConfig contextConfig = new ContextConfig();
141              * defaultContext.addLifecycleListener(contextConfig);
142              * contextConfig.setContext(defaultContext);
143              * contextConfig.processContextConfig(new File(contextConfig
144              * .getBaseDir(), "conf/context.xml"));
145              * contextConfig.processContextConfig(new File(contextConfig
146              * .getConfigBase(), "context.xml.default")); }
147              */

148             return defaultContext;
149         } else
150             return super.defaultInstance(bean);
151     }
152 }
Popular Tags