Permalink
Browse files

add shared test config for the domain controller client

  • Loading branch information...
1 parent 0f5832a commit d4eba34beba43248b584f8cd75737973aaf9a79f @emuckenhuber emuckenhuber committed with kabir Feb 23, 2012
@@ -141,5 +141,16 @@ public static DomainClient create(final InetAddress address, int port, CallbackH
return new DomainClientImpl(address, port, handler);
}
+ /**
+ * Create an {@link org.jboss.as.controller.client.helpers.domain.DomainClient} instance based on an existing
+ * {@link ModelControllerClient}.
+ *
+ * @param client the client
+ * @return A domain domain
+ */
+ public static DomainClient create(final ModelControllerClient client) {
+ return new DomainClientImpl(client);
+ }
+
}
}
@@ -65,6 +65,10 @@ public DomainClientImpl(InetAddress address, int port, CallbackHandler handler)
this.delegate = ModelControllerClient.Factory.create(address, port, handler);
}
+ public DomainClientImpl(ModelControllerClient delegate) {
+ this.delegate = delegate;
+ }
+
@Override
public ModelNode execute(ModelNode operation) throws IOException {
return delegate.execute(operation);
@@ -0,0 +1,153 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, 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.jboss.as.test.integration.domain.management.util;
+
+import org.jboss.as.process.protocol.StreamUtils;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Emanuel Muckenhuber
+ */
+class ProcessWrapper {
+
+ private final String processName;
+ private final List<String> command;
+ private final Map<String, String> env;
+ private final String workingDirectory;
+
+ private Process process;
+ private boolean stopped;
+
+ ProcessWrapper(final String processName, final List<String> command, final Map<String, String> env, final String workingDirectory) {
+ assert processName != null;
+ assert command != null;
+ assert env != null;
+ assert workingDirectory != null;
+ this.processName = processName;
+ this.command = command;
+ this.env = env;
+ this.workingDirectory = workingDirectory;
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ @Override
+ public void run() {
+ ProcessWrapper.this.stop();
+ }
+ });
+ }
+
+ void start() throws Exception {
+ synchronized (this) {
+ if(stopped) {
+ throw new IllegalStateException();
+ }
+ final ProcessBuilder builder = new ProcessBuilder(command);
+ builder.redirectErrorStream(true);
+ builder.environment().putAll(env);
+ builder.directory(new File(workingDirectory));
+ process = builder.start();
+
+ final InputStream stdout = process.getInputStream();
+ final Runnable consoleConsumer = new ConsoleConsumer(stdout, System.out);
+ new Thread(consoleConsumer, "Console consumer " + processName).start();
+ }
+ }
+
+ int waitFor() throws InterruptedException {
+ final Process process;
+ synchronized (this) {
+ process = this.process;
+ }
+ if(process != null) {
+ return process.waitFor();
+ }
+ return 0;
+ }
+
+ void stop() {
+ synchronized (this) {
+ boolean stopped = this.stopped;
+ if(! stopped) {
+ this.stopped = true;
+ final Process process = this.process;
+ if(process != null) {
+ process.destroy();
+ }
+ }
+ }
+ }
+
+ /**
+ * Runnable that consumes the output of the process. If nothing consumes the output the AS will hang on some platforms
+ *
+ * @author Stuart Douglas
+ *
+ */
+ private static class ConsoleConsumer implements Runnable {
+ private final InputStream source;
+ private final PrintStream target;
+ private final boolean writeOutput = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
+
+ @Override
+ public Boolean run() {
+ // this needs a better name
+ String val = System.getProperty("org.jboss.as.writeconsole");
+ return val == null || !"false".equals(val);
+ }
+ });
+
+ private ConsoleConsumer(final InputStream source, final PrintStream target) {
+ this.source = source;
+ this.target = target;
+ }
+
+ public void run() {
+ final InputStream source = this.source;
+ try {
+ final BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(source)));
+ String s;
+ while ((s = reader.readLine()) != null) {
+ if(writeOutput) {
+ target.println(s);
+ }
+ }
+ source.close();
+ } catch (IOException e) {
+ e.printStackTrace(target);
+ } finally {
+ StreamUtils.safeClose(source);
+ }
+ }
+ }
+
+}
@@ -0,0 +1,73 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, 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.jboss.as.test.integration.domain.management.util;
+
+import org.jboss.as.controller.client.ModelControllerClient;
+import org.jboss.as.protocol.ProtocolChannelClient;
+import org.jboss.remoting3.Endpoint;
+import org.jboss.remoting3.Remoting;
+import org.jboss.remoting3.remote.RemoteConnectionProviderFactory;
+import org.xnio.OptionMap;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.net.URI;
+
+/**
+ * @author Emanuel Muckenhuber
+ */
+public class SharedTestClientConfiguration implements Closeable {
+
+ static final String ENDPOINT_NAME = "mgmt-test-client";
+ private final Endpoint endpoint;
+
+ SharedTestClientConfiguration(Endpoint endpoint) {
+ this.endpoint = endpoint;
+ }
+
+ public ModelControllerClient createControllerClient(final URI connectionURI) throws IOException {
+
+ final ProtocolChannelClient.Configuration configuration = new ProtocolChannelClient.Configuration();
+ configuration.setEndpoint(endpoint);
+ configuration.setUri(connectionURI);
+ final ProtocolChannelClient client = ProtocolChannelClient.create(configuration);
+
+ return null;
+ }
+
+ @Override
+ public void close() throws IOException {
+ if(endpoint != null) try {
+ endpoint.close();
+ } catch (IOException e) {
+
+ }
+ }
+
+ public static SharedTestClientConfiguration create() throws IOException {
+ final Endpoint endpoint = Remoting.createEndpoint(ENDPOINT_NAME, OptionMap.EMPTY);
+ endpoint.addConnectionProvider("remote", new RemoteConnectionProviderFactory(), OptionMap.EMPTY);
+ return new SharedTestClientConfiguration(endpoint);
+ }
+
+}

0 comments on commit d4eba34

Please sign in to comment.