bundlefspersistencemanager.java

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

JAVA
680
字号
        this.name = context.getHomeDir().getName();        // create item fs        if (itemFSBlockSize == 0) {            itemFs = new BasedFileSystem(context.getFileSystem(), "items");        } else {            /*            CQFileSystem cqfs = new CQFileSystem();            cqfs.setPath(new File(context.getHomeDir(), "items.dat").getCanonicalPath());            cqfs.setAutoRepair(false);            cqfs.setAutoSync(false);            cqfs.setBlockSize(itemFSBlockSize);            cqfs.setCacheInitialBlocks(itemFSInitialCache);            cqfs.setCacheMaximumBlocks(itemFSMaximumCache);            cqfs.init();            itemFs = cqfs;            */        }        // create correct blob store        if (useLocalFsBlobStore()) {            LocalFileSystem blobFS = new LocalFileSystem();            blobFS.setRoot(new File(context.getHomeDir(), "blobs"));            blobFS.init();            blobStore = new BundleFsPersistenceManager.FSBlobStore(blobFS);        } else if (useItemBlobStore()) {            blobStore = new BundleFsPersistenceManager.FSBlobStore(itemFs);        } else /* useCqFsBlobStore() */ {//blobStore = createCQFSBlobStore(context);        }        // load namespaces        binding = new BundleBinding(errorHandling, blobStore, getNsIndex(), getNameIndex());        binding.setMinBlobSize(minBlobSize);        initialized = true;    }    /**     * {@inheritDoc}     */    public synchronized void close() throws Exception {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        try {            // close blob store            blobStore.close();            blobStore = null;            itemFs.close();            itemFs = null;        } finally {            initialized = false;        }    }    /**     * {@inheritDoc}     */    protected synchronized NodePropBundle loadBundle(NodeId id)            throws ItemStateException {        DataInputStream din = null;        try {            String path = buildNodeFilePath(null, id).toString();            if (!itemFs.exists(path)) {                return null;            }            InputStream in = itemFs.getInputStream(path);            TrackingInputStream cin = new TrackingInputStream(in);            din = new DataInputStream(cin);            NodePropBundle bundle = binding.readBundle(din, id);            bundle.setSize(cin.getPosition());            return bundle;        } catch (Exception e) {            String msg = "failed to read bundle: " + id + ": " + e;            log.error(msg);            throw new ItemStateException(msg, e);        } finally {            closeStream(din);        }    }    /**     * {@inheritDoc}     */    protected synchronized boolean existsBundle(NodeId id) throws ItemStateException {        try {            StringBuffer buf = buildNodeFilePath(null, id);            return itemFs.exists(buf.toString());        } catch (Exception e) {            String msg = "failed to check existence of bundle: " + id;            BundleFsPersistenceManager.log.error(msg, e);            throw new ItemStateException(msg, e);        }    }        /**     * Creates the file path for the given node id that is     * suitable for storing node states in a filesystem.     *     * @param buf buffer to append to or <code>null</code>     * @param id the id of the node     * @return the buffer with the appended data.     */    protected StringBuffer buildNodeFilePath(StringBuffer buf, NodeId id) {        if (buf == null) {            buf = new StringBuffer();        }        buildNodeFolderPath(buf, id);        buf.append('.');        buf.append(NODEFILENAME);        return buf;    }            /**     * Creates the file path for the given references id that is     * suitable for storing reference states in a filesystem.     *     * @param buf buffer to append to or <code>null</code>     * @param id the id of the node     * @return the buffer with the appended data.     */    protected StringBuffer buildNodeReferencesFilePath(StringBuffer buf,                                                       NodeReferencesId id) {        if (buf == null) {            buf = new StringBuffer();        }        buildNodeFolderPath(buf, id.getTargetId());        buf.append('.');        buf.append(NODEREFSFILENAME);        return buf;    }        /**     * {@inheritDoc}     */    protected synchronized void storeBundle(NodePropBundle bundle) throws ItemStateException {        try {            StringBuffer buf = buildNodeFolderPath(null, bundle.getId());            buf.append('.');            buf.append(NODEFILENAME);            String fileName = buf.toString();            String dir = fileName.substring(0, fileName.lastIndexOf(FileSystem.SEPARATOR_CHAR));            if (!itemFs.exists(dir)) {                itemFs.createFolder(dir);            }            OutputStream out = itemFs.getOutputStream(fileName);            DataOutputStream dout = new DataOutputStream(out);            binding.writeBundle(dout, bundle);            dout.close();        } catch (Exception e) {            String msg = "failed to write bundle: " + bundle.getId();            BundleFsPersistenceManager.log.error(msg, e);            throw new ItemStateException(msg, e);        }    }    /**     * {@inheritDoc}     */    protected synchronized void destroyBundle(NodePropBundle bundle) throws ItemStateException {        try {            StringBuffer buf = buildNodeFilePath(null, bundle.getId());            itemFs.deleteFile(buf.toString());        } catch (Exception e) {            if (e instanceof NoSuchItemStateException) {                throw (NoSuchItemStateException) e;            }            String msg = "failed to delete bundle: " + bundle.getId();            BundleFsPersistenceManager.log.error(msg, e);            throw new ItemStateException(msg, e);        }    }    /**     * {@inheritDoc}     */    public synchronized NodeReferences load(NodeReferencesId targetId)            throws NoSuchItemStateException, ItemStateException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        InputStream in = null;        try {            String path = buildNodeReferencesFilePath(null, targetId).toString();            if (!itemFs.exists(path)) {                // special case                throw new NoSuchItemStateException(targetId.toString());            }            in = itemFs.getInputStream(path);            NodeReferences refs = new NodeReferences(targetId);            Serializer.deserialize(refs, in);            return refs;        } catch (NoSuchItemStateException e) {            throw e;        } catch (Exception e) {            String msg = "failed to read references: " + targetId;            BundleFsPersistenceManager.log.error(msg, e);            throw new ItemStateException(msg, e);        } finally {            closeStream(in);        }    }    /**     * {@inheritDoc}     */    public synchronized void store(NodeReferences refs)            throws ItemStateException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        try {            StringBuffer buf = buildNodeFolderPath(null, refs.getTargetId());            buf.append('.');            buf.append(NODEREFSFILENAME);            String fileName = buf.toString();            String dir = fileName.substring(0, fileName.lastIndexOf(FileSystem.SEPARATOR_CHAR));            if (!itemFs.exists(dir)) {                itemFs.createFolder(dir);            }            OutputStream out = itemFs.getOutputStream(fileName);            Serializer.serialize(refs, out);            out.close();        } catch (Exception e) {            String msg = "failed to write property state: " + refs.getTargetId();            BundleFsPersistenceManager.log.error(msg, e);            throw new ItemStateException(msg, e);        }    }    /**     * {@inheritDoc}     */    public synchronized void destroy(NodeReferences refs) throws ItemStateException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        try {            StringBuffer buf = buildNodeReferencesFilePath(null, refs.getId());            itemFs.deleteFile(buf.toString());        } catch (Exception e) {            if (e instanceof NoSuchItemStateException) {                throw (NoSuchItemStateException) e;            }            String msg = "failed to delete references: " + refs.getTargetId();            BundleFsPersistenceManager.log.error(msg, e);            throw new ItemStateException(msg, e);        }    }    /**     * {@inheritDoc}     */    public synchronized boolean exists(NodeReferencesId targetId) throws ItemStateException {        if (!initialized) {            throw new IllegalStateException("not initialized");        }        try {            StringBuffer buf = buildNodeReferencesFilePath(null, targetId);            return itemFs.exists(buf.toString());        } catch (Exception e) {            String msg = "failed to check existence of node references: " + targetId;            BundleFsPersistenceManager.log.error(msg, e);            throw new ItemStateException(msg, e);        }    }    /**     * closes the input stream     * @param ins     */    protected void closeStream(InputStream ins) {        if (ins != null) {            try {                ins.close();            } catch (IOException ignore) {                // ignore            }        }    }    /**     * logs an sql exception     * @param message     * @param se     */    protected void logException(String message, SQLException se) {        if (message != null) {            BundleFsPersistenceManager.log.error(message);        }        BundleFsPersistenceManager.log.error("       Reason: " + se.getMessage());        BundleFsPersistenceManager.log.error("   State/Code: " + se.getSQLState() + "/" +                se.getErrorCode());        BundleFsPersistenceManager.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.     */    private class FSBlobStore extends FileSystemBLOBStore implements BundleFsPersistenceManager.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            }        }    }}

⌨️ 快捷键说明

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