001: /*
002: * MessageService: The message service daemon
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ProducerImpl.java
020: */
021:
022: // the package path
023: package com.rift.coad.daemon.messageservice;
024:
025: // java imports
026: import java.rmi.Remote;
027: import java.rmi.RemoteException;
028: import java.util.ArrayList;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Date;
033: import java.util.Set;
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import javax.transaction.UserTransaction;
037: import javax.transaction.Status;
038:
039: // logging import
040: import org.apache.log4j.Logger;
041:
042: // message service imports
043: import com.rift.coad.lib.common.RandomGuid;
044: import com.rift.coad.lib.security.SessionManager;
045: import com.rift.coad.lib.security.ThreadPermissionSession;
046: import com.rift.coad.daemon.messageservice.message.MessageImpl;
047: import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
048: import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
049: import com.rift.coad.daemon.messageservice.message.MessageManagerFactory;
050: import com.rift.coad.daemon.messageservice.message.MessageManagerImpl;
051:
052: /**
053: * This object is responsible for producing new message and submitting them to
054: * the message service for processing.
055: *
056: * @author Brett Chaldecott
057: */
058: public class ProducerImpl implements Producer {
059:
060: // the logger reference
061: protected static Logger log = Logger.getLogger(ProducerImpl.class
062: .getName());
063:
064: // the private member variables
065: private Context context = null;
066: private String from = null;
067: private UserTransaction ut = null;
068:
069: /**
070: * Creates a new instance of ProducerImpl
071: *
072: * @param from The from address for the messages.
073: * @exception MessageServiceException
074: */
075: public ProducerImpl(String from) throws MessageServiceException {
076: try {
077: this .from = from;
078: context = new InitialContext();
079: ut = (UserTransaction) context
080: .lookup("java:comp/UserTransaction");
081: } catch (Exception ex) {
082: log.error("Failed to instanciate the producer : "
083: + ex.getMessage(), ex);
084: throw new MessageServiceException(
085: "Failed to instanciate the producer : "
086: + ex.getMessage(), ex);
087: }
088: }
089:
090: /**
091: * This method is responsible for creating a new text message for the
092: * message service.
093: *
094: * @return A newly created text message.
095: * @param type The type of message.
096: * @exception RemoteException
097: * @exception MessageServiceException
098: */
099: public TextMessage createTextMessage(int type)
100: throws RemoteException, MessageServiceException {
101: try {
102: ThreadPermissionSession session = SessionManager
103: .getInstance().getSession();
104: Set principals = new HashSet();
105: for (Iterator iter = session.getPrincipals().iterator(); iter
106: .hasNext();) {
107: principals.add(iter.next());
108: }
109:
110: TextMessageImpl textMessage = new TextMessageImpl(
111: RandomGuid.getInstance().getGuid(), new Date(), 0,
112: new Date(), session.getUser().getName(), session
113: .getUser().getSessionId(), new ArrayList(
114: principals), from, type,
115: Message.UNDELIVERED);
116: textMessage.setNextProcessDate(new Date());
117: return textMessage;
118: } catch (Exception ex) {
119: log.error("Failed to create the text message : "
120: + ex.getMessage(), ex);
121: throw new MessageServiceException(
122: "Failed to create the text message : "
123: + ex.getMessage(), ex);
124: }
125: }
126:
127: /**
128: * This method is responsible for creating a new RPC message for the
129: * message service.
130: *
131: * @return A newly created text message.
132: * @param type The type of message.
133: * @exception RemoteException
134: * @exception MessageServiceException
135: */
136: public RPCMessage createRPCMessage(int type)
137: throws RemoteException, MessageServiceException {
138: try {
139: ThreadPermissionSession session = SessionManager
140: .getInstance().getSession();
141: Set principals = new HashSet();
142: for (Iterator iter = session.getPrincipals().iterator(); iter
143: .hasNext();) {
144: principals.add(iter.next());
145: }
146: RPCMessageImpl rpcMessage = new RPCMessageImpl(RandomGuid
147: .getInstance().getGuid(), new Date(), 0,
148: new Date(), session.getUser().getName(), session
149: .getUser().getSessionId(), new ArrayList(
150: principals), from, type,
151: Message.UNDELIVERED);
152: rpcMessage.setNextProcessDate(new Date());
153: return rpcMessage;
154: } catch (Exception ex) {
155: log.error("Failed to create the rpc message : "
156: + ex.getMessage(), ex);
157: throw new MessageServiceException(
158: "Failed to create the rpc message : "
159: + ex.getMessage(), ex);
160: }
161: }
162:
163: /**
164: * This method is responsible for submitting a new message for processing by
165: * the message service.
166: *
167: * @param newMessage The new message to be processed.
168: * @exception RemoteException
169: * @exception MessageServiceException
170: */
171: public void submit(Message newMessage) throws RemoteException,
172: MessageServiceException {
173: if ((newMessage instanceof RPCMessage)
174: && (((RPCMessage) newMessage).getMethodBodyXML() == null)) {
175: throw new MessageServiceException("The xml body is not set");
176: } else if ((newMessage instanceof TextMessage)
177: && (((TextMessage) newMessage).getTextBody() == null)) {
178: throw new MessageServiceException(
179: "The text body is not set");
180: }
181: try {
182: ((MessageImpl) newMessage).setNextProcessDate(new Date());
183: MessageManager messageManager = MessageManagerFactory
184: .getInstance().getMessageManager(newMessage);
185: MessageQueue messageQueue = MessageQueueManager
186: .getInstance().getQueue(
187: MessageQueueManager.UNSORTED);
188: ((MessageManagerImpl) messageManager)
189: .assignToQueue(MessageQueueManager.UNSORTED);
190: messageQueue.addMessage(messageManager);
191: } catch (Throwable ex) {
192: log.error("Failed to add the message : " + ex.getMessage(),
193: ex);
194: throw new MessageServiceException(
195: "Failed to add the message : " + ex.getMessage(),
196: ex);
197: }
198: }
199:
200: }
|