01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.brokers;
20:
21: import java.io.File;
22: import org.apache.log4j.Logger;
23: import org.mactor.framework.MactorException;
24:
25: class ArchiveUtil {
26: protected static Logger log = Logger
27: .getLogger(PollingMessageBrokerTemplate.class);
28: private static long counter = System.currentTimeMillis();
29:
30: private static synchronized long getNext() {
31: return counter++;
32: }
33:
34: String prefix = System.currentTimeMillis() + "_";
35: String path;
36:
37: public ArchiveUtil(String path) {
38: this .path = path;
39: }
40:
41: public File archive(String channel, Message message) {
42: if (message == null)
43: return null;
44: File dir = new File(path + "/" + channel);
45: log.debug("Archive dir:" + dir.getAbsolutePath());
46: if (!dir.exists() && !dir.mkdirs()) {
47: log.info("Unable to archive messages. Directory '"
48: + dir.getAbsolutePath() + "' could not be created");
49: return null;
50: }
51: File file = null;
52: try {
53: file = new File(dir.getAbsolutePath() + "/" + prefix
54: + getNext() + ".xml");
55: log.debug("Archiving file:" + file.getAbsolutePath());
56: message.writeToFile(file);
57: return file;
58: } catch (MactorException e) {
59: log.info("Failed to archive message. Path '"
60: + file.getAbsolutePath() + "'. Error: "
61: + e.getMessage(), e);
62: }
63: return null;
64: }
65:
66: public File archive_response(File relatedFile, Message message) {
67: if (relatedFile == null || message == null)
68: return null;
69: File file = null;
70: try {
71: file = new File(relatedFile.getAbsolutePath() + "_resp.xml");
72: log.debug("Archiving file:" + file.getAbsolutePath());
73: message.writeToFile(file);
74: return file;
75: } catch (MactorException e) {
76: log.info("Failed to archive message. Path '"
77: + file.getAbsolutePath() + "'. Error: "
78: + e.getMessage(), e);
79: }
80: return null;
81: }
82: }
|