View Javadoc
1   /*
2    * Copyright 2012-2017 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.codelibs.core.lang.StringUtil;
23  import org.dbflute.tomcat.TomcatBoot;
24  
25  public class FessBoot extends TomcatBoot {
26  
27      private static final String LOGGING_PROPERTIES = "logging.properties";
28  
29      private static final String FESS_CONTEXT_PATH = "fess.context.path";
30  
31      private static final String FESS_PORT = "fess.port";
32  
33      private static final String FESS_TEMP_PATH = "fess.temp.path";
34  
35      private static final String FESS_WEBAPP_PATH = "fess.webapp.path";
36  
37      private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
38  
39      private static final String TOMCAT_CONFIG_PATH = "tomcat.config.path";
40  
41      public FessBoot(final int port, final String contextPath) {
42          super(port, contextPath);
43      }
44  
45      @Override
46      protected String prepareWebappPath() {
47          final String value = System.getProperty(FESS_WEBAPP_PATH);
48          if (value != null) {
49              return value;
50          }
51          return super.prepareWebappPath();
52      }
53  
54      @Override
55      protected String getMarkDir() {
56          return new File(System.getProperty(JAVA_IO_TMPDIR), "fessboot").getAbsolutePath();
57      }
58  
59      // ===================================================================================
60      //                                                                        main
61      //                                                                        ============
62  
63      public static void main(final String[] args) {
64          // update java.io.tmpdir
65          final String tempPath = System.getProperty(FESS_TEMP_PATH);
66          if (tempPath != null) {
67              System.setProperty(JAVA_IO_TMPDIR, tempPath);
68          }
69  
70          final TomcatBoot tomcatBoot = new FessBoot(getPort(), getContextPath()) //
71                  .useTldDetect(); // for JSP
72          if (tempPath != null) {
73              tomcatBoot.atBaseDir(new File(tempPath, "webapp").getAbsolutePath());
74          }
75          final String tomcatConfigPath = getTomcatConfigPath();
76          if (tomcatConfigPath != null) {
77              tomcatBoot.configure(tomcatConfigPath); // e.g. URIEncoding
78          }
79          tomcatBoot.logging(LOGGING_PROPERTIES, op -> {
80              op.ignoreNoFile();
81              String fessLogPath = System.getProperty("fess.log.path");
82              if (fessLogPath == null) {
83                  fessLogPath = "../../logs";
84              }
85              op.replace("fess.log.path", fessLogPath.replace("\\", "/"));
86          }) // uses jdk14logger
87                  .asDevelopment(isNoneEnv()).bootAwait();
88      }
89  
90      public static void shutdown(final String[] args) {
91          System.exit(0);
92      }
93  
94      private static boolean isNoneEnv() {
95          return System.getProperty("lasta.env") == null;
96      }
97  
98      protected static int getPort() {
99          final String value = System.getProperty(FESS_PORT);
100         if (value != null) {
101             return Integer.parseInt(value);
102         }
103         return 8080;
104     }
105 
106     protected static String getContextPath() {
107         final String value = System.getProperty(FESS_CONTEXT_PATH);
108         if (value != null) {
109             if ("/".equals(value)) {
110                 return StringUtil.EMPTY;
111             } else {
112                 return value;
113             }
114         }
115         return StringUtil.EMPTY;
116     }
117 
118     protected static String getTomcatConfigPath() {
119         return System.getProperty(TOMCAT_CONFIG_PATH);
120     }
121 }