1 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 ; 54 import java.io.FileInputStream ; 55 import java.io.FileNotFoundException ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.io.InputStreamReader ; 59 import java.io.Reader ; 60 import java.net.MalformedURLException ; 61 import java.net.URL ; 62 import java.net.UnknownHostException ; 63 import java.util.jar.JarEntry ; 64 import java.util.jar.JarFile ; 65 66 69 public class Unmarshaller { 70 71 private final Class clazz; 72 private final File xmlFile; 73 74 private static DTDResolver resolver = new DTDResolver(); 75 76 public Unmarshaller(Class type, String xmlFileName) { 77 this.clazz = type; 78 this.xmlFile = new File (xmlFileName); 79 } 80 81 public static Object unmarshal(Class clazz, String xmlFile, String jarLocation) throws OpenEJBException { 82 return new Unmarshaller(clazz, xmlFile).unmarshal(jarLocation); 83 } 84 85 public Object unmarshal(String location) throws OpenEJBException { 86 File file = new File (location); 87 if (file.isDirectory()){ 88 return unmarshalFromDirectory(file); 89 } else { 90 return unmarshalFromJar(file); 91 } 92 } 93 94 public Object unmarshalFromJar(File jarFile) throws OpenEJBException { 95 String jarLocation = jarFile.getPath(); 96 String file = xmlFile.getName(); 97 98 JarFile jar = JarUtils.getJarFile(jarLocation); 99 JarEntry entry = jar.getJarEntry(xmlFile.getPath().replaceAll("\\\\", "/")); 100 101 if (entry == null) throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotFindFile", file, jarLocation)); 102 103 Reader reader = null; 104 InputStream stream = null; 105 106 try { 107 stream = jar.getInputStream(entry); 108 reader = new InputStreamReader (stream); 109 return unmarshalObject(reader, file, jarLocation); 110 } catch (IOException 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 e) { 118 throw new OpenEJBException(EjbJarUtils.messages.format("file.0020", jarLocation, e.getLocalizedMessage())); 119 } 120 } 121 } 122 123 public Object unmarshalFromDirectory(File directory) throws OpenEJBException { 124 String file = xmlFile.getName(); 125 126 Reader reader = null; 127 InputStream stream = null; 128 129 try { 130 File fullPath = new File (directory, xmlFile.getPath() ); 131 stream = new FileInputStream (fullPath); 132 reader = new InputStreamReader (stream); 133 return unmarshalObject(reader, file, directory.getPath()); 134 } catch (FileNotFoundException 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 e) { 141 throw new OpenEJBException(EjbJarUtils.messages.format("file.0020", directory.getPath(), e.getLocalizedMessage())); 142 } 143 } 144 } 145 146 public Object unmarshal(URL url) throws OpenEJBException { 147 String file = xmlFile.getName(); 148 149 Reader reader = null; 150 InputStream stream = null; 151 152 try { 153 URL fullURL = new URL (url, xmlFile.getPath()); 154 stream = fullURL.openConnection().getInputStream(); 155 reader = new InputStreamReader (stream); 156 return unmarshalObject(reader, file, fullURL.getPath()); 157 } catch (MalformedURLException e) { 158 throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotFindFile", file, url.getPath())); 159 } catch (IOException 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 e) { 166 throw new OpenEJBException(EjbJarUtils.messages.format("file.0020", url.getPath(), e.getLocalizedMessage())); 167 } 168 } 169 } 170 171 private Object unmarshalObject(Reader reader, String file, String 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 ) { 179 throw new OpenEJBException(EjbJarUtils.messages.format("xml.unkownHost", file, jarLocation, e.getLocalizedMessage())); 180 } else if (e.getException() instanceof org.xml.sax.SAXException ) { 181 throw new OpenEJBException(EjbJarUtils.messages.format("xml.cannotParse", file, jarLocation, e.getLocalizedMessage())); 182 } else if (e.getException() instanceof IOException ) { 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 |