1 18 19 package sync4j.framework.tools.beans; 20 21 import java.beans.XMLEncoder ; 22 import java.beans.XMLDecoder ; 23 24 import java.io.*; 25 26 import sync4j.framework.tools.beans.LazyInitBean; 27 import sync4j.framework.tools.beans.BeanException; 28 import sync4j.framework.tools.beans.BeanInstantiationException; 29 import sync4j.framework.tools.beans.BeanInitializationException; 30 31 32 33 40 public class BeanFactory { 41 42 protected BeanFactory() { } 43 44 60 public static Object getNoInitBeanInstance(ClassLoader classLoader, String beanName) 61 throws BeanInstantiationException, 62 BeanInitializationException, 63 BeanNotFoundException { 64 65 if (classLoader == null) { 66 classLoader = ClassLoader.getSystemClassLoader(); 67 } 68 69 Object bean = null; 70 71 try { 75 bean = Class.forName(beanName, true, classLoader).newInstance(); 76 if (bean instanceof LazyInitBean) { 77 ((LazyInitBean)bean).init(); 78 } 79 return bean; 80 } catch (ClassNotFoundException e) { 81 } catch (BeanInitializationException e) { 85 throw e; 86 } catch (Exception e) { 87 throw new BeanInstantiationException( "Error instantiating " 91 + beanName 92 , e 93 ); 94 } 95 InputStream is = classLoader.getResourceAsStream(beanName); 96 97 if (is == null) { 98 throw new BeanNotFoundException( beanName, classLoader); 99 } 100 101 BeanExceptionListener exceptionListener = new BeanExceptionListener(); 102 103 synchronized (BeanFactory.class) { 112 XMLDecoder d = new XMLDecoder (is, null, exceptionListener); 113 bean = d.readObject(); 114 d.close(); 115 } 116 117 if (exceptionListener.exceptionThrown()) { 118 Throwable t = exceptionListener.getException(); 119 120 if (t.getCause() != null) { 121 t = t.getCause(); 122 } 123 throw new BeanInstantiationException( "Error instantiating " 124 + beanName 125 , t 126 ); 127 } 128 129 return bean; 130 } 131 132 147 public static Object getBeanInstance(ClassLoader classLoader, String beanName) 148 throws BeanInstantiationException, 149 BeanInitializationException, 150 BeanNotFoundException { 151 152 Object bean = null; 153 154 bean = getNoInitBeanInstance(classLoader, beanName); 155 156 if (bean instanceof LazyInitBean) { 157 ((LazyInitBean)bean).init(); 158 } 159 160 return bean; 161 } 162 163 164 176 public static Object getBeanInstance(String beanName) 177 throws BeanInstantiationException, 178 BeanInitializationException, 179 BeanNotFoundException { 180 return getBeanInstance(null, beanName); 181 } 182 183 184 196 public static Object getBeanInstance(File beanFile) 197 throws BeanInstantiationException, 198 BeanInitializationException, 199 BeanNotFoundException { 200 try { 201 Object bean = null; 202 203 XMLDecoder e = new XMLDecoder (new FileInputStream(beanFile)); 204 bean = e.readObject(); 205 e.close(); 206 207 if (bean instanceof LazyInitBean) { 208 ((LazyInitBean)bean).init(); 209 } 210 return bean; 211 } catch (FileNotFoundException e) { 212 throw new BeanNotFoundException(beanFile.getName(), (ClassLoader )null); 213 } catch (Exception e) { 214 String msg = "Bean creation (" 215 + beanFile 216 + ") failed: " 217 + e.getMessage() 218 ; 219 throw new BeanInstantiationException(msg, e); 220 } 221 } 222 223 231 public static void saveBeanInstance(Object obj, File file) 232 throws BeanException { 233 XMLEncoder encoder = null; 234 235 try { 236 encoder = new XMLEncoder (new FileOutputStream(file)); 237 encoder.writeObject(obj); 238 } catch (IOException e) { 239 String msg = "Bean saving (" + file + ") failed: " + e.getMessage(); 240 throw new BeanException(msg, e); 241 } finally { 242 if (encoder != null) { 243 encoder.close(); 244 } 245 } 246 } 247 248 257 public static String marshal(ClassLoader classLoader, Object o) 258 throws BeanException { 259 XMLEncoder enc = null; 260 ClassLoader oldClassLoader = null; 261 262 if (classLoader != null) { 263 oldClassLoader = Thread.currentThread().getContextClassLoader(); 264 Thread.currentThread().setContextClassLoader(classLoader); 265 } 266 267 BeanExceptionListener exceptionListener = new BeanExceptionListener(); 268 269 try { 270 ByteArrayOutputStream os = new ByteArrayOutputStream(); 271 272 enc = new XMLEncoder (os); 273 274 enc.writeObject(o); 275 276 enc.close(); enc = null; 278 if (exceptionListener.exceptionThrown()) { 279 Throwable t = exceptionListener.getException(); 280 throw new BeanException(t.getMessage(), t); 281 } 282 283 return os.toString(); 284 } finally { 285 if (enc != null) { 286 enc.close(); 287 } 288 289 if (classLoader != null) { 290 Thread.currentThread().setContextClassLoader(oldClassLoader); 291 } 292 } 293 } 294 295 304 public static String marshal(Object o) 305 throws BeanException { 306 return marshal(null, o); 307 } 308 309 310 321 public static Object unmarshal(ClassLoader classLoader, String xml) 322 throws BeanException { 323 XMLDecoder dec = null; 324 ClassLoader oldClassLoader = null; 325 326 if (classLoader != null) { 327 oldClassLoader = Thread.currentThread().getContextClassLoader(); 328 Thread.currentThread().setContextClassLoader(classLoader); 329 } 330 331 BeanExceptionListener exceptionListener = new BeanExceptionListener(); 332 333 try { 334 ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes()); 335 336 dec = new XMLDecoder (is); 337 338 Object ret = dec.readObject(); 339 340 dec.close(); dec = null; 341 342 if (exceptionListener.exceptionThrown()) { 343 Throwable t = exceptionListener.getException(); 344 throw new BeanException(t.getMessage(), t); 345 } 346 347 return ret; 348 } finally { 349 if (dec != null) { 350 dec.close(); 351 } 352 353 if (classLoader != null) { 354 Thread.currentThread().setContextClassLoader(oldClassLoader); 355 } 356 } 357 } 358 359 368 public static Object unmarshal(String xml) 369 throws BeanException { 370 return unmarshal(null, xml); 371 } 372 373 380 public static void main(String [] args) throws Exception { 381 if (args.length != 2) { 382 System.out.println("Syntax: sync4j.framework.tools.beans.BeanFactory <class name> <file name>"); 383 return; 384 } 385 386 saveBeanInstance(getBeanInstance(args[0]), new File(args[1])); 387 } 388 } 389 | Popular Tags |