[FELIX-4571] NullPointerException when using Repository impl with Aries subsystem impl
This commit should fix this problem.
Unit test also included.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1626798 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java
index f896f4d..554aea5 100644
--- a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java
+++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java
@@ -1,4 +1,4 @@
-/*
+/*
* 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
@@ -18,8 +18,19 @@
*/
package org.apache.felix.bundlerepository.impl;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
import java.net.URI;
-import java.util.*;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
import org.apache.felix.bundlerepository.Capability;
import org.apache.felix.bundlerepository.Property;
@@ -119,7 +130,62 @@
public Long getSize()
{
- return ((Long) m_map.get(Resource.SIZE));
+ Object sz = m_map.get(Resource.SIZE);
+ if (sz instanceof Long)
+ return ((Long) sz);
+
+ long size = findResourceSize();
+ m_map.put(Resource.SIZE, size);
+ return size;
+ }
+
+ private long findResourceSize()
+ {
+ String uri = getURI();
+ if (uri != null) {
+ try
+ {
+ URL url = new URL(uri);
+ if ("file".equals(url.getProtocol()))
+ return new File(url.getFile()).length();
+ else
+ return findResourceSize(url);
+ }
+ catch (Exception e)
+ {
+ // TODO should really log this...
+ }
+ }
+ return -1L;
+ }
+
+ private long findResourceSize(URL url) throws IOException
+ {
+ byte[] bytes = new byte[8192];
+
+ // Not a File URL, stream the whole thing through to find out the size
+ InputStream is = null;
+ long fileSize = 0;
+ try
+ {
+ is = url.openStream();
+
+ int length = 0;
+ while ((length = is.read(bytes)) != -1)
+ {
+ fileSize += length;
+ }
+ }
+ catch (Exception ex)
+ {
+ // should really log this...
+ }
+ finally
+ {
+ if (is != null)
+ is.close();
+ }
+ return fileSize;
}
public Requirement[] getRequirements()
@@ -169,7 +235,7 @@
}
/**
- * Default setter method when setting parsed data from the XML file.
+ * Default setter method when setting parsed data from the XML file.
**/
public Object put(Object key, Object value)
{
diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java
new file mode 100644
index 0000000..8f9dd3a
--- /dev/null
+++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.bundlerepository.impl;
+
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.bundlerepository.Property;
+import org.apache.felix.bundlerepository.Repository;
+
+public class ResourceImplTest extends TestCase
+{
+ public void testGetSizeFileResource() {
+ ResourceImpl res = new ResourceImpl();
+ res.put(Property.URI, "repo_files/test_file_3.jar");
+
+ final URL dir = getClass().getResource("/repo_files");
+ Repository repo = new RepositoryImpl() {
+ { setURI(dir.toExternalForm()); }
+ };
+ res.setRepository(repo);
+
+ assertEquals("Should have obtained the file size", 3, (long) res.getSize());
+ }
+
+ public void testGetSizeNonExistentFileResource() {
+ ResourceImpl res = new ResourceImpl();
+ res.put(Property.URI, "repo_files/test_file_3_garbage.jar");
+
+ final URL dir = getClass().getResource("/repo_files");
+ Repository repo = new RepositoryImpl() {
+ { setURI(dir.toExternalForm()); }
+ };
+ res.setRepository(repo);
+
+ assertEquals("File size should be reported as 0", 0, (long) res.getSize());
+ }
+
+ public void testGetSizeNonFileResource() {
+ final URL testFile4 = getClass().getResource("/repo_files/test_file_4.jar");
+
+ ResourceImpl res = new ResourceImpl();
+ res.put(Property.URI, "jar:" + testFile4.toExternalForm() + "!/blah.txt");
+
+ final URL dir = getClass().getResource("/repo_files");
+ Repository repo = new RepositoryImpl() {
+ { setURI(dir.toExternalForm()); }
+ };
+ res.setRepository(repo);
+
+ assertEquals("Should have obtained the file size", 5, (long) res.getSize());
+ }
+
+ public void testGetSizeNonExistentResource() {
+ final URL testFile4 = getClass().getResource("/repo_files/test_file_4.jar");
+
+ ResourceImpl res = new ResourceImpl();
+ res.put(Property.URI, "jar:" + testFile4.toExternalForm() + "!/blah_xyz.txt");
+
+ final URL dir = getClass().getResource("/repo_files");
+ Repository repo = new RepositoryImpl() {
+ { setURI(dir.toExternalForm()); }
+ };
+ res.setRepository(repo);
+
+ assertEquals("File size should be reported as 0", 0, (long) res.getSize());
+ }
+}
diff --git a/bundlerepository/src/test/resources/repo_files/test_file_4.jar b/bundlerepository/src/test/resources/repo_files/test_file_4.jar
new file mode 100644
index 0000000..5816bf4
--- /dev/null
+++ b/bundlerepository/src/test/resources/repo_files/test_file_4.jar
Binary files differ