/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. 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.
*/
package org.jvnet.glassfish.comms.admin.cli.extensions.commands;
import com.sun.enterprise.cli.framework.*;
import com.sun.enterprise.cli.commands.*;
import com.sun.enterprise.admin.common.JMXFileTransfer;
import javax.management.MBeanServerConnection;
import com.sun.enterprise.util.SystemPropertyConstants;
import javax.management.ObjectName;
import java.util.Vector;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.net.URLClassLoader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class DCRCommand extends S1ASCommand
{
private static final String DCRFILE_OPTION = "dcrfile";
private static final String CREATE_CLBCONFIG_OP = "createConvergedLbConfig";
private static final String SET_DCRFILE_OP = "setDcrFile";
private static final String DCR_PLUGIN_INTERFACE = "org.glassfish.comms.api.datacentric.DcrPlugin";
/**
* An abstract method that Executes the command
* @throws CommandException
*/
public void runCommand() throws CommandException, CommandValidationException
{
validateOptions();
try {
String operationName = getOperationName();
String dcrFileLocation = "";
boolean upload = true;
if (operationName.equals(CREATE_CLBCONFIG_OP)) {
dcrFileLocation = getCLOption(DCRFILE_OPTION);
if (dcrFileLocation == null)
upload = false;
} else if (operationName.equals(SET_DCRFILE_OP))
dcrFileLocation = (String) operands.get(0);
// upload the dcr file to the das config directory
MBeanServerConnection mbsc = getMBeanServerConnection(getHost(), getPort(),
getUser(), getPassword());
String remoteLocation = null;
if (upload) {
remoteLocation = uploadFileToServer(mbsc, dcrFileLocation);
CLILogger.getInstance().printDebugMessage("uploadedFile = " +
remoteLocation);
}
// update the converged-lb-policy element with dcrfile attribute
String objectName = getObjectName();
Object[] params = getParamsInfo();
String[] types = getTypesInfo();
Object returnValue = mbsc.invoke(new ObjectName(objectName),
operationName, params, types);
handleReturnValue(returnValue);
CLILogger.getInstance().printDetailMessage(getLocalizedString(
"CommandSuccessful", new Object[] {name}));
} catch(Exception e) {
displayExceptionMessage(e);
}
}
/**
*Uploads file to temp location on server.
*@throws CommandException
*/
private String uploadFileToServer(MBeanServerConnection mbsc, String dcrFileLocation)
throws CommandException, IOException
{
Vector operands = getOperands();
File file = new File(dcrFileLocation);
if (!file.exists()) {
String msg = getLocalizedString("InvalidDcrFile");
throw new CommandException(msg);
} else if (file.isDirectory()) {
String msg = getLocalizedString("DcrFileIsDirectory");
throw new CommandException(msg);
} else if (file.length() == 0) {
String msg = getLocalizedString("EmptyDcrFile");
throw new CommandException(msg);
}
try {
validateDCR(file);
} catch (CommandException e) {
String msg = getLocalizedString("InvalidDcrDTDCheck");
throw new CommandException(msg + " " + e.getMessage());
}
return new JMXFileTransfer(mbsc).uploadFile(dcrFileLocation, "config");
}
private void validateDCR(File dcrFile) throws CommandException {
if (dcrFile.getName().endsWith(".jar"))
validateDCRJar(dcrFile);
else if (dcrFile.getName().endsWith(".xml"))
validateDCRXml(dcrFile);
else
throw new CommandException(getLocalizedString("InvalidDCRFileExtension"));
}
private void validateDCRJar(File dcrFile) throws CommandException {
try {
JarFile jarFile = new JarFile(dcrFile);
if (jarFile == null)
throw new CommandException(getLocalizedString("InvalidJarFile"));
Manifest mf = jarFile.getManifest();
if (mf == null)
throw new CommandException(getLocalizedString("InvalidJarFileNoMF"));
Attributes attr = mf.getMainAttributes();
String str = (String) attr.getValue("Dcr-Plugin-Class");
if (str == null)
throw new CommandException(getLocalizedString("InvalidJarFileNoMFKey"));
URL[] urls = { new URL("jar:file:" + dcrFile.getPath() + "!/") };
URLClassLoader urlCL = URLClassLoader.newInstance(urls);
Class inf[] = urlCL.loadClass(str).getInterfaces();
boolean valid = false;
for (int i=0; i< inf.length; i++) {
if (inf[i].getName().equals(DCR_PLUGIN_INTERFACE)) {
valid = true;
break;
}
}
if (!valid)
throw new CommandException(getLocalizedString("InvalidJarFileNoDCRPluginInterface", new Object[] {str, DCR_PLUGIN_INTERFACE}));
} catch (ClassNotFoundException cnfe) {
throw new CommandException(getLocalizedString("InvalidJarFileNoDCRPluginClass"));
} catch (IOException ioe) {
throw new CommandException(ioe.getMessage());
}
}
private void validateDCRXml(File dcrFile) throws CommandException {
try {
FileInputStream in = new FileInputStream(dcrFile);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new ErrorHandler());
db.setEntityResolver(new ErrorHandler());
db.parse(in);
} catch (SAXException se) {
throw new CommandException(se.getMessage());
} catch (ParserConfigurationException pce) {
throw new CommandException(pce.getMessage());
} catch (IOException ioe) {
throw new CommandException(ioe.getMessage());
}
}
final static class ErrorHandler extends DefaultHandler {
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
final String DCR_DTD_FILE = "lib" + File.separator + "dtds" +
File.separator + "sun-data-centric-rule_1_0.dtd";
String installDir = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
String customSystemId = installDir + File.separator + DCR_DTD_FILE;
InputSource is = new InputSource(new FileInputStream(new File(customSystemId)));
return is;
}
public void error(SAXParseException e) throws SAXException {
throw new SAXException(e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
throw new SAXException(e.getMessage());
}
}
}