KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > entity > MediaEntity


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.media.entity;
9
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.ejb.CreateException JavaDoc;
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 /**
45  * Abstract class to make the implementation of the Media Entity Bean cleaner.
46  *
47  * <p>It implements most of the <code>javax.emb.Media</code>,
48  * <code>javax.emb.MediaEntityLocal</code> and
49  * <code>javax.emb.MediaEntityLocalHome</code> required methods, and leaves
50  * the persistance methods to the <code>MediaEntityBean</code> subclass.
51  *
52  * @version <tt>$Revision: 1.1 $</tt>
53  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo ArgŁello</a>
54  */

55 public abstract class MediaEntity
56 {
57    /** Logger. */
58    private Logger log = Logger.getLogger(MediaEntity.class);
59
60    // Container Managed Persistance (CMP): ------------------------------------
61

62    public abstract String JavaDoc getManagedIdentity();
63    public abstract void setManagedIdentity(String JavaDoc identity);
64
65    public abstract byte[] getManagedContent();
66    public abstract void setManagedContent(byte[] content);
67
68    public abstract String JavaDoc getManagedLocation();
69    public abstract void setManagedLocation(String JavaDoc location);
70
71    public abstract String JavaDoc getManagedDescription();
72    public abstract void setManagedDescription(String JavaDoc description);
73
74    public abstract String JavaDoc getManagedName();
75    public abstract void setManagedName(String JavaDoc name);
76
77    public abstract String JavaDoc getManagedMimeType();
78    public abstract void setManagedMimeType(String JavaDoc mimeType);
79
80    public abstract long getManagedLastModified();
81    public abstract void setManagedLastModified(long lastModified);
82
83    public abstract Vector JavaDoc getManagedListeners();
84    public abstract void setManagedListeners(Vector JavaDoc listeners);
85
86    // Container Managed Relationships (CMR): ----------------------------------
87

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 JavaDoc getManagedParents();
98    public abstract void setManagedParents(Collection JavaDoc parents);
99
100    public abstract Collection JavaDoc getManagedChildren();
101    public abstract void setManagedChildren(Collection JavaDoc children);
102
103    public abstract Collection JavaDoc getManagedMetaDatas();
104    public abstract void setManagedMetaDatas(Collection JavaDoc metadatas);
105
106    // javax.emb.Media and javax.emb.MediaEntityLocal interfaces: --------------
107

108    /**
109    * @see javax.emb.MediaEntityLocal#addListener(javax.emb.MediaListener)
110    */

111    public void addListener(MediaListener listener)
112    {
113       if (listener == null)
114       {
115          throw new NullPointerException JavaDoc();
116       }
117
118       Vector JavaDoc listeners = getManagedListeners();
119
120       if (!listeners.contains(listener))
121       {
122          listeners.add(listener);
123          setManagedListeners(listeners);
124       }
125    }
126
127    /**
128     * @see javax.emb.MediaEntityLocal#addMetaData(javax.emb.MetaDataEntityLocal)
129     */

130    public void addMetaData(MetaDataEntityLocal metaData) throws MediaException
131    {
132       if (metaData == null)
133       {
134          throw new NullPointerException JavaDoc();
135       }
136
137       if (!getManagedMetaDatas().contains(metaData))
138       {
139          getManagedMetaDatas().add(metaData);
140       }
141    }
142
143    /**
144     * @see javax.emb.MediaEntityLocal#convert(javax.emb.MediaConverterSpec[])
145     */

146    public void convert(MediaConverterSpec[] specifications)
147       throws MediaException
148    {
149       if (specifications == null)
150       {
151          throw new NullPointerException JavaDoc();
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          // Nothing to do
164
return;
165       }
166
167       // No MediaConverterSpec can be null
168
for (int i = 0; i < specificationsLength; i++)
169       {
170          if (specifications[i] == null)
171          {
172             throw new ConversionException();
173          }
174       }
175
176       InputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(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 JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
189       lastSpecification.getConverter().process(inputStream, outputStream);
190       byte[] convertedContent = outputStream.toByteArray();
191
192       try
193       {
194          outputStream.close();
195       }
196       catch (IOException JavaDoc ignore)
197       {
198       }
199
200       String JavaDoc convertedName =
201          getFileName(getManagedName())
202             + "."
203             + lastSpecification.getTargetFileExtension();
204
205       String JavaDoc convertedMimeType = lastSpecification.getTargetMimeType();
206
207       setContent(convertedContent);
208       setName(convertedName);
209       setMimeType(convertedMimeType);
210       updateLastModified();
211    }
212
213    /**
214     * @see javax.emb.MediaEntityLocal#exportMedia(java.net.URL)
215     */

216    public URL JavaDoc exportMedia(URL JavaDoc targetDirectoryLocation) throws MediaException
217    {
218       String JavaDoc name = getManagedName();
219
220       String JavaDoc exportedFilePrefix = getFileName(name);
221       String JavaDoc exportedFileSuffix = getFileExtension(name);
222
223       try
224       {
225          File JavaDoc targetDirectory = new File JavaDoc(targetDirectoryLocation.getPath());
226
227          // TODO: Refactor to an MBean since we can't do file I/O inside an EJB!
228
File JavaDoc exportedFile =
229             File.createTempFile(
230                exportedFilePrefix,
231                exportedFileSuffix,
232                targetDirectory);
233
234          OutputStream JavaDoc exportedFileStream = new FileOutputStream JavaDoc(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             // Copy content to exportedFileStream
244
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 JavaDoc e)
253          {
254             // Throwed by OutputStream.write(byte[], int, int);
255
throw new ContentAccessException(e.getMessage());
256          }
257          finally
258          {
259             try
260             {
261                exportedFileStream.close();
262             }
263             catch (IOException JavaDoc ignore)
264             {
265             }
266          }
267       }
268       catch (IOException JavaDoc e)
269       {
270          // Throwed by File.createTempFile(String, String);
271
throw new ContentAccessException(e.getMessage());
272       }
273    }
274
275    /**
276      * @see javax.emb.MediaEntityLocal#getChildren()
277      */

278    public MediaEntityLocal[] getChildren() throws MediaException
279    {
280       Collection JavaDoc children = getManagedChildren();
281       return (MediaEntityLocal[]) children.toArray(new MediaEntityLocal[0]);
282    }
283
284    /**
285     * @see javax.emb.Media#getContent()
286     */

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    /**
302     * @see javax.emb.MediaEntityLocal#getDescription()
303     */

304    public String JavaDoc getDescription() throws MediaException
305    {
306       return getManagedDescription();
307    }
308
309    /**
310     * @see javax.emb.Media#getFormat()
311     */

312    public MediaFormat getFormat() throws MediaException
313    {
314       String JavaDoc name = getName();
315
316       if (name == null)
317       {
318          throw new FormatNotFoundException();
319       }
320
321       String JavaDoc fileExtension = getFileExtension(name);
322
323       MediaFormat mediaFormat =
324          MediaFormatRegistry.SINGLETON.lookup(fileExtension);
325
326       return mediaFormat;
327    }
328
329    /**
330     * @see javax.emb.Media#getHeader()
331     */

332    public MediaHeader getHeader() throws MediaException
333    {
334       InputStream JavaDoc content = new ByteArrayInputStream JavaDoc(getContent());
335       return getFormat().extractHeader(content);
336    }
337
338    /**
339     * @see javax.emb.MediaEntityLocal#getLastModified()
340     */

341    public long getLastModified() throws MediaException
342    {
343       return getManagedLastModified();
344    }
345
346    /**
347     * @see javax.emb.MediaEntityLocal#getListeners()
348     */

349    public MediaListener[] getListeners() throws MediaException
350    {
351       Vector JavaDoc listeners = getManagedListeners();
352       return (MediaListener[]) listeners.toArray(new MediaListener[0]);
353    }
354
355    /**
356     * @see javax.emb.MediaEntityLocal#getLocation()
357     */

358    public URL JavaDoc getLocation() throws MediaException
359    {
360       String JavaDoc location = getManagedLocation();
361
362       try
363       {
364          return new URL JavaDoc(location);
365       }
366       catch (MalformedURLException JavaDoc e)
367       {
368          return null;
369       }
370    }
371
372    /**
373     * @see javax.emb.MediaEntityLocal#getMetaData()
374     */

375    public MetaDataEntityLocal[] getMetaData() throws MediaException
376    {
377       return (MetaDataEntityLocal[]) getManagedMetaDatas().toArray(
378          new MetaDataEntityLocal[0]);
379    }
380
381    /**
382     * @see javax.emb.Media#getMimeType()
383     */

384    public String JavaDoc getMimeType() throws MediaException
385    {
386       String JavaDoc mimeType = getManagedMimeType();
387
388       if (mimeType == null)
389       {
390          mimeType = getFormat().getDefaultMimeType();
391       }
392
393       return mimeType;
394    }
395
396    /**
397     * @see javax.emb.Media#getName()
398     */

399    public String JavaDoc getName() throws MediaException
400    {
401       return getManagedName();
402    }
403
404    /**
405      * @see javax.emb.MediaEntityLocal#getNextVersion()
406      */

407    public MediaEntityLocal getNextVersion() throws MediaException
408    {
409       return getManagedNextVersion();
410    }
411
412    /**
413     * @see javax.emb.MediaEntityLocal#getParents()
414     */

415    public MediaEntityLocal[] getParents() throws MediaException
416    {
417       Collection JavaDoc parents = getManagedParents();
418       return (MediaEntityLocal[]) parents.toArray(new MediaEntityLocal[0]);
419    }
420
421    /**
422     * @see javax.emb.MediaEntityLocal#getPreviousVersion()
423     */

424    public MediaEntityLocal getPreviousVersion() throws MediaException
425    {
426       return getManagedPreviousVersion();
427    }
428
429    /**
430     * @see javax.emb.Media#getProxy()
431     */

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             // Build a MediaBean from the proxy content
442
InputStream JavaDoc content = new ByteArrayInputStream JavaDoc(proxy.getContent());
443             proxyMedia =
444                new MediaBean(content, proxy.getMimeType(), proxy.getName());
445          }
446          else
447          {
448             // Return a transient proxy from this format
449
InputStream JavaDoc content = new ByteArrayInputStream JavaDoc(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    /**
469     * @see javax.emb.Media#getSize()
470     */

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    /**
482     * @see javax.emb.MediaEntityLocal#importMedia(java.net.URL, java.lang.String)
483     */

484    public void importMedia(URL JavaDoc sourceLocation, String JavaDoc name)
485       throws MediaException
486    {
487       if (sourceLocation == null)
488       {
489          throw new NullPointerException JavaDoc();
490       }
491
492       setName(name);
493
494       InputStream JavaDoc sourceStream = null;
495
496       try
497       {
498          // TODO: Refactor to an MBean since we can't do file I/O inside an EJB!
499
sourceStream = new FileInputStream JavaDoc(sourceLocation.getPath());
500          setContent(sourceStream);
501       }
502       catch (IOException JavaDoc 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 JavaDoc ignore)
515             {
516             }
517          }
518       }
519    }
520
521    /**
522     * @see javax.emb.Media#readContent(long, byte[])
523     */

524    public int readContent(long position, byte[] buffer) throws MediaException
525    {
526       return readContent(position, buffer, 0, buffer.length);
527    }
528
529    /**
530     * @see javax.emb.Media#readContent(long, byte[], int, int)
531     */

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    /**
548     * @see javax.emb.MediaEntityLocal#removeListener(javax.emb.MediaListener)
549     */

550    public void removeListener(MediaListener listener) throws MediaException
551    {
552       if (listener == null)
553       {
554          throw new NullPointerException JavaDoc();
555       }
556
557       Vector JavaDoc listeners = getManagedListeners();
558
559       if (listeners.contains(listener))
560       {
561          listeners.remove(listener);
562          setManagedListeners(listeners);
563       }
564    }
565
566    /**
567     * @see javax.emb.MediaEntityLocal#removeMetaData(javax.emb.MetaDataEntityLocal)
568     */

569    public void removeMetaData(MetaDataEntityLocal metaData)
570       throws MediaException
571    {
572       if (metaData == null)
573       {
574          throw new NullPointerException JavaDoc();
575       }
576
577       getManagedMetaDatas().remove(metaData);
578    }
579
580    /**
581     * @see javax.emb.MediaEntityLocal#setChildren(javax.emb.MediaEntityLocal[])
582     */

583    public void setChildren(MediaEntityLocal[] children) throws MediaException
584    {
585       if (children == null)
586       {
587          throw new NullPointerException JavaDoc();
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    /**
607     * @see javax.emb.MediaEntityLocal#setContent(byte[])
608     */

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    /**
621     * @see javax.emb.MediaEntityLocal#setContent(java.io.InputStream)
622     */

623    public void setContent(InputStream JavaDoc content) throws MediaException
624    {
625       if (content == null)
626       {
627          throw new NullPointerException JavaDoc();
628       }
629
630       if (getLocation() != null)
631       {
632          throw new ContentUnmutableException();
633       }
634
635       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc e)
655       {
656          throw new ContentAccessException(e.getMessage());
657       }
658       finally
659       {
660          try
661          {
662             out.close();
663          }
664          catch (IOException JavaDoc ignore)
665          {
666          }
667       }
668    }
669
670    /**
671     * @see javax.emb.MediaEntityLocal#setDescription(java.lang.String)
672     */

673    public void setDescription(String JavaDoc description) throws MediaException
674    {
675       setManagedDescription(description);
676       updateLastModified();
677    }
678
679    /**
680     * @see javax.emb.MediaEntityLocal#setLocation(java.net.URL)
681     */

682    public void setLocation(URL JavaDoc location) throws MediaException
683    {
684       if (getContent() != null)
685       {
686          throw new ContentUnmutableException();
687       }
688
689       setManagedLocation(location.toExternalForm());
690       updateLastModified();
691    }
692
693    /**
694     * @see javax.emb.MediaEntityLocal#setMimeType(java.lang.String)
695     */

696    public void setMimeType(String JavaDoc mimeType) throws MediaException
697    {
698       setManagedMimeType(mimeType);
699       updateLastModified();
700    }
701
702    /**
703     * @see javax.emb.MediaEntityLocal#setName(java.lang.String)
704     */

705    public void setName(String JavaDoc name) throws MediaException
706    {
707       setManagedName(name);
708       updateLastModified();
709    }
710
711    /**
712     * @see javax.emb.MediaEntityLocal#setPreviousVersion(javax.emb.MediaEntityLocal)
713     */

714    public void setPreviousVersion(MediaEntityLocal mediaEntity)
715       throws MediaException
716    {
717       // TODO: Implement
718
throw new UnsupportedOperationException JavaDoc("Not implemented yet!");
719    }
720
721    /**
722     * @see javax.emb.MediaEntityLocal#setProxy(javax.emb.MediaEntityLocal)
723     */

724    public void setProxy(MediaEntityLocal mediaEntity) throws MediaException
725    {
726       setManagedProxy(mediaEntity);
727    }
728
729    // javax.emb.MediaEntityLocalHome interface: -------------------------------
730

731    /**
732     * @see javax.emb.MediaEntityLocalHome#exportMedia(javax.emb.MediaEntityLocal[], java.net.URL)
733     */

734    public URL JavaDoc[] ejbHomeExportMedia(
735       MediaEntityLocal[] sourceMedia,
736       URL JavaDoc targetDirectoryLocation)
737       throws MediaException
738    {
739       // TODO: Implement
740
throw new UnsupportedOperationException JavaDoc("Not implemented yet!");
741    }
742
743    /**
744     * @see javax.emb.MediaEntityLocalHome#importMedia(java.net.URL[], java.lang.String[])
745     */

746    public MediaEntityLocal[] ejbHomeImportMedia(
747       URL JavaDoc[] sourceLocations,
748       String JavaDoc[] names)
749       throws CreateException JavaDoc, MediaException
750    {
751       // TODO: Implement
752
throw new UnsupportedOperationException JavaDoc("Not implemented yet!");
753    }
754
755    /**
756     * @see javax.emb.MediaEntityLocalHome#publishContent(javax.emb.Media, byte, javax.emb.ProtocolConstraints)
757     */

758    public URL JavaDoc ejbHomePublishContent(
759       Media mediaObject,
760       byte protocol,
761       ProtocolConstraints constraints)
762       throws MediaException
763    {
764       // TODO: Implement
765
throw new UnsupportedOperationException JavaDoc("Not implemented yet!");
766    }
767
768    /**
769     * @see javax.emb.MediaEntityLocalHome#publishMedia(javax.emb.MediaEntityLocal[], byte, javax.emb.ProtocolConstraints)
770     */

771    public Media ejbHomePublishMedia(
772       MediaEntityLocal[] playlist,
773       byte transferType,
774       ProtocolConstraints constraints)
775       throws MediaException
776    {
777       // TODO: Implement
778
throw new UnsupportedOperationException JavaDoc("Not implemented yet!");
779    }
780
781    // Protected: --------------------------------------------------------------
782

783    protected void updateLastModified()
784    {
785       setManagedLastModified(System.currentTimeMillis());
786    }
787
788    // Private: ----------------------------------------------------------------
789

790    private String JavaDoc getFileName(String JavaDoc 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 JavaDoc getFileExtension(String JavaDoc 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