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  
18  package com.sun.syndication.fetcher.impl;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URLConnection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.Properties;
27  import java.util.Set;
28  
29  import com.sun.syndication.fetcher.FeedFetcherI;
30  import com.sun.syndication.fetcher.FetcherEvent;
31  import com.sun.syndication.fetcher.FetcherListener;
32  
33  
34  public abstract class AbstractFeedFetcher implements FeedFetcherI {
35  	private Set fetcherEventListeners;
36  	private String userAgent;
37  	
38  	public AbstractFeedFetcher() {
39  		fetcherEventListeners = Collections.synchronizedSet(new HashSet());
40  		String fetcherVersion = "UNKNOWN";
41  		
42  		Properties props = new Properties();
43  		String resourceName = "fetcher.properties";
44  		
45  		try {
46  			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourceName);
47  			if (inputStream == null) {
48  			    inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
49  			}			
50  			if (inputStream != null) {
51  				props.load(inputStream);
52  				System.setProperties(props);				
53  				inputStream.close();
54  			} else {
55  				System.err.println("Could not find " + resourceName + " on classpath");
56  			}
57  		} catch (IOException e) {
58  			// do nothing
59  			System.err.println("Error reading " + resourceName + " from classpath: " + e.getMessage());
60  		}		
61  		
62  		
63  		userAgent =  DEFAULT_USER_AGENT + " Ver: " + System.getProperty("rome.fetcher.version", "UNKNOWN");
64  	}
65  
66  	/***
67  	 * @return the User-Agent currently being sent to servers
68  	 */
69  	public String getUserAgent() {
70  		return userAgent;
71  	}
72  
73  	/***
74  	 * @param string The User-Agent to sent to servers
75  	 */
76  	public void setUserAgent(String string) {
77  		userAgent = string;
78  	}
79  
80  	/***
81  	 * @param eventType The event type to fire
82  	 * @param connection the current connection
83  	 */
84  	protected void fireEvent(String eventType, URLConnection connection) {
85  		FetcherEvent fetcherEvent = new FetcherEvent(this, connection.getURL(), eventType);
86  		synchronized(fetcherEventListeners) {
87  			Iterator iter = fetcherEventListeners.iterator();
88  			while ( iter.hasNext()) {
89  				FetcherListener fetcherEventListener = (FetcherListener) iter.next();
90  				fetcherEventListener.fetcherEvent(fetcherEvent);							
91  			}					
92  		}
93  	}
94  
95  	/***
96  	 * @see com.sun.syndication.fetcher.FeedFetcherI#addFetcherEventListener(com.sun.syndication.fetcher.FetcherListener)
97  	 */
98  	public void addFetcherEventListener(FetcherListener listener) {
99  		if (listener != null) {
100 			fetcherEventListeners.add(listener);		
101 		}	
102 		
103 	}
104 
105 	/***
106 	 * @see com.sun.syndication.fetcher.FeedFetcherI#removeFetcherPolledListener(com.sun.syndication.fetcher.FetcherListener)
107 	 */
108 	public void removeFetcherPolledListener(FetcherListener listener) {
109 		if (listener != null) {
110 			fetcherEventListeners.remove(listener);		
111 		}		
112 	}
113 	
114 	
115 }