Author: davidb Date: Mon Sep 22 14:56:14 2014 New Revision: 1626798 URL: http://svn.apache.org/r1626798 Log: [FELIX-4571] NullPointerException when using Repository impl with Aries subsystem impl
This commit should fix this problem. Unit test also included. Added: felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java felix/trunk/bundlerepository/src/test/resources/repo_files/test_file_4.jar (with props) Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java?rev=1626798&r1=1626797&r2=1626798&view=diff ============================================================================== --- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java (original) +++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java Mon Sep 22 14:56:14 2014 @@ -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 class ResourceImpl implements Res 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 @@ public class ResourceImpl implements Res } /** - * 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) { Added: felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java?rev=1626798&view=auto ============================================================================== --- felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java (added) +++ felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java Mon Sep 22 14:56:14 2014 @@ -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()); + } +} Added: felix/trunk/bundlerepository/src/test/resources/repo_files/test_file_4.jar URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/test/resources/repo_files/test_file_4.jar?rev=1626798&view=auto ============================================================================== Binary file - no diff available. Propchange: felix/trunk/bundlerepository/src/test/resources/repo_files/test_file_4.jar ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream