1 /*
2 * Created on 09.mar.2003
3 *
4 */
5 package net.sf.panoptes.controller;
6
7 import java.lang.reflect.InvocationTargetException;
8 import java.util.Properties;
9
10 import net.sf.panoptes.model.component.ComponentUtil;
11 import net.sf.panoptes.model.component.registry.ComponentAlreadyExistsException;
12 import net.sf.panoptes.model.component.registry.MalformedComponentNameException;
13 import net.sf.panoptes.module.jmx.MBeanServerComponent;
14 import net.sf.panoptes.module.jmx.connector.MBeanServerConnector;
15 import net.sf.panoptes.module.jmx.wizard.NewNodeWizard;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.wizard.WizardDialog;
23 import org.eclipse.swt.widgets.Shell;
24
25 /***
26 * Controller for MBeanConnections
27 *
28 * @author Dag Liodden
29 *
30 */
31 public class MBeanConnectionController {
32
33 private MainController mainController;
34 private Log log = LogFactory.getLog(getClass());
35
36 public MBeanConnectionController(MainController mainController) {
37 this.mainController = mainController;
38 }
39
40 /***
41 * Runs the new MBeanComponent connection wizard.
42 *
43 */
44 public void newMBeanConnectionWizard() {
45 NewNodeWizard wiz = new NewNodeWizard(this);
46 WizardDialog dlg = new WizardDialog(new Shell(), wiz);
47 dlg.create();
48 dlg.open();
49 }
50
51 /***
52 * Called by the NewNodeWizard when it is done.
53 *
54 * @param name
55 * @param url
56 * @param rmiPort
57 * @param connectImmediately
58 */
59 public void addMBeanServerConnection(
60 String name,
61 Properties properties,
62 String connectorClass,
63 boolean connectImmediately) {
64 try {
65
66 MBeanServerConnector connector;
67 try {
68 connector = (MBeanServerConnector) Class.forName(connectorClass).newInstance();
69 } catch (ClassNotFoundException e) {
70 throw new Exception("Connector class '" + connectorClass + "' was not found", e);
71 }
72 connector.setProperties(properties);
73
74 MBeanServerComponent connection =
75 new MBeanServerComponent(connector);
76
77 connection.setName(name);
78 connection.setHostName((String) properties.get("hostname"));
79
80 mainController.getRootNode().getComponentRegistry().registerComponent(
81 connection,
82 ComponentUtil.createMBeanServerName(connection));
83 if (connectImmediately)
84 connectToMBeanServer(connection);
85 } catch (ComponentAlreadyExistsException e) {
86 mainController.displayError(
87 "Server already registered",
88 "You already have a connection to this server.");
89 } catch (MalformedComponentNameException e) {
90 log.error("Unable to create component name", e);
91 mainController.displayError(
92 "Internal failure",
93 "Unable to create a suitable name for this server.",
94 e);
95 } catch (Exception e) {
96 log.error("Unable to connect to JMX Server", e);
97 mainController.displayError("Unable to add server", e.getMessage());
98 }
99 }
100 /***
101
102 /*
103 * Helper method for connecting to an MBeanServer. Shows progress etc.
104 *
105 * @param conn the server to connect to
106 */
107 public void connectToMBeanServer(final MBeanServerComponent conn) {
108
109 /***
110 * Create worker thread
111 */
112 IRunnableWithProgress worker = new IRunnableWithProgress() {
113 public void run(IProgressMonitor monitor) throws InvocationTargetException {
114 try {
115 monitor.setTaskName("Connect to JMX Server.");
116 monitor.beginTask("Connecting", IProgressMonitor.UNKNOWN);
117
118 monitor.worked(IProgressMonitor.UNKNOWN);
119 // TODO: This SHOULD cause an indeterminate progress indicator but doesn't
120 conn.connect();
121 mainController.getMainWindow().refresh(mainController.getRootNode());
122 monitor.done();
123 } catch (Exception e) {
124 monitor.done();
125 log.error("Unable to connect", e);
126 mainController.displayError(
127 "Unable to connect",
128 "Unable to connect to JMX Server: " + e.getMessage(),
129 e);
130 }
131 }
132 };
133
134 /***
135 * Run worker thread
136 */
137 try {
138 new ProgressMonitorDialog(mainController.getMainWindow().getShell()).run(
139 false,
140 false,
141 worker);
142 } catch (InterruptedException e) {
143 e.printStackTrace();
144 } catch (InvocationTargetException e) {
145 e.printStackTrace();
146 }
147
148 }
149
150 }
This page was automatically generated by Maven