001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.brokers;
020:
021: import java.io.File;
022: import java.io.FileReader;
023: import java.io.FileWriter;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.Reader;
027: import java.util.Map;
028: import javax.xml.transform.Templates;
029: import javax.xml.transform.TransformerException;
030: import javax.xml.transform.TransformerFactory;
031: import javax.xml.transform.stream.StreamSource;
032: import org.dom4j.Document;
033: import org.dom4j.DocumentException;
034: import org.dom4j.DocumentHelper;
035: import org.dom4j.io.DocumentResult;
036: import org.dom4j.io.DocumentSource;
037: import org.dom4j.io.SAXReader;
038: import org.mactor.framework.AppUtil;
039: import org.mactor.framework.MactorException;
040:
041: /**
042: * The message implementation
043: *
044: * @author Lars Ivar Almli
045: */
046: public class Message {
047:
048: private boolean consumed;
049: private Document doc;
050: private Document docNoNs;
051: private String content;
052: private Map<String, String> messageProperties;
053: private MessageContextInfo messageContextInfo;
054:
055: private static String app_instance_id = AppUtil.getAppInstanceId();
056:
057: private String id;
058:
059: private static Object lock = new Object();
060: private static long counter = 0;
061:
062: private static long getNextId() {
063: synchronized (lock) {
064: return counter++;
065: }
066: }
067:
068: public Message() {
069: long seq = getNextId();
070: this .id = app_instance_id + "_" + seq;
071: this .messageContextInfo = new MessageContextInfo(seq);
072: }
073:
074: public Map<String, String> getMessageProperties() {
075: return messageProperties;
076: }
077:
078: public void consume() {
079: this .consumed = true;
080: }
081:
082: public boolean isConsumed() {
083: return consumed;
084: }
085:
086: public String getContent() {
087: return content;
088: }
089:
090: public Document getContentDocument() throws MactorException {
091: if (this .doc != null)
092: return this .doc;
093: try {
094: this .doc = DocumentHelper.parseText(content);
095: return this .doc;
096: } catch (DocumentException de) {
097: throw new MactorException(de);
098: }
099: }
100:
101: public static Message createMessage(String content)
102: throws MactorException {
103: return createMessage(content, null);
104: }
105:
106: public static Message createMessage(String content,
107: Map<String, String> messageProperties)
108: throws MactorException {
109: Message m = new Message();
110: m.content = content;
111: m.getContentDocument();// force parse..
112: m.messageProperties = messageProperties;
113: return m;
114: }
115:
116: public static Message createMessage(Document doc)
117: throws MactorException {
118: return createMessage(doc, null);
119: }
120:
121: public static Message createMessage(Document doc,
122: Map<String, String> messageProperties)
123: throws MactorException {
124: Message m = new Message();
125: m.doc = doc;
126: m.content = doc.asXML();
127: m.messageProperties = messageProperties;
128: return m;
129: }
130:
131: public static Message createMessage(InputStream inputStream)
132: throws MactorException {
133: return createMessage(inputStream, null);
134: }
135:
136: public static Message createMessage(InputStream inputStream,
137: Map<String, String> messageProperties)
138: throws MactorException {
139: try {
140: Message m = new Message();
141: m.doc = new SAXReader().read(inputStream);
142: m.content = m.doc.asXML();
143: m.messageProperties = messageProperties;
144: return m;
145: } catch (DocumentException de) {
146: throw new MactorException(de);
147: }
148: }
149:
150: public static Message createMessage(Reader reader)
151: throws MactorException {
152: return createMessage(reader, null);
153: }
154:
155: public static Message createMessage(Reader reader,
156: Map<String, String> messageProperties)
157: throws MactorException {
158: try {
159: Message m = new Message();
160: m.doc = new SAXReader().read(reader);
161: m.content = m.doc.asXML();
162: m.messageProperties = messageProperties;
163: return m;
164: } catch (DocumentException de) {
165: throw new MactorException(de);
166: }
167: }
168:
169: public static Message createMessage(File file)
170: throws MactorException {
171: return createMessage(file, null);
172: }
173:
174: public static Message createMessage(File file,
175: Map<String, String> messageProperties)
176: throws MactorException {
177: try {
178: return createMessage(new FileReader(file),
179: messageProperties);
180: } catch (IOException ioe) {
181: throw new MactorException(
182: "Failed to load the message from file '"
183: + file.getAbsolutePath() + "'", ioe);
184: }
185: }
186:
187: public void writeToFile(File destFile) throws MactorException {
188: try {
189: FileWriter w = new FileWriter(destFile);
190: doc.write(w);
191: w.flush();
192: w.close();
193: } catch (IOException ioe) {
194: throw new MactorException("Failed to write the file '"
195: + destFile.getAbsolutePath() + "'. Error:"
196: + ioe.getMessage(), ioe);
197: }
198: }
199:
200: public Document getContentDocumentNoNs() throws MactorException {
201: if (docNoNs != null)
202: return docNoNs;
203: if (doc == null)
204: return null;
205: try {
206: TransformerFactory factory = TransformerFactory
207: .newInstance();
208: Templates t = factory.newTemplates(new StreamSource(
209: "lib/remove-ns.xsl"));
210: DocumentSource source = new DocumentSource(doc);
211: DocumentResult result = new DocumentResult();
212: t.newTransformer().transform(source, result);
213: this .docNoNs = result.getDocument();
214: return docNoNs;
215: } catch (TransformerException tfe) {
216: throw new MactorException(
217: "Failed remove namespaces from document Error:"
218: + tfe.getMessage(), tfe);
219: }
220: }
221:
222: public String getId() {
223: return id;
224: }
225:
226: public MessageContextInfo getMessageContextInfo() {
227: return messageContextInfo;
228: }
229: }
|