View Javadoc

1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16   */
17  package org.bejug.javacareers.feeder;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStreamWriter;
23  import java.io.Writer;
24  import java.nio.charset.Charset;
25  import java.util.List;
26  import java.util.Timer;
27  import java.util.TimerTask;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.bejug.javacareers.feeder.model.JobList;
32  import org.bejug.javacareers.feeder.parser.RssFeedGenerator;
33  import org.bejug.javacareers.jobs.model.JobOffer;
34  import org.gnu.stealthp.rsslib.RSSItem;
35  
36  /***
37   * Generates an RSS2.0 file from a datasource
38   * TODO: Split from thread
39   *
40   * @author Bavo Bruylandt (Last modified by $Author: shally $)
41   * @version $Revision: 1.5 $ - $Date: 2005/12/20 15:36:45 $
42   */
43  public class RssGeneratorFeederTask extends FeederTask {
44  
45      /***
46       * The class logger.
47       */
48      public static final Log LOG =
49              LogFactory.getLog(RssGeneratorFeederTask.class);
50  
51      /***
52       *
53       */
54      private int generateCycle;
55  
56  
57      /***
58       *
59       */
60      public void init() {
61          FeederDaemonConfig config = getFeederDaemonConfig();
62          generateCycle = config.getGenerateCycle();
63          LOG.info("Debug: Generatecycle: " + generateCycle);
64          LOG.info("Debug: ProxyHost: " + config.getProxyHost());
65          String webpath = "";
66          webpath = getJavaCareersConfig().getWebPathUrl();
67          //getJavaCareersConfig().getUploadPath()
68          LOG.info("Debug: webpath: "+webpath);
69  
70          filepath = webpath + File.separator+config.getRssFilepath();
71  
72  //        filepath = "/"+config.getRssFilepath();
73          constructFeeder(config);
74          Timer timer = new Timer();
75          timer.schedule(new TimerTask() {
76              public void run() {
77                  LOG.info("Debug: <<<<<<<<<<<< Generating feed at startup>>>>>>>>>>>>>>>>>>>");
78                  createRSSFeed();
79              }
80          }, 120 * 1000);
81  
82  
83      }
84  
85     
86  
87      /***
88       * Starts the task in seperate thread.
89       */
90      public synchronized void run() {
91          LOG.info("Debug: <<<<<<<<<<<< Generating feed >>>>>>>>>>>>>>>>>>>");
92          createRSSFeed();
93      }
94  
95      /***
96       * Create RssFeed
97       *
98       */
99      void createRSSFeed() {
100         LOG.info("Debug: Creating RSS file");
101         JobList jobList = new JobList("RSS list");
102         List tempList = getJobService().getJobOffers();
103         for (int i = 0; i < tempList.size(); i++) {
104             JobOffer offer = (JobOffer) tempList.get(i);
105             //JobEntry entry = new JobEntry(offer);
106             jobList.addJobOffer(offer);
107         }
108 
109         RssFeedGenerator generator = new RssFeedGenerator();
110         generator.setChannel(channel);
111         LOG.info("Generating RSS feed - adding items");
112         for (int i = jobList.size()-1; i >= 0; i--) {
113             JobOffer entry = (JobOffer) jobList.get(i);
114             RSSItem item = new RSSItem();
115             String jobPage = getJavaCareersConfig().getJobPageUrl();
116             String host = getJavaCareersConfig().getUrl()+getJavaCareersConfig().getAppname();
117             item.setLink(host+"/"+jobPage+"?id="+entry.getId());
118             item.setDescription(entry.getDescription());
119             item.setTitle(entry.getTitle());
120             generator.addItem(item);
121             LOG.info("Debug: Added item: " + item.getTitle());
122         }
123         LOG.info("Debug: Filepath: " + filepath);
124 
125         writeRSSFeed(generator);
126     }
127 
128     /***
129      * @param generator RssFeedGenerator
130      */
131     private void writeRSSFeed(RssFeedGenerator generator) {
132         File file = new File(filepath);
133         LOG.info("Debug: Using file:" + file.getAbsolutePath() +
134                   " with FilePath: " + filepath);
135 
136         Writer writer = null;
137         try {
138             writer = new OutputStreamWriter(new FileOutputStream(file),
139                                             Charset.forName("UTF-8"));
140 
141             //iso-8859-1
142             generator.parseToRSS(writer);
143         }
144         catch (IOException e) {
145             LOG.error(e);
146         }
147         finally {
148             if (writer != null) {
149                 try {
150                     writer.close();
151                 }
152                 catch (IOException ex) {
153                     LOG.error(ex);
154                 }
155             }
156         }
157         LOG.info("Debug: Written RSS file to " + file.getAbsolutePath());
158     }
159 
160 
161 
162 
163 }
164 
165 /***
166  * $Log: RssGeneratorFeederTask.java,v $
167  * Revision 1.5  2005/12/20 15:36:45  shally
168  * CheckStyle and PMD changes.
169  *
170  * Revision 1.4  2005/12/09 10:46:55  shally
171  * Opkuis voor checkstyle en PMD
172  *
173  * Revision 1.3  2005/09/30 14:38:08  bavo_jcs
174  * Fixed URL
175  *
176  * Revision 1.2  2005/09/13 08:11:17  schauwvliege
177  * organize imports
178  *
179  * Revision 1.1  2005/08/26 07:58:29  ge0ffrey
180  * split up the sources in service, serviceimpl and webclient
181  *
182  * Revision 1.9  2005/08/23 13:08:04  bavo_jcs
183  * Reversed RSS listing
184  *
185  * Revision 1.8  2005/08/10 09:04:48  bavo_jcs
186  * Optimized imports according to checkstyle
187  *
188  * Revision 1.7  2005/08/09 12:59:54  bavo_jcs
189  * Optimized imports
190  *
191  * Revision 1.6  2005/07/28 07:19:31  bavo_jcs
192  * URL path fix
193  *
194  * Revision 1.5  2005/06/17 11:42:46  schauwvliege
195  * CheckStyle/ PMD
196  *
197  * Revision 1.4  2005/06/16 13:46:27  bavo_jcs
198  * made jobs accessible through GET method URL
199  * adapted RSS file to use this
200  *
201  * Revision 1.3  2005/06/14 12:05:52  schauwvliege
202  * CheckStyle and fixing tests
203  *
204  * Revision 1.2  2005/06/09 08:18:43  bejug_cc
205  * Fix initial import
206  *
207  * Revision 1.15  2005/06/07 14:38:48  bbr
208  * Lucene highlightterms added
209  *
210  * Revision 1.14  2005/06/07 09:46:13  bbr
211  * job source, webpath, indexing
212  *
213  * Revision 1.13  2005/05/31 13:30:49  bbr
214  * reorganized contexts for tests
215  *
216  * Revision 1.12  2005/05/31 11:56:19  bbr
217  * deleted JobEntry
218  *
219  * Revision 1.11  2005/05/30 14:14:56  bbr
220  * servletcontext enabled
221  *
222  * Revision 1.10  2005/05/30 12:04:56  bbr
223  * using javacareersconfig
224  * timertask at startup
225  *
226  * Revision 1.9  2005/05/26 17:25:08  sja
227  * Removed jobService variable.
228  *
229  * Revision 1.8  2005/05/26 15:38:00  bbr
230  * UTF-8 fix
231  *
232  * Revision 1.7  2005/05/26 12:04:27  sja
233  * Removed default constructor.
234  *
235  * Revision 1.6  2005/05/26 08:59:30  bbr
236  * split cron
237  * made tasks run at startup
238  *
239  * Revision 1.5  2005/05/25 15:40:08  ssc
240  * renamed getOffers to getJobOffers
241  *
242  * Revision 1.4  2005/05/25 15:16:12  bbr
243  * generator bean
244  *
245  * Revision 1.3  2005/05/25 15:03:55  bbr
246  * generator bean
247  *
248  * Revision 1.2  2005/05/24 15:33:26  bbr
249  * Using spring sheduling
250  *
251  * Revision 1.1  2005/05/24 11:52:39  bbr
252  * Using spring sheduling
253  *
254  * Revision 1.1  2005/05/23 17:04:57  sja
255  * Moved to org.bejug.javacareers.feeder package.
256  *
257  * Revision 1.1  2005/05/23 08:46:33  PSONG09
258  * added feeder source files to project
259  *
260  * Revision 1.7  2005/05/23 07:15:34  stephan_janssen
261  * Code cleanup.
262  *
263  * Revision 1.6  2005/05/12 12:56:01  stephan_janssen
264  * Closed writer and added writeRSSFeed method.
265  *
266  * Revision 1.5  2005/05/11 14:25:23  bavo_jcs
267  * - renamed main files
268  *
269  * Revision 1.4  2005/05/11 11:53:25  bavo_jcs
270  * refactored
271  * - conform to conventions
272  * - some javadoc
273  * - Added FeederTask design
274  *
275  * Revision 1.3  2005/05/10 16:04:01  bavo_jcs
276  * cleanup
277  *
278  * Revision 1.2  2005/05/10 11:32:49  bavo_jcs
279  * integrated with services from JavaCareers Web
280  *
281  * Revision 1.1  2005/05/09 15:48:45  bavo_jcs
282  * added RSS generator
283  * cleanup/javadoc TODO
284  *
285  */