1 7 8 package java.beans; 9 10 import java.applet.*; 11 12 import java.awt.*; 13 14 import java.beans.AppletInitializer ; 15 16 import java.beans.beancontext.BeanContext ; 17 18 import java.io.*; 19 20 import java.lang.reflect.Constructor ; 21 22 import java.net.URL ; 23 import java.lang.reflect.Array ; 24 25 28 29 public class Beans { 30 31 46 47 public static Object instantiate(ClassLoader cls, String beanName) throws java.io.IOException , ClassNotFoundException { 48 return Beans.instantiate(cls, beanName, null, null); 49 } 50 51 67 68 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws java.io.IOException , ClassNotFoundException { 69 return Beans.instantiate(cls, beanName, beanContext, null); 70 } 71 72 122 123 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer) 124 throws java.io.IOException , ClassNotFoundException { 125 126 java.io.InputStream ins; 127 java.io.ObjectInputStream oins = null; 128 Object result = null; 129 boolean serialized = false; 130 java.io.IOException serex = null; 131 132 if (cls == null) { 138 try { 139 cls = ClassLoader.getSystemClassLoader(); 140 } catch (SecurityException ex) { 141 } 144 } 145 146 final String serName = beanName.replace('.','/').concat(".ser"); 148 final ClassLoader loader = cls; 149 ins = (InputStream)java.security.AccessController.doPrivileged 150 (new java.security.PrivilegedAction () { 151 public Object run() { 152 if (loader == null) 153 return ClassLoader.getSystemResourceAsStream(serName); 154 else 155 return loader.getResourceAsStream(serName); 156 } 157 }); 158 if (ins != null) { 159 try { 160 if (cls == null) { 161 oins = new ObjectInputStream(ins); 162 } else { 163 oins = new ObjectInputStreamWithLoader(ins, cls); 164 } 165 result = oins.readObject(); 166 serialized = true; 167 oins.close(); 168 } catch (java.io.IOException ex) { 169 ins.close(); 170 serex = ex; 173 } catch (ClassNotFoundException ex) { 174 ins.close(); 175 throw ex; 176 } 177 } 178 179 if (result == null) { 180 Class cl; 182 183 try { 184 if (cls == null) { 185 cl = Class.forName(beanName); 186 } else { 187 cl = cls.loadClass(beanName); 188 } 189 } catch (ClassNotFoundException ex) { 190 if (serex != null) { 194 throw serex; 195 } 196 throw ex; 197 } 198 199 202 203 try { 204 result = cl.newInstance(); 205 } catch (Exception ex) { 206 throw new ClassNotFoundException ("" + cl + " : " + ex, ex); 209 } 210 } 211 212 if (result != null) { 213 214 216 AppletStub stub = null; 217 218 if (result instanceof Applet) { 219 Applet applet = (Applet) result; 220 boolean needDummies = initializer == null; 221 222 if (needDummies) { 223 224 228 233 final String resourceName; 234 235 if (serialized) { 236 resourceName = beanName.replace('.','/').concat(".ser"); 238 } else { 239 resourceName = beanName.replace('.','/').concat(".class"); 241 } 242 243 URL objectUrl = null; 244 URL codeBase = null; 245 URL docBase = null; 246 247 249 final ClassLoader cloader = cls; 250 objectUrl = (URL ) 251 java.security.AccessController.doPrivileged 252 (new java.security.PrivilegedAction () { 253 public Object run() { 254 if (cloader == null) 255 return ClassLoader.getSystemResource 256 (resourceName); 257 else 258 return cloader.getResource(resourceName); 259 } 260 }); 261 262 270 if (objectUrl != null) { 271 String s = objectUrl.toExternalForm(); 272 273 if (s.endsWith(resourceName)) { 274 int ix = s.length() - resourceName.length(); 275 codeBase = new URL (s.substring(0,ix)); 276 docBase = codeBase; 277 278 ix = s.lastIndexOf('/'); 279 280 if (ix >= 0) { 281 docBase = new URL (s.substring(0,ix+1)); 282 } 283 } 284 } 285 286 BeansAppletContext context = new BeansAppletContext(applet); 288 289 stub = (AppletStub)new BeansAppletStub(applet, context, codeBase, docBase); 290 applet.setStub(stub); 291 } else { 292 initializer.initialize(applet, beanContext); 293 } 294 295 297 if (beanContext != null) { 298 beanContext.add(result); 299 } 300 301 304 if (!serialized) { 305 applet.setSize(100,100); 309 applet.init(); 310 } 311 312 if (needDummies) { 313 ((BeansAppletStub)stub).active = true; 314 } else initializer.activate(applet); 315 316 } else if (beanContext != null) beanContext.add(result); 317 } 318 319 return result; 320 } 321 322 323 338 public static Object getInstanceOf(Object bean, Class <?> targetType) { 339 return bean; 340 } 341 342 353 public static boolean isInstanceOf(Object bean, Class <?> targetType) { 354 return Introspector.isSubclass(bean.getClass(), targetType); 355 } 356 357 358 366 public static boolean isDesignTime() { 367 return designTime; 368 } 369 370 383 public static boolean isGuiAvailable() { 384 return guiAvailable; 385 } 386 387 403 404 public static void setDesignTime(boolean isDesignTime) 405 throws SecurityException { 406 SecurityManager sm = System.getSecurityManager(); 407 if (sm != null) { 408 sm.checkPropertiesAccess(); 409 } 410 designTime = isDesignTime; 411 } 412 413 429 430 public static void setGuiAvailable(boolean isGuiAvailable) 431 throws SecurityException { 432 SecurityManager sm = System.getSecurityManager(); 433 if (sm != null) { 434 sm.checkPropertiesAccess(); 435 } 436 guiAvailable = isGuiAvailable; 437 } 438 439 440 private static boolean designTime; 441 private static boolean guiAvailable; 442 static { 443 guiAvailable = !GraphicsEnvironment.isHeadless(); 444 } 445 } 446 447 451 452 class ObjectInputStreamWithLoader extends ObjectInputStream 453 { 454 private ClassLoader loader; 455 456 459 460 public ObjectInputStreamWithLoader(InputStream in, ClassLoader loader) 461 throws IOException, StreamCorruptedException { 462 463 super(in); 464 if (loader == null) { 465 throw new IllegalArgumentException ("Illegal null argument to ObjectInputStreamWithLoader"); 466 } 467 this.loader = loader; 468 } 469 470 473 474 private Class primitiveType(char type) { 475 switch (type) { 476 case 'B': return byte.class; 477 case 'C': return char.class; 478 case 'D': return double.class; 479 case 'F': return float.class; 480 case 'I': return int.class; 481 case 'J': return long.class; 482 case 'S': return short.class; 483 case 'Z': return boolean.class; 484 default: return null; 485 } 486 } 487 488 491 protected Class resolveClass(ObjectStreamClass classDesc) 492 throws IOException, ClassNotFoundException { 493 494 String cname = classDesc.getName(); 495 if (cname.startsWith("[")) { 496 Class component; int dcount; for (dcount=1; cname.charAt(dcount)=='['; dcount++) ; 500 if (cname.charAt(dcount) == 'L') { 501 component = loader.loadClass(cname.substring(dcount+1, 502 cname.length()-1)); 503 } else { 504 if (cname.length() != dcount+1) { 505 throw new ClassNotFoundException (cname); } 507 component = primitiveType(cname.charAt(dcount)); 508 } 509 int dim[] = new int[dcount]; 510 for (int i=0; i<dcount; i++) { 511 dim[i]=0; 512 } 513 return Array.newInstance(component, dim).getClass(); 514 } else { 515 return loader.loadClass(cname); 516 } 517 } 518 } 519 520 524 525 class BeansAppletContext implements AppletContext { 526 Applet target; 527 java.util.Hashtable imageCache = new java.util.Hashtable (); 528 529 BeansAppletContext(Applet target) { 530 this.target = target; 531 } 532 533 public AudioClip getAudioClip(URL url) { 534 try { 538 return (AudioClip) url.getContent(); 539 } catch (Exception ex) { 540 return null; 541 } 542 } 543 544 public synchronized Image getImage(URL url) { 545 Object o = imageCache.get(url); 546 if (o != null) { 547 return (Image)o; 548 } 549 try { 550 o = url.getContent(); 551 if (o == null) { 552 return null; 553 } 554 if (o instanceof Image) { 555 imageCache.put(url, o); 556 return (Image) o; 557 } 558 Image img = target.createImage((java.awt.image.ImageProducer )o); 560 imageCache.put(url, img); 561 return img; 562 563 } catch (Exception ex) { 564 return null; 565 } 566 } 567 568 public Applet getApplet(String name) { 569 return null; 570 } 571 572 public java.util.Enumeration getApplets() { 573 java.util.Vector applets = new java.util.Vector (); 574 applets.addElement(target); 575 return applets.elements(); 576 } 577 578 public void showDocument(URL url) { 579 } 581 582 public void showDocument(URL url, String target) { 583 } 585 586 public void showStatus(String status) { 587 } 589 590 public void setStream(String key, InputStream stream)throws IOException{ 591 } 593 594 public InputStream getStream(String key){ 595 return null; 597 } 598 599 public java.util.Iterator getStreamKeys(){ 600 return null; 602 } 603 } 604 605 609 class BeansAppletStub implements AppletStub { 610 transient boolean active; 611 transient Applet target; 612 transient AppletContext context; 613 transient URL codeBase; 614 transient URL docBase; 615 616 BeansAppletStub(Applet target, 617 AppletContext context, URL codeBase, 618 URL docBase) { 619 this.target = target; 620 this.context = context; 621 this.codeBase = codeBase; 622 this.docBase = docBase; 623 } 624 625 public boolean isActive() { 626 return active; 627 } 628 629 public URL getDocumentBase() { 630 return docBase; 632 } 633 634 public URL getCodeBase() { 635 return codeBase; 637 } 638 639 public String getParameter(String name) { 640 return null; 641 } 642 643 public AppletContext getAppletContext() { 644 return context; 645 } 646 647 public void appletResize(int width, int height) { 648 } 650 } 651 | Popular Tags |