浏览代码

fix #2474 load jar file on web app

Shinsuke Sugaya 5 年之前
父节点
当前提交
49fe8c0cec

+ 8 - 0
src/main/java/org/codelibs/fess/FessBoot.java

@@ -19,10 +19,12 @@ package org.codelibs.fess;
 
 
 import java.io.File;
 import java.io.File;
 
 
+import org.apache.catalina.Context;
 import org.apache.catalina.Host;
 import org.apache.catalina.Host;
 import org.apache.catalina.core.StandardHost;
 import org.apache.catalina.core.StandardHost;
 import org.codelibs.core.lang.StringUtil;
 import org.codelibs.core.lang.StringUtil;
 import org.codelibs.fess.tomcat.valve.SuppressErrorReportValve;
 import org.codelibs.fess.tomcat.valve.SuppressErrorReportValve;
+import org.codelibs.fess.tomcat.webresources.FessWebResourceRoot;
 import org.dbflute.tomcat.TomcatBoot;
 import org.dbflute.tomcat.TomcatBoot;
 
 
 public class FessBoot extends TomcatBoot {
 public class FessBoot extends TomcatBoot {
@@ -125,4 +127,10 @@ public class FessBoot extends TomcatBoot {
     protected static String getTomcatConfigPath() {
     protected static String getTomcatConfigPath() {
         return System.getProperty(TOMCAT_CONFIG_PATH);
         return System.getProperty(TOMCAT_CONFIG_PATH);
     }
     }
+
+    protected void setupWebappContext() {
+        super.setupWebappContext();
+        Context context = (Context) server.getHost().findChild(StringUtil.EMPTY);
+        context.setResources(new FessWebResourceRoot(context));
+    }
 }
 }

+ 17 - 12
src/main/java/org/codelibs/fess/helper/PluginHelper.java

@@ -242,6 +242,18 @@ public class PluginHelper {
     }
     }
 
 
     public void installArtifact(final Artifact artifact) {
     public void installArtifact(final Artifact artifact) {
+        switch (artifact.getType()) {
+        case THEME:
+            install(artifact);
+            ComponentUtil.getThemeHelper().install(artifact);
+            break;
+        default:
+            install(artifact);
+            break;
+        }
+    }
+
+    protected void install(final Artifact artifact) {
         final String fileName = artifact.getFileName();
         final String fileName = artifact.getFileName();
         final String url = artifact.getUrl();
         final String url = artifact.getUrl();
         if (StringUtil.isBlank(url)) {
         if (StringUtil.isBlank(url)) {
@@ -264,16 +276,6 @@ public class PluginHelper {
                 throw new PluginException("Failed to install the artifact " + artifact.getName(), e);
                 throw new PluginException("Failed to install the artifact " + artifact.getName(), e);
             }
             }
         }
         }
-
-        switch (artifact.getType()) {
-        case DATA_STORE:
-            break;
-        case THEME:
-            ComponentUtil.getThemeHelper().install(artifact);
-            break;
-        default:
-            break;
-        }
     }
     }
 
 
     protected CurlRequest createCurlRequest(final String url) {
     protected CurlRequest createCurlRequest(final String url) {
@@ -293,15 +295,18 @@ public class PluginHelper {
         }
         }
 
 
         switch (artifact.getType()) {
         switch (artifact.getType()) {
-        case DATA_STORE:
-            break;
         case THEME:
         case THEME:
             ComponentUtil.getThemeHelper().uninstall(artifact);
             ComponentUtil.getThemeHelper().uninstall(artifact);
+            uninstall(fileName, jarPath);
             break;
             break;
         default:
         default:
+            uninstall(fileName, jarPath);
             break;
             break;
         }
         }
 
 
+    }
+
+    protected void uninstall(final String fileName, final Path jarPath) {
         try {
         try {
             Files.delete(jarPath);
             Files.delete(jarPath);
         } catch (final IOException e) {
         } catch (final IOException e) {

+ 58 - 0
src/main/java/org/codelibs/fess/tomcat/webresources/FessWebResourceRoot.java

@@ -0,0 +1,58 @@
+/*
+ * Copyright 2012-2020 CodeLibs Project and the Others.
+ *
+ * Licensed 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.codelibs.fess.tomcat.webresources;
+
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.WebResource;
+import org.apache.catalina.webresources.StandardRoot;
+import org.codelibs.core.jar.JarFileUtil;
+
+public class FessWebResourceRoot extends StandardRoot {
+    private static final Logger logger = Logger.getLogger(FessWebResourceRoot.class.getName());
+
+    public FessWebResourceRoot(Context context) {
+        super(context);
+    }
+
+    protected void processWebInfLib() throws LifecycleException {
+        super.processWebInfLib();
+
+        WebResource[] possibleJars = listResources("/WEB-INF/plugin", false);
+
+        for (WebResource possibleJar : possibleJars) {
+            if (possibleJar.isFile() && possibleJar.getName().endsWith(".jar")) {
+                try (final JarFile jarFile = JarFileUtil.create(possibleJar.getCanonicalPath())) {
+                    final Manifest manifest = jarFile.getManifest();
+                    if (manifest != null && manifest.getEntries() != null) {
+                        Attributes attributes = manifest.getMainAttributes();
+                        if (attributes != null && attributes.get("Fess-WebAppJar") != null) {
+                            createWebResourceSet(ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", possibleJar.getURL(), "/");
+                        }
+                    }
+                } catch (final Exception e) {
+                    logger.log(Level.WARNING, "Failed to read " + possibleJar, e);
+                }
+            }
+        }
+    }
+}