Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlyn Normington2012-07-25 07:17:50 -0400
committerGlyn Normington2012-07-25 07:17:50 -0400
commit540ab7da158178d976a33821fcd7e8bbc3ea1f47 (patch)
tree0f4b2b1283a7d9bd19d48ba01fd572b2c0e76ae8
parenta78041bd2ae16d1daab9678259044d74d07eb842 (diff)
downloadorg.eclipse.virgo.kernel-540ab7da158178d976a33821fcd7e8bbc3ea1f47.tar.gz
org.eclipse.virgo.kernel-540ab7da158178d976a33821fcd7e8bbc3ea1f47.tar.xz
org.eclipse.virgo.kernel-540ab7da158178d976a33821fcd7e8bbc3ea1f47.zip
385743: rename to PathGenerator and tidy up file/directory management
-rw-r--r--org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathGenerator.java70
-rw-r--r--org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathHistory.java56
-rw-r--r--org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/StandardArtifactStorage.java36
-rw-r--r--org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/artifact/internal/PathGeneratorTests.java (renamed from org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/artifact/internal/PathHistoryTests.java)63
-rw-r--r--org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/pipeline/stage/transform/internal/SyntheticContextBundleCreatingTransformerTests.java6
5 files changed, 132 insertions, 99 deletions
diff --git a/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathGenerator.java b/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathGenerator.java
new file mode 100644
index 00000000..2ba78490
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathGenerator.java
@@ -0,0 +1,70 @@
+
+package org.eclipse.virgo.kernel.install.artifact.internal;
+
+import org.eclipse.virgo.util.io.PathReference;
+
+final class PathGenerator {
+
+ private final PathReference baseDirectory;
+
+ private long generation = 0;
+
+ private boolean hasPrevious = false; // if true, then generation != 0
+
+ private final Object monitor = new Object();
+
+ private final String baseName;
+
+ PathGenerator(PathReference basePathReference) {
+ if (basePathReference == null) {
+ throw new IllegalArgumentException("Null path");
+ }
+ if ("".equals(basePathReference.getName())) {
+ throw new IllegalArgumentException("Empty filename");
+ }
+ this.baseDirectory = basePathReference.getParent();
+ this.baseName = basePathReference.getName();
+ }
+
+ PathReference getCurrentPath() {
+ synchronized (this.monitor) {
+ return getGenerationPath(this.generation);
+ }
+ }
+
+ /**
+ * Note that the history is only one level deep.
+ */
+ void next() {
+ synchronized (this.monitor) {
+ if (this.generation != 0) {
+ getPreviousPath().delete(true);
+ }
+ this.generation++;
+ this.hasPrevious = true;
+ }
+ }
+
+ /**
+ * This may only be called if the current generation has not been used.
+ */
+ void previous() {
+ synchronized (this.monitor) {
+ if (!this.hasPrevious) {
+ throw new IllegalStateException("No stash available");
+ }
+ getCurrentPath().delete(true);
+ this.generation--;
+ this.hasPrevious = false;
+ }
+ }
+
+ private PathReference getPreviousPath() {
+ return getGenerationPath(this.generation - 1);
+ }
+
+ private PathReference getGenerationPath(long instance) {
+ return this.baseDirectory.newChild(Long.toString(instance)).newChild(this.baseName);
+ }
+
+}
diff --git a/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathHistory.java b/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathHistory.java
deleted file mode 100644
index 54d8b1fe..00000000
--- a/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/PathHistory.java
+++ /dev/null
@@ -1,56 +0,0 @@
-
-package org.eclipse.virgo.kernel.install.artifact.internal;
-
-import org.eclipse.virgo.util.io.PathReference;
-
-final class PathHistory {
-
- private final PathReference baseStagingPathReference;
-
- private long instance = 0;
-
- private boolean stashed = false;
-
- private final Object monitor = new Object();
-
- PathHistory(PathReference baseStagingPathReference) {
- this.baseStagingPathReference = baseStagingPathReference;
- }
-
- PathReference getCurrentPath() {
- synchronized (this.monitor) {
- return getInstancePathReference(this.baseStagingPathReference, this.instance);
- }
- }
-
- /**
- * Note that the stash is only one level deep.
- */
- void stash() {
- synchronized (this.monitor) {
- getPreviousPathReference().delete(true);
- this.instance++;
- this.stashed = true;
- }
- }
-
- void unstash() {
- synchronized (this.monitor) {
- if (!this.stashed) {
- throw new IllegalStateException("No stash available");
- }
- getCurrentPath().delete(true);
- this.instance--;
- this.stashed = false;
- }
- }
-
- private PathReference getPreviousPathReference() {
- return getInstancePathReference(this.baseStagingPathReference, this.instance - 1);
- }
-
- private static PathReference getInstancePathReference(PathReference baseStagingPathReference, long instance) {
- return new PathReference(String.format("%s-%d", baseStagingPathReference.getAbsolutePath(), instance));
- }
-
-}
diff --git a/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/StandardArtifactStorage.java b/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/StandardArtifactStorage.java
index 7c3801ef..d8c8d7ac 100644
--- a/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/StandardArtifactStorage.java
+++ b/org.eclipse.virgo.kernel.deployer/src/main/java/org/eclipse/virgo/kernel/install/artifact/internal/StandardArtifactStorage.java
@@ -40,8 +40,8 @@ final class StandardArtifactStorage implements ArtifactStorage {
private final EventLogger eventLogger;
private final boolean unpackBundles;
-
- private final PathHistory artifactHistory;
+
+ private final PathGenerator pathGenerator;
public StandardArtifactStorage(PathReference sourcePathReference, PathReference baseStagingPathReference, ArtifactFSFactory artifactFSFactory,
EventLogger eventLogger, String unpackBundlesOption) {
@@ -51,44 +51,47 @@ final class StandardArtifactStorage implements ArtifactStorage {
this.eventLogger = eventLogger;
- this.unpackBundles = (unpackBundlesOption == null) || DEPLOYER_UNPACK_BUNDLES_TRUE.equalsIgnoreCase(unpackBundlesOption);
-
- artifactHistory = new PathHistory(baseStagingPathReference);
+ this.unpackBundles = unpackBundlesOption == null || DEPLOYER_UNPACK_BUNDLES_TRUE.equalsIgnoreCase(unpackBundlesOption);
+
+ this.pathGenerator = new PathGenerator(baseStagingPathReference);
synchronize(this.sourcePathReference, false);
}
-
+ @Override
public void synchronize() {
synchronize(this.sourcePathReference, true);
}
+ @Override
public ArtifactFS getArtifactFS() {
- return this.artifactFSFactory.create(this.artifactHistory.getCurrentPath().toFile());
+ return this.artifactFSFactory.create(this.pathGenerator.getCurrentPath().toFile());
}
+ @Override
public void synchronize(URI sourceUri) {
synchronize(new PathReference(sourceUri), true);
}
+ @Override
public void rollBack() {
- this.artifactHistory.unstash();
+ this.pathGenerator.previous();
}
+ @Override
public void delete() {
- PathReference currentPathReference = this.artifactHistory.getCurrentPath();
+ PathReference currentPathReference = this.pathGenerator.getCurrentPath();
currentPathReference.delete(true);
}
private void synchronize(PathReference normalizedSourcePathReference, boolean stash) {
PathReference currentPathReference;
if (stash) {
- this.artifactHistory.stash();
- currentPathReference = this.artifactHistory.getCurrentPath();
- } else {
- currentPathReference = this.artifactHistory.getCurrentPath();
- currentPathReference.delete(true);
- }
+ this.pathGenerator.next();
+ }
+ currentPathReference = this.pathGenerator.getCurrentPath();
+ currentPathReference.getParent().createDirectory();
+ currentPathReference.delete(true);
if (normalizedSourcePathReference != null && !normalizedSourcePathReference.isDirectory()
&& needsUnpacking(normalizedSourcePathReference.getName())) {
@@ -99,7 +102,6 @@ final class StandardArtifactStorage implements ArtifactStorage {
throw new RuntimeException(String.format("Exception unpacking '%s'", normalizedSourcePathReference), e);
}
} else if (normalizedSourcePathReference != null) {
- currentPathReference.getParent().createDirectory();
normalizedSourcePathReference.copy(currentPathReference, true);
} else {
currentPathReference.createDirectory();
@@ -116,7 +118,7 @@ final class StandardArtifactStorage implements ArtifactStorage {
String fileExtension = fileName.substring(dotLocation + 1);
// Always unpack .par/.zip. Unpack .jar/.war if and only if kernel property deployer.unpackBundles is either not
// specified or is "true"
- return ALWAYS_UNPACKED_EXTENSIONS.contains(fileExtension) || (this.unpackBundles && CONFIGURABLY_UNPACKED_EXTENSIONS.contains(fileExtension));
+ return ALWAYS_UNPACKED_EXTENSIONS.contains(fileExtension) || this.unpackBundles && CONFIGURABLY_UNPACKED_EXTENSIONS.contains(fileExtension);
}
}
diff --git a/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/artifact/internal/PathHistoryTests.java b/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/artifact/internal/PathGeneratorTests.java
index 59de47d4..0f28d88a 100644
--- a/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/artifact/internal/PathHistoryTests.java
+++ b/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/artifact/internal/PathGeneratorTests.java
@@ -11,6 +11,7 @@
package org.eclipse.virgo.kernel.install.artifact.internal;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -20,17 +21,17 @@ import org.eclipse.virgo.util.io.PathReference;
import org.junit.Before;
import org.junit.Test;
-public class PathHistoryTests {
+public class PathGeneratorTests {
- private static final String TEST_PATH = "target/artifactHistoryTests/";
+ private static final String TEST_PATH = "target/pathGeneratorTests/";
private static final String TEST_FILENAME = "some.jar";
- private PathHistory artifactHistory;
+ private PathGenerator artifactHistory;
@Before
public void setUp() throws Exception {
- this.artifactHistory = new PathHistory(new PathReference(TEST_PATH + TEST_FILENAME));
+ this.artifactHistory = new PathGenerator(new PathReference(TEST_PATH + TEST_FILENAME));
}
@Test
@@ -42,7 +43,7 @@ public class PathHistoryTests {
@Test
public void testStash() {
PathReference original = this.artifactHistory.getCurrentPath();
- this.artifactHistory.stash();
+ this.artifactHistory.next();
PathReference c = this.artifactHistory.getCurrentPath();
checkPath(c);
assertFalse(original.equals(c));
@@ -51,8 +52,8 @@ public class PathHistoryTests {
@Test
public void testUnstash() {
PathReference original = this.artifactHistory.getCurrentPath();
- this.artifactHistory.stash();
- this.artifactHistory.unstash();
+ this.artifactHistory.next();
+ this.artifactHistory.previous();
PathReference c = this.artifactHistory.getCurrentPath();
checkPath(c);
assertTrue(original.equals(c));
@@ -62,9 +63,9 @@ public class PathHistoryTests {
@Test
public void testRepeatedStash() {
PathReference original = this.artifactHistory.getCurrentPath();
- this.artifactHistory.stash();
+ this.artifactHistory.next();
PathReference next = this.artifactHistory.getCurrentPath();
- this.artifactHistory.stash();
+ this.artifactHistory.next();
PathReference last = this.artifactHistory.getCurrentPath();
checkPath(last);
assertFalse(original.equals(next));
@@ -73,21 +74,21 @@ public class PathHistoryTests {
@Test
public void testFileDeletionOnUnstash() {
- this.artifactHistory.stash();
+ this.artifactHistory.next();
PathReference c = this.artifactHistory.getCurrentPath();
c.createFile();
assertTrue(c.exists());
- this.artifactHistory.unstash();
+ this.artifactHistory.previous();
assertFalse(c.exists());
}
@Test
public void testDirectoryDeletionOnUnstash() {
- this.artifactHistory.stash();
+ this.artifactHistory.next();
PathReference c = this.artifactHistory.getCurrentPath();
c.createDirectory();
assertTrue(c.exists());
- this.artifactHistory.unstash();
+ this.artifactHistory.previous();
assertFalse(c.exists());
}
@@ -96,9 +97,9 @@ public class PathHistoryTests {
PathReference c = this.artifactHistory.getCurrentPath();
c.createFile();
assertTrue(c.exists());
- this.artifactHistory.stash();
+ this.artifactHistory.next();
assertTrue(c.exists());
- this.artifactHistory.stash();
+ this.artifactHistory.next();
assertFalse(c.exists());
}
@@ -107,29 +108,45 @@ public class PathHistoryTests {
PathReference c = this.artifactHistory.getCurrentPath();
c.createDirectory();
assertTrue(c.exists());
- this.artifactHistory.stash();
+ this.artifactHistory.next();
assertTrue(c.exists());
- this.artifactHistory.stash();
+ this.artifactHistory.next();
assertFalse(c.exists());
}
@Test(expected=IllegalStateException.class)
public void testBadUnstash() {
- this.artifactHistory.unstash();
+ this.artifactHistory.previous();
}
@Test(expected=IllegalStateException.class)
public void testDoubleUnstash() {
- this.artifactHistory.stash();
- this.artifactHistory.stash();
- this.artifactHistory.unstash();
- this.artifactHistory.unstash();
+ this.artifactHistory.next();
+ this.artifactHistory.next();
+ this.artifactHistory.previous();
+ this.artifactHistory.previous();
}
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testNullConstructorPath() {
+ new PathGenerator(null);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testEmptyConstructorPath() {
+ new PathGenerator(new PathReference(""));
+ }
+
+ public void testDirectorylessConstructorPath() {
+ PathGenerator ph = new PathGenerator(new PathReference("a"));
+ assertEquals("a", ph.getCurrentPath().getName());
+ }
+
private void checkPath(PathReference c) {
File file = c.toFile();
assertTrue(file.toURI().toString().indexOf(TEST_PATH) != -1);
- //TODO: assertEquals(TEST_FILENAME, c.getName());
+ assertEquals(TEST_FILENAME, c.getName());
}
}
diff --git a/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/pipeline/stage/transform/internal/SyntheticContextBundleCreatingTransformerTests.java b/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/pipeline/stage/transform/internal/SyntheticContextBundleCreatingTransformerTests.java
index d7e30aef..fb65f339 100644
--- a/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/pipeline/stage/transform/internal/SyntheticContextBundleCreatingTransformerTests.java
+++ b/org.eclipse.virgo.kernel.deployer/src/test/java/org/eclipse/virgo/kernel/install/pipeline/stage/transform/internal/SyntheticContextBundleCreatingTransformerTests.java
@@ -83,7 +83,7 @@ public final class SyntheticContextBundleCreatingTransformerTests {
GraphNode<InstallArtifact> planInstallGraph = createMockPlan(true, new Version(1, 0, 0), "plan-name", "bundle1", "bundle2", "bundle3");
InstallArtifact syntheticContextInstallArtifact = createMock(InstallArtifact.class);
- File syntheticBundleDir = new File("target/work/staging/plan-name-1/bundle/plan-name-1-synthetic.context/1.0.0/plan-name-1-synthetic.context.jar-0").getAbsoluteFile();
+ File syntheticBundleDir = new File("target/work/staging/plan-name-1/bundle/plan-name-1-synthetic.context/1.0.0/0/plan-name-1-synthetic.context.jar").getAbsoluteFile();
expect(
this.installArtifactGraphFactory.constructInstallArtifactGraph(eq(new ArtifactIdentity("bundle", "plan-name-1-synthetic.context",
new Version(1, 0, 0), ScopeNameFactory.createScopeName("plan-name", new Version(1, 0, 0)))), isA(ArtifactStorage.class),
@@ -109,7 +109,7 @@ public final class SyntheticContextBundleCreatingTransformerTests {
InstallArtifact syntheticContextInstallArtifact = createMock(InstallArtifact.class);
- File syntheticBundleDir = new File("target/work/staging/plan-name-1/bundle/plan-name-1-synthetic.context/1.0.0/plan-name-1-synthetic.context.jar-0").getAbsoluteFile();
+ File syntheticBundleDir = new File("target/work/staging/plan-name-1/bundle/plan-name-1-synthetic.context/1.0.0/0/plan-name-1-synthetic.context.jar").getAbsoluteFile();
expect(
this.installArtifactGraphFactory.constructInstallArtifactGraph(eq(new ArtifactIdentity("bundle", "plan-name-1-synthetic.context",
new Version(1, 0, 0), ScopeNameFactory.createScopeName("plan-name", new Version(1, 0, 0)))), isA(ArtifactStorage.class),
@@ -136,7 +136,7 @@ public final class SyntheticContextBundleCreatingTransformerTests {
InstallArtifact syntheticContextInstallArtifact = createMock(InstallArtifact.class);
File syntheticBundleDir = new File(
- "target/work/staging/nested-plan-1/bundle/nested-plan-1-synthetic.context/1.0.0/nested-plan-1-synthetic.context.jar-0").getAbsoluteFile();
+ "target/work/staging/nested-plan-1/bundle/nested-plan-1-synthetic.context/1.0.0/0/nested-plan-1-synthetic.context.jar").getAbsoluteFile();
expect(
this.installArtifactGraphFactory.constructInstallArtifactGraph(eq(new ArtifactIdentity("bundle", "nested-plan-1-synthetic.context",
new Version(1, 0, 0), ScopeNameFactory.createScopeName("nested-plan", new Version(1, 0, 0)))), isA(ArtifactStorage.class),

Back to the top