📄 util.java
字号:
/* Derby - Class org.apache.derby.impl.tools.ij.util Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.tools.ij;import org.apache.derby.tools.JDBCDisplayUtil;import org.apache.derby.iapi.tools.i18n.*;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.Types;import java.util.Properties;import java.util.Vector;import java.util.Locale;/** Methods used to control setup for apps as well as display some internal ij structures. @see org.apache.derby.tools.JDBCDisplayUtil @author ames */public final class util implements java.security.PrivilegedAction { private static boolean HAVE_BIG_DECIMAL; { boolean haveBigDecimal; try { Class.forName("java.math.BigDecimal"); haveBigDecimal = true; } catch (Throwable t) { haveBigDecimal = false; } HAVE_BIG_DECIMAL = haveBigDecimal; } private static final Class[] DS_GET_CONN_TYPES = {"".getClass(), "".getClass()}; private util() {} //----------------------------------------------------------------- // Methods for starting up JBMS /** * Find the argument that follows the specified parameter. * * @param param the parameter (e.g. "-p") * @param args the argument list to consider. * * @return the argument that follows the parameter, or null if not found */ static public String getArg(String param, String[] args) { int pLocn; Properties p; if (args == null) return null; for (pLocn=0; pLocn<args.length; pLocn++) { if (param.equals(args[pLocn])) break; } if (pLocn >= (args.length-1)) // not found or no file return null; return args[pLocn+1]; } /** ij is started with "-p[r] file OtherArgs"; the file contains properties to control the driver and database used to run ij, and can provide additional system properties. <p> getPropertyArg will look at the args and take out a "-p <file>" pair, reading the file into the system properties. <p> If there was a -p without a following <file>, no action is taken. @exception IOException thrown if file not found @param args the argument list to consider. @return true if a property item was found and loaded. */ static public boolean getPropertyArg(String[] args) throws IOException { String n; InputStream in1; Properties p; if ((n = getArg("-p", args))!= null){ in1 = new FileInputStream(n); in1 = new BufferedInputStream(in1); } else if ((n = getArg("-pr", args)) != null) { in1 = getResourceAsStream(n); if (in1 == null) throw ijException.resourceNotFound(); } else return false; p = System.getProperties(); // Trim off excess whitespace in property file, if any, and // then load those properties into 'p'. util.loadWithTrimmedValues(in1, p); return true; } /** ij is started with "-ca[r] file OtherArgs"; the file contains connection attibute properties to pass to getConnection <p> getConnAttributeArg will look at the args and take out a "-ca[r] <file>" pair and returning the Properties <p> @exception IOException thrown if file not found @param args the argument list to consider. @return properties in the file */ static public Properties getConnAttributeArg(String[] args) throws IOException { String n; InputStream in1; Properties p = new Properties(); if ((n = getArg("-ca", args))!= null){ in1 = new FileInputStream(n); in1 = new BufferedInputStream(in1); } else if ((n = getArg("-car", args)) != null) { in1 = getResourceAsStream(n); if (in1 == null) throw ijException.resourceNotFound(); } else return null; // Trim off excess whitespace in property file, if any, and // then load those properties into 'p'. util.loadWithTrimmedValues(in1, p); return p; } /** Convenience routine to qualify a resource name with "ij.defaultPackageName" if it is not qualified (does not begin with a "/"). @param absolute true means return null if the name is not absolute and false means return partial names. */ static String qualifyResourceName(String resourceName, boolean absolute) { resourceName=resourceName.trim(); if (resourceName.startsWith("/")) { return resourceName; } else { String pName = util.getSystemProperty("ij.defaultResourcePackage").trim(); if (pName == null) return null; if ((pName).endsWith("/")) resourceName = pName+resourceName; else resourceName = pName+"/"+resourceName; if (absolute && !resourceName.startsWith("/")) return null; else return resourceName; } } /** Convenience routine to get a resource as a BufferedInputStream. If the resourceName is not absolute (does not begin with a "/") this qualifies the name with the "ij.defaultResourcePackage" name. @param resourceName the name of the resource @return a buffered stream for the resource if it exists and null otherwise. */ static public InputStream getResourceAsStream(String resourceName) { Class c= util.class; resourceName = qualifyResourceName(resourceName,true); if (resourceName == null) return null; InputStream is = c.getResourceAsStream(resourceName); if (is != null) is = new BufferedInputStream(is, utilMain.BUFFEREDFILESIZE); return is; } /** Return the name of the ij command file or null if none is specified. The command file may be proceeded with -f flag on the command line. Alternatively, the command file may be specified without a -f flag. In this case we assume the first unknown argument is the command file. <P> This should only be called after calling invalidArgs. <p> If there is no such argument, a null is returned. @param args the argument list to consider. @return the name of the first argument not preceded by "-p", null if none found. @exception IOException thrown if file not found */ static public String getFileArg(String[] args) throws IOException { String fileName; int fLocn; boolean foundP = false; if (args == null) return null; if ((fileName=getArg("-f",args))!=null) return fileName; // //The first unknown arg is the file for (int ix=0; ix < args.length; ix++) if(args[ix].equals("-f") || args[ix].equals("-fr") || args[ix].equals("-ca") || args[ix].equals("-car") || args[ix].equals("-p") || args[ix].equals("-pr")) ix++; //skip the parameter to these args else return args[ix]; return null; } /** Return the name of a resource containing input commands or null iff none has been specified. */ static public String getInputResourceNameArg(String[] args) { return getArg("-fr", args); } /** Verify the ij line arguments command arguments. @return true if the args are invalid <UL> <LI>Only legal argument provided. <LI>Only specify a quantity once. </UL> */ static public boolean invalidArgs(String[] args, boolean gotProp, String file, String inputResourceName) { int countSupported = 0; boolean haveInput = false; for (int ix=0; ix < args.length; ix++) { // //If the arguemnt is a supported flag skip the flags argument if(!haveInput && (args[ix].equals("-f") || args[ix].equals("-fr"))) { haveInput = true; ix++; if (ix >= args.length) return true; } else if ((args[ix].equals("-p") || args[ix].equals("-pr") || args[ix].equals("-ca") || args[ix].equals("-car") )) { // next arg is the file/resource name ix++; if (ix >= args.length) return true; } // //Assume the first unknown arg is a file name. else if (!haveInput) { haveInput = true; } else { return true; } } return false; } /** * print a usage message for invocations of main(). */ static void Usage(LocalizedOutput out) { out.println( LocalizedResource.getMessage("IJ_UsageJavaComCloudToolsIjPPropeInput")); out.flush(); } private static final Class[] STRING_P = { "".getClass() }; private static final Class[] INT_P = { Integer.TYPE }; static public void setupDataSource(Object ds) throws Exception { // Loop over set methods on Datasource object, if there is a property // then call the method with corresponding value. java.lang.reflect.Method[] methods = ds.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { java.lang.reflect.Method m = methods[i]; String name = m.getName(); if (name.startsWith("set") && (name.length() > "set".length())) { String property = name.substring("set".length()); // setXyyyZwww property = "ij.dataSource."+property.substring(0,1).toLowerCase(java.util.Locale.ENGLISH)+ property.substring(1); // xyyyZwww String value = util.getSystemProperty(property); //System.out.println("setupDateSource: method="+name+" property="+property+" value="+((value==null)?"null":value)); if (value != null) { try { // call string method m.invoke(ds, new Object[] {value}); } catch (Throwable ignore) { // failed, assume it's an integer parameter m.invoke(ds, new Object[] {Integer.valueOf(value)}); } } } } } /** This will look for the System properties "ij.driver" and "ij.database" and return a java.sql.Connection if it successfully connects. The deprecated driver and database properties are examined first. <p> If no connection was possible, it will return a null. <p> Failure to load the driver class is quietly ignored. @param defaultDriver the driver to use if no property value found @param defaultURL the database URL to use if no property value found @param connInfo Connection attributes to pass to getConnection @return a connection to the defaultURL if possible; null if not. @exception SQLException on failure to connect. @exception ClassNotFoundException on failure to load driver. @exception InstantiationException on failure to load driver. @exception IllegalAccessException on failure to load driver. */ static public Connection startJBMS(String defaultDriver, String defaultURL, Properties connInfo) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { Connection con = null; String driverName; String databaseURL; // deprecate the non-ij prefix. actually, we should defer to jdbc.drivers... driverName = util.getSystemProperty("driver"); if (driverName == null) driverName = util.getSystemProperty("ij.driver"); if (driverName == null || driverName.length()==0) driverName = defaultDriver; if (driverName != null) { util.loadDriver(driverName); } String jdbcProtocol = util.getSystemProperty("ij.protocol"); if (jdbcProtocol != null) util.loadDriverIfKnown(jdbcProtocol); String user = util.getSystemProperty("ij.user"); String password = util.getSystemProperty("ij.password"); // deprecate the non-ij prefix name databaseURL = util.getSystemProperty("database"); if (databaseURL == null) databaseURL = util.getSystemProperty("ij.database"); if (databaseURL == null || databaseURL.length()==0) databaseURL = defaultURL; if (databaseURL != null) { // add protocol if might help find driver. // if have full URL, load driver for it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -