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  package com.mindtree.techworks.insight.gui.action;
25  
26  import java.awt.event.ActionEvent;
27  
28  import javax.swing.AbstractAction;
29  import javax.swing.JOptionPane;
30  
31  import com.mindtree.techworks.insight.InsightConstants;
32  import com.mindtree.techworks.insight.appender.ParsedEventAppender;
33  import com.mindtree.techworks.insight.gui.Insight;
34  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
35  import com.mindtree.techworks.insight.model.ReceiverFormat;
36  import com.mindtree.techworks.insight.preferences.util.Log4JPatternInterpeter;
37  import com.mindtree.techworks.insight.preferences.util.PreferenceInterpreter;
38  import com.mindtree.techworks.insight.receiver.AbstractReceiver;
39  import com.mindtree.techworks.insight.receiver.ReceiverInitializationException;
40  import com.mindtree.techworks.insight.receiver.ReceiverInterpreter;
41  import com.mindtree.techworks.insight.receiver.ReceiverListener;
42  import com.mindtree.techworks.insight.receiver.RemoteProtocolStreamReceiver;
43  import com.mindtree.techworks.insight.receiver.RuntimeNamespaceContainer;
44  import com.mindtree.techworks.insight.spi.LogNamespace;
45  
46  
47  /**
48   * This is an action class that starts up the
49   * <code>RemoteProtocolStreamReceiver</code> to receive log events. It is
50   * implemented as a singleton, so that the stop action can get a hold of the
51   * singleton instance to stop the Receiver.
52   * 
53   * @see javax.swing.AbstractAction
54   * @see com.mindtree.techworks.insight.receiver.ReceiverListener
55   * @see com.mindtree.techworks.insight.receiver.RemoteProtocolStreamReceiver
56   * @see com.mindtree.techworks.insight.gui.action.StopReceiverAction
57   * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
58   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
59   */
60  public class RemoteProtocolListenerAction extends AbstractAction implements
61  		ReceiverListener {
62  
63  	// -------------------------------------------------------------------------
64  	// Class variables
65  	// -------------------------------------------------------------------------
66  
67  	/**
68  	 * Serial version UID for serialization
69  	 */
70  	private static final long serialVersionUID = -2277220522584222352L;
71  
72  	/**
73  	 * Singleton instance of the class
74  	 */
75  	private static RemoteProtocolListenerAction instance;
76  
77  	// -------------------------------------------------------------------------
78  	// Instance variables
79  	// -------------------------------------------------------------------------
80  
81  	/**
82  	 * Insight instance
83  	 */
84  	private Insight insight;
85  
86  	/**
87  	 * The AbstractReceiver instance, if any
88  	 */
89  	private AbstractReceiver receiver;
90  
91  	// -------------------------------------------------------------------------
92  	// Constructor
93  	// -------------------------------------------------------------------------
94  
95  	/**
96  	 * Creates an instance of the RemoteProtocolListenerAction. Since this class
97  	 * is a singleton, the constructor is private.
98  	 */
99  	private RemoteProtocolListenerAction () {
100 
101 		RuntimeNamespaceContainer.initializeAvailableNamespaceColorList ();
102 	}
103 
104 	// -------------------------------------------------------------------------
105 	// Static methods
106 	// -------------------------------------------------------------------------
107 
108 	/**
109 	 * Accessor method to obtain instance of this class
110 	 * 
111 	 * @return singleton instance of this class
112 	 */
113 	public static final RemoteProtocolListenerAction getInstance () {
114 
115 		if (null == instance) {
116 			synchronized (RemoteProtocolListenerAction.class) {
117 				if (null == instance) {
118 					instance = new RemoteProtocolListenerAction ();
119 				}
120 			}
121 		}
122 		return instance;
123 	}
124 
125 	// -------------------------------------------------------------------------
126 	// Methods implemented from com.mindtree.techworks.insight.receiver.ReceiverListener
127 	// -------------------------------------------------------------------------
128 
129 	/**
130 	 * @see com.mindtree.techworks.insight.receiver.ReceiverListener#startLoadEventNotification(com.mindtree.techworks.insight.spi.LogNamespace[])
131 	 */
132 	public void startLoadEventNotification (LogNamespace[] namespaces) {
133 
134 		// Do nothing
135 
136 	}
137 
138 	/**
139 	 * @see com.mindtree.techworks.insight.receiver.ReceiverListener#endLoadEventNotification(com.mindtree.techworks.insight.spi.LogNamespace[],
140 	 *      int)
141 	 */
142 	public void endLoadEventNotification (LogNamespace[] namespaces,
143 			int infoFlag) {
144 
145 		StatusBar.getInstance ().clearDisplay (1);
146 		if (infoFlag == ReceiverInterpreter.SUCCESS) {
147 			// Add the namespaces to the loaded namespaces list 
148 			for (int i = 0; i < namespaces.length; i++ ) {
149 				RuntimeNamespaceContainer.getLoadedNamespaces().add(namespaces[i]);
150 			}
151 		} else {
152 			JOptionPane.showMessageDialog (insight, InsightConstants
153 					.getLiteral ("ERROR_NO_MATCHING_ENTRIES"), InsightConstants
154 					.getLiteral ("ERROR"), JOptionPane.ERROR_MESSAGE);
155 		}
156 	}
157 
158 	// -------------------------------------------------------------------------
159 	// Methods overridden from java.awt.event.ActionListener
160 	// -------------------------------------------------------------------------
161 
162 	/**
163 	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
164 	 */
165 	public void actionPerformed (ActionEvent event) {
166 
167 		startupReceiver ();
168 
169 	}
170 
171 	// -------------------------------------------------------------------------
172 	// Public methods
173 	// -------------------------------------------------------------------------
174 
175 	/**
176 	 * Initializes this LoadFileAction the first time. Subsequent calls are
177 	 * ignored
178 	 * 
179 	 * @param insightInstance
180 	 *            the Insight that created an instance of this class
181 	 */
182 	public void initialize (Insight insightInstance) {
183 
184 		if (this.insight == null) {
185 			this.insight = insightInstance;
186 		}
187 	}
188 
189 	/**
190 	 * Returns the currently active AbstractReceiver, if any, or null otherwise
191 	 * 
192 	 * @return Returns null or the receiver.
193 	 */
194 	public AbstractReceiver getReceiver () {
195 
196 		return receiver;
197 	}
198 
199 	// -------------------------------------------------------------------------
200 	// Private util methods
201 	// -------------------------------------------------------------------------
202 
203 	/**
204 	 * Starts the receiver to log events.
205 	 */
206 	private void startupReceiver () {
207 
208 		int portToListenOn = PreferenceInterpreter
209 				.getRemoteProtocolListenerPort ();
210 		StatusBar
211 				.getInstance ()
212 				.setDisplayText (
213 									1,
214 									InsightConstants
215 											.getLiteral ("REMOTE_PROTOCOL_LISTENING")
216 											+ portToListenOn, true);
217 
218 		ReceiverFormat[] receiverFormat = Log4JPatternInterpeter
219 				.getInterpretedRecieverFormat ();
220 
221 		try {
222 			this.receiver = new RemoteProtocolStreamReceiver (receiverFormat,
223 					portToListenOn);
224 		} catch (ReceiverInitializationException rie) {
225 			StatusBar.getInstance ().setDisplayText (1, rie.getMessage (),
226 														false);
227 			return;
228 		}
229 		this.receiver.addAppender (ParsedEventAppender
230 				.getInstance (this.insight.getController ()));
231 		this.receiver.addReceiverListener (this);
232 		this.receiver.addMutatorListener (this.insight.getController ());
233 		this.receiver.addMutatorListener (this.insight.getController ()
234 				.getCurrentLogEventModel ().getPageSet ());
235 		this.receiver.startup ();
236 
237 	}
238 
239 }