Debian Codegraph @

src/java/org/astrogrid/samp/xmlrpc/HubMode.java

raw
1 package org.astrogrid.samp.xmlrpc;
2
3 import java.awt.AWTException;
4 import java.awt.Image;
5 import java.awt.MenuItem;
6 import java.awt.PopupMenu;
7 import java.awt.Toolkit;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Random;
15 import java.util.logging.Logger;
16 import javax.swing.JFrame;
17 import org.astrogrid.samp.Client;
18 import org.astrogrid.samp.gui.GuiHubService;
19 import org.astrogrid.samp.gui.MessageTrackerHubService;
20 import org.astrogrid.samp.gui.SysTray;
21 import org.astrogrid.samp.hub.HubService;
22 import org.astrogrid.samp.hub.BasicHubService;
23
24 /**
25 * Specifies a particular hub implementation for use with {@link HubRunner}.
26 *
27 * @author Mark Taylor
28 * @since 20 Nov 2008
29 * @deprecated use {@link org.astrogrid.samp.hub.HubServiceMode} with
30 * {@link org.astrogrid.samp.hub.Hub} instead
31 */
32 public abstract class HubMode {
33 // This class looks like an enumeration-type class to external users.
34 // It is actually a HubService factory.
35
36 private final String name_;
37 private final boolean isDaemon_;
38 private static final Logger logger_ =
39 Logger.getLogger( HubMode.class.getName() );
40
41 /** Hub mode with no GUI representation of hub operations. */
42 public static final HubMode NO_GUI;
43
44 /** Hub mode with a GUI representation of connected clients. */
45 public static final HubMode CLIENT_GUI;
46
47 /** Hub mode with a GUI representation of clients and messages. */
48 public static HubMode MESSAGE_GUI;
49
50 /** Array of available hub modes. */
51 private static final HubMode[] KNOWN_MODES = new HubMode[] {
52 NO_GUI = createBasicHubMode( "no-gui" ),
53 CLIENT_GUI = createGuiHubMode( "client-gui" ),
54 MESSAGE_GUI = createMessageTrackerHubMode( "msg-gui" ),
55 };
56
57 /**
58 * Constructor.
59 *
60 * @param name mode name
61 * @param isDaemon true if the hub will start only daemon threads
62 */
63 HubMode( String name, boolean isDaemon ) {
64 name_ = name;
65 isDaemon_ = isDaemon;
66 }
67
68 /**
69 * Returns a new HubService object.
70 *
71 * @param random random number generator
72 * @param runners 1-element array of HubRunners - this should be
73 * populated with the runner once it has been constructed
74 */
75 abstract HubService createHubService( Random random, HubRunner[] runners );
76
77 /**
78 * Indicates whether the hub service will start only daemon threads.
79 * If it returns true, the caller may need to make sure that the
80 * JVM doesn't stop too early.
81 *
82 * @return true iff no non-daemon threads will be started by the service
83 */
84 boolean isDaemon() {
85 return isDaemon_;
86 }
87
88 /**
89 * Returns this mode's name.
90 *
91 * @return mode name
92 */
93 String getName() {
94 return name_;
95 }
96
97 public String toString() {
98 return name_;
99 }
100
101 /**
102 * Returns one of the known modes which has a name as given.
103 *
104 * @param name mode name (case-insensitive)
105 * @return mode with given name, or null if none known
106 */
107 public static HubMode getModeFromName( String name ) {
108 HubMode[] modes = KNOWN_MODES;
109 for ( int im = 0; im < modes.length; im++ ) {
110 HubMode mode = modes[ im ];
111 if ( mode.name_.equalsIgnoreCase( name ) ) {
112 return mode;
113 }
114 }
115 return null;
116 }
117
118 /**
119 * Returns an array of the hub modes which can actually be used.
120 *
121 * @return available mode list
122 */
123 public static HubMode[] getAvailableModes() {
124 List modeList = new ArrayList();
125 for ( int i = 0; i < KNOWN_MODES.length; i++ ) {
126 HubMode mode = KNOWN_MODES[ i ];
127 if ( ! ( mode instanceof BrokenHubMode ) ) {
128 modeList.add( mode );
129 }
130 }
131 return (HubMode[]) modeList.toArray( new HubMode[ 0 ] );
132 }
133
134 /**
135 * Used to perform common configuration of hub display windows
136 * for GUI-type hub modes.
137 *
138 * @param frame hub window
139 * @param runners 1-element array which will contain an associated
140 * hub runner object if one exists
141 */
142 static void configureHubWindow( JFrame frame, HubRunner[] runners ) {
143 SysTray sysTray = SysTray.getInstance();
144 if ( sysTray.isSupported() ) {
145 try {
146 configureWindowForSysTray( frame, runners, sysTray );
147 }
148 catch ( AWTException e ) {
149 logger_.warning( "Failed to install in system tray: " + e );
150 configureWindowBasic( frame, runners );
151 }
152 logger_.info( "Hub started in system tray" );
153 }
154 else {
155 logger_.info( "System tray not supported: displaying hub window" );
156 configureWindowBasic( frame, runners );
157 }
158 }
159
160 /**
161 * Performs common configuration of hub display window without
162 * system tray functionality.
163 * @param frame hub window
164 * @param runners 1-element array which will contain an associated
165 * hub runner object if one exists
166 */
167 private static void configureWindowBasic( JFrame frame,
168 final HubRunner[] runners ) {
169 frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
170 frame.addWindowListener( new WindowAdapter() {
171 public void windowClosed( WindowEvent evt ) {
172 HubRunner runner = runners[ 0 ];
173 if ( runner != null ) {
174 runner.shutdown();
175 }
176 }
177 } );
178 frame.setVisible( true );
179 }
180
181 /**
182 * Performs common configuration of hub display window with
183 * system tray functionality.
184 *
185 * @param frame hub window
186 * @param runners 1-element array which will contain an associated
187 * hub runner object if one exists
188 * @param sysTray system tray facade object
189 */
190
191 private static void configureWindowForSysTray( final JFrame frame,
192 final HubRunner[] runners,
193 final SysTray sysTray )
194 throws AWTException {
195
196 /* Prepare the items for display in the tray icon popup menu. */
197 final MenuItem showItem;
198 final MenuItem hideItem;
199 final MenuItem stopItem;
200 MenuItem[] items = new MenuItem[] {
201 showItem = new MenuItem( "Show Hub Window" ),
202 hideItem = new MenuItem( "Hide Hub Window" ),
203 stopItem = new MenuItem( "Stop Hub" ),
204 };
205 ActionListener iconListener = new ActionListener() {
206 public void actionPerformed( ActionEvent evt ) {
207 frame.setVisible( true );
208 showItem.setEnabled( false );
209 hideItem.setEnabled( true );
210 }
211 };
212
213 /* Construct and install the tray icon. */
214 Image im = Toolkit.getDefaultToolkit()
215 .createImage( Client.class.getResource( "images/hub.png" ) );
216 String tooltip = "SAMP Hub";
217 PopupMenu popup = new PopupMenu();
218 final Object trayIcon =
219 sysTray.addIcon( im, tooltip, popup, iconListener );
220
221 /* Arrange for the menu items to do something appropriate when
222 * triggered. */
223 ActionListener popListener = new ActionListener() {
224 public void actionPerformed( ActionEvent evt ) {
225 String cmd = evt.getActionCommand();
226 if ( cmd.equals( showItem.getActionCommand() ) ||
227 cmd.equals( hideItem.getActionCommand() ) ) {
228 boolean visible = cmd.equals( showItem.getActionCommand() );
229 frame.setVisible( visible );
230 showItem.setEnabled( ! visible );
231 hideItem.setEnabled( visible );
232 }
233 else if ( cmd.equals( stopItem.getActionCommand() ) ) {
234 HubRunner runner = runners[ 0 ];
235 if ( runner != null ) {
236 runner.shutdown();
237 }
238 try {
239 sysTray.removeIcon( trayIcon );
240 }
241 catch ( AWTException e ) {
242 logger_.warning( e.toString() );
243 }
244 frame.dispose();
245 }
246 }
247 };
248 for ( int i = 0; i < items.length; i++ ) {
249 items[ i ].addActionListener( popListener );
250 popup.add( items[ i ] );
251 }
252
253 /* Arrange that a manual window close will set the action states
254 * correctly. */
255 frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
256 frame.addWindowListener( new WindowAdapter() {
257 public void windowClosed( WindowEvent evt ) {
258 showItem.setEnabled( true );
259 hideItem.setEnabled( false );
260 }
261 } );
262
263 /* Set to initial state. */
264 popListener.actionPerformed(
265 new ActionEvent( frame, 0, hideItem.getActionCommand() ) );
266 }
267
268 /**
269 * Constructs a mode for BasicHubService.
270 *
271 * @return non-gui mode
272 */
273 private static HubMode createBasicHubMode( String name ) {
274 try {
275 return new HubMode( name, true ) {
276 HubService createHubService( Random random,
277 HubRunner[] runners ) {
278 return new BasicHubService( random );
279 }
280 };
281 }
282 catch ( Throwable e ) {
283 return new BrokenHubMode( name, e );
284 }
285 }
286
287 /**
288 * Constructs a mode for GuiHubService.
289 *
290 * @return mode without message tracking
291 */
292 private static HubMode createGuiHubMode( String name ) {
293 try {
294 GuiHubService.class.getName();
295 return new HubMode( name, false ) {
296 HubService createHubService( Random random,
297 final HubRunner[] runners ) {
298 return new GuiHubService( random ) {
299 public void start() {
300 super.start();
301 configureHubWindow( createHubWindow(), runners );
302 }
303 };
304 }
305 };
306 }
307 catch ( Throwable e ) {
308 return new BrokenHubMode( name, e );
309 }
310 }
311
312 /**
313 * Constructs a mode for MessageTrackerHubService.
314 *
315 * @return mode with message tracking
316 */
317 private static HubMode createMessageTrackerHubMode( String name ) {
318 try {
319 MessageTrackerHubService.class.getName();
320 return new HubMode( name, false ) {
321 HubService createHubService( Random random,
322 final HubRunner[] runners ) {
323 return new MessageTrackerHubService( random ) {
324 public void start() {
325 super.start();
326 configureHubWindow( createHubWindow(), runners );
327 }
328 };
329 }
330 };
331 }
332 catch ( Throwable e ) {
333 return new BrokenHubMode( name, e );
334 }
335 }
336
337 /**
338 * HubMode implemenetation for modes which cannot be used because they
339 * rely on classes unavailable at runtime.
340 */
341 private static class BrokenHubMode extends HubMode {
342 private final Throwable error_;
343
344 /**
345 * Constructor.
346 *
347 * @param name mode name
348 * @param error error explaining why mode is unavailable for use
349 */
350 BrokenHubMode( String name, Throwable error ) {
351 super( name, false );
352 error_ = error;
353 }
354 HubService createHubService( Random random, HubRunner[] runners ) {
355 throw new RuntimeException( "Hub mode " + getName()
356 + " unavailable", error_ );
357 }
358 }
359 }