/*
- * Copyright (C) 2017 Roland Häder
+ * Copyright (C) 2017, 2018 Free Software Foundation
*
* 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
package org.mxchange.jcoreee.bean;
import java.io.Serializable;
-import java.security.Principal;
-import java.text.MessageFormat;
-import java.util.Locale;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
import javax.faces.FacesException;
-import javax.faces.application.FacesMessage;
-import javax.faces.context.FacesContext;
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 org.mxchange.jcoreeelogger.beans.local.logger.Log;
-import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
/**
* A generic bean class
*/
private Connection connection;
- /**
- * Logger instance
- */
- @Log
- private LoggerBeanLocal loggerBeanLocal;
-
/**
* Message producer
*/
* need to send emails.
*/
protected BaseBean () {
- 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(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
- }
}
/**
Context context = new InitialContext();
// Get factory from JMS resource
- QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N
+ QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi);
// Lookup queue
- this.queue = (Queue) context.lookup(queueJndi); //NOI18N
+ this.queue = (Queue) context.lookup(queueJndi);
// Create connection
this.connection = connectionFactory.createConnection();
}
}
- /**
- * Determines principal's name or returns null if no principal (security) is
- * set.
- * <p>
- * @return Principal's name or null
- */
- protected String determinePrincipalName () {
- // Get principal
- Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
-
- // Init with null
- String principalName = null;
-
- // Is the principal set?
- if (userPrincipal instanceof Principal) {
- // Get principal's name
- principalName = userPrincipal.getName();
- }
-
- // Return it
- return principalName;
- }
-
-
- /**
- * Getter for loggerBeanLocal
- * <p>
- * @return Logger instance
- */
- protected LoggerBeanLocal getLoggerBeanLocal () {
- return this.loggerBeanLocal;
- }
-
/**
* Getter for configured message producer instance
* <p>
return this.session;
}
-
- /**
- * Sends given message to configured queue
- * <p>
- * @param message Message to send
- * <p>
- * @throws JMSException if something went wrong
- */
- protected void sendMessage (final ObjectMessage message) throws JMSException {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
-
- // The parameter should be valid
- if (null == message) {
- // Throw NPE
- throw new NullPointerException("message is null"); //NOI18N
- } else if (null == this.getMessageProducer()) {
- // Throw NPE again
- throw new NullPointerException("this.messageProvider is null"); //NOI18N
- }
-
- // Send it
- this.getMessageProducer().send(message);
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
- }
-
-
}