KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > distributed > util > BuildAgentUtility


1 package net.sourceforge.cruisecontrol.distributed.util;
2
3 import org.apache.log4j.Logger;
4 import net.jini.core.lookup.ServiceItem;
5 import net.sourceforge.cruisecontrol.distributed.BuildAgentService;
6
7 import javax.swing.JFrame JavaDoc;
8 import javax.swing.JPanel JavaDoc;
9 import javax.swing.JButton JavaDoc;
10 import javax.swing.JTextArea JavaDoc;
11 import javax.swing.JScrollPane JavaDoc;
12 import javax.swing.JComboBox JavaDoc;
13 import javax.swing.JCheckBox JavaDoc;
14 import javax.swing.ComboBoxModel JavaDoc;
15 import javax.swing.DefaultComboBoxModel JavaDoc;
16 import javax.swing.text.BadLocationException JavaDoc;
17 import javax.swing.SwingUtilities JavaDoc;
18 import java.rmi.RemoteException JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.WindowAdapter JavaDoc;
22 import java.awt.event.WindowEvent JavaDoc;
23 import java.awt.BorderLayout JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * Created by IntelliJ IDEA.
31  * User: drollo
32  * Date: Aug 1, 2005
33  * Time: 4:00:38 PM
34  * To change this template use File | Settings | File Templates.
35  */

36 public final class BuildAgentUtility {
37     private static final Logger LOG = Logger.getLogger(BuildAgentUtility.class);
38
39     // @todo make BuidAgentService implement/extend jini ServiceUI?
40
private static final class UI extends JFrame JavaDoc {
41         private static final int CONSOLE_LINE_BUFFER_SIZE = 1000;
42
43         private final BuildAgentUtility buildAgentUtility;
44         private final JPanel JavaDoc northPanel;
45         private final JButton JavaDoc btnRefresh = new JButton JavaDoc("Refresh");
46         private final JComboBox JavaDoc cmbAgents = new JComboBox JavaDoc();
47         private final JButton JavaDoc btnInvoke = new JButton JavaDoc("Invoke");
48         private static final String JavaDoc METH_RESTART = "restart";
49         private static final String JavaDoc METH_KILL = "kill";
50         private final JComboBox JavaDoc cmbRestartOrKill = new JComboBox JavaDoc(new String JavaDoc[] {METH_RESTART, METH_KILL});
51         private final JCheckBox JavaDoc chkAfterBuildFinished = new JCheckBox JavaDoc("Wait for build to finish.", true);
52         private final JButton JavaDoc btnInvokeOnAll = new JButton JavaDoc("Invoke on All");
53         private final JPanel JavaDoc pnlEdit = new JPanel JavaDoc(new BorderLayout JavaDoc());
54         private final JButton JavaDoc btnClose = new JButton JavaDoc("Close");
55         private final JTextArea JavaDoc txaConsole = new JTextArea JavaDoc();
56         private final JScrollPane JavaDoc scrConsole = new JScrollPane JavaDoc();
57
58         private UI(final BuildAgentUtility buildAgentUtil) {
59             super("CruiseControl Distributed - Build Agent Utility");
60
61             buildAgentUtility = buildAgentUtil;
62
63             btnClose.addActionListener(new ActionListener JavaDoc() {
64                 public void actionPerformed(final ActionEvent JavaDoc e) {
65                     exitForm();
66                 }
67             });
68             addWindowListener(new WindowAdapter JavaDoc() {
69                 public void windowClosing(final WindowEvent JavaDoc evt) {
70                     exitForm();
71                 }
72             });
73
74             btnRefresh.addActionListener(new ActionListener JavaDoc() {
75                 public void actionPerformed(final ActionEvent JavaDoc e) {
76                     refreshAgentList();
77                 }
78             });
79
80             cmbAgents.addActionListener(new ActionListener JavaDoc() {
81                 public void actionPerformed(final ActionEvent JavaDoc e) {
82                     btnInvoke.setEnabled(true);
83                 }
84             });
85
86             btnInvoke.setEnabled(false);
87             btnInvoke.addActionListener(new ActionListener JavaDoc() {
88                 public void actionPerformed(final ActionEvent JavaDoc e) {
89                     try {
90                         invokeOnAgent(
91                                 ((ComboItemWrapper) cmbAgents.getSelectedItem()).getAgent()
92                         );
93                     } catch (RemoteException JavaDoc e1) {
94                         appendInfo(e1.getMessage());
95                         throw new RuntimeException JavaDoc(e1);
96                     }
97                 }
98             });
99
100             btnInvokeOnAll.addActionListener(new ActionListener JavaDoc() {
101                 public void actionPerformed(final ActionEvent JavaDoc e) {
102                     for (int i = 0; i < cmbAgents.getItemCount(); i++) {
103                         try {
104                             invokeOnAgent(((ComboItemWrapper) cmbAgents.getItemAt(i)).getAgent());
105                         } catch (RemoteException JavaDoc e1) {
106                             appendInfo(e1.getMessage());
107                             //throw new RuntimeException(e1); // allow remaining items to be invoked
108
}
109                     }
110                 }
111             });
112
113             txaConsole.setFont(new Font JavaDoc("Courier New", 0, 12));
114
115             scrConsole.setViewportView(txaConsole);
116             scrConsole.setPreferredSize(new Dimension JavaDoc(525, 300));
117
118
119             getContentPane().setLayout(new BorderLayout JavaDoc());
120             final JPanel JavaDoc pnlNN = new JPanel JavaDoc(new BorderLayout JavaDoc());
121             pnlNN.add(btnRefresh, BorderLayout.WEST);
122             pnlNN.add(cmbAgents, BorderLayout.CENTER);
123             pnlNN.add(btnInvoke, BorderLayout.EAST);
124
125             final JPanel JavaDoc pnlNS = new JPanel JavaDoc(new BorderLayout JavaDoc());
126             pnlNS.add(btnClose, BorderLayout.EAST);
127
128             pnlEdit.add(cmbRestartOrKill, BorderLayout.WEST);
129             pnlEdit.add(chkAfterBuildFinished, BorderLayout.CENTER);
130             pnlEdit.add(btnInvokeOnAll, BorderLayout.EAST);
131
132             northPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
133             northPanel.add(pnlNN, BorderLayout.NORTH);
134             northPanel.add(pnlEdit, BorderLayout.CENTER);
135             northPanel.add(pnlNS, BorderLayout.SOUTH);
136             getContentPane().add(northPanel, BorderLayout.NORTH);
137             getContentPane().add(scrConsole, BorderLayout.CENTER);
138             pack();
139             setVisible(true);
140         }
141
142         private void invokeOnAgent(final BuildAgentService agent) throws RemoteException JavaDoc {
143             if (METH_RESTART.equals(cmbRestartOrKill.getSelectedItem())) {
144                 agent.restart(chkAfterBuildFinished.isSelected());
145             } else {
146                 agent.kill(chkAfterBuildFinished.isSelected());
147             }
148         }
149
150         private static final class ComboItemWrapper {
151             private static ComboItemWrapper[] wrapArray(final ServiceItem[] serviceItems) {
152                 final ComboItemWrapper[] result = new ComboItemWrapper[serviceItems.length];
153                 for (int i = 0; i < serviceItems.length; i++) {
154                     result[i] = new ComboItemWrapper(serviceItems[i]);
155                 }
156                 return result;
157             }
158
159             private final ServiceItem serviceItem;
160             private ComboItemWrapper(final ServiceItem serviceItemToWrap) {
161                 this.serviceItem = serviceItemToWrap;
162             }
163             public BuildAgentService getAgent() {
164                 return (BuildAgentService) serviceItem.service;
165             }
166             public String JavaDoc toString() {
167                 try {
168                     return getAgent().getMachineName() + ": " + serviceItem.serviceID;
169                 } catch (RemoteException JavaDoc e) {
170                     return "Error: " + e.getMessage();
171                 }
172             }
173         }
174
175         private void refreshAgentList() {
176             btnRefresh.setEnabled(false);
177             btnInvoke.setEnabled(false);
178             btnInvokeOnAll.setEnabled(false);
179             cmbAgents.setEnabled(false);
180             new Thread JavaDoc() {
181                 public void run() {
182                     try {
183                         final List JavaDoc tmpList = new ArrayList JavaDoc();
184                         final String JavaDoc agentInfoAll = buildAgentUtility.getAgentInfoAll(tmpList);
185                         final ServiceItem[] serviceItems = (ServiceItem[]) tmpList.toArray(new ServiceItem[]{});
186                         final ComboBoxModel JavaDoc comboBoxModel = new DefaultComboBoxModel JavaDoc(
187                                 ComboItemWrapper.wrapArray(serviceItems));
188                         SwingUtilities.invokeLater(new Runnable JavaDoc() {
189                             public void run() {
190                                 cmbAgents.setModel(comboBoxModel);
191                             }
192                         });
193                         setInfo(agentInfoAll);
194                     } finally {
195                         SwingUtilities.invokeLater(new Runnable JavaDoc() {
196                             public void run() {
197                                 btnRefresh.setEnabled(true);
198                                 btnInvokeOnAll.setEnabled(true);
199                                 cmbAgents.setEnabled(true);
200                             }
201                         });
202                     }
203                 }
204             } .start();
205         }
206
207         private static void exitForm() {
208             System.exit(0);
209         }
210
211         private void setInfo(final String JavaDoc infoText) {
212             LOG.debug(infoText);
213             SwingUtilities.invokeLater(new Runnable JavaDoc() {
214                 public void run() {
215                     txaConsole.setText(infoText);
216                 }
217             });
218         }
219
220         private void appendInfo(final String JavaDoc infoText) {
221             SwingUtilities.invokeLater(new Runnable JavaDoc() {
222                 public void run() {
223                     txaConsole.append(infoText + "\n");
224                     if (txaConsole.getLineCount() > CONSOLE_LINE_BUFFER_SIZE) {
225                         // remove old lines
226
try {
227                             txaConsole.replaceRange("", 0,
228                                     txaConsole.getLineEndOffset(
229                                             txaConsole.getLineCount() - CONSOLE_LINE_BUFFER_SIZE
230                                     ));
231                         } catch (BadLocationException JavaDoc e) {
232                             ; //ignore
233
}
234                     }
235                     // Make sure the last line is always visible
236
txaConsole.setCaretPosition(txaConsole.getDocument().getLength());
237                 }
238             });
239
240         }
241     }
242
243     private final UI ui;
244
245
246     private BuildAgentUtility() {
247         ui = new UI(this);
248         ui.btnRefresh.doClick();
249     }
250
251     private String JavaDoc getAgentInfoAll(final List JavaDoc lstServiceItems) {
252         final String JavaDoc waitMessage = "Waiting 5 seconds for registrars to report in...";
253         ui.setInfo(waitMessage);
254         LOG.info(waitMessage);
255
256         final StringBuffer JavaDoc result = new StringBuffer JavaDoc();
257         try {
258             // Use new instance each time to avoid stale cached info
259
final MulticastDiscovery discovery = new MulticastDiscovery(null);
260             try {
261                 Thread.sleep(5000);
262             } catch (InterruptedException JavaDoc e1) {
263                 LOG.warn("Sleep interrupted", e1);
264             }
265
266             final ServiceItem[] serviceItems
267                     = discovery.getLookupCache().lookup(MulticastDiscovery.FLTR_ANY, Integer.MAX_VALUE);
268             // don't wait for the terminate
269
new Thread JavaDoc() {
270                 public void run() {
271                     discovery.terminate();
272                 }
273             } .start();
274
275             // clear and rebuild list
276
for (int i = 0; i < lstServiceItems.size(); i++) {
277                 lstServiceItems.remove(i);
278             }
279             for (int i = 0; i < serviceItems.length; i++) {
280                 lstServiceItems.add(serviceItems[i]);
281             }
282
283             result.append("Found: " + serviceItems.length + " agents.\n");
284             ServiceItem serviceItem;
285             BuildAgentService agent;
286             String JavaDoc agentInfo;
287             for (int x = 0; x < serviceItems.length; x++) {
288                 serviceItem = serviceItems[x];
289                 agent = (BuildAgentService) serviceItem.service;
290                 agentInfo = "Build Agent: " + serviceItem.serviceID + "\n"
291                         + agent.asString()
292                         + MulticastDiscovery.toStringEntries(serviceItem.attributeSets)
293                         + "\n";
294                 LOG.debug(agentInfo);
295                 result.append(agentInfo);
296             }
297         } catch (RemoteException JavaDoc e) {
298             final String JavaDoc message = "Search failed due to an unexpected error";
299             LOG.error(message, e);
300             throw new RuntimeException JavaDoc(message, e);
301         }
302
303         return result.toString();
304     }
305
306     public static void main(final String JavaDoc[] args) {
307         new BuildAgentUtility();
308     }
309 }
310
Popular Tags