001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial Developer : Sauthier Guillaume
022: * --------------------------------------------------------------------------
023: * $Id: WSDLHandlerFactory.java 5188 2004-07-27 14:21:30Z sauthieg $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.ws.handler;
026:
027: import java.util.Properties;
028:
029: import java.lang.reflect.Constructor;
030:
031: import java.io.InputStream;
032: import java.io.IOException;
033: import java.io.File;
034: import java.io.FileInputStream;
035:
036: import org.objectweb.jonas.ws.WSServiceException;
037:
038: import org.objectweb.util.monolog.api.BasicLevel;
039: import org.objectweb.util.monolog.api.Logger;
040:
041: import org.objectweb.jonas.common.JProp;
042: import org.objectweb.jonas.common.Log;
043: import org.objectweb.jonas_lib.I18n;
044:
045: /**
046: * WSDLHandlerFactory is used to dynamically instantiate a WSDLHandler
047: * from a Property file.<br/>
048: * Minimum properties in file : <br/>
049: * - <code>jonas.service.wsdl.class</code> : Fully qualified classname to be instanciated
050: *
051: * @author Guillaume Sauthier
052: */
053: public class WSDLHandlerFactory {
054:
055: /** Classname property */
056: public static final String CLASSNAME = "jonas.service.wsdl.class";
057:
058: /**
059: * Logger
060: */
061: private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
062:
063: /**
064: * I18n
065: */
066: private static I18n i18n = I18n
067: .getInstance(WSDLHandlerFactory.class);
068:
069: /**
070: * Construct a new WSDLHandlerFactory
071: */
072: private WSDLHandlerFactory() {
073: }
074:
075: /**
076: * @return Returns a new WSDLHandlerFactory instance.
077: */
078: public static WSDLHandlerFactory newInstance() {
079: return new WSDLHandlerFactory();
080: }
081:
082: /**
083: * Create a new WSDLHandler.
084: *
085: * @param filename properties filename to be loaded
086: *
087: * @return a WSDLHandler init from properties
088: *
089: * @throws WSServiceException when filename is null or cannot be
090: * loaded (as ClassLoader Resource or as System file)
091: */
092: public WSDLHandler newHandler(String filename)
093: throws WSServiceException {
094:
095: Properties props = loadProperties(filename + ".properties");
096:
097: String classname = props.getProperty(CLASSNAME);
098:
099: if (classname == null) {
100: // i18n WSDLHandlerFactory.newHandler.noClassname
101: String err = i18n.getMessage(
102: "WSDLHandlerFactory.newHandler.noClassname",
103: filename + ".properties");
104: logger.log(BasicLevel.ERROR, err);
105: throw new WSServiceException(err);
106: }
107:
108: Object[] pvalues = new Object[] { props };
109: Class[] types = new Class[] { Properties.class };
110:
111: // Instanciation
112: WSDLHandler handler = null;
113: try {
114: Class handlerClazz = Class.forName(classname);
115: Constructor handlerConstr = handlerClazz
116: .getConstructor(types);
117: handler = (WSDLHandler) handlerConstr.newInstance(pvalues);
118: } catch (Exception e) {
119: // i18n WSDLHandlerFactory.newHandler.instantiationFailure
120: String err = i18n
121: .getMessage(
122: "WSDLHandlerFactory.newHandler.instantiationFailure",
123: filename, classname);
124: logger.log(BasicLevel.ERROR, err);
125: throw new WSServiceException(err, e);
126: }
127:
128: return handler;
129: }
130:
131: /**
132: * Load Properties from a properties file.
133: * Try to load the file first as a ClassLoader resource and
134: * as a File if not found in ClassLoader.
135: *
136: * @param filename filename to be loaded
137: *
138: * @return the Properties object loaded from filename
139: *
140: * @throws WSServiceException if properties cannot be loaded.
141: */
142: private Properties loadProperties(String filename)
143: throws WSServiceException {
144:
145: Properties props = new Properties();
146:
147: InputStream is = getFromClassLoader(filename);
148:
149: if (is == null) {
150: // load from ${jonas.base}/conf
151: is = getFromFile(JProp.getJonasBase() + File.separator
152: + "conf" + File.separator + filename);
153: }
154:
155: if (is != null) {
156: try {
157: props.load(is);
158: } catch (IOException ioe) {
159: // !!! i18n WSDLHandlerFactory.loadProperties.ioe
160: String err = i18n.getMessage(
161: "WSDLHandlerFactory.loadProperties.ioe",
162: filename);
163: logger.log(BasicLevel.ERROR, err);
164: throw new WSServiceException(err);
165: }
166: } else {
167: // !!! i18n WSDLHandlerFactory.loadProperties.notFound
168: String err = i18n.getMessage(
169: "WSDLHandlerFactory.loadProperties.notFound",
170: filename);
171: logger.log(BasicLevel.ERROR, err);
172: throw new WSServiceException(err);
173: }
174:
175: return props;
176: }
177:
178: /**
179: * Get the file as ClassLoader Resource (can be null if not found).
180: *
181: * @param filename the filename searched in the ClassLoader
182: *
183: * @return an InputStream from the ClassLoader
184: */
185: private InputStream getFromClassLoader(String filename) {
186:
187: String clProps = filename;
188:
189: // if filename do not start with a "/" prepend it.
190: // otherwise, getResourceAsStream will attemps to load it
191: // from directory of the current package.
192: if (!clProps.startsWith("/")) {
193: clProps = "/" + clProps;
194: }
195:
196: return getClass().getResourceAsStream(clProps);
197: }
198:
199: /**
200: * Get the file as File (can be null if not found).
201: *
202: * @param filename the filename searched in the System files.
203: *
204: * @return an InputStream from the File
205: */
206: private InputStream getFromFile(String filename) {
207:
208: InputStream is = null;
209:
210: // Create a file
211: File file = new File(filename);
212: try {
213: is = new FileInputStream(file);
214: } catch (IOException ioe) {
215: logger.log(BasicLevel.WARN, "Cannot load " + filename
216: + " from file system.");
217: is = null;
218: }
219:
220: return is;
221: }
222:
223: }
|