Source Code Cross Referenced for MailServiceImpl.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » mail » 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 » org.objectweb.jonas.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999 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: MailServiceImpl.java 8366 2006-05-18 08:43:07Z danesa $
024:         * --------------------------------------------------------------------------
025:         */package org.objectweb.jonas.mail;
026:
027:        import java.io.FileNotFoundException;
028:        import java.util.Enumeration;
029:        import java.util.Hashtable;
030:        import java.util.List;
031:        import java.util.Properties;
032:        import java.util.StringTokenizer;
033:        import java.util.Vector;
034:
035:        import javax.management.JMException;
036:        import javax.management.MBeanServer;
037:        import javax.management.ObjectName;
038:        import javax.management.modelmbean.ModelMBean;
039:        import javax.naming.Context;
040:        import javax.naming.InitialContext;
041:        import javax.naming.NamingException;
042:
043:        import org.apache.commons.modeler.ManagedBean;
044:        import org.apache.commons.modeler.Registry;
045:        import org.objectweb.jonas.common.JModule;
046:        import org.objectweb.jonas.common.JProp;
047:        import org.objectweb.jonas.common.Log;
048:        import org.objectweb.jonas.common.PropDump;
049:        import org.objectweb.jonas.jmx.J2eeObjectName;
050:        import org.objectweb.jonas.jmx.JmxService;
051:        import org.objectweb.jonas.jmx.JonasObjectName;
052:        import org.objectweb.jonas.mail.factory.JavaMail;
053:        import org.objectweb.jonas.mail.factory.JavaMailMimePartDS;
054:        import org.objectweb.jonas.mail.factory.JavaMailMimePartDSResource;
055:        import org.objectweb.jonas.mail.factory.JavaMailSession;
056:        import org.objectweb.jonas.mail.factory.JavaMailSessionResource;
057:        import org.objectweb.jonas.management.JonasMBeanTools;
058:        import org.objectweb.jonas.service.AbsServiceImpl;
059:        import org.objectweb.jonas.service.ServiceException;
060:        import org.objectweb.jonas.service.ServiceManager;
061:        import org.objectweb.util.monolog.api.BasicLevel;
062:        import org.objectweb.util.monolog.api.Logger;
063:
064:        /**
065:         * This class provides an implementation of the javaMail service.
066:         * @author Florent Benoit
067:         * @author Ludovic Bert
068:         * Contributor(s):
069:         * Adriana Danes :
070:         * - Make possible to change configuration of a JOnAS mail factory object.
071:         * - Make possible to change the JNDI name of a JOnAS mail factory object.
072:         */
073:        public class MailServiceImpl extends AbsServiceImpl implements 
074:                MailService, MailServiceImplMBean {
075:
076:            /**
077:             * The list of the registered mail session factories.
078:             */
079:            private Vector mailSessionList = null;
080:
081:            /**
082:             * The list of the registered mime part data source factories.
083:             */
084:            private Vector mailMimePartDSList = null;
085:
086:            /**
087:             * Reference to a MBean server.
088:             */
089:            private MBeanServer mbeanServer = null;
090:
091:            /**
092:             * Initial Context for Naming
093:             */
094:            private Context ictx = null;
095:
096:            // the name of the JOnAS server that is administerd by the MBeanServer
097:            // the current management domain's name
098:            protected String serverName = null;
099:            protected String domainName = null;
100:
101:            /**
102:             * Logger for this service.
103:             */
104:            private static Logger logger = null;
105:
106:            /**
107:             * List of the factories names to load when starting the MailService
108:             */
109:            private Vector factoryNames = new Vector();
110:
111:            /**
112:             *  List of the javax.mail.Session factories currently loaded
113:             */
114:            private Hashtable jMailSessionFactories = new Hashtable();
115:
116:            /**
117:             *  List of the javax.mail.internet.MimePartDataSource factories currently loaded
118:             */
119:            private Hashtable jMailMimePartDSFactories = new Hashtable();
120:
121:            /**
122:             *  List of the binded factories (jndi name -> factory name)
123:             *  This is needed by EJB components in order to find out the factory name
124:             *  based upon their jndi name
125:             */
126:            private Hashtable bindedFactories = new Hashtable();
127:
128:            /**
129:             * Session Factory
130:             */
131:            private static final int JAVAX_MAIL_SESSION_FACTORY = 1;
132:
133:            /**
134:             * MimepartDatasource Factory
135:             */
136:            private static final int JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE = 2;
137:
138:            /**
139:             * JOnAS-specific properties (name)
140:             */
141:            public static final String PROPERTY_NAME = "mail.factory.name";
142:
143:            /**
144:             * JOnAS-specific properties (type)
145:             */
146:            public static final String PROPERTY_TYPE = "mail.factory.type";
147:
148:            public static final String SESSION_PROPERTY_TYPE = "javax.mail.Session";
149:            public static final String MIMEPART_PROPERTY_TYPE = "javax.mail.internet.MimePartDataSource";
150:
151:            /**
152:             * Mail service configuration parameters (factories)
153:             */
154:            public static final String FACTORIES = "jonas.service.mail.factories";
155:
156:            /**
157:             * Mail service configuration parameters (class)
158:             */
159:            public static final String CLASS = "jonas.service.mail.class";
160:
161:            /**
162:             * Init the Mail service.
163:             * @param ctx the configuration of the Ear service.
164:             * @throws ServiceException if the initialization failed.
165:             */
166:            protected void doInit(Context ctx) throws ServiceException {
167:
168:                // get logger for this service
169:                logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
170:                // the logger for reconfig management
171:                super .initLogger(Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX));
172:
173:                //Get the inital Context
174:                try {
175:                    ictx = new InitialContext();
176:                } catch (NamingException e) {
177:                    logger
178:                            .log(BasicLevel.ERROR,
179:                                    "Cannot create initial context during the mail service initializing");
180:                    throw new ServiceException(
181:                            "Cannot create initial context during the mail service initializing",
182:                            e);
183:                }
184:
185:                // Get the current server and domain names
186:                try {
187:                    serverName = (String) ctx.lookup(JProp.JONAS_NAME);
188:                    domainName = (String) ctx.lookup(JProp.DOMAIN_NAME);
189:                } catch (NamingException ne) {
190:                    logger.log(BasicLevel.DEBUG,
191:                            "Cannot initialize the Mail service " + ne);
192:                    throw new ServiceException(
193:                            "Cannot initialize the Mail service", ne);
194:                }
195:
196:                // Get the JMX Server via JMX Service
197:                try {
198:                    mbeanServer = ((JmxService) ServiceManager.getInstance()
199:                            .getJmxService()).getJmxServer();
200:                } catch (Exception e) {
201:                    // the JMX service may not be started
202:                    mbeanServer = null;
203:                }
204:
205:                // Get the list of the factory names
206:                String factories = null;
207:                try {
208:                    factories = (String) ctx.lookup(FACTORIES);
209:                } catch (NamingException e) {
210:                    ; // No problem if there is no value for 'factories'
211:                }
212:                if (factories != null) {
213:                    StringTokenizer st = new StringTokenizer(factories, ",");
214:                    while (st.hasMoreTokens()) {
215:                        factoryNames.add(st.nextToken().trim());
216:                    }
217:                }
218:                if (logger.isLoggable(BasicLevel.DEBUG)) {
219:                    logger.log(BasicLevel.DEBUG, "Mail service initialized");
220:                }
221:
222:            }
223:
224:            /**
225:             * Start the Mail Service.
226:             * @throws ServiceException if the initialization failed.
227:             */
228:            protected void doStart() throws ServiceException {
229:
230:                // creates each factory
231:                String factoryName = null;
232:                for (int i = 0; i < factoryNames.size(); i++) {
233:                    factoryName = (String) factoryNames.elementAt(i);
234:                    try {
235:                        JProp prop = JProp.getInstance(factoryName);
236:                        if (logger.isLoggable(BasicLevel.DEBUG)) {
237:                            logger.log(BasicLevel.DEBUG,
238:                                    "Creating mail factory " + factoryName);
239:                        }
240:                        createMailFactory(factoryName, prop.getConfigFileEnv());
241:                    } catch (Exception e) {
242:                        if (logger.isLoggable(BasicLevel.ERROR)) {
243:                            logger.log(BasicLevel.ERROR,
244:                                    "JOnAS: Cannot create mail factory "
245:                                            + factoryName + " : " + e);
246:                            logger.log(BasicLevel.ERROR, "Please check the "
247:                                    + factoryName + ".properties file");
248:                        }
249:                    }
250:                }
251:
252:                try {
253:                    // Register MailService MBean : MailServiceImplMBean
254:                    if (mbeanServer != null) {
255:                        mbeanServer.registerMBean(this , JonasObjectName
256:                                .mailService());
257:                    }
258:                } catch (JMException e) {
259:                    throw new ServiceException(
260:                            "Cannot start the MailService: ", e);
261:                }
262:
263:            }
264:
265:            /**
266:             * Stop the Mail service.
267:             * @throws ServiceException if the stop failed.
268:             */
269:            protected void doStop() throws ServiceException {
270:
271:                try {
272:                    unbindMailFactories();
273:                } catch (MailServiceException e) {
274:                    logger.log(BasicLevel.ERROR,
275:                            "Cannot unbind mail factories " + e);
276:                    throw new ServiceException("Cannot unbind mail factories ",
277:                            e);
278:                }
279:
280:                if (mbeanServer != null) {
281:                    try {
282:                        mbeanServer.unregisterMBean(JonasObjectName
283:                                .mailService());
284:                        // unregister all mail factories
285:                    } catch (Exception e) {
286:                        logger.log(BasicLevel.ERROR,
287:                                "Cannot stop the Mail Service (JMX): "
288:                                        + e.getMessage());
289:                        throw new ServiceException(
290:                                "Cannot stop the mail service (JMX).", e);
291:                    }
292:                }
293:                if (logger.isLoggable(BasicLevel.DEBUG)) {
294:                    logger.log(BasicLevel.DEBUG, "mail service stopped");
295:                }
296:            }
297:
298:            /**
299:             * This method is used when a Mail Factory configuration is modified via jonasAdmin.
300:             * In this case, the updated JavaMail object (JavaMailSession or JavaMailMimePartDS object)
301:             * must be rebound in JNDI
302:             * @param factory the factory
303:             * @throws MailServiceException if the recreation of
304:             * the factory failed.
305:             */
306:            public void recreateJavaMailFactory(JavaMail factory)
307:                    throws MailServiceException {
308:                String jndiName = factory.getName();
309:                // Rebind the factory object in the naming context
310:                try {
311:                    ictx.rebind(jndiName, factory);
312:                } catch (NamingException e) {
313:                    logger.log(BasicLevel.ERROR, "Cannot bind mail factory '"
314:                            + jndiName + "'  :" + e.getMessage());
315:                    throw new MailServiceException("Cannot bind mail factory "
316:                            + jndiName + ".", e);
317:                }
318:            }
319:
320:            /**
321:             * This method is used when a particular Mail Factory configuration operation is done via jonasAdmin :
322:             * when the JNDI name of this resource is modified.
323:             * In this case, the initial JavaMail object (JavaMailSession or JavaMailMimePartDS object) must be unbound
324:             * and the updated JavaMail object must be reloaded.
325:             * Also, the Mail Service private data structures must be updated.
326:             * @param oldName old name of the factory
327:             * @param factory the new factory
328:             * @throws MailServiceException if the rename of the
329:             * the factory failed.
330:             */
331:            public void renameJavaMailFactory(String oldName, JavaMail factory)
332:                    throws MailServiceException {
333:
334:                if (logger.isLoggable(BasicLevel.DEBUG)) {
335:                    logger.log(BasicLevel.DEBUG,
336:                            "In renameMailFactory, old name = " + oldName);
337:                }
338:                try {
339:                    ictx.unbind(oldName);
340:                    if (logger.isLoggable(BasicLevel.DEBUG)) {
341:                        logger.log(BasicLevel.DEBUG, oldName + " unbound");
342:                    }
343:                } catch (Exception e) {
344:                    if (logger.isLoggable(BasicLevel.DEBUG)) {
345:                        logger.log(BasicLevel.DEBUG,
346:                                "Warning: cannot unbind mail factory object named "
347:                                        + oldName);
348:                    }
349:                }
350:                String jndiName = factory.getName();
351:                // Rebind the factory object in the naming context
352:                try {
353:                    ictx.rebind(jndiName, factory);
354:                    if (logger.isLoggable(BasicLevel.DEBUG)) {
355:                        logger.log(BasicLevel.DEBUG,
356:                                "factory rebound under the name " + jndiName);
357:                    }
358:                } catch (NamingException e) {
359:                    logger.log(BasicLevel.ERROR, "Cannot bind mail factory '"
360:                            + jndiName + "'  :" + e.getMessage());
361:                    throw new MailServiceException("Cannot bind mail factory "
362:                            + jndiName + ".", e);
363:                }
364:                bindedFactories.put(jndiName, factory.getFactoryName());
365:                bindedFactories.remove(oldName);
366:            }
367:
368:            /**
369:             * Create a mail factory with the specified properties and register it
370:             * into the registry.
371:             * @param factoryName name of the factory to create
372:             * @param props the properties used to configure the mail factory.
373:             * @throws MailServiceException if the creation or the registration of
374:             * the factory failed.
375:             */
376:            public void createMailFactory(String factoryName, Properties props)
377:                    throws MailServiceException {
378:
379:                if (logger.isLoggable(BasicLevel.DEBUG)) {
380:                    PropDump
381:                            .print(
382:                                    "These are the properties from which the MailService picks to construct Mail Factories",
383:                                    props, logger, BasicLevel.DEBUG);
384:                }
385:
386:                Object factory = null;
387:
388:                //Factory type/jndi name must be non null
389:                String factoryType = props.getProperty(PROPERTY_TYPE);
390:                String jndiName = props.getProperty(PROPERTY_NAME);
391:
392:                if (jndiName == null) {
393:                    logger
394:                            .log(BasicLevel.ERROR,
395:                                    "The property 'mail.factory.name' is a required property.");
396:                    throw new MailServiceException(
397:                            "The property 'mail.factory.name' is a required property for this factory.");
398:                }
399:
400:                if (factoryType == null) {
401:                    logger
402:                            .log(BasicLevel.ERROR,
403:                                    "The property 'mail.factory.type' is a required property.");
404:                    throw new MailServiceException(
405:                            "The property 'mail.factory.type' is a required property for this factory.");
406:                }
407:
408:                // Verify that jndi name not already used
409:                if (bindedFactories.containsKey(jndiName)) {
410:                    logger.log(BasicLevel.ERROR,
411:                            "There is already a factory bound with the name "
412:                                    + jndiName);
413:                    throw new MailServiceException(
414:                            "There is already a factory bound with the name '"
415:                                    + jndiName
416:                                    + "', please correct the provided configuration properties");
417:                }
418:
419:                //Define the type of our factory
420:                int typeOfFactory;
421:                if (factoryType.equalsIgnoreCase("javax.mail.Session")) {
422:                    typeOfFactory = JAVAX_MAIL_SESSION_FACTORY;
423:                } else if (factoryType
424:                        .equalsIgnoreCase("javax.mail.internet.MimePartDataSource")) {
425:                    typeOfFactory = JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE;
426:                } else {
427:                    typeOfFactory = 0;
428:                }
429:
430:                // Create the factory object and register it in the internal data structure
431:                switch (typeOfFactory) {
432:
433:                case JAVAX_MAIL_SESSION_FACTORY:
434:                    JavaMailSession sessionFactory = new JavaMailSession(
435:                            factoryName, jndiName, props);
436:                    jMailSessionFactories.put(factoryName, sessionFactory);
437:                    factory = sessionFactory;
438:                    break;
439:
440:                case JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE:
441:                    JavaMailMimePartDS mimeFactory = new JavaMailMimePartDS(
442:                            factoryName, jndiName, props);
443:                    jMailMimePartDSFactories.put(factoryName, mimeFactory);
444:                    factory = mimeFactory;
445:                    break;
446:
447:                default:
448:                    throw new MailServiceException(
449:                            "Can not create a factory of the type '"
450:                                    + factoryType
451:                                    + "'. This type is incorrect.");
452:                }
453:
454:                // Bind the factory object in the naming context
455:                try {
456:                    ictx.rebind(jndiName, factory);
457:                    bindedFactories.put(jndiName, factoryName);
458:                } catch (NamingException e) {
459:                    logger.log(BasicLevel.ERROR, "Cannot bind mail factory '"
460:                            + jndiName + "'  :" + e.getMessage());
461:                    throw new MailServiceException("Cannot bind mail factory "
462:                            + jndiName + ".", e);
463:                }
464:
465:                logger.log(BasicLevel.INFO, "Mapping Mail Factory "
466:                        + factoryType + " on " + jndiName);
467:
468:                // Register the factory object as an MBean with the jmx server
469:                // Get the MBean register (and load mbeans-descriptors)
470:                Registry oRegistry = JonasMBeanTools.getRegistry();
471:
472:                // Register the factory object as an MBean with the jmx server
473:                try {
474:                    if (mbeanServer != null) {
475:                        ObjectName on = null;
476:                        switch (typeOfFactory) {
477:                        case JAVAX_MAIL_SESSION_FACTORY:
478:                            // J2EEManagement
479:
480:                            on = J2eeObjectName.JavaMailResource(domainName,
481:                                    factoryName, serverName,
482:                                    SESSION_PROPERTY_TYPE);
483:                            JavaMailSessionResource javaMailSessionResource = new JavaMailSessionResource(
484:                                    on.toString(), false, false, false,
485:                                    (JavaMailSession) factory);
486:                            // JSR77
487:                            ManagedBean oManaged = oRegistry
488:                                    .findManagedBean("JavaMailSessionResource");
489:                            ModelMBean oMBean = oManaged
490:                                    .createMBean(javaMailSessionResource);
491:                            mbeanServer.registerMBean(oMBean, on);
492:                            //mbeanServer.registerMBean(s_mbean, on);
493:                            if (logger.isLoggable(BasicLevel.DEBUG)) {
494:                                logger.log(BasicLevel.DEBUG,
495:                                        "Register session mail factory with name "
496:                                                + factoryName);
497:                            }
498:                            break;
499:                        case JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE:
500:                            // J2EEManagement
501:                            on = J2eeObjectName.JavaMailResource(domainName,
502:                                    factoryName, serverName,
503:                                    MIMEPART_PROPERTY_TYPE);
504:                            JavaMailMimePartDSResource javaMailMimePartDSResource = new JavaMailMimePartDSResource(
505:                                    on.toString(), false, false, false,
506:                                    (JavaMailMimePartDS) factory);
507:                            // JSR77
508:                            oManaged = oRegistry
509:                                    .findManagedBean("JavaMailMimePartDSResource");
510:                            oMBean = oManaged
511:                                    .createMBean(javaMailMimePartDSResource);
512:                            mbeanServer.registerMBean(oMBean, on);
513:                            if (logger.isLoggable(BasicLevel.DEBUG)) {
514:                                logger.log(BasicLevel.DEBUG,
515:                                        "Register mime mail factory with name "
516:                                                + factoryName);
517:                            }
518:                        }
519:                    }
520:                } catch (JMException me) {
521:                    throw new MailServiceException(
522:                            "Cannot register mail factory '" + factoryName
523:                                    + "' in JMX server", me);
524:                } catch (Exception e) {
525:                    logger.log(BasicLevel.WARN,
526:                            "Could not register JavaMailResource MBean");
527:                }
528:
529:            }
530:
531:            /**
532:             * Create a mail factory with the specified properties and register it
533:             * into the registry.
534:             * @param name the mail factory name
535:             * @param props the properties used to configure the mail factory.
536:             * @param loadFromFile true if the mail factory is loaded from a .properties file
537:             * @throws MailServiceException if the creation or the registration of
538:             * the factory failed.
539:             * @throws MailServiceException
540:             */
541:            public void createMailFactoryMBean(String name, Properties props,
542:                    Boolean loadFromFile) throws MailServiceException {
543:
544:                boolean fromFile = loadFromFile.booleanValue();
545:                if (!fromFile) {
546:                    try {
547:                        if (logger.isLoggable(BasicLevel.DEBUG)) {
548:                            logger
549:                                    .log(BasicLevel.DEBUG,
550:                                            "Call getInstance on JProp in order to create the properties file");
551:                        }
552:                        JProp.getInstance(name, props);
553:                    } catch (Exception e) {
554:                        logger
555:                                .log(
556:                                        BasicLevel.ERROR,
557:                                        "Cannot create mail factory "
558:                                                + name
559:                                                + " as cannot create properties file : "
560:                                                + e.toString());
561:                        throw new ServiceException(
562:                                "MailService: Cannot create mail factory "
563:                                        + name + ",\n" + e.toString());
564:                    }
565:                }
566:                //call the internal proc
567:                try {
568:                    createMailFactory(name, props);
569:                } catch (Exception e) {
570:                    logger.log(BasicLevel.ERROR, "Cannot create mail factory: "
571:                            + name);
572:                    throw new ServiceException(
573:                            "MailService: Cannot create mail factory: " + name
574:                                    + ",\n" + e.toString());
575:                }
576:            }
577:
578:            /**
579:             * Unregister all the binding factories on the server and in JMX Server.
580:             * @throws MailServiceException if the unregistration of the factories
581:             * failed.
582:             */
583:            public void unbindMailFactories() throws MailServiceException {
584:                //Unbind all factories
585:                for (Enumeration e = jMailSessionFactories.keys(); e
586:                        .hasMoreElements();) {
587:                    unbindMailFactoryMBean((String) e.nextElement());
588:                }
589:                for (Enumeration e = jMailMimePartDSFactories.keys(); e
590:                        .hasMoreElements();) {
591:                    unbindMailFactoryMBean((String) e.nextElement());
592:                }
593:            }
594:
595:            /**
596:             * Unregister the factory with the given name.
597:             * @param factoryName the name of the factory to unbind.
598:             * @throws MailServiceException if the unregistration of the factory
599:             * failed.
600:             */
601:            public void unbindMailFactoryMBean(String factoryName)
602:                    throws MailServiceException {
603:
604:                // determine the type of the factory and the jndi name
605:                String name = null;
606:                int typeOfFactory;
607:                JavaMailSession jmailSession = (JavaMailSession) jMailSessionFactories
608:                        .get(factoryName);
609:                JavaMailMimePartDS jMailMimePartDS = (JavaMailMimePartDS) jMailMimePartDSFactories
610:                        .get(factoryName);
611:                if (jmailSession != null) {
612:                    name = jmailSession.getName();
613:                    typeOfFactory = JAVAX_MAIL_SESSION_FACTORY;
614:                } else if (jMailMimePartDS != null) {
615:                    name = jMailMimePartDS.getName();
616:                    typeOfFactory = JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE;
617:                } else {
618:                    throw new MailServiceException(
619:                            "Can not unload the mail factory '" + factoryName
620:                                    + "' (this is not a known factory name");
621:                }
622:
623:                // unbind the factory
624:                try {
625:                    ictx.unbind(name);
626:                    bindedFactories.remove(name);
627:                } catch (NamingException e) {
628:                    throw new MailServiceException(
629:                            "Can not unbind the factory '" + name + "'.", e);
630:                }
631:
632:                // remove JProp instance corresponding to this datasource in order to
633:                // allow re-create in case its re-loaded
634:                JProp.removeInstance(name);
635:
636:                // De-register the factory object from the jmx server and remove the factory from the internal data structure
637:                try {
638:                    if (mbeanServer != null) {
639:                        ObjectName on = null;
640:                        switch (typeOfFactory) {
641:                        case JAVAX_MAIL_SESSION_FACTORY:
642:                            // J2EEManagement
643:                            on = J2eeObjectName.JavaMailResource(domainName,
644:                                    factoryName, serverName,
645:                                    SESSION_PROPERTY_TYPE);
646:                            mbeanServer.unregisterMBean(on);
647:                            if (logger.isLoggable(BasicLevel.DEBUG)) {
648:                                logger.log(BasicLevel.DEBUG,
649:                                        "Unregister session mail factory with name "
650:                                                + factoryName);
651:                            }
652:                            jMailSessionFactories.remove(factoryName);
653:                            break;
654:                        case JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE:
655:                            // J2EEManagement
656:                            on = J2eeObjectName.JavaMailResource(domainName,
657:                                    factoryName, serverName,
658:                                    MIMEPART_PROPERTY_TYPE);
659:                            mbeanServer.unregisterMBean(on);
660:                            if (logger.isLoggable(BasicLevel.DEBUG)) {
661:                                logger.log(BasicLevel.DEBUG,
662:                                        "Unregister mime mail factory with name "
663:                                                + factoryName);
664:                            }
665:                            jMailMimePartDSFactories.remove(factoryName);
666:                        }
667:                    }
668:                } catch (JMException me) {
669:                    throw new MailServiceException(
670:                            "Cannot unregister mail factory '" + factoryName
671:                                    + "' from the JMX server", me);
672:                }
673:
674:            }
675:
676:            /**
677:             * Gets the factory name given the jndi name. Null is returned if the given name is not binded.
678:             * @param jndiName the jndi name
679:             * @return the factory name given the jndi name. Null is returned if the given name is not binded.
680:             */
681:            public String getFactoryName(String jndiName) {
682:                return (String) bindedFactories.get(jndiName);
683:            }
684:
685:            /**
686:             * Gets the total number of mail factories available in JOnAS
687:             * @return Integer Total number of mail factories available in JOnAS
688:             */
689:            public Integer getCurrentNumberOfMailFactories() {
690:                return new Integer(jMailSessionFactories.size()
691:                        + jMailMimePartDSFactories.size());
692:            }
693:
694:            /**
695:             * Gets the number of Session mail factories available in JOnAS
696:             * @return Integer Number of Session mail factories available in JOnAS
697:             */
698:            public Integer getCurrentNumberOfSessionMailFactories() {
699:                return new Integer(jMailSessionFactories.size());
700:            }
701:
702:            /**
703:             * Gets the integer Number of internet
704:             * @return Integer Number of internet.MimePartDataSource mail factories available in JOnAS
705:             */
706:            public Integer getCurrentNumberOfMimeMailFactories() {
707:                return new Integer(jMailMimePartDSFactories.size());
708:            }
709:
710:            /**
711:             * Gets the mail factory configuration properties from a local file
712:             * @param configFile configuration to use
713:             * @return mail factory configuration properties from a local file
714:             * @throws Exception if it fails
715:             */
716:            public Properties getMailFactoryPropertiesFile(String configFile)
717:                    throws Exception {
718:                try {
719:                    return JProp.getInstance(configFile).getConfigFileEnv();
720:                } catch (Exception e) {
721:                    if (e instanceof  FileNotFoundException) {
722:                        logger
723:                                .log(
724:                                        BasicLevel.ERROR,
725:                                        "Please check if "
726:                                                + configFile
727:                                                + ".properties is available in JONAS_ROOT/config, HOME, or .");
728:                    } else {
729:                        logger
730:                                .log(BasicLevel.ERROR,
731:                                        "Error occured when reading file "
732:                                                + configFile);
733:                    }
734:                    throw e;
735:                }
736:            }
737:
738:            /**
739:             * MBean method:
740:             * @return the list of properties files describing mail factories found in JONAS_BASE/conf
741:             */
742:            public List getMailFactoryPropertiesFiles() throws Exception {
743:                return JModule.getMailFactoryPropsInDir();
744:            }
745:
746:            /**
747:             * MBean method:
748:             * @return the list of properties files describing mail factories found in JONAS_BASE/conf
749:             */
750:            public List getMimePartMailFactoryPropertiesFiles()
751:                    throws Exception {
752:                return JModule.getMailFactoryPropsInDir(MIMEPART_PROPERTY_TYPE);
753:            }
754:
755:            /**
756:             * MBean method:
757:             * @return the list of properties files describing mail factories found in JONAS_BASE/conf
758:             */
759:            public List getSessionMailFactoryPropertiesFiles() throws Exception {
760:                return JModule.getMailFactoryPropsInDir(SESSION_PROPERTY_TYPE);
761:            }
762:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.