karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject [2/2] karaf git commit: [KARAF-4210] Make sure resource is released
Date Mon, 06 Feb 2017 15:55:52 GMT
[KARAF-4210] Make sure resource is released


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/868c6852
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/868c6852
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/868c6852

Branch: refs/heads/master
Commit: 868c6852424a80e07d2f925791c28ac498581145
Parents: e720fc7
Author: Christian Schneider <chris@die-schneider.net>
Authored: Mon Feb 6 16:55:38 2017 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Mon Feb 6 16:55:38 2017 +0100

----------------------------------------------------------------------
 .../core/internal/MavenConfigService.java       | 79 ++++++++++++--------
 .../core/internal/MavenConfigServiceTest.java   |  2 -
 2 files changed, 49 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/868c6852/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/MavenConfigService.java
----------------------------------------------------------------------
diff --git a/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/MavenConfigService.java
b/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/MavenConfigService.java
index 9ad7b0a..4ff09bc 100644
--- a/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/MavenConfigService.java
+++ b/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/MavenConfigService.java
@@ -18,7 +18,7 @@ package org.apache.karaf.bundle.core.internal;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
+import java.io.InputStream;
 import java.util.Dictionary;
 
 import javax.xml.stream.XMLInputFactory;
@@ -33,24 +33,18 @@ import org.slf4j.LoggerFactory;
 
 public class MavenConfigService {
 
-	private final Logger logger = LoggerFactory.getLogger(MavenConfigService.class);
+	private final static Logger logger = LoggerFactory.getLogger(MavenConfigService.class);
 	private final ConfigurationAdmin configurationAdmin;
 
 	public MavenConfigService(ConfigurationAdmin configurationAdmin) {
 		this.configurationAdmin = configurationAdmin;
 	}
 
-    public File getLocalRepository() {
+    File getLocalRepository() {
         String path = null;
-        try {
-            Configuration configuration = configurationAdmin.getConfiguration("org.ops4j.pax.url.mvn",
null);
-            if (configuration != null) {
-                Dictionary<String, Object> dict = configuration.getProperties();
-                path = getLocalRepoFromConfig(dict);
-
-            }
-        } catch (Exception e) {
-            logger.error("Error retrieving maven configuration", e);
+        Dictionary<String, Object> config = getConfiguration();
+        if (config != null) {
+        	path = getLocalRepoFromConfig(config);
         }
         if (path == null) {
             path = System.getProperty("user.home") + File.separator + ".m2" + File.separator
+ "repository";
@@ -63,34 +57,59 @@ public class MavenConfigService {
         }
     }
 
-    static String getLocalRepoFromConfig(Dictionary<String, Object> dict) throws XMLStreamException,
FileNotFoundException {
+	private Dictionary<String, Object> getConfiguration() {
+		try {
+            Configuration configuration = configurationAdmin.getConfiguration("org.ops4j.pax.url.mvn",
null);
+            if (configuration != null) {
+                return configuration.getProperties();
+            }
+        } catch (Exception e) {
+            logger.error("Error retrieving maven configuration", e);
+        }
+		return null;
+	}
+
+    static String getLocalRepoFromConfig(Dictionary<String, Object> dict) {
         String path = null;
         if (dict != null) {
             path = (String) dict.get("org.ops4j.pax.url.mvn.localRepository");
             if (path == null) {
                 String settings = (String) dict.get("org.ops4j.pax.url.mvn.settings");
                 if (settings != null) {
-                    File file = new File(settings);
-                    XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(new
FileInputStream(file));
-                    try {
-                        int event;
-                        String elementName = null;
-                        while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT)
{
-                            if (event == XMLStreamConstants.START_ELEMENT) {
-                                elementName = reader.getLocalName();
-                            } else if (event == XMLStreamConstants.END_ELEMENT) {
-                                elementName = null;
-                            } else if (event == XMLStreamConstants.CHARACTERS &&
"localRepository".equals(elementName))  {
-                                path = reader.getText().trim();
-                            }
-                        }
-                    } finally {
-                        reader.close();
-                    }
+                    path = getLocalRepositoryFromSettings(new File(settings));
                 }
             }
         }
         return path;
     }
 
+	private static String getLocalRepositoryFromSettings(File file) {
+		XMLStreamReader reader = null;
+		try (InputStream fin = new FileInputStream(file)) {
+			reader = XMLInputFactory.newFactory().createXMLStreamReader(fin);
+		    int event;
+		    String elementName = null;
+		    while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
+		        if (event == XMLStreamConstants.START_ELEMENT) {
+		            elementName = reader.getLocalName();
+		        } else if (event == XMLStreamConstants.END_ELEMENT) {
+		            elementName = null;
+		        } else if (event == XMLStreamConstants.CHARACTERS && "localRepository".equals(elementName))
 {
+		            return reader.getText().trim();
+		        }
+		    }
+		} catch (Exception e) {
+			logger.error("Error retrieving maven configuration", e);
+		} finally {
+			if (reader != null) {
+				try {
+					reader.close();
+				} catch (XMLStreamException e) {
+					// Ignore
+				}
+			}
+		}
+		return null;
+	}
+
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/868c6852/bundle/core/src/test/java/org/apache/karaf/bundle/core/internal/MavenConfigServiceTest.java
----------------------------------------------------------------------
diff --git a/bundle/core/src/test/java/org/apache/karaf/bundle/core/internal/MavenConfigServiceTest.java
b/bundle/core/src/test/java/org/apache/karaf/bundle/core/internal/MavenConfigServiceTest.java
index 55af586..77eda63 100644
--- a/bundle/core/src/test/java/org/apache/karaf/bundle/core/internal/MavenConfigServiceTest.java
+++ b/bundle/core/src/test/java/org/apache/karaf/bundle/core/internal/MavenConfigServiceTest.java
@@ -16,8 +16,6 @@
  */
 package org.apache.karaf.bundle.core.internal;
 
-import java.io.File;
-import java.util.HashMap;
 import java.util.Hashtable;
 
 import org.junit.Test;


Mime
View raw message