1 package net.sf.logdistiller.publishers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.*;
18 import java.util.*;
19
20 import com.sun.syndication.feed.synd.*;
21 import com.sun.syndication.io.FeedException;
22 import com.sun.syndication.io.SyndFeedInput;
23 import com.sun.syndication.io.SyndFeedOutput;
24
25 import net.sf.logdistiller.*;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class FeedPublisher
48 extends Publisher
49 {
50 public final static String DEFAULT_FEED_TYPE = "atom_0.3";
51
52 public String getId()
53 {
54 return "feed";
55 }
56
57 public void publish( LogDistillation logdistillation, LogDistiller.Report report )
58 throws IOException, PublishException
59 {
60
61 String content = logdistillation.getContent();
62 String subject =
63 "[logdistiller:" + logdistillation.getDefinition().getId() + "]"
64 + ( ( content == null ) ? "" : ( " " + content ) ) + " global report: "
65 + logdistillation.getEventCount() + " logevents";
66 ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
67 StringWriter sw = new StringWriter();
68 format.report( logdistillation, sw );
69 String body = sw.toString();
70 String url = logdistillation.getDefinition().getOutput().getUrl();
71
72
73 SyndEntry entry = createEntry( subject, url, format.getContentType(), body );
74
75
76 addEntry( entry, logdistillation, report );
77 }
78
79 public void publish( LogDistillation.Group group, LogDistiller.Report report )
80 throws IOException, PublishException
81 {
82
83 LogDistillation logdistillation = group.getLogdistillation();
84 String content = logdistillation.getContent();
85 String subject =
86 "[logdistiller:" + logdistillation.getDefinition().getOutput().getId() + "]"
87 + ( ( content == null ) ? "" : ( " " + content ) ) + " report for group "
88 + group.getDefinition().getId() + ": " + group.getEventCount() + " logevents";
89 ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
90 StringWriter sw = new StringWriter();
91 format.report( group, sw );
92 String body = sw.toString();
93 String url = logdistillation.getDefinition().getOutput().getUrl();
94
95
96 SyndEntry entry = createEntry( subject, url, format.getContentType(), body );
97
98
99 addEntry( entry, logdistillation, report );
100 }
101
102
103
104
105
106
107
108
109 private SyndFeed createFeed( LogDistillation logdistillation, LogDistiller.Report report )
110 {
111 String type = report.getParam( "type", DEFAULT_FEED_TYPE );
112 String title = report.getParam( "title", "Logdistiller " + logdistillation.getDefinition().getOutput().getId() );
113 String url = logdistillation.getDefinition().getOutput().getUrl();
114 String link = report.getParam( "link", ( url == null ) ? "http://logdistiller.sf.net/" : url );
115 String description =
116 report.getParam( "description", "LogDistiller reports for "
117 + logdistillation.getDefinition().getOutput().getId() );
118
119 SyndFeed feed = new SyndFeedImpl();
120 feed.setFeedType( type );
121 feed.setTitle( title );
122 feed.setLink( link );
123 feed.setDescription( description );
124
125 return feed;
126 }
127
128
129
130
131
132
133
134
135
136
137 private SyndEntry createEntry( String title, String link, String contentType, String contentValue )
138 {
139 SyndEntry entry = new SyndEntryImpl();
140 entry.setTitle( title );
141 if ( link != null )
142 {
143 entry.setLink( link );
144 }
145 entry.setPublishedDate( new Date() );
146
147 SyndContent content = new SyndContentImpl();
148 content.setType( contentType );
149 content.setValue( contentValue );
150 List<SyndContent> contents = new ArrayList<SyndContent>();
151 contents.add( content );
152
153 entry.setContents( contents );
154 return entry;
155 }
156
157
158
159
160
161
162
163
164
165
166 private void addEntry( SyndEntry entry, LogDistillation logdistillation, LogDistiller.Report report )
167 throws IOException, PublishException
168 {
169 String filename = report.getParam( "filename", "feed.xml" );
170 int maxEntries = Integer.parseInt( report.getParam( "maxEntries", "100" ) );
171 File file = logdistillation.newDestinationFile( filename );
172
173
174 SyndFeed feed;
175 if ( file.canRead() && ( file.length() > 0 ) )
176 {
177 SyndFeedInput input = new SyndFeedInput();
178 try
179 {
180 feed = input.build( file );
181 }
182 catch ( FeedException fe )
183 {
184 throw new PublishException( "error while reading feed " + filename, fe );
185 }
186 }
187 else
188 {
189 feed = createFeed( logdistillation, report );
190 }
191
192
193 List<SyndEntry> entries = feed.getEntries();
194 entries.add( 0, entry );
195 if ( entries.size() > maxEntries )
196 {
197 entries.remove( entries.size() - 1 );
198 }
199 feed.setEntries( entries );
200
201
202 SyndFeedOutput output = new SyndFeedOutput();
203
204 File tmpFile = logdistillation.newDestinationFile( filename + ".tmp" );
205 try
206 {
207 output.output( feed, tmpFile );
208
209 file.delete();
210 tmpFile.renameTo( file );
211 }
212 catch ( FeedException fe )
213 {
214 throw new PublishException( "error while writing temporary feed " + filename + ".tmp", fe );
215 }
216 finally
217 {
218 tmpFile.delete();
219 }
220 }
221 }