Commit aece0c78 authored by Yann Mombrun's avatar Yann Mombrun

Reduce verbosity of various things. Update Javadoc and code style.

Add new parameter to activemq to prevent from error in logs
parent ae3f75ae
Pipeline #2944 passed with stages
in 46 minutes and 22 seconds
......@@ -33,6 +33,11 @@ public abstract class AbstractApplicationServer extends WebLabServer {
protected abstract String[] getStopScript();
/**
* Get start script.
*
* @return Return start script.
*/
protected abstract String[] getStartScript();
......
......@@ -17,6 +17,7 @@
*/
package org.ow2.weblab.bundle.server;
import java.io.File;
import java.io.IOException;
import javax.management.MBeanServerConnection;
......@@ -42,16 +43,39 @@ public class ActiveMQServer extends AbstractApplicationServer {
private String processIdentificationClue;
private String dataDir;
/**
* The default constructor
*/
public ActiveMQServer() {
this.setMandatory(true);
}
/**
* Ensure data folder exists (to prevent warning in activemq start script)
*/
@Override
protected void checkConfiguration() {
if (this.dataDir == null) {
this.logger.debug("No dataDir property configured for activemq. There will be a few logs in activemq start script.");
} else {
final File theDataDir = new File(this.dataDir);
if (!theDataDir.exists() && !theDataDir.mkdir()) {
this.logger.warn("Unable to create data directory for activemq at " + theDataDir.getAbsolutePath());
}
}
}
@Override
protected String[] getStartScript() {
final String ext = System.getProperty("os.name").contains("Windows") ? ".bat" : "";
return new String[] { PathUtils.createPath(this.getHome(), "bin") + "activemq" + ext, WebLabServer.START };
}
......@@ -59,6 +83,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
@Override
protected String[] getStopScript() {
final String ext = System.getProperty("os.name").contains("Windows") ? ".bat" : "";
return new String[] { PathUtils.createPath(this.getHome(), "bin") + "activemq" + ext, WebLabServer.STOP };
}
......@@ -66,6 +91,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
@Override
public JMXConnector getJMXConnector() {
return ProcessUtils.createJMXclient(this.getHost(), this.getJmxPort(), "", "", "jmxrmi", 2000, false, this.logger);
}
......@@ -73,6 +99,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
@Override
public String getProcessIdentificationClue() {
return this.processIdentificationClue;
}
......@@ -82,18 +109,21 @@ public class ActiveMQServer extends AbstractApplicationServer {
* the processIdentificationClue to set
*/
public void setProcessIdentificationClue(final String processIdentificationClue) {
this.processIdentificationClue = processIdentificationClue;
}
@Override
public String supportedCommands() {
return this.getName() + " [start|stop|restart|status|[queue] purge] \t will start, stop and give information on " + this.getName() + " current status. Last command purges a given queue.\n";
}
@Override
public boolean isServerFullyStarted(final boolean showDetails) {
if (!this.isEnabled()) {
this.logger.info("Can not check if " + this.getName() + " is started since control is disabled.");
return true;
......@@ -117,6 +147,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
* @return The list of queues
*/
public ObjectName[] listQueues(final MBeanServerConnection connection) {
final ObjectName activeMQ;
try {
activeMQ = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + this.getHost());
......@@ -132,6 +163,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
private void listQueuesStatus() {
try (final JMXConnector connector = this.getJMXConnector()) {
final MBeanServerConnection connection = connector.getMBeanServerConnection();
if (connection == null) {
......@@ -154,6 +186,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
* The name of the queue to purge. If null, then purge all queues
*/
public void purge(final String queue) {
boolean queueFound = false;
try (final JMXConnector connector = this.getJMXConnector()) {
final MBeanServerConnection connection = connector.getMBeanServerConnection();
......@@ -163,7 +196,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
}
for (final ObjectName queueName : this.listQueues(connection)) {
final QueueViewMBean queueMbean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
if (queue == null || queue.equals(queueMbean.getName())) {
if ((queue == null) || queue.equals(queueMbean.getName())) {
queueFound = true;
if (queueMbean.getQueueSize() > 0) {
this.logger.info("Purging queue " + queueMbean.getName() + ".");
......@@ -183,4 +216,23 @@ public class ActiveMQServer extends AbstractApplicationServer {
}
}
/**
* @return the dataDir
*/
public String getDataDir() {
return this.dataDir;
}
/**
* @param dataDir
* the dataDir to set
*/
public void setDataDir(final String dataDir) {
this.dataDir = dataDir;
}
}
......@@ -93,7 +93,7 @@ public class SolrServer extends AbstractApplicationServer {
public SolrServer() {
// this.setMandatory(true);
// Empty
}
......@@ -203,10 +203,10 @@ public class SolrServer extends AbstractApplicationServer {
}
@Override
/**
* Check that configuration is OK and store it in shared zookeeper server
*/
@Override
protected void checkConfiguration() {
if (this.zookeeperEnsemble == null) {
this.logger.debug("No zookeeper ensemble defined. No solr configuration uploaded on zookeeper.");
......@@ -292,7 +292,15 @@ public class SolrServer extends AbstractApplicationServer {
// get list of currently existing collection
try {
final List<String> existingCollections = SolrUtils.getCollections(this.solrClient);
List<String> existingCollections;
try {
existingCollections = SolrUtils.getCollections(this.solrClient);
} catch (final Exception e) {
this.logger.debug("Error accessing collections. Retrying one second later before ignoring it.");
Thread.yield();
Thread.sleep(1000);
existingCollections = SolrUtils.getCollections(this.solrClient);
}
if (existingCollections != null) {
for (final Entry<String, HashMap<String, String>> collection : this.initCollections.entrySet()) {
final String collectionName = collection.getKey();
......@@ -318,12 +326,12 @@ public class SolrServer extends AbstractApplicationServer {
}
}
} catch (final Exception e) {
this.logger.error("error while creating inital collections in solr", e);
this.logger.error("Error while retrieving or creating inital collections in solr", e);
} finally {
try {
this.solrClient.close();
} catch (final IOException e) {
this.logger.error("error while closing solr client", e);
} catch (final IOException ioe) {
this.logger.error("error while closing solr client", ioe);
}
}
}
......
......@@ -112,105 +112,126 @@ public abstract class WebLabServer {
public WebLabServer() {
}
public String getHome() {
return this.home;
}
public void setHome(final String home) {
this.home = home;
}
public int getPort() {
return this.port;
}
public void setPort(final int port) {
this.port = port;
}
public int getJmxPort() {
return this.jmxPort;
}
public void setJmxPort(final int jmxPort) {
this.jmxPort = jmxPort;
}
public int getAjpPort() {
return this.ajpPort;
}
public void setAjpPort(final int ajpPort) {
this.ajpPort = ajpPort;
}
public int getTimeout() {
return this.timeout;
}
public void setTimeout(final int timeout) {
this.timeout = timeout;
}
public String getBinDirectory() {
return this.binDirectory;
}
public void setBinDirectory(final String binDirectory) {
this.binDirectory = binDirectory;
}
public String getName() {
return this.name;
}
public void setName(final String name) {
this.name = name;
}
public String getScript() {
return this.script;
}
public void setScript(final String script) {
this.script = script;
}
public Env getEnv() {
return this.env;
}
public void setEnv(final Env env) {
this.env = env;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
......@@ -219,6 +240,7 @@ public abstract class WebLabServer {
* @return the mandatory
*/
public boolean isMandatory() {
return this.mandatory;
}
......@@ -228,15 +250,25 @@ public abstract class WebLabServer {
* the mandatory to set
*/
public void setMandatory(final boolean mandatory) {
this.mandatory = mandatory;
}
/**
* @param subDir
* The name of the sub directory inside home
* @return Absolute path to sub directory
*/
public String getSubDir(final String subDir) {
return PathUtils.createPath(this.home, subDir);
}
/**
* @return Something to be grepped in ps aux
*/
public abstract String getProcessIdentificationClue();
......@@ -250,6 +282,7 @@ public abstract class WebLabServer {
* @return the process executing the command
*/
public Process execute(final Env env_p, final String command) {
if (!this.isEnabled()) {
this.logger.info(this.getName() + " discarded command " + command + " since control is disabled.");
return ProcessUtils.dummyProccess();
......@@ -272,6 +305,7 @@ public abstract class WebLabServer {
* @return server process status
*/
public State status() {
State status = State.UNDEFINED;
try {
if (-1 == ProcessUtils.getProcessId(this.logger, this)) {
......@@ -293,6 +327,7 @@ public abstract class WebLabServer {
* @return the pid of the WebLabServer using JMX
*/
public int getPid() {
int pid = -1;
if (this.isEnabled() && (this.getJMXConnector() != null)) {
try (JMXConnector connector = this.getJMXConnector()) {
......@@ -316,6 +351,7 @@ public abstract class WebLabServer {
* @return MemoryMXBean of the WebLabServer
*/
public MemoryMXBean monitorMemory() {
MemoryMXBean memory = null;
if (this.isEnabled()) {
try (JMXConnector connector = this.getJMXConnector()) {
......@@ -370,6 +406,7 @@ public abstract class WebLabServer {
public String supportedCommands() {
return this.name + " [start|stop|restart|status] \t will start, stop and give information on " + this.name + " current status.\n";
}
......@@ -378,6 +415,7 @@ public abstract class WebLabServer {
* @return the cleanablePathsAfterStop
*/
public List<String> getCleanablePathsAfterStop() {
return this.cleanablePathsAfterStop;
}
......@@ -387,6 +425,7 @@ public abstract class WebLabServer {
* the cleanablePathsAfterStop to set
*/
public void setCleanablePathsAfterStop(final List<String> cleanablePathsAfterStop) {
this.cleanablePathsAfterStop = cleanablePathsAfterStop;
}
......@@ -395,14 +434,17 @@ public abstract class WebLabServer {
* @return the host
*/
public String getHost() {
return this.host;
}
/**
* @param host the host to set
* @param host
* the host to set
*/
public void setHost(String host) {
this.host = host;
}
......
......@@ -33,6 +33,9 @@ public class ZooKeeperServer extends AbstractApplicationServer {
private String confFile;
/**
* The default constructor
*/
public ZooKeeperServer() {
// Nothing to do
}
......
......@@ -31,6 +31,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
......@@ -102,8 +103,8 @@ public class ProcessUtils {
final Process process;
try {
process = pbuilder.start();
new StreamGobbler(logger, process.getErrorStream(), true).start();
new StreamGobbler(logger, process.getInputStream(), false).start();
new StreamGobbler(logger, process.getErrorStream(), false, Level.WARNING).start();
new StreamGobbler(logger, process.getInputStream(), false, Level.FINE).start();
} catch (final IOException e) {
logger.error(e.getLocalizedMessage(), e);
return null;
......@@ -186,14 +187,14 @@ public class ProcessUtils {
* @throws IOException
* If the process cannot be started (or an error occurred when calling start at least)
* @throws InterruptedException
* If an error occured when waiting for the end of the process
* If an error occurred when waiting for the end of the process
*/
public static List<String> execute(final ProcessBuilder processBuilder, final Log logger) throws IOException, InterruptedException {
final ProcessBuilder processb = processBuilder;
logger.debug("Executing commands: " + processBuilder.command() + " in " + processBuilder.directory());
logger.debug("Executing commands: " + processBuilder.command() + ((processBuilder.directory() == null) ? "" : " in " + processBuilder.directory()));
final Process process = processb.start();
final StreamGobbler stream = new StreamGobbler(logger, process.getInputStream(), true);
final StreamGobbler stream = new StreamGobbler(logger, process.getInputStream(), true, Level.OFF);
stream.start();
process.waitFor();
......@@ -264,6 +265,10 @@ public class ProcessUtils {
}
/**
* @param details
* Initial map
*/
public Env(final Map<String, String> details) {
if (details != null) {
......@@ -288,6 +293,12 @@ public class ProcessUtils {
}
/**
*
* Class in charge of consuming an input stream and optionally to write it into the provided logger
*
* @author ymombrun
*/
public static class StreamGobbler extends Thread {
......@@ -297,24 +308,42 @@ public class ProcessUtils {
Log logger;
boolean keepData = false;
List<String> data = Collections.synchronizedList(new LinkedList<String>());
boolean nolog = false;
int level = Level.FINE.intValue();
boolean end = false;
// reads everything from is until empty.
public StreamGobbler(final Log logger, final InputStream istream, final boolean nolog) {
/**
* reads everything from is until empty.
*
* @param logger
* The logger to write into
* @param istream
* The input stream to read
* @param keepData
* Whether or not to keep log entries
* @param level
* The level of the logs to be written
*/
public StreamGobbler(final Log logger, final InputStream istream, final boolean keepData, final Level level) {
this.istream = istream;
this.logger = logger;
this.nolog = nolog;
this.level = level.intValue();
this.keepData = keepData;
}
/**
* @return The messages read by this steam gobbler
*/
public List<String> getData() {
while (!this.end) {
......@@ -332,16 +361,23 @@ public class ProcessUtils {
@Override
public void run() {
InputStreamReader isr = null;
try {
isr = new InputStreamReader(this.istream);
final BufferedReader buffer = new BufferedReader(isr);
try (InputStreamReader isr = new InputStreamReader(this.istream); final BufferedReader buffer = new BufferedReader(isr);) {
String line = null;
while ((line = buffer.readLine()) != null) {
if (this.nolog) {
if (this.keepData) {
this.data.add(line);
} else {
}
if (this.level < Level.FINER.intValue()) {
// Ignore it
} else if (this.level < Level.INFO.intValue()) {
this.logger.debug(line);
} else if (this.level < Level.WARNING.intValue()) {
this.logger.info(line);
} else if (this.level < Level.SEVERE.intValue()) {
this.logger.warn(line);
} else if (this.level < Level.OFF.intValue()) {
this.logger.error(line);
}
Thread.yield();
}
......@@ -349,20 +385,13 @@ public class ProcessUtils {
this.logger.error(ioe.getLocalizedMessage(), ioe);
} finally {
this.end = true;
if (isr != null) {
try {
isr.close();
} catch (final IOException ioe) {
this.logger.error(ioe.getLocalizedMessage(), ioe);
}
}
}
}
}
/**
* Create a dummy proccess
* Create a dummy prccess
*
* @return a dummy process definition
*/
......
......@@ -97,8 +97,8 @@ public class LinuxProcessManager extends ProcessManager {
@Override
protected ProcessInfo parseProcessInfo(final String line, final Log logger) {
if (logger.isDebugEnabled()) {
logger.debug("parsing process info line: " + line);
if (logger.isTraceEnabled()) {
logger.trace("Parsing process info line: " + line);
}
final String trimmedLine = line.trim();
......@@ -109,11 +109,11 @@ public class LinuxProcessManager extends ProcessManager {
final String pid = matcher.group(1);
final String command = matcher.group(2);
procInfo = new ProcessInfo(pid, command);
if (logger.isDebugEnabled()) {
logger.debug("processus info: " + "(" + procInfo.getPid() + ") " + procInfo);
if (logger.isTraceEnabled()) {
logger.trace("processus info: " + "(" + procInfo.getPid() + ") " + procInfo);
}
} else {
logger.warn("Could not find pid and command for process line : " + trimmedLine);
logger.warn("Could not find pid and command for process line: " + trimmedLine);
}
return procInfo;
......@@ -128,13 +128,13 @@ public class LinuxProcessManager extends ProcessManager {
@Override
public void setPermission(final String weblabHome, final String permissions, final boolean recursive, final Log logger) {
public void setPermission(final String path, final String permissions, final boolean recursive, final Log logger) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Setting permissions " + permissions + " on " + weblabHome);
if (logger.isTraceEnabled()) {
logger.trace("Setting permissions " + permissions + " on " + path);
}
ProcessUtils.execute(new ProcessBuilder("chmod", "-R", permissions, weblabHome), logger);
ProcessUtils.execute(new ProcessBuilder("chmod", "-R", permissions, path), logger);
} catch (final Exception exception) {
logger.error(exception.getMessage(), exception);
}
......
......@@ -48,24 +48,28 @@ public class WindowsProcessManager extends ProcessManager {
@Override
protected String killCommand(final int pid, final boolean force) {
return "taskkill /PID " + (force ? pid + " /F" : pid + "");
}
@Override
protected ProcessBuilder getProcessCommand() {
return new ProcessBuilder("tasklist", "/FO", "CSV", "/NH");
}
@Override
protected ProcessBuilder getNetstatCommand() {
return new ProcessBuilder("netstat", "-ano");
}
@Override
protected ProcessInfo parseProcessInfo(final String line, final Log logger) {
ProcessInfo procInfo = null;
final Matcher matcher = this.processPattern.matcher(line);
if (matcher.matches()) {
......@@ -81,6 +85,7 @@ public class WindowsProcessManager extends ProcessManager {
@Override
protected Map<Integer, ProcessInfo> buildProcessInfo(final List<String> data) {
final Map<Integer, ProcessInfo> portProcs = new HashMap<>();