1 7 8 package org.jboss.media.entity; 9 10 import java.io.ByteArrayInputStream ; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.OutputStream ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Collection ; 21 import java.util.Vector ; 22 23 import javax.ejb.CreateException ; 24 import javax.emb.ContentAccessException; 25 import javax.emb.ContentUnmutableException; 26 import javax.emb.ConversionException; 27 import javax.emb.FormatNotFoundException; 28 import javax.emb.GenericMediaFormat; 29 import javax.emb.Media; 30 import javax.emb.MediaBean; 31 import javax.emb.MediaConverter; 32 import javax.emb.MediaConverterSpec; 33 import javax.emb.MediaEntityLocal; 34 import javax.emb.MediaException; 35 import javax.emb.MediaFormat; 36 import javax.emb.MediaFormatRegistry; 37 import javax.emb.MediaHeader; 38 import javax.emb.MediaListener; 39 import javax.emb.MetaDataEntityLocal; 40 import javax.emb.ProtocolConstraints; 41 42 import org.jboss.logging.Logger; 43 44 55 public abstract class MediaEntity 56 { 57 58 private Logger log = Logger.getLogger(MediaEntity.class); 59 60 62 public abstract String getManagedIdentity(); 63 public abstract void setManagedIdentity(String identity); 64 65 public abstract byte[] getManagedContent(); 66 public abstract void setManagedContent(byte[] content); 67 68 public abstract String getManagedLocation(); 69 public abstract void setManagedLocation(String location); 70 71 public abstract String getManagedDescription(); 72 public abstract void setManagedDescription(String description); 73 74 public abstract String getManagedName(); 75 public abstract void setManagedName(String name); 76 77 public abstract String getManagedMimeType(); 78 public abstract void setManagedMimeType(String mimeType); 79 80 public abstract long getManagedLastModified(); 81 public abstract void setManagedLastModified(long lastModified); 82 83 public abstract Vector getManagedListeners(); 84 public abstract void setManagedListeners(Vector listeners); 85 86 88 public abstract MediaEntityLocal getManagedProxy(); 89 public abstract void setManagedProxy(MediaEntityLocal proxy); 90 91 public abstract MediaEntityLocal getManagedPreviousVersion(); 92 public abstract void setManagedPreviousVersion(MediaEntityLocal previousVersion); 93 94 public abstract MediaEntityLocal getManagedNextVersion(); 95 public abstract void setManagedNextVersion(MediaEntityLocal nextVersion); 96 97 public abstract Collection getManagedParents(); 98 public abstract void setManagedParents(Collection parents); 99 100 public abstract Collection getManagedChildren(); 101 public abstract void setManagedChildren(Collection children); 102 103 public abstract Collection getManagedMetaDatas(); 104 public abstract void setManagedMetaDatas(Collection metadatas); 105 106 108 111 public void addListener(MediaListener listener) 112 { 113 if (listener == null) 114 { 115 throw new NullPointerException (); 116 } 117 118 Vector listeners = getManagedListeners(); 119 120 if (!listeners.contains(listener)) 121 { 122 listeners.add(listener); 123 setManagedListeners(listeners); 124 } 125 } 126 127 130 public void addMetaData(MetaDataEntityLocal metaData) throws MediaException 131 { 132 if (metaData == null) 133 { 134 throw new NullPointerException (); 135 } 136 137 if (!getManagedMetaDatas().contains(metaData)) 138 { 139 getManagedMetaDatas().add(metaData); 140 } 141 } 142 143 146 public void convert(MediaConverterSpec[] specifications) 147 throws MediaException 148 { 149 if (specifications == null) 150 { 151 throw new NullPointerException (); 152 } 153 154 if (getManagedLocation() != null) 155 { 156 throw new ContentUnmutableException(); 157 } 158 159 int specificationsLength = specifications.length; 160 161 if (specificationsLength == 0) 162 { 163 return; 165 } 166 167 for (int i = 0; i < specificationsLength; i++) 169 { 170 if (specifications[i] == null) 171 { 172 throw new ConversionException(); 173 } 174 } 175 176 InputStream inputStream = new ByteArrayInputStream (getContent()); 177 178 for (int i = 0; i < specificationsLength - 1; i++) 179 { 180 MediaConverterSpec mediaConverterSpec = specifications[i]; 181 MediaConverter mediaConverter = mediaConverterSpec.getConverter(); 182 inputStream = mediaConverter.process(inputStream); 183 } 184 185 MediaConverterSpec lastSpecification = 186 specifications[specificationsLength - 1]; 187 188 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 189 lastSpecification.getConverter().process(inputStream, outputStream); 190 byte[] convertedContent = outputStream.toByteArray(); 191 192 try 193 { 194 outputStream.close(); 195 } 196 catch (IOException ignore) 197 { 198 } 199 200 String convertedName = 201 getFileName(getManagedName()) 202 + "." 203 + lastSpecification.getTargetFileExtension(); 204 205 String convertedMimeType = lastSpecification.getTargetMimeType(); 206 207 setContent(convertedContent); 208 setName(convertedName); 209 setMimeType(convertedMimeType); 210 updateLastModified(); 211 } 212 213 216 public URL exportMedia(URL targetDirectoryLocation) throws MediaException 217 { 218 String name = getManagedName(); 219 220 String exportedFilePrefix = getFileName(name); 221 String exportedFileSuffix = getFileExtension(name); 222 223 try 224 { 225 File targetDirectory = new File (targetDirectoryLocation.getPath()); 226 227 File exportedFile = 229 File.createTempFile( 230 exportedFilePrefix, 231 exportedFileSuffix, 232 targetDirectory); 233 234 OutputStream exportedFileStream = new FileOutputStream (exportedFile); 235 236 try 237 { 238 int DEFAULT_BUFFER_SIZE = 65536; 239 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 240 int bytesRead; 241 long position = 0; 242 243 while ((bytesRead = readContent(position, buffer)) != -1) 245 { 246 exportedFileStream.write(buffer, 0, bytesRead); 247 position += bytesRead; 248 } 249 250 return exportedFile.toURL(); 251 } 252 catch (IOException e) 253 { 254 throw new ContentAccessException(e.getMessage()); 256 } 257 finally 258 { 259 try 260 { 261 exportedFileStream.close(); 262 } 263 catch (IOException ignore) 264 { 265 } 266 } 267 } 268 catch (IOException e) 269 { 270 throw new ContentAccessException(e.getMessage()); 272 } 273 } 274 275 278 public MediaEntityLocal[] getChildren() throws MediaException 279 { 280 Collection children = getManagedChildren(); 281 return (MediaEntityLocal[]) children.toArray(new MediaEntityLocal[0]); 282 } 283 284 287 public byte[] getContent() throws MediaException 288 { 289 byte[] content = getManagedContent(); 290 291 if (content == null) 292 { 293 throw new ContentAccessException(); 294 } 295 else 296 { 297 return content; 298 } 299 } 300 301 304 public String getDescription() throws MediaException 305 { 306 return getManagedDescription(); 307 } 308 309 312 public MediaFormat getFormat() throws MediaException 313 { 314 String name = getName(); 315 316 if (name == null) 317 { 318 throw new FormatNotFoundException(); 319 } 320 321 String fileExtension = getFileExtension(name); 322 323 MediaFormat mediaFormat = 324 MediaFormatRegistry.SINGLETON.lookup(fileExtension); 325 326 return mediaFormat; 327 } 328 329 332 public MediaHeader getHeader() throws MediaException 333 { 334 InputStream content = new ByteArrayInputStream (getContent()); 335 return getFormat().extractHeader(content); 336 } 337 338 341 public long getLastModified() throws MediaException 342 { 343 return getManagedLastModified(); 344 } 345 346 349 public MediaListener[] getListeners() throws MediaException 350 { 351 Vector listeners = getManagedListeners(); 352 return (MediaListener[]) listeners.toArray(new MediaListener[0]); 353 } 354 355 358 public URL getLocation() throws MediaException 359 { 360 String location = getManagedLocation(); 361 362 try 363 { 364 return new URL (location); 365 } 366 catch (MalformedURLException e) 367 { 368 return null; 369 } 370 } 371 372 375 public MetaDataEntityLocal[] getMetaData() throws MediaException 376 { 377 return (MetaDataEntityLocal[]) getManagedMetaDatas().toArray( 378 new MetaDataEntityLocal[0]); 379 } 380 381 384 public String getMimeType() throws MediaException 385 { 386 String mimeType = getManagedMimeType(); 387 388 if (mimeType == null) 389 { 390 mimeType = getFormat().getDefaultMimeType(); 391 } 392 393 return mimeType; 394 } 395 396 399 public String getName() throws MediaException 400 { 401 return getManagedName(); 402 } 403 404 407 public MediaEntityLocal getNextVersion() throws MediaException 408 { 409 return getManagedNextVersion(); 410 } 411 412 415 public MediaEntityLocal[] getParents() throws MediaException 416 { 417 Collection parents = getManagedParents(); 418 return (MediaEntityLocal[]) parents.toArray(new MediaEntityLocal[0]); 419 } 420 421 424 public MediaEntityLocal getPreviousVersion() throws MediaException 425 { 426 return getManagedPreviousVersion(); 427 } 428 429 432 public Media getProxy() throws MediaException 433 { 434 MediaEntityLocal proxy = getManagedProxy(); 435 Media proxyMedia = null; 436 437 try 438 { 439 if (proxy != null) 440 { 441 InputStream content = new ByteArrayInputStream (proxy.getContent()); 443 proxyMedia = 444 new MediaBean(content, proxy.getMimeType(), proxy.getName()); 445 } 446 else 447 { 448 InputStream content = new ByteArrayInputStream (getContent()); 450 proxyMedia = getFormat().extractProxy(content); 451 } 452 453 if (proxyMedia != null) 454 { 455 return proxyMedia; 456 } 457 else 458 { 459 return new GenericMediaFormat().extractProxy(null); 460 } 461 } 462 catch (MediaException e) 463 { 464 return new GenericMediaFormat().extractProxy(null); 465 } 466 } 467 468 471 public long getSize() throws MediaException 472 { 473 if (getContent() == null) 474 { 475 throw new ContentAccessException(); 476 } 477 478 return getContent().length; 479 } 480 481 484 public void importMedia(URL sourceLocation, String name) 485 throws MediaException 486 { 487 if (sourceLocation == null) 488 { 489 throw new NullPointerException (); 490 } 491 492 setName(name); 493 494 InputStream sourceStream = null; 495 496 try 497 { 498 sourceStream = new FileInputStream (sourceLocation.getPath()); 500 setContent(sourceStream); 501 } 502 catch (IOException e) 503 { 504 throw new ContentAccessException(); 505 } 506 finally 507 { 508 if (sourceStream != null) 509 { 510 try 511 { 512 sourceStream.close(); 513 } 514 catch (IOException ignore) 515 { 516 } 517 } 518 } 519 } 520 521 524 public int readContent(long position, byte[] buffer) throws MediaException 525 { 526 return readContent(position, buffer, 0, buffer.length); 527 } 528 529 532 public int readContent(long position, byte[] buffer, int offset, int length) 533 throws MediaException 534 { 535 byte[] content = getManagedContent(); 536 537 if (content == null) 538 { 539 throw new ContentAccessException(); 540 } 541 542 System.arraycopy(content, (int) position, buffer, offset, length); 543 544 return content.length; 545 } 546 547 550 public void removeListener(MediaListener listener) throws MediaException 551 { 552 if (listener == null) 553 { 554 throw new NullPointerException (); 555 } 556 557 Vector listeners = getManagedListeners(); 558 559 if (listeners.contains(listener)) 560 { 561 listeners.remove(listener); 562 setManagedListeners(listeners); 563 } 564 } 565 566 569 public void removeMetaData(MetaDataEntityLocal metaData) 570 throws MediaException 571 { 572 if (metaData == null) 573 { 574 throw new NullPointerException (); 575 } 576 577 getManagedMetaDatas().remove(metaData); 578 } 579 580 583 public void setChildren(MediaEntityLocal[] children) throws MediaException 584 { 585 if (children == null) 586 { 587 throw new NullPointerException (); 588 } 589 590 if (getLocation() != null) 591 { 592 throw new ContentUnmutableException(); 593 } 594 595 if (getContent() == null) 596 { 597 throw new ContentAccessException(); 598 } 599 600 for (int i = 0; i < children.length; i++) 601 { 602 getManagedChildren().add(children[i]); 603 } 604 } 605 606 609 public void setContent(byte[] content) throws MediaException 610 { 611 if (getLocation() != null) 612 { 613 throw new ContentUnmutableException(); 614 } 615 616 setManagedContent(content); 617 updateLastModified(); 618 } 619 620 623 public void setContent(InputStream content) throws MediaException 624 { 625 if (content == null) 626 { 627 throw new NullPointerException (); 628 } 629 630 if (getLocation() != null) 631 { 632 throw new ContentUnmutableException(); 633 } 634 635 ByteArrayOutputStream out = new ByteArrayOutputStream (); 636 637 try 638 { 639 int DEFAULT_BUFFER_SIZE = 65536; 640 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 641 int bytesRead; 642 643 while ((bytesRead = content.read(buffer)) != -1) 644 { 645 out.write(buffer, 0, bytesRead); 646 } 647 648 byte[] newContent = new byte[out.size()]; 649 newContent = out.toByteArray(); 650 651 setManagedContent(newContent); 652 updateLastModified(); 653 } 654 catch (IOException e) 655 { 656 throw new ContentAccessException(e.getMessage()); 657 } 658 finally 659 { 660 try 661 { 662 out.close(); 663 } 664 catch (IOException ignore) 665 { 666 } 667 } 668 } 669 670 673 public void setDescription(String description) throws MediaException 674 { 675 setManagedDescription(description); 676 updateLastModified(); 677 } 678 679 682 public void setLocation(URL location) throws MediaException 683 { 684 if (getContent() != null) 685 { 686 throw new ContentUnmutableException(); 687 } 688 689 setManagedLocation(location.toExternalForm()); 690 updateLastModified(); 691 } 692 693 696 public void setMimeType(String mimeType) throws MediaException 697 { 698 setManagedMimeType(mimeType); 699 updateLastModified(); 700 } 701 702 705 public void setName(String name) throws MediaException 706 { 707 setManagedName(name); 708 updateLastModified(); 709 } 710 711 714 public void setPreviousVersion(MediaEntityLocal mediaEntity) 715 throws MediaException 716 { 717 throw new UnsupportedOperationException ("Not implemented yet!"); 719 } 720 721 724 public void setProxy(MediaEntityLocal mediaEntity) throws MediaException 725 { 726 setManagedProxy(mediaEntity); 727 } 728 729 731 734 public URL [] ejbHomeExportMedia( 735 MediaEntityLocal[] sourceMedia, 736 URL targetDirectoryLocation) 737 throws MediaException 738 { 739 throw new UnsupportedOperationException ("Not implemented yet!"); 741 } 742 743 746 public MediaEntityLocal[] ejbHomeImportMedia( 747 URL [] sourceLocations, 748 String [] names) 749 throws CreateException , MediaException 750 { 751 throw new UnsupportedOperationException ("Not implemented yet!"); 753 } 754 755 758 public URL ejbHomePublishContent( 759 Media mediaObject, 760 byte protocol, 761 ProtocolConstraints constraints) 762 throws MediaException 763 { 764 throw new UnsupportedOperationException ("Not implemented yet!"); 766 } 767 768 771 public Media ejbHomePublishMedia( 772 MediaEntityLocal[] playlist, 773 byte transferType, 774 ProtocolConstraints constraints) 775 throws MediaException 776 { 777 throw new UnsupportedOperationException ("Not implemented yet!"); 779 } 780 781 783 protected void updateLastModified() 784 { 785 setManagedLastModified(System.currentTimeMillis()); 786 } 787 788 790 private String getFileName(String name) 791 { 792 int lastDotPosition = name.lastIndexOf('.'); 793 794 if (lastDotPosition == -1) 795 { 796 return name; 797 } 798 else 799 { 800 return name.substring(0, lastDotPosition); 801 } 802 } 803 804 private String getFileExtension(String name) 805 { 806 int lastDotPosition = name.lastIndexOf('.'); 807 808 if (lastDotPosition == -1) 809 { 810 return null; 811 } 812 else 813 { 814 return name.substring(lastDotPosition + 1); 815 } 816 } 817 } | Popular Tags |