Source Code Cross Referenced for FileMessageBroker.java in  » Testing » MActor » org » mactor » brokers » file » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » MActor » org.mactor.brokers.file 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.file;
020:
021:        import java.io.File;
022:        import java.io.FileWriter;
023:        import java.io.IOException;
024:        import java.util.LinkedList;
025:        import java.util.List;
026:        import org.mactor.brokers.Message;
027:        import org.mactor.brokers.MessageBroker;
028:        import org.mactor.brokers.PollingMessageBrokerTemplate;
029:        import org.mactor.framework.MactorException;
030:        import org.mactor.framework.spec.MessageBrokersConfig.MessageBrokerConfig;
031:        import org.mactor.framework.spec.MessageBrokersConfig.MessageBrokerConfig.ChannelConfig;
032:
033:        /**
034:         * A message broker for communicating via file shares.
035:         * 
036:         * <p>
037:         * Message broker config structure (sample):
038:         * 
039:         * <pre>
040:         *  &lt;message_broker_config xmlns=&quot;http://schemas.mactor.org/framework&quot;&gt;
041:         *  	&lt;message_broker name=&quot;MactorDemoFileBroker&quot; 
042:         *  		archive_consumed_messages=&quot;true&quot; archive_dead_letter_messages=&quot;true&quot; 
043:         *  		archive_path=&quot;/tmp/archive_fileshare&quot; 
044:         *  		message_read_interval_seconds=&quot;1&quot; message_read_limit=&quot;40&quot; 
045:         *  		broker_class=&quot;org.mactor.brokers.file.FileMessageBroker&quot;&gt; 
046:         *  
047:         *  		&lt;value name=&quot;PostAction&quot;&gt;RENAME&lt;/value&gt; &lt;!--RENAME|DELETE--&gt;
048:         *  		&lt;channel name=&quot;OutgoingOrder&quot;&gt; 
049:         *  			&lt;value name=&quot;dir&quot;&gt;/tmp/mactor/file_channels/OutgoingOrder&lt;/value&gt;  
050:         *  			&lt;value name=&quot;suffix&quot;&gt;.xml&lt;/value&gt; 
051:         *  		&lt;/channel&gt; 
052:         *  		&lt;channel name=&quot;IncomingOrderStatus&quot;&gt; 
053:         *  			&lt;value name=&quot;dir&quot;&gt;/tmp/mactor/file_channels/IncomingOrderStatus&lt;/value&gt;  
054:         *  			&lt;value name=&quot;filter_pattern&quot;&gt;.xml&lt;/value&gt; 
055:         *  		&lt;/channel&gt;  
056:         *  	&lt;/message_broker&gt;
057:         *  &lt;/message_broker_config&gt;
058:         * </pre>
059:         * </p>
060:         * 
061:         * @author Lars Ivar Almli
062:         * @see MessageBroker
063:         */
064:        public class FileMessageBroker extends PollingMessageBrokerTemplate {
065:            private MessageBrokerConfig config;
066:
067:            private final boolean postActionDelete;
068:
069:            public FileMessageBroker(MessageBrokerConfig config)
070:                    throws MactorException {
071:                super (config);
072:                this .config = config;
073:                this .postActionDelete = "DELETE".equalsIgnoreCase(config
074:                        .getValue("PostAction"));
075:            }
076:
077:            @Override
078:            protected synchronized List<Message> doGetMessages(String channel,
079:                    int maxMessageCount) throws MactorException {
080:                ChannelConfig cf = config.getRequieredChannelConfig(channel);
081:                String filterPattern = cf.getValue("filter_patter", ".xml");
082:                File dir = new File(getDir(cf));
083:                List<File> candidates = new LinkedList<File>();
084:                for (File f : dir.listFiles()) {
085:                    if (f.isFile() && f.getName().endsWith(filterPattern)) {
086:                        File target = new File(f.getAbsolutePath() + "_seen");
087:                        if (f.renameTo(target))
088:                            candidates.add(target);
089:                        else
090:                            log
091:                                    .error("Failed to parse message received on channel '"
092:                                            + channel
093:                                            + "' to '"
094:                                            + target.getAbsolutePath() + "'");
095:                    }
096:                    if (candidates.size() >= maxMessageCount)
097:                        break;
098:                }
099:                List<Message> messages = new LinkedList<Message>();
100:                for (File file : candidates) {
101:                    try {
102:                        messages.add(Message.createMessage(file));
103:                        if (postActionDelete)
104:                            if (!file.delete())
105:                                log.error("Failed to delete file '"
106:                                        + file.getAbsolutePath()
107:                                        + "' received on channel '" + channel
108:                                        + "'");
109:                    } catch (MactorException me) {
110:                        log.error(
111:                                "Failed to parse message received on channel '"
112:                                        + channel + "'", me);
113:                    }
114:                }
115:                return messages;
116:            }
117:
118:            @Override
119:            protected synchronized void doPublishMessage(String channel,
120:                    Message message) throws MactorException {
121:                ChannelConfig cf = config.getRequieredChannelConfig(channel);
122:                String dir = getDir(cf);
123:                String fn = dir + getNext() + cf.getValue("suffix", ".xml")
124:                        + "__";
125:                try {
126:                    File f = new File(fn);
127:                    FileWriter fw = new FileWriter(fn);
128:                    fw.write(message.getContent() + "");
129:                    fw.close();
130:                    f.renameTo(new File(fn.substring(0, fn.length() - 2)));
131:                } catch (IOException ioe) {
132:                    throw new MactorException(
133:                            "Failed to write message to dir '" + dir
134:                                    + "'. File name: ''" + fn + "' . Error:"
135:                                    + ioe.getMessage());
136:                }
137:            }
138:
139:            private String getDir(ChannelConfig cf) throws MactorException {
140:                String dir = cf.getRequieredValue("dir");
141:                File f = new File(dir);
142:                if (!f.exists())
143:                    if (!f.mkdirs())
144:                        throw new MactorException(
145:                                "Unable to create the dir '"
146:                                        + dir
147:                                        + "' specified in the channel config for channel: "
148:                                        + cf.getName());
149:                if (!dir.endsWith("\\") && !dir.endsWith("/"))
150:                    dir = dir + "/";
151:                return dir;
152:            }
153:
154:            private static long counter = System.currentTimeMillis();
155:
156:            private static synchronized long getNext() {
157:                return counter++;
158:            }
159:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.