KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > ProducerImpl


1 /*
2  * MessageService: The message service daemon
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ProducerImpl.java
20  */

21
22 // the package path
23
package com.rift.coad.daemon.messageservice;
24
25 // java imports
26
import java.rmi.Remote JavaDoc;
27 import java.rmi.RemoteException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.transaction.UserTransaction JavaDoc;
37 import javax.transaction.Status JavaDoc;
38
39 // logging import
40
import org.apache.log4j.Logger;
41
42 // message service imports
43
import com.rift.coad.lib.common.RandomGuid;
44 import com.rift.coad.lib.security.SessionManager;
45 import com.rift.coad.lib.security.ThreadPermissionSession;
46 import com.rift.coad.daemon.messageservice.message.MessageImpl;
47 import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
48 import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
49 import com.rift.coad.daemon.messageservice.message.MessageManagerFactory;
50 import com.rift.coad.daemon.messageservice.message.MessageManagerImpl;
51
52 /**
53  * This object is responsible for producing new message and submitting them to
54  * the message service for processing.
55  *
56  * @author Brett Chaldecott
57  */

58 public class ProducerImpl implements Producer {
59     
60     // the logger reference
61
protected static Logger log =
62             Logger.getLogger(ProducerImpl.class.getName());
63     
64     
65     
66     // the private member variables
67
private Context JavaDoc context = null;
68     private String JavaDoc from = null;
69     private UserTransaction JavaDoc ut = null;
70     
71     /**
72      * Creates a new instance of ProducerImpl
73      *
74      * @param from The from address for the messages.
75      * @exception MessageServiceException
76      */

77     public ProducerImpl(String JavaDoc from) throws MessageServiceException {
78         try {
79             this.from = from;
80             context = new InitialContext JavaDoc();
81             ut = (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
82         } catch (Exception JavaDoc ex) {
83             log.error("Failed to instanciate the producer : " + ex.getMessage(),
84                     ex);
85             throw new MessageServiceException(
86                     "Failed to instanciate the producer : " + ex.getMessage(),
87                     ex);
88         }
89     }
90     
91     
92     /**
93      * This method is responsible for creating a new text message for the
94      * message service.
95      *
96      * @return A newly created text message.
97      * @param type The type of message.
98      * @exception RemoteException
99      * @exception MessageServiceException
100      */

101     public TextMessage createTextMessage(int type) throws RemoteException JavaDoc,
102             MessageServiceException {
103         try {
104             ThreadPermissionSession session = SessionManager.getInstance().
105                     getSession();
106             Set JavaDoc principals = new HashSet JavaDoc();
107             for (Iterator JavaDoc iter = session.getPrincipals().iterator();
108             iter.hasNext();) {
109                 principals.add(iter.next());
110             }
111             
112             TextMessageImpl textMessage = new TextMessageImpl(
113                     RandomGuid.getInstance().getGuid(),new Date JavaDoc(),
114                     0,new Date JavaDoc(),session.getUser().getName(),
115                     session.getUser().getSessionId(),
116                     new ArrayList JavaDoc(principals),from,type,
117                     Message.UNDELIVERED);
118             textMessage.setNextProcessDate(new Date JavaDoc());
119             return textMessage;
120         } catch (Exception JavaDoc ex) {
121             log.error("Failed to create the text message : " + ex.getMessage(),
122                     ex);
123             throw new MessageServiceException(
124                     "Failed to create the text message : " + ex.getMessage(),
125                     ex);
126         }
127     }
128     
129     
130     /**
131      * This method is responsible for creating a new RPC message for the
132      * message service.
133      *
134      * @return A newly created text message.
135      * @param type The type of message.
136      * @exception RemoteException
137      * @exception MessageServiceException
138      */

139     public RPCMessage createRPCMessage(int type) throws RemoteException JavaDoc,
140             MessageServiceException {
141         try {
142             ThreadPermissionSession session = SessionManager.getInstance().
143                     getSession();
144             Set JavaDoc principals = new HashSet JavaDoc();
145             for (Iterator JavaDoc iter = session.getPrincipals().iterator();
146             iter.hasNext();) {
147                 principals.add(iter.next());
148             }
149             RPCMessageImpl rpcMessage = new RPCMessageImpl(
150                     RandomGuid.getInstance().getGuid(),new Date JavaDoc(),
151                     0,new Date JavaDoc(),session.getUser().getName(),
152                     session.getUser().getSessionId(),
153                     new ArrayList JavaDoc(principals),from,type,
154                     Message.UNDELIVERED);
155             rpcMessage.setNextProcessDate(new Date JavaDoc());
156             return rpcMessage;
157         } catch (Exception JavaDoc ex) {
158             log.error("Failed to create the rpc message : " + ex.getMessage(),
159                     ex);
160             throw new MessageServiceException(
161                     "Failed to create the rpc message : " + ex.getMessage(),
162                     ex);
163         }
164     }
165     
166     
167     /**
168      * This method is responsible for submitting a new message for processing by
169      * the message service.
170      *
171      * @param newMessage The new message to be processed.
172      * @exception RemoteException
173      * @exception MessageServiceException
174      */

175     public void submit(Message newMessage) throws RemoteException JavaDoc,
176             MessageServiceException {
177         if ((newMessage instanceof RPCMessage) &&
178                 (((RPCMessage)newMessage).getMethodBodyXML() == null)) {
179             throw new MessageServiceException("The xml body is not set");
180         } else if ((newMessage instanceof TextMessage) &&
181                 (((TextMessage)newMessage).getTextBody() == null)) {
182             throw new MessageServiceException("The text body is not set");
183         }
184         try {
185             ((MessageImpl)newMessage).setNextProcessDate(new Date JavaDoc());
186             MessageManager messageManager = MessageManagerFactory.getInstance().
187                     getMessageManager(newMessage);
188             MessageQueue messageQueue = MessageQueueManager.getInstance().
189                     getQueue(MessageQueueManager.UNSORTED);
190             ((MessageManagerImpl)messageManager).assignToQueue(
191                     MessageQueueManager.UNSORTED);
192             messageQueue.addMessage(messageManager);
193         } catch (Exception JavaDoc ex) {
194             log.error("Failed to add the message : " +
195                     ex.getMessage(),ex);
196             throw new MessageServiceException("Failed to add the message : " +
197                     ex.getMessage(),ex);
198         }
199     }
200
201 }
202
Popular Tags