Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2011, 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.webservices.deployer;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
 
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALLOW_RESOURCE_SERVICE_RESTART;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.COMPOSITE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.NAME;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OPERATION_HEADERS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_ATTRIBUTE_OPERATION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RELEASE_VERSION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REMOVE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REQUIRED;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ROLLBACK_ON_RUNTIME_FAILURE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.STEPS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
 import static org.jboss.as.security.Constants.CLASSIC;
 import static org.jboss.as.security.Constants.CODE;
 import static org.jboss.as.security.Constants.FLAG;
 import static org.jboss.as.security.Constants.LOGIN_MODULE;
 import static org.jboss.as.security.Constants.MODULE_OPTIONS;
 import static org.jboss.as.security.Constants.SECURITY_DOMAIN;

Remote deployer that uses AS7 client deployment API.

Author(s):
Richard Opalka
Alessio Soldano
 
 public final class RemoteDeployer implements Deployer {
 
     private static final Logger LOGGER = Logger.getLogger(RemoteDeployer.class);
     private static final int DEFAULT_PORT = 9999;
     private static final String JBWS_DEPLOYER_HOST = "jbossws.deployer.host";
     private static final String JBWS_DEPLOYER_PORT = "jbossws.deployer.port";
     private static final String JBWS_DEPLOYER_AUTH_USER = "jbossws.deployer.authentication.username";
     private static final String JBWS_DEPLOYER_AUTH_PWD = "jbossws.deployer.authentication.password";
     private static final CallbackHandler callbackHandler = getCallbackHandler();
     private static final int TIMEOUT = 60000;
     private static InetAddress address;
    private static Integer port;
    private final Map<URLStringurl2Id = new HashMap<URLString>();
    private final Map<StringIntegersecurityDomainUsers = new HashMap<StringInteger>(1);
    private final Map<StringIntegerarchiveCounters = new HashMap<StringInteger>();
    private final Semaphore httpsConnSemaphore = new Semaphore(1);
    static {
        try {
            final String host = System.getProperty();
             = host != null ? InetAddress.getByName(host) : InetAddress.getByName("localhost");
             = Integer.getInteger();
        } catch (final IOException e) {
            .fatal(e.getMessage(), e);
            System.exit(1);
        }
    }
    @Override
    public void deploy(final URL archiveURLthrows Exception {
        synchronized () {
            String k = archiveURL.toString();
            if (.containsKey(k)) {
                int count = .get(k);
                .put(k, (count + 1));
                return;
            } else {
                .put(k, 1);
            }
            final ModelControllerClient client = newModelControllerClient();
            final ServerDeploymentManager deploymentManager = newDeploymentManager(client);
            final DeploymentPlanBuilder builder = deploymentManager.newDeploymentPlan().add(archiveURL).andDeploy();
            final DeploymentPlan plan = builder.build();
            final DeploymentAction deployAction = builder.getLastAction();
            final String uniqueId = deployAction.getDeploymentUnitUniqueName();
            executeDeploymentPlan(plandeployActionclientdeploymentManager);
            .put(archiveURLuniqueId);
        }
    }
    @Override
    public void undeploy(final URL archiveURLthrows Exception {
        synchronized () {
            String k = archiveURL.toString();
            if (.containsKey(k)) {
                int count = .get(k);
                if (count > 1) {
                    .put(k, (count - 1));
                    return;
                } else {
                    .remove(k);
                }
            } else {
                .warn("Trying to undeploy archive " + archiveURL + " which is not currently deployed!");
                return;
            }
            final ModelControllerClient client = newModelControllerClient();
            final ServerDeploymentManager deploymentManager = newDeploymentManager(client);
            final DeploymentPlanBuilder builder = deploymentManager.newDeploymentPlan();
            final String uniqueName = .get(archiveURL);
            if (uniqueName != null) {
                final DeploymentPlan plan = builder.undeploy(uniqueName).remove(uniqueName).build();
                final DeploymentAction deployAction = builder.getLastAction();
                try {
                    executeDeploymentPlan(plandeployActionclientdeploymentManager);
                } finally {
                    .remove(archiveURL);
                }
            }
        }
    }
    private void executeDeploymentPlan(final DeploymentPlan planfinal DeploymentAction deployAction,
            final ModelControllerClient clientfinal ServerDeploymentManager deploymentManagerthrows Exception {
        try {
            final ServerDeploymentPlanResult planResult = deploymentManager.execute(plan).get();
            if (deployAction != null) {
                final ServerDeploymentActionResult actionResult = planResult.getDeploymentActionResult(deployAction.getId());
                if (actionResult != null) {
                    final Exception deploymentException = (ExceptionactionResult.getDeploymentException();
                    if (deploymentException != null)
                        throw deploymentException;
                }
            }
        } catch (final Exception e) {
            .fatal(e.getMessage(), e);
            throw e;
        } finally {
            client.close();
            deploymentManager.close();
        }
    }
    public String getServerVersion() throws Exception {
        final ModelNode request = new ModelNode();
        request.get().set();
        request.get().setEmptyList();
        request.get().set();
        final ModelNode response = applyUpdate(request);
        return response.get().asString();
    }
    @Override
    public void addSecurityDomain(String nameMap<StringStringauthenticationOptionsthrows Exception {
        synchronized () {
            if (.containsKey(name)) {
                int count = .get(name);
                .put(name, (count + 1));
                return;
            } else {
                .put(name, 1);
            }
            final List<ModelNodeupdates = new ArrayList<ModelNode>();
            final ModelNode compositeOp = new ModelNode();
            compositeOp.get().set();
            compositeOp.get().setEmptyList();
            ModelNode steps = compositeOp.get();
            PathAddress address = PathAddress.pathAddress()
                    .append("security")
                    .append(name);
            steps.add(Util.createAddOperation(address));
            address = address.append(.);
            steps.add(Util.createAddOperation(address));
            ModelNode loginModule = Util.createAddOperation(address.append("UsersRoles"));
            loginModule.get().set("UsersRoles");
            loginModule.get().set();
            loginModule.get().get().set(true);
            final ModelNode moduleOptions = loginModule.get();
            if (authenticationOptions != null) {
                for (final String k : authenticationOptions.keySet()) {
                    moduleOptions.add(kauthenticationOptions.get(k));
                }
            }
            steps.add(loginModule);
            updates.add(compositeOp);
            applyUpdates(updates);
        }
    }
    @Override
    public void removeSecurityDomain(String namethrows Exception {
        synchronized () {
            int count = .get(name);
            if (count > 1) {
                .put(name, (count - 1));
                return;
            } else {
                .remove(name);
            }
            final ModelNode op = new ModelNode();
            op.get().set();
            op.get().add("security");
            op.get().add(name);
            op.get().set(false);
            applyUpdate(op);
        }
    }
    public void addHttpsConnector(Map<StringStringsslOptionsthrows Exception {
        .acquire();
        try {
            final ModelNode composite = Util.getEmptyOperation(new ModelNode());
            final ModelNode steps = composite.get();
            ModelNode op = createOpNode("subsystem=web/connector=jbws-test-https-connector");
            op.get("socket-binding").set("https");
            op.get("scheme").set("https");
            op.get("protocol").set("HTTP/1.1");
            op.get("secure").set(true);
            op.get("enabled").set(true);
            steps.add(op);
            ModelNode ssl = createOpNode("subsystem=web/connector=jbws-test-https-connector/ssl=configuration");
            if (sslOptions != null) {
                for (final String k : sslOptions.keySet()) {
                    ssl.get(k).set(sslOptions.get(k));
                }
            }
            steps.add(ssl);
            applyUpdate(composite);
        } catch (Exception e) {
            .release();
            throw e;
        }
    }
    public void removeHttpsConnector() throws Exception {
        try {
            ModelNode op = createOpNode("subsystem=web/connector=jbws-test-https-connector");
            applyUpdate(op);
        } finally {
            .release();
        }
    }
    private static void applyUpdates(final List<ModelNodeupdatesthrows Exception {
        for (final ModelNode update : updates) {
            applyUpdate(update);
        }
    }
    private static ModelNode applyUpdate(final ModelNode updatethrows Exception {
        final ModelControllerClient client = newModelControllerClient();
        try {
            final ModelNode result = client.execute(new OperationBuilder(update).build());
            checkResult(result);
            return result;
        } finally {
            client.close();
        }
    }
    private static void checkResult(final ModelNode resultthrows Exception {
        if (result.hasDefined() && .equals(result.get().asString())) {
            if (result.hasDefined()) {
                .info(result.get());
            }
        } else if (result.hasDefined()) {
            throw new Exception(result.get().toString());
        } else {
            throw new Exception("Operation not successful; outcome = " + result.get());
        }
    }
    private static CallbackHandler getCallbackHandler() {
        final String username = getSystemProperty(null);
        if (username == null || ("${" +  + "}").equals(username)) {
            return null;
        }
        String pwd = getSystemProperty(null);
        if (("${" +  + "}").equals(pwd)) {
            pwd = null;
        }
        final String password = pwd;
        return new CallbackHandler() {
            public void handle(Callback[] callbacksthrows IOExceptionUnsupportedCallbackException {
                for (Callback current : callbacks) {
                    if (current instanceof NameCallback) {
                        NameCallback ncb = (NameCallbackcurrent;
                        ncb.setName(username);
                    } else if (current instanceof PasswordCallback) {
                        PasswordCallback pcb = (PasswordCallbackcurrent;
                        pcb.setPassword(password.toCharArray());
                    } else if (current instanceof RealmCallback) {
                        RealmCallback rcb = (RealmCallbackcurrent;
                        rcb.setText(rcb.getDefaultText());
                    } else if (current instanceof RealmChoiceCallback) {
                        // Ignored but not rejected.
                    } else {
                        throw new UnsupportedCallbackException(current);
                    }
                }
            }
        };
    }
    public static ModelNode createOpNode(String addressString operation) {
        ModelNode op = new ModelNode();
        // set address
        ModelNode list = op.get("address").setEmptyList();
        if (address != null) {
            String[] pathSegments = address.split("/");
            for (String segment : pathSegments) {
                String[] elements = segment.split("=");
                list.add(elements[0], elements[1]);
            }
        }
        op.get("operation").set(operation);
        return op;
    }
    private static String getSystemProperty(final String namefinal String defaultValue) {
        PrivilegedAction<Stringaction = new PrivilegedAction<String>() {
            public String run() {
                return System.getProperty(namedefaultValue);
            }
        };
        return AccessController.doPrivileged(action);
    }
    private static ModelControllerClient newModelControllerClient() throws Exception {
        return ModelControllerClient.Factory.create(.getHostAddress(), null);
    }
        return ServerDeploymentManager.Factory.create(client);
    }
    @Override
    public void addJaspiSecurityDomain(String nameString loginModuleStackNameMap<StringStringloginModuleOptions,
            String authModuleNameMap<StringStringauthModuleOptionsthrows Exception {
        throw new UnsupportedOperationException("Jaspi authenticaiton is only supported after Wildfly-8.0.0.Final");
    }
New to GrepCode? Check out our FAQ X