01: /*
02: * Copyright 2005 Sun Microsystems, Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.apache.roller.util.rome;
17:
18: import java.io.File;
19: import java.io.FileInputStream;
20: import java.io.FileNotFoundException;
21: import java.io.FileOutputStream;
22: import java.io.IOException;
23: import java.io.ObjectInputStream;
24: import java.io.ObjectOutputStream;
25: import java.net.URL;
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: import org.apache.roller.util.Utilities;
33:
34: /**
35: * @author David M. Johnson
36: */
37: public class DiskFeedInfoCache implements FeedFetcherCache {
38: private static Log logger = LogFactory.getFactory().getInstance(
39: DiskFeedInfoCache.class);
40:
41: protected String cachePath = null;
42:
43: public DiskFeedInfoCache(String cachePath) {
44: this .cachePath = cachePath;
45: }
46:
47: public SyndFeedInfo getFeedInfo(URL url) {
48: SyndFeedInfo info = null;
49: String fileName = cachePath
50: + File.separator
51: + "feed_"
52: + Utilities.replaceNonAlphanumeric(url.toString(), '_')
53: .trim();
54: FileInputStream fis;
55: try {
56: fis = new FileInputStream(fileName);
57: ObjectInputStream ois = new ObjectInputStream(fis);
58: info = (SyndFeedInfo) ois.readObject();
59: fis.close();
60: } catch (FileNotFoundException fnfe) {
61: logger.debug("Cache miss for " + url.toString());
62: } catch (ClassNotFoundException cnfe) {
63: // Error writing to cahce is fatal
64: throw new RuntimeException("Attempting to read from cache",
65: cnfe);
66: } catch (IOException fnfe) {
67: // Error writing to cahce is fatal
68: throw new RuntimeException("Attempting to read from cache",
69: fnfe);
70: }
71: if (info == null)
72: logger.info("Cache MISS!");
73: return info;
74: }
75:
76: public void setFeedInfo(URL url, SyndFeedInfo feedInfo) {
77: String fileName = cachePath
78: + File.separator
79: + "feed_"
80: + Utilities.replaceNonAlphanumeric(url.toString(), '_')
81: .trim();
82: FileOutputStream fos;
83: try {
84: fos = new FileOutputStream(fileName);
85: ObjectOutputStream oos = new ObjectOutputStream(fos);
86: oos.writeObject(feedInfo);
87: fos.flush();
88: fos.close();
89: } catch (Exception e) {
90: // Error writing to cahce is fatal
91: throw new RuntimeException("Attempting to write to cache",
92: e);
93: }
94: }
95: }
|