001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.ij.Main
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.tools.ij;
023:
024: import org.apache.derby.tools.JDBCDisplayUtil;
025: import org.apache.derby.iapi.tools.i18n.LocalizedResource;
026: import org.apache.derby.iapi.tools.i18n.LocalizedInput;
027: import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
028:
029: import java.io.FileInputStream;
030: import java.io.BufferedInputStream;
031: import java.io.BufferedReader;
032: import java.io.FileOutputStream;
033: import java.io.FileNotFoundException;
034: import java.io.InputStream;
035: import java.io.Reader;
036: import java.io.PrintStream;
037: import java.io.UnsupportedEncodingException;
038: import java.io.IOException;
039:
040: import java.sql.Connection;
041: import java.sql.SQLException;
042:
043: import java.util.*;
044:
045: /**
046: * This is the controller for ij. It uses two parsers:
047: * one to grab the next statement, and another to
048: * see if it is an ij command, and if so execute it.
049: * If it is not an ij command, it is treated as a JSQL
050: * statement and executed against the current connection.
051: * ijParser controls the current connection, and so contains
052: * all of the state information for executing JSQL statements.
053: * <p>
054: * This was written to facilitate a test harness for language
055: * functionality tests.
056: *
057: * @author ames
058: *
059: */
060: public class Main {
061: private utilMain utilInstance;
062:
063: /**
064: * ij can be used directly on a shell command line through
065: * its main program.
066: * @param args allows 1 file name to be specified, from which
067: * input will be read; if not specified, stdin is used.
068: */
069: public static void main(String[] args) throws IOException {
070: mainCore(args, new Main(true));
071: }
072:
073: public static void mainCore(String[] args, Main main)
074: throws IOException {
075: LocalizedInput in = null;
076: InputStream in1 = null;
077: Main me;
078: String file;
079: String inputResourceName;
080: boolean gotProp;
081: Properties connAttributeDefaults = null;
082:
083: LocalizedResource langUtil = LocalizedResource.getInstance();
084: LocalizedOutput out = langUtil.getNewOutput(System.out);
085:
086: // Validate arguments, check for --help.
087: if (util.invalidArgs(args)) {
088: util.Usage(out);
089: return;
090: }
091:
092: // load the property file if specified
093: gotProp = util.getPropertyArg(args);
094:
095: // get the default connection attributes
096: connAttributeDefaults = util.getConnAttributeArg(args);
097:
098: // readjust output to derby.ui.locale and derby.ui.codeset if
099: // they were loaded from a property file.
100: langUtil.init();
101: out = langUtil.getNewOutput(System.out);
102: main.initAppUI();
103:
104: file = util.getFileArg(args);
105: inputResourceName = util.getInputResourceNameArg(args);
106: if (inputResourceName != null) {
107: in = langUtil.getNewInput(util
108: .getResourceAsStream(inputResourceName));
109: if (in == null) {
110: out.println(langUtil.getTextMessage("IJ_IjErroResoNo",
111: inputResourceName));
112: return;
113: }
114: } else if (file == null) {
115: in = langUtil.getNewInput(System.in);
116: out.flush();
117: } else {
118: try {
119: in1 = new FileInputStream(file);
120: if (in1 != null) {
121: in1 = new BufferedInputStream(in1,
122: utilMain.BUFFEREDFILESIZE);
123: in = langUtil.getNewInput(in1);
124: }
125: } catch (FileNotFoundException e) {
126: if (Boolean.getBoolean("ij.searchClassPath")) {
127: in = langUtil.getNewInput(util
128: .getResourceAsStream(file));
129: }
130: if (in == null) {
131: out.println(langUtil.getTextMessage(
132: "IJ_IjErroFileNo", file));
133: return;
134: }
135: }
136: }
137:
138: String outFile = util.getSystemProperty("ij.outfile");
139: if (outFile != null && outFile.length() > 0) {
140: LocalizedOutput oldOut = out;
141: try {
142: out = langUtil.getNewOutput(new FileOutputStream(
143: outFile));
144: } catch (IOException ioe) {
145: oldOut.println(langUtil.getTextMessage(
146: "IJ_IjErroUnabTo", outFile));
147: }
148: }
149:
150: // the old property name is deprecated...
151: String maxDisplayWidth = util
152: .getSystemProperty("maximumDisplayWidth");
153: if (maxDisplayWidth == null)
154: maxDisplayWidth = util
155: .getSystemProperty("ij.maximumDisplayWidth");
156: if (maxDisplayWidth != null && maxDisplayWidth.length() > 0) {
157: try {
158: int maxWidth = Integer.parseInt(maxDisplayWidth);
159: JDBCDisplayUtil.setMaxDisplayWidth(maxWidth);
160: } catch (NumberFormatException nfe) {
161: out.println(langUtil.getTextMessage("IJ_IjErroMaxiVa",
162: maxDisplayWidth));
163: }
164: }
165:
166: /* Use the main parameter to get to
167: * a new Main that we can use.
168: * (We can't do the work in Main(out)
169: * until after we do all of the work above
170: * us in this method.
171: */
172: me = main.getMain(out);
173:
174: /* Let the processing begin! */
175: me.go(in, out, connAttributeDefaults);
176: in.close();
177: out.close();
178: }
179:
180: /**
181: * Get the right Main (according to
182: * the JDBC version.
183: *
184: * @return The right main (according to the JDBC version).
185: */
186: public Main getMain(LocalizedOutput out) {
187: return new Main(out);
188: }
189:
190: /**
191: * Get the right utilMain (according to
192: * the JDBC version.
193: *
194: * @return The right utilMain (according to the JDBC version).
195: */
196: public utilMain getutilMain(int numConnections, LocalizedOutput out) {
197: return new utilMain(numConnections, out);
198: }
199:
200: /**
201: Give a shortcut to go on the utilInstance so
202: we don't expose utilMain.
203: */
204: private void go(LocalizedInput in, LocalizedOutput out,
205: Properties connAttributeDefaults) {
206: LocalizedInput[] inA = { in };
207: utilInstance.go(inA, out, connAttributeDefaults);
208: }
209:
210: /**
211: * create an ij tool waiting to be given input and output streams.
212: */
213: public Main() {
214: this (null);
215: }
216:
217: public Main(LocalizedOutput out) {
218: if (out == null) {
219: out = LocalizedResource.getInstance().getNewOutput(
220: System.out);
221: }
222: utilInstance = getutilMain(1, out);
223: utilInstance.initFromEnvironment();
224: }
225:
226: /**
227: * This constructor is only used so that we
228: * can get to the right Main based on the
229: * JDBC version. We don't do any work in
230: * this constructor and we only use this
231: * object to get to the right Main via
232: * getMain().
233: */
234: public Main(boolean trash) {
235: }
236:
237: private void initAppUI() {
238: //To fix a problem in the AppUI implementation, a reference to the AppUI class is
239: //maintained by this tool. Without this reference, it is possible for the
240: //AppUI class to be garbage collected and the initialization values lost.
241: //langUtilClass = LocalizedResource.class;
242:
243: // adjust the application in accordance with derby.ui.locale and derby.ui.codeset
244: LocalizedResource.getInstance();
245: }
246: }
|