You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2018/04/26 13:28:59 UTC
svn commit: r1830222 - in
/jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak:
api/Blob.java plugins/value/BinaryImpl.java plugins/value/ValueImpl.java
Author: reschke
Date: Thu Apr 26 13:28:59 2018
New Revision: 1830222
URL: http://svn.apache.org/viewvc?rev=1830222&view=rev
Log:
OAK-7453: oak-store-document: fix broken line ends in repo (in 1.2 - oak-core)
Modified:
jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java
jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/BinaryImpl.java
jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java
Modified: jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java?rev=1830222&r1=1830221&r2=1830222&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java (original)
+++ jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java Thu Apr 26 13:28:59 2018
@@ -1,90 +1,90 @@
-/*
- * 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.oak.api;
-
-import java.io.InputStream;
-
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
-
-/**
- * Immutable representation of a binary value of finite length.
- * <p>
- * Two blobs are considered equal in terms of {@link Object#equals(Object)}
- * if they contain the same sequences of bytes. Implementations can optimize
- * the equality checks by using strong hash codes or other similar means as
- * long as they comply with the above definition of equality.
- * <p>
- * Due to their nature blobs should not be used as keys in hash tables.
- * To highlight that and to ensure semantic correctness of the equality
- * contract across different blob implementations, the {@link Object#hashCode()}
- * method of all blob instances should return zero.
- */
-public interface Blob {
-
- /**
- * Returns a new stream for this blob. The streams returned from
- * multiple calls to this method are byte wise equals. That is,
- * subsequent calls to {@link java.io.InputStream#read() read}
- * return the same sequence of bytes as long as neither call throws
- * an exception.
- *
- * @return a new stream for this blob
- */
- @Nonnull
- InputStream getNewStream();
-
- /**
- * Returns the length of this blob or -1 if unknown.
- *
- * @return the length of this blob.
- */
- long length();
-
- /**
- * Returns a secure reference to this blob, or {@code null} if such
- * a reference is not available.
- *
- * @see <a href="https://issues.apache.org/jira/browse/OAK-834">OAK-834</a>
- * @return binary reference, or {@code null}
- */
- @CheckForNull
- String getReference();
-
- /**
- * A unique identifier of the content of this value. Usually this is a
- * message digest of the content (a cryptographically secure one-way hash).
- * This allows to avoid processing large binary values multiple times.
- * <p>
- * This method returns null if the identifier is unknown. The identifier may
- * not always be available, for example if the value has not yet been saved
- * or processed. Once an identifier is available, it will never change
- * because values are immutable.
- * <p>
- * If two values have the same identifier, the content of the value is
- * guaranteed to be the same. However it is not guaranteed that two values
- * with the same content will return the same identifier.
- * <p>
- * The identifier is opaque, meaning it can have any format and size.
- *
- * @return the unique identifier or null
- */
- @CheckForNull
- String getContentIdentity();
-}
+/*
+ * 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.oak.api;
+
+import java.io.InputStream;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+/**
+ * Immutable representation of a binary value of finite length.
+ * <p>
+ * Two blobs are considered equal in terms of {@link Object#equals(Object)}
+ * if they contain the same sequences of bytes. Implementations can optimize
+ * the equality checks by using strong hash codes or other similar means as
+ * long as they comply with the above definition of equality.
+ * <p>
+ * Due to their nature blobs should not be used as keys in hash tables.
+ * To highlight that and to ensure semantic correctness of the equality
+ * contract across different blob implementations, the {@link Object#hashCode()}
+ * method of all blob instances should return zero.
+ */
+public interface Blob {
+
+ /**
+ * Returns a new stream for this blob. The streams returned from
+ * multiple calls to this method are byte wise equals. That is,
+ * subsequent calls to {@link java.io.InputStream#read() read}
+ * return the same sequence of bytes as long as neither call throws
+ * an exception.
+ *
+ * @return a new stream for this blob
+ */
+ @Nonnull
+ InputStream getNewStream();
+
+ /**
+ * Returns the length of this blob or -1 if unknown.
+ *
+ * @return the length of this blob.
+ */
+ long length();
+
+ /**
+ * Returns a secure reference to this blob, or {@code null} if such
+ * a reference is not available.
+ *
+ * @see <a href="https://issues.apache.org/jira/browse/OAK-834">OAK-834</a>
+ * @return binary reference, or {@code null}
+ */
+ @CheckForNull
+ String getReference();
+
+ /**
+ * A unique identifier of the content of this value. Usually this is a
+ * message digest of the content (a cryptographically secure one-way hash).
+ * This allows to avoid processing large binary values multiple times.
+ * <p>
+ * This method returns null if the identifier is unknown. The identifier may
+ * not always be available, for example if the value has not yet been saved
+ * or processed. Once an identifier is available, it will never change
+ * because values are immutable.
+ * <p>
+ * If two values have the same identifier, the content of the value is
+ * guaranteed to be the same. However it is not guaranteed that two values
+ * with the same content will return the same identifier.
+ * <p>
+ * The identifier is opaque, meaning it can have any format and size.
+ *
+ * @return the unique identifier or null
+ */
+ @CheckForNull
+ String getContentIdentity();
+}
Modified: jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/BinaryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/BinaryImpl.java?rev=1830222&r1=1830221&r2=1830222&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/BinaryImpl.java (original)
+++ jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/BinaryImpl.java Thu Apr 26 13:28:59 2018
@@ -1,116 +1,116 @@
-/*
- * 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.oak.plugins.value;
-
-import static com.google.common.base.Objects.toStringHelper;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-import javax.annotation.CheckForNull;
-import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
-
-import com.google.common.base.Objects;
-import org.apache.jackrabbit.api.ReferenceBinary;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * TODO document
- */
-class BinaryImpl implements ReferenceBinary {
- private static final Logger LOG = LoggerFactory.getLogger(BinaryImpl.class);
-
- private final ValueImpl value;
-
- BinaryImpl(ValueImpl value) {
- this.value = value;
- }
-
- ValueImpl getBinaryValue() {
- return value.getType() == PropertyType.BINARY ? value : null;
- }
-
- //-------------------------------------------------------------< Binary >---
-
- @Override
- public InputStream getStream() throws RepositoryException {
- return value.getBlob().getNewStream();
- }
-
- @Override
- public int read(byte[] b, long position) throws IOException, RepositoryException {
- InputStream stream = getStream();
- try {
- if (position != stream.skip(position)) {
- throw new IOException("Can't skip to position " + position);
- }
- return stream.read(b);
- } finally {
- stream.close();
- }
- }
-
- @Override
- public long getSize() throws RepositoryException {
- switch (value.getType()) {
- case PropertyType.NAME:
- case PropertyType.PATH:
- // need to respect namespace remapping
- return value.getString().length();
- default:
- return value.getBlob().length();
- }
- }
-
- @Override
- public void dispose() {
- // nothing to do
- }
-
- //---------------------------------------------------< ReferenceBinary >--
-
- @Override @CheckForNull
- public String getReference() {
- try {
- return value.getBlob().getReference();
- } catch (RepositoryException e) {
- LOG.warn("Error getting binary reference", e);
- return null;
- }
- }
-
- @Override
- public boolean equals(Object other) {
- if (other instanceof ReferenceBinary) {
- return Objects.equal(getReference(), ((ReferenceBinary) other).getReference());
- } else {
- return false;
- }
- }
-
- @Override
- public int hashCode() {
- return Objects.hashCode(getReference());
- }
-
- @Override
- public String toString() {
- return toStringHelper(this).addValue(value).toString();
- }
+/*
+ * 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.oak.plugins.value;
+
+import static com.google.common.base.Objects.toStringHelper;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.annotation.CheckForNull;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+
+import com.google.common.base.Objects;
+import org.apache.jackrabbit.api.ReferenceBinary;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * TODO document
+ */
+class BinaryImpl implements ReferenceBinary {
+ private static final Logger LOG = LoggerFactory.getLogger(BinaryImpl.class);
+
+ private final ValueImpl value;
+
+ BinaryImpl(ValueImpl value) {
+ this.value = value;
+ }
+
+ ValueImpl getBinaryValue() {
+ return value.getType() == PropertyType.BINARY ? value : null;
+ }
+
+ //-------------------------------------------------------------< Binary >---
+
+ @Override
+ public InputStream getStream() throws RepositoryException {
+ return value.getBlob().getNewStream();
+ }
+
+ @Override
+ public int read(byte[] b, long position) throws IOException, RepositoryException {
+ InputStream stream = getStream();
+ try {
+ if (position != stream.skip(position)) {
+ throw new IOException("Can't skip to position " + position);
+ }
+ return stream.read(b);
+ } finally {
+ stream.close();
+ }
+ }
+
+ @Override
+ public long getSize() throws RepositoryException {
+ switch (value.getType()) {
+ case PropertyType.NAME:
+ case PropertyType.PATH:
+ // need to respect namespace remapping
+ return value.getString().length();
+ default:
+ return value.getBlob().length();
+ }
+ }
+
+ @Override
+ public void dispose() {
+ // nothing to do
+ }
+
+ //---------------------------------------------------< ReferenceBinary >--
+
+ @Override @CheckForNull
+ public String getReference() {
+ try {
+ return value.getBlob().getReference();
+ } catch (RepositoryException e) {
+ LOG.warn("Error getting binary reference", e);
+ return null;
+ }
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (other instanceof ReferenceBinary) {
+ return Objects.equal(getReference(), ((ReferenceBinary) other).getReference());
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(getReference());
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this).addValue(value).toString();
+ }
}
\ No newline at end of file
Modified: jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java?rev=1830222&r1=1830221&r2=1830222&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java (original)
+++ jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java Thu Apr 26 13:28:59 2018
@@ -1,395 +1,395 @@
-/*
- * 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.oak.plugins.value;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-
-import java.io.InputStream;
-import java.math.BigDecimal;
-import java.util.Calendar;
-
-import javax.annotation.Nonnull;
-import javax.jcr.Binary;
-import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
-import javax.jcr.Value;
-import javax.jcr.ValueFormatException;
-
-import com.google.common.base.Objects;
-import org.apache.jackrabbit.api.JackrabbitValue;
-import org.apache.jackrabbit.oak.api.Blob;
-import org.apache.jackrabbit.oak.api.PropertyState;
-import org.apache.jackrabbit.oak.api.Type;
-import org.apache.jackrabbit.oak.namepath.NamePathMapper;
-import org.apache.jackrabbit.oak.plugins.segment.SegmentNotFoundException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Implementation of {@link Value} based on {@code PropertyState}.
- */
-public class ValueImpl implements JackrabbitValue {
- private static final Logger LOG = LoggerFactory.getLogger(ValueImpl.class);
-
- public static Blob getBlob(Value value) throws RepositoryException {
- if (value instanceof ValueImpl) {
- return ((ValueImpl) value).getBlob();
- } else {
- return new BinaryBasedBlob(value.getBinary());
- }
- }
-
- private final PropertyState propertyState;
- private final Type<?> type;
- private final int index;
- private final NamePathMapper namePathMapper;
-
- private InputStream stream = null;
-
- /**
- * Create a new {@code Value} instance
- * @param property The property state this instance is based on
- * @param index The index
- * @param namePathMapper The name/path mapping used for converting JCR names/paths to
- * the internal representation.
- * @throws IllegalArgumentException if {@code index < propertyState.count()}
- * @throws RepositoryException if the underlying node state cannot be accessed
- */
- ValueImpl(@Nonnull PropertyState property, int index, @Nonnull NamePathMapper namePathMapper)
- throws RepositoryException {
- checkArgument(index < property.count());
- this.propertyState = checkNotNull(property);
- this.type = getType(property);
- this.index = index;
- this.namePathMapper = checkNotNull(namePathMapper);
- }
-
- /**
- * Create a new {@code Value} instance
- * @param property The property state this instance is based on
- * @param namePathMapper The name/path mapping used for converting JCR names/paths to
- * the internal representation.
- * @throws IllegalArgumentException if {@code property.isArray()} is {@code true}.
- * @throws RepositoryException if the underlying node state cannot be accessed
- */
- ValueImpl(@Nonnull PropertyState property, @Nonnull NamePathMapper namePathMapper)
- throws RepositoryException {
- this(checkSingleValued(property), 0, namePathMapper);
- }
-
- private static PropertyState checkSingleValued(PropertyState property) {
- checkArgument(!property.isArray());
- return property;
- }
-
- /**
- * Create a new {@code Value} instance
- * @param property The property state this instance is based on
- * @param index The index
- * @param namePathMapper The name/path mapping used for converting JCR names/paths to
- * the internal representation.
- * @throws IllegalArgumentException if {@code index < propertyState.count()}
- */
- @Nonnull
- static Value newValue(@Nonnull PropertyState property, int index, @Nonnull NamePathMapper namePathMapper) {
- try {
- return new ValueImpl(property, index, namePathMapper);
- } catch (RepositoryException e) {
- return new ErrorValue(e);
- }
- }
-
- /**
- * Create a new {@code Value} instance
- * @param property The property state this instance is based on
- * @param namePathMapper The name/path mapping used for converting JCR names/paths to
- * the internal representation.
- * @throws IllegalArgumentException if {@code property.isArray()} is {@code true}.
- */
- @Nonnull
- static Value newValue(@Nonnull PropertyState property, @Nonnull NamePathMapper namePathMapper) {
- try {
- return new ValueImpl(property, 0, namePathMapper);
- } catch (RepositoryException e) {
- return new ErrorValue(e);
- }
- }
-
- Blob getBlob() throws RepositoryException {
- return getValue(Type.BINARY, index);
- }
-
- /**
- * Same as {@link #getString()} unless that names and paths are returned in their
- * Oak representation instead of being mapped to their JCR representation.
- * @return A String representation of the value of this property.
- */
- public String getOakString() throws RepositoryException {
- return getValue(Type.STRING, index);
- }
-
- //--------------------------------------------------------------< Value >---
-
- /**
- * @see javax.jcr.Value#getType()
- */
- @Override
- public int getType() {
- return type.tag();
- }
-
- /**
- * @see javax.jcr.Value#getBoolean()
- */
- @Override
- public boolean getBoolean() throws RepositoryException {
- switch (getType()) {
- case PropertyType.STRING:
- case PropertyType.BINARY:
- case PropertyType.BOOLEAN:
- return getValue(Type.BOOLEAN, index);
- default:
- throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
- }
- }
-
- /**
- * @see javax.jcr.Value#getDate()
- */
- @Override
- public Calendar getDate() throws RepositoryException {
- try {
- switch (getType()) {
- case PropertyType.STRING:
- case PropertyType.BINARY:
- case PropertyType.DATE:
- String value = getValue(Type.DATE, index);
- return Conversions.convert(value).toCalendar();
- case PropertyType.LONG:
- case PropertyType.DOUBLE:
- case PropertyType.DECIMAL:
- return Conversions.convert(getValue(Type.LONG, index)).toCalendar();
- default:
- throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
- }
- }
- catch (IllegalArgumentException e) {
- throw new ValueFormatException("Error converting value to date", e);
- }
- }
-
- /**
- * @see javax.jcr.Value#getDecimal()
- */
- @Override
- public BigDecimal getDecimal() throws RepositoryException {
- try {
- switch (getType()) {
- case PropertyType.STRING:
- case PropertyType.BINARY:
- case PropertyType.LONG:
- case PropertyType.DOUBLE:
- case PropertyType.DATE:
- case PropertyType.DECIMAL:
- return getValue(Type.DECIMAL, index);
- default:
- throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
- }
- }
- catch (IllegalArgumentException e) {
- throw new ValueFormatException("Error converting value to decimal", e);
- }
- }
-
- /**
- * @see javax.jcr.Value#getDouble()
- */
- @Override
- public double getDouble() throws RepositoryException {
- try {
- switch (getType()) {
- case PropertyType.STRING:
- case PropertyType.BINARY:
- case PropertyType.LONG:
- case PropertyType.DOUBLE:
- case PropertyType.DATE:
- case PropertyType.DECIMAL:
- return getValue(Type.DOUBLE, index);
- default:
- throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
- }
- }
- catch (IllegalArgumentException e) {
- throw new ValueFormatException("Error converting value to double", e);
- }
- }
-
- /**
- * @see javax.jcr.Value#getLong()
- */
- @Override
- public long getLong() throws RepositoryException {
- try {
- switch (getType()) {
- case PropertyType.STRING:
- case PropertyType.BINARY:
- case PropertyType.LONG:
- case PropertyType.DOUBLE:
- case PropertyType.DATE:
- case PropertyType.DECIMAL:
- return getValue(Type.LONG, index);
- default:
- throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
- }
- }
- catch (IllegalArgumentException e) {
- throw new ValueFormatException("Error converting value to long", e);
- }
- }
-
- /**
- * @see javax.jcr.Value#getString()
- */
- @Override
- public String getString() throws RepositoryException {
- checkState(getType() != PropertyType.BINARY || stream == null,
- "getStream has previously been called on this Value instance. " +
- "In this case a new Value instance must be acquired in order to successfully call this method.");
-
- switch (getType()) {
- case PropertyType.NAME:
- return namePathMapper.getJcrName(getOakString());
- case PropertyType.PATH:
- String s = getOakString();
- if (s.startsWith("[") && s.endsWith("]")) {
- // identifier paths are returned as-is (JCR 2.0, 3.4.3.1)
- return s;
- } else {
- return namePathMapper.getJcrPath(s);
- }
- default:
- return getOakString();
- }
- }
-
- /**
- * @see javax.jcr.Value#getStream()
- */
- @Override
- public InputStream getStream() throws IllegalStateException, RepositoryException {
- if (stream == null) {
- stream = getBlob().getNewStream();
- }
- return stream;
- }
-
- /**
- * @see javax.jcr.Value#getBinary()
- */
- @Override
- public Binary getBinary() throws RepositoryException {
- return new BinaryImpl(this);
- }
-
- @Override
- public String getContentIdentity() {
- try {
- return getBlob().getContentIdentity();
- } catch (RepositoryException e) {
- LOG.warn("Error getting content identity", e);
- return null;
- }
- }
-
- //-------------------------------------------------------------< Object >---
-
- /**
- * @see Object#equals(Object)
- */
- @Override
- public boolean equals(Object other) {
- if (other instanceof ValueImpl) {
- ValueImpl that = (ValueImpl) other;
- Type<?> thisType = this.type;
- if (thisType.isArray()) {
- thisType = thisType.getBaseType();
- }
- Type<?> thatType = that.type;
- if (thatType.isArray()) {
- thatType = thatType.getBaseType();
- }
- try {
- return thisType == thatType
- && Objects.equal(
- getValue(thatType, index),
- that.getValue(thatType, that.index));
- } catch (RepositoryException e) {
- LOG.warn("Error while comparing values", e);
- return false;
- }
- } else {
- return false;
- }
- }
-
- /**
- * @see Object#hashCode()
- */
- @Override
- public int hashCode() {
- try {
- if (getType() == PropertyType.BINARY) {
- return getValue(Type.BINARY, index).hashCode();
- } else {
- return getValue(Type.STRING, index).hashCode();
- }
- } catch (RepositoryException e) {
- LOG.warn("Error while calculating hash code", e);
- return 0;
- }
- }
-
- @Override
- public String toString() {
- try {
- return getValue(Type.STRING, index);
- } catch (RepositoryException e) {
- return e.toString();
- }
- }
-
- //------------------------------------------------------------< private >---
-
- private <T> T getValue(Type<T> type, int index) throws RepositoryException {
- try {
- return propertyState.getValue(type, index);
- } catch (SegmentNotFoundException e) {
- throw new RepositoryException(e);
- }
- }
-
- private Type<?> getType(PropertyState property) throws RepositoryException {
- try {
- return propertyState.getType();
- } catch (SegmentNotFoundException e) {
- throw new RepositoryException(e);
- }
- }
-
+/*
+ * 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.oak.plugins.value;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+
+import java.io.InputStream;
+import java.math.BigDecimal;
+import java.util.Calendar;
+
+import javax.annotation.Nonnull;
+import javax.jcr.Binary;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+
+import com.google.common.base.Objects;
+import org.apache.jackrabbit.api.JackrabbitValue;
+import org.apache.jackrabbit.oak.api.Blob;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.plugins.segment.SegmentNotFoundException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of {@link Value} based on {@code PropertyState}.
+ */
+public class ValueImpl implements JackrabbitValue {
+ private static final Logger LOG = LoggerFactory.getLogger(ValueImpl.class);
+
+ public static Blob getBlob(Value value) throws RepositoryException {
+ if (value instanceof ValueImpl) {
+ return ((ValueImpl) value).getBlob();
+ } else {
+ return new BinaryBasedBlob(value.getBinary());
+ }
+ }
+
+ private final PropertyState propertyState;
+ private final Type<?> type;
+ private final int index;
+ private final NamePathMapper namePathMapper;
+
+ private InputStream stream = null;
+
+ /**
+ * Create a new {@code Value} instance
+ * @param property The property state this instance is based on
+ * @param index The index
+ * @param namePathMapper The name/path mapping used for converting JCR names/paths to
+ * the internal representation.
+ * @throws IllegalArgumentException if {@code index < propertyState.count()}
+ * @throws RepositoryException if the underlying node state cannot be accessed
+ */
+ ValueImpl(@Nonnull PropertyState property, int index, @Nonnull NamePathMapper namePathMapper)
+ throws RepositoryException {
+ checkArgument(index < property.count());
+ this.propertyState = checkNotNull(property);
+ this.type = getType(property);
+ this.index = index;
+ this.namePathMapper = checkNotNull(namePathMapper);
+ }
+
+ /**
+ * Create a new {@code Value} instance
+ * @param property The property state this instance is based on
+ * @param namePathMapper The name/path mapping used for converting JCR names/paths to
+ * the internal representation.
+ * @throws IllegalArgumentException if {@code property.isArray()} is {@code true}.
+ * @throws RepositoryException if the underlying node state cannot be accessed
+ */
+ ValueImpl(@Nonnull PropertyState property, @Nonnull NamePathMapper namePathMapper)
+ throws RepositoryException {
+ this(checkSingleValued(property), 0, namePathMapper);
+ }
+
+ private static PropertyState checkSingleValued(PropertyState property) {
+ checkArgument(!property.isArray());
+ return property;
+ }
+
+ /**
+ * Create a new {@code Value} instance
+ * @param property The property state this instance is based on
+ * @param index The index
+ * @param namePathMapper The name/path mapping used for converting JCR names/paths to
+ * the internal representation.
+ * @throws IllegalArgumentException if {@code index < propertyState.count()}
+ */
+ @Nonnull
+ static Value newValue(@Nonnull PropertyState property, int index, @Nonnull NamePathMapper namePathMapper) {
+ try {
+ return new ValueImpl(property, index, namePathMapper);
+ } catch (RepositoryException e) {
+ return new ErrorValue(e);
+ }
+ }
+
+ /**
+ * Create a new {@code Value} instance
+ * @param property The property state this instance is based on
+ * @param namePathMapper The name/path mapping used for converting JCR names/paths to
+ * the internal representation.
+ * @throws IllegalArgumentException if {@code property.isArray()} is {@code true}.
+ */
+ @Nonnull
+ static Value newValue(@Nonnull PropertyState property, @Nonnull NamePathMapper namePathMapper) {
+ try {
+ return new ValueImpl(property, 0, namePathMapper);
+ } catch (RepositoryException e) {
+ return new ErrorValue(e);
+ }
+ }
+
+ Blob getBlob() throws RepositoryException {
+ return getValue(Type.BINARY, index);
+ }
+
+ /**
+ * Same as {@link #getString()} unless that names and paths are returned in their
+ * Oak representation instead of being mapped to their JCR representation.
+ * @return A String representation of the value of this property.
+ */
+ public String getOakString() throws RepositoryException {
+ return getValue(Type.STRING, index);
+ }
+
+ //--------------------------------------------------------------< Value >---
+
+ /**
+ * @see javax.jcr.Value#getType()
+ */
+ @Override
+ public int getType() {
+ return type.tag();
+ }
+
+ /**
+ * @see javax.jcr.Value#getBoolean()
+ */
+ @Override
+ public boolean getBoolean() throws RepositoryException {
+ switch (getType()) {
+ case PropertyType.STRING:
+ case PropertyType.BINARY:
+ case PropertyType.BOOLEAN:
+ return getValue(Type.BOOLEAN, index);
+ default:
+ throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
+ }
+ }
+
+ /**
+ * @see javax.jcr.Value#getDate()
+ */
+ @Override
+ public Calendar getDate() throws RepositoryException {
+ try {
+ switch (getType()) {
+ case PropertyType.STRING:
+ case PropertyType.BINARY:
+ case PropertyType.DATE:
+ String value = getValue(Type.DATE, index);
+ return Conversions.convert(value).toCalendar();
+ case PropertyType.LONG:
+ case PropertyType.DOUBLE:
+ case PropertyType.DECIMAL:
+ return Conversions.convert(getValue(Type.LONG, index)).toCalendar();
+ default:
+ throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
+ }
+ }
+ catch (IllegalArgumentException e) {
+ throw new ValueFormatException("Error converting value to date", e);
+ }
+ }
+
+ /**
+ * @see javax.jcr.Value#getDecimal()
+ */
+ @Override
+ public BigDecimal getDecimal() throws RepositoryException {
+ try {
+ switch (getType()) {
+ case PropertyType.STRING:
+ case PropertyType.BINARY:
+ case PropertyType.LONG:
+ case PropertyType.DOUBLE:
+ case PropertyType.DATE:
+ case PropertyType.DECIMAL:
+ return getValue(Type.DECIMAL, index);
+ default:
+ throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
+ }
+ }
+ catch (IllegalArgumentException e) {
+ throw new ValueFormatException("Error converting value to decimal", e);
+ }
+ }
+
+ /**
+ * @see javax.jcr.Value#getDouble()
+ */
+ @Override
+ public double getDouble() throws RepositoryException {
+ try {
+ switch (getType()) {
+ case PropertyType.STRING:
+ case PropertyType.BINARY:
+ case PropertyType.LONG:
+ case PropertyType.DOUBLE:
+ case PropertyType.DATE:
+ case PropertyType.DECIMAL:
+ return getValue(Type.DOUBLE, index);
+ default:
+ throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
+ }
+ }
+ catch (IllegalArgumentException e) {
+ throw new ValueFormatException("Error converting value to double", e);
+ }
+ }
+
+ /**
+ * @see javax.jcr.Value#getLong()
+ */
+ @Override
+ public long getLong() throws RepositoryException {
+ try {
+ switch (getType()) {
+ case PropertyType.STRING:
+ case PropertyType.BINARY:
+ case PropertyType.LONG:
+ case PropertyType.DOUBLE:
+ case PropertyType.DATE:
+ case PropertyType.DECIMAL:
+ return getValue(Type.LONG, index);
+ default:
+ throw new ValueFormatException("Incompatible type " + PropertyType.nameFromValue(getType()));
+ }
+ }
+ catch (IllegalArgumentException e) {
+ throw new ValueFormatException("Error converting value to long", e);
+ }
+ }
+
+ /**
+ * @see javax.jcr.Value#getString()
+ */
+ @Override
+ public String getString() throws RepositoryException {
+ checkState(getType() != PropertyType.BINARY || stream == null,
+ "getStream has previously been called on this Value instance. " +
+ "In this case a new Value instance must be acquired in order to successfully call this method.");
+
+ switch (getType()) {
+ case PropertyType.NAME:
+ return namePathMapper.getJcrName(getOakString());
+ case PropertyType.PATH:
+ String s = getOakString();
+ if (s.startsWith("[") && s.endsWith("]")) {
+ // identifier paths are returned as-is (JCR 2.0, 3.4.3.1)
+ return s;
+ } else {
+ return namePathMapper.getJcrPath(s);
+ }
+ default:
+ return getOakString();
+ }
+ }
+
+ /**
+ * @see javax.jcr.Value#getStream()
+ */
+ @Override
+ public InputStream getStream() throws IllegalStateException, RepositoryException {
+ if (stream == null) {
+ stream = getBlob().getNewStream();
+ }
+ return stream;
+ }
+
+ /**
+ * @see javax.jcr.Value#getBinary()
+ */
+ @Override
+ public Binary getBinary() throws RepositoryException {
+ return new BinaryImpl(this);
+ }
+
+ @Override
+ public String getContentIdentity() {
+ try {
+ return getBlob().getContentIdentity();
+ } catch (RepositoryException e) {
+ LOG.warn("Error getting content identity", e);
+ return null;
+ }
+ }
+
+ //-------------------------------------------------------------< Object >---
+
+ /**
+ * @see Object#equals(Object)
+ */
+ @Override
+ public boolean equals(Object other) {
+ if (other instanceof ValueImpl) {
+ ValueImpl that = (ValueImpl) other;
+ Type<?> thisType = this.type;
+ if (thisType.isArray()) {
+ thisType = thisType.getBaseType();
+ }
+ Type<?> thatType = that.type;
+ if (thatType.isArray()) {
+ thatType = thatType.getBaseType();
+ }
+ try {
+ return thisType == thatType
+ && Objects.equal(
+ getValue(thatType, index),
+ that.getValue(thatType, that.index));
+ } catch (RepositoryException e) {
+ LOG.warn("Error while comparing values", e);
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * @see Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ try {
+ if (getType() == PropertyType.BINARY) {
+ return getValue(Type.BINARY, index).hashCode();
+ } else {
+ return getValue(Type.STRING, index).hashCode();
+ }
+ } catch (RepositoryException e) {
+ LOG.warn("Error while calculating hash code", e);
+ return 0;
+ }
+ }
+
+ @Override
+ public String toString() {
+ try {
+ return getValue(Type.STRING, index);
+ } catch (RepositoryException e) {
+ return e.toString();
+ }
+ }
+
+ //------------------------------------------------------------< private >---
+
+ private <T> T getValue(Type<T> type, int index) throws RepositoryException {
+ try {
+ return propertyState.getValue(type, index);
+ } catch (SegmentNotFoundException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
+ private Type<?> getType(PropertyState property) throws RepositoryException {
+ try {
+ return propertyState.getType();
+ } catch (SegmentNotFoundException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
}
\ No newline at end of file