KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > config > ServiceUtils


1 /**
2
3  * Redistribution and use of this software and associated documentation
4
5  * ("Software"), with or without modification, are permitted provided
6
7  * that the following conditions are met:
8
9  *
10
11  * 1. Redistributions of source code must retain copyright
12
13  * statements and notices. Redistributions must also contain a
14
15  * copy of this document.
16
17  *
18
19  * 2. Redistributions in binary form must reproduce the
20
21  * above copyright notice, this list of conditions and the
22
23  * following disclaimer in the documentation and/or other
24
25  * materials provided with the distribution.
26
27  *
28
29  * 3. The name "OpenEJB" must not be used to endorse or promote
30
31  * products derived from this Software without prior written
32
33  * permission of The OpenEJB Group. For written permission,
34
35  * please contact dev@openejb.org.
36
37  *
38
39  * 4. Products derived from this Software may not be called "OpenEJB"
40
41  * nor may "OpenEJB" appear in their names without prior written
42
43  * permission of The OpenEJB Group. OpenEJB is a registered
44
45  * trademark of The OpenEJB Group.
46
47  *
48
49  * 5. Due credit should be given to the OpenEJB Project
50
51  * (http://www.openejb.org/).
52
53  *
54
55  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
56
57  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
58
59  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
60
61  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
62
63  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64
65  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66
67  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
68
69  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
72
73  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74
75  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
76
77  * OF THE POSSIBILITY OF SUCH DAMAGE.
78
79  *
80
81  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
82
83  *
84
85  * $Id: ServiceUtils.java 2063 2005-08-06 16:55:19Z dblevins $
86
87  */

88
89 package org.openejb.alt.config;
90
91 import org.exolab.castor.xml.MarshalException;
92 import org.exolab.castor.xml.ValidationException;
93 import org.openejb.OpenEJBException;
94 import org.openejb.alt.config.sys.ServiceProvider;
95 import org.openejb.alt.config.sys.ServicesJar;
96 import org.openejb.util.Logger;
97 import org.openejb.util.Messages;
98
99 import java.io.*;
100 import java.net.MalformedURLException JavaDoc;
101 import java.net.URL JavaDoc;
102 import java.util.HashMap JavaDoc;
103 import java.util.Map JavaDoc;
104 import java.util.Properties JavaDoc;
105
106 /**
107  * Utility methods for reading, writing, and configuring services
108  *
109  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
110  */

111 public class ServiceUtils {
112
113     public static final String JavaDoc defaultProviderURL = "org.openejb";
114     private static Map JavaDoc loadedServiceJars = new HashMap JavaDoc();
115     public static Messages messages = new Messages("org.openejb.util.resources");
116     public static Logger logger = Logger.getInstance("OpenEJB", "org.openejb.util.resources");
117
118
119     public static ServiceProvider getServiceProvider(Service service) throws OpenEJBException {
120         return getServiceProvider(service.getProvider());
121     }
122
123     /**
124      * org.openejb#Default JDBC Connector
125      * <p/>
126      * Default JDBC Connector
127      * <p/>
128      * org.postgresql#JDBCService
129      *
130      * @param id
131      * @return ServiceProvider
132      * @throws OpenEJBException
133      */

134     public static ServiceProvider getServiceProvider(String JavaDoc id) throws OpenEJBException {
135
136         String JavaDoc providerName = null;
137         String JavaDoc serviceName = null;
138
139         if (id.indexOf("#") == -1) {
140             providerName = defaultProviderURL;
141             serviceName = id;
142         } else {
143             providerName = id.substring(0, id.indexOf("#"));
144             serviceName = id.substring(id.indexOf("#") + 1);
145         }
146
147         ServiceProvider service = null;
148
149         if (loadedServiceJars.get(providerName) == null) {
150             ServicesJar sj = readServicesJar(providerName);
151             ServiceProvider[] sp = sj.getServiceProvider();
152             HashMap JavaDoc services = new HashMap JavaDoc(sj.getServiceProviderCount());
153
154             for (int i = 0; i < sp.length; i++) {
155                 services.put(sp[i].getId(), sp[i]);
156             }
157
158             loadedServiceJars.put(providerName, services);
159             // This may return null if there is no service
160
// with the specified name.
161
service = (ServiceProvider) services.get(serviceName);
162         } else {
163             Map JavaDoc provider = (Map JavaDoc) loadedServiceJars.get(providerName);
164             service = (ServiceProvider) provider.get(serviceName);
165         }
166
167         if (service == null) {
168             throw new OpenEJBException(messages.format("conf.4901", serviceName, providerName));
169         }
170
171         return service;
172     }
173
174     /**
175      * Opens the specified jar file, locates the service-jar.xml file,
176      * <p/>
177      * unmarshals it to a java object and returns it. If there is no
178      * <p/>
179      * service-jar.xml in the jar an exception will be thrown.
180      *
181      * @param providerName
182      * @return ServicesJar
183      * @throws OpenEJBException
184      */

185     public static ServicesJar readServicesJar(String JavaDoc providerName) throws OpenEJBException {
186         try {
187             Unmarshaller unmarshaller = new Unmarshaller(ServicesJar.class, "service-jar.xml");
188             URL JavaDoc serviceURL = new URL JavaDoc("resource:/" + providerName.replace('.', '/')+"/");
189             return (ServicesJar) unmarshaller.unmarshal(serviceURL);
190         } catch (MalformedURLException JavaDoc e) {
191             throw new OpenEJBException(e);
192         }
193     }
194
195     public static void writeServicesJar(String JavaDoc xmlFile, ServicesJar servicesJarObject) throws OpenEJBException {
196
197         /* TODO: Just to be picky, the xml file created by
198         Castor is really hard to read -- it is all on one line.
199         People might want to edit this in the future by hand, so if Castor can
200         make the output look better that would be great! Otherwise we could
201         just spruce the output up by adding a few new lines and tabs.
202         */

203         Writer writer = null;
204
205         try {
206             File file = new File(xmlFile);
207             writer = new FileWriter(file);
208             servicesJarObject.marshal(writer);
209         } catch (IOException e) {
210             throw new OpenEJBException(messages.format("conf.4040", xmlFile, e.getLocalizedMessage()));
211         } catch (MarshalException e) {
212             if (e.getException() instanceof IOException) {
213                 throw new OpenEJBException(messages.format("conf.4040", xmlFile, e.getLocalizedMessage()));
214             } else {
215                 throw new OpenEJBException(messages.format("conf.4050", xmlFile, e.getLocalizedMessage()));
216             }
217         } catch (ValidationException e) {
218
219             /* TODO: Implement informative error handling here.
220                The exception will say "X doesn't match the regular
221                expression Y"
222                This should be checked and more relevant information
223                should be given -- not everyone understands regular
224                expressions.
225              */

226
227             /* NOTE: This doesn't seem to ever happen. When the object graph
228              * is invalid, the MarshalException is thrown, not this one as you
229              * would think.
230              */

231
232             throw new OpenEJBException(messages.format("conf.4060", xmlFile, e.getLocalizedMessage()));
233         }
234
235         try {
236             writer.close();
237         } catch (Exception JavaDoc e) {
238             throw new OpenEJBException(messages.format("file.0020", xmlFile, e.getLocalizedMessage()));
239         }
240     }
241
242     public static Properties JavaDoc assemblePropertiesFor(String JavaDoc confItem, String JavaDoc itemId, String JavaDoc itemContent,
243                                                    String JavaDoc confFile, ServiceProvider service) throws OpenEJBException {
244
245         Properties JavaDoc props = new Properties JavaDoc();
246
247         try {
248             /*
249              * 1. Load properties from the properties file referenced
250              * by the service provider
251              */

252             if (service.getPropertiesFile() != null) {
253                 props = loadProperties(service.getPropertiesFile().getFile());
254             }
255
256             /*
257              * 2. Load properties from the content in the service provider
258              * element of the service-jar.xml
259              */

260
261             if (service.getContent() != null) {
262                 StringBufferInputStream in = new StringBufferInputStream(service.getContent());
263                 props = loadProperties(in, props);
264             }
265         } catch (OpenEJBException ex) {
266             throw new OpenEJBException(messages.format("conf.0013", service.getId(), null, ex.getLocalizedMessage()));
267         }
268
269         /* 3. Load properties from the content in the Container
270          * element of the configuration file.
271          */

272         try {
273             if (itemContent != null) {
274                 StringBufferInputStream in = new StringBufferInputStream(itemContent);
275                 props = loadProperties(in, props);
276             }
277         } catch (OpenEJBException ex) {
278             throw new OpenEJBException(messages.format("conf.0014", confItem, itemId, confFile, ex.getLocalizedMessage()));
279         }
280
281         return props;
282     }
283
284     public static Properties JavaDoc loadProperties(String JavaDoc pFile) throws OpenEJBException {
285         return loadProperties(pFile, new Properties JavaDoc());
286     }
287
288     public static Properties JavaDoc loadProperties(String JavaDoc propertiesFile, Properties JavaDoc defaults) throws OpenEJBException {
289         try {
290             File pfile = new File(propertiesFile);
291             InputStream in = new FileInputStream(pfile);
292             return loadProperties(in, defaults);
293         } catch (FileNotFoundException ex) {
294             throw new OpenEJBException(messages.format("conf.0006", propertiesFile, ex.getLocalizedMessage()));
295         } catch (IOException ex) {
296             throw new OpenEJBException(messages.format("conf.0007", propertiesFile, ex.getLocalizedMessage()));
297         } catch (SecurityException JavaDoc ex) {
298             throw new OpenEJBException(messages.format("conf.0005", propertiesFile, ex.getLocalizedMessage()));
299         }
300     }
301
302     public static Properties JavaDoc loadProperties(InputStream in, Properties JavaDoc defaults)
303             throws OpenEJBException {
304
305         try {
306             /*
307             This may not work as expected. The desired effect is that
308             the load method will read in the properties and overwrite
309             the values of any properties that may have previously been
310             defined.
311             */

312             defaults.load(in);
313         } catch (IOException ex) {
314             throw new OpenEJBException(messages.format("conf.0012", ex.getLocalizedMessage()));
315         }
316
317         return defaults;
318     }
319
320 }
321
Popular Tags