Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlyn Normington2011-03-18 12:23:19 -0400
committerGlyn Normington2011-03-18 12:23:19 -0400
commit227721b4c1fb51e57cdc1cd2be90d16fa9796624 (patch)
treec21ab9449d9f3d95c531170ba7f2ce7d53dbd3d8
parent4b94e214e148f59106d35c64adf015958cf378e5 (diff)
downloadorg.eclipse.virgo.kernel-227721b4c1fb51e57cdc1cd2be90d16fa9796624.tar.gz
org.eclipse.virgo.kernel-227721b4c1fb51e57cdc1cd2be90d16fa9796624.tar.xz
org.eclipse.virgo.kernel-227721b4c1fb51e57cdc1cd2be90d16fa9796624.zip
bug 336941: add persistent name and version to the persistent form of a digraph and validate these on input
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraphPersistence.java3
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPersistence.java22
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPeristenceTests.java37
3 files changed, 56 insertions, 6 deletions
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraphPersistence.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraphPersistence.java
index 4029f70f..d9cd7889 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraphPersistence.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraphPersistence.java
@@ -34,8 +34,9 @@ public interface RegionDigraphPersistence {
* The specified stream remains open after this method returns.
*
* @param input an input stream to read a digraph from.
- * @return
+ * @return the new digraph
* @throws IOException if error occurs reading the digraph.
+ * @throws IllegalArgumentException if the input stream is not a digraph or has an incompatible persistent version
*/
RegionDigraph load(InputStream input) throws IOException;
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPersistence.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPersistence.java
index 810cb0a9..057f39a6 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPersistence.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPersistence.java
@@ -39,6 +39,10 @@ import org.osgi.framework.InvalidSyntaxException;
*/
final class StandardRegionDigraphPersistence implements RegionDigraphPersistence {
+ private static final String PERSISTENT_NAME = "virgo region digraph";
+
+ private static final int PERSISTENT_VERSION = 1;
+
static void writeRegionDigraph(DataOutputStream out, RegionDigraph digraph) throws IOException {
if (!(digraph instanceof StandardRegionDigraph))
throw new IllegalArgumentException("Only digraphs of type '" + StandardRegionDigraph.class.getName() + "' are allowed: "
@@ -46,6 +50,9 @@ final class StandardRegionDigraphPersistence implements RegionDigraphPersistence
Map<Region, Set<FilteredRegion>> filteredRegions = ((StandardRegionDigraph) digraph).getFilteredRegions();
try {
+ // write the persistent name and version
+ out.writeUTF(PERSISTENT_NAME);
+ out.writeInt(PERSISTENT_VERSION);
// write the number of regions
out.writeInt(filteredRegions.size());
// write each region
@@ -107,6 +114,15 @@ final class StandardRegionDigraphPersistence implements RegionDigraphPersistence
static RegionDigraph readRegionDigraph(DataInputStream in) throws IOException, InvalidSyntaxException, BundleException {
RegionDigraph digraph = new StandardRegionDigraph();
+ // Read and check the persistent name and version
+ String persistentName = in.readUTF();
+ if (!PERSISTENT_NAME.equals(persistentName)) {
+ throw new IllegalArgumentException("Input stream does not represent a digraph");
+ }
+ int persistentVersion = in.readInt();
+ if (PERSISTENT_VERSION != persistentVersion) {
+ throw new IllegalArgumentException("Input stream contains a digraph with an incompatible version '" + persistentVersion + "'");
+ }
// read the number of regions
int numRegions = in.readInt();
for (int i = 0; i < numRegions; i++) {
@@ -168,6 +184,9 @@ final class StandardRegionDigraphPersistence implements RegionDigraphPersistence
digraph.connect(tail, builder.build(), head);
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public RegionDigraph load(InputStream input) throws IOException {
try {
@@ -183,6 +202,9 @@ final class StandardRegionDigraphPersistence implements RegionDigraphPersistence
}
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public void save(RegionDigraph digraph, OutputStream output) throws IOException {
writeRegionDigraph(new DataOutputStream(output), digraph);
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPeristenceTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPeristenceTests.java
index 8e451607..e42b8c27 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPeristenceTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphPeristenceTests.java
@@ -163,6 +163,37 @@ public class StandardRegionDigraphPeristenceTests {
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidPersistentName() throws IOException, InvalidSyntaxException, BundleException {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(output);
+ dataOut.writeUTF("test");
+ dataOut.close();
+ byte[] byteArray = output.toByteArray();
+ readDigraph(byteArray);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidPersistentVersion() throws IOException, InvalidSyntaxException, BundleException {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(output);
+ dataOut.writeUTF("virgo region digraph");
+ dataOut.writeInt(-1);
+ dataOut.close();
+ byte[] byteArray = output.toByteArray();
+ readDigraph(byteArray);
+ }
+
+ private void readDigraph(byte[] byteArray) throws IOException, InvalidSyntaxException, BundleException {
+ ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
+ DataInputStream dataIn = new DataInputStream(input);
+ try {
+ StandardRegionDigraphPersistence.readRegionDigraph(dataIn);
+ } finally {
+ dataIn.close();
+ }
+ }
+
private void doTest() throws IOException, InvalidSyntaxException, BundleException {
// test a single write
doTest(1);
@@ -221,11 +252,7 @@ public class StandardRegionDigraphPeristenceTests {
}
static int countRegions(RegionDigraph digraph) {
- int result = 0;
- for (Region region : digraph) {
- result++;
- }
- return result;
+ return digraph.getRegions().size();
}
static void assertEquals(Region r1, Region r2) {

Back to the top