Author: jbonofre Date: Tue Oct 25 07:05:37 2011 New Revision: 1188542 URL: http://svn.apache.org/viewvc?rev=1188542&view=rev Log: Add new utils classes. Add the WSDD descriptor.
Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/CommandUtils.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/FileUtils.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/PublisherUtils.java incubator/kalumet/trunk/agent/src/main/resources/ incubator/kalumet/trunk/agent/src/main/resources/apache-kalumet.wsdd incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/J2EEApplicationClient.java - copied, changed from r1187742, incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/ApplicationClient.java Removed: incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/ApplicationClient.java Modified: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java Modified: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java?rev=1188542&r1=1188541&r2=1188542&view=diff ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java (original) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java Tue Oct 25 07:05:37 2011 @@ -93,7 +93,7 @@ public final class Main { // start the WS server try { int port = kalumet.getAgent(agentId).getPort(); - WsServer wsServer = new WsServer(port, "/kalumet-ws-server.wsdd"); + WsServer wsServer = new WsServer(port, "/apache-kalumet.wsdd"); wsServer.start(); LOGGER.info("WS server started on {}", port); } catch (Exception e) { Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/CommandUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/CommandUtils.java?rev=1188542&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/CommandUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/CommandUtils.java Tue Oct 25 07:05:37 2011 @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.kalumet.agent.utils; + +import org.apache.kalumet.KalumetException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Util to execute system commands. + */ +public class CommandUtils { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(CommandUtils.class); + + /** + * Execute a system command and return the output. + * + * @param command the system command to execute. + * @return he command execution output. + * @throws KalumetException in case of execution failure. + */ + public static String execute(String command) throws KalumetException { + LOGGER.info("Executing {}", command); + String[] shellCommand = null; + LOGGER.debug("Create the shell depending the shell"); + String osName = System.getProperty("os.name"); + if (osName.startsWith("Windows")) { + LOGGER.debug("MS Windows platform detected"); + String comSpec = System.getProperty("ComSpec"); + if (comSpec != null) { + LOGGER.debug("ComSpec MS Windows environment variable found"); + shellCommand = new String[]{comSpec, "/C", command}; + } else { + LOG.debug("ComSpec MS Windows environment variable is not defined, found the shell command depending of the MS Windows version."); + if (osName.startsWith("Windows 3") || osName.startsWith("Windows 95") || osName.startsWith("Windows 98") || osName.startsWith("Windows ME")) { + LOG.debug("MS Windows 3.1/95/98/Me detected, using: command.com /C " + command); + shellCommand = new String[]{"command.com", "/C", command}; + } else { + LOG.debug("MS Windows NT/XP/Vista detected, using: cmd.exe /C " + command); + shellCommand = new String[]{"cmd.exe", "/C", command}; + } + } + } else { + LOG.debug("Unix platform detected."); + String shell = System.getProperty("SHELL"); + if (shell != null) { + LOG.debug("SHELL Unix environment variable is defined, using it: " + shell + " -c " + command); + shellCommand = new String[]{shell, "-c", command}; + } else { + LOG.debug("SHELL Unix environment variable is not defined, using the default Unix shell: /bin/sh -c " + command); + shellCommand = new String[]{"/bin/sh", "-c", command}; + } + + } + try { + Runtime runtime = Runtime.getRuntime(); + // launch the system command + Process process = runtime.exec(shellCommand); + // get the error stream gobbler + StringBuffer errorBuffer = new StringBuffer(); + StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), errorBuffer); + // get the output stream gobbler + StringBuffer outputBuffer = new StringBuffer(); + StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), outputBuffer); + // start both gobblers + errorGobbler.start(); + outputGobbler.start(); + // wait the end of the process + int exitValue = process.waitFor(); + if (exitValue != 0) { + // an error occurs + LOG.error("Command {} execution failed: {}", command, errorBuffer.toString()); + throw new AutoDeployException("Command " + command + " execution failed: " + errorBuffer.toString()); + } + // command is OK + LOG.info("Command {} has been executed successfully", command); + LOG.debug(outputBuffer.toString()); + return outputBuffer.toString(); + } catch (Exception exception) { + LOG.error("Command {} execution failed", command, exception); + throw new AutoDeployException("Command " + command + " execution failed", exception); + } + + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/FileUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/FileUtils.java?rev=1188542&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/FileUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/FileUtils.java Tue Oct 25 07:05:37 2011 @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.kalumet.agent.utils; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.vfs.FileObject; +import org.apache.commons.vfs.FileType; +import org.apache.kalumet.FileManipulator; +import org.apache.kalumet.ws.client.SimplifiedFileObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.util.Date; + +/** + * Utils to manipulate file (view, browse) + */ +public class FileUtils { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(FileUtils.class); + + /** + * Wrapper method to read a file. + * + * @param path the file VFS path. + * @return the file content. + */ + public static String view(String path) { + String content = null; + try { + // get a file manipulator instance + FileManipulator fileManipulator = FileManipulator.getInstance(); + // get the file content + InputStream stream = fileManipulator.read(path); + // populate the content string + content = IOUtils.toString(stream); + } + catch (Exception e) { + LOGGER.warn("Can't view {}", path, e); + } + return content; + } + + /** + * Wrapper method to browse a path. + * + * @param path the path to browse. + * @return the list of children. + */ + public static SimplifiedFileObject[] browse(String path) { + SimplifiedFileObject[] children = null; + try { + // get a file manipulator instance + FileManipulator fileManipulator = FileManipulator.getInstance(); + // get the path children + FileObject[] fileObjects = fileManipulator.browse(path); + children = new SimplifiedFileObject[fileObjects.length]; + for (int i = 0; i < fileObjects.length; i++) { + SimplifiedFileObject file = new SimplifiedFileObject(); + file.setName(fileObjects[i].getName().getBaseName()); + file.setPath(fileObjects[i].getName().getPath()); + file.setFile(fileObjects[i].getType().equals(FileType.FILE)); + file.setLastModificationDate(new Date(fileObjects[i].getContent().getLastModifiedTime())); + if (fileObjects[i].getType().equals(FileType.FILE)){ + file.setSize(fileObjects[i].getContent().getSize()); + } else { + file.setSize(0); + } + children[i] = file; + } + } catch (Exception e) { + LOGGER.warn("Can't browse {}", path, e); + } + return children; + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/PublisherUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/PublisherUtils.java?rev=1188542&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/PublisherUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/PublisherUtils.java Tue Oct 25 07:05:37 2011 @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.kalumet.agent.utils; + +import org.apache.commons.io.FileUtils; +import org.apache.kalumet.FileManipulator; +import org.apache.kalumet.model.Destination; +import org.apache.kalumet.model.Email; +import org.apache.kalumet.model.Environment; +import org.apache.kalumet.model.update.UpdateLog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import java.io.File; +import java.io.FileOutputStream; +import java.util.Iterator; +import java.util.LinkedList; + +/** + * Util to publish update results. + */ +public class PublisherUtils { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(PublisherUtils.class); + + private final static String XSL_LOCATION = "/templates/publisher.xsl"; + + /** + * Send an e-mail with the result of the update. + * + * @param environment the target environment. + */ + public static void publish(Environment environment) { + LOGGER.debug("Publish update result for environment {}", environment.getName()); + try { + LOGGER.debug("Iterate in the publishers list"); + for (Iterator publisherIterator = environment.getPublishers().iterator(); publisherIterator.hasNext(); ) { + Email email = (Email) publisherIterator.next(); + LOGGER.debug("Construct the addresses list"); + LinkedList addresses = new LinkedList(); + for (Iterator destinationIterator = email.getDestinations().iterator(); destinationIterator.hasNext(); ) { + Destination destination = (Destination) destinationIterator.next(); + addresses.add(VariableUtils.replace(destination.getAddress(), environment.getVariables())); + } + LOGGER.debug("Generate the publish e-mail content"); + String xslFile = null; + try { + xslFile = PublisherUtils.class.getResource(XSL_LOCATION).toString(); + } catch (Exception e) { + LOGGER.warn("Can't load publisher XSL file from {}", XSL_LOCATION, e); + xslFile = null; + } + String environmentCacheDir = FileManipulator.createEnvironmentCacheDir(environment); + if (xslFile != null && xslFile.trim().length() > 0) { + LOGGER.debug("XSL transformation file found, generate and send a HTML e-mail"); + // create the XSL transformer + String inputFile = environmentCacheDir + "/" + UpdateLog.MAIN_LOG_FILE; + LOGGER.debug("XSL input file: {}", inputFile); + String outputFile = environmentCacheDir + "/cache.html"; + LOGGER.debug("XSL output file: {}", outputFile); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslFile)); + transformer.transform(new StreamSource(inputFile), new StreamResult(new FileOutputStream(outputFile))); + EmailUtils.sendHTMLEmail(VariableUtils.replace(email.getMailhost(), environment.getVariables()), VariableUtils.replace(email.getFrom(), environment.getVariables()), "AutoDeploy Update Report - Environment " + environment.getName(), addresses, (String) FileUtils.readFileToString(new File(outputFile), null)); + } else { + LOGGER.debug("No XSL transformation file found, send a text e-mail"); + EmailUtils.sendTextEmail(VariableUtils.replace(email.getMailhost(), environment.getVariables()), VariableUtils.replace(email.getFrom(), environment.getVariables()), "AutoDeploy Update Report - Environmment " + environment.getName(), addresses, (String) FileUtils.readFileToString(new File(environmentCacheDir + "/" + UpdateLog.MAIN_LOG_FILE))); + } + } + } catch (Exception e) { + LOGGER.warn("Can't publish update report", e); + } + } + +} Added: incubator/kalumet/trunk/agent/src/main/resources/apache-kalumet.wsdd URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/resources/apache-kalumet.wsdd?rev=1188542&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/resources/apache-kalumet.wsdd (added) +++ incubator/kalumet/trunk/agent/src/main/resources/apache-kalumet.wsdd Tue Oct 25 07:05:37 2011 @@ -0,0 +1,155 @@ +<?xml version="1.0" encoding="UTF-8"?> +<deployment name="defaultClientConfig" + xmlns="http://xml.apache.org/axis/wsdd/" + xmlns:java="http://xml.apache.org/axis/wsdd/providers/java" + xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler"> + + <globalConfiguration> + <parameter name="disablePrettyXML" value="true"/> + <parameter name="dotNetSoapEncFix" value="true"/> + <parameter name="enableNamespacePrefixOptimization" value="false"/> + <requestFlow> + <handler type="java:org.apache.axis.handlers.JWSHandler"> + <parameter name="scope" value="session"/> + </handler> + <handler type="java:org.apache.axis.handlers.JWSHandler"> + <parameter name="scope" value="request"/> + <parameter name="extension" value=".jwr"/> + </handler> + </requestFlow> + </globalConfiguration> + + <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/> + <handler type="java:org.apache.axis.transport.local.LocalResponder" name="LocalResponder"/> + <handler type="java:org;apache.axis.handlers.SimpleAuthenticationHandler" name="Authenticate"/> + + <service name="WsServerAdminService" provider="java:MSG"> + <namespace>http://xml.apache.org/axis/wsdd/</namespace> + <parameter name="allowedMethods" value="AdminService"/> + <parameter name="enableRemoteAdmin" value="false"/> + <parameter name="className" value="org.apache.axis.utils.Admin"/> + </service> + + <service name="EnvironmentService" provider="java:RPC"> + <parameter name="allowedMethods" value="update"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.EnvironmentUpdater"/> + </service> + + <service name="J2EEApplicationServerService" provider="java:RPC"> + <parameter name="allowedMethods" value="start + stop + status + update"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.J2EEApplicationServerUpdater"/> + </service> + + <service name="J2EEApplicationService" provider="java:RPC"> + <parameter name="allowedMethods" value="update"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.J2EEApplicationUpdater"/> + </service> + + <service name="ArchiveService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.ArchiveUpdater"/> + </service> + + <service name="ConfigurationFileService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.ConfigurationFileUpdater"/> + </service> + + <service name="ContentManagerService" provider="java:RPC"> + <parameter name="allowedMethods" value="update"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.ContentManagerUpdater"/> + </service> + + <service name="DatabaseService" provider="java:RPC"> + <parameter name="allowedMethods" value="update"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.DatabaseUpdater"/> + </service> + + <service name="JDBCConnectionPoolService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.JDBCConnectionPoolUpdater"/> + </service> + + <service name="JDBCDataSourceService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.JDBCDataSourceUpdater"/> + </service> + + <service name="JMSConnectionFactoryService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.JMSConnectionFactoryUpdater"/> + </service> + + <service name="JMSServerService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.JMSServerUpdater"/> + </service> + + <service name="JNDIBindingService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.JNDIBindingUpdater"/> + </service> + + <service name="SharedLibraryService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + check"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.SharedLibraryUpdater"/> + </service> + + <service name="SqlScriptService" provider="java:RPC"> + <parameter name="allowedMethods" value="execute"/> + <parameter name="className" value="org.apache.kalumet.agent.updater.SqlScriptUpdater"/> + </service> + + <service name="SoftwareService" provider="java:RPC"> + <parameter name="allowedMethods" value="update + executeCommand + updateLocation + updateConfigurationFile + updateDatabase"/> + <parameter name="className" value="org.apache.kalumet.updater.SoftwareUpdater"/> + </service> + + <service name="FileService" provider="java:RPC"> + <parameter name="allowedMethods" value="view + browse"/> + <parameter name="className" value="org.apache.kalumet.agent.utils.FileUtils"/> + </service> + + <service name="CommandService" provider="java:RPC"> + <parameter name="allowedMethods" value="execute"/> + <parameter name="className" value="org.apache.kalumet.agent.utils.CommandUtils"/> + </service> + + <service name="AgentService" provider="java:RPC"> + <parameter name="allowedMethods" value="getVersion"/> + <parameter name="className" value="org.apache.kalumet.agent.utils.AgentUtils"/> + </service> + + <transport name="http"> + <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/> + <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/> + <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/> + <requestFlow> + <handler type="URLMapper"/> + <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> + </requestFlow> + </transport> + + <transport name="local"> + <responseFlow> + <handler type="LocalResponder"/> + </responseFlow> + </transport> + +</deployment> \ No newline at end of file Copied: incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/J2EEApplicationClient.java (from r1187742, incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/ApplicationClient.java) URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/J2EEApplicationClient.java?p2=incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/J2EEApplicationClient.java&p1=incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/ApplicationClient.java&r1=1187742&r2=1188542&rev=1188542&view=diff ============================================================================== --- incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/ApplicationClient.java (original) +++ incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/ws/client/J2EEApplicationClient.java Tue Oct 25 07:05:37 2011 @@ -22,7 +22,7 @@ package org.apache.kalumet.ws.client; /** * J2EEApplication WS client. */ -public class ApplicationClient extends AbstractClient { +public class J2EEApplicationClient extends AbstractClient { /** * Default constructor. @@ -31,8 +31,8 @@ public class ApplicationClient extends A * @param port port number of the Kalumet agent WS server. * @throws ClientException in case of communication failure. */ - public ApplicationClient(String host, int port) throws ClientException { - super("http://" + host + ":" + port + "/axis/services/J2EEApplicationServer"); + public J2EEApplicationClient(String host, int port) throws ClientException { + super("http://" + host + ":" + port + "/axis/services/J2EEApplicationService"); } /**