Source Code Cross Referenced for MediaSampleSessionBean.java in  » J2EE » JOnAS-4.8.6 » emb » sample » session » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » emb.sample.session 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2005 Bull S.A.
004:         * Contact: jonas-team@objectweb.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * Initial developer(s): Brice Ruzand
022:         *
023:         * --------------------------------------------------------------------------
024:         * $Id$
025:         * --------------------------------------------------------------------------
026:         */
027:
028:        package emb.sample.session;
029:
030:        import java.io.InputStream;
031:        import java.net.URL;
032:        import java.util.ArrayList;
033:        import java.util.HashMap;
034:        import java.util.Iterator;
035:        import java.util.LinkedList;
036:        import java.util.List;
037:        import java.util.Map;
038:
039:        import javax.ejb.FinderException;
040:        import javax.ejb.RemoveException;
041:        import javax.ejb.SessionBean;
042:        import javax.ejb.SessionContext;
043:        import javax.emb.Media;
044:        import javax.emb.MediaEntityLocal;
045:        import javax.emb.MediaEntityLocalHome;
046:        import javax.emb.MediaException;
047:        import javax.naming.InitialContext;
048:        import javax.naming.NamingException;
049:
050:        import emb.sample.MediaSampleException;
051:
052:        /**
053:         * This is an example of Session Bean, statefull, and synchronized.
054:         *
055:         * @author JOnAS team
056:         */
057:        public class MediaSampleSessionBean implements  SessionBean {
058:
059:            /**
060:             * serialVersionUID
061:             */
062:            private static final long serialVersionUID = 2549701184925498878L;
063:
064:            // =======================================================================
065:            // Refs
066:            // =======================================================================
067:
068:            /**
069:             * Media Enotity Bean Home reference
070:             */
071:            private static final String MEB_HOME_REF = "java:comp/env/ejb/emb/MediaEntity";
072:
073:            /**
074:             * Reference on the env entry isCopyAllowed
075:             */
076:            public static final String IS_COPY_ALLOWED_REF = "java:comp/env/ejb/embSample/isCopyAllowed";
077:
078:            /**
079:             * Reference on the env entry maxMediaPerSession
080:             */
081:            public static final String MAX_MEDIA_PER_SESSION_REF = "java:comp/env/ejb/embSample/maxMediaPerSession";
082:
083:            // =======================================================================
084:            // Fields
085:            // =======================================================================
086:
087:            /**
088:             * cache of the env value
089:             */
090:            private Boolean isCopyAllowed = null;
091:
092:            /**
093:             * cache of the env value
094:             */
095:            private Integer maxMediaPerSession = null;
096:
097:            /**
098:             * list containing primary key of all generated media, use to remove them
099:             */
100:            private final List mediaListAllCreated = new LinkedList();
101:
102:            /**
103:             * list containing primary key of all media standart media that are beeing
104:             * showed
105:             */
106:            private final List mediaListStdShow = new LinkedList();
107:
108:            /**
109:             * list containing primary key of all that have been upload by user
110:             */
111:            private final List mediaListUpload = new LinkedList();
112:
113:            /**
114:             * Map containing name and primary key of some media
115:             */
116:            private final Map mediaRegistredName = new HashMap();
117:
118:            // =======================================================================
119:            // Medthods
120:            // =======================================================================
121:
122:            /**
123:             * There must be one ejbCreate() method per create() method on the Home
124:             * interface, and with the same signature.
125:             */
126:            public void ejbCreate() {
127:
128:                // in case we are outside transactions
129:            }
130:
131:            /**
132:             * Create a new MediaEntity from a stream
133:             *
134:             * @param contentStream content of the MediaEntity
135:             * @param name name of the MediaEntity
136:             * @param description description of the MediaEntity
137:             * @param status is ones of {@link MediaSampleSessionLocal#DONT_SHOW},
138:             *            {@link MediaSampleSessionLocal#SHOW_IN_LIST},
139:             *            {@link MediaSampleSessionLocal#USER_ADDED}
140:             * @param registeredName name to acces it with
141:             *            {@link #getMediaFromRegistredName(String)}
142:             * @return MediaEntity
143:             * @throws MediaSampleException if the mediaEntity cannot be create
144:             */
145:            public MediaEntityLocal createMediaEntity(
146:                    InputStream contentStream, String name, String description,
147:                    byte status, String registeredName)
148:                    throws MediaSampleException {
149:
150:                // check maxmedia
151:                checkMaxMedia();
152:
153:                MediaEntityLocal meb = null;
154:                try {
155:
156:                    meb = getMebHome().create();
157:                    meb.setName(name);
158:
159:                    if (!meb.getFormat().isEmbedded()) {
160:                        throw new MediaSampleException(
161:                                "A not embedded media have to be imported and not upload in order to import its children too.");
162:                    }
163:
164:                    meb.setContent(contentStream);
165:                    meb.setDescription(description);
166:
167:                    try {
168:                        // publish proxy
169:                        Media proxy = meb.getProxy();
170:                        getMebHome().publishContent(proxy,
171:                                MediaEntityLocalHome.TRANSFER_TYPE_BURST, null);
172:                    } catch (MediaException e) {
173:                        // do nothing
174:                    }
175:
176:                    // store the key
177:                    addMedia(meb, status);
178:
179:                    if (registeredName != null) {
180:                        mediaRegistredName.put(registeredName, meb
181:                                .getPrimaryKey());
182:                    }
183:
184:                    return meb;
185:
186:                } catch (Exception e) {
187:                    if (meb != null) {
188:                        try {
189:                            meb.remove();
190:                        } catch (Throwable e1) {
191:                            // do nothing
192:                        }
193:                    }
194:                    throw new MediaSampleException("Unable to create Media : "
195:                            + name + " : " + e, e);
196:                }
197:            }
198:
199:            /**
200:             * Create a new MediaEntity from an URL
201:             *
202:             * @param urlToImport of the media to import
203:             * @param description description of the MediaEntity
204:             * @param status is ones of {@link MediaSampleSessionLocal#DONT_SHOW},
205:             *            {@link MediaSampleSessionLocal#SHOW_IN_LIST},
206:             *            {@link MediaSampleSessionLocal#USER_ADDED}
207:             * @return MediaEntity
208:             * @throws MediaSampleException if the mediaEntity cannot be create
209:             */
210:            public MediaEntityLocal createMediaEntity(URL urlToImport,
211:                    String description, byte status)
212:                    throws MediaSampleException {
213:
214:                // check maxmedia
215:                checkMaxMedia();
216:
217:                MediaEntityLocal meb = null;
218:                try {
219:
220:                    MediaEntityLocal[] mebs = getMebHome().importMedia(
221:                            new URL[] { urlToImport }, null);
222:
223:                    if (mebs.length > 0 && mebs[0] != null) {
224:
225:                        meb = mebs[0];
226:                        meb.setDescription(description);
227:
228:                        try {
229:                            // publish proxy
230:                            Media proxy = meb.getProxy();
231:                            getMebHome().publishContent(proxy,
232:                                    MediaEntityLocalHome.TRANSFER_TYPE_BURST,
233:                                    null);
234:                        } catch (MediaException e) {
235:                            // do nothing
236:                        }
237:
238:                        // store the key
239:                        addMedia(meb, status);
240:
241:                        return meb;
242:                    }
243:                    throw new MediaSampleException("Unable to import the media");
244:                } catch (Exception e) {
245:                    if (meb != null) {
246:                        // totdo use super remover
247:                        try {
248:                            meb.remove();
249:                        } catch (Throwable e1) {
250:                            // do nothing
251:                        }
252:                    }
253:                    throw new MediaSampleException("Unable to import Media : "
254:                            + urlToImport + " : " + e, e);
255:                }
256:            }
257:
258:            /**
259:             * Create a new MediaEntity from a Media
260:             *
261:             * @param media to copy
262:             * @return MediaEntity
263:             * @throws MediaSampleException if the mediaEntity cannot be create
264:             */
265:            public MediaEntityLocal copyMediaEntity(Media media)
266:                    throws MediaSampleException {
267:
268:                if (!getCopyAllowed() && media instanceof  MediaEntityLocal) {
269:                    return (MediaEntityLocal) media;
270:                }
271:
272:                // check maxmedia
273:                checkMaxMedia();
274:
275:                try {
276:
277:                    MediaEntityLocal meb = getMebHome().create();
278:
279:                    // store the key
280:                    addMedia(meb, MediaSampleSessionLocal.DONT_SHOW);
281:
282:                    meb.setName(media.getName());
283:                    meb.setContent(media.getContent());
284:                    meb.setMimeType(media.getMimeType());
285:                    meb.setChildren(meb.getChildren());
286:
287:                    return meb;
288:
289:                } catch (Exception e) {
290:                    throw new MediaSampleException("Unable to copy Media", e);
291:                }
292:            }
293:
294:            /**
295:             * Get media list (only status {@link MediaSampleSessionLocal#SHOW_IN_LIST},
296:             * {@link MediaSampleSessionLocal#USER_ADDED}
297:             *
298:             * @return MediaEntity array
299:             * @throws MediaSampleException if media cannot be retrive
300:             */
301:
302:            public MediaEntityLocal[] getMediaList()
303:                    throws MediaSampleException {
304:
305:                try {
306:                    List mebs = new ArrayList(mediaListUpload.size()
307:                            + mediaListStdShow.size());
308:                    for (Iterator i = mediaListUpload.iterator(); i.hasNext();) {
309:                        String primaryKey = (String) i.next();
310:                        try {
311:                            MediaEntityLocal meb = getMebHome()
312:                                    .findByPrimaryKey(primaryKey);
313:                            mebs.add(meb);
314:                        } catch (FinderException e) {
315:                            // do nothing
316:                        }
317:                    }
318:                    for (Iterator i = mediaListStdShow.iterator(); i.hasNext();) {
319:                        String primaryKey = (String) i.next();
320:                        try {
321:                            MediaEntityLocal meb = getMebHome()
322:                                    .findByPrimaryKey(primaryKey);
323:                            mebs.add(meb);
324:                        } catch (FinderException e) {
325:                            // do nothing
326:                        }
327:                    }
328:
329:                    return (MediaEntityLocal[]) mebs
330:                            .toArray(new MediaEntityLocal[mebs.size()]);
331:
332:                } catch (NamingException e) {
333:                    throw new MediaSampleException(
334:                            "Unable to get MediaList due to " + e, e);
335:                }
336:
337:            }
338:
339:            /**
340:             * Get media from its premary key
341:             *
342:             * @param primaryKey the primary key of the wanted media
343:             * @return MediaEntity
344:             * @throws MediaSampleException if the mediaEntity cannot be retrive
345:             */
346:            public MediaEntityLocal getMediaFromPrimaryKey(String primaryKey)
347:                    throws MediaSampleException {
348:                try {
349:                    MediaEntityLocal meb = getMebHome().findByPrimaryKey(
350:                            primaryKey);
351:                    return meb;
352:                } catch (FinderException e) {
353:                    throw new MediaSampleException(
354:                            "Media with this primary key does not exist : "
355:                                    + primaryKey, e);
356:                } catch (NamingException e) {
357:                    throw new MediaSampleException(
358:                            "Unable to get Media due to " + e, e);
359:                }
360:            }
361:
362:            /**
363:             * Get media from registered name
364:             *
365:             * @param registeredName the registeredName of the wanted media
366:             * @return MediaEntity
367:             * @throws MediaSampleException if the mediaEntity cannot be retrive
368:             */
369:            public MediaEntityLocal getMediaFromRegistredName(
370:                    String registeredName) throws MediaSampleException {
371:
372:                String primaryKey = (String) mediaRegistredName
373:                        .get(registeredName);
374:                if (primaryKey != null) {
375:                    return getMediaFromPrimaryKey(primaryKey);
376:                }
377:                throw new MediaSampleException(
378:                        "unable to find Media with registered name : "
379:                                + registeredName);
380:
381:            }
382:
383:            /**
384:             * Remove default media to reinit the database, except uploaded media
385:             */
386:            public void removeDefaultMedia() {
387:
388:                // copy the primary list
389:                String[] primaryKeyToRemove = (String[]) mediaListAllCreated
390:                        .toArray(new String[mediaListAllCreated.size()]);
391:
392:                // remove al creating media during this session
393:                for (int i = 0; i < primaryKeyToRemove.length; i++) {
394:
395:                    if (!mediaListUpload.contains(primaryKeyToRemove[i])) {
396:
397:                        try {
398:                            MediaEntityLocal meb = getMediaFromPrimaryKey(primaryKeyToRemove[i]);
399:                            removeMedia(meb);
400:                        } catch (MediaSampleException e) {
401:                            // do nothing
402:                        }
403:                    }
404:                }
405:            }
406:
407:            // =======================================================================
408:            // Internals
409:            // =======================================================================
410:
411:            /**
412:             * Add meb to the right list
413:             *
414:             * @param meb the media to add
415:             * @param status where the medi is added, is ones of {@link #DONT_SHOW},
416:             *            {@link #SHOW_IN_LIST}, {@link #USER_ADDED}
417:             */
418:            private synchronized void addMedia(MediaEntityLocal meb, byte status) {
419:
420:                mediaListAllCreated.add(meb.getPrimaryKey());
421:                if (status == MediaSampleSessionLocal.SHOW_IN_LIST) {
422:                    mediaListStdShow.add(meb.getPrimaryKey());
423:                } else if (status == MediaSampleSessionLocal.USER_ADDED) {
424:                    mediaListUpload.add(meb.getPrimaryKey());
425:                }
426:
427:            }
428:
429:            /**
430:             * Remove media from all media list
431:             *
432:             * @param meb the media to add
433:             */
434:            private synchronized void removeMedia(MediaEntityLocal meb) {
435:
436:                recusiveRemoveMedia(meb);
437:
438:                mediaListAllCreated.remove(meb.getPrimaryKey());
439:                mediaListStdShow.remove(meb.getPrimaryKey());
440:                mediaListUpload.remove(meb.getPrimaryKey());
441:
442:            }
443:
444:            /**
445:             * Remove recurcively all preview version and children
446:             *
447:             * @param meb the media entity bean
448:             */
449:            private void recusiveRemoveMedia(MediaEntityLocal meb) {
450:                if (meb != null) {
451:
452:                    // recurcive call for children
453:
454:                    MediaEntityLocal[] children = meb.getChildren();
455:                    for (int i = 0; i < children.length; i++) {
456:                        recusiveRemoveMedia(children[i]);
457:                    }
458:
459:                    // recurcive call for preview version
460:                    recusiveRemoveMedia(meb.getPreviousVersion());
461:
462:                    try {
463:                        meb.remove();
464:                    } catch (RemoveException e) {
465:                        // do nothing
466:                    }
467:                }
468:            }
469:
470:            /**
471:             * Get the iscopy allow
472:             *
473:             * @return if copy is allow
474:             */
475:            private boolean getCopyAllowed() {
476:
477:                Boolean cache = isCopyAllowed;
478:
479:                if (cache == null) {
480:                    try {
481:                        cache = (Boolean) (new InitialContext())
482:                                .lookup(IS_COPY_ALLOWED_REF);
483:                        isCopyAllowed = cache;
484:
485:                    } catch (NamingException e) {
486:                        return true;
487:                    }
488:                }
489:                return cache.booleanValue();
490:            }
491:
492:            /**
493:             * Get MaxMediaPerSession
494:             *
495:             * @return maxmedia per session
496:             */
497:            private int getMaxMediaPerSession() {
498:
499:                Integer cache = maxMediaPerSession;
500:
501:                if (cache == null) {
502:                    try {
503:                        cache = (Integer) (new InitialContext())
504:                                .lookup(MAX_MEDIA_PER_SESSION_REF);
505:                        maxMediaPerSession = cache;
506:
507:                    } catch (NamingException e) {
508:                        return 0;
509:                    }
510:                }
511:                return cache.intValue();
512:            }
513:
514:            /**
515:             * test if MaxMediaPerSession has been reach
516:             *
517:             * @throws MediaSampleException if MaxMediaPerSession has been reach
518:             */
519:            private void checkMaxMedia() throws MediaSampleException {
520:
521:                if (!(getMaxMediaPerSession() == 0)) {
522:                    if (mediaListAllCreated.size() >= getMaxMediaPerSession() - 1) {
523:                        throw new MediaSampleException(
524:                                "You have reach the maximum number per session. You cannot create more media.");
525:                    }
526:                }
527:
528:            }
529:
530:            // =======================================================================
531:            // TOOLS
532:            // =======================================================================
533:
534:            /**
535:             * cached Home
536:             */
537:            private MediaEntityLocalHome mebHome = null;
538:
539:            /**
540:             * Provide a cached Home access
541:             *
542:             * @return MediaEntityLocalHome
543:             * @throws NamingException in case MediaEntityLocalHome is not found
544:             */
545:            private MediaEntityLocalHome getMebHome() throws NamingException {
546:                if (mebHome == null) {
547:                    mebHome = (MediaEntityLocalHome) new InitialContext()
548:                            .lookup(MEB_HOME_REF);
549:                }
550:                return mebHome;
551:            }
552:
553:            // =======================================================================
554:            // EJB contract
555:            // =======================================================================
556:
557:            /**
558:             * Session Context
559:             */
560:            private SessionContext mySessionCtx;
561:
562:            /**
563:             * getSessionContext
564:             *
565:             * @return SessionContext
566:             */
567:            public SessionContext getSessionContext() {
568:                return mySessionCtx;
569:            }
570:
571:            /**
572:             * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
573:             * @inheritDoc
574:             */
575:            public void setSessionContext(SessionContext ctx) {
576:                mySessionCtx = ctx;
577:            }
578:
579:            /**
580:             * @see javax.ejb.SessionBean#ejbActivate()
581:             * @inheritDoc
582:             */
583:            public void ejbActivate() {
584:                // do nothing
585:            }
586:
587:            /**
588:             * @see javax.ejb.SessionBean#ejbPassivate()
589:             * @inheritDoc
590:             */
591:            public void ejbPassivate() {
592:                // do nothing
593:            }
594:
595:            /**
596:             * @see javax.ejb.SessionBean#ejbRemove()
597:             * @inheritDoc
598:             */
599:            public void ejbRemove() {
600:
601:                // copy the primary list
602:                String[] primaryKeyToRemove = (String[]) mediaListAllCreated
603:                        .toArray(new String[mediaListAllCreated.size()]);
604:
605:                // remove al creating media during this session
606:                for (int i = 0; i < primaryKeyToRemove.length; i++) {
607:                    try {
608:                        MediaEntityLocal meb = getMediaFromPrimaryKey(primaryKeyToRemove[i]);
609:                        removeMedia(meb);
610:                    } catch (MediaSampleException e) {
611:                        // do nothing
612:                    }
613:                }
614:
615:            }
616:
617:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.