View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess;
17  
18  // DO NOT DEPEND OTHER JARs
19  
20  import java.io.File;
21  
22  import org.apache.catalina.Context;
23  import org.apache.catalina.Host;
24  import org.apache.catalina.core.StandardHost;
25  import org.codelibs.core.lang.StringUtil;
26  import org.codelibs.fess.tomcat.valve.SuppressErrorReportValve;
27  import org.codelibs.fess.tomcat.webresources.FessWebResourceRoot;
28  import org.dbflute.tomcat.TomcatBoot;
29  
30  public class FessBoot extends TomcatBoot {
31  
32      private static final String LOGGING_PROPERTIES = "logging.properties";
33  
34      private static final String FESS_CONTEXT_PATH = "fess.context.path";
35  
36      private static final String FESS_PORT = "fess.port";
37  
38      private static final String FESS_TEMP_PATH = "fess.temp.path";
39  
40      private static final String FESS_WEBAPP_PATH = "fess.webapp.path";
41  
42      private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
43  
44      private static final String TOMCAT_CONFIG_PATH = "tomcat.config.path";
45  
46      public FessBoot(final int port, final String contextPath) {
47          super(port, contextPath);
48      }
49  
50      @Override
51      protected String prepareWebappPath() {
52          final String value = System.getProperty(FESS_WEBAPP_PATH);
53          if (value != null) {
54              return value;
55          }
56          return super.prepareWebappPath();
57      }
58  
59      @Override
60      protected String getMarkDir() {
61          return new File(System.getProperty(JAVA_IO_TMPDIR), "fessboot").getAbsolutePath();
62      }
63  
64      // ===================================================================================
65      //                                                                        main
66      //                                                                        ============
67  
68      public static void main(final String[] args) {
69          // update java.io.tmpdir
70          final String tempPath = System.getProperty(FESS_TEMP_PATH);
71          if (tempPath != null) {
72              System.setProperty(JAVA_IO_TMPDIR, tempPath);
73          }
74  
75          final TomcatBoot tomcatBoot = new FessBoot(getPort(), getContextPath()) //
76                  .useTldDetect(); // for JSP
77          if (tempPath != null) {
78              tomcatBoot.atBaseDir(new File(tempPath, "webapp").getAbsolutePath());
79          }
80          final String tomcatConfigPath = getTomcatConfigPath();
81          if (tomcatConfigPath != null) {
82              tomcatBoot.configure(tomcatConfigPath); // e.g. URIEncoding
83          }
84          tomcatBoot.logging(LOGGING_PROPERTIES, op -> {
85              op.ignoreNoFile();
86              String fessLogPath = System.getProperty("fess.log.path");
87              if (fessLogPath == null) {
88                  fessLogPath = "../../logs";
89              }
90              op.replace("fess.log.path", fessLogPath.replace("\\", "/"));
91          }).asYouLikeIt(resource -> {
92              final Host host = resource.getHost();
93              if (host instanceof StandardHost) {
94                  ((StandardHost) host).setErrorReportValveClass(SuppressErrorReportValve.class.getName());
95              }
96          }).useTldDetect(jarName -> (jarName.contains("jstl") || jarName.contains("lasta-taglib"))).asDevelopment(isNoneEnv()).bootAwait();
97      }
98  
99      public static void shutdown(final String[] args) {
100         System.exit(0);
101     }
102 
103     private static boolean isNoneEnv() {
104         return System.getProperty("lasta.env") == null;
105     }
106 
107     protected static int getPort() {
108         final String value = System.getProperty(FESS_PORT);
109         if (value != null) {
110             return Integer.parseInt(value);
111         }
112         return 8080;
113     }
114 
115     protected static String getContextPath() {
116         final String value = System.getProperty(FESS_CONTEXT_PATH);
117         if (value != null && !"/".equals(value)) {
118             return value;
119         }
120         return StringUtil.EMPTY;
121     }
122 
123     protected static String getTomcatConfigPath() {
124         return System.getProperty(TOMCAT_CONFIG_PATH);
125     }
126 
127     @Override
128     protected void setupWebappContext() {
129         super.setupWebappContext();
130         String contextPath = getContextPath();
131         if (contextPath.length() > 0 && contextPath.endsWith("/")) {
132             contextPath = contextPath.replaceAll("/+$", StringUtil.EMPTY);
133         }
134         final Context context = (Context) server.getHost().findChild(contextPath);
135         if (context != null) {
136             context.setResources(new FessWebResourceRoot(context));
137         }
138     }
139 }