DispatcherUI.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * DispatcherUI.java
  10. *
  11. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. */
  15. import java.awt.BorderLayout;
  16. import java.awt.Dimension;
  17. import java.awt.GridBagConstraints;
  18. import java.awt.GridBagLayout;
  19. import java.awt.GridLayout;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.WindowAdapter;
  23. import java.awt.event.WindowEvent;
  24. import java.io.File;
  25. import java.net.URL;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.ImageIcon;
  28. import javax.swing.JButton;
  29. import javax.swing.JComboBox;
  30. import javax.swing.JEditorPane;
  31. import javax.swing.JFrame;
  32. import javax.swing.JLabel;
  33. import javax.swing.JMenu;
  34. import javax.swing.JMenuBar;
  35. import javax.swing.JMenuItem;
  36. import javax.swing.JOptionPane;
  37. import javax.swing.JPanel;
  38. import javax.swing.JScrollPane;
  39. import javax.swing.JTextArea;
  40. import javax.swing.JTextField;
  41. import com.cognos.developer.schemas.bibus._3.Dispatcher_Type;
  42. public class DispatcherUI extends JFrame {
  43. private CRNConnect connect;
  44. private static DispatcherUI frame;
  45. // The following variables represent the dialog components.
  46. private JTextArea textAreaPane;
  47. private JTextField cmURL;
  48. private JComboBox dispSearchPath;
  49. private JButton runButton;
  50. private JComboBox dispatcherOptions;
  51. private static Logon sessionLogon;
  52. private static final String DISPATCHER_PING = "Ping Dispatcher";
  53. private static final String DISPATCHER_PROPERTY = "Dispatcher Properties";
  54. private static final String DISPATCHER_START_SERVICE = "Start Service";
  55. private static final String DISPATCHER_STOP_SERVICE = "Stop Service";
  56. private static final String DISPATCHER_SET_LOGGING_LEVEL = "Set Logging Level";
  57. private static final String DISPATCHER_SET_MAX_PROCESSES = "Set Maximum Process";
  58. private static final int DISPATCHER_ENUM_PING = 1;
  59. private static final int DISPATCHER_ENUM_PROPERTY = 2;
  60. private static final int DISPATCHER_ENUM_START_SERVICE = 3;
  61. private static final int DISPATCHER_ENUM_STOP_SERVICE = 4;
  62. private static final int DISPATCHER_ENUM_SET_LOGGING_LEVEL = 5;
  63. private static final int DISPATCHER_ENUM_SET_MAX_PROCESSES = 6;
  64. private static int dispatcherType = 0;
  65. private String chosenDispMethod = null;
  66. public String DispSearchPath_URL = null;
  67. Dispatcher newDispatcher = new Dispatcher();
  68. // This is the constructor.
  69. public DispatcherUI(String title, CRNConnect connection) {
  70. // Set the title of the frame, even before the variables are declared.
  71. super(title);
  72. connect = connection;
  73. addComponents();
  74. }
  75. // Add all components to the frame's panel.
  76. private void addComponents() {
  77. JMenuBar mBar = new JMenuBar();
  78. this.setJMenuBar(mBar);
  79. // declare menuItems
  80. JMenuItem exit;
  81. JMenuItem about;
  82. JMenuItem overview;
  83. // Add and populate the File menu.
  84. JMenu fileMenu = new JMenu("File");
  85. mBar.add(fileMenu);
  86. exit = new JMenuItem("Exit");
  87. fileMenu.add(exit);
  88. exit.addActionListener(new MenuHandler());
  89. // Add and populate the Help menu.
  90. JMenu helpMenu = new JMenu("Help");
  91. mBar.add(helpMenu);
  92. about = new JMenuItem("About");
  93. helpMenu.add(about);
  94. about.addActionListener(new MenuHandler());
  95. overview = new JMenuItem("Overview");
  96. helpMenu.add(overview);
  97. overview.addActionListener(new MenuHandler());
  98. JPanel mainPanel = createMainPanel();
  99. JPanel outputPanel = createOutputPanel();
  100. JPanel panel = new JPanel(new BorderLayout());
  101. panel.add(mainPanel, BorderLayout.NORTH);
  102. panel.add(outputPanel);
  103. setContentPane(panel);
  104. }
  105. private JPanel createOutputPanel() {
  106. // Add the status text pane.
  107. textAreaPane = new JTextArea();
  108. // Add the ScrollPane to outputPanel
  109. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  110. areaScrollPane
  111. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  112. areaScrollPane.setPreferredSize(new Dimension(500, 275));
  113. //Create the output panel and it's layout objects
  114. GridBagLayout layout = new GridBagLayout(); //GridLayout(0, 1)
  115. JPanel outputNavPanel = new JPanel(layout);
  116. GridBagConstraints layoutConstraints = new GridBagConstraints();
  117. //Set the layout for the scroll pane and add it
  118. layoutConstraints.weightx = 1.0;
  119. layoutConstraints.weighty = 1.0;
  120. layoutConstraints.fill = GridBagConstraints.BOTH;
  121. layout.setConstraints(areaScrollPane, layoutConstraints);
  122. outputNavPanel.add(areaScrollPane);
  123. outputNavPanel.setBorder(BorderFactory.createCompoundBorder(
  124. BorderFactory.createCompoundBorder(BorderFactory
  125. .createTitledBorder("Output"), BorderFactory
  126. .createEmptyBorder(5, 5, 5, 5)), outputNavPanel
  127. .getBorder()));
  128. return outputNavPanel;
  129. }
  130. private JPanel createMainPanel() {
  131. // Add the URL text field and label
  132. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  133. cmURL.setText(CRNConnect.CM_URL);
  134. cmURL.setEditable(false);
  135. // Put together a panel for the URl
  136. JPanel cmURLPanel = new JPanel();
  137. cmURLPanel.add(new JLabel("Server URL:"));
  138. cmURLPanel.add(cmURL);
  139. String[] dispatcherList = newDispatcher.getAvailableDispatcher(connect,
  140. "//dispatcher");
  141. dispSearchPath = new JComboBox(dispatcherList);
  142. dispSearchPath.setSelectedItem(dispatcherList[0]);
  143. dispSearchPath.addActionListener(new dispatcherSearchPathHandler());
  144. // create dispatcher search path panel
  145. JPanel dispatcherPanel = new JPanel();
  146. dispatcherPanel.add(new JLabel("Dispatcher Search Path:"),
  147. BorderLayout.WEST);
  148. dispatcherPanel.add(dispSearchPath, BorderLayout.EAST);
  149. //get dropdown box and button panel
  150. JPanel optionButtonPanel = createMainButtonPanel();
  151. //create the main panel and add the components
  152. JPanel mainPanel = new JPanel(new GridLayout(3, 0));
  153. // Add the panels to the mainPanel
  154. mainPanel.add(cmURLPanel);
  155. mainPanel.add(dispatcherPanel);
  156. mainPanel.add(optionButtonPanel);
  157. return mainPanel;
  158. }
  159. private JPanel createMainButtonPanel() {
  160. // Create the button Panel
  161. JPanel buttonPanel = new JPanel();
  162. // Create and add the package output type combo box
  163. String[] dispOptions = { DISPATCHER_PING, DISPATCHER_PROPERTY,
  164. DISPATCHER_START_SERVICE, DISPATCHER_STOP_SERVICE,
  165. DISPATCHER_SET_LOGGING_LEVEL, DISPATCHER_SET_MAX_PROCESSES };
  166. dispatcherOptions = new JComboBox(dispOptions);
  167. dispatcherOptions.setSelectedItem(null);
  168. dispatcherOptions.addActionListener(new dispatcherSelectionHandler());
  169. buttonPanel.add(new JLabel("Dispatcher Options:"), BorderLayout.WEST);
  170. buttonPanel.add(dispatcherOptions, BorderLayout.WEST);
  171. // Create and add the Button
  172. runButton = new JButton("Run Option");
  173. runButton.addActionListener(new allButtonsHandler());
  174. buttonPanel.add(runButton, BorderLayout.EAST);
  175. return buttonPanel;
  176. }
  177. // handle menu bar buttons
  178. private class MenuHandler implements ActionListener {
  179. public void actionPerformed(ActionEvent e) {
  180. if (e.getActionCommand().startsWith("http://")) {
  181. connect.connectionChange(e.getActionCommand());
  182. }
  183. try {
  184. JMenuItem menuClicked = (JMenuItem) e.getSource();
  185. if (menuClicked.getText() == "Exit") {
  186. System.exit(0);
  187. }
  188. if (menuClicked.getText() == "About") {
  189. JOptionPane.showMessageDialog(((JMenuItem) e.getSource())
  190. .getParent(), "IBM Cognos Sample Application\n\n"
  191. + "Version 1.0.0\n"
  192. + "This application uses the IBM Cognos Software Development Kit",
  193. "About IBM Cognos Samples",
  194. JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
  195. "../Common/about.gif"));
  196. }
  197. if (menuClicked.getText().compareTo("Overview") == 0) {
  198. JFrame explainWindow = new JFrame(
  199. "Overview for Dispatcher Sample");
  200. File explainFile = new File(
  201. "Java_DispatcherUI_Explain.html");
  202. if (!explainFile.exists()) {
  203. JOptionPane.showMessageDialog(null,
  204. "Explain file not found");
  205. return;
  206. }
  207. URL explainURL = new URL("file:///"
  208. + explainFile.getAbsolutePath());
  209. JEditorPane explainPane = new JEditorPane();
  210. explainPane.setPage(explainURL);
  211. explainPane.setEditable(false);
  212. JScrollPane explainScroll = new JScrollPane(explainPane,
  213. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  214. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  215. explainWindow.getContentPane().add(explainScroll);
  216. explainWindow.setSize(640, 480);
  217. explainWindow.setVisible(true);
  218. }
  219. } catch (Exception ex) {
  220. }
  221. }
  222. }
  223. // The following is the button event handler.
  224. private class allButtonsHandler implements ActionListener {
  225. public void actionPerformed(ActionEvent e) {
  226. if (!Logon.loggedIn(connect)) {
  227. try {
  228. sessionLogon.logon(connect);
  229. } catch (Exception logonException) {
  230. System.out.println("The error: "
  231. + logonException.getMessage());
  232. }
  233. }
  234. JButton buttonPressed = ((JButton) e.getSource());
  235. // Click 'Run Option' button
  236. if (buttonPressed == runButton) {
  237. textAreaPane.setText("");
  238. DispSearchPath_URL = (String) dispSearchPath.getSelectedItem();
  239. if (DispSearchPath_URL == null || dispatcherType == 0) {
  240. return;
  241. }
  242. textAreaPane.setText("");
  243. switch (dispatcherType) {
  244. case 1: // Ping Dispatcher
  245. String dispatcherVersion = newDispatcher
  246. .getDispatcherVersion(connect, DispSearchPath_URL);
  247. dispatcherOptions.setSelectedItem(null);
  248. if (dispatcherVersion != null)
  249. textAreaPane.setText("The Dispatcher version is: "
  250. + dispatcherVersion);
  251. else
  252. textAreaPane
  253. .setText("Failed to access and run a dispatcher."
  254. + "\nPlease check to make sure the dispatcher search path is valid.");
  255. dispatcherType = 0;
  256. break;
  257. case 2: // Dispatcher status
  258. Dispatcher_Type dispatcherStatus = newDispatcher
  259. .getDispatcherStatus(connect, DispSearchPath_URL);
  260. dispatcherOptions.setSelectedItem(null);
  261. if (dispatcherStatus != null) {
  262. textAreaPane
  263. .setText("The following lists some of Dispatcher properties: ");
  264. textAreaPane
  265. .append("\n\nThe Dispatcher search path: "
  266. + dispatcherStatus.getSearchPath()
  267. .getValue()
  268. + "\nThe number of connections that can use to execute high-affinity requests: "
  269. + dispatcherStatus
  270. .getRsAffineConnections()
  271. .getValue()
  272. + "\nThe maximum number of processes for the Batch Report Service: "
  273. + dispatcherStatus
  274. .getBrsMaximumProcesses()
  275. .getValue()
  276. + "\nThe Dispatcher path: "
  277. + dispatcherStatus.getDispatcherPath()
  278. .getValue());
  279. } else {
  280. textAreaPane
  281. .setText("Failed to get Dispatcher status."
  282. + "\nPlease check to make sure the dispatcher search path is valid.");
  283. }
  284. dispatcherType = 0;
  285. break;
  286. case 3: // Start Service
  287. String mySearchPath = null;
  288. BaseClassWrapper[] serviceList = newDispatcher.getService(
  289. connect, "//dispatcher/*");
  290. // the Service list
  291. Object selectedService = JOptionPane.showInputDialog(null,
  292. "Select the service", null,
  293. JOptionPane.INFORMATION_MESSAGE, null, serviceList,
  294. null);
  295. if (selectedService != null) {
  296. BaseClassWrapper serviceName = (BaseClassWrapper) selectedService;
  297. mySearchPath = serviceName.getBaseClassObject()
  298. .getSearchPath().getValue();
  299. boolean startService = newDispatcher.startService(
  300. connect, mySearchPath);
  301. dispatcherOptions.setSelectedItem(null);
  302. if (startService) {
  303. textAreaPane.setText("The "
  304. + serviceName.toString()
  305. + " has been successfully started.");
  306. } else {
  307. textAreaPane.setText(serviceName.toString()
  308. + " could not be started.");
  309. }
  310. dispatcherType = 0;
  311. }
  312. break;
  313. case 4: // Stop Service
  314. String mySearchPath1 = null;
  315. BaseClassWrapper[] serviceList1 = newDispatcher.getService(
  316. connect, "//dispatcher/*");
  317. // the Service list
  318. Object selectedService1 = JOptionPane.showInputDialog(null,
  319. "Select the service", null,
  320. JOptionPane.INFORMATION_MESSAGE, null,
  321. serviceList1, null);
  322. if (selectedService1 != null) {
  323. BaseClassWrapper serviceName1 = (BaseClassWrapper) selectedService1;
  324. mySearchPath1 = serviceName1.getBaseClassObject()
  325. .getSearchPath().getValue();
  326. boolean stopService = newDispatcher.stopService(
  327. connect, mySearchPath1);
  328. dispatcherOptions.setSelectedItem(null);
  329. if (stopService) {
  330. textAreaPane.setText("The "
  331. + serviceName1.toString()
  332. + " has been successfully stopped.");
  333. } else {
  334. textAreaPane.setText(serviceName1.toString()
  335. + " could not be stopped.");
  336. }
  337. dispatcherType = 0;
  338. }
  339. break;
  340. case 5: // Set Logging Level
  341. String setLoggingResult = null;
  342. String[] myServices = { "AgentService",
  343. "BatchReportService", "ContentManagerService",
  344. "DataIntegrationService", "dispatcher",
  345. "DeliveryService", "EventManagementService",
  346. "IndexDataService", "IndexSearchService",
  347. "IndexUpdateService", "JobService",
  348. "MobileService", "MetadataService",
  349. "MetricsManagerService", "MonitorService",
  350. "PlanningAdministrationConsoleService",
  351. "PlanningRuntimeService", "PresentationService",
  352. "PlanningTaskService", "reportDataService",
  353. "ReportService", "SystemService" };
  354. Object selectedService2 = JOptionPane.showInputDialog(null,
  355. "Please select the service",
  356. "Services for setting logging level",
  357. JOptionPane.INFORMATION_MESSAGE, null, myServices,
  358. null);
  359. if (selectedService2 != null) {
  360. String myLoggingLevel;
  361. String myServiceName = (String) selectedService2;
  362. Object[] levelOptions = { "Minimal", "Basic",
  363. "Request", "Trace", "Full" };
  364. Object selectedLevel = JOptionPane.showInputDialog(
  365. null, "Please choose setting level",
  366. "Setting Level",
  367. JOptionPane.INFORMATION_MESSAGE, null,
  368. levelOptions, null);
  369. if (selectedLevel == null) {
  370. return;
  371. } else {
  372. myLoggingLevel = (String) selectedLevel;
  373. }
  374. if (myLoggingLevel != null) {
  375. // setting logging level as 'basic' level
  376. setLoggingResult = newDispatcher.setLoggingLevel(
  377. connect, DispSearchPath_URL, myServiceName,
  378. myLoggingLevel);
  379. }
  380. dispatcherOptions.setSelectedItem(null);
  381. if (setLoggingResult != null) {
  382. textAreaPane.setText("The logging level for "
  383. + myServiceName
  384. + " has been successfully set to '"
  385. + myLoggingLevel + "' level"
  386. + "\nThe Event ID: " + setLoggingResult);
  387. } else {
  388. textAreaPane
  389. .setText("Failed to set logging level for "
  390. + myServiceName
  391. + "."
  392. + "\nPlease check to make sure the dispatcher search path is valid.");
  393. }
  394. dispatcherType = 0;
  395. }
  396. break;
  397. case 6: // Set Maximum Processes
  398. String maxProcessStr = null;
  399. String[] availableServices = { "BatchReportService",
  400. "ReportService" };
  401. // the Service list
  402. Object chosenService = JOptionPane.showInputDialog(null,
  403. "Please select the service",
  404. "Services for setting max processes",
  405. JOptionPane.INFORMATION_MESSAGE, null,
  406. availableServices, null);
  407. if (chosenService != null) {
  408. String serviceName = (String) chosenService;
  409. Object[] numList = { "2", "3", "4", "5" };
  410. Object selectedMaxProcessNum = JOptionPane
  411. .showInputDialog(null,
  412. "Please choose the maximum process",
  413. "Setting maximum processes",
  414. JOptionPane.INFORMATION_MESSAGE, null,
  415. numList, null);
  416. maxProcessStr = (String) selectedMaxProcessNum;
  417. if (maxProcessStr != null) {
  418. // setting the maximum number of processes
  419. String setMaxProcesses = newDispatcher
  420. .setMaxProcesses(connect,
  421. DispSearchPath_URL, serviceName,
  422. maxProcessStr);
  423. dispatcherOptions.setSelectedItem(null);
  424. if (setMaxProcesses != null) {
  425. textAreaPane
  426. .setText("The maximum number of processes for the "
  427. + serviceName
  428. + " is successfully set to "
  429. + maxProcessStr
  430. + "\nThe Event ID: "
  431. + setMaxProcesses);
  432. } else {
  433. textAreaPane
  434. .setText("Failed to set maximum number of processesfor the Batch Report Service."
  435. + "\nPlease check to make sure the dispatcher search path is valid.");
  436. }
  437. } else {
  438. return;
  439. }
  440. dispatcherType = 0;
  441. }
  442. break;
  443. }
  444. }
  445. }
  446. }
  447. // This is the the available dispatcher combo box event handler
  448. private class dispatcherSearchPathHandler implements ActionListener {
  449. public void actionPerformed(ActionEvent dispatcherSearchPathEvent) {
  450. textAreaPane.setText("");
  451. DispSearchPath_URL = (String) dispSearchPath.getSelectedItem();
  452. }
  453. }
  454. // This is the dispatcher option combo box event handler.
  455. private class dispatcherSelectionHandler implements ActionListener {
  456. public void actionPerformed(ActionEvent dispatcherSelectedEvent) {
  457. textAreaPane.setText("");
  458. chosenDispMethod = (String) dispatcherOptions.getSelectedItem();
  459. if (chosenDispMethod == DISPATCHER_PING) {
  460. dispatcherType = DISPATCHER_ENUM_PING;
  461. } else if (chosenDispMethod == DISPATCHER_PROPERTY) {
  462. dispatcherType = DISPATCHER_ENUM_PROPERTY;
  463. } else if (chosenDispMethod == DISPATCHER_START_SERVICE) {
  464. dispatcherType = DISPATCHER_ENUM_START_SERVICE;
  465. } else if (chosenDispMethod == DISPATCHER_STOP_SERVICE) {
  466. dispatcherType = DISPATCHER_ENUM_STOP_SERVICE;
  467. } else if (chosenDispMethod == DISPATCHER_SET_LOGGING_LEVEL) {
  468. dispatcherType = DISPATCHER_ENUM_SET_LOGGING_LEVEL;
  469. } else if (chosenDispMethod == DISPATCHER_SET_MAX_PROCESSES) {
  470. dispatcherType = DISPATCHER_ENUM_SET_MAX_PROCESSES;
  471. }
  472. }
  473. }
  474. // Create the main method to execute the application.
  475. public static void main(String args[]) {
  476. CRNConnect connection = new CRNConnect();
  477. connection.connectToCognosServer();
  478. sessionLogon = new Logon();
  479. String logonOutput = "";
  480. while (!Logon.loggedIn(connection)) {
  481. logonOutput = sessionLogon.logon(connection);
  482. if (!Logon.loggedIn(connection)) {
  483. int retry = JOptionPane.showConfirmDialog(null,
  484. "Login Failed. Please try again.", "Login Failed",
  485. JOptionPane.OK_CANCEL_OPTION);
  486. if (retry != JOptionPane.OK_OPTION) {
  487. System.exit(0);
  488. }
  489. }
  490. }
  491. frame = new DispatcherUI("IBM Cognos Sample", connection);
  492. // Create a WindowAdapter so the application
  493. // is exited when the window is closed.
  494. frame.addWindowListener(new WindowAdapter() {
  495. public void windowClosing(WindowEvent e) {
  496. System.exit(0);
  497. }
  498. });
  499. frame.textAreaPane.setText(logonOutput);
  500. // Set the size of the frame and display it.
  501. frame.setSize(750, 440);
  502. frame.setVisible(true);
  503. frame.setResizable(true);
  504. }
  505. }