PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/webservices/tests-integration/src/main/java/org/jboss/as/webservices/deployer/RemoteDeployer.java

https://github.com/smcgowan/wildfly
Java | 261 lines | 205 code | 28 blank | 28 comment | 31 complexity | 80ebe960286a197892791adaf55b33f1 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.webservices.deployer;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
  24. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALLOW_RESOURCE_SERVICE_RESTART;
  25. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
  26. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.NAME;
  27. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
  28. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OPERATION_HEADERS;
  29. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
  30. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
  31. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_ATTRIBUTE_OPERATION;
  32. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RELEASE_VERSION;
  33. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REMOVE;
  34. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REQUIRED;
  35. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
  36. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ROLLBACK_ON_RUNTIME_FAILURE;
  37. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
  38. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
  39. import static org.jboss.as.security.Constants.AUTHENTICATION;
  40. import static org.jboss.as.security.Constants.CLASSIC;
  41. import static org.jboss.as.security.Constants.CODE;
  42. import static org.jboss.as.security.Constants.FLAG;
  43. import static org.jboss.as.security.Constants.LOGIN_MODULES;
  44. import static org.jboss.as.security.Constants.MODULE_OPTIONS;
  45. import static org.jboss.as.security.Constants.SECURITY_DOMAIN;
  46. import java.io.IOException;
  47. import java.net.InetAddress;
  48. import java.net.URL;
  49. import java.security.AccessController;
  50. import java.security.PrivilegedAction;
  51. import java.util.ArrayList;
  52. import java.util.HashMap;
  53. import java.util.List;
  54. import java.util.Map;
  55. import javax.security.auth.callback.Callback;
  56. import javax.security.auth.callback.CallbackHandler;
  57. import javax.security.auth.callback.NameCallback;
  58. import javax.security.auth.callback.PasswordCallback;
  59. import javax.security.auth.callback.UnsupportedCallbackException;
  60. import javax.security.sasl.RealmCallback;
  61. import javax.security.sasl.RealmChoiceCallback;
  62. import org.jboss.as.controller.client.ModelControllerClient;
  63. import org.jboss.as.controller.client.OperationBuilder;
  64. import org.jboss.as.controller.client.helpers.standalone.DeploymentAction;
  65. import org.jboss.as.controller.client.helpers.standalone.DeploymentPlan;
  66. import org.jboss.as.controller.client.helpers.standalone.DeploymentPlanBuilder;
  67. import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentActionResult;
  68. import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentManager;
  69. import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentPlanResult;
  70. import org.jboss.dmr.ModelNode;
  71. import org.jboss.logging.Logger;
  72. import org.jboss.wsf.spi.deployer.Deployer;
  73. /**
  74. * Remote deployer that uses AS7 client deployment API.
  75. *
  76. * @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
  77. * @author <a href="mailto:alessio.soldano@jboss.com">Alessio Soldano</a>
  78. */
  79. public final class RemoteDeployer implements Deployer {
  80. private static final Logger LOGGER = Logger.getLogger(RemoteDeployer.class);
  81. private static final int PORT = 9999;
  82. private static final String JBWS_DEPLOYER_AUTH_USER = "jbossws.deployer.authentication.username";
  83. private static final String JBWS_DEPLOYER_AUTH_PWD = "jbossws.deployer.authentication.password";
  84. private final Map<URL, String> url2Id = new HashMap<URL, String>();
  85. private final InetAddress address = InetAddress.getByName("127.0.0.1");
  86. private final CallbackHandler callbackHandler = getCallbackHandler();
  87. private final ServerDeploymentManager deploymentManager;
  88. public RemoteDeployer() throws IOException {
  89. deploymentManager = ServerDeploymentManager.Factory.create(address, PORT, callbackHandler);
  90. }
  91. @Override
  92. public void deploy(final URL archiveURL) throws Exception {
  93. final DeploymentPlanBuilder builder = deploymentManager.newDeploymentPlan().add(archiveURL).andDeploy();
  94. final DeploymentPlan plan = builder.build();
  95. final DeploymentAction deployAction = builder.getLastAction();
  96. final String uniqueId = deployAction.getDeploymentUnitUniqueName();
  97. executeDeploymentPlan(plan, deployAction);
  98. url2Id.put(archiveURL, uniqueId);
  99. }
  100. public void undeploy(final URL archiveURL) throws Exception {
  101. final DeploymentPlanBuilder builder = deploymentManager.newDeploymentPlan();
  102. final String uniqueName = url2Id.get(archiveURL);
  103. if (uniqueName != null) {
  104. final DeploymentPlan plan = builder.undeploy(uniqueName).remove(uniqueName).build();
  105. final DeploymentAction deployAction = builder.getLastAction();
  106. try {
  107. executeDeploymentPlan(plan, deployAction);
  108. } finally {
  109. url2Id.remove(archiveURL);
  110. }
  111. }
  112. }
  113. private void executeDeploymentPlan(final DeploymentPlan plan, final DeploymentAction deployAction) throws Exception {
  114. try {
  115. final ServerDeploymentPlanResult planResult = deploymentManager.execute(plan).get();
  116. if (deployAction != null) {
  117. final ServerDeploymentActionResult actionResult = planResult.getDeploymentActionResult(deployAction.getId());
  118. if (actionResult != null) {
  119. final Exception deploymentException = (Exception) actionResult.getDeploymentException();
  120. if (deploymentException != null)
  121. throw deploymentException;
  122. }
  123. }
  124. } catch (final Exception e) {
  125. LOGGER.fatal(e.getMessage(), e);
  126. throw e;
  127. }
  128. }
  129. public String getServerVersion() throws Exception {
  130. final ModelNode request = new ModelNode();
  131. request.get(OP).set(READ_ATTRIBUTE_OPERATION);
  132. request.get(OP_ADDR).setEmptyList();
  133. request.get(NAME).set(RELEASE_VERSION);
  134. final ModelNode response = applyUpdate(request, getModelControllerClient());
  135. return response.get(RESULT).asString();
  136. }
  137. public void addSecurityDomain(String name, Map<String, String> authenticationOptions) throws Exception {
  138. final List<ModelNode> updates = new ArrayList<ModelNode>();
  139. ModelNode op = new ModelNode();
  140. op.get(OP).set(ADD);
  141. op.get(OP_ADDR).add(SUBSYSTEM, "security");
  142. op.get(OP_ADDR).add(SECURITY_DOMAIN, name);
  143. updates.add(op);
  144. op = new ModelNode();
  145. op.get(OP).set(ADD);
  146. op.get(OP_ADDR).add(SUBSYSTEM, "security");
  147. op.get(OP_ADDR).add(SECURITY_DOMAIN, name);
  148. op.get(OP_ADDR).add(AUTHENTICATION, CLASSIC);
  149. final ModelNode loginModule = op.get(LOGIN_MODULES).add();
  150. loginModule.get(CODE).set("UsersRoles");
  151. loginModule.get(FLAG).set(REQUIRED);
  152. op.get(OPERATION_HEADERS).get(ALLOW_RESOURCE_SERVICE_RESTART).set(true);
  153. updates.add(op);
  154. final ModelNode moduleOptions = loginModule.get(MODULE_OPTIONS);
  155. if (authenticationOptions != null) {
  156. for (final String k : authenticationOptions.keySet()) {
  157. moduleOptions.add(k, authenticationOptions.get(k));
  158. }
  159. }
  160. applyUpdates(updates, getModelControllerClient());
  161. }
  162. public void removeSecurityDomain(String name) throws Exception {
  163. final ModelNode op = new ModelNode();
  164. op.get(OP).set(REMOVE);
  165. op.get(OP_ADDR).add(SUBSYSTEM, "security");
  166. op.get(OP_ADDR).add(SECURITY_DOMAIN, name);
  167. op.get(OPERATION_HEADERS, ROLLBACK_ON_RUNTIME_FAILURE).set(false);
  168. applyUpdate(op, getModelControllerClient());
  169. }
  170. private ModelControllerClient getModelControllerClient() {
  171. return ModelControllerClient.Factory.create(address, PORT, callbackHandler);
  172. }
  173. private static void applyUpdates(final List<ModelNode> updates, final ModelControllerClient client) throws Exception {
  174. for (final ModelNode update : updates) {
  175. applyUpdate(update, client);
  176. }
  177. }
  178. private static ModelNode applyUpdate(final ModelNode update, final ModelControllerClient client) throws Exception {
  179. final ModelNode result = client.execute(new OperationBuilder(update).build());
  180. checkResult(result);
  181. return result;
  182. }
  183. private static void checkResult(final ModelNode result) throws Exception {
  184. if (result.hasDefined(OUTCOME) && SUCCESS.equals(result.get(OUTCOME).asString())) {
  185. if (result.hasDefined(RESULT)) {
  186. LOGGER.info(result.get(RESULT));
  187. }
  188. } else if (result.hasDefined(FAILURE_DESCRIPTION)) {
  189. throw new Exception(result.get(FAILURE_DESCRIPTION).toString());
  190. } else {
  191. throw new Exception("Operation not successful; outcome = " + result.get(OUTCOME));
  192. }
  193. }
  194. private static CallbackHandler getCallbackHandler() {
  195. final String username = getSystemProperty(JBWS_DEPLOYER_AUTH_USER, null);
  196. if (username == null || ("${" + JBWS_DEPLOYER_AUTH_USER + "}").equals(username)) {
  197. return null;
  198. }
  199. String pwd = getSystemProperty(JBWS_DEPLOYER_AUTH_PWD, null);
  200. if (("${" + JBWS_DEPLOYER_AUTH_PWD + "}").equals(pwd)) {
  201. pwd = null;
  202. }
  203. final String password = pwd;
  204. return new CallbackHandler() {
  205. public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  206. for (Callback current : callbacks) {
  207. if (current instanceof NameCallback) {
  208. NameCallback ncb = (NameCallback) current;
  209. ncb.setName(username);
  210. } else if (current instanceof PasswordCallback) {
  211. PasswordCallback pcb = (PasswordCallback) current;
  212. pcb.setPassword(password.toCharArray());
  213. } else if (current instanceof RealmCallback) {
  214. RealmCallback rcb = (RealmCallback) current;
  215. rcb.setText(rcb.getDefaultText());
  216. } else if (current instanceof RealmChoiceCallback) {
  217. // Ignored but not rejected.
  218. } else {
  219. throw new UnsupportedCallbackException(current);
  220. }
  221. }
  222. }
  223. };
  224. }
  225. private static String getSystemProperty(final String name, final String defaultValue) {
  226. PrivilegedAction<String> action = new PrivilegedAction<String>() {
  227. public String run() {
  228. return System.getProperty(name, defaultValue);
  229. }
  230. };
  231. return AccessController.doPrivileged(action);
  232. }
  233. }