bundlebinding.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 660 行 · 第 1/2 页
JAVA
660 行
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.core.persistence.bundle.util;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.apache.jackrabbit.core.persistence.util.BLOBStore;import org.apache.jackrabbit.core.persistence.util.ResourceBasedBLOBStore;import org.apache.jackrabbit.core.NodeId;import org.apache.jackrabbit.core.PropertyId;import org.apache.jackrabbit.core.value.InternalValue;import org.apache.jackrabbit.core.value.BLOBFileValue;import org.apache.jackrabbit.core.nodetype.NodeDefId;import org.apache.jackrabbit.core.nodetype.PropDefId;import org.apache.jackrabbit.name.QName;import org.apache.jackrabbit.uuid.UUID;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.IOException;import java.io.InputStream;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import javax.jcr.PropertyType;/** * This Class implements efficient serialization methods for item states. */public class BundleBinding extends ItemStateBinding { /** the cvs/svn id */ static final String CVS_ID = "$URL: http://svn.apache.org/repos/asf/jackrabbit/tags/1.3.3/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/BundleBinding.java $ $Rev: 517626 $ $Date: 2007-03-13 12:00:04 +0200 (Tue, 13 Mar 2007) $"; /** * default logger */ private static Logger log = LoggerFactory.getLogger(BundleBinding.class); /** * Creates a new bundle binding * * @param errorHandling the error handling * @param blobStore the blobstore for retrieving blobs * @param nsIndex the namespace index * @param nameIndex the name index */ public BundleBinding(ErrorHandling errorHandling, BLOBStore blobStore, StringIndex nsIndex, StringIndex nameIndex) { super(errorHandling, blobStore, nsIndex, nameIndex); } /** * Deserializes a <code>NodePropBundle</code> from a data input stream. * * @param in the input stream * @param id the nodeid for the new budle * @return the bundle * @throws IOException if an I/O error occurs. */ public NodePropBundle readBundle(DataInputStream in, NodeId id) throws IOException { NodePropBundle bundle = new NodePropBundle(id); // read version and primary type...special handling int index = in.readInt(); // get version int version = (index >> 24) & 0xff; index &= 0x00ffffff; String uri = nsIndex.indexToString(index); String local = nameIndex.indexToString(in.readInt()); QName nodeTypeName = new QName(uri, local); // primaryType bundle.setNodeTypeName(nodeTypeName); // parentUUID bundle.setParentId(readID(in)); // definitionId bundle.setNodeDefId(NodeDefId.valueOf(in.readUTF())); // mixin types Set mixinTypeNames = new HashSet(); QName name = readIndexedQName(in); while (name != null) { mixinTypeNames.add(name); name = readIndexedQName(in); } bundle.setMixinTypeNames(mixinTypeNames); // properties name = readIndexedQName(in); while (name != null) { PropertyId pId = new PropertyId(bundle.getId(), name); NodePropBundle.PropertyEntry pState = readPropertyEntry(in, pId); bundle.addProperty(pState); name = readIndexedQName(in); } // set referenceable flag bundle.setReferenceable(in.readBoolean()); // child nodes (list of uuid/name pairs) NodeId childId = readID(in); while (childId != null) { bundle.addChildNodeEntry(readQName(in), childId); childId = readID(in); } // read modcount, since version 1.0 if (version >= VERSION_1) { bundle.setModCount(readModCount(in)); } return bundle; } /** * Checks a <code>NodePropBundle</code> from a data input stream. * * @param in the input stream * @return <code>true</code> if the data is valid; * <code>false</code> otherwise. */ public boolean checkBundle(DataInputStream in) { int version; // primaryType & version try { // read version and primary type...special handling int index = in.readInt(); // get version version = (index >> 24) & 0xff; index &= 0x00ffffff; String uri = nsIndex.indexToString(index); String local = nameIndex.indexToString(in.readInt()); QName nodeTypeName = new QName(uri, local); log.info("Serialzation Version: " + version); log.info("NodeTypeName: " + nodeTypeName); } catch (IOException e) { log.error("Error while reading NodeTypeName: " + e); return false; } try { UUID parentUuid = readUUID(in); log.info("ParentUUID: " + parentUuid); } catch (IOException e) { log.error("Error while reading ParentUUID: " + e); return false; } try { String definitionId = in.readUTF(); log.info("DefinitionId: " + definitionId); } catch (IOException e) { log.error("Error while reading DefinitionId: " + e); return false; } try { QName mixinName = readIndexedQName(in); while (mixinName != null) { log.info("MixinTypeName: " + mixinName); mixinName = readIndexedQName(in); } } catch (IOException e) { log.error("Error while reading MixinTypes: " + e); return false; } try { QName propName = readIndexedQName(in); while (propName != null) { log.info("PropertyName: " + propName); if (!checkPropertyState(in)) { return false; } propName = readIndexedQName(in); } } catch (IOException e) { log.error("Error while reading property names: " + e); return false; } try { boolean hasUUID = in.readBoolean(); log.info("hasUUID: " + hasUUID); } catch (IOException e) { log.error("Error while reading 'hasUUID': " + e); return false; } try { UUID cneUUID = readUUID(in); while (cneUUID != null) { QName cneName = readQName(in); log.info("ChildNodentry: " + cneUUID + ":" + cneName); cneUUID = readUUID(in); } } catch (IOException e) { log.error("Error while reading child node entry: " + e); return false; } if (version >= VERSION_1) { try { short modCount = readModCount(in); log.info("modCount: " + modCount); } catch (IOException e) { log.error("Error while reading mod cout: " + e); return false; } } return true; } /** * Serializes a <code>NodePropBundle</code> to a data output stream * * @param out the output stream * @param bundle the bundle to serialize * @throws IOException if an I/O error occurs. */ public void writeBundle(DataOutputStream out, NodePropBundle bundle) throws IOException { long size = out.size(); // primaryType and version out.writeInt((VERSION_CURRENT << 24) | nsIndex.stringToIndex(bundle.getNodeTypeName().getNamespaceURI())); out.writeInt(nameIndex.stringToIndex(bundle.getNodeTypeName().getLocalName())); // parentUUID writeID(out, bundle.getParentId()); // definitionId out.writeUTF(bundle.getNodeDefId().toString()); // mixin types Iterator iter = bundle.getMixinTypeNames().iterator(); while (iter.hasNext()) { writeIndexedQName(out, (QName) iter.next()); } writeIndexedQName(out, null); // properties iter = bundle.getPropertyNames().iterator(); while (iter.hasNext()) { QName pName = (QName) iter.next(); NodePropBundle.PropertyEntry pState = bundle.getPropertyEntry(pName); if (pState == null) { log.error("PropertyState missing in bundle: " + pName); } else { writeIndexedQName(out, pName); writeState(out, pState); } } writeIndexedQName(out, null); // write uuid flag out.writeBoolean(bundle.isReferenceable()); // child nodes (list of uuid/name pairs) iter = bundle.getChildNodeEntries().iterator(); while (iter.hasNext()) { NodePropBundle.ChildNodeEntry entry = (NodePropBundle.ChildNodeEntry) iter.next(); writeID(out, entry.getId()); // uuid writeQName(out, entry.getName()); // name } writeID(out, null); // write mod count writeModCount(out, bundle.getModCount()); // set size of bundle bundle.setSize(out.size()-size); } /** * Deserializes a <code>PropertyState</code> from the data input stream. * * @param in the input stream * @param id the property id for the new propert entry * @return the property entry * @throws IOException if an I/O error occurs. */ public NodePropBundle.PropertyEntry readPropertyEntry(DataInputStream in, PropertyId id) throws IOException { NodePropBundle.PropertyEntry entry = new NodePropBundle.PropertyEntry(id); // type and modcount int type = in.readInt(); entry.setModCount((short) ((type >> 16) & 0x0ffff)); type&=0x0ffff; entry.setType(type); // multiValued entry.setMultiValued(in.readBoolean()); // definitionId entry.setPropDefId(PropDefId.valueOf(in.readUTF())); // values int count = in.readInt(); // count InternalValue[] values = new InternalValue[count]; String[] blobIds = new String[count]; for (int i = 0; i < count; i++) { InternalValue val; switch (type) { case PropertyType.BINARY: int size = in.readInt(); if (size == -1) { blobIds[i] = in.readUTF(); try { if (blobStore instanceof ResourceBasedBLOBStore) { val = InternalValue.create(((ResourceBasedBLOBStore) blobStore).getResource(blobIds[i])); } else { val = InternalValue.create(blobStore.get(blobIds[i]), false); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?