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.job;
17  
18  import java.io.File;
19  import java.io.FilenameFilter;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.codelibs.fess.util.ComponentUtil;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  public abstract class ExecJob {
29  
30      private static final Logger logger = LoggerFactory.getLogger(ExecJob.class);
31  
32      protected JobExecutor jobExecutor;
33  
34      protected String sessionId;
35  
36      protected boolean useLocalElasticsearch = true;
37  
38      protected String logFilePath;
39  
40      protected String logLevel;
41  
42      protected List<String> jvmOptions = new ArrayList<>();
43  
44      protected String lastaEnv;
45  
46      public abstract String execute();
47  
48      protected abstract String getExecuteType();
49  
50      public ExecJob jobExecutor(final JobExecutor jobExecutor) {
51          this.jobExecutor = jobExecutor;
52          return this;
53      }
54  
55      public ExecJob sessionId(final String sessionId) {
56          this.sessionId = sessionId;
57          return this;
58      }
59  
60      public ExecJob logFilePath(final String logFilePath) {
61          this.logFilePath = logFilePath;
62          return this;
63      }
64  
65      public ExecJob logLevel(final String logLevel) {
66          this.logLevel = logLevel;
67          return this;
68      }
69  
70      public ExecJob useLocalElasticsearch(final boolean useLocalElasticsearch) {
71          this.useLocalElasticsearch = useLocalElasticsearch;
72          return this;
73      }
74  
75      public ExecJob remoteDebug() {
76          return jvmOptions("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=localhost:8000");
77      }
78  
79      public ExecJob gcLogging() {
80          final StringBuilder buf = new StringBuilder(100);
81          buf.append("-Xlog:gc*,gc+age=trace,safepoint:file=");
82          if (logFilePath != null) {
83              buf.append(logFilePath);
84          } else {
85              buf.append(ComponentUtil.getSystemHelper().getLogFilePath());
86          }
87          buf.append(File.separator);
88          buf.append("gc-").append(getExecuteType()).append(".log");
89          buf.append(":utctime,pid,tags:filecount=32,filesize=64m");
90          return jvmOptions(buf.toString());
91      }
92  
93      public ExecJob jvmOptions(final String... options) {
94          for (final String s : options) {
95              this.jvmOptions.add(s);
96          }
97          return this;
98      }
99  
100     public ExecJob lastaEnv(final String env) {
101         this.lastaEnv = env;
102         return this;
103     }
104 
105     protected void addSystemProperty(final List<String> cmdList, final String name, final String defaultValue, final String appendValue) {
106         final String value = System.getProperty(name);
107         if (value != null) {
108             final StringBuilder buf = new StringBuilder();
109             buf.append("-D").append(name).append("=").append(value);
110             if (appendValue != null) {
111                 buf.append(appendValue);
112             }
113             cmdList.add(buf.toString());
114         } else if (defaultValue != null) {
115             cmdList.add("-D" + name + "=" + defaultValue);
116         }
117     }
118 
119     protected void deleteTempDir(final File ownTmpDir) {
120         if (ownTmpDir == null) {
121             return;
122         }
123         if (!FileUtils.deleteQuietly(ownTmpDir)) {
124             logger.warn("Could not delete a temp dir: " + ownTmpDir.getAbsolutePath());
125         }
126     }
127 
128     protected void appendJarFile(final String cpSeparator, final StringBuilder buf, final File libDir, final String basePath) {
129         final File[] jarFiles = libDir.listFiles((FilenameFilter) (dir, name) -> name.toLowerCase().endsWith(".jar"));
130         if (jarFiles != null) {
131             for (final File file : jarFiles) {
132                 buf.append(cpSeparator);
133                 buf.append(basePath);
134                 buf.append(file.getName());
135             }
136         }
137     }
138 
139 }