SystemHelper.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright 2009-2013 the Fess 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 jp.sf.fess.helper;
  17. import java.io.File;
  18. import java.io.FilenameFilter;
  19. import java.io.Serializable;
  20. import java.io.UnsupportedEncodingException;
  21. import java.net.URLEncoder;
  22. import java.sql.Timestamp;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import java.util.Map;
  28. import java.util.UUID;
  29. import java.util.concurrent.atomic.AtomicBoolean;
  30. import jp.sf.fess.Constants;
  31. import jp.sf.fess.FessSystemException;
  32. import jp.sf.fess.db.exentity.RoleType;
  33. import jp.sf.fess.service.RoleTypeService;
  34. import jp.sf.fess.util.ResourceUtil;
  35. import org.apache.commons.lang.StringUtils;
  36. import org.codelibs.solr.lib.SolrGroup;
  37. import org.codelibs.solr.lib.policy.QueryType;
  38. import org.codelibs.solr.lib.policy.StatusPolicy;
  39. import org.seasar.framework.container.SingletonS2Container;
  40. import org.seasar.framework.container.annotation.tiger.InitMethod;
  41. import org.seasar.framework.util.FileUtil;
  42. import org.seasar.framework.util.StringUtil;
  43. import org.seasar.robot.util.CharUtil;
  44. import org.seasar.struts.util.RequestUtil;
  45. import org.seasar.struts.util.ServletContextUtil;
  46. import org.slf4j.Logger;
  47. import org.slf4j.LoggerFactory;
  48. public class SystemHelper implements Serializable {
  49. private static final long serialVersionUID = 1L;
  50. private static final Logger logger = LoggerFactory
  51. .getLogger(SystemHelper.class);
  52. private String adminRole = "fess";
  53. private String[] crawlerJavaOptions = new String[] {
  54. "-Djava.awt.headless=true", "-server", "-Xmx512m",
  55. "-XX:MaxPermSize=128m", "-XX:-UseGCOverheadLimit",
  56. "-XX:+UseConcMarkSweepGC", "-XX:CMSInitiatingOccupancyFraction=75",
  57. "-XX:+CMSIncrementalMode", "-XX:+CMSIncrementalPacing",
  58. "-XX:CMSIncrementalDutyCycleMin=0", "-XX:+UseParNewGC",
  59. "-XX:+UseStringCache", "-XX:+UseTLAB", "-XX:+DisableExplicitGC" };
  60. private String logFilePath = System.getProperty("fess.log.file");
  61. private String solrHome = System.getProperty("solr.solr.home");
  62. private String javaCommandPath = "java";
  63. private String filterPathEncoding = Constants.UTF_8;
  64. private boolean useOwnTmpDir = true;
  65. private String baseHelpLink = "http://fess.codelibs.org/{lang}/"
  66. + Constants.MAJOR_VERSION + ".0/admin/";
  67. private String[] supportedHelpLangs = new String[] { "ja" };
  68. private final Map<String, String> designJspFileNameMap = new HashMap<String, String>();
  69. private String[] supportedUploadedJSExtentions = new String[] { "js" };
  70. private String[] supportedUploadedCssExtentions = new String[] { "css" };
  71. private String[] supportedUploadedMediaExtentions = new String[] { "jpg",
  72. "jpeg", "gif", "png", "swf" };
  73. private String jarDir = "/jar/";
  74. private String launcherFileNamePrefix = "fess-launcher-";
  75. private int maxTextLength = 4000;
  76. // readonly
  77. private String launcherJarPath;
  78. // readonly
  79. private String launcherJnlpPath;
  80. private final AtomicBoolean forceStop = new AtomicBoolean(false);
  81. public String favoriteCountField = "favoriteCount_l_x_dv";
  82. public String clickCountField = "clickCount_l_x_dv";
  83. public String screenshotField = "screenshot_s_s";
  84. public String configIdField = "cid_s_s";
  85. public String expiresField = "expires_dt";
  86. public String urlField = "url";
  87. public String docIdField = "docId";
  88. public String idField = "id";
  89. @InitMethod
  90. public void init() {
  91. final File[] files = ResourceUtil.getJarFiles(launcherFileNamePrefix);
  92. if (files.length > 0) {
  93. final String fileName = files[0].getName();
  94. final String jarPath = ServletContextUtil.getServletContext()
  95. .getRealPath(jarDir);
  96. final File[] jarFiles = new File(jarPath)
  97. .listFiles(new FilenameFilter() {
  98. @Override
  99. public boolean accept(final File dir, final String name) {
  100. return name.startsWith(launcherFileNamePrefix);
  101. }
  102. });
  103. if (jarFiles != null) {
  104. for (final File jarFile : jarFiles) {
  105. if (jarFile.exists() && !jarFile.delete()) {
  106. logger.warn("Could not delete "
  107. + jarFile.getAbsolutePath());
  108. }
  109. }
  110. }
  111. final File launcherJarFile = new File(jarPath, fileName);
  112. final File parentLauncherJarFile = launcherJarFile.getParentFile();
  113. if (!parentLauncherJarFile.exists()
  114. && !parentLauncherJarFile.mkdirs()) {
  115. logger.warn("Could not create "
  116. + parentLauncherJarFile.getAbsolutePath());
  117. }
  118. FileUtil.copy(files[0], launcherJarFile);
  119. launcherJarPath = jarDir + fileName;
  120. launcherJnlpPath = launcherJarPath.replace(".jar", ".jnlp");
  121. final File launcherJnlpFile = new File(launcherJarFile
  122. .getAbsolutePath().replace(".jar", ".jnlp"));
  123. final File jnlpTemplateFile = new File(
  124. ResourceUtil.getOrigPath("jnlp/fess-launcher.jnlp"));
  125. if (!jnlpTemplateFile.isFile()) {
  126. throw new FessSystemException(
  127. jnlpTemplateFile.getAbsolutePath() + " is not found.");
  128. }
  129. try {
  130. String content = new String(
  131. FileUtil.getBytes(jnlpTemplateFile), Constants.UTF_8);
  132. content = content.replace("${launcherJarFile}", fileName);
  133. FileUtil.write(launcherJnlpFile.getAbsolutePath(),
  134. content.getBytes(Constants.UTF_8));
  135. } catch (final UnsupportedEncodingException e) {
  136. throw new FessSystemException("Could not write "
  137. + jnlpTemplateFile.getAbsolutePath(), e);
  138. }
  139. }
  140. }
  141. public String getUsername() {
  142. String username = RequestUtil.getRequest().getRemoteUser();
  143. if (StringUtil.isBlank(username)) {
  144. username = "guest";
  145. }
  146. return username;
  147. }
  148. public Timestamp getCurrentTimestamp() {
  149. return new Timestamp(System.currentTimeMillis());
  150. }
  151. public String getLogFilePath() {
  152. return logFilePath;
  153. }
  154. public void setLogFilePath(final String logFilePath) {
  155. this.logFilePath = logFilePath;
  156. }
  157. public String encodeUrlFilter(final String path) {
  158. if (filterPathEncoding == null || path == null) {
  159. return path;
  160. }
  161. try {
  162. final StringBuilder buf = new StringBuilder();
  163. for (int i = 0; i < path.length(); i++) {
  164. final char c = path.charAt(i);
  165. if (CharUtil.isUrlChar(c) || c == '^' || c == '{' || c == '}'
  166. || c == '|' || c == '\\') {
  167. buf.append(c);
  168. } else {
  169. buf.append(URLEncoder.encode(String.valueOf(c),
  170. filterPathEncoding));
  171. }
  172. }
  173. return buf.toString();
  174. } catch (final UnsupportedEncodingException e) {
  175. return path;
  176. }
  177. }
  178. public String getHelpLink(final String name) {
  179. final Locale locale = RequestUtil.getRequest().getLocale();
  180. if (locale != null) {
  181. final String lang = locale.getLanguage();
  182. for (final String l : supportedHelpLangs) {
  183. if (l.equals(lang)) {
  184. final String url = baseHelpLink + name + "-guide.html";
  185. return url.replaceAll("\\{lang\\}", lang);
  186. }
  187. }
  188. }
  189. return null;
  190. }
  191. public void addDesignJspFileName(final String key, final String value) {
  192. designJspFileNameMap.put(key, value);
  193. }
  194. public String getDesignJspFileName(final String fileName) {
  195. return designJspFileNameMap.get(fileName);
  196. }
  197. public String getAdminRole() {
  198. return adminRole;
  199. }
  200. public void setAdminRole(final String adminRole) {
  201. this.adminRole = adminRole;
  202. }
  203. public List<String> getAuthenticatedRoleList() {
  204. final RoleTypeService roleTypeService = SingletonS2Container
  205. .getComponent(RoleTypeService.class);
  206. final List<RoleType> roleTypeList = roleTypeService.getRoleTypeList();
  207. final List<String> roleList = new ArrayList<String>(roleTypeList.size());
  208. for (final RoleType roleType : roleTypeList) {
  209. roleList.add(roleType.getValue());
  210. }
  211. return roleList;
  212. }
  213. public String[] getCrawlerJavaOptions() {
  214. return crawlerJavaOptions;
  215. }
  216. public void setCrawlerJavaOptions(final String[] crawlerJavaOptions) {
  217. this.crawlerJavaOptions = crawlerJavaOptions;
  218. }
  219. public String getSolrHome() {
  220. return solrHome;
  221. }
  222. public void setSolrHome(final String solrHome) {
  223. this.solrHome = solrHome;
  224. }
  225. public String getJavaCommandPath() {
  226. return javaCommandPath;
  227. }
  228. public void setJavaCommandPath(final String javaCommandPath) {
  229. this.javaCommandPath = javaCommandPath;
  230. }
  231. /**
  232. * @return the filterPathEncoding
  233. */
  234. public String getFilterPathEncoding() {
  235. return filterPathEncoding;
  236. }
  237. /**
  238. * @param filterPathEncoding the filterPathEncoding to set
  239. */
  240. public void setFilterPathEncoding(final String filterPathEncoding) {
  241. this.filterPathEncoding = filterPathEncoding;
  242. }
  243. /**
  244. * @return the useOwnTmpDir
  245. */
  246. public boolean isUseOwnTmpDir() {
  247. return useOwnTmpDir;
  248. }
  249. /**
  250. * @param useOwnTmpDir the useOwnTmpDir to set
  251. */
  252. public void setUseOwnTmpDir(final boolean useOwnTmpDir) {
  253. this.useOwnTmpDir = useOwnTmpDir;
  254. }
  255. /**
  256. * @return the baseHelpLink
  257. */
  258. public String getBaseHelpLink() {
  259. return baseHelpLink;
  260. }
  261. /**
  262. * @param baseHelpLink the baseHelpLink to set
  263. */
  264. public void setBaseHelpLink(final String baseHelpLink) {
  265. this.baseHelpLink = baseHelpLink;
  266. }
  267. /**
  268. * @return the supportedHelpLangs
  269. */
  270. public String[] getSupportedHelpLangs() {
  271. return supportedHelpLangs;
  272. }
  273. /**
  274. * @param supportedHelpLangs the supportedHelpLangs to set
  275. */
  276. public void setSupportedHelpLangs(final String[] supportedHelpLangs) {
  277. this.supportedHelpLangs = supportedHelpLangs;
  278. }
  279. public String[] getSupportedUploadedJSExtentions() {
  280. return supportedUploadedJSExtentions;
  281. }
  282. public void setSupportedUploadedJSExtentions(
  283. final String[] supportedUploadedJSExtentions) {
  284. this.supportedUploadedJSExtentions = supportedUploadedJSExtentions;
  285. }
  286. public String[] getSupportedUploadedCssExtentions() {
  287. return supportedUploadedCssExtentions;
  288. }
  289. public void setSupportedUploadedCssExtentions(
  290. final String[] supportedUploadedCssExtentions) {
  291. this.supportedUploadedCssExtentions = supportedUploadedCssExtentions;
  292. }
  293. public String[] getSupportedUploadedMediaExtentions() {
  294. return supportedUploadedMediaExtentions;
  295. }
  296. public void setSupportedUploadedMediaExtentions(
  297. final String[] supportedUploadedMediaExtentions) {
  298. this.supportedUploadedMediaExtentions = supportedUploadedMediaExtentions;
  299. }
  300. public String getJarDir() {
  301. return jarDir;
  302. }
  303. public void setJarDir(final String jarDir) {
  304. this.jarDir = jarDir;
  305. }
  306. public String getLauncherFileNamePrefix() {
  307. return launcherFileNamePrefix;
  308. }
  309. public void setLauncherFileNamePrefix(final String launcherFileNamePrefix) {
  310. this.launcherFileNamePrefix = launcherFileNamePrefix;
  311. }
  312. public String getLauncherJarPath() {
  313. return launcherJarPath;
  314. }
  315. public String getLauncherJnlpPath() {
  316. return launcherJnlpPath;
  317. }
  318. public int getMaxTextLength() {
  319. return maxTextLength;
  320. }
  321. public void setMaxTextLength(final int maxTextLength) {
  322. this.maxTextLength = maxTextLength;
  323. }
  324. public void updateStatus(final SolrGroup solrGroup,
  325. final QueryType queryType) {
  326. final StatusPolicy statusPolicy = solrGroup.getStatusPolicy();
  327. for (final String serverName : solrGroup.getServerNames()) {
  328. statusPolicy.activate(queryType, serverName);
  329. }
  330. }
  331. public boolean isForceStop() {
  332. return forceStop.get();
  333. }
  334. public void setForceStop(final boolean b) {
  335. forceStop.set(true);
  336. }
  337. public String generateDocId(final Map<String, Object> map) {
  338. return UUID.randomUUID().toString().replace("-", "");
  339. }
  340. public String abbreviateLongText(final String str) {
  341. return StringUtils.abbreviate(str, maxTextLength);
  342. }
  343. }