org.glassfish.deployment.client.RemoteDeploymentFacility Maven / Gradle / Ivy
/*
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.deployment.client;
import com.sun.enterprise.admin.cli.remote.CLIRemoteCommand;
import com.sun.enterprise.cli.framework.CommandException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
/**
* Implements DeploymentFacility, currently using the CLIRemoteCommand to work with the
* admin back-end.
*
* Because CLIRemoteCommand uses the http interface with the admin back-end it
* is connectionless. Clients of RemoteDeploymentFacility must still invoke
* {@link #connect} before attempting to use the DF.
*
* @author tjquinn
*/
public class RemoteDeploymentFacility extends AbstractDeploymentFacility implements DeploymentFacility, TargetOwner {
private File passwordFile;
protected boolean doConnect() {
passwordFile = preparePasswordFile();
return true;
}
public boolean doDisconnect() {
passwordFile.delete();
return true;
}
private File preparePasswordFile() {
File pwFile = null;
try {
pwFile = File.createTempFile("rdf", ".dat");
PrintStream ps = new PrintStream(pwFile);
ps.println("AS_ADMIN_PASSWORD=" + getTargetDAS().getPassword());
ps.close();
return pwFile;
} catch (IOException ex) {
if (pwFile != null) {
pwFile.delete();
}
throw new RuntimeException(ex);
}
}
@Override
protected DFCommandRunner getDFCommandRunner(
String commandName,
Map commandOptions,
String[] operands) throws CommandException {
return new RemoteCommandRunner(commandName, commandOptions, operands);
}
private class RemoteCommandRunner implements DFCommandRunner {
private final String commandName;
private final Map commandOptions;
private final String[] operands;
private RemoteCommandRunner(
String commandName,
Map commandOptions,
String[] operands) {
this.commandOptions = commandOptions;
this.commandName = commandName;
this.operands = operands;
}
public DFDeploymentStatus run() throws CommandException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
String[] commandArgs = prepareRemoteCommandArguments(
commandName,
commandOptions,
operands
);
CLIRemoteCommand rc = new CLIRemoteCommand(commandArgs, "/xml", baos);
rc.runCommand();
DFDeploymentStatus status = CommandXMLResultParser.parse(new ByteArrayInputStream(baos.toByteArray()));
return status;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
/**
* Assembles an argument list suitable for use by CLIRemoteCommand from the
* command, options, and operand.
* @param commandName the command to execute
* @param options Map, with each key an option name and each value (optionally) the corresponding option value
* @param operands the operands to the command
* @return argument list for CLIRemoteCommand
*/
protected String[] prepareRemoteCommandArguments(
String commandName,
Map options,
String[] operands) {
ArrayList result = new ArrayList();
result.add(commandName);
if (options == null) {
options = Collections.EMPTY_MAP;
}
for (Map.Entry entry : options.entrySet()) {
result.add("--" + entry.getKey() + "=" + convertValue(entry.getValue()));
}
/*
* Add the authentication information from the
* caller-provided connection identifier.
*/
ServerConnectionIdentifier targetDAS = getTargetDAS();
if (targetDAS.isSecure()) {
result.add("--secure");
}
result.add("--host=" + targetDAS.getHostName());
result.add("--port=" + targetDAS.getHostPort());
result.add("--user=" + targetDAS.getUserName());
/*
* If we were typing the passwordfile option we would need to enclose
* the name in quote marks in case it had embedded spaces. Because
* we are essentially doing the shell's command-line parsing ourselves
* by placing options and their values into the argument list we do not
* need to do the quoting. If we did then the quote marks would be
* treated as part of the file spec and the file would not be found.
*/
result.add("--passwordfile=" + passwordFile.getAbsolutePath());
if (operands != null) {
for (String o : operands) {
result.add(o);
}
}
return result.toArray(new String[result.size()]);
}
private Object convertValue(Object value) {
if (value instanceof Properties) {
StringBuilder sb = new StringBuilder();
Properties p = (Properties) value;
for (Map.Entry