X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fbinary%2FBinaryObject.java;h=b9ff191b3bd1389bfd878c297da262a508bad6ad;hb=refs%2Fchanges%2F65%2F2565%2F2;hp=149d9930ecd6850e13646bd7b612a69c4a3d5e5d;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryObject.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryObject.java index 149d9930e..b9ff191b3 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryObject.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryObject.java @@ -1,59 +1,59 @@ -/******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.databoard.accessor.binary; -import java.io.File; -import java.io.IOException; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.Executor; -import java.util.concurrent.locks.Lock; - -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.CloseableAccessor; -import org.simantics.databoard.accessor.ParametrisedAccessor; -import org.simantics.databoard.accessor.error.AccessorConstructionException; -import org.simantics.databoard.accessor.error.AccessorException; -import org.simantics.databoard.accessor.error.ReferenceException; -import org.simantics.databoard.accessor.event.Event; -import org.simantics.databoard.accessor.event.InvalidatedEvent; -import org.simantics.databoard.accessor.file.FileAccessor; -import org.simantics.databoard.accessor.impl.AccessorParams; -import org.simantics.databoard.accessor.impl.ListenerEntry; -import org.simantics.databoard.accessor.interestset.InterestSet; -import org.simantics.databoard.accessor.reference.ChildReference; -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.adapter.AdapterConstructionException; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.serialization.Serializer; -import org.simantics.databoard.serialization.SerializerConstructionException; -import org.simantics.databoard.type.ArrayType; -import org.simantics.databoard.type.BooleanType; -import org.simantics.databoard.type.ByteType; -import org.simantics.databoard.type.Datatype; -import org.simantics.databoard.type.DoubleType; -import org.simantics.databoard.type.FloatType; -import org.simantics.databoard.type.IntegerType; -import org.simantics.databoard.type.LongType; -import org.simantics.databoard.type.MapType; -import org.simantics.databoard.type.OptionalType; -import org.simantics.databoard.type.RecordType; -import org.simantics.databoard.type.StringType; -import org.simantics.databoard.type.UnionType; -import org.simantics.databoard.type.VariantType; -import org.simantics.databoard.util.binary.BinaryFile; -import org.simantics.databoard.util.binary.Blob; -import org.simantics.databoard.util.binary.RandomAccessBinary; +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.locks.Lock; + +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.CloseableAccessor; +import org.simantics.databoard.accessor.ParametrisedAccessor; +import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.error.AccessorException; +import org.simantics.databoard.accessor.error.ReferenceException; +import org.simantics.databoard.accessor.event.Event; +import org.simantics.databoard.accessor.event.InvalidatedEvent; +import org.simantics.databoard.accessor.file.FileAccessor; +import org.simantics.databoard.accessor.impl.AccessorParams; +import org.simantics.databoard.accessor.impl.ListenerEntry; +import org.simantics.databoard.accessor.interestset.InterestSet; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.adapter.AdapterConstructionException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.serialization.SerializerConstructionException; +import org.simantics.databoard.type.ArrayType; +import org.simantics.databoard.type.BooleanType; +import org.simantics.databoard.type.ByteType; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.DoubleType; +import org.simantics.databoard.type.FloatType; +import org.simantics.databoard.type.IntegerType; +import org.simantics.databoard.type.LongType; +import org.simantics.databoard.type.MapType; +import org.simantics.databoard.type.OptionalType; +import org.simantics.databoard.type.RecordType; +import org.simantics.databoard.type.StringType; +import org.simantics.databoard.type.UnionType; +import org.simantics.databoard.type.VariantType; +import org.simantics.databoard.util.binary.BinaryFile; +import org.simantics.databoard.util.binary.Blob; +import org.simantics.databoard.util.binary.RandomAccessBinary; /** * BinaryObject is an accessor to a binary object, usually a random access file. @@ -61,23 +61,23 @@ import org.simantics.databoard.util.binary.RandomAccessBinary; *

* The file can be opened once. It may not be modified by any other instance other than * accessor while accessors are beign used. You must not create more than one - * instance of BinaryObjects for a file. - *

- * - * - * @see BinaryArray - * @see BinaryBoolean - * @see BinaryByte - * @see BinaryDouble - * @see BinaryFloat - * @see BinaryInteger - * @see BinaryLong - * @see BinaryMap - * @see BinaryOptional - * @see BinaryRecord - * @see BinaryString - * @see BinaryUnion - * @see BinaryVariant + * instance of BinaryObjects for a file. + *

+ * + * + * @see BinaryArray + * @see BinaryBoolean + * @see BinaryByte + * @see BinaryDouble + * @see BinaryFloat + * @see BinaryInteger + * @see BinaryLong + * @see BinaryMap + * @see BinaryOptional + * @see BinaryRecord + * @see BinaryString + * @see BinaryUnion + * @see BinaryVariant * @author Toni Kalajainen */ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableAccessor, ParametrisedAccessor { @@ -89,27 +89,27 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA /** Random access binary object */ protected Blob b; /** Type */ - protected Datatype type; - /** File, optional */ + protected Datatype type; + /** File, optional */ protected File file; - /** Accessor params */ - protected AccessorParams params; + /** Accessor params */ + protected AccessorParams params; BinaryObject(Accessor parent, Blob blob, Datatype type, AccessorParams params) { this.parent = parent; this.b = blob; - this.type = type; - this.params = params; - - if (parent!=null && parent instanceof BinaryObject) { - file = ((BinaryObject)parent).file(); - } else { - RandomAccessBinary sourceBinary = b.getSource(); - if (sourceBinary instanceof BinaryFile) { - BinaryFile bf = (BinaryFile) sourceBinary; - file = bf.file(); - } - } + this.type = type; + this.params = params; + + if (parent!=null && parent instanceof BinaryObject) { + file = ((BinaryObject)parent).file(); + } else { + RandomAccessBinary sourceBinary = b.getSource(); + if (sourceBinary instanceof BinaryFile) { + BinaryFile bf = (BinaryFile) sourceBinary; + file = bf.file(); + } + } } public Datatype type() { @@ -122,49 +122,49 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA } catch (IOException e) { throw new AccessorException(e); } - } - - @Override - public void reset() throws AccessorException { - try { - b.reset(); - } catch (IOException e) { - throw new AccessorException(e); - } - } - - public RandomAccessBinary getSource() { - RandomAccessBinary result = b; - while (result instanceof Blob) result = ((Blob)result).getParent(); - return result; } - - /** - * Close the random access file beneath + + @Override + public void reset() throws AccessorException { + try { + b.reset(); + } catch (IOException e) { + throw new AccessorException(e); + } + } + + public RandomAccessBinary getSource() { + RandomAccessBinary result = b; + while (result instanceof Blob) result = ((Blob)result).getParent(); + return result; + } + + /** + * Close the random access file beneath */ public void close() throws AccessorException { - writeLock(); + writeLock(); try { if (parent!=null) { ((FileAccessor) parent).close(); return; } - // Root Object - if (b==null) return; + // Root Object + if (b==null) return; RandomAccessBinary rab = getSource(); rab.flush(); - rab.close(); + rab.close(); b = null; } catch (IOException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } - } - - public boolean isOpen() { - return b.isOpen(); + } + + public boolean isOpen() { + return b.isOpen(); } /** @@ -172,18 +172,18 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA * * @return file or null */ - public File file() { + public File file() { return file; } public RandomAccessBinary getBinary() { return b; } - - @Override - public AccessorParams getParams() { - return params; - } + + @Override + public AccessorParams getParams() { + return params; + } @Override public void addListener(Listener listener, InterestSet interestSet, ChildReference path, Executor executor) throws AccessorException { @@ -224,16 +224,16 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA */ @Override public void setValue(Binding binding, Object newValue) - throws AccessorException { - assert b.isOpen(); - writeLock(); + throws AccessorException { + assert b.isOpen(); + writeLock(); try { setValueNoflush(binding, newValue); b.flush(); } catch (IOException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -245,22 +245,22 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA * @throws AccessorException */ public abstract void setValueNoflush(Binding binding, Object newValue) throws AccessorException; - - public boolean setValue(ChildReference path, Binding binding, Object obj) throws AccessorException { - try { - Accessor a = getComponent(path); - a.setValue(binding, obj); - return true; - } catch (ReferenceException re) { - return false; - } catch (AccessorConstructionException e) { - throw new AccessorException(e); - } - } + + public boolean setValue(ChildReference path, Binding binding, Object obj) throws AccessorException { + try { + Accessor a = getComponent(path); + a.setValue(binding, obj); + return true; + } catch (ReferenceException re) { + return false; + } catch (AccessorConstructionException e) { + throw new AccessorException(e); + } + } @Override - public Object getValue(Binding binding) throws AccessorException { - assert b.isOpen(); + public Object getValue(Binding binding) throws AccessorException { + assert b.isOpen(); readLock(); try { Serializer s = params.serializerScheme.getSerializer( binding ); @@ -268,56 +268,56 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA return s.deserialize(b, null); } catch (IOException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); + } + } + + public void getValue(Binding binding, Object obj) throws AccessorException { + assert b.isOpen(); + readLock(); + try { + Serializer s = params.serializerScheme.getSerializer( binding ); + b.position(0L); + s.deserializeTo(b, null, obj); + } catch (IOException e) { + throw new AccessorException(e); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); + } + } + + @Override + public boolean getValue(ChildReference path, Binding binding, Object obj) throws AccessorException { + try { + Accessor a = getComponent(path); + a.getValue(binding, obj); + return true; + } catch (ReferenceException re) { + return false; + } catch (AccessorConstructionException e) { + throw new AccessorException(e); + } + } + + public Object getValue(ChildReference path, Binding binding) throws AccessorException { + try { + Accessor a = getComponent(path); + return a.getValue(binding); + } catch (ReferenceException re) { + return null; + } catch (AccessorConstructionException e) { + throw new AccessorException(e); } } - - public void getValue(Binding binding, Object obj) throws AccessorException { - assert b.isOpen(); - readLock(); - try { - Serializer s = params.serializerScheme.getSerializer( binding ); - b.position(0L); - s.deserializeTo(b, null, obj); - } catch (IOException e) { - throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); - } - } - - @Override - public boolean getValue(ChildReference path, Binding binding, Object obj) throws AccessorException { - try { - Accessor a = getComponent(path); - a.getValue(binding, obj); - return true; - } catch (ReferenceException re) { - return false; - } catch (AccessorConstructionException e) { - throw new AccessorException(e); - } - } - - public Object getValue(ChildReference path, Binding binding) throws AccessorException { - try { - Accessor a = getComponent(path); - return a.getValue(binding); - } catch (ReferenceException re) { - return null; - } catch (AccessorConstructionException e) { - throw new AccessorException(e); - } - } - - Object adapt(Object value, Binding domain, Binding range) throws AdaptException, AdapterConstructionException { - return params.adapterScheme.getAdapter(domain, range, true, false).adapt(value); - } + + Object adapt(Object value, Binding domain, Binding range) throws AdaptException, AdapterConstructionException { + return params.adapterScheme.getAdapter(domain, range, true, false).adapt(value); + } /** * Send notification that this accessor has been detached from the parent @@ -347,8 +347,8 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA @Override public void apply(List cs, LinkedList rollback) throws AccessorException { - assert b.isOpen(); - writeLock(); + assert b.isOpen(); + writeLock(); try { boolean makeRollback = rollback != null; for (Event e : cs) { @@ -376,8 +376,8 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA } catch (IOException e1) { } throw new AccessorException(ae); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -388,7 +388,7 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA Datatype type = type(); // Binding binding = params.bindingScheme.getBinding(type); // Object instance = getValue(binding); -// return "Accessor("+binding.printValueDefinition(instance, true)+")"; +// return "Accessor("+binding.printValueDefinition(instance, true)+")"; return this.getClass()+"("+type+")"; // } catch (AccessorException e) { // return "Accessor(error="+e.getMessage()+")"; @@ -396,8 +396,8 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA // return "Accessor(error="+e.getMessage()+")"; // } catch (IOException e) { // return "Accessor(error="+e.getMessage()+")"; -// } catch (BindingConstructionException e) { -// return "Accessor(error="+e.getMessage()+")"; +// } catch (BindingConstructionException e) { +// return "Accessor(error="+e.getMessage()+")"; // } } @@ -443,9 +443,9 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA return new BinaryMap(null, blob, (MapType)type, params); } if (type instanceof ArrayType) { - return new BinaryArray(null, blob, (ArrayType)type, params); + return new BinaryArray(null, blob, (ArrayType)type, params); } - + throw new AccessorConstructionException("Can not create accessor to "+type); } catch (IOException e) { throw new AccessorConstructionException(e); @@ -497,65 +497,65 @@ public abstract class BinaryObject implements Accessor, FileAccessor, CloseableA } throw new AccessorConstructionException("Can not create accessor to "+type); } - - protected void emitEvent(ListenerEntry le, Event e) { - e.reference = ChildReference.concatenate(le.path, e.reference); - le.emitEvent(e); - } - - protected void emitEvents(ListenerEntry le, Collection events) { - for (Event e : events) - e.reference = ChildReference.concatenate(le.path, e.reference); - le.emitEvents(events); - } - - /** - * Get lock if available. - * - * @return lock or null - */ - public Lock getReadLock() { - return params.readLock; - } - - /** - * Get lock if available. - * - * @return lock or null - */ - public Lock getWriteLock() { - return params.writeLock; - } - - - /** - * Lock the lock if there is a lock. - */ - protected void readLock() { - if (params.readLock!=null) params.readLock.lock(); - } - - /** - * Unlock the lock if one exists - */ - protected void readUnlock() { - if (params.readLock!=null) params.readLock.unlock(); - } - - /** - * Lock the lock if there is a lock. - */ - protected void writeLock() { - if (params.writeLock!=null) params.writeLock.lock(); - } - - /** - * Unlock the lock if one exists - */ - protected void writeUnlock() { - if (params.writeLock!=null) params.writeLock.unlock(); - } - + + protected void emitEvent(ListenerEntry le, Event e) { + e.reference = ChildReference.concatenate(le.path, e.reference); + le.emitEvent(e); + } + + protected void emitEvents(ListenerEntry le, Collection events) { + for (Event e : events) + e.reference = ChildReference.concatenate(le.path, e.reference); + le.emitEvents(events); + } + + /** + * Get lock if available. + * + * @return lock or null + */ + public Lock getReadLock() { + return params.readLock; + } + + /** + * Get lock if available. + * + * @return lock or null + */ + public Lock getWriteLock() { + return params.writeLock; + } + + + /** + * Lock the lock if there is a lock. + */ + protected void readLock() { + if (params.readLock!=null) params.readLock.lock(); + } + + /** + * Unlock the lock if one exists + */ + protected void readUnlock() { + if (params.readLock!=null) params.readLock.unlock(); + } + + /** + * Lock the lock if there is a lock. + */ + protected void writeLock() { + if (params.writeLock!=null) params.writeLock.lock(); + } + + /** + * Unlock the lock if one exists + */ + protected void writeUnlock() { + if (params.writeLock!=null) params.writeLock.unlock(); + } + }