Remove dependency to Spring.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 16 Feb 2020 09:26:28 +0000 (10:26 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 16 Feb 2020 09:26:28 +0000 (10:26 +0100)
org.argeo.slc.factory/src/org/argeo/slc/rpmfactory/core/RpmSpecFile.java
org.argeo.slc.factory/src/org/argeo/slc/rpmfactory/core/YumListParser.java

index 4b4db3b2e7f945aaf64a78c210a5eda60612fd4e..8b5994f3207936545993f5ecdf4396c686fb3061 100644 (file)
 package org.argeo.slc.rpmfactory.core;
 
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.io.IOUtils;
-import org.springframework.core.io.Resource;
-
 public class RpmSpecFile {
-       private Resource specFile;
+       private Path specFile;
 
        private String name;
        private String version;
@@ -32,7 +31,7 @@ public class RpmSpecFile {
        private Map<String, String> sources = new HashMap<String, String>();
        private Map<String, String> patches = new HashMap<String, String>();
 
-       public RpmSpecFile(Resource specFile) {
+       public RpmSpecFile(Path specFile) {
                this.specFile = specFile;
                parseSpecFile();
        }
@@ -41,11 +40,9 @@ public class RpmSpecFile {
                parseSpecFile();
        }
 
-       @SuppressWarnings("unchecked")
        protected void parseSpecFile() {
                try {
-                       List<String> lines = (List<String>) IOUtils.readLines(specFile
-                                       .getInputStream());
+                       List<String> lines = (List<String>) Files.readAllLines(specFile);
 
                        lines: for (String line : lines) {
                                int indexSemiColon = line.indexOf(':');
@@ -101,7 +98,7 @@ public class RpmSpecFile {
                return buf.toString();
        }
 
-       public Resource getSpecFile() {
+       public Path getSpecFile() {
                return specFile;
        }
 
index 1b44dd494f9bca8227a120dd6292d7d36922c1be..ee89aa311cabdcab34e6e7fee4e74470443517ac 100644 (file)
@@ -2,6 +2,8 @@ package org.argeo.slc.rpmfactory.core;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.TreeSet;
@@ -12,7 +14,6 @@ import org.apache.commons.io.LineIterator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
-import org.springframework.core.io.Resource;
 
 /**
  * Reads the output of a 'yum list all' command and interpret the list of
@@ -25,19 +26,18 @@ public class YumListParser implements RpmPackageSet {
        /** Not installed but available */
        private Set<String> installable = new TreeSet<String>();
 
-       private Resource yumListOutput;
+       private Path yumListOutput;
 
        public void init() {
-               try {
-                       if (yumListOutput != null) {
-                               load(yumListOutput.getInputStream());
+               if (yumListOutput != null) {
+                       try (InputStream in = Files.newInputStream(yumListOutput)) {
+                               load(in);
                                if (log.isDebugEnabled())
-                                       log.debug(installed.size() + " installed, "
-                                                       + installable.size() + " installable, from "
+                                       log.debug(installed.size() + " installed, " + installable.size() + " installable, from "
                                                        + yumListOutput);
+                       } catch (IOException e) {
+                               throw new SlcException("Cannot initialize yum list parser", e);
                        }
-               } catch (IOException e) {
-                       throw new SlcException("Cannot initialize yum list parser", e);
                }
        }
 
@@ -48,32 +48,26 @@ public class YumListParser implements RpmPackageSet {
                        return installable.contains(packageName);
        }
 
-       protected void load(InputStream in) {
-               try {
-                       Boolean readingInstalled = false;
-                       Boolean readingAvailable = false;
-                       LineIterator it = IOUtils.lineIterator(in, "UTF-8");
-                       while (it.hasNext()) {
-                               String line = it.nextLine();
-                               if (line.trim().equals("Installed Packages")) {
-                                       readingInstalled = true;
-                               } else if (line.trim().equals("Available Packages")) {
-                                       readingAvailable = true;
-                                       readingInstalled = false;
-                               } else if (readingAvailable) {
-                                       if (Character.isLetterOrDigit(line.charAt(0))) {
-                                               installable.add(extractRpmName(line));
-                                       }
-                               } else if (readingInstalled) {
-                                       if (Character.isLetterOrDigit(line.charAt(0))) {
-                                               installed.add(extractRpmName(line));
-                                       }
+       protected void load(InputStream in) throws IOException {
+               Boolean readingInstalled = false;
+               Boolean readingAvailable = false;
+               LineIterator it = IOUtils.lineIterator(in, "UTF-8");
+               while (it.hasNext()) {
+                       String line = it.nextLine();
+                       if (line.trim().equals("Installed Packages")) {
+                               readingInstalled = true;
+                       } else if (line.trim().equals("Available Packages")) {
+                               readingAvailable = true;
+                               readingInstalled = false;
+                       } else if (readingAvailable) {
+                               if (Character.isLetterOrDigit(line.charAt(0))) {
+                                       installable.add(extractRpmName(line));
+                               }
+                       } else if (readingInstalled) {
+                               if (Character.isLetterOrDigit(line.charAt(0))) {
+                                       installed.add(extractRpmName(line));
                                }
                        }
-               } catch (IOException e) {
-                       throw new SlcException("Cannot load yum list output", e);
-               } finally {
-                       IOUtils.closeQuietly(in);
                }
        }
 
@@ -82,7 +76,7 @@ public class YumListParser implements RpmPackageSet {
                String packageName = st.nextToken();
                // consider the arch as an extension
                return FilenameUtils.getBaseName(packageName);
-               //return packageName.split("\\.")[0];
+               // return packageName.split("\\.")[0];
        }
 
        public Set<String> getInstalled() {
@@ -93,7 +87,7 @@ public class YumListParser implements RpmPackageSet {
                return installable;
        }
 
-       public void setYumListOutput(Resource yumListOutput) {
+       public void setYumListOutput(Path yumListOutput) {
                this.yumListOutput = yumListOutput;
        }