View Javadoc
1 package org.smartcomps.twister.deployer; 2 3 import org.dom4j.Document; 4 import org.dom4j.Node; 5 import org.dom4j.io.SAXReader; 6 7 import javax.swing.BorderFactory; 8 import javax.swing.Box; 9 import javax.swing.JButton; 10 import javax.swing.JFileChooser; 11 import javax.swing.JFrame; 12 import javax.swing.JPanel; 13 import javax.swing.JScrollBar; 14 import javax.swing.JScrollPane; 15 import javax.swing.JTextArea; 16 import javax.swing.JTextField; 17 import javax.swing.WindowConstants; 18 import javax.swing.filechooser.FileFilter; 19 import java.awt.Color; 20 import java.awt.Dimension; 21 import java.awt.GridBagConstraints; 22 import java.awt.GridBagLayout; 23 import java.awt.Insets; 24 import java.awt.Toolkit; 25 import java.awt.event.ActionEvent; 26 import java.awt.event.ActionListener; 27 import java.io.File; 28 import java.sql.DriverManager; 29 30 /*** 31 * UI Client to deploy process 32 */ 33 public class DeployerUI extends JPanel implements Runnable { 34 35 private JTextField choosenFileTextField = new JTextField("Enter process filename"); 36 37 private JButton chooseFileButton = new JButton("Choose ..."); 38 private JButton deployButton = new JButton("Deploy"); 39 private JButton exitButton = new JButton("Exit"); 40 private JButton retryButton = new JButton("Retry"); 41 42 // JLabel dbstatus = new JLabel(); 43 // private ImageIcon dbStatusOk = new ImageIcon("d://greenspot10_10.jpg"); 44 // private ImageIcon dbStatusWrk = new ImageIcon("d://orangespot10_10.jpg"); 45 // private ImageIcon dbStatusNok = new ImageIcon("d://redspot10_10.jpg"); 46 47 File selectedFile; 48 49 private Insets gbInsets = new Insets(0, 0, 5, 5); 50 private Insets marginInsets = new Insets(0, 0, 0, 0); 51 private int componentwidth = 65; 52 53 private JTextArea consoleTextArea = new JTextArea(); 54 private JScrollPane msgScrollPane = new JScrollPane(consoleTextArea); 55 56 private String databaseDriverClass; 57 private String databaseConnectionURL; 58 private String connectionUsername; 59 private String connectionPassword; 60 61 public DeployerUI() { 62 init(); 63 } 64 65 private void getDatabaseConfiguration() throws Exception { 66 Document doc = null; 67 SAXReader reader = new SAXReader(); 68 try { 69 doc = reader.read(getClass().getClassLoader().getResource("hibernate.cfg.xml")); 70 } catch (Exception e) { 71 throw new Exception("Unable to load the database configuration file", e); 72 } 73 74 // <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 75 Node connectionDriverClassNode = 76 doc.selectSingleNode("//hibernate-configuration/session-factory/property[@name=\"connection.driver_class\"]"); 77 if (connectionDriverClassNode == null) { 78 throw new Exception("The property 'connection.driver_class' is not defined in the hibernate.cfg.xml"); 79 } 80 databaseDriverClass = connectionDriverClassNode.getText(); 81 82 // <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> 83 Node connectionUrlNode = 84 doc.selectSingleNode("//hibernate-configuration/session-factory/property[@name=\"connection.url\"]"); 85 if (connectionUrlNode == null) { 86 throw new Exception("The property 'connection.url' is not defined in the hibernate.cfg.xml"); 87 } 88 databaseConnectionURL = connectionUrlNode.getText(); 89 90 // <property name="connection.username">sa</property> 91 Node connectionUsernameNode = 92 doc.selectSingleNode("//hibernate-configuration/session-factory/property[@name=\"connection.username\"]"); 93 if (connectionUsernameNode == null) { 94 throw new Exception("The property 'connection.username' is not defined in the hibernate.cfg.xml"); 95 } 96 connectionUsername = connectionUsernameNode.getText(); 97 98 // <property name="connection.password"></property> 99 Node connectionPasswordNode = 100 doc.selectSingleNode("//hibernate-configuration/session-factory/property[@name=\"connection.password\"]"); 101 if (connectionPasswordNode == null) { 102 throw new Exception("The property 'connection.password' is not defined in the hibernate.cfg.xml"); 103 } 104 connectionPassword = connectionPasswordNode.getText(); 105 } 106 107 public void run() { 108 consoleTextArea.setText(""); 109 // dbstatus.setToolTipText("Database connection ..."); 110 // dbstatus.setIcon(dbStatusWrk); 111 112 appendConsole("Try to connect to DB ...\n"); 113 try { 114 getDatabaseConfiguration(); 115 Class.forName(databaseDriverClass); 116 } catch (Exception e) { 117 // dbstatus.setIcon(dbStatusNok); 118 // dbstatus.setToolTipText("No database connection"); 119 appendConsole(e.getMessage() + "\n"); 120 appendConsole("No database connection\n"); 121 retryButton.setEnabled(true); 122 return; 123 } 124 try { 125 DriverManager.getConnection(databaseConnectionURL, connectionUsername, connectionPassword); 126 } catch (Exception e) { 127 // dbstatus.setIcon(dbStatusNok); 128 // dbstatus.setToolTipText("No database connection"); 129 appendConsole(e.getMessage() + "\n"); 130 appendConsole("No database connection\nCheck your database is running\n"); 131 retryButton.setEnabled(true); 132 return; 133 } 134 // dbstatus.setToolTipText("Database connected"); 135 // dbstatus.setIcon(dbStatusOk); 136 chooseFileButton.setEnabled(true); 137 appendConsole("Database connection established !\n"); 138 } 139 140 private void init() { 141 setBorder(BorderFactory.createTitledBorder("Twister process deployer")); 142 setLayout(new GridBagLayout()); 143 144 add(choosenFileTextField, new GridBagConstraints(0, 0, 5, 1, 1, 0, 145 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 146 gbInsets, 0, 0)); 147 148 chooseFileButton.addActionListener(new ActionListener() { 149 public void actionPerformed(ActionEvent e) { 150 chooseFile(); 151 } 152 }); 153 chooseFileButton.setPreferredSize(new Dimension(componentwidth, 23)); 154 chooseFileButton.setMargin(marginInsets); 155 chooseFileButton.setEnabled(false); 156 add(chooseFileButton, new GridBagConstraints(6, 0, 1, 1, 0, 0, 157 GridBagConstraints.CENTER, GridBagConstraints.NONE, 158 gbInsets, 0, 0)); 159 160 add(Box.createHorizontalStrut(componentwidth), new GridBagConstraints(0, 6, 1, 1, 0, 0, 161 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 162 gbInsets, 0, 0)); 163 164 retryButton.setPreferredSize(new Dimension(componentwidth, 23)); 165 retryButton.setMargin(marginInsets); 166 retryButton.setEnabled(false); 167 168 class ActionListenerImpl implements ActionListener { 169 private Runnable run; 170 171 public ActionListenerImpl(Runnable run) { 172 this.run = run; 173 } 174 175 public void actionPerformed(ActionEvent e) { 176 Thread t = new Thread(run); 177 t.start(); 178 retryButton.setEnabled(false); 179 } 180 } 181 182 retryButton.addActionListener(new ActionListenerImpl(this)); 183 add(retryButton, new GridBagConstraints(1, 6, 1, 1, 0, 0, 184 GridBagConstraints.CENTER, GridBagConstraints.NONE, 185 gbInsets, 0, 0)); 186 187 deployButton.setPreferredSize(new Dimension(componentwidth, 23)); 188 deployButton.setMargin(marginInsets); 189 deployButton.setEnabled(false); 190 deployButton.addActionListener(new ActionListener() { 191 public void actionPerformed(ActionEvent e) { 192 deployFile(); 193 } 194 }); 195 add(deployButton, new GridBagConstraints(2, 6, 1, 1, 0, 0, 196 GridBagConstraints.CENTER, GridBagConstraints.NONE, 197 gbInsets, 0, 0)); 198 199 exitButton.setPreferredSize(new Dimension(componentwidth, 23)); 200 exitButton.setMargin(marginInsets); 201 exitButton.addActionListener(new ActionListener() { 202 public void actionPerformed(ActionEvent e) { 203 System.exit(0); 204 } 205 }); 206 add(exitButton, new GridBagConstraints(3, 6, 1, 1, 0, 0, 207 GridBagConstraints.CENTER, GridBagConstraints.NONE, 208 gbInsets, 0, 0)); 209 210 // dbstatus.setIcon(dbStatusNok); 211 // dbstatus.setToolTipText("No database connection"); 212 // add(dbstatus, new GridBagConstraints(5, 1, 1, 1, 0, 0, 213 // GridBagConstraints.CENTER, GridBagConstraints.NONE, 214 // gbInsets, 0, 0)); 215 216 217 consoleTextArea.setEditable(false); 218 consoleTextArea.setBackground(Color.LIGHT_GRAY); 219 220 221 msgScrollPane.setBorder(BorderFactory.createTitledBorder("messages")); 222 // msgScrollPane.getHorizontalScrollBar().setVisible(true); 223 msgScrollPane.setAutoscrolls(true); 224 appendConsole("\n\n\n"); 225 add(msgScrollPane, new GridBagConstraints(0, 1, 7, 5, 0, 0, 226 GridBagConstraints.CENTER, GridBagConstraints.BOTH, 227 gbInsets, 0, 0)); 228 } 229 230 private void deployFile() { 231 selectedFile = new File(choosenFileTextField.getText()); 232 if (selectedFile.getPath().endsWith(".xml")) { 233 try { 234 TwisterDeployerFactory.getTwisterDeployer().deploy(selectedFile); 235 appendConsole("Deployment successful !"); 236 return; 237 } catch (Exception e) { 238 appendConsole(e.getMessage() + "\n"); 239 } 240 } else { 241 appendConsole("Please select an 'xml' file !\n"); 242 } 243 } 244 245 private void appendConsole(String s) { 246 consoleTextArea.append(s); 247 JScrollBar verticalScrollBar = msgScrollPane.getVerticalScrollBar(); 248 verticalScrollBar.setValue(verticalScrollBar.getMaximum()); 249 } 250 251 private void chooseFile() { 252 JFileChooser chooser = new JFileChooser(); 253 chooser.setFileFilter(new FileFilter() { 254 public String getDescription() { 255 return "xml process"; 256 } 257 258 public boolean accept(File f) { 259 boolean res = false; 260 if (f.getName().endsWith(".xml")) { 261 res = true; 262 } else if (f.isDirectory()) { 263 res = true; 264 } 265 return res; 266 } 267 }); 268 chooser.showOpenDialog(this); 269 selectedFile = chooser.getSelectedFile(); 270 if (selectedFile != null) { 271 deployButton.setEnabled(true); 272 choosenFileTextField.setText(selectedFile.getPath()); 273 } else { 274 appendConsole("Click Choose... to select a file !\n"); 275 } 276 } 277 278 public static void main(String[] args) { 279 JFrame f = new JFrame("Twister process deployer"); 280 f.setResizable(false); 281 // String frameIcon = "d://ticon.jpg"; 282 // f.setIconImage(new ImageIcon(frameIcon).getImage()); 283 DeployerUI ui = new DeployerUI(); 284 f.getContentPane().add(ui); 285 f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 286 f.pack(); 287 f.show(); 288 Dimension dScreen = Toolkit.getDefaultToolkit().getScreenSize(); 289 f.setLocation((dScreen.width - f.getWidth()) >> 1, (dScreen.height - f.getHeight()) >> 1); 290 Thread t = new Thread(ui); 291 t.start(); 292 } 293 } 294

This page was automatically generated by Maven