Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/swing/ScriptExecutorWizard.java


1   /*
2    * ScriptExecutorWizard.java
3    *
4    * Created on 14 gennaio 2002, 18.37
5       Javatools (modified version) - Some useful general classes.
6       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package javatools.swing;
26  
27  import java.io.*;
28  import javatools.applications.DBScriptExec.DBScriptExec;
29  import javatools.util.Props;
30  import javatools.util.ScriptCatcher;
31  import javatools.db.*;
32  
33  /**
34   * The wizard to manage collecting information for executing a script.
35   * It also executes it.
36   * @author Antonio Petrelli
37   * @version 0.1.10
38   */
39  public class ScriptExecutorWizard extends javatools.swing.AbstractWizard {
40  
41      /** Creates new ScriptExecutorWizard */
42      public ScriptExecutorWizard() {
43          javatoolsBundle = java.util.ResourceBundle.getBundle("res/JavatoolsBundle");
44          pfn = null;
45          wizPanels = new InputPanel[4];
46          wizPanels[0] = new PresentationPanel();
47          wizPanels[1] = new DBMSSpecificationPanel();
48          wizPanels[2] = new DBMSUserInfoPanel();
49          wizPanels[3] = new PresentationPanel();
50          wizPanels[0].setDescription("Presentation");
51          wizPanels[1].setDescription(javatoolsBundle.getString("DBMS_information"));
52          wizPanels[2].setDescription(javatoolsBundle.getString("DBMS_access_information"));
53          wizPanels[3].setDescription(javatoolsBundle.getString("Finished"));
54          wizPanels[1].setValue("ListFileName", "availableDBMS.script");
55          wizPanels[1].setValue("PropsFileName", "availableDBMS");
56          wizPanels[1].setValue("DBMSAddress", "localhost");
57          wizPanels[1].setValue("DBMSPort", "-1");
58          wizPanels[2].setValue("UserName", "");
59          wizPanels[2].setValue("Password", "");
60          initPanels();
61          setEnabledFinishButton(false);
62      }
63  
64      /** Executes the script.
65       * @throws WizardException If something goes wrong.
66       */    
67      public void execute() throws WizardException {
68          Props dbProps;
69          boolean success;
70          
71          if (pfn != null) {
72              success = true;
73              getParams();
74              if (!pathNeeded)
75                  DBScriptExec.setParams(selectedDbms, dbName, dbmsDriver,
76                      dbmsAddress, userName, password, port, scriptFileName);
77              else
78                  DBScriptExec.setParams(selectedDbms, dbDir, dbName, dbmsDriver,
79                      dbmsAddress, userName, password, port, scriptFileName);
80              try {
81                  DBScriptExec.runScript();
82              }
83              catch (DbException e) {
84                  success = false;
85              }
86              if (success) {
87                  try {
88                      dbProps = Props.singleton(pfn, false);
89                      dbProps.setProperty("dbms", selectedDbms);
90                      dbProps.setProperty("dbmsAddress", dbmsAddress);
91                      dbProps.setProperty("dbDir", dbDir);
92                      dbProps.setProperty("dbName", dbName);
93                      dbProps.setProperty("driver", dbmsDriver);
94                      dbProps.setProperty("port", Integer.toString(port));
95                      dbProps.setProperty("userName", userName);
96                      dbProps.setProperty("password", password);
97                      dbProps.store();
98                  }
99                  catch (IOException e) {
100                     System.out.println(e.getMessage());
101                     success = false;
102                 }
103             }
104             if (success)
105                 javax.swing.JOptionPane.showMessageDialog(null, successMessage);
106             else
107                 javax.swing.JOptionPane.showMessageDialog(null, failureMessage);
108         }
109         else
110             throw new WizardException("Properties file name not specified.");
111     }
112     
113     /** Sets the properties filename containing script filenames.
114      * See "HOW TO USE" file to know how to use it.
115      * @param name The filename needed.
116      */    
117     public void setScriptListFileName(String name) {
118         scpList = name;
119     }
120     
121     /** Sets the properties file name in which options will be stored.
122      * @param propsFileName The filename to use, without the ".properties" extension.
123      */    
124     public void setPropsFileName(String propsFileName) {
125         pfn = propsFileName;
126     }
127     
128     /** Loads the presentation text from a file.
129      * @param fileName The filename of the file containing the presentation text.
130      * @throws WizardException If file does not exist.
131      */    
132     public void loadPresentationText(String fileName) throws WizardException {
133         String tempString;
134         
135         try {
136             tempString = ScriptCatcher.textFile2String(fileName);
137         }
138         catch (FileNotFoundException e) {
139             throw new WizardException(e.getMessage());
140         }
141         if (wizPanels != null && wizPanels[0] != null) {
142             wizPanels[0].setValue("PresentationText", tempString);
143         }
144     }
145     
146     /** Loads the text to be displayed in the last panel.
147      * @param fileName The filename of the file containing the text.
148      * @throws WizardException If the file does not exist
149      */    
150     public void loadFinishText(String fileName) throws WizardException {
151         String tempString;
152         
153         try {
154             tempString = ScriptCatcher.textFile2String(fileName);
155         }
156         catch (FileNotFoundException e) {
157             throw new WizardException(e.getMessage());
158         }
159         if (wizPanels != null && wizPanels[3] != null) {
160             wizPanels[3].setValue("PresentationText", tempString);
161         }
162     }
163     
164     /** Sets the defaul database name.
165      * @param defaultName The default database name.
166      */    
167     public void setDefaultDatabaseName(String defaultName) {
168         setValue("DatabaseName", defaultName);
169     }
170     
171     /** Sets the properties filename containing default values.
172      * @param fileName The filename containing the properties.
173      */    
174     public void setDefaultPropertiesFileName(String fileName) {
175         Props tempProps;
176         
177         try {
178             tempProps = Props.singleton(fileName);
179             setDefaultDatabaseName(tempProps.getProperty("databaseName"));
180         }
181         catch (IOException e) {
182             System.out.println(e.getMessage());
183         }
184     }
185     
186     /** Sets the message to display when everything went OK.
187      * @param message The message to display.
188      */    
189     public void setSuccessMessage(String message) {
190         successMessage = message;
191     }
192     
193     /** Sets the message to display when something went wrong.
194      * @param message The message to display.
195      */    
196     public void setFailureMessage(String message) {
197         failureMessage = message;
198     }
199 
200     private void getParams() {
201         Props scpListProps;
202         String driverFileName, localConfigDir;
203         DbManager manager;
204         String dbPrefix, tempProperty;
205         
206         if (scpList != null && !scpList.equals("")) {
207             selectedDbms = getValue("SelectedDBMS");
208             scpListProps = null;
209             try {
210                 manager = DbManager.singleton();
211                 scpListProps = Props.singleton(scpList);
212                 localConfigDir = Props.getLocalConfigDir();
213                 if (!localConfigDir.equals(""))
214                     localConfigDir += "/";
215 /*                scriptFileName = localConfigDir +
216                     scpListProps.getProperty(selectedDbms+".scriptFileName");*/
217                 scriptFileName = scpListProps.getProperty(selectedDbms+".scriptFileName");
218                 if (scriptFileName == null || scriptFileName.equals("")) {
219                     System.out.println(javatoolsBundle.getString("Invalid_properties_file,_script_filename_is_not_present"));
220                     System.exit(1);
221                 }
222                 driverFileName = scpListProps.getProperty(selectedDbms+".driverFileName");
223                 if (driverFileName == null || driverFileName.equals("")) {
224                     System.out.println(javatoolsBundle.getString("Invalid_properties_file,_driver_file_name_is_not_present"));
225                     System.exit(1);
226                 }
227                 dbmsDriver
228                 = findOneDriver(driverFileName);
229                 if (dbmsDriver == null || dbmsDriver.equals("")) {
230                     System.out.println(javatoolsBundle.getString("Did_not_find_any_driver_to_perform_operations"));
231                     System.exit(1);
232                 }
233                 dbPrefix = selectedDbms+"."+dbmsDriver+".";
234                 tempProperty = manager.getProps().getProperty(dbPrefix + "pathNeeded");
235                 if (tempProperty.equals("true")) {
236                     pathNeeded = true;
237                 }
238                 else
239                     pathNeeded = false;
240             }
241             catch (IOException e) {
242                 System.out.println("Properties file for getting script filenames does not exist");
243                 System.exit(1);
244             }
245             dbmsAddress = getValue("DBMSAddress");
246             dbDir = getValue("DatabaseDir");
247             dbName = getValue("DatabaseName");
248             userName = getValue("UserName");
249             password = getValue("Password");
250             port = -1;
251             try {
252                 port = Integer.decode(getValue("DBMSPort")).intValue();
253             }
254             catch (NumberFormatException e) {
255             }
256         }
257     }
258     
259     private String findOneDriver(String fileName) {
260         ScriptCatcher driverScript;
261         String tempDriver, localConfigDir;
262         int i, numDrivers;
263         boolean found;
264         
265         driverScript = new ScriptCatcher();
266         localConfigDir = Props.getLocalConfigDir();
267         if (!localConfigDir.equals(""))
268             localConfigDir += "/";
269         try {
270             driverScript.loadScript(getClass().getClassLoader().getResourceAsStream("res/" + fileName));
271         }
272         catch (IOException e) {
273             return null;
274         }
275         numDrivers = driverScript.getNumCommands();
276         i=0;
277         found = false;
278         tempDriver = "";
279         while (!found && i < numDrivers) {
280             found = true;
281             tempDriver = driverScript.getCommandText(i);
282             try {
283                 Class.forName(tempDriver);
284             }
285             catch (ClassNotFoundException e) {
286                 found = false;
287             }
288             i++;
289         }
290         if (found)
291             return tempDriver;
292         else
293             return null;
294     }
295     
296     private String selectedDbms, dbmsAddress, dbDir, dbName, userName, password, pfn,
297         scpList, scriptFileName, dbmsDriver;
298     private String successMessage, failureMessage;
299     private boolean pathNeeded;
300     private int port;
301     private java.util.ResourceBundle javatoolsBundle;
302 }