Skip to content
Permalink
Browse files
AMQCLI-3 - Added more tests and cleanup
Added a builder to make export configuration easier
Added more CLI options
  • Loading branch information
cshannon committed Mar 9, 2017
1 parent 5637f04 commit 9723dbfb4d7e65d13a03000fd05cdddfa1dd816c
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 100 deletions.
@@ -0,0 +1,141 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.activemq.cli.kahadb.exporter;

import java.io.File;

public class ExportConfiguration {

private File source;

private File target;

private String queuePattern;

private String topicPattern;

private boolean multiKaha;

private boolean compress;

private boolean overwrite;

public File getSource() {
return source;
}

public void setSource(File source) {
this.source = source;
}

public File getTarget() {
return target;
}

public void setTarget(File target) {
this.target = target;
}

public String getQueuePattern() {
return queuePattern;
}

public void setQueuePattern(String queuePattern) {
this.queuePattern = queuePattern;
}

public String getTopicPattern() {
return topicPattern;
}

public void setTopicPattern(String topicPattern) {
this.topicPattern = topicPattern;
}

public boolean isMultiKaha() {
return multiKaha;
}

public void setMultiKaha(boolean multiKaha) {
this.multiKaha = multiKaha;
}

public boolean isCompress() {
return compress;
}

public void setCompress(boolean compress) {
this.compress = compress;
}

public boolean isOverwrite() {
return overwrite;
}

public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}

public static class ExportConfigurationBuilder {

private final ExportConfiguration config = new ExportConfiguration();

public static ExportConfigurationBuilder newBuilder() {
return new ExportConfigurationBuilder();
}

public ExportConfigurationBuilder setSource(File source) {
config.setSource(source);
return this;
}

public ExportConfigurationBuilder setTarget(File target) {
config.setTarget(target);
return this;
}

public ExportConfigurationBuilder setMultiKaha(boolean multiKaha) {
config.setMultiKaha(multiKaha);
return this;
}

public ExportConfigurationBuilder setQueuePattern(String queuePattern) {
config.setQueuePattern(queuePattern);
return this;
}

public ExportConfigurationBuilder setTopicPattern(String topicPattern) {
config.setTopicPattern(topicPattern);
return this;
}

public ExportConfigurationBuilder setCompress(boolean compress) {
config.setCompress(compress);
return this;
}

public ExportConfigurationBuilder setOverwrite(boolean overwrite) {
config.setOverwrite(overwrite);
return this;
}

public ExportConfiguration build() {
return config;
}

}
}
@@ -28,6 +28,7 @@
import javax.xml.stream.XMLStreamWriter;

import org.apache.activemq.cli.artemis.schema.ArtemisJournalMarshaller;
import org.apache.activemq.cli.kahadb.exporter.ExportConfiguration.ExportConfigurationBuilder;
import org.apache.activemq.cli.kahadb.exporter.artemis.ArtemisXmlMessageRecoveryListener;
import org.apache.activemq.cli.kahadb.exporter.artemis.ArtemisXmlMetadataExporter;
import org.apache.activemq.store.kahadb.FilteredKahaDBPersistenceAdapter;
@@ -66,34 +67,49 @@ public static void main(String[] args) {

}

@Command(name = "kahadb", description = "Export KahaDb")
@Command(name = "kahadb", description = "Export a KahaDb store to Artemis XML")
public static class ExportKahaDb implements Runnable
{
@Option(name="-source", type = OptionType.COMMAND, description = "Data store directory location")
@Option(name = {"-s", "--source"}, required = true, type = OptionType.COMMAND, description = "Data store directory location")
public String source;

@Option(name = "-target", type = OptionType.COMMAND, description = "Xml output file location")
@Option(name = {"-t", "--target"}, required = true, type = OptionType.COMMAND, description = "Xml output file location")
public String target;

@Option(name = "-c", type = OptionType.COMMAND, description = "Compress output xml file")
@Option(name = {"--qp", "--queuePattern"}, type = OptionType.COMMAND, description = "Queue Export Pattern")
public String queuePattern;

@Option(name = {"--tp", "--queuePattern"}, type = OptionType.COMMAND, description = "Topic Export Pattern")
public String topicPattern;

@Option(name = "-c", type = OptionType.COMMAND, description = "Compress output xml file using gzip")
public boolean compress;

@Option(name = "-f", type = OptionType.COMMAND, description = "Force XML output and overwrite existing file")
public boolean overwrite;

/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
LOG.info("Starting store export");
try {
Exporter.exportKahaDbStore(new File(source), new File(target), compress);
Exporter.exportStore(ExportConfigurationBuilder.newBuilder()
.setSource(new File(source))
.setTarget(new File(target))
.setQueuePattern(queuePattern)
.setTopicPattern(topicPattern)
.setCompress(compress)
.build());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}

}
}

@Command(name = "mkahadb", description = "Export MultiKahaDb")
@Command(name = "mkahadb", description = "Export a MultiKahaDb store to Artemis XML")
public static class ExportMultiKahaDb extends ExportKahaDb
{

@@ -104,52 +120,42 @@ public static class ExportMultiKahaDb extends ExportKahaDb
public void run() {
LOG.info("Exporting");
try {
Exporter.exportMultiKahaDbStore(new File(source), new File(target), compress);
Exporter.exportStore(ExportConfigurationBuilder.newBuilder()
.setMultiKaha(true)
.setSource(new File(source))
.setTarget(new File(target))
.setQueuePattern(queuePattern)
.setTopicPattern(topicPattern)
.setCompress(compress)
.build());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}

}
}

public static void exportKahaDbStore(final File kahaDbDir, final File artemisXml) throws Exception {
Exporter.exportStore(kahaDbDir, artemisXml, false, false);
}

public static void exportKahaDbStore(final File kahaDbDir, final File artemisXml,
boolean compress) throws Exception {
Exporter.exportStore(kahaDbDir, artemisXml, false, compress);
}

public static void exportMultiKahaDbStore(final File kahaDbDir, final File artemisXml) throws Exception {
Exporter.exportStore(kahaDbDir, artemisXml, true, false);
}

public static void exportMultiKahaDbStore(final File kahaDbDir, final File artemisXml,
boolean compress) throws Exception {
Exporter.exportStore(kahaDbDir, artemisXml, true, compress);
}

private static void exportStore(final File kahaDbDir, final File artemisXml,
boolean multiKaha, boolean compress) throws Exception {
public static void exportStore(final ExportConfiguration config) throws Exception {

if (artemisXml.exists()) {
throw new IllegalStateException("File: " + artemisXml + " already exists");
if (config.getTarget().exists()) {
throw new IllegalStateException("File: " + config.getTarget() + " already exists");
}

long start = System.currentTimeMillis();
try(OutputStream fos = new BufferedOutputStream(compress ? new GZIPOutputStream(
new FileOutputStream(artemisXml)) : new FileOutputStream(artemisXml))) {
try(OutputStream fos = new BufferedOutputStream(config.isCompress() ? new GZIPOutputStream(
new FileOutputStream(config.getTarget())) : new FileOutputStream(config.getTarget()))) {

final XMLStreamWriter xmlWriter = XMLOutputFactory.newFactory().createXMLStreamWriter(fos);
final ArtemisJournalMarshaller xmlMarshaller = new ArtemisJournalMarshaller(xmlWriter);

xmlMarshaller.appendJournalOpen();

if (multiKaha) {
appendMultiKahaDbStore(xmlMarshaller, getMultiKahaDbAdapter(kahaDbDir));
if (config.isMultiKaha()) {
appendMultiKahaDbStore(xmlMarshaller, getMultiKahaDbAdapter(config.getSource()),
config.getQueuePattern(), config.getTopicPattern());
} else {
appendKahaDbStore(xmlMarshaller, getKahaDbAdapter(kahaDbDir));
appendKahaDbStore(xmlMarshaller, getKahaDbAdapter(config.getSource()),
config.getQueuePattern(), config.getTopicPattern());
}

xmlMarshaller.appendJournalClose(true);
@@ -162,7 +168,8 @@ private static void exportStore(final File kahaDbDir, final File artemisXml,


private static void appendMultiKahaDbStore(final ArtemisJournalMarshaller xmlMarshaller,
MultiKahaDBPersistenceAdapter multiAdapter) throws Exception {
final MultiKahaDBPersistenceAdapter multiAdapter, final String queuePattern,
final String topicPattern) throws Exception {

try {
multiAdapter.start();
@@ -184,8 +191,8 @@ private static void appendMultiKahaDbStore(final ArtemisJournalMarshaller xmlMar

xmlMarshaller.appendMessagesElement();
for (KahaDBExporter dbExporter : dbExporters) {
dbExporter.exportQueues();
dbExporter.exportTopics();
dbExporter.exportQueues(queuePattern);
dbExporter.exportTopics(topicPattern);
}
xmlMarshaller.appendEndElement();
} finally {
@@ -194,7 +201,7 @@ private static void appendMultiKahaDbStore(final ArtemisJournalMarshaller xmlMar
}

private static void appendKahaDbStore(final ArtemisJournalMarshaller xmlMarshaller,
KahaDBPersistenceAdapter adapter) throws Exception {
final KahaDBPersistenceAdapter adapter, final String queuePattern, final String topicPattern) throws Exception {

try {
adapter.start();
@@ -207,8 +214,8 @@ private static void appendKahaDbStore(final ArtemisJournalMarshaller xmlMarshall
dbExporter.exportMetadata();
xmlMarshaller.appendEndElement();
xmlMarshaller.appendMessagesElement();
dbExporter.exportQueues();
dbExporter.exportTopics();
dbExporter.exportQueues(queuePattern);
dbExporter.exportTopics(topicPattern);
xmlMarshaller.appendEndElement();
} finally {
adapter.stop();
@@ -67,11 +67,13 @@ public void exportTopics() throws IOException {

@Override
public void exportQueues(String pattern) throws IOException {
pattern = pattern != null ? pattern : DestinationFilter.ANY_DESCENDENT;
exportDestinations(new ActiveMQQueue(pattern));
}

@Override
public void exportTopics(String pattern) throws IOException {
pattern = pattern != null ? pattern : DestinationFilter.ANY_DESCENDENT;
exportDestinations(new ActiveMQTopic(pattern));
}

0 comments on commit 9723dbf

Please sign in to comment.