KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > ij > Main


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.ij.Main
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.ij;
23
24 import org.apache.derby.tools.JDBCDisplayUtil;
25 import org.apache.derby.iapi.tools.i18n.LocalizedResource;
26 import org.apache.derby.iapi.tools.i18n.LocalizedInput;
27 import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
28
29 import java.io.FileInputStream JavaDoc;
30 import java.io.BufferedInputStream JavaDoc;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.Reader JavaDoc;
36 import java.io.PrintStream JavaDoc;
37 import java.io.UnsupportedEncodingException JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 import java.sql.Connection JavaDoc;
41 import java.sql.SQLException JavaDoc;
42
43 import java.util.*;
44
45 /**
46  * This is the controller for ij. It uses two parsers:
47  * one to grab the next statement, and another to
48  * see if it is an ij command, and if so execute it.
49  * If it is not an ij command, it is treated as a JSQL
50  * statement and executed against the current connection.
51  * ijParser controls the current connection, and so contains
52  * all of the state information for executing JSQL statements.
53  * <p>
54  * This was written to facilitate a test harness for language
55  * functionality tests.
56  *
57  * @author ames
58  *
59  */

60 public class Main {
61     private utilMain utilInstance;
62
63     /**
64      * ij can be used directly on a shell command line through
65      * its main program.
66      * @param args allows 1 file name to be specified, from which
67      * input will be read; if not specified, stdin is used.
68      */

69     public static void main(String JavaDoc[] args)
70         throws IOException JavaDoc
71     {
72         mainCore(args, new Main(true));
73     }
74
75     public static void mainCore(String JavaDoc[] args, Main main)
76         throws IOException JavaDoc
77     {
78         LocalizedInput in = null;
79         InputStream JavaDoc in1 = null;
80         Main me;
81         String JavaDoc file;
82         String JavaDoc inputResourceName;
83         boolean gotProp;
84         Properties connAttributeDefaults = null;
85
86         LocalizedResource langUtil = LocalizedResource.getInstance();
87         LocalizedOutput out = langUtil.getNewOutput(System.out);
88
89                 // Validate arguments, check for --help.
90
if (util.invalidArgs(args)) {
91             util.Usage(out);
92             return;
93         }
94
95         // load the property file if specified
96
gotProp = util.getPropertyArg(args);
97
98         // get the default connection attributes
99
connAttributeDefaults = util.getConnAttributeArg(args);
100
101         // readjust output to derby.ui.locale and derby.ui.codeset if
102
// they were loaded from a property file.
103
langUtil.init();
104         out = langUtil.getNewOutput(System.out);
105                 main.initAppUI();
106
107         file = util.getFileArg(args);
108         inputResourceName = util.getInputResourceNameArg(args);
109         if (inputResourceName != null) {
110             in = langUtil.getNewInput(util.getResourceAsStream(inputResourceName));
111             if (in == null) {
112                 out.println(langUtil.getTextMessage("IJ_IjErroResoNo",inputResourceName));
113                 return;
114             }
115         } else if (file == null) {
116             in = langUtil.getNewInput(System.in);
117                         out.flush();
118                 } else {
119                     try {
120                         in1 = new FileInputStream JavaDoc(file);
121                         if (in1 != null) {
122                             in1 = new BufferedInputStream JavaDoc(in1, utilMain.BUFFEREDFILESIZE);
123                             in = langUtil.getNewInput(in1);
124                         }
125                     } catch (FileNotFoundException JavaDoc e) {
126                         if (Boolean.getBoolean("ij.searchClassPath")) {
127                             in = langUtil.getNewInput(util.getResourceAsStream(file));
128                         }
129                         if (in == null) {
130                         out.println(langUtil.getTextMessage("IJ_IjErroFileNo",file));
131                       return;
132                         }
133                     }
134                 }
135
136         String JavaDoc outFile = util.getSystemProperty("ij.outfile");
137         if (outFile != null && outFile.length()>0) {
138             LocalizedOutput oldOut = out;
139             try {
140                 out = langUtil.getNewOutput(new FileOutputStream JavaDoc(outFile));
141             }
142             catch (IOException JavaDoc ioe) {
143                 oldOut.println(langUtil.getTextMessage("IJ_IjErroUnabTo",outFile));
144             }
145         }
146
147         // the old property name is deprecated...
148
String JavaDoc maxDisplayWidth = util.getSystemProperty("maximumDisplayWidth");
149         if (maxDisplayWidth==null)
150             maxDisplayWidth = util.getSystemProperty("ij.maximumDisplayWidth");
151         if (maxDisplayWidth != null && maxDisplayWidth.length() > 0) {
152             try {
153                 int maxWidth = Integer.parseInt(maxDisplayWidth);
154                 JDBCDisplayUtil.setMaxDisplayWidth(maxWidth);
155             }
156             catch (NumberFormatException JavaDoc nfe) {
157                 out.println(langUtil.getTextMessage("IJ_IjErroMaxiVa", maxDisplayWidth));
158             }
159         }
160
161         /* Use the main parameter to get to
162          * a new Main that we can use.
163          * (We can't do the work in Main(out)
164          * until after we do all of the work above
165          * us in this method.
166          */

167         me = main.getMain(out);
168
169         /* Let the processing begin! */
170         me.go(in, out, connAttributeDefaults);
171         in.close(); out.close();
172     }
173
174     /**
175      * Get the right Main (according to
176      * the JDBC version.
177      *
178      * @return The right main (according to the JDBC version).
179      */

180     public Main getMain(LocalizedOutput out)
181     {
182         return new Main(out);
183     }
184
185     /**
186      * Get the right utilMain (according to
187      * the JDBC version.
188      *
189      * @return The right utilMain (according to the JDBC version).
190      */

191     public utilMain getutilMain(int numConnections, LocalizedOutput out)
192     {
193         return new utilMain(numConnections, out);
194     }
195
196     /**
197         Give a shortcut to go on the utilInstance so
198         we don't expose utilMain.
199      */

200     private void go(LocalizedInput in, LocalizedOutput out ,
201                    Properties connAttributeDefaults)
202     {
203         LocalizedInput[] inA = { in } ;
204         utilInstance.go(inA, out,connAttributeDefaults);
205     }
206
207     /**
208      * create an ij tool waiting to be given input and output streams.
209      */

210     public Main() {
211         this(null);
212     }
213
214     public Main(LocalizedOutput out) {
215         if (out == null) {
216             out = LocalizedResource.getInstance().getNewOutput(System.out);
217         }
218         utilInstance = getutilMain(1, out);
219         utilInstance.initFromEnvironment();
220     }
221
222     /**
223      * This constructor is only used so that we
224      * can get to the right Main based on the
225      * JDBC version. We don't do any work in
226      * this constructor and we only use this
227      * object to get to the right Main via
228      * getMain().
229      */

230     public Main(boolean trash)
231     {
232     }
233   private void initAppUI(){
234     //To fix a problem in the AppUI implementation, a reference to the AppUI class is
235
//maintained by this tool. Without this reference, it is possible for the
236
//AppUI class to be garbage collected and the initialization values lost.
237
//langUtilClass = LocalizedResource.class;
238

239         // adjust the application in accordance with derby.ui.locale and derby.ui.codeset
240
LocalizedResource.getInstance();
241   }
242 }
243
Popular Tags