01: /*
02: * MessageService: The message service daemon
03: * Copyright (C) 2007 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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: * MessageStoreImpl.java
20: */
21:
22: // package path
23: package com.rift.coad.daemon.messageservice;
24:
25: // java imports
26: import java.rmi.Remote;
27: import java.rmi.RemoteException;
28: import java.util.Date;
29:
30: // logging import
31: import org.apache.log4j.Logger;
32:
33: // coadunation imports
34: import com.rift.coad.daemon.messageservice.message.MessageImpl;
35: import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
36: import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
37: import com.rift.coad.daemon.messageservice.message.MessageManagerFactory;
38: import com.rift.coad.daemon.messageservice.message.MessageManagerImpl;
39:
40: /**
41: * This class is responsible for implementing the message store interface.
42: *
43: * @author Brett Chaldecott
44: */
45: public class MessageStoreImpl implements MessageStore {
46:
47: // the logger reference
48: protected static Logger log = Logger
49: .getLogger(MessageStoreImpl.class.getName());
50:
51: /**
52: * Creates a new instance of MessageStoreImpl
53: */
54: public MessageStoreImpl() {
55: }
56:
57: /**
58: * This method adds a message to the message store of a message service.
59: *
60: * @param message The message to add to the store.
61: * @exception MessageServiceException
62: * @exception RemoteException
63: */
64: public void addMessage(Message newMessage)
65: throws MessageServiceException, RemoteException {
66: if ((newMessage instanceof RPCMessage)
67: && (((RPCMessage) newMessage).getMethodBodyXML() == null)) {
68: throw new MessageServiceException("The xml body is not set");
69: } else if ((newMessage instanceof TextMessage)
70: && (((TextMessage) newMessage).getTextBody() == null)) {
71: throw new MessageServiceException(
72: "The text body is not set");
73: }
74: try {
75: log.debug("Receive message : " + newMessage.getMessageId());
76: IDLock.getInstance().lock(newMessage.getMessageId());
77: ((MessageImpl) newMessage).setNextProcessDate(new Date());
78: MessageManager messageManager = MessageManagerFactory
79: .getInstance().getMessageManager(newMessage);
80: MessageQueue messageQueue = MessageQueueManager
81: .getInstance().getQueue(
82: MessageQueueManager.UNSORTED);
83: ((MessageManagerImpl) messageManager)
84: .assignToQueue(MessageQueueManager.UNSORTED);
85: messageQueue.addMessage(messageManager);
86: log.debug("Message added : " + newMessage.getMessageId());
87: } catch (Exception ex) {
88: log.error("Failed to add the message : " + ex.getMessage(),
89: ex);
90: throw new MessageServiceException(
91: "Failed to add the message : " + ex.getMessage(),
92: ex);
93: }
94: }
95: }
|