View Javadoc
1   /*
2    * Copyright 2012-2019 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.Host;
23  import org.apache.catalina.core.StandardHost;
24  import org.codelibs.core.lang.StringUtil;
25  import org.codelibs.fess.tomcat.valve.SuppressErrorReportValve;
26  import org.dbflute.tomcat.TomcatBoot;
27  
28  public class FessBoot extends TomcatBoot {
29  
30      private static final String LOGGING_PROPERTIES = "logging.properties";
31  
32      private static final String FESS_CONTEXT_PATH = "fess.context.path";
33  
34      private static final String FESS_PORT = "fess.port";
35  
36      private static final String FESS_TEMP_PATH = "fess.temp.path";
37  
38      private static final String FESS_WEBAPP_PATH = "fess.webapp.path";
39  
40      private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
41  
42      private static final String TOMCAT_CONFIG_PATH = "tomcat.config.path";
43  
44      public FessBoot(final int port, final String contextPath) {
45          super(port, contextPath);
46      }
47  
48      @Override
49      protected String prepareWebappPath() {
50          final String value = System.getProperty(FESS_WEBAPP_PATH);
51          if (value != null) {
52              return value;
53          }
54          return super.prepareWebappPath();
55      }
56  
57      @Override
58      protected String getMarkDir() {
59          return new File(System.getProperty(JAVA_IO_TMPDIR), "fessboot").getAbsolutePath();
60      }
61  
62      // ===================================================================================
63      //                                                                        main
64      //                                                                        ============
65  
66      public static void main(final String[] args) {
67          // update java.io.tmpdir
68          final String tempPath = System.getProperty(FESS_TEMP_PATH);
69          if (tempPath != null) {
70              System.setProperty(JAVA_IO_TMPDIR, tempPath);
71          }
72  
73          final TomcatBoot tomcatBoot = new FessBoot(getPort(), getContextPath()) //
74                  .useTldDetect(); // for JSP
75          if (tempPath != null) {
76              tomcatBoot.atBaseDir(new File(tempPath, "webapp").getAbsolutePath());
77          }
78          final String tomcatConfigPath = getTomcatConfigPath();
79          if (tomcatConfigPath != null) {
80              tomcatBoot.configure(tomcatConfigPath); // e.g. URIEncoding
81          }
82          tomcatBoot.logging(LOGGING_PROPERTIES, op -> {
83              op.ignoreNoFile();
84              String fessLogPath = System.getProperty("fess.log.path");
85              if (fessLogPath == null) {
86                  fessLogPath = "../../logs";
87              }
88              op.replace("fess.log.path", fessLogPath.replace("\\", "/"));
89          }).asYouLikeIt(resource -> {
90              final Host host = resource.getHost();
91              if (host instanceof StandardHost) {
92                  ((StandardHost) host).setErrorReportValveClass(SuppressErrorReportValve.class.getName());
93              }
94          }).useTldDetect(jarName -> {
95              return jarName.contains("jstl") || jarName.contains("lasta-taglib");
96          }).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) {
118             if ("/".equals(value)) {
119                 return StringUtil.EMPTY;
120             } else {
121                 return value;
122             }
123         }
124         return StringUtil.EMPTY;
125     }
126 
127     protected static String getTomcatConfigPath() {
128         return System.getProperty(TOMCAT_CONFIG_PATH);
129     }
130 }