Source Code Cross Referenced for SessionMailerBean.java in  » J2EE » JOnAS-4.8.6 » mailsb » 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 » mailsb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2004 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): Florent BENOIT & Ludovic BERT
022:         * --------------------------------------------------------------------------
023:         * $Id: SessionMailerBean.java 4618 2004-04-19 06:39:30Z benoitf $
024:         * --------------------------------------------------------------------------
025:         */package mailsb;
026:
027:        //import java
028:        import java.rmi.RemoteException;
029:
030:        //import javax
031:        import javax.ejb.EJBException;
032:        import javax.ejb.SessionBean;
033:        import javax.ejb.SessionContext;
034:
035:        import javax.mail.Message;
036:        import javax.mail.MessagingException;
037:        import javax.mail.Session;
038:        import javax.mail.Transport;
039:        import javax.mail.internet.InternetAddress;
040:        import javax.mail.internet.MimeMessage;
041:
042:        import javax.naming.InitialContext;
043:        import javax.naming.NamingException;
044:
045:        /**
046:         * Implementation of the mailer Session Bean. It uses javax.mail.Session This
047:         * bean is a statefull, and synchronized bean. The container uses the
048:         * SessionBean methods to notify the enterprise Bean instances of the instance's
049:         * life cycle events.
050:         * @author Florent Benoit
051:         * @author Ludovic Bert
052:         */
053:        public class SessionMailerBean implements  SessionBean {
054:
055:            /**
056:             * Name of the bean
057:             */
058:            private String name = null;
059:
060:            /**
061:             * Session context that the container provides for a session enterprise Bean
062:             * instance.
063:             */
064:            private SessionContext sessionContext = null;
065:
066:            /**
067:             * Reference to the javax.mail.Message object which is the message to send
068:             * with javamail
069:             */
070:            private Message message = null;
071:
072:            /* ========================= ejbCreate methods =========================== */
073:
074:            /**
075:             * There must be one ejbCreate() method per create() method on the Home
076:             * interface, and with the same signature.
077:             * @param name the name of the bean
078:             */
079:            public void ejbCreate(String name) {
080:                this .name = name;
081:            }
082:
083:            /* =============== javax.ejb.SessionBean 2.0 implementation ============== */
084:
085:            /**
086:             * Set the associated session context. The container calls this method after
087:             * the instance creation. The enterprise Bean instance should store the
088:             * reference to the context object in an instance variable. This method is
089:             * called with no transaction context.
090:             * @param sessionContext A SessionContext interface for the instance.
091:             * @throws EJBException Thrown by the method to indicate a failure caused by
092:             *         a system-level error.
093:             * @throws java.rmi.RemoteException This exception is defined in the method
094:             *         signature to provide backward compatibility for applications
095:             *         written for the EJB 1.0 specification. Enterprise beans written
096:             *         for the EJB 1.1 specification should throw the
097:             *         javax.ejb.EJBException instead of this exception. Enterprise
098:             *         beans written for the EJB2.0 and higher specifications must throw
099:             *         the javax.ejb.EJBException instead of this exception.
100:             */
101:            public void setSessionContext(SessionContext sessionContext)
102:                    throws EJBException, java.rmi.RemoteException {
103:                this .sessionContext = sessionContext;
104:            }
105:
106:            /**
107:             * A container invokes this method before it ends the life of the session
108:             * object. This happens as a result of a client's invoking a remove
109:             * operation, or when a container decides to terminate the session object
110:             * after a timeout. This method is called with no transaction context.
111:             * @throws EJBException Thrown by the method to indicate a failure caused by
112:             *         a system-level error.
113:             * @throws java.rmi.RemoteException This exception is defined in the method
114:             *         signature to provide backward compatibility for enterprise beans
115:             *         written for the EJB 1.0 specification. Enterprise beans written
116:             *         for the EJB 1.1 specification should throw the
117:             *         javax.ejb.EJBException instead of this exception. Enterprise
118:             *         beans written for the EJB2.0 and higher specifications must throw
119:             *         the javax.ejb.EJBException instead of this exception.
120:             */
121:            public void ejbRemove() throws EJBException,
122:                    java.rmi.RemoteException {
123:                // Nothing to do for this simple mailer example
124:            }
125:
126:            /**
127:             * The activate method is called when the instance is activated from its
128:             * "passive" state. The instance should acquire any resource that it has
129:             * released earlier in the ejbPassivate() method. This method is called with
130:             * no transaction context.
131:             * @throws EJBException Thrown by the method to indicate a failure caused by
132:             *         a system-level error.
133:             * @throws java.rmi.RemoteException This exception is defined in the method
134:             *         signature to provide backward compatibility for enterprise beans
135:             *         written for the EJB 1.0 specification. Enterprise beans written
136:             *         for the EJB 1.1 specification should throw the
137:             *         javax.ejb.EJBException instead of this exception. Enterprise
138:             *         beans written for the EJB2.0 and higher specifications must throw
139:             *         the javax.ejb.EJBException instead of this exception.
140:             */
141:            public void ejbActivate() throws EJBException,
142:                    java.rmi.RemoteException {
143:                // Nothing to do for this simple mailer example
144:            }
145:
146:            /**
147:             * The passivate method is called before the instance enters the "passive"
148:             * state. The instance should release any resources that it can re-acquire
149:             * later in the ejbActivate() method. After the passivate method completes,
150:             * the instance must be in a state that allows the container to use the Java
151:             * Serialization protocol to externalize and store away the instance's
152:             * state. This method is called with no transaction context.
153:             * @throws EJBException Thrown by the method to indicate a failure caused by
154:             *         a system-level error.
155:             * @throws java.rmi.RemoteException This exception is defined in the method
156:             *         signature to provide backward compatibility for enterprise beans
157:             *         written for the EJB 1.0 specification. Enterprise beans written
158:             *         for the EJB 1.1 specification should throw the
159:             *         javax.ejb.EJBException instead of this exception. Enterprise
160:             *         beans written for the EJB2.0 and higher specifications must throw
161:             *         the javax.ejb.EJBException instead of this exception.
162:             */
163:            public void ejbPassivate() throws EJBException,
164:                    java.rmi.RemoteException {
165:                // Nothing to do for this simple mailer example
166:            }
167:
168:            /* ======================== Mailer implementation ======================== */
169:
170:            /**
171:             * Set the message with a specific recipient, subject and content.
172:             * @param recipient the 'TO' field of the message.
173:             * @param subject the subject of the message.
174:             * @param content the content of the message.
175:             * @throws Exception if a problem occurs.
176:             * @throws RemoteException if the call failed.
177:             */
178:            public void setMessage(String recipient, String subject,
179:                    String content) throws Exception, RemoteException {
180:
181:                //Get the initial context
182:                InitialContext ictx = null;
183:                try {
184:                    ictx = new InitialContext();
185:                } catch (NamingException e) {
186:                    throw new Exception("Can not get an inital context : "
187:                            + e.getMessage());
188:                }
189:
190:                //get a new Session from our ENC envirnoment java:comp/env
191:                Session session = null;
192:                try {
193:                    session = (Session) ictx
194:                            .lookup("java:comp/env/mail/MailSession");
195:                } catch (NamingException e) {
196:                    throw new Exception(
197:                            "You have not configure the mail factory with the name specified"
198:                                    + " in the jonas-ejb-jar.xml file for java:comp/env/mail/MailSession ."
199:                                    + " By default, the factory's name is mailSession_1 :"
200:                                    + e.getMessage());
201:                }
202:
203:                try {
204:                    //Create the message
205:                    message = new MimeMessage(session);
206:                    InternetAddress[] toRecipients = new InternetAddress[] { new InternetAddress(
207:                            recipient) };
208:                    /*
209:                     * try { toRecipients[0].validate(); } catch (AddressException e) {
210:                     * throw new Exception("A failure occurs when validating the email
211:                     * address '" + recipient + "' :" + e.getMessage()); }
212:                     */
213:                    message.setRecipients(Message.RecipientType.TO,
214:                            toRecipients);
215:                    message.setSubject(subject);
216:                    message.setContent(content, "text/plain");
217:                } catch (MessagingException e) {
218:                    throw new Exception(
219:                            "A failure occurs when getting a message from the session and setting "
220:                                    + "the different parameters :"
221:                                    + e.getMessage());
222:                }
223:
224:            }
225:
226:            /**
227:             * Send the message which was previously configured.
228:             * @throws Exception if a problem occurs.
229:             * @throws RemoteException if the send failed.
230:             */
231:            public void send() throws Exception, RemoteException {
232:
233:                if (message == null) {
234:                    throw new Exception(
235:                            "The message can not be send because the method setMessage() "
236:                                    + " was not called before the send() method.");
237:                }
238:
239:                try {
240:                    Transport.send(message);
241:                } catch (MessagingException e) {
242:                    throw new Exception("The message can not be send : "
243:                            + e.getMessage());
244:                }
245:            }
246:
247:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.