]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fixed various bugs in TG readers 93/1593/4
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 16 Mar 2018 14:55:52 +0000 (16:55 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 19 Mar 2018 10:00:04 +0000 (12:00 +0200)
Added primary install-location based relative path resolution to
database baselining logic. Secondary relative baseline resolution logic
still uses working directory.

refs #7825

Change-Id: I154528e674e6d478ea4bdb1de4d38c38d5097fdf

bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphFileReader.java
bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphs.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/ByteFileReader.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java
bundles/org.simantics.project/src/org/simantics/project/management/PlatformUtil.java
bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java
bundles/org.simantics/src/org/simantics/SimanticsPlatform.java

index 86c3d2fcbb18c51bae4e8bfefbdcd7928de57611..68a94e304581f71d55d1c57d9e3220a5da6bac3c 100644 (file)
@@ -135,9 +135,9 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
                Extensions extensions;
                int resourceCount;
                
                Extensions extensions;
                int resourceCount;
                
-               private int identities;
-               private int stmLength;
-               private int valueLength;
+               private int identities = -1;
+               private int stmLength = -1;
+               private int valueLength = -1;
                
                public FileTransferableGraphSource() throws Exception {
                    init();
                
                public FileTransferableGraphSource() throws Exception {
                    init();
@@ -176,33 +176,41 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
 
                @Override
                public int getIdentityCount() throws Exception {
 
                @Override
                public int getIdentityCount() throws Exception {
-                       identities = safeInt();
+                       if(identities == -1) {
+                               identities = safeInt();
+                       }
                        return identities;
                }
 
                @Override
                public int getStatementCount() throws Exception {
                        return identities;
                }
 
                @Override
                public int getStatementCount() throws Exception {
-                       stmLength = safeInt();
+                       if(stmLength == -1) {
+                               stmLength = safeInt();
+                       }
                        return stmLength;
                }
 
                @Override
                public int getValueCount() throws Exception {
                        return stmLength;
                }
 
                @Override
                public int getValueCount() throws Exception {
-                       valueLength = safeInt();
+                       if(valueLength == -1) {
+                               valueLength = safeInt();
+                       }
                        return valueLength;
                }
 
                @Override
                public void forStatements(ReadGraph graph, TransferableGraphSourceProcedure<int[]> procedure) throws Exception {
                        return valueLength;
                }
 
                @Override
                public void forStatements(ReadGraph graph, TransferableGraphSourceProcedure<int[]> procedure) throws Exception {
-                       
+
                        int[] value = new int[4];
 
                        int[] value = new int[4];
 
+                       int stmLength = getStatementCount();
+
                        for(int stmIndex=0;stmIndex<stmLength;) {
                        for(int stmIndex=0;stmIndex<stmLength;) {
-                               
+
                                value[stmIndex & 3] = safeInt();
                                stmIndex++;
                                if((stmIndex & 3) == 0) procedure.execute(value);
                                value[stmIndex & 3] = safeInt();
                                stmIndex++;
                                if((stmIndex & 3) == 0) procedure.execute(value);
-                               
+
                                // Cached bytes 
                                int avail = (SIZE-byteIndex) >> 2;
                                int allowed = Math.min(stmLength-stmIndex, avail);
                                // Cached bytes 
                                int avail = (SIZE-byteIndex) >> 2;
                                int allowed = Math.min(stmLength-stmIndex, avail);
@@ -213,94 +221,100 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
 //                                     statements[stmIndex++] = ((bytes[index++]&0xff)<<24) | ((bytes[index++]&0xff)<<16) | ((bytes[index++]&0xff)<<8) | ((bytes[index++]&0xff));                              
                                }
                                byteIndex += allowed<<2;
 //                                     statements[stmIndex++] = ((bytes[index++]&0xff)<<24) | ((bytes[index++]&0xff)<<16) | ((bytes[index++]&0xff)<<8) | ((bytes[index++]&0xff));                              
                                }
                                byteIndex += allowed<<2;
-                               
+
                        }
                        }
-                       
+
                }
 
                @Override
                public void forIdentities(ReadGraph graph, TransferableGraphSourceProcedure<Identity> procedure) throws Exception {
                }
 
                @Override
                public void forIdentities(ReadGraph graph, TransferableGraphSourceProcedure<Identity> procedure) throws Exception {
-                       
+
+                       int identities = getIdentityCount();
+
                        for(int i=0;i<identities;i++) {
                        for(int i=0;i<identities;i++) {
-                               
+
                                int rid = safeInt();
                                byte type = bytes[byteIndex++];
                                // External
                                if(type == 1) {
                                int rid = safeInt();
                                byte type = bytes[byteIndex++];
                                // External
                                if(type == 1) {
-                                       
+
                                        int parent = safeInt();
                                        int parent = safeInt();
-                                       int nameLen = bytes[byteIndex++]&0xff;
-                                       
+                                       int nameLen = getDynamicUInt32();
                                        if(byteIndex+nameLen < SIZE) {
                                                procedure.execute(new Identity(rid, new External(parent, utf(bytes, byteIndex, byteIndex + nameLen))));
                                                byteIndex += nameLen;
                                        } else {
                                                procedure.execute(new Identity(rid, new External(parent, utf(safeBytes(nameLen), 0, nameLen))));
                                        }
                                        if(byteIndex+nameLen < SIZE) {
                                                procedure.execute(new Identity(rid, new External(parent, utf(bytes, byteIndex, byteIndex + nameLen))));
                                                byteIndex += nameLen;
                                        } else {
                                                procedure.execute(new Identity(rid, new External(parent, utf(safeBytes(nameLen), 0, nameLen))));
                                        }
-                                       
                                } 
                                // Internal
                                else if(type == 3) {
                                } 
                                // Internal
                                else if(type == 3) {
-                                       
+
                                        int parent = safeInt();
                                        int parent = safeInt();
-                                       int nameLen = bytes[byteIndex++]&0xff;
+                                       int nameLen = getDynamicUInt32();
                                        if(byteIndex+nameLen < SIZE) {
                                                procedure.execute(new Identity(rid, new Internal(parent, utf(bytes, byteIndex, byteIndex + nameLen))));
                                                byteIndex += nameLen;
                                        } else {
                                                procedure.execute(new Identity(rid, new Internal(parent, utf(safeBytes(nameLen), 0, nameLen))));
                                        }
                                        if(byteIndex+nameLen < SIZE) {
                                                procedure.execute(new Identity(rid, new Internal(parent, utf(bytes, byteIndex, byteIndex + nameLen))));
                                                byteIndex += nameLen;
                                        } else {
                                                procedure.execute(new Identity(rid, new Internal(parent, utf(safeBytes(nameLen), 0, nameLen))));
                                        }
-                                       
+
                                }
                                // Root
                                else if(type == 0) {
                                }
                                // Root
                                else if(type == 0) {
-                                       
-                                       int nameLen = bytes[byteIndex++]&0xff;
+
+                                       int nameLen = getDynamicUInt32();
                                        String name = utf(safeBytes(nameLen), 0, nameLen);
                                        String name = utf(safeBytes(nameLen), 0, nameLen);
-                                       int nameLen2 = bytes[byteIndex++]&0xff;
+                                       int nameLen2 = getDynamicUInt32();
                                        String rType = utf(safeBytes(nameLen2), 0, nameLen2);
                                        procedure.execute(new Identity(rid, new Root(name, rType)));
 
                                        String rType = utf(safeBytes(nameLen2), 0, nameLen2);
                                        procedure.execute(new Identity(rid, new Root(name, rType)));
 
-                               } else if(type == 2) {
-                                       throw new UnsupportedOperationException();
+                               }
+                               // Optional
+                               else if(type == 2) {
+                                       throw new UnsupportedOperationException("Optional identities not supported");
+                               } else {
+                                       throw new IllegalStateException("Unsupported identity type " + type);
                                }
 
                        }
                                }
 
                        }
-                       
+
                }
 
                @Override
                public void forValues(ReadGraph graph, TransferableGraphSourceProcedure<Value> procedure) throws Exception {
                }
 
                @Override
                public void forValues(ReadGraph graph, TransferableGraphSourceProcedure<Value> procedure) throws Exception {
-                       
+
+                       int valueLength = getValueCount();
+
                        Serializer variantSerializer = Bindings.getSerializerUnchecked(Bindings.VARIANT);
                        Serializer variantSerializer = Bindings.getSerializerUnchecked(Bindings.VARIANT);
-                       
+
                        List<Object> idcontext = new ArrayList<>(); 
                        List<Object> idcontext = new ArrayList<>(); 
-                       
+
                        for(int i=0;i<valueLength;i++) {
                                int resource = safeInt();
                        for(int i=0;i<valueLength;i++) {
                                int resource = safeInt();
+                               idcontext.clear();
                                Variant value = (Variant)variantSerializer
                                Variant value = (Variant)variantSerializer
-                                       .deserialize((DataInput)dis, idcontext);
+                                               .deserialize((DataInput)dis, idcontext);
                                procedure.execute(new Value(resource, value));
                        }
                                procedure.execute(new Value(resource, value));
                        }
-                       
-                       
+
                }
                }
-               
+
                @Override
                public void forValues2(ReadGraph graph, TransferableGraphSourceValueProcedure procedure) throws Exception {
 
                    Binding datatypeBinding = Bindings.getBinding(Datatype.class);
             Serializer datatypeSerializer = Bindings.getSerializerUnchecked(datatypeBinding);
                @Override
                public void forValues2(ReadGraph graph, TransferableGraphSourceValueProcedure procedure) throws Exception {
 
                    Binding datatypeBinding = Bindings.getBinding(Datatype.class);
             Serializer datatypeSerializer = Bindings.getSerializerUnchecked(datatypeBinding);
-            
+
             List<Object> idContext = new ArrayList<>(); 
             List<Object> idContext = new ArrayList<>(); 
-            
+
             for(int i=0;i<valueLength;i++) {
                 int resource = safeInt();
                 idContext.clear();
                 Datatype type = (Datatype)datatypeSerializer.deserialize((DataInput)dis, idContext);
                 procedure.execute(resource, type, dis);
             }
             for(int i=0;i<valueLength;i++) {
                 int resource = safeInt();
                 idContext.clear();
                 Datatype type = (Datatype)datatypeSerializer.deserialize((DataInput)dis, idContext);
                 procedure.execute(resource, type, dis);
             }
-                   
+
                }
 
                @Override
                }
 
                @Override
@@ -316,7 +330,7 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
        public TransferableGraphSource readTG() throws Exception {
 
                if(getSize() == 0) return null;
        public TransferableGraphSource readTG() throws Exception {
 
                if(getSize() == 0) return null;
-               
+
                return new FileTransferableGraphSource();
 
        }
                return new FileTransferableGraphSource();
 
        }
index 0841e1455569ed08e0f184c750c5125e9fe55aa3..2b93df74dceeb0fed0b3f0c871a62fe043968d8a 100644 (file)
@@ -597,15 +597,14 @@ public class TransferableGraphs {
 
        public static TransferableGraph1 create(ReadGraph graph, TransferableGraphSource source) throws DatabaseException {
                
 
        public static TransferableGraph1 create(ReadGraph graph, TransferableGraphSource source) throws DatabaseException {
                
-               final TIntArrayList statements = new TIntArrayList();
-               final ArrayList<Value> values = new ArrayList<>();
-               final ArrayList<Identity> identities = new ArrayList<>();
-               
                try {
 
                try {
 
+                       ArrayList<Identity> identities = new ArrayList<>(source.getIdentityCount());
+                       source.forIdentities(graph, i -> identities.add(i));
+                       TIntArrayList statements = new TIntArrayList(source.getStatementCount());
                        source.forStatements(graph, r -> statements.addAll(r));
                        source.forStatements(graph, r -> statements.addAll(r));
+                       ArrayList<Value> values = new ArrayList<>(source.getValueCount());
                        source.forValues(graph, v -> values.add(v));
                        source.forValues(graph, v -> values.add(v));
-                       source.forIdentities(graph, i -> identities.add(i));
 
                        return new TransferableGraph1(source.getResourceCount(), 
                                        identities.toArray(new Identity[identities.size()]),
 
                        return new TransferableGraph1(source.getResourceCount(), 
                                        identities.toArray(new Identity[identities.size()]),
index 4c14bded6639fbf0662e6548f486d38dd27be912..e930a00fcad35b9c36bc323bd119c165f8f495dc 100644 (file)
@@ -69,9 +69,7 @@ public class ByteFileReader implements Closeable {
        }
 
        final protected byte[] safeBytes(int amount) throws IOException {
        }
 
        final protected byte[] safeBytes(int amount) throws IOException {
-
                byte[] result = new byte[amount];
                byte[] result = new byte[amount];
-               
                int has = size-byteIndex;
                if(amount >= has) {
                        ReadableByteChannel c = channel;
                int has = size-byteIndex;
                if(amount >= has) {
                        ReadableByteChannel c = channel;
@@ -86,6 +84,9 @@ public class ByteFileReader implements Closeable {
                                int got = c.read(bb2);
                                if(got == -1) throw new IOException("Unexpected end-of-file");
                                has += got; 
                                int got = c.read(bb2);
                                if(got == -1) throw new IOException("Unexpected end-of-file");
                                has += got; 
+                               // For some unknown reason this is needed!
+                               // Spec indicates that read would increment position but it does not.
+                               bb2.position(has);
                        }
                        size = c.read(bb);
                        bb.position(0);
                        }
                        size = c.read(bb);
                        bb.position(0);
@@ -104,7 +105,7 @@ public class ByteFileReader implements Closeable {
            int result;
         if(has == 0) {
             ReadableByteChannel c = channel;
            int result;
         if(has == 0) {
             ReadableByteChannel c = channel;
-            ByteBuffer bb = byteBuffer;            
+            ByteBuffer bb = byteBuffer;
             size = c.read(bb);
             if(size == -1) {
                                throw new EOFException("Unexpected end-of-file");
             size = c.read(bb);
             if(size == -1) {
                                throw new EOFException("Unexpected end-of-file");
@@ -114,43 +115,40 @@ public class ByteFileReader implements Closeable {
             if(size == 0)
                 return -1;
         }
             if(size == 0)
                 return -1;
         }
-        result = bytes[byteIndex];
-        if(result < 0)
-            result += 256;
-        ++byteIndex;        
+        result = bytes[byteIndex++] & 0xff;
         return result;
        }
 
        public int getDynamicUInt32() throws IOException {
         return result;
        }
 
        public int getDynamicUInt32() throws IOException {
-               int length = getByte()&0xff
+               int length = getByte(); 
                if(length >= 0x80) {
                        if(length >= 0xc0) {
                                if(length >= 0xe0) {
                                        if(length >= 0xf0) {
                                                length &= 0x0f;
                if(length >= 0x80) {
                        if(length >= 0xc0) {
                                if(length >= 0xe0) {
                                        if(length >= 0xf0) {
                                                length &= 0x0f;
-                                               length += ((getByte()&0xff)<<3);
-                                               length += ((getByte()&0xff)<<11);
-                                               length += ((getByte()&0xff)<<19);
+                                               length += (getByte()<<3);
+                                               length += (getByte()<<11);
+                                               length += (getByte()<<19);
                                                length += 0x10204080;
                                        }
                                        else {
                                                length &= 0x1f;
                                                length += 0x10204080;
                                        }
                                        else {
                                                length &= 0x1f;
-                                               length += ((getByte()&0xff)<<4);
-                                               length += ((getByte()&0xff)<<12);
-                                               length += ((getByte()&0xff)<<20);
+                                               length += (getByte()<<4);
+                                               length += (getByte()<<12);
+                                               length += (getByte()<<20);
                                                length += 0x204080;
                                        }
                                }
                                else {
                                        length &= 0x3f;
                                                length += 0x204080;
                                        }
                                }
                                else {
                                        length &= 0x3f;
-                                       length += ((getByte()&0xff)<<5);
-                                       length += ((getByte()&0xff)<<13);
+                                       length += (getByte()<<5);
+                                       length += (getByte()<<13);
                                        length += 0x4080;
                                }
                        }
                        else {
                                length &= 0x7f;
                                        length += 0x4080;
                                }
                        }
                        else {
                                length &= 0x7f;
-                               length += ((getByte()&0xff)<<6);
+                               length += (getByte()<<6);
                                length += 0x80;
                        }
                }
                                length += 0x80;
                        }
                }
@@ -159,10 +157,12 @@ public class ByteFileReader implements Closeable {
 
        final protected int safeInt() throws IOException {
 
 
        final protected int safeInt() throws IOException {
 
+               byte[] bytes = this.bytes;
+
                if(byteIndex >= (size-5)) {
                        int result = 0;
                        ReadableByteChannel c = channel;
                if(byteIndex >= (size-5)) {
                        int result = 0;
                        ReadableByteChannel c = channel;
-               ByteBuffer bb = byteBuffer;
+                       ByteBuffer bb = byteBuffer;
                        if(byteIndex == size) {
                                size = c.read(bb);
                                if(size == -1) throw new EOFException("Unexpected end-of-file");
                        if(byteIndex == size) {
                                size = c.read(bb);
                                if(size == -1) throw new EOFException("Unexpected end-of-file");
@@ -190,7 +190,7 @@ public class ByteFileReader implements Closeable {
                                bb.position(0);
                                byteIndex = 0;
                        }
                                bb.position(0);
                                byteIndex = 0;
                        }
-                       result |= ((int)(bytes[byteIndex++]&0xff)<<0);
+                       result |= ((int)(bytes[byteIndex++]&0xff));
                        if(byteIndex == size) {
                                size = c.read(bb);
                                bb.position(0);
                        if(byteIndex == size) {
                                size = c.read(bb);
                                bb.position(0);
@@ -200,9 +200,9 @@ public class ByteFileReader implements Closeable {
                } else {
                        return ((bytes[byteIndex++]&0xff)<<24) | ((bytes[byteIndex++]&0xff)<<16) | ((bytes[byteIndex++]&0xff)<<8) | ((bytes[byteIndex++]&0xff));
                }
                } else {
                        return ((bytes[byteIndex++]&0xff)<<24) | ((bytes[byteIndex++]&0xff)<<16) | ((bytes[byteIndex++]&0xff)<<8) | ((bytes[byteIndex++]&0xff));
                }
-               
+
        }
        }
-       
+
        final protected int getSize() {
                return size;
        }
        final protected int getSize() {
                return size;
        }
index ae1ec711ce676e9d3932cbbbc10485601cbb2f1d..077eb6617a1703f3cbffa913633e2b4c738f95ab 100644 (file)
@@ -161,7 +161,7 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 
                int resourceCount = safeInt();
                
 
                int resourceCount = safeInt();
                
-               List<Object> idcontext = new ArrayList<Object>(); 
+               List<Object> idcontext = new ArrayList<>(); 
                dis = new DataInputStream(in);
                Extensions extensions = (Extensions)Bindings.getSerializerUnchecked(Extensions.class).deserialize((DataInput)dis, idcontext);
                
                dis = new DataInputStream(in);
                Extensions extensions = (Extensions)Bindings.getSerializerUnchecked(Extensions.class).deserialize((DataInput)dis, idcontext);
                
@@ -174,27 +174,27 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 //             long duration = System.nanoTime() - start;
 //             LOGGER.warn("start in " + 1e-9*duration + "s.");
 //             start = System.nanoTime();
 //             long duration = System.nanoTime() - start;
 //             LOGGER.warn("start in " + 1e-9*duration + "s.");
 //             start = System.nanoTime();
-               
+
                for(int i=0;i<identities;i++) {
                        int rid = safeInt();
                        byte type = bytes[byteIndex++];
                        // External
                        if(type == 1) {
                for(int i=0;i<identities;i++) {
                        int rid = safeInt();
                        byte type = bytes[byteIndex++];
                        // External
                        if(type == 1) {
-                               
+
                                int parent = safeInt();
                                int nameLen = getDynamicUInt32();
                                int parent = safeInt();
                                int nameLen = getDynamicUInt32();
-                               
+
                                if(byteIndex+nameLen < SIZE) {
                                        ids[i] = new Identity(rid, new External(parent, utf(bytes, byteIndex, byteIndex + nameLen)));
                                        byteIndex += nameLen;
                                } else {
                                        ids[i] = new Identity(rid, new External(parent, utf(safeBytes(nameLen), 0, nameLen)));
                                }
                                if(byteIndex+nameLen < SIZE) {
                                        ids[i] = new Identity(rid, new External(parent, utf(bytes, byteIndex, byteIndex + nameLen)));
                                        byteIndex += nameLen;
                                } else {
                                        ids[i] = new Identity(rid, new External(parent, utf(safeBytes(nameLen), 0, nameLen)));
                                }
-                               
+
                        } 
                        // Internal
                        else if(type == 3) {
                        } 
                        // Internal
                        else if(type == 3) {
-                               
+
                                int parent = safeInt();
                                int nameLen = getDynamicUInt32();
                                if(byteIndex+nameLen < SIZE) {
                                int parent = safeInt();
                                int nameLen = getDynamicUInt32();
                                if(byteIndex+nameLen < SIZE) {
@@ -215,6 +215,8 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 
                        } else if(type == 2) {
                                throw new UnsupportedOperationException();
 
                        } else if(type == 2) {
                                throw new UnsupportedOperationException();
+                       } else {
+                               throw new IllegalStateException();
                        }
 
                }
                        }
 
                }
@@ -257,11 +259,11 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 
                Serializer variantSerializer = Bindings.getSerializerUnchecked(Bindings.VARIANT);
                
 
                Serializer variantSerializer = Bindings.getSerializerUnchecked(Bindings.VARIANT);
                
-               idcontext = new ArrayList<Object>(); 
                dis = new DataInputStream(in);
                
                for(int i=0;i<valueLength;i++) {
                        int resource = safeInt();
                dis = new DataInputStream(in);
                
                for(int i=0;i<valueLength;i++) {
                        int resource = safeInt();
+                       idcontext.clear();
                        Variant value = (Variant)variantSerializer
                                .deserialize((DataInput)dis, idcontext);
                        values[i] = new Value(resource, value);
                        Variant value = (Variant)variantSerializer
                                .deserialize((DataInput)dis, idcontext);
                        values[i] = new Value(resource, value);
index 9dfa67efef7be394081c583f325b09ed63dfea4e..a8916251e07c738413866bde501a42b3e7c3eec6 100644 (file)
@@ -392,8 +392,10 @@ public class PlatformUtil {
        private static GraphBundleEx tryGetOnDemandGraph(Bundle bundle, URL url) throws IOException {
                try {
                        Integer cachedHash = readCachedHash(url);
        private static GraphBundleEx tryGetOnDemandGraph(Bundle bundle, URL url) throws IOException {
                try {
                        Integer cachedHash = readCachedHash(url);
-                       if (cachedHash == null)
+                       if (cachedHash == null) {
+                               LOGGER.info("No cached hash for " + bundle);
                                return null;
                                return null;
+                       }
 
                        Supplier<TransferableGraph1> graphSource = () -> {
                                try {
 
                        Supplier<TransferableGraph1> graphSource = () -> {
                                try {
@@ -443,7 +445,7 @@ public class PlatformUtil {
                // For an unknown reason this is totally broken when running the TestSCLOsgi
                // in the SDK Tycho build. It returns incomplete results because the
                // ReadableByteChannel used by ByteFileReader starts returning 0 unexpectedly.
                // For an unknown reason this is totally broken when running the TestSCLOsgi
                // in the SDK Tycho build. It returns incomplete results because the
                // ReadableByteChannel used by ByteFileReader starts returning 0 unexpectedly.
-//                             try (TransferableGraphFileReader reader = new TransferableGraphFileReader(is)) {
+//             try (TransferableGraphFileReader reader = new TransferableGraphFileReader(is)) {
 //                     return reader.readTG();
 //             }
                return DataContainers.readFile(new DataInputStream(is), handlers);
 //                     return reader.readTG();
 //             }
                return DataContainers.readFile(new DataInputStream(is), handlers);
index d0b8e69492ff44c274077fffcc7940f646029dc5..a3b409027a0c314354a3e172eac62163094b8be0 100644 (file)
@@ -61,12 +61,7 @@ public class BaselineCreatorApplication implements IApplication {
 
        private static Path getInstanceLocation() throws CoreException, IOException {
                Location l = Platform.getInstanceLocation();
 
        private static Path getInstanceLocation() throws CoreException, IOException {
                Location l = Platform.getInstanceLocation();
-               if (l == null)
-                       throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
-                                       "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
-
-               Location instanceLoc = Platform.getInstanceLocation();
-               if (instanceLoc == null || instanceLoc.isReadOnly())
+               if (l == null || l.isReadOnly())
                        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                        "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
 
                        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                        "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
 
index 3638d3407df271caa1869b2e9d97cc266e3b535e..ad3b86d9c989814c64fbb1783c5a8982fe60940c 100644 (file)
@@ -41,6 +41,7 @@ import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubMonitor;
+import org.eclipse.osgi.service.datalocation.Location;
 import org.eclipse.osgi.service.resolver.BundleDescription;
 import org.ini4j.Ini;
 import org.ini4j.InvalidFileFormatException;
 import org.eclipse.osgi.service.resolver.BundleDescription;
 import org.ini4j.Ini;
 import org.ini4j.InvalidFileFormatException;
@@ -696,7 +697,42 @@ public class SimanticsPlatform implements LifecycleListener {
         resetDatabase(monitor);
     }
 
         resetDatabase(monitor);
     }
 
-    public boolean handleBaselineDatabase() throws PlatformException {
+    private static Path tryGetInstallLocation() {
+        Location l = Platform.getInstallLocation();
+        return l == null ? null : new File(l.getURL().getPath()).toPath();
+    }
+
+    private Path resolveBaselineFile() throws PlatformException {
+        String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
+        if (dbBaselineArchive == null)
+            return null;
+
+        Path baseline = Paths.get(dbBaselineArchive);
+        if (baseline.isAbsolute()) {
+            if (!Files.isRegularFile(baseline))
+                throw new PlatformException("Specified database baseline archive " + baseline
+                        + " does not exist. Cannot initialize workspace database from baseline.");
+            return baseline;
+        }
+
+        // Relative path resolution order:
+        // 1. from the platform "install location"
+        // 2. from working directory
+        Path installLocation = tryGetInstallLocation();
+        if (installLocation != null) {
+            Path installedBaseline = installLocation.resolve(dbBaselineArchive);
+            if (Files.isRegularFile(installedBaseline))
+                return installedBaseline;
+        }
+        if (!Files.isRegularFile(baseline))
+            throw new PlatformException("Specified database baseline archive " + baseline
+                    + " does not exist in either the install location (" + installLocation
+                    + ") or the working directory (" + Paths.get(".").toAbsolutePath()
+                    + "). Cannot initialize workspace database.");
+        return null;
+    }
+
+    private boolean handleBaselineDatabase() throws PlatformException {
         Path workspaceLocation = Platform.getLocation().toFile().toPath();
         Path baselineIndicatorFile = workspaceLocation.resolve(".baselined");
         if (Files.isRegularFile(baselineIndicatorFile)) {
         Path workspaceLocation = Platform.getLocation().toFile().toPath();
         Path baselineIndicatorFile = workspaceLocation.resolve(".baselined");
         if (Files.isRegularFile(baselineIndicatorFile)) {
@@ -705,14 +741,10 @@ public class SimanticsPlatform implements LifecycleListener {
             return true;
         }
 
             return true;
         }
 
-        String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
-        if (dbBaselineArchive == null)
+        Path baseline = resolveBaselineFile();
+        if (baseline == null)
             return false;
 
             return false;
 
-        Path baseline = Paths.get(dbBaselineArchive);
-        if (!Files.isRegularFile(baseline))
-            throw new PlatformException("Specified database baseline archive " + baseline + " does not exist. Cannot initialize workspace database.");
-
         DatabaseBaselines.validateBaselineFile(baseline);
         DatabaseBaselines.validateWorkspaceForBaselineInitialization(workspaceLocation);
         DatabaseBaselines.initializeWorkspaceWithBaseline(baseline, workspaceLocation, baselineIndicatorFile);
         DatabaseBaselines.validateBaselineFile(baseline);
         DatabaseBaselines.validateWorkspaceForBaselineInitialization(workspaceLocation);
         DatabaseBaselines.initializeWorkspaceWithBaseline(baseline, workspaceLocation, baselineIndicatorFile);