~ubuntu-branches/ubuntu/intrepid/tomcat5.5/intrepid

« back to all changes in this revision

Viewing changes to container/modules/storeconfig-ha/src/share/org/apache/catalina/storeconfig/StoreContextAppender.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-27 11:19:17 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060927111917-wov6fmkz3x6rsl68
Tags: 5.5.17-1ubuntu1
(Build-) depend on libmx4j-java (>= 3.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2004-2005 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;
 
19
import java.io.IOException;
 
20
 
 
21
import org.apache.catalina.Container;
 
22
import org.apache.catalina.core.StandardContext;
 
23
import org.apache.catalina.core.StandardHost;
 
24
 
 
25
/**
 
26
 * store StandardContext Attributes ... TODO DefaultContext Handling
 
27
 * 
 
28
 * @author Peter Rossbach
 
29
 *  
 
30
 */
 
31
public class StoreContextAppender extends StoreAppender {
 
32
 
 
33
    /*
 
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>
 
37
     * 
 
38
     * @see org.apache.catalina.config.StoreAppender#isPrintValue(java.lang.Object,
 
39
     *      java.lang.Object, java.lang.String,
 
40
     *      org.apache.catalina.config.StoreDescription)
 
41
     */
 
42
    public boolean isPrintValue(Object bean, Object bean2, String attrName,
 
43
            StoreDescription desc) {
 
44
        boolean isPrint = super.isPrintValue(bean, bean2, attrName, desc);
 
45
        if (isPrint) {
 
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());
 
60
                }
 
61
            }
 
62
        }
 
63
        return isPrint;
 
64
    }
 
65
 
 
66
    protected File getAppBase(StandardHost host) {
 
67
 
 
68
        File appBase;
 
69
        File file = new File(host.getAppBase());
 
70
        if (!file.isAbsolute())
 
71
            file = new File(System.getProperty("catalina.base"), host
 
72
                    .getAppBase());
 
73
        try {
 
74
            appBase = file.getCanonicalFile();
 
75
        } catch (IOException e) {
 
76
            appBase = file;
 
77
        }
 
78
        return (appBase);
 
79
 
 
80
    }
 
81
 
 
82
    protected File getDocBase(StandardContext context, File appBase) {
 
83
 
 
84
        File docBase;
 
85
        File file = new File(context.getDocBase());
 
86
        if (!file.isAbsolute())
 
87
            file = new File(appBase, context.getDocBase());
 
88
        try {
 
89
            docBase = file.getCanonicalFile();
 
90
        } catch (IOException e) {
 
91
            docBase = file;
 
92
        }
 
93
        return (docBase);
 
94
 
 
95
    }
 
96
 
 
97
    /**
 
98
     * Make default Work Dir.
 
99
     * 
 
100
     * @param context
 
101
     * @return The default working directory for the context.
 
102
     */
 
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;
 
112
            } else {
 
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
 
117
                        + contextPath;
 
118
            }
 
119
        }
 
120
        return defaultWorkDir;
 
121
    }
 
122
 
 
123
    /*
 
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?>
 
128
     * 
 
129
     * @see org.apache.catalina.storeconfig.StoreAppender#defaultInstance(java.lang.Object)
 
130
     */
 
131
    public Object defaultInstance(Object bean) throws InstantiationException,
 
132
            IllegalAccessException {
 
133
        if (bean instanceof StandardContext) {
 
134
            StandardContext defaultContext = new StandardContext();
 
135
            /*
 
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")); }
 
145
             */
 
146
            return defaultContext;
 
147
        } else
 
148
            return super.defaultInstance(bean);
 
149
    }
 
150
}