1 21 22 27 28 package javax.activation; 29 30 import java.io.InputStream ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.io.PipedInputStream ; 34 import java.io.PipedOutputStream ; 35 import java.net.URL ; 36 import java.awt.datatransfer.Transferable ; 37 import java.awt.datatransfer.DataFlavor ; 38 import java.awt.datatransfer.UnsupportedFlavorException ; 39 40 75 76 public class DataHandler implements Transferable { 77 78 private DataSource dataSource = null; 81 private DataSource objDataSource = null; 82 83 private Object object = null; 87 private String objectMimeType = null; 88 89 private CommandMap currentCommandMap = null; 91 92 private static final DataFlavor emptyFlavors[] = new DataFlavor [0]; 94 private DataFlavor transferFlavors[] = emptyFlavors; 95 96 private DataContentHandler dataContentHandler = null; 98 private DataContentHandler factoryDCH = null; 99 100 private static DataContentHandlerFactory factory = null; 102 private DataContentHandlerFactory oldFactory = null; 103 private String shortType = null; 105 106 113 public DataHandler(DataSource ds) { 114 dataSource = ds; 116 oldFactory = factory; } 118 119 128 public DataHandler(Object obj, String mimeType) { 129 object = obj; 130 objectMimeType = mimeType; 131 oldFactory = factory; } 133 134 141 public DataHandler(URL url) { 142 dataSource = new URLDataSource (url); 143 oldFactory = factory; } 145 146 149 private synchronized CommandMap getCommandMap() { 150 if (currentCommandMap != null) 151 return currentCommandMap; 152 else 153 return CommandMap.getDefaultCommandMap(); 154 } 155 156 170 public DataSource getDataSource() { 171 if (dataSource == null) { 172 if (objDataSource == null) 174 objDataSource = new DataHandlerDataSource (this); 175 return objDataSource; 176 } 177 return dataSource; 178 } 179 180 188 public String getName() { 189 if (dataSource != null) 190 return dataSource.getName(); 191 else 192 return null; 193 } 194 195 202 public String getContentType() { 203 if (dataSource != null) return dataSource.getContentType(); 205 else 206 return objectMimeType; } 208 209 233 public InputStream getInputStream() throws IOException { 234 InputStream ins = null; 235 236 if (dataSource != null) { 237 ins = dataSource.getInputStream(); 238 } else { 239 DataContentHandler dch = getDataContentHandler(); 240 if (dch == null) 242 throw new UnsupportedDataTypeException ( 243 "no DCH for MIME type " + getBaseType()); 244 245 if (dch instanceof ObjectDataContentHandler ) { 246 if (((ObjectDataContentHandler )dch).getDCH() == null) 247 throw new UnsupportedDataTypeException ( 248 "no object DCH for MIME type " + getBaseType()); 249 } 250 final DataContentHandler fdch = dch; 252 253 final PipedOutputStream pos = new PipedOutputStream (); 260 PipedInputStream pin = new PipedInputStream (pos); 261 new Thread ( 262 new Runnable () { 263 public void run() { 264 try { 265 fdch.writeTo(object, objectMimeType, pos); 266 } catch (IOException e) { 267 268 } finally { 269 try { 270 pos.close(); 271 } catch (IOException ie) { } 272 } 273 } 274 }, 275 "DataHandler.getInputStream").start(); 276 ins = pin; 277 } 278 279 return ins; 280 } 281 282 297 public void writeTo(OutputStream os) throws IOException { 298 if (dataSource != null) { 300 InputStream is = null; 301 byte data[] = new byte[8*1024]; 302 int bytes_read; 303 304 is = dataSource.getInputStream(); 305 306 try { 307 while ((bytes_read = is.read(data)) > 0) { 308 os.write(data, 0, bytes_read); 309 } 310 } finally { 311 is.close(); 312 is = null; 313 } 314 } else { DataContentHandler dch = getDataContentHandler(); 316 dch.writeTo(object, objectMimeType, os); 317 } 318 } 319 320 332 public OutputStream getOutputStream() throws IOException { 333 if (dataSource != null) 334 return dataSource.getOutputStream(); 335 else 336 return null; 337 } 338 339 363 public synchronized DataFlavor [] getTransferDataFlavors() { 364 if (factory != oldFactory) transferFlavors = emptyFlavors; 366 367 if (transferFlavors == emptyFlavors) 369 transferFlavors = getDataContentHandler().getTransferDataFlavors(); 370 return transferFlavors; 371 } 372 373 385 public boolean isDataFlavorSupported(DataFlavor flavor) { 386 DataFlavor [] lFlavors = getTransferDataFlavors(); 387 388 for (int i = 0; i < lFlavors.length; i++) { 389 if (lFlavors[i].equals(flavor)) 390 return true; 391 } 392 return false; 393 } 394 395 428 public Object getTransferData(DataFlavor flavor) 429 throws UnsupportedFlavorException , IOException { 430 return getDataContentHandler().getTransferData(flavor, dataSource); 431 } 432 433 445 public synchronized void setCommandMap(CommandMap commandMap) { 446 if (commandMap != currentCommandMap || commandMap == null) { 447 transferFlavors = emptyFlavors; 449 dataContentHandler = null; 450 451 currentCommandMap = commandMap; 452 } 453 } 454 455 468 public CommandInfo [] getPreferredCommands() { 469 if (dataSource != null) 470 return getCommandMap().getPreferredCommands(getBaseType(), 471 dataSource); 472 else 473 return getCommandMap().getPreferredCommands(getBaseType()); 474 } 475 476 488 public CommandInfo [] getAllCommands() { 489 if (dataSource != null) 490 return getCommandMap().getAllCommands(getBaseType(), dataSource); 491 else 492 return getCommandMap().getAllCommands(getBaseType()); 493 } 494 495 507 public CommandInfo getCommand(String cmdName) { 508 if (dataSource != null) 509 return getCommandMap().getCommand(getBaseType(), cmdName, 510 dataSource); 511 else 512 return getCommandMap().getCommand(getBaseType(), cmdName); 513 } 514 515 532 public Object getContent() throws IOException { 533 if (object != null) 534 return object; 535 else 536 return getDataContentHandler().getContent(getDataSource()); 537 } 538 539 551 public Object getBean(CommandInfo cmdinfo) { 552 Object bean = null; 553 554 try { 555 ClassLoader cld = null; 557 cld = SecuritySupport.getContextClassLoader(); 559 if (cld == null) 560 cld = this.getClass().getClassLoader(); 561 bean = cmdinfo.getCommandObject(this, cld); 562 } catch (IOException e) { 563 } catch (ClassNotFoundException e) { } 564 565 return bean; 566 } 567 568 585 private synchronized DataContentHandler getDataContentHandler() { 586 587 if (factory != oldFactory) { 589 oldFactory = factory; 590 factoryDCH = null; 591 dataContentHandler = null; 592 transferFlavors = emptyFlavors; 593 } 594 595 if (dataContentHandler != null) 596 return dataContentHandler; 597 598 String simpleMT = getBaseType(); 599 600 if (factoryDCH == null && factory != null) 601 factoryDCH = factory.createDataContentHandler(simpleMT); 602 603 if (factoryDCH != null) 604 dataContentHandler = factoryDCH; 605 606 if (dataContentHandler == null) { 607 if (dataSource != null) 608 dataContentHandler = getCommandMap(). 609 createDataContentHandler(simpleMT, dataSource); 610 else 611 dataContentHandler = getCommandMap(). 612 createDataContentHandler(simpleMT); 613 } 614 615 if (dataSource != null) 618 dataContentHandler = new DataSourceDataContentHandler ( 619 dataContentHandler, 620 dataSource); 621 else 622 dataContentHandler = new ObjectDataContentHandler ( 623 dataContentHandler, 624 object, 625 objectMimeType); 626 return dataContentHandler; 627 } 628 629 633 private synchronized String getBaseType() { 634 if (shortType == null) { 635 String ct = getContentType(); 636 try { 637 MimeType mt = new MimeType (ct); 638 shortType = mt.getBaseType(); 639 } catch (MimeTypeParseException e) { 640 shortType = ct; 641 } 642 } 643 return shortType; 644 } 645 646 659 public static synchronized void setDataContentHandlerFactory( 660 DataContentHandlerFactory newFactory) { 661 if (factory != null) 662 throw new Error ("DataContentHandlerFactory already defined"); 663 664 SecurityManager security = System.getSecurityManager(); 665 if (security != null) { 666 try { 667 security.checkSetFactory(); 669 } catch (SecurityException ex) { 670 if (DataHandler .class.getClassLoader() != 674 newFactory.getClass().getClassLoader()) 675 throw ex; 676 } 677 } 678 factory = newFactory; 679 } 680 } 681 682 687 class DataHandlerDataSource implements DataSource { 688 DataHandler dataHandler = null; 689 690 693 public DataHandlerDataSource(DataHandler dh) { 694 this.dataHandler = dh; 695 } 696 697 701 public InputStream getInputStream() throws IOException { 702 return dataHandler.getInputStream(); 703 } 704 705 709 public OutputStream getOutputStream() throws IOException { 710 return dataHandler.getOutputStream(); 711 } 712 713 717 public String getContentType() { 718 return dataHandler.getContentType(); 719 } 720 721 725 public String getName() { 726 return dataHandler.getName(); } 728 } 729 730 737 class DataSourceDataContentHandler implements DataContentHandler { 738 private DataSource ds = null; 739 private DataFlavor transferFlavors[] = null; 740 private DataContentHandler dch = null; 741 742 745 public DataSourceDataContentHandler(DataContentHandler dch, DataSource ds) { 746 this.ds = ds; 747 this.dch = dch; 748 } 749 750 754 public DataFlavor [] getTransferDataFlavors() { 755 756 if (transferFlavors == null) { 757 if (dch != null) { transferFlavors = dch.getTransferDataFlavors(); 759 } else { 760 transferFlavors = new DataFlavor [1]; 761 transferFlavors[0] = 762 new ActivationDataFlavor (ds.getContentType(), 763 ds.getContentType()); 764 } 765 } 766 return transferFlavors; 767 } 768 769 775 public Object getTransferData(DataFlavor df, DataSource ds) throws 776 UnsupportedFlavorException , IOException { 777 778 if (dch != null) 779 return dch.getTransferData(df, ds); 780 else if (df.equals(getTransferDataFlavors()[0])) return ds.getInputStream(); 782 else 783 throw new UnsupportedFlavorException (df); 784 } 785 786 public Object getContent(DataSource ds) throws IOException { 787 788 if (dch != null) 789 return dch.getContent(ds); 790 else 791 return ds.getInputStream(); 792 } 793 794 797 public void writeTo(Object obj, String mimeType, OutputStream os) 798 throws IOException { 799 if (dch != null) 800 dch.writeTo(obj, mimeType, os); 801 else 802 throw new UnsupportedDataTypeException ( 803 "no DCH for content type " + ds.getContentType()); 804 } 805 } 806 807 814 class ObjectDataContentHandler implements DataContentHandler { 815 private DataFlavor transferFlavors[] = null; 816 private Object obj; 817 private String mimeType; 818 private DataContentHandler dch = null; 819 820 823 public ObjectDataContentHandler(DataContentHandler dch, 824 Object obj, String mimeType) { 825 this.obj = obj; 826 this.mimeType = mimeType; 827 this.dch = dch; 828 } 829 830 834 public DataContentHandler getDCH() { 835 return dch; 836 } 837 838 842 public DataFlavor [] getTransferDataFlavors() { 843 if (transferFlavors == null) { 844 if (dch != null) { 845 transferFlavors = dch.getTransferDataFlavors(); 846 } else { 847 transferFlavors = new DataFlavor [1]; 848 transferFlavors[0] = new ActivationDataFlavor (obj.getClass(), 849 mimeType, mimeType); 850 } 851 } 852 return transferFlavors; 853 } 854 855 861 public Object getTransferData(DataFlavor df, DataSource ds) 862 throws UnsupportedFlavorException , IOException { 863 864 if (dch != null) 865 return dch.getTransferData(df, ds); 866 else if (df.equals(transferFlavors[0])) return obj; 868 else 869 throw new UnsupportedFlavorException (df); 870 871 } 872 873 public Object getContent(DataSource ds) { 874 return obj; 875 } 876 877 880 public void writeTo(Object obj, String mimeType, OutputStream os) 881 throws IOException { 882 if (dch != null) 883 dch.writeTo(obj, mimeType, os); 884 else 885 throw new UnsupportedDataTypeException ( 886 "no object DCH for MIME type " + this.mimeType); 887 } 888 } 889 | Popular Tags |