Skip to content

Commit

Permalink
Added store reader/writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
achiyae committed Jan 7, 2021
1 parent 9b475a9 commit 54d2695
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 95 deletions.
122 changes: 30 additions & 92 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.bthink-bgu</groupId>
<artifactId>BPjs</artifactId>
<name>BPjs</name>
<version>0.11.0-SNAPSHOT</version>
<version>0.11.1-SNAPSHOT</version>
<description>Provides runtime and analysis for behavioral programs written in
JavaScript. It can run stand-alone (from the commmandline) or be
embedded in larger JVM-based systems.</description>
Expand Down Expand Up @@ -38,6 +38,33 @@
<target>11</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>${project.properties.exec.mainClass}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<source>8</source>
</configuration>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
Expand All @@ -63,24 +90,6 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>il.ac.bgu.cs.bp.bpjs.mains.BPJsCliRunner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<source>8</source>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
Expand Down Expand Up @@ -193,78 +202,6 @@
<version>1.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>selenium-chrome-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-htmlunit-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-firefox-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-ie-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-safari-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-support</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>webbit</artifactId>
<groupId>org.webbitserver</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>1.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>selenium-remote-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>operalaunchers</artifactId>
<groupId>com.opera</groupId>
</exclusion>
<exclusion>
<artifactId>protobuf-java</artifactId>
<groupId>com.google.protobuf</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>commons-jxpath</artifactId>
<groupId>commons-jxpath</groupId>
</exclusion>
<exclusion>
<artifactId>commons-exec</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
<exclusion>
<artifactId>ini4j</artifactId>
<groupId>org.ini4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<distributionManagement>
<repository>
Expand All @@ -278,9 +215,10 @@
</distributionManagement>
<properties>
<javax.activation.version>1.2.0</javax.activation.version>
<exec.mainClass>il.ac.bgu.cs.bp.bpjs.mains.BPJsCliRunner</exec.mainClass>
<exec.mainClass>il.ac.bgu.cs.bp.bpjs.mains.BPjsCliRunner</exec.mainClass>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jaxb.api.version>2.3.0</jaxb.api.version>
</properties>
</project>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public byte[] serialize(BProgramSyncSnapshot bpss) throws IOException {
ObjectOutputStream outs = new ObjectOutputStream(bytes)) {

outs.writeObject(new Header(bpss.getBThreadSnapshots().size(), bpss.getExternalEvents().size(), bpss.getViolationTag()));
outs.writeObject( bpss.getDataStore() );

writeStoreSnapshot(bpss.getDataStore(), outs, globalScope);

for (BThreadSyncSnapshot bss : bpss.getBThreadSnapshots()) {
writeBThreadSnapshot(bss, outs, globalScope);
Expand Down Expand Up @@ -117,7 +117,7 @@ public BProgramSyncSnapshot deserialize(byte[] bytes) throws IOException, ClassN
= new ScriptableInputStream(new ByteArrayInputStream(bytes), globalScope)) {
Header header = (Header) sis.readObject();

Map<String, Object> dataStore = (Map<String, Object>) sis.readObject();
Map<String, Object> dataStore = readStoreSnapshot(sis, globalScope);

Set<BThreadSyncSnapshot> bthreads = new HashSet<>(header.bthreadCount);

Expand Down Expand Up @@ -182,6 +182,37 @@ public BThreadSyncSnapshot deserializeBThread( byte[] serializedBT, Map<String,
}
}

private void writeStoreSnapshot(Map<String,Object> bprogramDataStore, ObjectOutputStream outs, ScriptableObject scope) throws IOException {
ByteArrayOutputStream storeBytes = new ByteArrayOutputStream();
try (BProgramSyncSnapshotOutputStream bpos = new BProgramSyncSnapshotOutputStream(storeBytes, scope)) {
bpos.writeObject(bprogramDataStore);
bpos.flush();
}
storeBytes.flush();
storeBytes.close();
outs.writeObject(storeBytes.toByteArray());
}

private Map<String,Object> readStoreSnapshot(ScriptableInputStream sis, ScriptableObject scope) throws IOException, ClassNotFoundException {

byte[] storeBytes = (byte[]) sis.readObject();

final BProgramJsProxy bpProxy = new BProgramJsProxy(bprogram);

StubProvider stubPrv = (StreamObjectStub stub) -> {
if (stub == StreamObjectStub.BP_PROXY) {
return bpProxy;
}
throw new IllegalArgumentException("Unknown stub " + stub);
};

try (ByteArrayInputStream inBytes = new ByteArrayInputStream(storeBytes);
BProgramSyncSnapshotInputStream bssis = new BProgramSyncSnapshotInputStream(inBytes, scope, stubPrv)) {
return (Map<String,Object>) bssis.readObject();
}

}

private void writeBThreadSnapshot(BThreadSyncSnapshot bss, ObjectOutputStream outs, ScriptableObject scope) throws IOException {
outs.writeObject(bss.getName());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* The MIT License
*
* Copyright 2017 michael.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package il.ac.bgu.cs.bp.bpjs.bprogramio;

import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.serialize.ScriptableInputStream;

import java.io.IOException;
import java.io.InputStream;

/**
*
* @author michael
*/
public class BProgramSyncSnapshotInputStream extends ScriptableInputStream {

private final StubProvider stubProvider;

public BProgramSyncSnapshotInputStream(InputStream in, Scriptable scope, StubProvider aProvider) throws IOException {
super(in, scope);
stubProvider = aProvider;
}

@Override
protected Object resolveObject(Object obj) throws IOException {
return ( obj instanceof StreamObjectStub )
? stubProvider.get((StreamObjectStub) obj)
: obj;
}

@Override
protected Object readObjectOverride() throws IOException, ClassNotFoundException {
return super.readObjectOverride();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* The MIT License
*
* Copyright 2017 michael.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package il.ac.bgu.cs.bp.bpjs.bprogramio;

import il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy;
import il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.serialize.ScriptableOutputStream;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* Output stream for {@link BThreadSyncSnapshot} objects. Creates stubs for objects that can link back to the java
* context, such as the {@code XJsProxy} classes.
*
* @author michael
*/
public class BProgramSyncSnapshotOutputStream extends ScriptableOutputStream {

private final List<StreamObjectStub> stubs = new ArrayList<>();

@SuppressWarnings("OverridableMethodCallInConstructor")
public BProgramSyncSnapshotOutputStream(OutputStream out, Scriptable scope) throws IOException {
super(out, scope);
}

@Override
protected Object replaceObject(Object obj) throws IOException {
if ( obj instanceof BProgramJsProxy ) {
stubs.add(StreamObjectStub.BP_PROXY);
return StreamObjectStub.BP_PROXY;
} else if ( ! (obj instanceof java.io.Serializable) ) {
System.err.println("Attempt to write a non-serializable object " + obj.toString());
return "NOT SERIALIZABLE: " + obj.toString();
} else {
return obj;
}
}

public List<StreamObjectStub> getStubs() {
return stubs;
}

}

0 comments on commit 54d2695

Please sign in to comment.