Search in sources :

Example 21 with FileSystemResource

use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.

the class InMemPersistenceManager method loadContents.

/**
     * Reads the content of the hash maps from the file system
     *
     * @throws Exception if an error occurs
     */
public synchronized void loadContents() throws Exception {
    // read item states
    FileSystemResource fsRes = new FileSystemResource(wspFS, STATE_FILE_PATH);
    if (!fsRes.exists()) {
        return;
    }
    BufferedInputStream bis = new BufferedInputStream(fsRes.getInputStream());
    DataInputStream in = new DataInputStream(bis);
    try {
        // number of entries
        int n = in.readInt();
        while (n-- > 0) {
            // entry type
            byte type = in.readByte();
            ItemId id;
            if (type == NODE_ENTRY) {
                // entry type: node
                // id
                String s = in.readUTF();
                id = NodeId.valueOf(s);
            } else {
                // entry type: property
                // id
                String s = in.readUTF();
                id = PropertyId.valueOf(s);
            }
            // data length
            int length = in.readInt();
            byte[] data = new byte[length];
            // data
            in.readFully(data);
            // store in map
            stateStore.put(id, data);
        }
    } finally {
        in.close();
    }
    // read references
    fsRes = new FileSystemResource(wspFS, REFS_FILE_PATH);
    bis = new BufferedInputStream(fsRes.getInputStream());
    in = new DataInputStream(bis);
    try {
        // number of entries
        int n = in.readInt();
        while (n-- > 0) {
            // target id
            String s = in.readUTF();
            NodeId id = NodeId.valueOf(s);
            // data length
            int length = in.readInt();
            byte[] data = new byte[length];
            // data
            in.readFully(data);
            // store in map
            refsStore.put(id, data);
        }
    } finally {
        in.close();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) NodeId(org.apache.jackrabbit.core.id.NodeId) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) DataInputStream(java.io.DataInputStream) ItemId(org.apache.jackrabbit.core.id.ItemId)

Example 22 with FileSystemResource

use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.

the class InMemPersistenceManager method storeContents.

/**
     * Writes the content of the hash maps to the file system
     *
     * @throws Exception if an error occurs
     */
public synchronized void storeContents() throws Exception {
    // write item states
    FileSystemResource fsRes = new FileSystemResource(wspFS, STATE_FILE_PATH);
    fsRes.makeParentDirs();
    BufferedOutputStream bos = new BufferedOutputStream(fsRes.getOutputStream());
    DataOutputStream out = new DataOutputStream(bos);
    try {
        // number of entries
        out.writeInt(stateStore.size());
        // entries
        for (ItemId id : stateStore.keySet()) {
            if (id.denotesNode()) {
                // entry type
                out.writeByte(NODE_ENTRY);
            } else {
                // entry type
                out.writeByte(PROP_ENTRY);
            }
            // id
            out.writeUTF(id.toString());
            byte[] data = stateStore.get(id);
            // data length
            out.writeInt(data.length);
            // data
            out.write(data);
        }
    } finally {
        out.close();
    }
    // write references
    fsRes = new FileSystemResource(wspFS, REFS_FILE_PATH);
    fsRes.makeParentDirs();
    bos = new BufferedOutputStream(fsRes.getOutputStream());
    out = new DataOutputStream(bos);
    try {
        // number of entries
        out.writeInt(refsStore.size());
        // entries
        for (NodeId id : refsStore.keySet()) {
            // target id
            out.writeUTF(id.toString());
            byte[] data = refsStore.get(id);
            // data length
            out.writeInt(data.length);
            // data
            out.write(data);
        }
    } finally {
        out.close();
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) NodeId(org.apache.jackrabbit.core.id.NodeId) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) BufferedOutputStream(java.io.BufferedOutputStream) ItemId(org.apache.jackrabbit.core.id.ItemId)

Example 23 with FileSystemResource

use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.

the class ObjectPersistenceManager method store.

/**
     * {@inheritDoc}
     */
protected void store(NodeReferences refs) throws ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    String refsFilePath = buildNodeReferencesFilePath(refs.getTargetId());
    FileSystemResource refsFile = new FileSystemResource(itemStateFS, refsFilePath);
    try {
        refsFile.makeParentDirs();
        OutputStream out = new BufferedOutputStream(refsFile.getOutputStream());
        try {
            Serializer.serialize(refs, out);
        } finally {
            out.close();
        }
    } catch (Exception e) {
        String msg = "failed to store " + refs;
        log.debug(msg);
        throw new ItemStateException(msg, e);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) BufferedOutputStream(java.io.BufferedOutputStream) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 24 with FileSystemResource

use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.

the class ObjectPersistenceManager method destroy.

/**
     * {@inheritDoc}
     */
protected void destroy(PropertyState state) throws ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    // delete binary values (stored as files)
    InternalValue[] values = state.getValues();
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
            InternalValue val = values[i];
            if (val != null) {
                val.deleteBinaryResource();
            }
        }
    }
    // delete property file
    String propFilePath = buildPropFilePath(state.getPropertyId());
    FileSystemResource propFile = new FileSystemResource(itemStateFS, propFilePath);
    try {
        if (propFile.exists()) {
            // delete resource and prune empty parent folders
            propFile.delete(true);
        }
    } catch (FileSystemException fse) {
        String msg = "failed to delete property state: " + state.getParentId() + "/" + state.getName();
        log.debug(msg);
        throw new ItemStateException(msg, fse);
    }
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 25 with FileSystemResource

use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.

the class Serializer method serialize.

/**
     * Serializes the specified <code>PropertyState</code> object to the given
     * binary <code>stream</code>. Binary values are stored in the specified
     * <code>BLOBStore</code>.
     *
     * @param state     <code>state</code> to serialize
     * @param stream    the stream where the <code>state</code> should be
     *                  serialized to
     * @param blobStore handler for BLOB data
     * @throws Exception if an error occurs during the serialization
     * @see #deserialize(PropertyState, InputStream,BLOBStore)
     */
public static void serialize(PropertyState state, OutputStream stream, BLOBStore blobStore) throws Exception {
    DataOutputStream out = new DataOutputStream(stream);
    // type
    out.writeInt(state.getType());
    // multiValued
    out.writeBoolean(state.isMultiValued());
    // definitionId
    out.writeUTF("");
    // modCount
    out.writeShort(state.getModCount());
    // values
    InternalValue[] values = state.getValues();
    // count
    out.writeInt(values.length);
    for (int i = 0; i < values.length; i++) {
        InternalValue val = values[i];
        if (state.getType() == PropertyType.BINARY) {
            // special handling required for binary value:
            // put binary value in BLOB store
            InputStream in = val.getStream();
            String blobId = blobStore.createId(state.getPropertyId(), i);
            try {
                blobStore.put(blobId, in, val.getLength());
            } finally {
                IOUtils.closeQuietly(in);
            }
            // store id of BLOB as property value
            // value
            out.writeUTF(blobId);
            // in BLOB store and discard old value instance (e.g. temp file)
            if (blobStore instanceof ResourceBasedBLOBStore) {
                // optimization: if the BLOB store is resource-based
                // retrieve the resource directly rather than having
                // to read the BLOB from an input stream
                FileSystemResource fsRes = ((ResourceBasedBLOBStore) blobStore).getResource(blobId);
                values[i] = InternalValue.create(fsRes);
            } else {
                in = blobStore.get(blobId);
                try {
                    values[i] = InternalValue.create(in);
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
            val.discard();
        } else {
            /**
                 * because writeUTF(String) has a size limit of 65k,
                 * Strings are serialized as <length><byte[]>
                 */
            //out.writeUTF(val.toString());   // value
            byte[] bytes = val.toString().getBytes(ENCODING);
            // lenght of byte[]
            out.writeInt(bytes.length);
            // byte[]
            out.write(bytes);
        }
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) InternalValue(org.apache.jackrabbit.core.value.InternalValue)

Aggregations

FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)37 FileSystemException (org.apache.jackrabbit.core.fs.FileSystemException)23 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)19 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)19 OutputStream (java.io.OutputStream)9 IOException (java.io.IOException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 RepositoryException (javax.jcr.RepositoryException)7 NodeId (org.apache.jackrabbit.core.id.NodeId)7 InputStream (java.io.InputStream)6 InternalValue (org.apache.jackrabbit.core.value.InternalValue)6 BufferedOutputStream (java.io.BufferedOutputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)5 BufferedWriter (java.io.BufferedWriter)4 DataInputStream (java.io.DataInputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 FileSystem (org.apache.jackrabbit.core.fs.FileSystem)4 DataOutputStream (java.io.DataOutputStream)3 Writer (java.io.Writer)3 ArrayList (java.util.ArrayList)3