Make tools more friendly to user errors

This commit is contained in:
Clebert Suconic 2015-05-13 15:06:11 -04:00
parent dc82cd0a51
commit 6cb68f2ce9
9 changed files with 70 additions and 21 deletions

View File

@ -17,10 +17,15 @@
package org.apache.activemq.artemis.cli.commands; package org.apache.activemq.artemis.cli.commands;
import javax.inject.Inject;
import java.io.File; import java.io.File;
import io.airlift.airline.Arguments; import io.airlift.airline.Arguments;
import io.airlift.airline.Help;
import io.airlift.airline.Option; import io.airlift.airline.Option;
import io.airlift.airline.model.CommandGroupMetadata;
import io.airlift.airline.model.CommandMetadata;
import io.airlift.airline.model.GlobalMetadata;
import org.apache.activemq.artemis.core.config.FileDeploymentManager; import org.apache.activemq.artemis.core.config.FileDeploymentManager;
import org.apache.activemq.artemis.core.config.impl.FileConfiguration; import org.apache.activemq.artemis.core.config.impl.FileConfiguration;
import org.apache.activemq.artemis.dto.BrokerDTO; import org.apache.activemq.artemis.dto.BrokerDTO;
@ -39,6 +44,10 @@ public abstract class Configurable
@Option(name = "--broker", description = "This would override the broker configuration from the bootstrap") @Option(name = "--broker", description = "This would override the broker configuration from the bootstrap")
String brokerConfig; String brokerConfig;
@Inject
public GlobalMetadata global;
private BrokerDTO brokerDTO = null; private BrokerDTO brokerDTO = null;
private String brokerInstance; private String brokerInstance;
@ -47,6 +56,15 @@ public abstract class Configurable
private FileConfiguration fileConfiguration; private FileConfiguration fileConfiguration;
protected void treatError(Exception e, String group, String command)
{
ActiveMQBootstrapLogger.LOGGER.debug(e.getMessage(), e);
System.err.println();
System.err.println("Error:" + e.getMessage());
System.err.println();
helpGroup(group, command);
}
protected String getBrokerInstance() protected String getBrokerInstance()
{ {
if (brokerInstance == null) if (brokerInstance == null)
@ -64,6 +82,24 @@ public abstract class Configurable
return brokerInstance; return brokerInstance;
} }
protected void helpGroup(String groupName, String commandName)
{
for (CommandGroupMetadata group: global.getCommandGroups())
{
if (group.getName().equals(groupName))
{
for (CommandMetadata command: group.getCommands())
{
if (command.getName().equals(commandName))
{
Help.help(command);
}
}
break;
}
}
}
protected String getBrokerHome() protected String getBrokerHome()
{ {
if (brokerHome == null) if (brokerHome == null)
@ -89,7 +125,6 @@ public abstract class Configurable
if (getBrokerInstance() == null) if (getBrokerInstance() == null)
{ {
final String defaultLocation = "../data"; final String defaultLocation = "../data";
ActiveMQBootstrapLogger.LOGGER.brokerConfigNotFound(defaultLocation);
fileConfiguration = new FileConfiguration(); fileConfiguration = new FileConfiguration();
// These will be the default places in case the file can't be loaded // These will be the default places in case the file can't be loaded
fileConfiguration.setBindingsDirectory(defaultLocation + "/bindings"); fileConfiguration.setBindingsDirectory(defaultLocation + "/bindings");

View File

@ -25,6 +25,7 @@ import io.airlift.airline.Command;
import io.airlift.airline.Option; import io.airlift.airline.Option;
import org.apache.activemq.artemis.cli.Artemis; import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.components.ExternalComponent; import org.apache.activemq.artemis.components.ExternalComponent;
import org.apache.activemq.artemis.core.config.impl.FileConfiguration;
import org.apache.activemq.artemis.core.server.ActiveMQComponent; import org.apache.activemq.artemis.core.server.ActiveMQComponent;
import org.apache.activemq.artemis.dto.BrokerDTO; import org.apache.activemq.artemis.dto.BrokerDTO;
import org.apache.activemq.artemis.dto.ComponentDTO; import org.apache.activemq.artemis.dto.ComponentDTO;
@ -50,6 +51,8 @@ public class Run extends Configurable implements Action
Artemis.printBanner(); Artemis.printBanner();
createDirectories(getFileConfiguration());
BrokerDTO broker = getBrokerDTO(); BrokerDTO broker = getBrokerDTO();
addShutdownHook(broker.server.getConfigurationFile().getParentFile()); addShutdownHook(broker.server.getConfigurationFile().getParentFile());
@ -77,6 +80,15 @@ public class Run extends Configurable implements Action
return null; return null;
} }
private void createDirectories(FileConfiguration fileConfiguration)
{
new File(fileConfiguration.getBindingsDirectory()).mkdirs();
new File(fileConfiguration.getJournalDirectory()).mkdirs();
new File(fileConfiguration.getPagingDirectory()).mkdirs();
new File(fileConfiguration.getLargeMessagesDirectory()).mkdirs();
}
/** /**
* Add a simple shutdown hook to stop the server. * Add a simple shutdown hook to stop the server.
* @param configurationDir * @param configurationDir

View File

@ -17,11 +17,10 @@
package org.apache.activemq.artemis.cli.commands.tools; package org.apache.activemq.artemis.cli.commands.tools;
import java.io.File;
import io.airlift.airline.Option; import io.airlift.airline.Option;
import org.apache.activemq.artemis.cli.commands.Configurable; import org.apache.activemq.artemis.cli.commands.Configurable;
import org.apache.activemq.artemis.integration.bootstrap.ActiveMQBootstrapBundle;
import java.io.File;
/** Abstract class for places where you need bindings, journal paging and large messages configuration */ /** Abstract class for places where you need bindings, journal paging and large messages configuration */
public abstract class DataAbstract extends Configurable public abstract class DataAbstract extends Configurable
@ -44,9 +43,10 @@ public abstract class DataAbstract extends Configurable
if (largeMessges == null) if (largeMessges == null)
{ {
largeMessges = getFileConfiguration().getLargeMessagesDirectory(); largeMessges = getFileConfiguration().getLargeMessagesDirectory();
checkIfDirectoryExists(largeMessges);
} }
checkIfDirectoryExists(largeMessges);
return largeMessges; return largeMessges;
} }
@ -56,9 +56,10 @@ public abstract class DataAbstract extends Configurable
if (binding == null) if (binding == null)
{ {
binding = getFileConfiguration().getBindingsDirectory(); binding = getFileConfiguration().getBindingsDirectory();
checkIfDirectoryExists(binding);
} }
checkIfDirectoryExists(binding);
return binding; return binding;
} }
@ -67,9 +68,10 @@ public abstract class DataAbstract extends Configurable
if (journal == null) if (journal == null)
{ {
journal = getFileConfiguration().getJournalDirectory(); journal = getFileConfiguration().getJournalDirectory();
checkIfDirectoryExists(journal);
} }
checkIfDirectoryExists(journal);
return journal; return journal;
} }
@ -80,6 +82,8 @@ public abstract class DataAbstract extends Configurable
paging = getFileConfiguration().getPagingDirectory(); paging = getFileConfiguration().getPagingDirectory();
} }
checkIfDirectoryExists(paging);
return paging; return paging;
} }
@ -88,7 +92,7 @@ public abstract class DataAbstract extends Configurable
File f = new File(directory); File f = new File(directory);
if (!f.exists()) if (!f.exists())
{ {
throw ActiveMQBootstrapBundle.BUNDLE.directoryDoesNotExist(directory); throw new IllegalStateException("Could not find folder: " + directory + ", please pass --bindings, --journal and --paging as arguments");
} }
} }

View File

@ -69,7 +69,7 @@ public class DecodeJournal extends Configurable implements Action
} }
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); treatError(e, "data", "decode");
} }
return null; return null;

View File

@ -64,7 +64,7 @@ public class EncodeJournal extends Configurable implements Action
} }
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); treatError(e, "data", "encode");
} }
return null; return null;

View File

@ -54,13 +54,11 @@ import org.apache.activemq.artemis.core.server.impl.FileLockNodeManager;
import org.apache.activemq.artemis.core.settings.HierarchicalRepository; import org.apache.activemq.artemis.core.settings.HierarchicalRepository;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings; import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.apache.activemq.artemis.core.settings.impl.HierarchicalObjectRepository; import org.apache.activemq.artemis.core.settings.impl.HierarchicalObjectRepository;
import org.apache.activemq.artemis.integration.bootstrap.ActiveMQBootstrapLogger;
import org.apache.activemq.artemis.utils.ExecutorFactory; import org.apache.activemq.artemis.utils.ExecutorFactory;
@Command(name = "print", description = "Print data records information (WARNING: don't use while a production server is running)") @Command(name = "print", description = "Print data records information (WARNING: don't use while a production server is running)")
public class PrintData extends DataAbstract implements Action public class PrintData extends DataAbstract implements Action
{ {
@Override @Override
public Object execute(ActionContext context) throws Exception public Object execute(ActionContext context) throws Exception
{ {
@ -70,7 +68,7 @@ public class PrintData extends DataAbstract implements Action
} }
catch (Exception e) catch (Exception e)
{ {
ActiveMQBootstrapLogger.LOGGER.printDataFailed(e.getMessage()); treatError(e, "data", "print");
} }
return null; return null;
} }

View File

@ -126,7 +126,14 @@ public final class XmlDataExporter extends DataAbstract implements Action
@Override @Override
public Object execute(ActionContext context) throws Exception public Object execute(ActionContext context) throws Exception
{ {
process(System.out, getBinding(), getJournal(), getPaging(), getLargeMessages()); try
{
process(System.out, getBinding(), getJournal(), getPaging(), getLargeMessages());
}
catch (Exception e)
{
treatError(e, "data", "exp");
}
return null; return null;
} }

View File

@ -18,7 +18,6 @@ package org.apache.activemq.artemis.integration.bootstrap;
import org.jboss.logging.Messages; import org.jboss.logging.Messages;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageBundle; import org.jboss.logging.annotations.MessageBundle;
/** /**
@ -33,6 +32,4 @@ public interface ActiveMQBootstrapBundle
{ {
ActiveMQBootstrapBundle BUNDLE = Messages.getBundle(ActiveMQBootstrapBundle.class); ActiveMQBootstrapBundle BUNDLE = Messages.getBundle(ActiveMQBootstrapBundle.class);
@Message(id = 109000, value = "Directory does not exist: {0}", format = Message.Format.MESSAGE_FORMAT)
IllegalStateException directoryDoesNotExist(String directory);
} }

View File

@ -61,10 +61,6 @@ public interface ActiveMQBootstrapLogger extends BasicLogger
@Message(id = 101003, value = "Halting ActiveMQ Artemis Server after user request", format = Message.Format.MESSAGE_FORMAT) @Message(id = 101003, value = "Halting ActiveMQ Artemis Server after user request", format = Message.Format.MESSAGE_FORMAT)
void serverKilled(); void serverKilled();
@LogMessage(level = Logger.Level.INFO)
@Message(id = 101004, value = "Broker configuration not found. Looking for data files in the ''{0}'' directory.", format = Message.Format.MESSAGE_FORMAT)
void brokerConfigNotFound(String defaultLocation);
@LogMessage(level = Logger.Level.INFO) @LogMessage(level = Logger.Level.INFO)
@Message(id = 101005, value = "Using broker configuration: {0}", format = Message.Format.MESSAGE_FORMAT) @Message(id = 101005, value = "Using broker configuration: {0}", format = Message.Format.MESSAGE_FORMAT)
void usingBrokerConfig(String location); void usingBrokerConfig(String location);