KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: Unmarshaller.java 2160 2005-09-16 23:36:15Z jcscoobyrs $
44  */

45
46 package org.openejb.alt.config;
47
48 import org.exolab.castor.xml.MarshalException;
49 import org.exolab.castor.xml.ValidationException;
50 import org.openejb.OpenEJBException;
51 import org.openejb.util.JarUtils;
52
53 import java.io.File JavaDoc;
54 import java.io.FileInputStream JavaDoc;
55 import java.io.FileNotFoundException JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58 import java.io.InputStreamReader JavaDoc;
59 import java.io.Reader JavaDoc;
60 import java.net.MalformedURLException JavaDoc;
61 import java.net.URL JavaDoc;
62 import java.net.UnknownHostException JavaDoc;
63 import java.util.jar.JarEntry JavaDoc;
64 import java.util.jar.JarFile JavaDoc;
65
66 /**
67  * @version $Revision: 2160 $ $Date: 2005-09-16 16:36:15 -0700 (Fri, 16 Sep 2005) $
68  */

69 public class Unmarshaller {
70
71     private final Class JavaDoc clazz;
72     private final File JavaDoc xmlFile;
73
74     private static DTDResolver resolver = new DTDResolver();
75
76     public Unmarshaller(Class JavaDoc type, String JavaDoc xmlFileName) {
77         this.clazz = type;
78         this.xmlFile = new File JavaDoc(xmlFileName);
79     }
80
81     public static Object JavaDoc unmarshal(Class JavaDoc clazz, String JavaDoc xmlFile, String JavaDoc jarLocation) throws OpenEJBException {
82         return new Unmarshaller(clazz, xmlFile).unmarshal(jarLocation);
83     }
84
85     public Object JavaDoc unmarshal(String JavaDoc location) throws OpenEJBException {
86         File JavaDoc file = new File JavaDoc(location);
87         if (file.isDirectory()){
88             return unmarshalFromDirectory(file);
89         } else {
90             return unmarshalFromJar(file);
91         }
92     }
93
94     public Object JavaDoc unmarshalFromJar(File JavaDoc jarFile) throws OpenEJBException {
95         String JavaDoc jarLocation = jarFile.getPath();
96         String JavaDoc file = xmlFile.getName();
97
98         JarFile JavaDoc jar = JarUtils.getJarFile(jarLocation);
99         JarEntry JavaDoc entry = jar.getJarEntry(xmlFile.getPath().replaceAll("\\\\", "/"));
100
101         if (entry == null) throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotFindFile", file, jarLocation));
102
103         Reader JavaDoc reader = null;
104         InputStream JavaDoc stream = null;
105
106         try {
107             stream = jar.getInputStream(entry);
108             reader = new InputStreamReader JavaDoc(stream);
109             return unmarshalObject(reader, file, jarLocation);
110         } catch (IOException JavaDoc e) {
111             throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotRead", file, jarLocation, e.getLocalizedMessage()));
112         } finally {
113             try {
114                 if (stream != null) stream.close();
115                 if (reader != null) reader.close();
116                 if (jar != null) jar.close();
117             } catch (Exception JavaDoc e) {
118                 throw new OpenEJBException(EjbJarUtils.messages.format("file.0020", jarLocation, e.getLocalizedMessage()));
119             }
120         }
121     }
122
123     public Object JavaDoc unmarshalFromDirectory(File JavaDoc directory) throws OpenEJBException {
124         String JavaDoc file = xmlFile.getName();
125
126         Reader JavaDoc reader = null;
127         InputStream JavaDoc stream = null;
128
129         try {
130             File JavaDoc fullPath = new File JavaDoc(directory, xmlFile.getPath() );
131             stream = new FileInputStream JavaDoc(fullPath);
132             reader = new InputStreamReader JavaDoc(stream);
133             return unmarshalObject(reader, file, directory.getPath());
134         } catch (FileNotFoundException JavaDoc e) {
135             throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotFindFile", file, directory.getPath()));
136         } finally {
137             try {
138                 if (stream != null) stream.close();
139                 if (reader != null) reader.close();
140             } catch (Exception JavaDoc e) {
141                 throw new OpenEJBException(EjbJarUtils.messages.format("file.0020", directory.getPath(), e.getLocalizedMessage()));
142             }
143         }
144     }
145
146     public Object JavaDoc unmarshal(URL JavaDoc url) throws OpenEJBException {
147         String JavaDoc file = xmlFile.getName();
148
149         Reader JavaDoc reader = null;
150         InputStream JavaDoc stream = null;
151
152         try {
153             URL JavaDoc fullURL = new URL JavaDoc(url, xmlFile.getPath());
154             stream = fullURL.openConnection().getInputStream();
155             reader = new InputStreamReader JavaDoc(stream);
156             return unmarshalObject(reader, file, fullURL.getPath());
157         } catch (MalformedURLException JavaDoc e) {
158             throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotFindFile", file, url.getPath()));
159         } catch (IOException JavaDoc e) {
160             throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotRead", file, url.getPath(), e.getLocalizedMessage()));
161         } finally {
162             try {
163                 if (stream != null) stream.close();
164                 if (reader != null) reader.close();
165             } catch (Exception JavaDoc e) {
166                 throw new OpenEJBException(EjbJarUtils.messages.format("file.0020", url.getPath(), e.getLocalizedMessage()));
167             }
168         }
169     }
170
171     private Object JavaDoc unmarshalObject(Reader JavaDoc reader, String JavaDoc file, String JavaDoc jarLocation) throws OpenEJBException {
172         try {
173             org.exolab.castor.xml.Unmarshaller unmarshaller = new org.exolab.castor.xml.Unmarshaller(clazz);
174             unmarshaller.setWhitespacePreserve(true);
175             unmarshaller.setEntityResolver(resolver);
176             return unmarshaller.unmarshal(reader);
177         } catch (MarshalException e) {
178             if (e.getException() instanceof UnknownHostException JavaDoc) {
179                 throw new OpenEJBException(EjbJarUtils.messages.format("xml.unkownHost", file, jarLocation, e.getLocalizedMessage()));
180             } else if (e.getException() instanceof org.xml.sax.SAXException JavaDoc) {
181                 throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotParse", file, jarLocation, e.getLocalizedMessage()));
182             } else if (e.getException() instanceof IOException JavaDoc) {
183                 throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotRead", file, jarLocation, e.getLocalizedMessage()));
184             } else if (e.getException() instanceof ValidationException){
185                 throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotValidate", file, jarLocation, e.getLocalizedMessage()));
186             } else {
187                 e.printStackTrace();
188                 throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotUnmarshal", file, jarLocation, e.getLocalizedMessage()));
189             }
190         } catch (ValidationException e) {
191             throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotValidate", file, jarLocation, e.getLocalizedMessage()));
192         }
193     }
194 }
195
Popular Tags