Search in sources :

Example 1 with ResourceBasedBLOBStore

use of org.apache.jackrabbit.core.persistence.util.ResourceBasedBLOBStore in project jackrabbit by apache.

the class XMLPersistenceManager method store.

/**
     * {@inheritDoc}
     */
protected void store(PropertyState state) throws ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    String propFilePath = buildPropFilePath(state.getPropertyId());
    FileSystemResource propFile = new FileSystemResource(itemStateFS, propFilePath);
    try {
        propFile.makeParentDirs();
        OutputStream os = propFile.getOutputStream();
        // write property state to xml file
        Writer writer = null;
        try {
            String encoding = DEFAULT_ENCODING;
            try {
                writer = new BufferedWriter(new OutputStreamWriter(os, encoding));
            } catch (UnsupportedEncodingException e) {
                // should never get here!
                OutputStreamWriter osw = new OutputStreamWriter(os);
                encoding = osw.getEncoding();
                writer = new BufferedWriter(osw);
            }
            String typeName;
            int type = state.getType();
            try {
                typeName = PropertyType.nameFromValue(type);
            } catch (IllegalArgumentException iae) {
                // should never be getting here
                throw new ItemStateException("unexpected property-type ordinal: " + type, iae);
            }
            writer.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
            writer.write("<" + PROPERTY_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(state.getName().toString()) + "\" " + PARENTUUID_ATTRIBUTE + "=\"" + state.getParentId() + "\" " + MULTIVALUED_ATTRIBUTE + "=\"" + Boolean.toString(state.isMultiValued()) + "\" " + MODCOUNT_ATTRIBUTE + "=\"" + state.getModCount() + "\" " + TYPE_ATTRIBUTE + "=\"" + typeName + "\">\n");
            // values
            writer.write("\t<" + VALUES_ELEMENT + ">\n");
            InternalValue[] values = state.getValues();
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    writer.write("\t\t<" + VALUE_ELEMENT + ">");
                    InternalValue val = values[i];
                    if (val != null) {
                        if (type == 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
                            writer.write(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 {
                                    try {
                                        in.close();
                                    } catch (IOException e) {
                                    // ignore
                                    }
                                }
                            }
                            val.discard();
                        } else {
                            writer.write(Text.encodeIllegalXMLCharacters(val.toString()));
                        }
                    }
                    writer.write("</" + VALUE_ELEMENT + ">\n");
                }
            }
            writer.write("\t</" + VALUES_ELEMENT + ">\n");
            writer.write("</" + PROPERTY_ELEMENT + ">\n");
        } finally {
            writer.close();
        }
    } catch (Exception e) {
        String msg = "failed to store property state: " + state.getParentId() + "/" + state.getName();
        log.debug(msg);
        throw new ItemStateException(msg, e);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) IOException(java.io.IOException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) IOException(java.io.IOException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedWriter(java.io.BufferedWriter) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) ResourceBasedBLOBStore(org.apache.jackrabbit.core.persistence.util.ResourceBasedBLOBStore) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 2 with ResourceBasedBLOBStore

use of org.apache.jackrabbit.core.persistence.util.ResourceBasedBLOBStore in project jackrabbit by apache.

the class XMLPersistenceManager method readState.

private void readState(DOMWalker walker, PropertyState state) throws ItemStateException {
    // first do some paranoid sanity checks
    if (!walker.getName().equals(PROPERTY_ELEMENT)) {
        String msg = "invalid serialization format (unexpected element: " + walker.getName() + ")";
        log.debug(msg);
        throw new ItemStateException(msg);
    }
    // check name
    if (!state.getName().equals(factory.create(walker.getAttribute(NAME_ATTRIBUTE)))) {
        String msg = "invalid serialized state: name mismatch";
        log.debug(msg);
        throw new ItemStateException(msg);
    }
    // check parentUUID
    NodeId parentId = NodeId.valueOf(walker.getAttribute(PARENTUUID_ATTRIBUTE));
    if (!parentId.equals(state.getParentId())) {
        String msg = "invalid serialized state: parentUUID mismatch";
        log.debug(msg);
        throw new ItemStateException(msg);
    }
    // now we're ready to read state
    // type
    String typeName = walker.getAttribute(TYPE_ATTRIBUTE);
    int type;
    try {
        type = PropertyType.valueFromName(typeName);
    } catch (IllegalArgumentException iae) {
        // should never be getting here
        throw new ItemStateException("unexpected property-type: " + typeName, iae);
    }
    state.setType(type);
    // multiValued
    String multiValued = walker.getAttribute(MULTIVALUED_ATTRIBUTE);
    state.setMultiValued(Boolean.parseBoolean(multiValued));
    // modification count
    String modCount = walker.getAttribute(MODCOUNT_ATTRIBUTE);
    state.setModCount(Short.parseShort(modCount));
    // values
    ArrayList<InternalValue> values = new ArrayList<InternalValue>();
    if (walker.enterElement(VALUES_ELEMENT)) {
        while (walker.iterateElements(VALUE_ELEMENT)) {
            // read serialized value
            String content = walker.getContent();
            if (PropertyType.STRING == type) {
                // STRING value can be empty; ignore length
                values.add(InternalValue.valueOf(content, type));
            } else if (content.length() > 0) {
                // non-empty non-STRING value
                if (type == PropertyType.BINARY) {
                    try {
                        // in the BLOB store
                        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(content);
                            values.add(InternalValue.create(fsRes));
                        } else {
                            InputStream in = blobStore.get(content);
                            try {
                                values.add(InternalValue.create(in));
                            } finally {
                                IOUtils.closeQuietly(in);
                            }
                        }
                    } catch (Exception e) {
                        String msg = "error while reading serialized binary value";
                        log.debug(msg);
                        throw new ItemStateException(msg, e);
                    }
                } else {
                    // non-empty non-STRING non-BINARY value
                    values.add(InternalValue.valueOf(content, type));
                }
            } else {
                // empty non-STRING value
                log.warn(state.getPropertyId() + ": ignoring empty value of type " + PropertyType.nameFromValue(type));
            }
        }
        walker.leaveElement();
    }
    state.setValues((InternalValue[]) values.toArray(new InternalValue[values.size()]));
}
Also used : ResourceBasedBLOBStore(org.apache.jackrabbit.core.persistence.util.ResourceBasedBLOBStore) InputStream(java.io.InputStream) NodeId(org.apache.jackrabbit.core.id.NodeId) ArrayList(java.util.ArrayList) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) IOException(java.io.IOException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 FileSystemException (org.apache.jackrabbit.core.fs.FileSystemException)2 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)2 ResourceBasedBLOBStore (org.apache.jackrabbit.core.persistence.util.ResourceBasedBLOBStore)2 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)2 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)2 InternalValue (org.apache.jackrabbit.core.value.InternalValue)2 BufferedWriter (java.io.BufferedWriter)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 NodeId (org.apache.jackrabbit.core.id.NodeId)1