Commit dd8c1a06 authored by Laurent Martin's avatar Laurent Martin
Browse files

WEBLAB-1456 WEBLAB-1457 add Heritrix class to servers. Refactor ServerManager...

WEBLAB-1456 WEBLAB-1457 add Heritrix class to servers. Refactor ServerManager so that it waits at each step that server instance was started


git-svn-id: svn://svn.forge.objectweb.org/svnroot/weblab/trunk/WebLabApplications/bundle@4291 a803c184-d8de-4a0e-a52b-d3eef88212b6
parent 9376b61c
......@@ -54,6 +54,11 @@
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......
/**
* WEBLAB: Service oriented integration platform for media mining and intelligence applications
*
* Copyright (C) 2004 - 2015 Airbus Defence and Space
*
* This library is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301 USA
*/
package org.ow2.weblab.bundle.server;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.management.MBeanServerConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeLayeredSocketFactory;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.BasicClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.ow2.weblab.bundle.utils.ProcessUtils;
import org.ow2.weblab.bundle.utils.Utils;
/**
* Manage a Heritrix server
* @author asaval
*
*/
public class Heritrix extends WebLabServer {
static {
//for localhost testing only
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){
// TODO: rewrite
@Override
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
if (hostname.equals("localhost")) {
return true;
}
return false;
}
});
}
private String uiHost;
private String commandLineArguments;
private String user;
private String password;
public Heritrix(){}
@Override
public String getProcessIdentificationClue() {
return this.getHome();
}
@Override
public boolean isServerFullyStarted(boolean showDetails) {
if (!isEnabled()) {
this.logger.info("Can not check if " + getName()
+ " is started since control is disabled.");
return true;
}
this.logger.info("Checking " + getName() + " status ... ");
if (showDetails) {
// Connect through the REST port
DefaultHttpClient client_l = null;
try {
client_l = new DefaultHttpClient(connectionManager());
client_l.getCredentialsProvider().setCredentials(
new AuthScope(getUiHost(), getPort()),
new UsernamePasswordCredentials(this.user, this.password));
// Getting information from management server
HttpGet httpget = new HttpGet(new URL("https", getUiHost() , getPort(), "/engine").toURI());
HttpResponse response = client_l.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
// Writing the output from Heritrix directly to the logger
ByteArrayOutputStream bos_l = new ByteArrayOutputStream();
entity.writeTo(bos_l);
this.logger.info(bos_l.toString());
}
} catch (URISyntaxException uriEx_l) {
this.logger.log(Level.SEVERE, "Wrong URL format when talking to WebUI", uriEx_l);
} catch (ClientProtocolException ex) {
this.logger.log(Level.SEVERE, "Wrong protocol while taking to WebUI", ex);
} catch (IOException ioEx_l) {
this.logger.log(Level.SEVERE, "Got an IOException while talking to WebUI", ioEx_l);
} catch (KeyManagementException exception) {
this.logger.log(Level.SEVERE, exception.getMessage());
} catch (NoSuchAlgorithmException exception) {
this.logger.log(Level.SEVERE, exception.getMessage());
} finally {
HttpClientUtils.closeQuietly(client_l);
}
}
return !Utils.isPortAvailable(this.logger, getPort());
}
@Override
public Process start() {
this.logger.info("Starting " + getName());
if (!isEnabled()) {
this.logger.info(getName()
+ " not started since control is disabled.");
return ProcessUtils.dummyProccess();
}
// check status
if (status() != State.STOPPED) {
this.logger.severe(getName() + " is not stopped ! Aborting start...");
return null;
}
// check pid file
if (ProcessUtils.checkPidFile(this.logger, this, false)) {
// server already started; we abort.
this.logger.severe(getName() + " already started ! Aborting ...");
return null;
}
String command = getScript().trim()+" "+this.commandLineArguments.trim();
this.logger.info(getName() + " is starting ...");
if (!isEnabled()){
this.logger.info(getName()+" discarded commmand "+command+" since control is disabled.");
return ProcessUtils.dummyProccess();
}
return ProcessUtils.runProcess(this.logger, this.getBinDirectory(), getEnv(), command.split(" "));
}
@Override
public void stop() {
// Talking to WebUI of Heritrix to stop it
DefaultHttpClient client_l = null;
try {
client_l = new DefaultHttpClient(connectionManager());
client_l.getCredentialsProvider().setCredentials(
new AuthScope(getUiHost(), getPort()),
new UsernamePasswordCredentials(this.user, this.password));
//posting the shutdown request
HttpPost httppost = new HttpPost(new URL("https", getUiHost(), getPort(), "/engine").toURI());
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("I'm sure", "on"));
nameValuePairs.add(new BasicNameValuePair("action", "Exit Java Process"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client_l.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
// Writing the output from Heritrix directly to the logger
ByteArrayOutputStream bos_l = new ByteArrayOutputStream();
entity.writeTo(bos_l);
this.logger.info(bos_l.toString());
}
//We don't care about response
this.logger.info(getName() + " stopped");
} catch (URISyntaxException uriEx_l) {
this.logger.log(Level.SEVERE, "Wrong URL format when talking to Heritrix WebUI", uriEx_l);
} catch (ClientProtocolException ex) {
this.logger.log(Level.SEVERE, "Wrong protocol while taking to Heritrix WebUI", ex);
} catch (IOException ioEx_l) {
this.logger.log(Level.SEVERE, ioEx_l.getMessage());
} catch (KeyManagementException exception) {
this.logger.log(Level.SEVERE, exception.getMessage());
} catch (NoSuchAlgorithmException exception) {
this.logger.log(Level.SEVERE, exception.getMessage());
} finally {
HttpClientUtils.closeQuietly(client_l);
}
}
/**
* Execute a command
*
* @param command
* The name of the command called
* @return The started process description (if any)
*/
public Process execute(String command) {
if (!isEnabled()) {
this.logger.info(getName() + " discarded command " + command + " since control is disabled.");
return ProcessUtils.dummyProccess();
}
if ("stop".equals(command) || "restart".equals(command)) {
// stop
stop();
}
if ("start".equals(command) || "restart".equals(command)) {
// start
return start();
}
return null;
}
public String getUiHost() {
return this.uiHost;
}
public void setUiHost(String uiHost) {
this.uiHost = uiHost;
}
public String getUser() {
return this.user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCommandLineArguments() {
return this.commandLineArguments;
}
public void setCommandLineArguments(String commandLineArguments) {
this.commandLineArguments = commandLineArguments;
}
@Override
public MBeanServerConnection getMBeanServerConnection() {
return null;
}
private ClientConnectionManager connectionManager() throws NoSuchAlgorithmException, KeyManagementException{
SSLContext sslContext = SSLContext.getInstance("SSL");
// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
String authType) throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
String authType) throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
}}, new SecureRandom());
// TODO add certificate exception if not on the good host / port, do NOT use this code in production !
SchemeLayeredSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme httpsScheme = new Scheme("https", 443, sf);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(httpsScheme);
// apache HttpClient version >4.2 should use BasicClientConnectionManager
return new BasicClientConnectionManager(schemeRegistry);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment