1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.tools.ij.Main |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.tools.ij; |
22 | |
23 | import org.apache.derby.tools.JDBCDisplayUtil; |
24 | import org.apache.derby.iapi.tools.i18n.LocalizedResource; |
25 | import org.apache.derby.iapi.tools.i18n.LocalizedInput; |
26 | import org.apache.derby.iapi.tools.i18n.LocalizedOutput; |
27 | |
28 | import java.io.FileInputStream; |
29 | import java.io.BufferedInputStream; |
30 | import java.io.BufferedReader; |
31 | import java.io.FileOutputStream; |
32 | import java.io.FileNotFoundException; |
33 | import java.io.InputStream; |
34 | import java.io.Reader; |
35 | import java.io.PrintStream; |
36 | import java.io.UnsupportedEncodingException; |
37 | import java.io.IOException; |
38 | |
39 | import java.sql.Connection; |
40 | import java.sql.SQLException; |
41 | |
42 | import java.util.*; |
43 | |
44 | /** |
45 | * This is the controller for ij. It uses two parsers: |
46 | * one to grab the next statement, and another to |
47 | * see if it is an ij command, and if so execute it. |
48 | * If it is not an ij command, it is treated as a JSQL |
49 | * statement and executed against the current connection. |
50 | * ijParser controls the current connection, and so contains |
51 | * all of the state information for executing JSQL statements. |
52 | * <p> |
53 | * This was written to facilitate a test harness for language |
54 | * functionality tests. |
55 | * |
56 | * @author ames |
57 | * |
58 | */ |
59 | public class Main { |
60 | public LocalizedOutput out; |
61 | public utilMain utilInstance; |
62 | public Class langUtilClass; |
63 | |
64 | |
65 | |
66 | /** |
67 | * ij can be used directly on a shell command line through |
68 | * its main program. |
69 | * @param args allows 1 file name to be specified, from which |
70 | * input will be read; if not specified, stdin is used. |
71 | */ |
72 | public static void main(String[] args) |
73 | throws IOException |
74 | { |
75 | mainCore(args, new Main(true)); |
76 | } |
77 | |
78 | public static void mainCore(String[] args, Main main) |
79 | throws IOException |
80 | { |
81 | LocalizedInput in = null; |
82 | InputStream in1 = null; |
83 | Main me; |
84 | String file; |
85 | String inputResourceName; |
86 | boolean gotProp; |
87 | Properties connAttributeDefaults = null; |
88 | |
89 | LocalizedResource langUtil = LocalizedResource.getInstance(); |
90 | LocalizedOutput out = langUtil.getNewOutput(System.out); |
91 | |
92 | // Validate arguments, check for --help. |
93 | if (util.invalidArgs(args)) { |
94 | util.Usage(out); |
95 | return; |
96 | } |
97 | |
98 | // load the property file if specified |
99 | gotProp = util.getPropertyArg(args); |
100 | |
101 | // get the default connection attributes |
102 | connAttributeDefaults = util.getConnAttributeArg(args); |
103 | |
104 | // readjust output to derby.ui.locale and derby.ui.codeset if |
105 | // they were loaded from a property file. |
106 | langUtil.init(); |
107 | out = langUtil.getNewOutput(System.out); |
108 | main.initAppUI(); |
109 | |
110 | file = util.getFileArg(args); |
111 | inputResourceName = util.getInputResourceNameArg(args); |
112 | if (inputResourceName != null) { |
113 | in = langUtil.getNewInput(util.getResourceAsStream(inputResourceName)); |
114 | if (in == null) { |
115 | out.println(langUtil.getTextMessage("IJ_IjErroResoNo",inputResourceName)); |
116 | return; |
117 | } |
118 | } else if (file == null) { |
119 | in = langUtil.getNewInput(System.in); |
120 | out.flush(); |
121 | } else { |
122 | try { |
123 | in1 = new FileInputStream(file); |
124 | if (in1 != null) { |
125 | in1 = new BufferedInputStream(in1, utilMain.BUFFEREDFILESIZE); |
126 | in = langUtil.getNewInput(in1); |
127 | } |
128 | } catch (FileNotFoundException e) { |
129 | if (Boolean.getBoolean("ij.searchClassPath")) { |
130 | in = langUtil.getNewInput(util.getResourceAsStream(file)); |
131 | } |
132 | if (in == null) { |
133 | out.println(langUtil.getTextMessage("IJ_IjErroFileNo",file)); |
134 | return; |
135 | } |
136 | } |
137 | } |
138 | |
139 | String outFile = util.getSystemProperty("ij.outfile"); |
140 | if (outFile != null && outFile.length()>0) { |
141 | LocalizedOutput oldOut = out; |
142 | try { |
143 | out = langUtil.getNewOutput(new FileOutputStream(outFile)); |
144 | } |
145 | catch (IOException ioe) { |
146 | oldOut.println(langUtil.getTextMessage("IJ_IjErroUnabTo",outFile)); |
147 | } |
148 | } |
149 | |
150 | // the old property name is deprecated... |
151 | String maxDisplayWidth = util.getSystemProperty("maximumDisplayWidth"); |
152 | if (maxDisplayWidth==null) |
153 | maxDisplayWidth = util.getSystemProperty("ij.maximumDisplayWidth"); |
154 | if (maxDisplayWidth != null && maxDisplayWidth.length() > 0) { |
155 | try { |
156 | int maxWidth = Integer.parseInt(maxDisplayWidth); |
157 | JDBCDisplayUtil.setMaxDisplayWidth(maxWidth); |
158 | } |
159 | catch (NumberFormatException nfe) { |
160 | out.println(langUtil.getTextMessage("IJ_IjErroMaxiVa", maxDisplayWidth)); |
161 | } |
162 | } |
163 | |
164 | /* Use the main parameter to get to |
165 | * a new Main that we can use. |
166 | * (We can't do the work in Main(out) |
167 | * until after we do all of the work above |
168 | * us in this method. |
169 | */ |
170 | me = main.getMain(out); |
171 | |
172 | /* Let the processing begin! */ |
173 | me.go(in, out, connAttributeDefaults); |
174 | in.close(); out.close(); |
175 | } |
176 | |
177 | /** |
178 | * Get the right Main (according to |
179 | * the JDBC version. |
180 | * |
181 | * @return The right main (according to the JDBC version). |
182 | */ |
183 | public Main getMain(LocalizedOutput out) |
184 | { |
185 | return new Main(out); |
186 | } |
187 | |
188 | /** |
189 | * Get the right utilMain (according to |
190 | * the JDBC version. |
191 | * |
192 | * @return The right utilMain (according to the JDBC version). |
193 | */ |
194 | public utilMain getutilMain(int numConnections, LocalizedOutput out) |
195 | { |
196 | return new utilMain(numConnections, out); |
197 | } |
198 | |
199 | /** |
200 | Give a shortcut to go on the utilInstance so |
201 | we don't expose utilMain. |
202 | */ |
203 | public void go(LocalizedInput in, LocalizedOutput out , |
204 | Properties connAttributeDefaults) |
205 | { |
206 | LocalizedInput[] inA = { in } ; |
207 | utilInstance.go(inA, out,connAttributeDefaults); |
208 | } |
209 | |
210 | public void go(InputStream in, PrintStream out, |
211 | Properties connAttributeDefaults) |
212 | { |
213 | initAppUI(); |
214 | LocalizedResource langUtil = LocalizedResource.getInstance(); |
215 | go(langUtil.getNewInput(in), langUtil.getNewOutput(out), |
216 | connAttributeDefaults); |
217 | } |
218 | |
219 | /** |
220 | * create an ij tool waiting to be given input and output streams. |
221 | */ |
222 | public Main() { |
223 | this(null); |
224 | } |
225 | |
226 | public Main(LocalizedOutput out) { |
227 | if (out!=null) { |
228 | this.out = out; |
229 | } else { |
230 | this.out = LocalizedResource.getInstance().getNewOutput(System.out); |
231 | } |
232 | utilInstance = getutilMain(1, this.out); |
233 | } |
234 | |
235 | /** |
236 | * This constructor is only used so that we |
237 | * can get to the right Main based on the |
238 | * JDBC version. We don't do any work in |
239 | * this constructor and we only use this |
240 | * object to get to the right Main via |
241 | * getMain(). |
242 | */ |
243 | public Main(boolean trash) |
244 | { |
245 | } |
246 | public void initAppUI(){ |
247 | //To fix a problem in the AppUI implementation, a reference to the AppUI class is |
248 | //maintained by this tool. Without this reference, it is possible for the |
249 | //AppUI class to be garbage collected and the initialization values lost. |
250 | //langUtilClass = LocalizedResource.class; |
251 | |
252 | // adjust the application in accordance with derby.ui.locale and derby.ui.codeset |
253 | LocalizedResource.getInstance(); |
254 | } |
255 | } |