KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > util > rome > DiskFeedInfoCache


1 /*
2  * Copyright 2005 Sun Microsystems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.roller.util.rome;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.sun.syndication.fetcher.impl.FeedFetcherCache;
31 import com.sun.syndication.fetcher.impl.SyndFeedInfo;
32
33 /**
34  * @author David M. Johnson
35  */

36 public class DiskFeedInfoCache implements FeedFetcherCache
37 {
38     private static Log logger =
39         LogFactory.getFactory().getInstance(DiskFeedInfoCache.class);
40     
41     protected String JavaDoc cachePath = null;
42     public DiskFeedInfoCache(String JavaDoc cachePath)
43     {
44         this.cachePath = cachePath;
45     }
46     public SyndFeedInfo getFeedInfo(URL JavaDoc url)
47     {
48         SyndFeedInfo info = null;
49         String JavaDoc fileName = cachePath + File.separator + "feed_" + url.hashCode();
50         FileInputStream JavaDoc fis;
51         try
52         {
53             fis = new FileInputStream JavaDoc(fileName);
54             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
55             info = (SyndFeedInfo)ois.readObject();
56             fis.close();
57         }
58         catch (FileNotFoundException JavaDoc fnfe)
59         {
60             logger.debug("Cache miss for " + url.toString());
61         }
62         catch (ClassNotFoundException JavaDoc cnfe)
63         {
64             // Error writing to cahce is fatal
65
throw new RuntimeException JavaDoc("Attempting to read from cache", cnfe);
66         }
67         catch (IOException JavaDoc fnfe)
68         {
69             // Error writing to cahce is fatal
70
throw new RuntimeException JavaDoc("Attempting to read from cache", fnfe);
71         }
72         if (info == null) logger.info("Cache MISS!");
73         return info;
74     }
75
76     public void setFeedInfo(URL JavaDoc url, SyndFeedInfo feedInfo)
77     {
78         String JavaDoc fileName = cachePath + File.separator + "feed_" + url.hashCode();
79         FileOutputStream JavaDoc fos;
80         try
81         {
82             fos = new FileOutputStream JavaDoc(fileName);
83             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
84             oos.writeObject(feedInfo);
85             fos.flush();
86             fos.close();
87         }
88         catch (Exception JavaDoc e)
89         {
90             // Error writing to cahce is fatal
91
throw new RuntimeException JavaDoc("Attempting to write to cache", e);
92         }
93     }
94 }
95
Popular Tags