bundledbpersistencemanager.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,354 行 · 第 1/4 页

JAVA
1,354
字号
     *     * @see AbstractPersistenceManager#store(NodeReferences)     */    public synchronized void store(NodeReferences refs) throws ItemStateException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        PreparedStatement stmt = null;        try {            // check if insert or update            if (exists(refs.getId())) {                stmt = nodeReferenceUpdate;            } else {                stmt = nodeReferenceInsert;            }            ByteArrayOutputStream out = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE);            // serialize references            Serializer.serialize(refs, out);            // we are synchronized on this instance, therefore we do not            // not have to additionally synchronize on the preparedStatement            stmt.setBytes(1, out.toByteArray());            setKey(stmt, refs.getTargetId().getUUID(), 2);            stmt.execute();            // there's no need to close a ByteArrayOutputStream            //out.close();        } catch (Exception e) {            String msg = "failed to write property state: " + refs.getTargetId();            log.error(msg, e);            throw new ItemStateException(msg, e);        } finally {            resetStatement(stmt);        }    }    /**     * {@inheritDoc}     */    public synchronized void destroy(NodeReferences refs) throws ItemStateException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        PreparedStatement stmt = nodeReferenceDelete;        try {            setKey(stmt, refs.getTargetId().getUUID(), 1);            stmt.execute();        } catch (Exception e) {            if (e instanceof NoSuchItemStateException) {                throw (NoSuchItemStateException) e;            }            String msg = "failed to delete references: " + refs.getTargetId();            log.error(msg, e);            throw new ItemStateException(msg, e);        } finally {            resetStatement(stmt);        }    }    /**     * {@inheritDoc}     */    public synchronized boolean exists(NodeReferencesId targetId) throws ItemStateException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        PreparedStatement stmt = nodeReferenceSelect;        ResultSet rs = null;        try {            setKey(stmt, targetId.getTargetId().getUUID(), 1);            stmt.execute();            rs = stmt.getResultSet();            // a reference exists, if the result has at least one entry            return rs.next();        } catch (Exception e) {            String msg = "failed to check existence of node references: " + targetId;            log.error(msg, e);            throw new ItemStateException(msg, e);        } finally {            closeResultSet(rs);            resetStatement(stmt);        }    }    /**     * Resets the given <code>PreparedStatement</code> by clearing the     * parameters and warnings contained.     *     * @param stmt The <code>PreparedStatement</code> to reset. If     *             <code>null</code> this method does nothing.     */    protected synchronized void resetStatement(PreparedStatement stmt) {        if (stmt != null) {            try {                stmt.clearParameters();                stmt.clearWarnings();            } catch (SQLException se) {                logException("Failed resetting PreparedStatement", se);            }        }    }    /**     * Closes the result set     * @param rs the result set     */    protected void closeResultSet(ResultSet rs) {        if (rs != null) {            try {                rs.close();            } catch (SQLException se) {                logException("Failed closing ResultSet", se);            }        }    }    /**     * closes the input stream     * @param ins the inputs stream     */    protected void closeStream(InputStream ins) {        if (ins != null) {            try {                ins.close();            } catch (IOException ignore) {                // ignore            }        }    }    /**     * closes the statement     * @param stmt the statemenet     */    protected void closeStatement(PreparedStatement stmt) {        if (stmt != null) {            try {                stmt.close();            } catch (SQLException se) {                logException("Failed closing PreparedStatement", se);            }        }    }    /**     * logs an sql exception     * @param message the message     * @param se the exception     */    protected void logException(String message, SQLException se) {        if (message != null) {            log.error(message);        }        log.error("       Reason: " + se.getMessage());        log.error("   State/Code: " + se.getSQLState() + "/" +                se.getErrorCode());        log.debug("   dump:", se);    }    /**     * @inheritDoc     */    public String toString() {        return name;    }    /**     * Helper interface for closeable stores     */    protected static interface CloseableBLOBStore extends BLOBStore {        void close();    }    /**     * own implementation of the filesystem blob store that uses a different     * blob-id scheme.     */    protected class FSBlobStore extends FileSystemBLOBStore implements CloseableBLOBStore {        private FileSystem fs;        public FSBlobStore(FileSystem fs) {            super(fs);            this.fs = fs;        }        public String createId(PropertyId id, int index) {            return buildBlobFilePath(null, id, index).toString();        }        public void close() {            try {                fs.close();                fs = null;            } catch (Exception e) {                // ignore            }        }    }    /**     * Implementation of a blob store that stores the data inside the database     */    protected class DbBlobStore implements CloseableBLOBStore {        protected PreparedStatement blobInsert;        protected PreparedStatement blobUpdate;        protected PreparedStatement blobSelect;        protected PreparedStatement blobSelectExist;        protected PreparedStatement blobDelete;        public DbBlobStore() throws SQLException {            blobInsert =                    con.prepareStatement("insert into "                    + schemaObjectPrefix + "BINVAL (BINVAL_DATA, BINVAL_ID) values (?, ?)");            blobUpdate =                    con.prepareStatement("update "                    + schemaObjectPrefix + "BINVAL set BINVAL_DATA = ? where BINVAL_ID = ?");            blobSelect =                    con.prepareStatement("select BINVAL_DATA from "                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?");            blobSelectExist =                    con.prepareStatement("select 1 from "                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?");            blobDelete =                    con.prepareStatement("delete from "                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?");        }        /**         * {@inheritDoc}         */        public String createId(PropertyId id, int index) {            StringBuffer buf = new StringBuffer();            buf.append(id.getParentId().toString());            buf.append('.');            buf.append(getNsIndex().stringToIndex(id.getName().getNamespaceURI()));            buf.append('.');            buf.append(getNameIndex().stringToIndex(id.getName().getLocalName()));            buf.append('.');            buf.append(index);            return buf.toString();        }        /**         * {@inheritDoc}         */        public InputStream get(String blobId) throws Exception {            PreparedStatement stmt = blobSelect;            synchronized (stmt) {                try {                    stmt.setString(1, blobId);                    stmt.execute();                    final ResultSet rs = stmt.getResultSet();                    if (!rs.next()) {                        closeResultSet(rs);                        throw new Exception("no such BLOB: " + blobId);                    }                    InputStream in = rs.getBinaryStream(1);                    if (in == null) {                        // some databases treat zero-length values as NULL;                        // return empty InputStream in such a case                        closeResultSet(rs);                        return new ByteArrayInputStream(new byte[0]);                    }                    /**                     * return an InputStream wrapper in order to                     * close the ResultSet when the stream is closed                     */                    return new FilterInputStream(in) {                        public void close() throws IOException {                            in.close();                            // now it's safe to close ResultSet                            closeResultSet(rs);                        }                    };                } finally {                    resetStatement(stmt);                }            }        }        /**         * {@inheritDoc}         */        public synchronized void put(String blobId, InputStream in, long size)                throws Exception {            PreparedStatement stmt = blobSelectExist;            try {                stmt.setString(1, blobId);                stmt.execute();                ResultSet rs = stmt.getResultSet();                // a BLOB exists if the result has at least one entry                boolean exists = rs.next();                resetStatement(stmt);                closeResultSet(rs);                stmt = (exists) ? blobUpdate : blobInsert;                stmt.setBinaryStream(1, in, (int) size);                stmt.setString(2, blobId);                stmt.executeUpdate();            } finally {                resetStatement(stmt);            }        }        /**         * {@inheritDoc}         */        public synchronized boolean remove(String blobId) throws Exception {            PreparedStatement stmt = blobDelete;            try {                stmt.setString(1, blobId);                return stmt.executeUpdate() == 1;            } finally {                resetStatement(stmt);            }        }        public void close() {            closeStatement(blobInsert);            closeStatement(blobUpdate);            closeStatement(blobSelect);            closeStatement(blobSelectExist);            closeStatement(blobDelete);        }    }}

⌨️ 快捷键说明

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