-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
+/*******************************************************************************
+ * 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;\r
-import java.io.IOException;\r
-import java.util.Collection;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import java.util.concurrent.Executor;\r
-import java.util.concurrent.locks.Lock;\r
-\r
-import org.simantics.databoard.accessor.Accessor;\r
-import org.simantics.databoard.accessor.CloseableAccessor;\r
-import org.simantics.databoard.accessor.ParametrisedAccessor;\r
-import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
-import org.simantics.databoard.accessor.error.AccessorException;\r
-import org.simantics.databoard.accessor.error.ReferenceException;\r
-import org.simantics.databoard.accessor.event.Event;\r
-import org.simantics.databoard.accessor.event.InvalidatedEvent;\r
-import org.simantics.databoard.accessor.file.FileAccessor;\r
-import org.simantics.databoard.accessor.impl.AccessorParams;\r
-import org.simantics.databoard.accessor.impl.ListenerEntry;\r
-import org.simantics.databoard.accessor.interestset.InterestSet;\r
-import org.simantics.databoard.accessor.reference.ChildReference;\r
-import org.simantics.databoard.adapter.AdaptException;\r
-import org.simantics.databoard.adapter.AdapterConstructionException;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.serialization.Serializer;\r
-import org.simantics.databoard.serialization.SerializerConstructionException;\r
-import org.simantics.databoard.type.ArrayType;\r
-import org.simantics.databoard.type.BooleanType;\r
-import org.simantics.databoard.type.ByteType;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.databoard.type.DoubleType;\r
-import org.simantics.databoard.type.FloatType;\r
-import org.simantics.databoard.type.IntegerType;\r
-import org.simantics.databoard.type.LongType;\r
-import org.simantics.databoard.type.MapType;\r
-import org.simantics.databoard.type.OptionalType;\r
-import org.simantics.databoard.type.RecordType;\r
-import org.simantics.databoard.type.StringType;\r
-import org.simantics.databoard.type.UnionType;\r
-import org.simantics.databoard.type.VariantType;\r
-import org.simantics.databoard.util.binary.BinaryFile;\r
-import org.simantics.databoard.util.binary.Blob;\r
-import org.simantics.databoard.util.binary.RandomAccessBinary;\r
+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.
* <p>
* 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.\r
- * <p>\r
- * \r
- * \r
- * @see BinaryArray\r
- * @see BinaryBoolean\r
- * @see BinaryByte\r
- * @see BinaryDouble\r
- * @see BinaryFloat\r
- * @see BinaryInteger\r
- * @see BinaryLong\r
- * @see BinaryMap\r
- * @see BinaryOptional\r
- * @see BinaryRecord\r
- * @see BinaryString\r
- * @see BinaryUnion\r
- * @see BinaryVariant\r
+ * instance of BinaryObjects for a file.
+ * <p>
+ *
+ *
+ * @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 <toni.kalajainen@vtt.fi>
*/
public abstract class BinaryObject implements Accessor, FileAccessor, CloseableAccessor, ParametrisedAccessor {
/** Random access binary object */
protected Blob b;
/** Type */
- protected Datatype type;\r
- /** File, optional */\r
+ protected Datatype type;
+ /** File, optional */
protected File file;
- /** Accessor params */\r
- protected AccessorParams params;\r
+ /** Accessor params */
+ protected AccessorParams params;
BinaryObject(Accessor parent, Blob blob, Datatype type, AccessorParams params) {
this.parent = parent;
this.b = blob;
- this.type = type;\r
- this.params = params;\r
- \r
- if (parent!=null && parent instanceof BinaryObject) {\r
- file = ((BinaryObject)parent).file();\r
- } else {\r
- RandomAccessBinary sourceBinary = b.getSource();\r
- if (sourceBinary instanceof BinaryFile) {\r
- BinaryFile bf = (BinaryFile) sourceBinary;\r
- file = bf.file();\r
- }\r
- }\r
+ 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() {
} catch (IOException e) {
throw new AccessorException(e);
}
- }\r
- \r
- @Override\r
- public void reset() throws AccessorException {\r
- try {\r
- b.reset();\r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
- \r
- public RandomAccessBinary getSource() {\r
- RandomAccessBinary result = b;\r
- while (result instanceof Blob) result = ((Blob)result).getParent();\r
- return result;\r
}
-\r
- /**\r
- * Close the random access file beneath\r
+
+ @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();\r
+ writeLock();
try {
if (parent!=null) {
((FileAccessor) parent).close();
return;
}
- // Root Object\r
- if (b==null) return;\r
+ // Root Object
+ if (b==null) return;
RandomAccessBinary rab = getSource();
rab.flush();
- rab.close();\r
+ rab.close();
b = null;
} catch (IOException e) {
throw new AccessorException(e);
- } finally {\r
- writeUnlock();\r
+ } finally {
+ writeUnlock();
}
- }\r
- \r
- public boolean isOpen() {\r
- return b.isOpen();\r
+ }
+
+ public boolean isOpen() {
+ return b.isOpen();
}
/**
*
* @return file or <code>null</code>
*/
- public File file() {\r
+ public File file() {
return file;
}
public RandomAccessBinary getBinary() {
return b;
}
- \r
- @Override\r
- public AccessorParams getParams() {\r
- return params;\r
- }\r
+
+ @Override
+ public AccessorParams getParams() {
+ return params;
+ }
@Override
public void addListener(Listener listener, InterestSet interestSet, ChildReference path, Executor executor) throws AccessorException {
*/
@Override
public void setValue(Binding binding, Object newValue)
- throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
+ throws AccessorException {
+ assert b.isOpen();
+ writeLock();
try {
setValueNoflush(binding, newValue);
b.flush();
} catch (IOException e) {
throw new AccessorException(e);
- } finally {\r
- writeUnlock();\r
+ } finally {
+ writeUnlock();
}
}
* @throws AccessorException
*/
public abstract void setValueNoflush(Binding binding, Object newValue) throws AccessorException;
-\r
- public boolean setValue(ChildReference path, Binding binding, Object obj) throws AccessorException {\r
- try {\r
- Accessor a = getComponent(path);\r
- a.setValue(binding, obj);\r
- return true;\r
- } catch (ReferenceException re) {\r
- return false;\r
- } catch (AccessorConstructionException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
+
+ 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 {\r
- assert b.isOpen();\r
+ public Object getValue(Binding binding) throws AccessorException {
+ assert b.isOpen();
readLock();
try {
Serializer s = params.serializerScheme.getSerializer( binding );
return s.deserialize(b, null);
} catch (IOException e) {
throw new AccessorException(e);
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- readUnlock();\r
+ } 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);
}
}
- \r
- public void getValue(Binding binding, Object obj) throws AccessorException {\r
- assert b.isOpen();\r
- readLock();\r
- try {\r
- Serializer s = params.serializerScheme.getSerializer( binding );\r
- b.position(0L);\r
- s.deserializeTo(b, null, obj);\r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- readUnlock();\r
- }\r
- }\r
- \r
- @Override\r
- public boolean getValue(ChildReference path, Binding binding, Object obj) throws AccessorException {\r
- try {\r
- Accessor a = getComponent(path);\r
- a.getValue(binding, obj);\r
- return true;\r
- } catch (ReferenceException re) {\r
- return false;\r
- } catch (AccessorConstructionException e) {\r
- throw new AccessorException(e);\r
- }\r
- } \r
- \r
- public Object getValue(ChildReference path, Binding binding) throws AccessorException {\r
- try {\r
- Accessor a = getComponent(path);\r
- return a.getValue(binding);\r
- } catch (ReferenceException re) {\r
- return null;\r
- } catch (AccessorConstructionException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
- \r
- Object adapt(Object value, Binding domain, Binding range) throws AdaptException, AdapterConstructionException {\r
- return params.adapterScheme.getAdapter(domain, range, true, false).adapt(value);\r
- } \r
+
+ 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
@Override
public void apply(List<Event> cs, LinkedList<Event> rollback) throws AccessorException {
- assert b.isOpen();\r
- writeLock();\r
+ assert b.isOpen();
+ writeLock();
try {
boolean makeRollback = rollback != null;
for (Event e : cs) {
} catch (IOException e1) {
}
throw new AccessorException(ae);
- } finally {\r
- writeUnlock();\r
+ } finally {
+ writeUnlock();
}
}
Datatype type = type();
// Binding binding = params.bindingScheme.getBinding(type);
// Object instance = getValue(binding);
-// return "Accessor("+binding.printValueDefinition(instance, true)+")";\r
+// return "Accessor("+binding.printValueDefinition(instance, true)+")";
return this.getClass()+"("+type+")";
// } catch (AccessorException e) {
// return "Accessor(error="+e.getMessage()+")";
// return "Accessor(error="+e.getMessage()+")";
// } catch (IOException e) {
// return "Accessor(error="+e.getMessage()+")";
-// } catch (BindingConstructionException e) {\r
-// return "Accessor(error="+e.getMessage()+")";\r
+// } catch (BindingConstructionException e) {
+// return "Accessor(error="+e.getMessage()+")";
// }
}
return new BinaryMap(null, blob, (MapType)type, params);
}
if (type instanceof ArrayType) {
- return new BinaryArray(null, blob, (ArrayType)type, params);\r
+ return new BinaryArray(null, blob, (ArrayType)type, params);
}
- \r
+
throw new AccessorConstructionException("Can not create accessor to "+type);
} catch (IOException e) {
throw new AccessorConstructionException(e);
}
throw new AccessorConstructionException("Can not create accessor to "+type);
}
- \r
- protected void emitEvent(ListenerEntry le, Event e) { \r
- e.reference = ChildReference.concatenate(le.path, e.reference);\r
- le.emitEvent(e);\r
- } \r
-\r
- protected void emitEvents(ListenerEntry le, Collection<Event> events) {\r
- for (Event e : events)\r
- e.reference = ChildReference.concatenate(le.path, e.reference);\r
- le.emitEvents(events);\r
- } \r
- \r
- /**\r
- * Get lock if available. \r
- * \r
- * @return lock or <tt>null</tt>\r
- */\r
- public Lock getReadLock() {\r
- return params.readLock;\r
- }\r
- \r
- /**\r
- * Get lock if available. \r
- * \r
- * @return lock or <tt>null</tt>\r
- */\r
- public Lock getWriteLock() {\r
- return params.writeLock;\r
- }\r
- \r
-\r
- /**\r
- * Lock the lock if there is a lock.\r
- */\r
- protected void readLock() {\r
- if (params.readLock!=null) params.readLock.lock();\r
- }\r
- \r
- /**\r
- * Unlock the lock if one exists\r
- */\r
- protected void readUnlock() {\r
- if (params.readLock!=null) params.readLock.unlock();\r
- }\r
-\r
- /**\r
- * Lock the lock if there is a lock.\r
- */\r
- protected void writeLock() {\r
- if (params.writeLock!=null) params.writeLock.lock();\r
- }\r
- \r
- /**\r
- * Unlock the lock if one exists\r
- */\r
- protected void writeUnlock() {\r
- if (params.writeLock!=null) params.writeLock.unlock();\r
- }\r
- \r
+
+ protected void emitEvent(ListenerEntry le, Event e) {
+ e.reference = ChildReference.concatenate(le.path, e.reference);
+ le.emitEvent(e);
+ }
+
+ protected void emitEvents(ListenerEntry le, Collection<Event> events) {
+ for (Event e : events)
+ e.reference = ChildReference.concatenate(le.path, e.reference);
+ le.emitEvents(events);
+ }
+
+ /**
+ * Get lock if available.
+ *
+ * @return lock or <tt>null</tt>
+ */
+ public Lock getReadLock() {
+ return params.readLock;
+ }
+
+ /**
+ * Get lock if available.
+ *
+ * @return lock or <tt>null</tt>
+ */
+ 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();
+ }
+
}