TomcatManagementHelperImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2009-2014 the 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 jp.sf.fess.helper.impl;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.LinkedHashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import jp.sf.fess.Constants;
  23. import jp.sf.fess.FessSystemException;
  24. import jp.sf.fess.helper.WebManagementHelper;
  25. import org.apache.http.HttpEntity;
  26. import org.apache.http.HttpResponse;
  27. import org.apache.http.HttpStatus;
  28. import org.apache.http.auth.AuthScope;
  29. import org.apache.http.auth.Credentials;
  30. import org.apache.http.auth.UsernamePasswordCredentials;
  31. import org.apache.http.client.methods.HttpGet;
  32. import org.apache.http.impl.client.DefaultHttpClient;
  33. import org.seasar.framework.util.InputStreamUtil;
  34. import org.codelibs.core.util.StringUtil;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. public class TomcatManagementHelperImpl implements WebManagementHelper {
  38. private static final Logger logger = LoggerFactory
  39. .getLogger(TomcatManagementHelperImpl.class);
  40. protected Map<String, SolrInstance> solrInstanceMap = new LinkedHashMap<String, SolrInstance>();
  41. public void addSolrInstance(final SolrInstance solrInstance) {
  42. if (!solrInstance.isValid()) {
  43. throw new FessTomcatManagerException("SolrInstance is invalid: "
  44. + solrInstance);
  45. }
  46. solrInstanceMap.put(solrInstance.name, solrInstance);
  47. }
  48. /* (non-Javadoc)
  49. * @see jp.sf.fess.helper.WebManagementHelper#hasSolrInstance()
  50. */
  51. @Override
  52. public boolean hasSolrInstance() {
  53. return !solrInstanceMap.isEmpty();
  54. }
  55. /* (non-Javadoc)
  56. * @see jp.sf.fess.helper.WebManagementHelper#getSolrInstanceNameList()
  57. */
  58. @Override
  59. public List<String> getSolrInstanceNameList() {
  60. final List<String> solrInstanceNameList = new ArrayList<String>();
  61. solrInstanceNameList.addAll(solrInstanceMap.keySet());
  62. return solrInstanceNameList;
  63. }
  64. /* (non-Javadoc)
  65. * @see jp.sf.fess.helper.WebManagementHelper#getStatus(java.lang.String)
  66. */
  67. @Override
  68. public String getStatus(final String name) {
  69. final SolrInstance solrInstance = solrInstanceMap.get(name);
  70. if (solrInstance != null) {
  71. try {
  72. return solrInstance.status();
  73. } catch (final Exception e) {
  74. logger.error("System error on a solr instance (" + name + ").",
  75. e);
  76. return "error";
  77. }
  78. }
  79. return "none";
  80. }
  81. /* (non-Javadoc)
  82. * @see jp.sf.fess.helper.WebManagementHelper#start(java.lang.String)
  83. */
  84. @Override
  85. public void start(final String name) {
  86. final SolrInstance solrInstance = solrInstanceMap.get(name);
  87. if (solrInstance != null) {
  88. solrInstance.start();
  89. } else {
  90. throw new FessTomcatManagerException("Solr instance (" + name
  91. + ") is not found.");
  92. }
  93. }
  94. /* (non-Javadoc)
  95. * @see jp.sf.fess.helper.WebManagementHelper#stop(java.lang.String)
  96. */
  97. @Override
  98. public void stop(final String name) {
  99. final SolrInstance solrInstance = solrInstanceMap.get(name);
  100. if (solrInstance != null) {
  101. solrInstance.stop();
  102. } else {
  103. throw new FessTomcatManagerException("Solr instance (" + name
  104. + ") is not found.");
  105. }
  106. }
  107. /* (non-Javadoc)
  108. * @see jp.sf.fess.helper.WebManagementHelper#reload(java.lang.String)
  109. */
  110. @Override
  111. public void reload(final String name) {
  112. final SolrInstance solrInstance = solrInstanceMap.get(name);
  113. if (solrInstance != null) {
  114. solrInstance.reload();
  115. } else {
  116. throw new FessTomcatManagerException("Solr instance (" + name
  117. + ") is not found.");
  118. }
  119. }
  120. public static class SolrInstance {
  121. public String name;
  122. public String contextPath;
  123. public String managerUrl;
  124. public String schema;
  125. public String username;
  126. public String password;
  127. public void start() {
  128. final StringBuilder buf = new StringBuilder();
  129. buf.append(managerUrl);
  130. if (!managerUrl.endsWith("/")) {
  131. buf.append('/');
  132. }
  133. buf.append("start?path=");
  134. buf.append(contextPath);
  135. final String responseBody = getResponseBody(buf.toString());
  136. if (!responseBody.trim().startsWith("OK")) {
  137. throw new FessTomcatManagerException(
  138. "Failed to start a solr instance. The reponse is \n"
  139. + responseBody);
  140. }
  141. }
  142. public void stop() {
  143. final StringBuilder buf = new StringBuilder();
  144. buf.append(managerUrl);
  145. if (!managerUrl.endsWith("/")) {
  146. buf.append('/');
  147. }
  148. buf.append("stop?path=");
  149. buf.append(contextPath);
  150. final String responseBody = getResponseBody(buf.toString());
  151. if (!responseBody.trim().startsWith("OK")) {
  152. throw new FessTomcatManagerException(
  153. "Failed to start a solr instance. The reponse is \n"
  154. + responseBody);
  155. }
  156. }
  157. public void reload() {
  158. final StringBuilder buf = new StringBuilder();
  159. buf.append(managerUrl);
  160. if (!managerUrl.endsWith("/")) {
  161. buf.append('/');
  162. }
  163. buf.append("reload?path=");
  164. buf.append(contextPath);
  165. final String responseBody = getResponseBody(buf.toString());
  166. if (!responseBody.trim().startsWith("OK")) {
  167. throw new FessTomcatManagerException(
  168. "Failed to start a solr instance. The reponse is \n"
  169. + responseBody);
  170. }
  171. }
  172. public String status() {
  173. final StringBuilder buf = new StringBuilder();
  174. buf.append(managerUrl);
  175. if (!managerUrl.endsWith("/")) {
  176. buf.append('/');
  177. }
  178. buf.append("list");
  179. final String responseBody = getResponseBody(buf.toString());
  180. if (!responseBody.trim().startsWith("OK")) {
  181. throw new FessTomcatManagerException(
  182. "Failed to start a solr instance. The reponse is \n"
  183. + responseBody);
  184. }
  185. final String[] lines = responseBody.split("\n");
  186. for (final String line : lines) {
  187. if (line.trim().startsWith(contextPath)) {
  188. final String[] data = line.split(":");
  189. if (data.length > 1) {
  190. return data[1];
  191. }
  192. }
  193. }
  194. return "unknown";
  195. }
  196. protected String getResponseBody(final String url) {
  197. // Create an instance of HttpClient.
  198. final DefaultHttpClient client = new DefaultHttpClient();
  199. if (username != null && password != null) {
  200. final Credentials defaultcreds = new UsernamePasswordCredentials(
  201. username, password);
  202. if (schema == null) {
  203. schema = Constants.BASIC;
  204. }
  205. client.getCredentialsProvider().setCredentials(
  206. new AuthScope(AuthScope.ANY_HOST, -1,
  207. AuthScope.ANY_REALM, schema), defaultcreds);
  208. }
  209. // Create a method instance.
  210. final HttpGet httpGet = new HttpGet(url);
  211. try {
  212. // Execute the method.
  213. final HttpResponse response = client.execute(httpGet);
  214. final int statusCode = response.getStatusLine().getStatusCode();
  215. if (statusCode != HttpStatus.SC_OK) {
  216. throw new FessTomcatManagerException("Could not access "
  217. + url + ". HTTP Status is " + statusCode + ".");
  218. }
  219. final HttpEntity entity = response.getEntity();
  220. if (entity != null) {
  221. // Read the response body.
  222. final String value = new String(
  223. InputStreamUtil.getBytes(entity.getContent()),
  224. Constants.UTF_8);
  225. // Release the connection.
  226. entity.consumeContent();
  227. return value;
  228. }
  229. throw new FessTomcatManagerException("No response from " + url);
  230. } catch (final IOException e) {
  231. throw new FessTomcatManagerException("Fatal transport error: "
  232. + url, e);
  233. } finally {
  234. client.getConnectionManager().shutdown();
  235. }
  236. }
  237. public boolean isValid() {
  238. if (StringUtil.isBlank(name) || StringUtil.isBlank(managerUrl)
  239. || StringUtil.isBlank(contextPath)) {
  240. return false;
  241. }
  242. return true;
  243. }
  244. @Override
  245. public String toString() {
  246. final StringBuilder buf = new StringBuilder();
  247. buf.append("name:").append(name).append(", ");
  248. buf.append("managerUrl:").append(managerUrl).append(", ");
  249. buf.append("contextPath:").append(contextPath).append(", ");
  250. buf.append("schema:").append(schema).append(", ");
  251. buf.append("username:").append(username).append(", ");
  252. buf.append("password:").append(password);
  253. return buf.toString();
  254. }
  255. }
  256. public static class FessTomcatManagerException extends FessSystemException {
  257. private static final long serialVersionUID = 1L;
  258. public FessTomcatManagerException(final String message,
  259. final Throwable cause) {
  260. super(message, cause);
  261. }
  262. public FessTomcatManagerException(final String message) {
  263. super(message);
  264. }
  265. public FessTomcatManagerException(final Throwable cause) {
  266. super(cause);
  267. }
  268. }
  269. }