]> git.mxchange.org Git - jjobs-ejb.git/commitdiff
Continued with email delivery:
authorRoland Haeder <roland@mxchange.org>
Sun, 13 Mar 2016 17:31:12 +0000 (18:31 +0100)
committerRoland Haeder <roland@mxchange.org>
Sun, 13 Mar 2016 17:31:12 +0000 (18:31 +0100)
- Added EJB for email change mail delivery
- added needed jar(s)
- updated project files

lib/jmailer-ee.jar [new file with mode: 0644]
nbproject/build-impl.xml
nbproject/genfiles.properties
nbproject/project.properties
nbproject/project.xml
src/java/org/mxchange/jmailee/model/delivery/JobsEmailDeliveryMessageBean.java [new file with mode: 0644]
src/java/org/mxchange/jusercore/model/email_address/JobsEmailChangeSessionBean.java

diff --git a/lib/jmailer-ee.jar b/lib/jmailer-ee.jar
new file mode 100644 (file)
index 0000000..ec343a7
Binary files /dev/null and b/lib/jmailer-ee.jar differ
index e765d00b292d895372e5beb408794a408addf93f..29638bcf4af16f2ac56879602840605a59b57230 100644 (file)
@@ -836,6 +836,7 @@ exists or setup the property manually. For example like this:
         <copyfiles files="${file.reference.juser-core.jar}" todir="${build.classes.dir}"/>
         <copyfiles files="${file.reference.juser-lib.jar}" todir="${build.classes.dir}"/>
         <copyfiles files="${file.reference.jletter-lib.jar}" todir="${build.classes.dir}"/>
+        <copyfiles files="${file.reference.jmailer-ee.jar}" todir="${build.classes.dir}"/>
         <copyfiles files="${reference.jjobs-lib.jar}" todir="${build.classes.dir}"/>
     </target>
     <target if="is.server.weblogic" name="-library-inclusion-in-archive-weblogic">
@@ -849,9 +850,10 @@ exists or setup the property manually. For example like this:
         <basename file="${file.reference.juser-core.jar}" property="manifest.file.reference.juser-core.jar"/>
         <basename file="${file.reference.juser-lib.jar}" property="manifest.file.reference.juser-lib.jar"/>
         <basename file="${file.reference.jletter-lib.jar}" property="manifest.file.reference.jletter-lib.jar"/>
+        <basename file="${file.reference.jmailer-ee.jar}" property="manifest.file.reference.jmailer-ee.jar"/>
         <basename file="${reference.jjobs-lib.jar}" property="manifest.reference.jjobs-lib.jar"/>
         <manifest file="${build.ear.classes.dir}/META-INF/MANIFEST.MF" mode="update">
-            <attribute name="Extension-List" value="jar-1 jar-2 jar-3 jar-4 jar-5 jar-6 jar-7 jar-8 jar-9 jar-10 jar-11 "/>
+            <attribute name="Extension-List" value="jar-1 jar-2 jar-3 jar-4 jar-5 jar-6 jar-7 jar-8 jar-9 jar-10 jar-11 jar-12 "/>
             <attribute name="jar-1-Extension-Name" value="${manifest.file.reference.jcore.jar}"/>
             <attribute name="jar-2-Extension-Name" value="${manifest.file.reference.jcoreee.jar}"/>
             <attribute name="jar-3-Extension-Name" value="${manifest.file.reference.jcore-logger-lib.jar}"/>
@@ -862,7 +864,8 @@ exists or setup the property manually. For example like this:
             <attribute name="jar-8-Extension-Name" value="${manifest.file.reference.juser-core.jar}"/>
             <attribute name="jar-9-Extension-Name" value="${manifest.file.reference.juser-lib.jar}"/>
             <attribute name="jar-10-Extension-Name" value="${manifest.file.reference.jletter-lib.jar}"/>
-            <attribute name="jar-11-Extension-Name" value="${manifest.reference.jjobs-lib.jar}"/>
+            <attribute name="jar-11-Extension-Name" value="${manifest.file.reference.jmailer-ee.jar}"/>
+            <attribute name="jar-12-Extension-Name" value="${manifest.reference.jjobs-lib.jar}"/>
         </manifest>
     </target>
     <target depends="compile" name="library-inclusion-in-manifest">
@@ -876,6 +879,7 @@ exists or setup the property manually. For example like this:
         <copyfiles files="${file.reference.juser-core.jar}" todir="${dist.ear.dir}/lib"/>
         <copyfiles files="${file.reference.juser-lib.jar}" todir="${dist.ear.dir}/lib"/>
         <copyfiles files="${file.reference.jletter-lib.jar}" todir="${dist.ear.dir}/lib"/>
+        <copyfiles files="${file.reference.jmailer-ee.jar}" todir="${dist.ear.dir}/lib"/>
         <copyfiles files="${reference.jjobs-lib.jar}" todir="${dist.ear.dir}/lib"/>
         <manifest file="${build.ear.classes.dir}/META-INF/MANIFEST.MF" mode="update"/>
     </target>
index 06c57adce83d3854ba6d40541ead3da4ba77f395..9d653d134f1c83a1b2204584dd6d6961d769f8a9 100644 (file)
@@ -1,8 +1,8 @@
-build.xml.data.CRC32=1100511e
+build.xml.data.CRC32=8210263f
 build.xml.script.CRC32=9c0f08a9
 build.xml.stylesheet.CRC32=5910fda3@1.55.1
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
-nbproject/build-impl.xml.data.CRC32=1100511e
-nbproject/build-impl.xml.script.CRC32=866bcec0
+nbproject/build-impl.xml.data.CRC32=8210263f
+nbproject/build-impl.xml.script.CRC32=1069361c
 nbproject/build-impl.xml.stylesheet.CRC32=6096d939@1.55.1
index e3a440e50c91ec30325b794b1a480b8204453e75..9ff211d357ae7d7cef935a6bcd4cdea59a8dfaef 100644 (file)
@@ -28,6 +28,7 @@ file.reference.jcore.jar=lib/jcore.jar
 file.reference.jcoreee.jar=lib/jcoreee.jar
 file.reference.jcountry-core.jar=lib/jcountry-core.jar
 file.reference.jletter-lib.jar=lib/jletter-lib.jar
+file.reference.jmailer-ee.jar=lib/jmailer-ee.jar
 file.reference.jphone-core.jar=lib/jphone-core.jar
 file.reference.juser-core.jar=lib/juser-core.jar
 file.reference.juser-lib.jar=lib/juser-lib.jar
@@ -56,6 +57,7 @@ javac.classpath=\
     ${file.reference.juser-core.jar}:\
     ${file.reference.juser-lib.jar}:\
     ${file.reference.jletter-lib.jar}:\
+    ${file.reference.jmailer-ee.jar}:\
     ${reference.jjobs-lib.jar}
 javac.compilerargs=-Xlint:unchecked -Xlint:deprecation
 javac.debug=true
@@ -101,6 +103,7 @@ source.reference.jcore.jar=../jcore/src/
 source.reference.jcoreee.jar=../jcoreee/src/
 source.reference.jcountry-core.jar=../jcountry-core/src/
 source.reference.jletter-lib.jar=../jletter-lib/src/
+source.reference.jmailer-ee.jar=../jmailer-ee/src/
 source.reference.jphone-core.jar=../jphone-core/src/
 source.reference.juser-core.jar=../juser-core/src/
 source.reference.juser-lib.jar=../juser-lib/src/
index 8b607f6a53e18139ed59b17591ce00e896c4380c..4713c99d96806c9de23334d2b8a0924a07917c9b 100644 (file)
@@ -15,6 +15,7 @@
             <included-library dirs="200">file.reference.juser-core.jar</included-library>
             <included-library dirs="200">file.reference.juser-lib.jar</included-library>
             <included-library dirs="200">file.reference.jletter-lib.jar</included-library>
+            <included-library dirs="200">file.reference.jmailer-ee.jar</included-library>
             <included-library dirs="200">reference.jjobs-lib.jar</included-library>
             <source-roots>
                 <root id="src.dir"/>
diff --git a/src/java/org/mxchange/jmailee/model/delivery/JobsEmailDeliveryMessageBean.java b/src/java/org/mxchange/jmailee/model/delivery/JobsEmailDeliveryMessageBean.java
new file mode 100644 (file)
index 0000000..a912f53
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2016 quix0r
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jmailee.model.delivery;
+
+import java.io.Serializable;
+import java.text.MessageFormat;
+import java.util.Date;
+import javax.annotation.Resource;
+import javax.ejb.ActivationConfigProperty;
+import javax.ejb.MessageDriven;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.ObjectMessage;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcoreeelogger.beans.local.logger.Log;
+import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
+
+/**
+ * A message queue for sending out emails
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@MessageDriven (activationConfig = {
+       @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
+       @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
+})
+public class JobsEmailDeliveryMessageBean implements MessageListener {
+
+       /**
+        * Email session
+        */
+       @Resource (name = "jmail/jjobs")
+       private Session jmailjjobs;
+
+       /**
+        * Logger bean
+        */
+       @Log
+       private LoggerBeanLocal loggerBeanLocal;
+
+       /**
+        * Default constructor
+        */
+       public JobsEmailDeliveryMessageBean () {
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Lookup logger
+                       this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
+               } catch (final NamingException ex) {
+                       // Continue to throw
+                       throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
+               }
+       }
+
+       @Override
+       public void onMessage (final Message message) {
+               // Trace message
+               this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
+
+               // Is the message castable to ObjectMessage?
+               if (null == message) {
+                       // message is null
+                       throw new NullPointerException("message is null"); //NOI18N
+               } else if (!(message instanceof ObjectMessage)) {
+                       // Not castable
+                       throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
+               }
+
+               // Securely cast it
+               ObjectMessage objectMessage = (ObjectMessage) message;
+
+               // Init instance
+               Serializable object;
+
+               try {
+                       // Get object from it
+                       object = objectMessage.getObject();
+               } catch (final JMSException ex) {
+                       // Log exception ...
+                       this.loggerBeanLocal.logException(ex);
+
+                       // ... and don't continue
+                       return;
+               }
+
+               // Debug message
+               this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
+
+               // Does this object implement WrapableCheckout ?
+               if (null == object) {
+                       // object cannot be null
+                       throw new NullPointerException("object is null"); //NOI18N
+               } else if (!(object instanceof WrapableEmailDelivery)) {
+                       // Not proper interface used
+                       throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N
+               }
+
+               // Cast the object to the wrapper interface
+               WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
+
+               // Trace message
+               this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
+       }
+
+       /**
+        * Sends an email to given email address with subject line.
+        * <p>
+        * @param emailAddress Email address for recipient
+        * @param subjectLine  Subject line
+        * @param body         Body part
+        * <p>
+        * @throws NamingException    If the resource cannot be found
+        * @throws MessagingException If something happened on message delivery
+        */
+       private void sendMail (final String emailAddress, final String subjectLine, final String body) throws NamingException, MessagingException {
+               // Get MIME message instance
+               MimeMessage message = new MimeMessage(this.jmailjjobs);
+
+               // Set subject, recipients and body
+               message.setSubject(subjectLine);
+               message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress, true));
+               message.setSentDate(new Date());
+               message.setText(body);
+
+               // Directly send email
+               Transport.send(message);
+       }
+
+}
index 1a67eb186c0c63fc2dea262b105ea6bd1745de23..4b5fe414278f40768d4e446cba70e259b89d8dbd 100644 (file)
@@ -19,12 +19,26 @@ package org.mxchange.jusercore.model.email_address;
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
 import java.util.List;
+import javax.annotation.PostConstruct;
 import javax.ejb.EJB;
 import javax.ejb.EJBException;
 import javax.ejb.Stateless;
+import javax.faces.FacesException;
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.MessageProducer;
+import javax.jms.ObjectMessage;
+import javax.jms.Queue;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.Session;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.jmailee.model.delivery.DeliverableEmail;
+import org.mxchange.jmailee.model.delivery.Mailer;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 import org.mxchange.jusercore.model.user.UserUtils;
 
@@ -41,6 +55,31 @@ public class JobsEmailChangeSessionBean extends BaseDatabaseBean implements Emai
         */
        private static final long serialVersionUID = 182_698_165_971_548L;
 
+       /**
+        * Connection
+        */
+       private Connection connection;
+
+       /**
+        * Object message
+        */
+       private ObjectMessage message;
+
+       /**
+        * Message producer
+        */
+       private MessageProducer messageProducer;
+
+       /**
+        * Mailer message queue
+        */
+       private Queue queue;
+
+       /**
+        * Session instance
+        */
+       private Session session;
+
        /**
         * User bean
         */
@@ -73,30 +112,78 @@ public class JobsEmailChangeSessionBean extends BaseDatabaseBean implements Emai
        }
 
        @Override
-       public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailAddress) {
+       public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailChange) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: emailChange={0} - CALLED!", emailChange)); //NOI18N
 
                // Email address change should be valid
-               if (null == emailAddress) {
+               if (null == emailChange) {
                        // Abort here
-                       throw new NullPointerException("emailAddress is null"); //NOI18N
-               } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
+                       throw new NullPointerException("emailChange is null"); //NOI18N
+               } else if (emailChange.getEmailChangeUser() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N
+               } else if (emailChange.getEmailChangeUser().getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N
+               } else if (emailChange.getEmailChangeUser().getUserId() < 1) {
+                       // Not valid id
+                       throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N
+               } else if (!this.userBean.ifUserExists(emailChange.getEmailChangeUser())) {
                        // User does not exist
-                       throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
-               } else if (emailAddress.getEmailAddress().trim().isEmpty()) {
+                       throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailChange.getEmailChangeId())); //NOI18N
+               } else if (emailChange.getEmailAddress().trim().isEmpty()) {
                        // Email address is empty
-                       throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
-               } else if (this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) {
+                       throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //NOI18N
+               } else if (this.isEmailAddressEnqueued(emailChange.getEmailAddress())) {
                        // Email address is already enqueued
-                       throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailAddress.getEmailAddress())); //NOI18N
+                       throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailChange.getEmailAddress())); //NOI18N
                }
 
                // The email change is not (yet) there, add secure hash and "created" timestamp
-               emailAddress.setEmailChangeCreated(new GregorianCalendar());
-               this.generateSecureHash(emailAddress);
+               emailChange.setEmailChangeCreated(new GregorianCalendar());
+               this.generateSecureHash(emailChange);
 
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+               // Persist it
+               //this.getEntityManager().persist(emailChange);
+
+               // Get mailer instance
+               DeliverableEmail mailer = new Mailer();
+
+               // Send out email change
+               mailer.sendEmailChangeMail(this.messageProducer, this.message, emailChange);
+       }
+
+       /**
+        * Initialization of this bean
+        */
+       @PostConstruct
+       public void init () {
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Get factory from JMS resource
+                       QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jjobs-queue-factory"); //NOI18N
+
+                       // Lookup queue
+                       this.queue = (Queue) context.lookup("jms/jjobs-email-queue"); //NOI18N
+
+                       // Create connection
+                       this.connection = connectionFactory.createConnection();
+
+                       // Init session instance
+                       this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+                       // And message producer
+                       this.messageProducer = this.session.createProducer(this.queue);
+
+                       // Finally the message instance itself
+                       this.message = this.session.createObjectMessage();
+               } catch (final NamingException | JMSException e) {
+                       // Continued to throw
+                       throw new FacesException(e);
+               }
        }
 
        @Override