FELIX-5009: allow relative pathnames for gogo install and update.
Thanks to Benson Margulies
This closes #34
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1700124 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/command/pom.xml b/gogo/command/pom.xml
index e61f4b9..e40bfdc 100644
--- a/gogo/command/pom.xml
+++ b/gogo/command/pom.xml
@@ -56,6 +56,12 @@
<artifactId>org.apache.felix.bundlerepository</artifactId>
<version>1.6.0</version>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.10.19</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/gogo/command/src/main/java/org/apache/felix/gogo/command/Basic.java b/gogo/command/src/main/java/org/apache/felix/gogo/command/Basic.java
index 0ac4899..8ca212b 100644
--- a/gogo/command/src/main/java/org/apache/felix/gogo/command/Basic.java
+++ b/gogo/command/src/main/java/org/apache/felix/gogo/command/Basic.java
@@ -423,13 +423,14 @@
}
@Descriptor("install bundle using URLs")
- public void install(@Descriptor("target URLs") String[] urls)
+ public void install(@Descriptor("command session")CommandSession session,
+ @Descriptor("target URLs") String[] urls) throws IOException
{
StringBuffer sb = new StringBuffer();
for (String url : urls)
{
- String location = url.trim();
+ String location = Util.resolveUri(session, url.trim());
Bundle bundle = null;
try
{
@@ -876,11 +877,13 @@
}
@Descriptor("update bundle from URL")
- public void update(@Descriptor("target bundle") Bundle bundle,
- @Descriptor("URL from where to retrieve bundle") String location)
- {
+ public void update(
+ @Descriptor("command session") CommandSession session,
+ @Descriptor("target bundle") Bundle bundle,
+ @Descriptor("URL from where to retrieve bundle") String location) throws IOException {
if (location != null)
{
+ location = Util.resolveUri(session, location.trim());
try
{
// Get the bundle.
diff --git a/gogo/command/src/main/java/org/apache/felix/gogo/command/Files.java b/gogo/command/src/main/java/org/apache/felix/gogo/command/Files.java
index 43bb4cf..b474af7 100644
--- a/gogo/command/src/main/java/org/apache/felix/gogo/command/Files.java
+++ b/gogo/command/src/main/java/org/apache/felix/gogo/command/Files.java
@@ -27,11 +27,10 @@
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Descriptor;
import org.osgi.framework.BundleContext;
+import static org.apache.felix.gogo.command.Util.CWD;
public class Files
{
- private static final String CWD = "_cwd";
-
private final BundleContext m_bc;
public Files(BundleContext bc)
diff --git a/gogo/command/src/main/java/org/apache/felix/gogo/command/Util.java b/gogo/command/src/main/java/org/apache/felix/gogo/command/Util.java
index 4b4e76b..14759e9 100644
--- a/gogo/command/src/main/java/org/apache/felix/gogo/command/Util.java
+++ b/gogo/command/src/main/java/org/apache/felix/gogo/command/Util.java
@@ -26,12 +26,15 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
+import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
+
+import org.apache.felix.service.command.CommandSession;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@@ -39,6 +42,10 @@
public class Util
{
+
+
+ static final String CWD = "_cwd";
+
public static String getBundleName(Bundle bundle)
{
if (bundle != null)
@@ -490,4 +497,29 @@
return result;
}
-}
\ No newline at end of file
+
+ /**
+ * Intepret a string as a URI relative to the current working directory.
+ * @param session the session (where the CWD is stored)
+ * @param relativeUri the input URI
+ * @return the resulting URI as a string
+ * @throws IOException
+ */
+ public static String resolveUri(CommandSession session, String relativeUri) throws IOException
+ {
+ File cwd = (File) session.get(CWD);
+ if (cwd == null)
+ {
+ cwd = new File("").getCanonicalFile();
+ session.put(CWD, cwd);
+ }
+ if ((relativeUri == null) || (relativeUri.length() == 0))
+ {
+ return relativeUri;
+ }
+
+ URI curUri = cwd.toURI();
+ URI newUri = curUri.resolve(relativeUri);
+ return newUri.toString();
+ }
+}
diff --git a/gogo/command/src/test/java/org/apache/felix/gogo/command/UtilTest.java b/gogo/command/src/test/java/org/apache/felix/gogo/command/UtilTest.java
new file mode 100644
index 0000000..89008cf
--- /dev/null
+++ b/gogo/command/src/test/java/org/apache/felix/gogo/command/UtilTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.gogo.command;
+
+import org.apache.felix.service.command.CommandSession;
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ *
+ */
+public class UtilTest {
+
+ @Test
+ public void relativeUri() throws Exception {
+ CommandSession session = mock(CommandSession.class);
+ when(session.get(Util.CWD)).thenReturn(null); // start with no CWD
+ String u = Util.resolveUri(session, "three");
+ assertEquals(new File(new File("").getAbsoluteFile(), "three").toURI().toString(), u);
+
+ when(session.get(Util.CWD)).thenReturn(new File(".").getCanonicalFile());
+ u = Util.resolveUri(session, "file:/a/b/c");
+ assertEquals("file:/a/b/c", u);
+ u = Util.resolveUri(session, "three");
+ assertEquals(new File("./three").getCanonicalFile().toURI().toString(), u);
+ }
+}