View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  
25  package com.mindtree.techworks.insight.receiver;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.io.InputStreamReader;
30  import java.net.URL;
31  
32  import com.mindtree.techworks.insight.spi.LogEvent;
33  import com.mindtree.techworks.insight.spi.LogNamespace;
34  
35  /**
36  *
37  * The <code>FileStreamReceiver</code> class is a concrete implementation of the
38  * AbstractReceiver class. It supports receiving and building events from 
39  * files.
40  * @author  Regunath B
41  * @version 1.0, 05/07/07
42  * @see com.mindtree.techworks.insight.receiver.AbstractReceiver
43  */
44  
45  public class FileStreamReceiver extends AbstractReceiver  {
46  	
47  	
48  	/**
49  	 * The LogNamespace for this receiver
50  	 */
51  	private LogNamespace logNamespace;
52  	
53  	/**
54  	 * The tailing indicator for this receiver
55  	 */
56  	private boolean isTailing ;
57  	/**
58  	 * The Interpreter of String to LogEvents which we get by reader.
59  	 */
60  	private LogInterpreter logInterpreter =null;
61  	
62  	/**
63  	 * Reader instance which is being used for reading the file. 
64  	 */
65  	private BufferedReader reader;
66  	
67  	/**
68  	 * Indicator to demote if end of stream has been reached. This flag depends on whether the reader has reached
69  	 * end of stream. However, this flag is set to true one call later than actual end signalled by the reader to
70  	 * permit the Loginterpreter to interpret any last events if they exist
71  	 */
72  	private boolean hasReachedEOS = false;
73  	
74  	/**
75  	 * Constructor for this class.
76  	 * @param namespace the LogNamespace that contains the file information to read events from 
77  	 * @param isTailing true if the receiver must be a tailing receiver
78  	 * @throws ReceiverInitializationException if the receiver is not able to initialize.
79  	 */
80  	public FileStreamReceiver (LogNamespace namespace, boolean isTailing) throws ReceiverInitializationException{
81  		initialize(namespace, isTailing);
82  	}
83  	
84  	/**
85  	 * Overriden superclass method
86  	 * @see com.mindtree.techworks.insight.receiver.AbstractReceiver#getNamespaces()
87  	 */
88  	public LogNamespace[] getNamespaces() {
89  		return new LogNamespace[] {this.logNamespace};
90  	}
91  
92  	/**
93  	 * Overriden superclass method
94  	 * @see com.mindtree.techworks.insight.receiver.AbstractReceiver#isTailing()
95  	 */
96  	public boolean isTailing() {
97  		return this.isTailing;
98  	}
99  
100 	/**
101 	 * Overriden superclass method
102 	 * @see com.mindtree.techworks.insight.receiver.AbstractReceiver#initialize(com.mindtree.techworks.insight.spi.LogNamespace, boolean)
103 	 */
104 	protected void initialize(LogNamespace namespace, boolean isTailingReceiver) throws ReceiverInitializationException{
105 		
106 		this.logNamespace = namespace;
107 		this.isTailing = isTailingReceiver;
108 		logInterpreter = new LogInterpreter(namespace);
109 	    
110 		String url = namespace.getLocalSourceString();
111 
112 		// Modification for Bugzilla bug #2874. In the fix for #2715 URL 
113 		// Encode was introduced, but file systems allow spaces also in their
114 		// directory names, which was getting encoded to '+' by the URLEncode
115 		// class. This fix, specifically fixes the issue of #2715, while 
116 		// allowing all other special characters.
117 		url = url.replaceAll("#", "%23");
118 		
119 		try {
120 			reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
121         }catch (IOException ioe) {
122           throw new ReceiverInitializationException("Unable to load : " + (url==null?null:url.toString()) , ioe);
123         }
124 	}
125 
126 	/**
127 	 * Overriden superclass method
128 	 * @see com.mindtree.techworks.insight.receiver.AbstractReceiver#deInitialize()
129 	 */
130 	protected void deInitialize() {
131 	    try {
132 	    	if (reader != null) {
133 	    		reader.close();
134 		    }
135 	    } catch (IOException ioe) {
136 	    	ioe.printStackTrace();
137 	    }		
138 	}
139 
140 	/**
141 	 * Overriden superclass method
142 	 * @see com.mindtree.techworks.insight.receiver.AbstractReceiver#getNextEvents()
143 	 */
144 	protected LogEvent[] getNextEvents() {
145 		LogEvent[] events = null;
146 		String line = null;
147 		try {
148 			line = reader.readLine();
149 		} catch(IOException ioe) {
150 			// no events to process if error occurs
151 			return events;
152 		}		
153 		return this.logInterpreter.parseLogMessage(line);
154 	}
155 
156 	/**
157 	 * Overriden superclass method
158 	 * @see com.mindtree.techworks.insight.receiver.AbstractReceiver#hasMoreEvents()
159 	 */
160 	protected boolean hasMoreEvents() {
161 		boolean hasData = false;
162 		try {
163 			hasData = reader.ready();
164 			if (!hasData) {
165 				// check to see if the logical hasReachedEOS has been set already
166 				if (hasReachedEOS) {
167 					return hasData;
168 				} else {
169 					hasReachedEOS = true;
170 					return true;
171 				}
172 			}
173 		} catch(IOException ioe) {
174 			// ignore it
175 		}
176 		return hasData;
177 	}
178 	
179 
180 }