package jboss51deployer; import java.io.File; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import org.jboss.deployers.spi.management.deploy.DeploymentManager; import org.jboss.deployers.spi.management.deploy.DeploymentProgress; import org.jboss.deployers.spi.management.deploy.DeploymentStatus; import org.jboss.profileservice.spi.ProfileKey; import org.jboss.profileservice.spi.ProfileService; /** * * @author alitokmen */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { try { new Main().deployAndUndeploy("test.war", new File("/Users/alitokmen/Documents/CARGO/resources/testdata/simple-war/target/simple-war-1.0.3-SNAPSHOT.war")); } catch (Exception ex) { ex.printStackTrace(); } } public void deployAndUndeploy(String deploymentName, File deploymentFile) throws Exception { DeploymentManager deployMgr = getDeploymentManager(); if(deployMgr == null) throw new IllegalStateException("Null deployment manager."); String[] repositoryNames = null; // Distribute a zipped file deployMgr.loadProfile(new ProfileKey(ProfileKey.DEFAULT)); DeploymentProgress distribute = deployMgr.distribute(deploymentName, deploymentFile.toURI().toURL(), true); // Run distribute.run(); // Check if the deploy failed checkFailed(distribute); // Get the deployed names repositoryNames = distribute.getDeploymentID().getRepositoryNames(); // Start the deployment DeploymentProgress start = deployMgr.start(repositoryNames); // Run start.run(); // checkFailed(start); // Stop the deployment DeploymentProgress stop = deployMgr.stop(repositoryNames); // Run stop.run(); // checkFailed(stop); // Remove the deployment DeploymentProgress remove = deployMgr.remove(repositoryNames); // Run remove.run(); // checkFailed(remove); } void checkFailed(DeploymentProgress progress) throws Exception { DeploymentStatus status = progress.getDeploymentStatus(); if(status.isFailed()) throw new RuntimeException("Failed to deploy", status.getFailure()); } DeploymentManager getDeploymentManager() throws Exception { ProfileService ps = getProfileService(); return ps.getDeploymentManager(); } ProfileService getProfileService() throws Exception { InitialContext ctx = getInitialContext(); return (ProfileService) ctx.lookup("ProfileService"); } InitialContext getInitialContext() throws Exception { Properties props = new Properties(); props.setProperty( Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(Context.PROVIDER_URL, "jnp://" + "localhost" + ":" + "1099"); props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); return new InitialContext(props); } }