2
* Copyright 2004-2005 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.apache.catalina.storeconfig;
19
import java.io.IOException;
21
import org.apache.catalina.Container;
22
import org.apache.catalina.core.StandardContext;
23
import org.apache.catalina.core.StandardHost;
26
* store StandardContext Attributes ... TODO DefaultContext Handling
28
* @author Peter Rossbach
31
public class StoreContextAppender extends StoreAppender {
34
* Print Context Values. <ul><li> Spezial handling to default workDir.
35
* </li><li> Don't save path at external context.xml </li><li> Don't
36
* generate docBase for host.appBase webapps <LI></ul>
38
* @see org.apache.catalina.config.StoreAppender#isPrintValue(java.lang.Object,
39
* java.lang.Object, java.lang.String,
40
* org.apache.catalina.config.StoreDescription)
42
public boolean isPrintValue(Object bean, Object bean2, String attrName,
43
StoreDescription desc) {
44
boolean isPrint = super.isPrintValue(bean, bean2, attrName, desc);
46
StandardContext context = ((StandardContext) bean);
47
if ("workDir".equals(attrName)) {
48
String defaultWorkDir = getDefaultWorkDir(context);
49
isPrint = !defaultWorkDir.equals(context.getWorkDir());
50
} else if ("path".equals(attrName)) {
51
isPrint = desc.isStoreSeparate()
52
&& desc.isExternalAllowed()
53
&& context.getConfigFile() == null;
54
} else if ("docBase".equals(attrName)) {
55
Container host = context.getParent();
56
if (host instanceof StandardHost) {
57
File appBase = getAppBase(((StandardHost) host));
58
File docBase = getDocBase(context,appBase);
59
isPrint = !appBase.equals(docBase.getParentFile());
66
protected File getAppBase(StandardHost host) {
69
File file = new File(host.getAppBase());
70
if (!file.isAbsolute())
71
file = new File(System.getProperty("catalina.base"), host
74
appBase = file.getCanonicalFile();
75
} catch (IOException e) {
82
protected File getDocBase(StandardContext context, File appBase) {
85
File file = new File(context.getDocBase());
86
if (!file.isAbsolute())
87
file = new File(appBase, context.getDocBase());
89
docBase = file.getCanonicalFile();
90
} catch (IOException e) {
98
* Make default Work Dir.
101
* @return The default working directory for the context.
103
protected String getDefaultWorkDir(StandardContext context) {
104
String defaultWorkDir = null;
105
String contextPath = context.getPath().length() == 0 ? "_" : context
106
.getPath().substring(1);
107
Container host = context.getParent();
108
if (host instanceof StandardHost) {
109
String hostWorkDir = ((StandardHost) host).getWorkDir();
110
if (hostWorkDir != null) {
111
defaultWorkDir = hostWorkDir + File.separator + contextPath;
113
String engineName = context.getParent().getParent().getName();
114
String hostName = context.getParent().getName();
115
defaultWorkDir = "work" + File.separator + engineName
116
+ File.separator + hostName + File.separator
120
return defaultWorkDir;
124
* Generate a real default StandardContext TODO read and interpret the
125
* default context.xml and context.xml.default TODO Cache a Default
126
* StandardContext ( with reloading strategy) TODO remove really all
127
* elements, but detection is hard... To Listener or Valve from same class?>
129
* @see org.apache.catalina.storeconfig.StoreAppender#defaultInstance(java.lang.Object)
131
public Object defaultInstance(Object bean) throws InstantiationException,
132
IllegalAccessException {
133
if (bean instanceof StandardContext) {
134
StandardContext defaultContext = new StandardContext();
136
* if (!((StandardContext) bean).getOverride()) {
137
* defaultContext.setParent(((StandardContext)bean).getParent());
138
* ContextConfig contextConfig = new ContextConfig();
139
* defaultContext.addLifecycleListener(contextConfig);
140
* contextConfig.setContext(defaultContext);
141
* contextConfig.processContextConfig(new File(contextConfig
142
* .getBaseDir(), "conf/context.xml"));
143
* contextConfig.processContextConfig(new File(contextConfig
144
* .getConfigBase(), "context.xml.default")); }
146
return defaultContext;
148
return super.defaultInstance(bean);