KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.server;
57
58 import org.jboss.axis.AxisEngine;
59 import org.jboss.axis.AxisFault;
60 import org.jboss.axis.AxisProperties;
61 import org.jboss.axis.EngineConfiguration;
62 import org.jboss.axis.utils.ClassUtils;
63 import org.jboss.axis.utils.Messages;
64 import org.jboss.logging.Logger;
65
66 import java.io.File JavaDoc;
67 import java.util.Map JavaDoc;
68
69 /**
70  * Helper class for obtaining AxisServers. Default implementation.
71  *
72  * @author Glen Daniels (gdaniels@macromedia.com)
73  */

74
75 public class DefaultAxisServerFactory implements AxisServerFactory
76 {
77    private static Logger log = Logger.getLogger(DefaultAxisServerFactory.class.getName());
78
79    /**
80     * Get an AxisServer.
81     * <p/>
82     * Factory obtains EngineConfiguration as first found of the following:
83     * a) EngineConfiguration instance, keyed to
84     * EngineConfiguration.PROPERTY_NAME in 'environment', or
85     * b) EngineConfiguration class name, keyed to
86     * AxisEngine.PROP_DEFAULT_CONFIG_CLASS in AxisProperties.
87     * Class is instantiated if found.
88     * <p/>
89     * If an EngineConfiguration cannot be located, the default
90     * AxisServer constructor is used.
91     * <p/>
92     * The AxisServer's option AxisEngine.PROP_ATTACHMENT_DIR is set to
93     * the (first found) value of either AxisEngine.ENV_ATTACHMENT_DIR
94     * or AxisEngine.ENV_SERVLET_REALPATH.
95     *
96     * @param environment The following keys are used:
97     * AxisEngine.ENV_ATTACHMENT_DIR
98     * - Set as default value for Axis option
99     * AxisEngine.PROP_ATTACHMENT_DIR
100     * AxisEngine.ENV_SERVLET_REALPATH
101     * - Set as alternate default value for Axis option
102     * AxisEngine.PROP_ATTACHMENT_DIR
103     * EngineConfiguration.PROPERTY_NAME
104     * - Instance of EngineConfiguration,
105     * if not set then an attempt is made to retreive
106     * a class name from AxisEngine.PROP_CONFIG_CLASS
107     */

108    public AxisServer getServer(Map JavaDoc environment) throws AxisFault
109    {
110       log.debug("Enter: DefaultAxisServerFactory::getServer");
111
112       AxisServer ret = createServer(environment);
113
114       if (ret != null)
115       {
116          if (environment != null)
117          {
118             ret.setOptionDefault(AxisEngine.PROP_ATTACHMENT_DIR,
119                     (String JavaDoc)environment.get(AxisEngine.ENV_ATTACHMENT_DIR));
120
121             ret.setOptionDefault(AxisEngine.PROP_ATTACHMENT_DIR,
122                     (String JavaDoc)environment.get(AxisEngine.ENV_SERVLET_REALPATH));
123          }
124
125          String JavaDoc attachmentsdir = (String JavaDoc)ret.getOption(AxisEngine.PROP_ATTACHMENT_DIR);
126
127          if (attachmentsdir != null)
128          {
129             File JavaDoc attdirFile = new File JavaDoc(attachmentsdir);
130             if (!attdirFile.isDirectory())
131             {
132                attdirFile.mkdirs();
133             }
134          }
135       }
136
137       log.debug("Exit: DefaultAxisServerFactory::getServer");
138
139       return ret;
140    }
141
142    /**
143     * Do the actual work of creating a new AxisServer, using the
144     * configuration, or using the default constructor if null is passed.
145     *
146     * @return a shiny new AxisServer, ready for use.
147     */

148    private static AxisServer createServer(Map JavaDoc environment)
149    {
150       EngineConfiguration config = getEngineConfiguration(environment);
151
152       // Return new AxisServer using the appropriate config
153
return (config == null) ? new AxisServer() : new AxisServer(config);
154    }
155
156    /**
157     * Look for EngineConfiguration, it is first of:
158     * a) EngineConfiguration instance, keyed to
159     * EngineConfiguration.PROPERTY_NAME in 'environment', or
160     * b) EngineConfiguration class name, keyed to
161     * AxisEngine.PROP_DEFAULT_CONFIG_CLASS in AxisProperties.
162     * Class is instantiated if found.
163     */

164    private static EngineConfiguration getEngineConfiguration(Map JavaDoc environment)
165    {
166       log.debug("Enter: DefaultAxisServerFactory::getEngineConfiguration");
167
168       EngineConfiguration config = null;
169
170       if (environment != null)
171       {
172          try
173          {
174             config = (EngineConfiguration)environment.get(EngineConfiguration.PROPERTY_NAME);
175          }
176          catch (ClassCastException JavaDoc e)
177          {
178             log.warn(Messages.getMessage("engineConfigWrongClass00"), e);
179             // Fall through
180
}
181       }
182
183       if (config == null)
184       {
185          // A default engine configuration class may be set in a system
186
// property. If so, try creating an engine configuration.
187
String JavaDoc configClass = AxisProperties.getProperty(AxisEngine.PROP_DEFAULT_CONFIG_CLASS);
188          if (configClass != null)
189          {
190             try
191             {
192                // Got one - so try to make it (which means it had better have
193
// a default constructor - may make it possible later to pass
194
// in some kind of environmental parameters...)
195
Class JavaDoc cls = ClassUtils.forName(configClass);
196                config = (EngineConfiguration)cls.newInstance();
197             }
198             catch (ClassNotFoundException JavaDoc e)
199             {
200                log.warn(Messages.getMessage("engineConfigNoClass00", configClass), e);
201                // Fall through
202
}
203             catch (InstantiationException JavaDoc e)
204             {
205                log.warn(Messages.getMessage("engineConfigNoInstance00", configClass), e);
206                // Fall through
207
}
208             catch (IllegalAccessException JavaDoc e)
209             {
210                log.warn(Messages.getMessage("engineConfigIllegalAccess00", configClass), e);
211                // Fall through
212
}
213             catch (ClassCastException JavaDoc e)
214             {
215                log.warn(Messages.getMessage("engineConfigWrongClass01", configClass), e);
216                // Fall through
217
}
218          }
219       }
220
221       log.debug("Exit: DefaultAxisServerFactory::getEngineConfiguration");
222
223       return config;
224    }
225 }
226
Popular Tags