View Javadoc

1   /*
2    * Copyright 2004 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   */
17  package com.sun.syndication.fetcher.impl;
18  
19  import java.io.Serializable;
20  import java.net.URL;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  
26  /***
27   * <p>A very simple implementation of the {@link com.sun.syndication.fetcher.impl.FeedFetcherCache} interface.</p>
28   * 
29   * <p>This implementation uses a HashMap to cache retrieved feeds. This implementation is
30   * most suitible for sort term (client aggregator?) use, as the memory usage will increase
31   * over time as the number of feeds in the cache increases.</p>
32   * 
33   * @author Nick Lothian
34   *
35   */
36  public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
37  	private static final long serialVersionUID = -1594665619950916222L;
38  
39  	static HashMapFeedInfoCache _instance;
40  	
41  	private Map infoCache;
42  	
43  	/***
44  	 * <p>Constructor for HashMapFeedInfoCache</p>
45  	 * 
46  	 * <p>Only use this if you want multiple instances of the cache. 
47  	 * Usually getInstance() is more appropriate.</p>
48  	 *
49  	 */
50  	public HashMapFeedInfoCache() {
51  		setInfoCache(createInfoCache());
52  	}
53  
54  	/***
55  	 * Get the global instance of the cache
56  	 * @return an implementation of FeedFetcherCache
57  	 */
58  	public static synchronized FeedFetcherCache getInstance() {
59  		if (_instance == null) {
60  			_instance = new HashMapFeedInfoCache();			
61  		}
62  		return _instance;
63  	}
64  
65  	protected Map createInfoCache() {
66   		return (Collections.synchronizedMap(new HashMap()));
67   	}
68  
69  	
70  	protected Object get(Object key) {
71  		return getInfoCache().get(key);
72  	}
73  
74  	/***
75  	 * @see extensions.io.FeedFetcherCache#getFeedInfo(java.net.URL)
76  	 */
77  	public SyndFeedInfo getFeedInfo(URL feedUrl) {
78  		return (SyndFeedInfo) get(feedUrl);
79  	}
80  
81  	protected void put(Object key, Object value) {
82  		getInfoCache().put(key, value);
83  	}
84  
85  	/***
86  	 * @see extensions.io.FeedFetcherCache#setFeedInfo(java.net.URL, extensions.io.SyndFeedInfo)
87  	 */
88  	public void setFeedInfo(URL feedUrl, SyndFeedInfo syndFeedInfo) {
89  		put(feedUrl, syndFeedInfo);		
90  	}
91  
92  	protected synchronized final Map getInfoCache() {
93  		return infoCache;
94  	}
95  
96  	/***
97  	 * The API of this class indicates that map must thread safe. In other
98  	 * words, be sure to wrap it in a synchronized map unless you know
99  	 * what you are doing.
100 	 * 
101 	 * @param map the map to use as the info cache.
102 	 */
103 	protected synchronized final void setInfoCache(Map map) {
104 		infoCache = map;
105 	}
106 
107 }