瀏覽代碼

#2203 refactoring

Shinsuke Sugaya 6 年之前
父節點
當前提交
569a9527f6

+ 36 - 21
src/main/java/org/codelibs/fess/helper/PluginHelper.java

@@ -43,10 +43,10 @@ public class PluginHelper {
 
     protected static final String VERSION = "version";
 
-    public Artifact[] getArtifacts(final PluginType pluginType) {
+    public Artifact[] getArtifacts(final ArtifactType artifactType) {
         final List<Artifact> list = new ArrayList<>();
         for (final String url : getRepositories()) {
-            list.addAll(processRepository(pluginType, url, ""));
+            list.addAll(processRepository(artifactType, url));
         }
         return list.toArray(new Artifact[list.size()]);
     }
@@ -56,17 +56,17 @@ public class PluginHelper {
                 stream -> stream.map(s -> s.trim()).toArray(n -> new String[n]));
     }
 
-    protected List<Artifact> processRepository(final PluginType pluginType, final String url, final String index) {
+    protected List<Artifact> processRepository(final ArtifactType artifactType, final String url) {
         final List<Artifact> list = new ArrayList<>();
-        final String repoContent = getRepositoryContent(url + "/" + index);
-        final Matcher matcher = Pattern.compile("href=\"[^\"]*(" + pluginType.getId() + "[a-zA-Z0-9\\-]+)/?\"").matcher(repoContent);
+        final String repoContent = getRepositoryContent(url);
+        final Matcher matcher = Pattern.compile("href=\"[^\"]*(" + artifactType.getId() + "[a-zA-Z0-9\\-]+)/?\"").matcher(repoContent);
         while (matcher.find()) {
             final String name = matcher.group(1);
-            try {
-                final String pluginUrl = url + "/" + name + "/";
-                final String mavenMetadata = getRepositoryContent(pluginUrl + "maven-metadata.xml");
+            final String pluginUrl = url + (url.endsWith("/") ? name + "/" : "/" + name + "/");
+            final String mavenMetadata = getRepositoryContent(pluginUrl + "maven-metadata.xml");
+            try (StringReader reader = new StringReader(mavenMetadata)) {
                 final DOMParser parser = new DOMParser();
-                parser.parse(new InputSource(new StringReader(mavenMetadata)));
+                parser.parse(new InputSource(reader));
                 final Document document = parser.getDocument();
                 final NodeList nodeList = document.getElementsByTagName(VERSION);
                 for (int i = 0; i < nodeList.getLength(); i++) {
@@ -74,7 +74,7 @@ public class PluginHelper {
                     list.add(new Artifact(name, version, pluginUrl + version + "/" + name + "-" + version + ".jar"));
                 }
             } catch (final Exception e) {
-                logger.warn("Failed to parse maven-metadata.xml.", e);
+                logger.warn("Failed to parse " + pluginUrl + "metadata.xml.", e);
             }
         }
         return list;
@@ -88,21 +88,29 @@ public class PluginHelper {
         }
     }
 
-    protected Artifact[] getArtifactsAlreadyInstalled(final PluginType pluginType) {
-        final List<Artifact> list = new ArrayList<>();
-        File[] jarFiles = ResourceUtil.getJarFiles(pluginType.getId());
+    public Artifact[] getInstalledArtifacts(final ArtifactType artifactType) {
+        final File[] jarFiles = ResourceUtil.getJarFiles(artifactType.getId());
+        final List<Artifact> list = new ArrayList<>(jarFiles.length);
         for (final File file : jarFiles) {
-            list.add(getArtifactFromFileName(pluginType, file.getName()));
+            list.add(getArtifactFromFileName(artifactType, file.getName()));
         }
         return list.toArray(new Artifact[list.size()]);
     }
 
-    protected Artifact getArtifactFromFileName(final PluginType pluginType, final String fileName) {
-        final String convertedFileName = fileName.substring(pluginType.getId().length() + 1, fileName.lastIndexOf('.'));
+    protected Artifact getArtifactFromFileName(final ArtifactType artifactType, final String fileName) {
+        final String convertedFileName = fileName.substring(artifactType.getId().length() + 1, fileName.lastIndexOf('.'));
         final int firstIndexOfDash = convertedFileName.indexOf("-");
-        final String artifactName = pluginType.getId() + "-" + convertedFileName.substring(0, firstIndexOfDash);
+        final String artifactName = artifactType.getId() + "-" + convertedFileName.substring(0, firstIndexOfDash);
         final String artifactVersion = convertedFileName.substring(firstIndexOfDash + 1);
-        return new Artifact(artifactName, artifactVersion, "NONE");
+        return new Artifact(artifactName, artifactVersion, null);
+    }
+
+    public void installArtifact(Artifact artifact) {
+        // TODO
+    }
+
+    public void deleteInstalledArtifact(Artifact artifact) {
+        // TODO
     }
 
     public static class Artifact {
@@ -129,17 +137,24 @@ public class PluginHelper {
         }
     }
 
-    public enum PluginType {
-        DATA_STORE("fess-ds");
+    public enum ArtifactType {
+        DATA_STORE("fess-ds"), UNKNOWN("unknown");
 
         private String id;
 
-        private PluginType(String id) {
+        private ArtifactType(String id) {
             this.id = id;
         }
 
         public String getId() {
             return id;
         }
+
+        public ArtifactType getType(String name) {
+            if (name.startsWith(DATA_STORE.getId())) {
+                return DATA_STORE;
+            }
+            return UNKNOWN;
+        }
     }
 }

+ 14 - 13
src/test/java/org/codelibs/fess/helper/PluginHelperTest.java

@@ -15,7 +15,6 @@
  */
 package org.codelibs.fess.helper;
 
-import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 
@@ -24,9 +23,8 @@ import org.codelibs.core.io.ResourceUtil;
 import org.codelibs.fess.Constants;
 import org.codelibs.fess.exception.FessSystemException;
 import org.codelibs.fess.helper.PluginHelper.Artifact;
-import org.codelibs.fess.helper.PluginHelper.PluginType;
+import org.codelibs.fess.helper.PluginHelper.ArtifactType;
 import org.codelibs.fess.unit.UnitFessTestCase;
-import org.lastaflute.di.exception.IORuntimeException;
 
 public class PluginHelperTest extends UnitFessTestCase {
     private PluginHelper pluginHelper;
@@ -40,17 +38,20 @@ public class PluginHelperTest extends UnitFessTestCase {
             }
 
             protected String getRepositoryContent(String url) {
+                if (url.endsWith("/")) {
+                    url = url + "index.html";
+                }
                 if (url.contains("plugin/repo1")) {
                     try (InputStream is = ResourceUtil.getResourceAsStream(url)) {
                         return new String(InputStreamUtil.getBytes(is), Constants.UTF_8);
-                    } catch (IOException e) {
-                        throw new IORuntimeException(e);
+                    } catch (Exception e) {
+                        return "";
                     }
                 } else if (url.contains("plugin/repo2")) {
                     try (InputStream is = ResourceUtil.getResourceAsStream(url)) {
                         return new String(InputStreamUtil.getBytes(is), Constants.UTF_8);
-                    } catch (IOException e) {
-                        throw new IORuntimeException(e);
+                    } catch (Exception e) {
+                        return "";
                     }
                 }
                 throw new FessSystemException("unknown");
@@ -59,29 +60,29 @@ public class PluginHelperTest extends UnitFessTestCase {
     }
 
     public void test_processRepository1() {
-        List<Artifact> list = pluginHelper.processRepository(PluginType.DATA_STORE, "plugin/repo1/", "index.html");
+        List<Artifact> list = pluginHelper.processRepository(ArtifactType.DATA_STORE, "plugin/repo1/");
         assertEquals(7, list.size());
         assertEquals("fess-ds-atlassian", list.get(0).getName());
         assertEquals("12.2.0", list.get(0).getVersion());
-        assertEquals("plugin/repo1/fess-ds-atlassian/12.2.0/fess-ds-atlassian-12.2.0.jar", list.get(0).getUrl().replace("//", "/"));
+        assertEquals("plugin/repo1/fess-ds-atlassian/12.2.0/fess-ds-atlassian-12.2.0.jar", list.get(0).getUrl());
     }
 
     public void test_processRepository2() {
-        List<Artifact> list = pluginHelper.processRepository(PluginType.DATA_STORE, "plugin/repo2/", "index.html");
+        List<Artifact> list = pluginHelper.processRepository(ArtifactType.DATA_STORE, "plugin/repo2/");
         assertEquals(7, list.size());
         assertEquals("fess-ds-atlassian", list.get(0).getName());
         assertEquals("12.2.0", list.get(0).getVersion());
-        assertEquals("plugin/repo2/fess-ds-atlassian/12.2.0/fess-ds-atlassian-12.2.0.jar", list.get(0).getUrl().replace("//", "/"));
+        assertEquals("plugin/repo2/fess-ds-atlassian/12.2.0/fess-ds-atlassian-12.2.0.jar", list.get(0).getUrl());
     }
 
     public void test_getArtifactFromFileName1() {
-        Artifact artifact = pluginHelper.getArtifactFromFileName(PluginType.DATA_STORE, "fess-ds-atlassian-13.2.0.jar");
+        Artifact artifact = pluginHelper.getArtifactFromFileName(ArtifactType.DATA_STORE, "fess-ds-atlassian-13.2.0.jar");
         assertEquals("fess-ds-atlassian", artifact.getName());
         assertEquals("13.2.0", artifact.getVersion());
     }
 
     public void test_getArtifactFromFileName2() {
-        Artifact artifact = pluginHelper.getArtifactFromFileName(PluginType.DATA_STORE, "fess-ds-atlassian-13.2.1-20190708.212247-1.jar");
+        Artifact artifact = pluginHelper.getArtifactFromFileName(ArtifactType.DATA_STORE, "fess-ds-atlassian-13.2.1-20190708.212247-1.jar");
         assertEquals("fess-ds-atlassian", artifact.getName());
         assertEquals("13.2.1-20190708.212247-1", artifact.getVersion());
     }