KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > server > DefaultAxisServerFactory


1 /*
2  * Copyright 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.axis.server;
18
19 import org.apache.axis.AxisEngine;
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.AxisProperties;
22 import org.apache.axis.EngineConfiguration;
23 import org.apache.axis.components.logger.LogFactory;
24 import org.apache.axis.utils.ClassUtils;
25 import org.apache.axis.utils.Messages;
26 import org.apache.commons.logging.Log;
27
28 import java.io.File JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Helper class for obtaining AxisServers. Default implementation.
33  *
34  * @author Glen Daniels (gdaniels@apache.org)
35  */

36
37 public class DefaultAxisServerFactory implements AxisServerFactory {
38     protected static Log log =
39         LogFactory.getLog(DefaultAxisServerFactory.class.getName());
40
41     /**
42      * Get an AxisServer.
43      * <p>
44      * Factory obtains EngineConfiguration as first found of the following:
45      * a) EngineConfiguration instance, keyed to
46      * EngineConfiguration.PROPERTY_NAME in 'environment', or
47      * b) EngineConfiguration class name, keyed to
48      * AxisEngine.PROP_DEFAULT_CONFIG_CLASS in AxisProperties.
49      * Class is instantiated if found.
50      * <p>
51      * If an EngineConfiguration cannot be located, the default
52      * AxisServer constructor is used.
53      * <p>
54      * The AxisServer's option AxisEngine.PROP_ATTACHMENT_DIR is set to
55      * the (first found) value of either AxisEngine.ENV_ATTACHMENT_DIR
56      * or AxisEngine.ENV_SERVLET_REALPATH.
57      *
58      * @param environment The following keys are used:
59      * AxisEngine.ENV_ATTACHMENT_DIR
60      * - Set as default value for Axis option
61      * AxisEngine.PROP_ATTACHMENT_DIR
62      * AxisEngine.ENV_SERVLET_REALPATH
63      * - Set as alternate default value for Axis option
64      * AxisEngine.PROP_ATTACHMENT_DIR
65      * EngineConfiguration.PROPERTY_NAME
66      * - Instance of EngineConfiguration,
67      * if not set then an attempt is made to retreive
68      * a class name from AxisEngine.PROP_CONFIG_CLASS
69      */

70     public AxisServer getServer(Map JavaDoc environment) throws AxisFault {
71         log.debug("Enter: DefaultAxisServerFactory::getServer");
72
73         AxisServer ret = createServer(environment);
74
75         if (ret != null) {
76             if (environment != null) {
77                 ret.setOptionDefault(AxisEngine.PROP_ATTACHMENT_DIR,
78                     (String JavaDoc)environment.get(AxisEngine.ENV_ATTACHMENT_DIR));
79
80                 ret.setOptionDefault(AxisEngine.PROP_ATTACHMENT_DIR,
81                     (String JavaDoc)environment.get(AxisEngine.ENV_SERVLET_REALPATH));
82             }
83
84             String JavaDoc attachmentsdir = (String JavaDoc)ret.getOption(AxisEngine.PROP_ATTACHMENT_DIR);
85
86             if (attachmentsdir != null) {
87                 File JavaDoc attdirFile = new File JavaDoc(attachmentsdir);
88                 if (!attdirFile.isDirectory()) {
89                     attdirFile.mkdirs();
90                 }
91             }
92         }
93
94         log.debug("Exit: DefaultAxisServerFactory::getServer");
95
96         return ret;
97     }
98
99     /**
100      * Do the actual work of creating a new AxisServer, using the
101      * configuration, or using the default constructor if null is passed.
102      *
103      * @return a shiny new AxisServer, ready for use.
104      */

105     private static AxisServer createServer(Map JavaDoc environment) {
106         EngineConfiguration config = getEngineConfiguration(environment);
107
108         // Return new AxisServer using the appropriate config
109
return (config == null) ? new AxisServer() : new AxisServer(config);
110     }
111
112     /**
113      * Look for EngineConfiguration, it is first of:
114      * a) EngineConfiguration instance, keyed to
115      * EngineConfiguration.PROPERTY_NAME in 'environment', or
116      * b) EngineConfiguration class name, keyed to
117      * AxisEngine.PROP_DEFAULT_CONFIG_CLASS in AxisProperties.
118      * Class is instantiated if found.
119      */

120     private static EngineConfiguration getEngineConfiguration(Map JavaDoc environment)
121     {
122         log.debug("Enter: DefaultAxisServerFactory::getEngineConfiguration");
123
124         EngineConfiguration config = null;
125
126         if (environment != null) {
127             try {
128                 config = (EngineConfiguration)environment.get(EngineConfiguration.PROPERTY_NAME);
129             } catch (ClassCastException JavaDoc e) {
130                 log.warn(Messages.getMessage("engineConfigWrongClass00"), e);
131                 // Fall through
132
}
133         }
134
135         if (config == null) {
136             // A default engine configuration class may be set in a system
137
// property. If so, try creating an engine configuration.
138
String JavaDoc configClass = AxisProperties.getProperty(AxisEngine.PROP_DEFAULT_CONFIG_CLASS);
139             if (configClass != null) {
140                 try {
141                     // Got one - so try to make it (which means it had better have
142
// a default constructor - may make it possible later to pass
143
// in some kind of environmental parameters...)
144
Class JavaDoc cls = ClassUtils.forName(configClass);
145                     config = (EngineConfiguration)cls.newInstance();
146                 } catch (ClassNotFoundException JavaDoc e) {
147                     log.warn(Messages.getMessage("engineConfigNoClass00", configClass), e);
148                     // Fall through
149
} catch (InstantiationException JavaDoc e) {
150                     log.warn(Messages.getMessage("engineConfigNoInstance00", configClass), e);
151                     // Fall through
152
} catch (IllegalAccessException JavaDoc e) {
153                     log.warn(Messages.getMessage("engineConfigIllegalAccess00", configClass), e);
154                     // Fall through
155
} catch (ClassCastException JavaDoc e) {
156                     log.warn(Messages.getMessage("engineConfigWrongClass01", configClass), e);
157                     // Fall through
158
}
159             }
160         }
161
162         log.debug("Exit: DefaultAxisServerFactory::getEngineConfiguration");
163
164         return config;
165     }
166 }
167
Popular Tags