⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientbasedatasource.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        //        // This ClientBaseDataSource class provides several standard JDBC properties.        // The names and values of the data source properties are also stored        // in the reference using the StringRefAddr class.        // This is all the information needed to reconstruct a ClientBaseDataSource object.        Reference ref = new Reference(this.getClass().getName(), ClientDataSourceFactory.class.getName(), null);        Class clz = getClass();        Field[] fields = clz.getFields();        for (int i = 0; i < fields.length; i++) {            String name = fields[i].getName();            if (name.startsWith("propertyKey_")) {                if (Modifier.isTransient(fields[i].getModifiers())) {                    continue; // if it is transient, then skip this propertyKey.                }                try {                    String propertyKey = fields[i].get(this).toString();                    // search for property field.                    Field propertyField;                    clz = getClass(); // start from current class.                    while (true) {                        try {                            propertyField = clz.getDeclaredField(name.substring(12));                            break; // found the property field, so break the while loop.                        } catch (NoSuchFieldException nsfe) {                            // property field is not found at current level of class, so continue to super class.                            clz = clz.getSuperclass();                            if (clz == Object.class) {                                throw new NamingException("bug check: corresponding property field does not exist");                            }                            continue;                        }                    }                    if (!Modifier.isTransient(propertyField.getModifiers())) {                        // if the property is not transient:                        // get the property.                        AccessController.doPrivileged(new SetAccessibleAction(propertyField, true));                        //propertyField.setAccessible (true);                        Object propertyObj = propertyField.get(this);                        String property = (propertyObj == null) ? null : String.valueOf(propertyObj);                        // add into reference.                        ref.add(new StringRefAddr(propertyKey, property));                    }                } catch (IllegalAccessException e) {                    throw new NamingException("bug check: property cannot be accessed");                } catch (PrivilegedActionException e) {                    throw new NamingException("Privileged action exception occurred.");                }            }        }        return ref;    }    /**     * Not an external.  Do not document in pubs. Populates member data for this data source given a JNDI reference.     */    public void hydrateFromReference(Reference ref) throws SQLException {        RefAddr address;        Class clz = getClass();        Field[] fields = clz.getFields();        for (int i = 0; i < fields.length; i++) {            String name = fields[i].getName();            if (name.startsWith("propertyKey_")) {                if (Modifier.isTransient(fields[i].getModifiers())) {                    continue; // if it is transient, then skip this propertyKey.                }                try {                    String propertyKey = fields[i].get(this).toString();                    // search for property field.                    Field propertyField;                    clz = getClass(); // start from current class.                    while (true) {                        try {                            propertyField = clz.getDeclaredField(name.substring(12));                            break; // found the property field, so break the while loop.                        } catch (NoSuchFieldException nsfe) {                            // property field is not found at current level of class, so continue to super class.                            clz = clz.getSuperclass();                            if (clz == Object.class) {                                throw new SqlException(new LogWriter(logWriter, traceLevel), "bug check: corresponding property field does not exist");                            }                            continue;                        }                    }                    if (!Modifier.isTransient(propertyField.getModifiers())) {                        // if the property is not transient:                        // set the property.                        address = ref.get(propertyKey);                        if (address != null) {                            propertyField.setAccessible(true);                            String type = propertyField.getType().toString();                            if (type.equals("boolean")) {                                boolean value = ((String) address.getContent()).equalsIgnoreCase("true");                                propertyField.setBoolean(this, value);                            } else if (type.equals("byte")) {                                byte value = Byte.parseByte((String) address.getContent());                                propertyField.setByte(this, value);                            } else if (type.equals("short")) {                                short value = Short.parseShort((String) address.getContent());                                propertyField.setShort(this, value);                            } else if (type.equals("int")) {                                int value = Integer.parseInt((String) address.getContent());                                propertyField.setInt(this, value);                            } else if (type.equals("long")) {                                long value = Long.parseLong((String) address.getContent());                                propertyField.setLong(this, value);                            } else if (type.equals("float")) {                                float value = Float.parseFloat((String) address.getContent());                                propertyField.setFloat(this, value);                            } else if (type.equals("double")) {                                double value = Double.parseDouble((String) address.getContent());                                propertyField.setDouble(this, value);                            } else if (type.equals("char")) {                                char value = ((String) address.getContent()).charAt(0);                                propertyField.setChar(this, value);                            } else {                                propertyField.set(this, address.getContent());                            }                        }                    }                } catch (IllegalAccessException e) {                    throw new SqlException(new LogWriter(this.logWriter, this.traceLevel), "bug check: property cannot be accessed");                }            }        }    }    // ----------------------supplemental methods---------------------------------    /**     * Not an external.  Do not document in pubs. Returns all non-transient properties of a ClientBaseDataSource.     */    public Properties getProperties() throws SQLException {        Properties properties = new Properties();        Class clz = getClass();        Field[] fields = clz.getFields();        for (int i = 0; i < fields.length; i++) {            String name = fields[i].getName();            if (name.startsWith("propertyKey_")) {                if (Modifier.isTransient(fields[i].getModifiers())) {                    continue; // if it is transient, then skip this propertyKey.                }                try {                    String propertyKey = fields[i].get(this).toString();                    // search for property field.                    Field propertyField;                    clz = getClass(); // start from current class.                    while (true) {                        try {                            propertyField = clz.getDeclaredField(name.substring(12));                            break; // found the property field, so break the while loop.                        } catch (NoSuchFieldException nsfe) {                            // property field is not found at current level of class, so continue to super class.                            clz = clz.getSuperclass();                            if (clz == Object.class) {                                throw new SqlException(new LogWriter(logWriter, traceLevel), "bug check: corresponding property field does not exist");                            }                            continue;                        }                    }                    if (!Modifier.isTransient(propertyField.getModifiers())) {                        // if the property is not transient:                        // get the property.                        propertyField.setAccessible(true);                        Object propertyObj = propertyField.get(this);                        String property = String.valueOf(propertyObj); // don't use toString becuase it may be null.                        if ("password".equals(propertyKey)) {                            StringBuffer sb = new StringBuffer(property);                            for (int j = 0; j < property.length(); j++) {                                sb.setCharAt(j, '*');                            }                            property = sb.toString();                        }                        // add into prperties.                        properties.setProperty(propertyKey, property);                    }                } catch (IllegalAccessException e) {                    throw new SqlException(new LogWriter(this.logWriter, this.traceLevel), "bug check: property cannot be accessed");                }            }        }        return properties;    }    //---------------------- helper methods --------------------------------------    // The java.io.PrintWriter overrides the traceFile setting.    // If neither traceFile nor jdbc logWriter are set, then null is returned.    // logWriterInUseSuffix used only for trace directories to indicate whether    // log writer is use is from xads, cpds, sds, ds, driver, config, reset.    public LogWriter computeDncLogWriterForNewConnection(String logWriterInUseSuffix) throws SqlException {        return computeDncLogWriterForNewConnection(logWriter, traceDirectory, traceFile, traceFileAppend, traceLevel, logWriterInUseSuffix, traceFileSuffixIndex_++);    }    // Called on for connection requests.    // The java.io.PrintWriter overrides the traceFile setting.    // If neither traceFile, nor logWriter, nor traceDirectory are set, then null is returned.    static public LogWriter computeDncLogWriterForNewConnection(PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, int traceLevel, String logWriterInUseSuffix, int traceFileSuffixIndex) throws SqlException {        int globaltraceFileSuffixIndex = Configuration.traceFileSuffixIndex__++;        // compute regular dnc log writer if there is any        LogWriter dncLogWriter = computeDncLogWriter(logWriter, traceDirectory, traceFile, traceFileAppend, logWriterInUseSuffix, traceFileSuffixIndex, traceLevel);        if (dncLogWriter != null) {            return dncLogWriter;        }        // compute global default dnc log writer if there is any        dncLogWriter = computeDncLogWriter(null, Configuration.traceDirectory__, Configuration.traceFile__, Configuration.traceFileAppend__, "_global", globaltraceFileSuffixIndex, Configuration.traceLevel__);        return dncLogWriter;    }    // Compute a DNC log writer before a connection is created.    static LogWriter computeDncLogWriter(PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, String logWriterInUseSuffix, int traceFileSuffixIndex, int traceLevel) throws SqlException {        // Otherwise, the trace file will still be created even TRACE_NONE.        if (traceLevel == TRACE_NONE) {            return null;        }        PrintWriter printWriter = computePrintWriter(logWriter, traceDirectory, traceFile, traceFileAppend, logWriterInUseSuffix, traceFileSuffixIndex);        if (printWriter == null) {            return null;        }        LogWriter dncLogWriter = new NetLogWriter(printWriter, traceLevel);        if (printWriter != logWriter && traceDirectory != null)        // When printWriter is an internal trace file and        // traceDirectory is not null, each connection has        // its own trace file and the trace file is not cached,        // so we can close it when DNC log writer is closed.        {            dncLogWriter.printWriterNeedsToBeClosed_ = true;        }        return dncLogWriter;    }    // Compute a DNC log writer after a connection is created.    // Declared public for use by am.Connection.  Not a public external.    public static LogWriter computeDncLogWriter(Connection connection, PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, String logWriterInUseSuffix, int traceFileSuffixIndex, int traceLevel) throws SqlException {        // Otherwise, the trace file will still be created even TRACE_NONE.        if (traceLevel == TRACE_NONE) {            return null;        }        PrintWriter printWriter = computePrintWriter(logWriter, traceDirectory, traceFile, traceFileAppend, logWriterInUseSuffix, traceFileSuffixIndex);        if (printWriter == null) {            return null;        }        LogWriter dncLogWriter = connection.agent_.newLogWriter_(printWriter, traceLevel);        if (printWriter != logWriter && traceDirectory != null)        // When printWriter is an internal trace file and        // traceDirectory is not null, each connection has        // its own trace file and the trace file is not cached,        // so we can close it when DNC log writer is closed.        {            dncLogWriter.printWriterNeedsToBeClosed_ = true;        }        return dncLogWriter;    }    // This method handles all the override semantics.    // The logWriter overrides the traceFile, and traceDirectory settings.    // If neither traceFile, nor logWriter, nor traceDirectory are set, then null is returned.    static PrintWriter computePrintWriter(PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, String logWriterInUseSuffix, int traceFileSuffixIndex) throws SqlException {        if (logWriter != null)  // java.io.PrintWriter is specified        {            return logWriter;        } else { // check trace file setting.            if (traceDirectory != null) {                String fileName;                if (traceFile == null) {                    fileName = traceDirectory + "/" + logWriterInUseSuffix + "_" + traceFileSuffixIndex;                } else {                    fileName = traceDirectory + "/" + traceFile + logWriterInUseSuffix + "_" + traceFileSuffixIndex;                }                return LogWriter.getPrintWriter(fileName, true); // no file append and not enable caching.            } else if (traceFile != null) {                return LogWriter.getPrintWriter(traceFile, traceFileAppend);            }        }        return null;    }    private static boolean parseBoolean(String boolString, boolean defaultBool) {        if (boolString != null) {            return (boolString.equalsIgnoreCase("true") || boolString.equalsIgnoreCase("yes"));        }        return defaultBool;    }    private static String parseString(String string, String defaultString) {        if (string != null) {            return string;        }        return defaultString;    }    private static short parseShort(String shortString, short defaultShort) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -