--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.tools.ij.Main\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.tools.ij;\r
+\r
+import org.apache.derby.tools.JDBCDisplayUtil;\r
+import org.apache.derby.iapi.tools.i18n.LocalizedResource;\r
+import org.apache.derby.iapi.tools.i18n.LocalizedInput;\r
+import org.apache.derby.iapi.tools.i18n.LocalizedOutput;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.BufferedInputStream;\r
+import java.io.BufferedReader;\r
+import java.io.FileOutputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.InputStream;\r
+import java.io.Reader;\r
+import java.io.PrintStream;\r
+import java.io.UnsupportedEncodingException;\r
+import java.io.IOException;\r
+\r
+import java.security.AccessController;\r
+import java.security.PrivilegedAction;\r
+import java.sql.Connection;\r
+import java.sql.SQLException;\r
+\r
+import java.util.*;\r
+\r
+/**\r
+ * This is the controller for ij. It uses two parsers:\r
+ * one to grab the next statement, and another to\r
+ * see if it is an ij command, and if so execute it.\r
+ * If it is not an ij command, it is treated as a JSQL\r
+ * statement and executed against the current connection.\r
+ * ijParser controls the current connection, and so contains\r
+ * all of the state information for executing JSQL statements.\r
+ * <p>\r
+ * This was written to facilitate a test harness for language\r
+ * functionality tests.\r
+ *\r
+ *\r
+ */\r
+public class Main {\r
+ private utilMain utilInstance;\r
+\r
+ /**\r
+ * ij can be used directly on a shell command line through\r
+ * its main program.\r
+ * @param args allows 1 file name to be specified, from which\r
+ * input will be read; if not specified, stdin is used.\r
+ */\r
+ public static void main(String[] args) \r
+ throws IOException \r
+ {\r
+ mainCore(args, new Main(true));\r
+ }\r
+\r
+ public static void mainCore(String[] args, Main main)\r
+ throws IOException \r
+ {\r
+ LocalizedInput in = null;\r
+ InputStream in1 = null;\r
+ Main me;\r
+ String file;\r
+ String inputResourceName;\r
+ boolean gotProp;\r
+ Properties connAttributeDefaults = null;\r
+\r
+ LocalizedResource langUtil = LocalizedResource.getInstance();\r
+ LocalizedOutput out = langUtil.getNewOutput(System.out);\r
+\r
+ // Validate arguments, check for --help.\r
+ if (util.invalidArgs(args)) {\r
+ util.Usage(out);\r
+ return;\r
+ }\r
+\r
+ // load the property file if specified\r
+ gotProp = util.getPropertyArg(args);\r
+\r
+ // get the default connection attributes\r
+ connAttributeDefaults = util.getConnAttributeArg(args);\r
+\r
+ // readjust output to derby.ui.locale and derby.ui.codeset if \r
+ // they were loaded from a property file.\r
+ langUtil.init();\r
+ out = langUtil.getNewOutput(System.out);\r
+ main.initAppUI();\r
+\r
+ file = util.getFileArg(args);\r
+ inputResourceName = util.getInputResourceNameArg(args);\r
+ if (inputResourceName != null) {\r
+ in = langUtil.getNewInput(util.getResourceAsStream(inputResourceName));\r
+ if (in == null) {\r
+ out.println(langUtil.getTextMessage("IJ_IjErroResoNo",inputResourceName));\r
+ return;\r
+ }\r
+ } else if (file == null) {\r
+ in = langUtil.getNewInput(System.in);\r
+ out.flush();\r
+ } else {\r
+ try {\r
+ in1 = new FileInputStream(file);\r
+ if (in1 != null) {\r
+ in1 = new BufferedInputStream(in1, utilMain.BUFFEREDFILESIZE);\r
+ in = langUtil.getNewInput(in1);\r
+ }\r
+ } catch (FileNotFoundException e) {\r
+ if (Boolean.getBoolean("ij.searchClassPath")) {\r
+ in = langUtil.getNewInput(util.getResourceAsStream(file));\r
+ }\r
+ if (in == null) {\r
+ out.println(langUtil.getTextMessage("IJ_IjErroFileNo",file));\r
+ return;\r
+ }\r
+ }\r
+ }\r
+\r
+ final String outFile = util.getSystemProperty("ij.outfile");\r
+ if (outFile != null && outFile.length()>0) {\r
+ LocalizedOutput oldOut = out;\r
+ FileOutputStream fos = (FileOutputStream) AccessController.doPrivileged(new PrivilegedAction() {\r
+ public Object run() {\r
+ FileOutputStream out = null;\r
+ try {\r
+ out = new FileOutputStream(outFile);\r
+ } catch (FileNotFoundException e) {\r
+ out = null;\r
+ }\r
+ return out;\r
+ }\r
+ });\r
+ out = langUtil.getNewOutput(fos);\r
+\r
+ if (out == null)\r
+ oldOut.println(langUtil.getTextMessage("IJ_IjErroUnabTo",outFile));\r
+ \r
+ }\r
+\r
+ // the old property name is deprecated...\r
+ String maxDisplayWidth = util.getSystemProperty("maximumDisplayWidth");\r
+ if (maxDisplayWidth==null) \r
+ maxDisplayWidth = util.getSystemProperty("ij.maximumDisplayWidth");\r
+ if (maxDisplayWidth != null && maxDisplayWidth.length() > 0) {\r
+ try {\r
+ int maxWidth = Integer.parseInt(maxDisplayWidth);\r
+ JDBCDisplayUtil.setMaxDisplayWidth(maxWidth);\r
+ }\r
+ catch (NumberFormatException nfe) {\r
+ out.println(langUtil.getTextMessage("IJ_IjErroMaxiVa", maxDisplayWidth));\r
+ }\r
+ }\r
+\r
+ /* Use the main parameter to get to\r
+ * a new Main that we can use. \r
+ * (We can't do the work in Main(out)\r
+ * until after we do all of the work above\r
+ * us in this method.\r
+ */\r
+ me = main.getMain(out);\r
+\r
+ /* Let the processing begin! */\r
+ me.go(in, out, connAttributeDefaults);\r
+ in.close(); out.close();\r
+ }\r
+\r
+ /**\r
+ * Get the right Main (according to \r
+ * the JDBC version.\r
+ *\r
+ * @return The right main (according to the JDBC version).\r
+ */\r
+ public Main getMain(LocalizedOutput out)\r
+ {\r
+ return new Main(out);\r
+ }\r
+\r
+ /**\r
+ * Get the right utilMain (according to \r
+ * the JDBC version.\r
+ *\r
+ * @return The right utilMain (according to the JDBC version).\r
+ */\r
+ public utilMain getutilMain(int numConnections, LocalizedOutput out)\r
+ {\r
+ return new utilMain(numConnections, out);\r
+ }\r
+\r
+ /**\r
+ Give a shortcut to go on the utilInstance so\r
+ we don't expose utilMain.\r
+ */\r
+ private void go(LocalizedInput in, LocalizedOutput out , \r
+ Properties connAttributeDefaults)\r
+ {\r
+ LocalizedInput[] inA = { in } ;\r
+ utilInstance.go(inA, out,connAttributeDefaults);\r
+ }\r
+\r
+ /**\r
+ * create an ij tool waiting to be given input and output streams.\r
+ */\r
+ public Main() {\r
+ this(null);\r
+ }\r
+\r
+ public Main(LocalizedOutput out) {\r
+ if (out == null) {\r
+ out = LocalizedResource.getInstance().getNewOutput(System.out);\r
+ }\r
+ utilInstance = getutilMain(1, out);\r
+ utilInstance.initFromEnvironment();\r
+ }\r
+\r
+ /**\r
+ * This constructor is only used so that we \r
+ * can get to the right Main based on the\r
+ * JDBC version. We don't do any work in\r
+ * this constructor and we only use this\r
+ * object to get to the right Main via\r
+ * getMain().\r
+ */\r
+ public Main(boolean trash)\r
+ {\r
+ }\r
+ private void initAppUI(){\r
+ //To fix a problem in the AppUI implementation, a reference to the AppUI class is\r
+ //maintained by this tool. Without this reference, it is possible for the\r
+ //AppUI class to be garbage collected and the initialization values lost.\r
+ //langUtilClass = LocalizedResource.class;\r
+\r
+ // adjust the application in accordance with derby.ui.locale and derby.ui.codeset\r
+ LocalizedResource.getInstance(); \r
+ }\r
+ \r
+}\r