/
ImportJournalOperation.java
135 lines (117 loc) · 6.32 KB
/
ImportJournalOperation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.extension.messaging.activemq;
import static org.jboss.as.controller.PathAddress.EMPTY_ADDRESS;
import static org.jboss.as.controller.RunningMode.NORMAL;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.cli.commands.tools.XmlDataImporter;
import org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory;
import org.jboss.as.controller.AbstractRuntimeOnlyHandler;
import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.SimpleOperationDefinitionBuilder;
import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.controller.services.path.PathResourceDefinition;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.wildfly.extension.messaging.activemq.logging.MessagingLogger;
import static org.wildfly.extension.messaging.activemq.ExportJournalOperation.checkAllowedOnJournal;
/**
* Import a dump of Artemis journal in a running Artemis server.
* WildFly must be running in NORMAL mode to perform this operation.
*
* The dump file MUST be on WildFly host. It is not attached to the operation stream.
*
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2015 Red Hat inc.
*/
public class ImportJournalOperation extends AbstractRuntimeOnlyHandler {
private static AttributeDefinition FILE = SimpleAttributeDefinitionBuilder.create("file", PathResourceDefinition.PATH)
.setAllowExpression(false)
.setRequired(true)
.build();
private static final String OPERATION_NAME = "import-journal";
static final ImportJournalOperation INSTANCE = new ImportJournalOperation();
private ImportJournalOperation() {
}
static void registerOperation(final ManagementResourceRegistration registry, final ResourceDescriptionResolver resourceDescriptionResolver) {
registry.registerOperationHandler(new SimpleOperationDefinitionBuilder(OPERATION_NAME, resourceDescriptionResolver)
.addParameter(FILE)
.setRuntimeOnly()
.setReplyValueType(ModelType.BOOLEAN)
.build(),
INSTANCE);
}
@Override
protected void executeRuntimeStep(OperationContext context, ModelNode operation) throws OperationFailedException {
if (context.getRunningMode() != NORMAL) {
throw MessagingLogger.ROOT_LOGGER.managementOperationAllowedOnlyInRunningMode(OPERATION_NAME, NORMAL);
}
checkAllowedOnJournal(context, OPERATION_NAME);
String file = FILE.resolveModelAttribute(context, operation).asString();
final XmlDataImporter importer = new XmlDataImporter();
TransportConfiguration transportConfiguration = createInVMTransportConfiguration(context);
try (
InputStream is = new FileInputStream(new File(file));
ServerLocator serverLocator = ActiveMQClient.createServerLocator(false, transportConfiguration);
ClientSessionFactory sf = serverLocator.createSessionFactory()
) {
ClientSession session = sf.createSession();
importer.process(is, session);
} catch (Exception e) {
throw new OperationFailedException(e);
}
}
/**
* The XmlDataImporter requires a connector to connect to the artemis broker.
*
* We require to use a in-vm one so that importing a journal is not subject to any network connection problem.
*/
private TransportConfiguration createInVMTransportConfiguration(OperationContext context) throws OperationFailedException {
final Resource serverResource = context.readResource(EMPTY_ADDRESS, false);
Set<Resource.ResourceEntry> invmConnectors = serverResource.getChildren(CommonAttributes.IN_VM_CONNECTOR);
if (invmConnectors.isEmpty()) {
throw MessagingLogger.ROOT_LOGGER.noInVMConnector();
}
Resource.ResourceEntry connectorEntry = invmConnectors.iterator().next();
Resource connectorResource = context.readResource(PathAddress.pathAddress(connectorEntry.getPathElement()), false);
ModelNode model = connectorResource.getModel();
Map<String, Object> params = new HashMap<>(CommonAttributes.PARAMS.unwrap(context, model));
params.put(InVMTransportDefinition.SERVER_ID.getName(), InVMTransportDefinition.SERVER_ID.resolveModelAttribute(context, model).asInt());
TransportConfiguration transportConfiguration = new TransportConfiguration(InVMConnectorFactory.class.getName(), params);
return transportConfiguration;
}
}