KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > handler > WSDLHandlerFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial Developer : Sauthier Guillaume
22  * --------------------------------------------------------------------------
23  * $Id: WSDLHandlerFactory.java,v 1.4 2004/07/27 14:21:30 sauthieg Exp $
24  * --------------------------------------------------------------------------
25 */

26
27 package org.objectweb.jonas.ws.handler;
28
29 import java.util.Properties JavaDoc;
30
31 import java.lang.reflect.Constructor JavaDoc;
32
33 import java.io.InputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37
38 import org.objectweb.jonas.ws.WSServiceException;
39
40 import org.objectweb.util.monolog.api.BasicLevel;
41 import org.objectweb.util.monolog.api.Logger;
42
43 import org.objectweb.jonas.common.JProp;
44 import org.objectweb.jonas.common.Log;
45 import org.objectweb.jonas_lib.I18n;
46
47 /**
48  * WSDLHandlerFactory is used to dynamically instantiate a WSDLHandler
49  * from a Property file.<br/>
50  * Minimum properties in file : <br/>
51  * - <code>jonas.service.wsdl.class</code> : Fully qualified classname to be instanciated
52  *
53  * @author Guillaume Sauthier
54  */

55 public class WSDLHandlerFactory {
56
57     /** Classname property */
58     public static final String JavaDoc CLASSNAME = "jonas.service.wsdl.class";
59
60     /**
61      * Logger
62      */

63     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
64
65     /**
66      * I18n
67      */

68     private static I18n i18n = I18n.getInstance(WSDLHandlerFactory.class);
69
70     /**
71      * Construct a new WSDLHandlerFactory
72      */

73     private WSDLHandlerFactory() { }
74
75     /**
76      * @return Returns a new WSDLHandlerFactory instance.
77      */

78     public static WSDLHandlerFactory newInstance() {
79         return new WSDLHandlerFactory();
80     }
81
82     /**
83      * Create a new WSDLHandler.
84      *
85      * @param filename properties filename to be loaded
86      *
87      * @return a WSDLHandler init from properties
88      *
89      * @throws WSServiceException when filename is null or cannot be
90      * loaded (as ClassLoader Resource or as System file)
91      */

92     public WSDLHandler newHandler(String JavaDoc filename)
93         throws WSServiceException {
94
95         Properties JavaDoc props = loadProperties(filename + ".properties");
96
97         String JavaDoc classname = props.getProperty(CLASSNAME);
98
99         if (classname == null) {
100             // i18n WSDLHandlerFactory.newHandler.noClassname
101
String JavaDoc err = i18n.getMessage("WSDLHandlerFactory.newHandler.noClassname",
102                                          filename + ".properties");
103             logger.log(BasicLevel.ERROR, err);
104             throw new WSServiceException(err);
105         }
106
107         Object JavaDoc[] pvalues = new Object JavaDoc[] {props};
108         Class JavaDoc[] types = new Class JavaDoc[] {Properties JavaDoc.class};
109
110         // Instanciation
111
WSDLHandler handler = null;
112         try {
113             Class JavaDoc handlerClazz = Class.forName(classname);
114             Constructor JavaDoc handlerConstr = handlerClazz.getConstructor(types);
115             handler = (WSDLHandler) handlerConstr.newInstance(pvalues);
116         } catch (Exception JavaDoc e) {
117             // i18n WSDLHandlerFactory.newHandler.instantiationFailure
118
String JavaDoc err = i18n.getMessage("WSDLHandlerFactory.newHandler.instantiationFailure",
119                                          filename,
120                                          classname);
121             logger.log(BasicLevel.ERROR, err);
122             throw new WSServiceException(err, e);
123         }
124
125         return handler;
126     }
127
128     /**
129      * Load Properties from a properties file.
130      * Try to load the file first as a ClassLoader resource and
131      * as a File if not found in ClassLoader.
132      *
133      * @param filename filename to be loaded
134      *
135      * @return the Properties object loaded from filename
136      *
137      * @throws WSServiceException if properties cannot be loaded.
138      */

139     private Properties JavaDoc loadProperties(String JavaDoc filename)
140         throws WSServiceException {
141
142         Properties JavaDoc props = new Properties JavaDoc();
143
144         InputStream JavaDoc is = getFromClassLoader(filename);
145
146         if (is == null) {
147             // load from ${jonas.base}/conf
148
is = getFromFile(JProp.getJonasBase() + File.separator + "conf" + File.separator + filename);
149         }
150
151         if (is != null) {
152             try {
153                 props.load(is);
154             } catch (IOException JavaDoc ioe) {
155             // !!! i18n WSDLHandlerFactory.loadProperties.ioe
156
String JavaDoc err = i18n.getMessage("WSDLHandlerFactory.loadProperties.ioe",
157                                          filename);
158             logger.log(BasicLevel.ERROR, err);
159             throw new WSServiceException(err);
160             }
161         } else {
162             // !!! i18n WSDLHandlerFactory.loadProperties.notFound
163
String JavaDoc err = i18n.getMessage("WSDLHandlerFactory.loadProperties.notFound",
164                                          filename);
165             logger.log(BasicLevel.ERROR, err);
166             throw new WSServiceException(err);
167         }
168
169         return props;
170     }
171
172     /**
173      * Get the file as ClassLoader Resource (can be null if not found).
174      *
175      * @param filename the filename searched in the ClassLoader
176      *
177      * @return an InputStream from the ClassLoader
178      */

179     private InputStream JavaDoc getFromClassLoader(String JavaDoc filename) {
180
181         String JavaDoc clProps = filename;
182
183         // if filename do not start with a "/" prepend it.
184
// otherwise, getResourceAsStream will attemps to load it
185
// from directory of the current package.
186
if (!clProps.startsWith("/")) {
187             clProps = "/" + clProps;
188         }
189
190         return getClass().getResourceAsStream(clProps);
191     }
192
193     /**
194      * Get the file as File (can be null if not found).
195      *
196      * @param filename the filename searched in the System files.
197      *
198      * @return an InputStream from the File
199      */

200     private InputStream JavaDoc getFromFile(String JavaDoc filename) {
201
202         InputStream JavaDoc is = null;
203
204         // Create a file
205
File JavaDoc file = new File JavaDoc(filename);
206         try {
207             is = new FileInputStream JavaDoc(file);
208         } catch (IOException JavaDoc ioe) {
209             logger.log(BasicLevel.WARN, "Cannot load " + filename + " from file system.");
210             is = null;
211         }
212
213         return is;
214     }
215
216 }
217
Popular Tags