123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /*
- * Copyright 2009-2014 the CodeLibs Project and the Others.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
- */
- package jp.sf.fess.helper.impl;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import jp.sf.fess.Constants;
- import jp.sf.fess.FessSystemException;
- import jp.sf.fess.helper.WebManagementHelper;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.Credentials;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.seasar.framework.util.InputStreamUtil;
- import org.codelibs.core.util.StringUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class TomcatManagementHelperImpl implements WebManagementHelper {
- private static final Logger logger = LoggerFactory
- .getLogger(TomcatManagementHelperImpl.class);
- protected Map<String, SolrInstance> solrInstanceMap = new LinkedHashMap<String, SolrInstance>();
- public void addSolrInstance(final SolrInstance solrInstance) {
- if (!solrInstance.isValid()) {
- throw new FessTomcatManagerException("SolrInstance is invalid: "
- + solrInstance);
- }
- solrInstanceMap.put(solrInstance.name, solrInstance);
- }
- /* (non-Javadoc)
- * @see jp.sf.fess.helper.WebManagementHelper#hasSolrInstance()
- */
- @Override
- public boolean hasSolrInstance() {
- return !solrInstanceMap.isEmpty();
- }
- /* (non-Javadoc)
- * @see jp.sf.fess.helper.WebManagementHelper#getSolrInstanceNameList()
- */
- @Override
- public List<String> getSolrInstanceNameList() {
- final List<String> solrInstanceNameList = new ArrayList<String>();
- solrInstanceNameList.addAll(solrInstanceMap.keySet());
- return solrInstanceNameList;
- }
- /* (non-Javadoc)
- * @see jp.sf.fess.helper.WebManagementHelper#getStatus(java.lang.String)
- */
- @Override
- public String getStatus(final String name) {
- final SolrInstance solrInstance = solrInstanceMap.get(name);
- if (solrInstance != null) {
- try {
- return solrInstance.status();
- } catch (final Exception e) {
- logger.error("System error on a solr instance (" + name + ").",
- e);
- return "error";
- }
- }
- return "none";
- }
- /* (non-Javadoc)
- * @see jp.sf.fess.helper.WebManagementHelper#start(java.lang.String)
- */
- @Override
- public void start(final String name) {
- final SolrInstance solrInstance = solrInstanceMap.get(name);
- if (solrInstance != null) {
- solrInstance.start();
- } else {
- throw new FessTomcatManagerException("Solr instance (" + name
- + ") is not found.");
- }
- }
- /* (non-Javadoc)
- * @see jp.sf.fess.helper.WebManagementHelper#stop(java.lang.String)
- */
- @Override
- public void stop(final String name) {
- final SolrInstance solrInstance = solrInstanceMap.get(name);
- if (solrInstance != null) {
- solrInstance.stop();
- } else {
- throw new FessTomcatManagerException("Solr instance (" + name
- + ") is not found.");
- }
- }
- /* (non-Javadoc)
- * @see jp.sf.fess.helper.WebManagementHelper#reload(java.lang.String)
- */
- @Override
- public void reload(final String name) {
- final SolrInstance solrInstance = solrInstanceMap.get(name);
- if (solrInstance != null) {
- solrInstance.reload();
- } else {
- throw new FessTomcatManagerException("Solr instance (" + name
- + ") is not found.");
- }
- }
- public static class SolrInstance {
- public String name;
- public String contextPath;
- public String managerUrl;
- public String schema;
- public String username;
- public String password;
- public void start() {
- final StringBuilder buf = new StringBuilder();
- buf.append(managerUrl);
- if (!managerUrl.endsWith("/")) {
- buf.append('/');
- }
- buf.append("start?path=");
- buf.append(contextPath);
- final String responseBody = getResponseBody(buf.toString());
- if (!responseBody.trim().startsWith("OK")) {
- throw new FessTomcatManagerException(
- "Failed to start a solr instance. The reponse is \n"
- + responseBody);
- }
- }
- public void stop() {
- final StringBuilder buf = new StringBuilder();
- buf.append(managerUrl);
- if (!managerUrl.endsWith("/")) {
- buf.append('/');
- }
- buf.append("stop?path=");
- buf.append(contextPath);
- final String responseBody = getResponseBody(buf.toString());
- if (!responseBody.trim().startsWith("OK")) {
- throw new FessTomcatManagerException(
- "Failed to start a solr instance. The reponse is \n"
- + responseBody);
- }
- }
- public void reload() {
- final StringBuilder buf = new StringBuilder();
- buf.append(managerUrl);
- if (!managerUrl.endsWith("/")) {
- buf.append('/');
- }
- buf.append("reload?path=");
- buf.append(contextPath);
- final String responseBody = getResponseBody(buf.toString());
- if (!responseBody.trim().startsWith("OK")) {
- throw new FessTomcatManagerException(
- "Failed to start a solr instance. The reponse is \n"
- + responseBody);
- }
- }
- public String status() {
- final StringBuilder buf = new StringBuilder();
- buf.append(managerUrl);
- if (!managerUrl.endsWith("/")) {
- buf.append('/');
- }
- buf.append("list");
- final String responseBody = getResponseBody(buf.toString());
- if (!responseBody.trim().startsWith("OK")) {
- throw new FessTomcatManagerException(
- "Failed to start a solr instance. The reponse is \n"
- + responseBody);
- }
- final String[] lines = responseBody.split("\n");
- for (final String line : lines) {
- if (line.trim().startsWith(contextPath)) {
- final String[] data = line.split(":");
- if (data.length > 1) {
- return data[1];
- }
- }
- }
- return "unknown";
- }
- protected String getResponseBody(final String url) {
- // Create an instance of HttpClient.
- final DefaultHttpClient client = new DefaultHttpClient();
- if (username != null && password != null) {
- final Credentials defaultcreds = new UsernamePasswordCredentials(
- username, password);
- if (schema == null) {
- schema = Constants.BASIC;
- }
- client.getCredentialsProvider().setCredentials(
- new AuthScope(AuthScope.ANY_HOST, -1,
- AuthScope.ANY_REALM, schema), defaultcreds);
- }
- // Create a method instance.
- final HttpGet httpGet = new HttpGet(url);
- try {
- // Execute the method.
- final HttpResponse response = client.execute(httpGet);
- final int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode != HttpStatus.SC_OK) {
- throw new FessTomcatManagerException("Could not access "
- + url + ". HTTP Status is " + statusCode + ".");
- }
- final HttpEntity entity = response.getEntity();
- if (entity != null) {
- // Read the response body.
- final String value = new String(
- InputStreamUtil.getBytes(entity.getContent()),
- Constants.UTF_8);
- // Release the connection.
- entity.consumeContent();
- return value;
- }
- throw new FessTomcatManagerException("No response from " + url);
- } catch (final IOException e) {
- throw new FessTomcatManagerException("Fatal transport error: "
- + url, e);
- } finally {
- client.getConnectionManager().shutdown();
- }
- }
- public boolean isValid() {
- if (StringUtil.isBlank(name) || StringUtil.isBlank(managerUrl)
- || StringUtil.isBlank(contextPath)) {
- return false;
- }
- return true;
- }
- @Override
- public String toString() {
- final StringBuilder buf = new StringBuilder();
- buf.append("name:").append(name).append(", ");
- buf.append("managerUrl:").append(managerUrl).append(", ");
- buf.append("contextPath:").append(contextPath).append(", ");
- buf.append("schema:").append(schema).append(", ");
- buf.append("username:").append(username).append(", ");
- buf.append("password:").append(password);
- return buf.toString();
- }
- }
- public static class FessTomcatManagerException extends FessSystemException {
- private static final long serialVersionUID = 1L;
- public FessTomcatManagerException(final String message,
- final Throwable cause) {
- super(message, cause);
- }
- public FessTomcatManagerException(final String message) {
- super(message);
- }
- public FessTomcatManagerException(final Throwable cause) {
- super(cause);
- }
- }
- }
|