1 28 29 30 package org.jibx.binding; 31 32 import java.io.ByteArrayOutputStream ; 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.net.URLClassLoader ; 39 import java.util.ArrayList ; 40 import java.util.HashMap ; 41 42 import org.jibx.binding.classes.BoundClass; 43 import org.jibx.binding.classes.ClassCache; 44 import org.jibx.binding.classes.ClassFile; 45 import org.jibx.binding.classes.MungedClass; 46 import org.jibx.binding.def.BindingDefinition; 47 import org.jibx.runtime.JiBXException; 48 49 59 60 public class Loader extends URLClassLoader 61 { 62 63 private ArrayList m_bindings; 64 65 66 private boolean m_isBound; 67 68 69 private HashMap m_classMap; 70 71 78 public Loader(URL [] paths, ClassLoader parent) { 79 80 super(paths, parent); 82 m_bindings = new ArrayList (); 83 m_classMap = new HashMap (); 84 85 ArrayList fpaths = new ArrayList (paths.length); 87 for (int i = 0; i < paths.length; i++) { 88 URL path = paths[i]; 89 if ("file".equals(path.getProtocol())) { 90 fpaths.add(path.getPath()); 91 } 92 } 93 94 String [] dirs = (String [])fpaths.toArray(new String [0]); 96 ClassCache.setPaths(dirs); 97 ClassFile.setPaths(dirs); 98 99 BoundClass.reset(); 101 MungedClass.reset(); 102 BindingDefinition.reset(); 103 } 104 105 112 public Loader(URL [] paths) { 113 this(paths, ClassLoader.getSystemClassLoader().getParent()); 114 } 115 116 123 public Loader() throws MalformedURLException { 124 this(getClassPaths()); 125 } 126 127 133 public void reset() { 134 m_bindings.clear(); 135 m_classMap.clear(); 136 m_isBound = false; 137 BoundClass.reset(); 138 MungedClass.reset(); 139 BindingDefinition.reset(); 140 } 141 142 147 public static URL [] getClassPaths() throws MalformedURLException { 148 String [] paths = Utility.getClassPaths(); 149 URL [] urls = new URL [paths.length]; 150 for (int i = 0; i < urls.length; i++) { 151 urls[i] = new File (paths[i]).toURL(); 152 } 153 return urls; 154 } 155 156 172 public void loadBinding(String fname, String sname, InputStream is, URL url) 173 throws JiBXException, IOException { 174 175 if (m_isBound) { 177 throw new IllegalStateException 178 ("Call not allowed after bindings compiled"); 179 } else { 180 m_bindings.add(Utility.loadBinding(fname, sname, is, url, true)); 181 } 182 } 183 184 194 public void loadFileBinding(String path) throws JiBXException, IOException { 195 196 if (m_isBound) { 198 throw new IllegalStateException 199 ("Call not allowed after bindings compiled"); 200 } else { 201 m_bindings.add(Utility.loadFileBinding(path, true)); 202 } 203 } 204 205 215 public void loadResourceBinding(String path) 216 throws JiBXException, IOException { 217 218 if (m_isBound) { 220 throw new IllegalStateException 221 ("Call not allowed after bindings compiled"); 222 } else { 223 String fname = path; 224 int split = fname.lastIndexOf('/'); 225 if (split >= 0) { 226 fname = fname.substring(split+1); 227 } 228 String sname = fname; 229 split = sname.lastIndexOf('.'); 230 if (split >= 0) { 231 sname = sname.substring(0, split); 232 } 233 sname = Utility.convertName(sname); 234 InputStream is = getResourceAsStream(path); 235 if (is == null) { 236 throw new IOException ("Resource " + path + " not found"); 237 } else { 238 loadBinding(fname, sname, is, null); 239 } 240 } 241 } 242 243 249 public void processBindings() throws JiBXException { 250 if (!m_isBound) { 251 252 int count = m_bindings.size(); 254 for (int i = 0; i < count; i++) { 255 BindingDefinition binding = 256 (BindingDefinition)m_bindings.get(i); 257 binding.generateCode(false); 258 } 259 260 ClassFile[][] lists = MungedClass.fixChanges(false); 262 count = lists[0].length; 263 for (int i = 0; i < count; i++) { 264 ClassFile clas = lists[0][i]; 265 m_classMap.put(clas.getName(), clas); 266 } 267 268 m_isBound = true; 270 } 271 } 272 273 285 protected Class findClass(String name) throws ClassNotFoundException { 286 287 if (!m_isBound) { 289 try { 290 processBindings(); 291 } catch (JiBXException e) { 292 e.printStackTrace(); 293 } 294 } 295 296 ClassFile clas = (ClassFile)m_classMap.get(name); 298 if (clas != null) { 299 try { 300 301 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 303 clas.writeFile(bos); 304 byte[] bytes = bos.toByteArray(); 305 return defineClass(name, bytes, 0, bytes.length); 306 307 } catch (IOException e) { 308 throw new ClassNotFoundException 309 ("Unable to load modified class " + name); 310 } 311 } else { 312 313 return super.findClass(name); 315 316 } 317 } 318 } | Popular Tags |