You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kalumet-commits@incubator.apache.org by jb...@apache.org on 2011/10/29 09:37:45 UTC

svn commit: r1194852 [2/3] - in /incubator/kalumet/trunk: ./ agent/ agent/src/main/java/org/apache/kalumet/agent/updater/

Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationServerUpdater.java
URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationServerUpdater.java?rev=1194852&view=auto
==============================================================================
--- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationServerUpdater.java (added)
+++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationServerUpdater.java Sat Oct 29 09:37:44 2011
@@ -0,0 +1,700 @@
+/*
+ * 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.updater;
+
+import org.apache.kalumet.FileManipulator;
+import org.apache.kalumet.KalumetException;
+import org.apache.kalumet.agent.Configuration;
+import org.apache.kalumet.agent.utils.EventUtils;
+import org.apache.kalumet.controller.core.J2EEApplicationServerController;
+import org.apache.kalumet.controller.core.J2EEApplicationServerControllerFactory;
+import org.apache.kalumet.model.*;
+import org.apache.kalumet.model.update.UpdateLog;
+import org.apache.kalumet.model.update.UpdateMessage;
+import org.apache.kalumet.utils.CommandUtils;
+import org.apache.kalumet.utils.NotifierUtils;
+import org.apache.kalumet.utils.PublisherUtils;
+import org.apache.kalumet.utils.VariableUtils;
+import org.apache.kalumet.ws.client.ClientException;
+import org.apache.kalumet.ws.client.J2EEApplicationServerClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Update a J2EE application server.
+ */
+public class J2EEApplicationServerUpdater {
+
+    private static final transient Logger LOGGER = LoggerFactory.getLogger(J2EEApplicationServerUpdater.class);
+
+    /**
+     * Wrapper class to update a JEE application server (via WS).
+     *
+     * @param environmentName the target environment name.
+     * @param serverName      the target JEE application server name.
+     * @param delegation      flag indicates if the update is a atomic call or part of an update launched by another agent.
+     * @throws KalumetException if the JEE application server update fails.
+     */
+    public static void update(String environmentName, String serverName, boolean delegation) throws KalumetException {
+        LOGGER.info("J2EE application server {} update requested by WS", serverName);
+        // load configuration
+        LOGGER.debug("Loading configuration");
+        Kalumet kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION);
+        Environment environment = kalumet.getEnvironment(environmentName);
+        if (environment == null) {
+            LOGGER.error("Environment {} is not found in the configuration", environmentName);
+            throw new KalumetException("Environment " + environmentName + " is not found in the configuration");
+        }
+        J2EEApplicationServer applicationServer = environment.getJ2EEApplicationServers().getJ2EEApplicationServer(serverName);
+        if (applicationServer == null) {
+            LOGGER.error("J2EE application server {} is not found in environment {}", serverName, environmentName);
+            throw new KalumetException("J2EE application server " + serverName + " is not found in environment " + environmentName);
+        }
+        // update configuration cache
+        LOGGER.debug("Updating configuration cache");
+        Configuration.CONFIG_CACHE = kalumet;
+
+        EventUtils.post(environment, "UPDATE", "J2EE application server " + serverName + " update requested by WS");
+        UpdateLog updateLog = new UpdateLog("J2EE application server " + serverName + " update in progress ...", environment.getName(), environment);
+
+        if (!delegation) {
+            // it's not a delegation from another agent, send a notification and waiting for the count down
+            LOGGER.info("Send a notification and waiting for the count down");
+            EventUtils.post(environment, "UPDATE", "Send a notification and waiting for the count down");
+            NotifierUtils.waitAndNotify(environment);
+        }
+
+        try {
+            // launch the update
+            LOGGER.debug("Call J2EE application server updater");
+            J2EEApplicationServerUpdater.update(kalumet, environment, applicationServer, updateLog);
+        } catch (Exception e) {
+            // an error occurs
+            LOGGER.error("J2EE application server {} update failed", serverName, e);
+            EventUtils.post(environment, "ERROR", "J2EE application server " + serverName + " update failed: " + e.getMessage());
+            if (!delegation) {
+                // it's not a delegation from another agent, publish update result
+                updateLog.setStatus("J2EE application server " + serverName + " update failed");
+                updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application server " + serverName + " update failed: " + e.getMessage()));
+                PublisherUtils.publish(environment);
+            }
+            throw new UpdateException("J2EE application server " + serverName + " update failed", e);
+        }
+
+        // update is completed
+        LOGGER.info("J2EE application server {} updated", applicationServer.getName());
+        EventUtils.post(environment, "UPDATE", "J2EE application server " + serverName + " updated");
+
+        if (!delegation) {
+            // it's not a delegation from another agent, publish update result
+            if (updateLog.isUpdated()) {
+                updateLog.setStatus("J2EE application server " + serverName + " updated");
+            } else {
+                updateLog.setStatus("J2EE application server " + serverName + " already up to date");
+            }
+            updateLog.addUpdateMessage(new UpdateMessage("info", "Update completed"));
+            LOGGER.info("Publishing update report");
+            PublisherUtils.publish(environment);
+        }
+    }
+
+    /**
+     * Update a J2EE application server.
+     *
+     * @param kalumet     the main configuration.
+     * @param environment the target <code>Environment</code>.
+     * @param server      the target <code>ApplicationServer</code> to update.
+     * @param updateLog   the <code>UpdateLog</code> to use.
+     */
+    public static void update(Kalumet kalumet, Environment environment, J2EEApplicationServer server, UpdateLog updateLog) throws UpdateException {
+        String applicationServerJmxUrl = VariableUtils.replace(server.getJmxurl(), environment.getVariables());
+        LOGGER.info("Updating J2EE application server {}", server.getName());
+
+        if (!server.isActive()) {
+            LOGGER.info("J2EE application server {} is inactive, so not updated", server.getName());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " is inactive, so not updated"));
+            EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " is inactive, so not updated");
+            return;
+        }
+
+        if (server.getAgent() != null && server.getAgent().trim().length() > 0 &&
+                !server.getAgent().equals(Configuration.AGENT_ID)) {
+            // delegates the update to another agent
+            LOGGER.info("Delegating J2EE application server {} update to agent {}", server.getName(), server.getAgent());
+            EventUtils.post(environment, "UPDATE", "Delegating J2EE application server " + server.getName() + " update to agent " + server.getAgent());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "Delegating J2EE application server " + server.getName() + " update to agent " + server.getAgent()));
+            Agent delegationAgent = Configuration.CONFIG_CACHE.getAgent(server.getAgent());
+            if (delegationAgent == null) {
+                // the target agent is not found in the configuration
+                LOGGER.error("Agent {} not found in the configuration", server.getAgent());
+                throw new UpdateException("Agent " + server.getAgent() + " not found in the configuration");
+            }
+            try {
+                // request the update via WebService call
+                LOGGER.debug("Call J2EE application server WS");
+                J2EEApplicationServerClient client = new J2EEApplicationServerClient(delegationAgent.getHostname(), delegationAgent.getPort());
+                client.update(environment.getName(), server.getName(), true);
+            } catch (ClientException e) {
+                // an error occurs during the update on the remote agent
+                LOGGER.error("J2EE application server {} update failed", server.getName(), e);
+                throw new UpdateException("J2EE application server " + server.getName() + " update failed", e);
+            }
+            return;
+        }
+
+        EventUtils.post(environment, "UPDATE", "Updating J2EE application server " + server.getName());
+        updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " located " + applicationServerJmxUrl));
+
+        // update JDBC connection pools
+        LOGGER.info("Updating JDBC connection pools");
+        for (Iterator connectionPoolIterator = server.getJDBCConnectionPools().iterator(); connectionPoolIterator.hasNext(); ) {
+            JDBCConnectionPool connectionPool = (JDBCConnectionPool) connectionPoolIterator.next();
+            try {
+                JDBCConnectionPoolUpdater.update(environment, server, connectionPool, updateLog);
+            } catch (UpdateException updateException) {
+                // the JDBC connection pool update has failed
+                if (connectionPool.isBlocker()) {
+                    // connection pool is update blocker
+                    LOGGER.error("JDBC connection pool {} update failed", connectionPool.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "JDBC connection pool " + connectionPool.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "JDBC connection pool " + connectionPool.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("JDBC connection ool " + connectionPool.getName() + " update failed", updateException);
+                } else {
+                    // connection pool is not update blocker
+                    LOGGER.warn("JDBC connection pool {} update failed", connectionPool.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "JDBC connection pool " + connectionPool.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "JDBC connection pool " + connectionPool.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "JDBC connection pool " + connectionPool.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "JDBC connection pool " + connectionPool.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update JDBC data sources
+        LOGGER.info("Updating JDBC data sources");
+        for (Iterator dataSourceIterator = server.getJDBCDataSources().iterator(); dataSourceIterator.hasNext(); ) {
+            JDBCDataSource dataSource = (JDBCDataSource) dataSourceIterator.next();
+            try {
+                JDBCDataSourceUpdater.update(environment, server, dataSource, updateLog);
+            } catch (UpdateException updateException) {
+                // the JDBC data source update has failed
+                if (dataSource.isBlocker()) {
+                    // data source is update blocker
+                    LOGGER.error("JDBC data source {} udpate failed", dataSource.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "JDBC data source " + dataSource.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "JDBC data source " + dataSource.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("JDBC data source " + dataSource.getName() + " update failed", updateException);
+                } else {
+                    // data source is not update blocker
+                    LOGGER.warn("JDBC data source {} update failed", dataSource.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "JDBC data source " + dataSource.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "JDBC data source " + dataSource.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "JDBC data source " + dataSource.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "JDBC data source " + dataSource.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update JMS connection factories
+        LOGGER.info("Updating JMS connection factories");
+        for (Iterator jmsConnectionFactoryIterator = server.getJMSConnectionFactories().iterator(); jmsConnectionFactoryIterator.hasNext(); ) {
+            JMSConnectionFactory jmsConnectionFactory = (JMSConnectionFactory) jmsConnectionFactoryIterator.next();
+            try {
+                JMSConnectionFactoryUpdater.update(environment, server, jmsConnectionFactory, updateLog);
+            } catch (UpdateException updateException) {
+                // the JMS connection factory update has failed
+                if (jmsConnectionFactory.isBlocker()) {
+                    // JMS connection factory is update blocker
+                    LOGGER.error("JMS connection factory {} update failed", jmsConnectionFactory.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "JMS connection factory " + jmsConnectionFactory.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "JMS connection factory " + jmsConnectionFactory.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("JMS connection factory " + jmsConnectionFactory.getName() + " update failed: " + updateException.getMessage(), updateException);
+                } else {
+                    // JMS connection factory is not update blocker
+                    LOGGER.warn("JMS connection factory {} update failed", jmsConnectionFactory.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "JMS connection factory " + jmsConnectionFactory.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "JMS connection factory " + jmsConnectionFactory.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "JMS connection factory " + jmsConnectionFactory.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "JMS connection factory " + jmsConnectionFactory.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update JMS servers
+        LOGGER.info("Updating JMS servers");
+        for (Iterator jmsServerIterator = server.getJMSServers().iterator(); jmsServerIterator.hasNext(); ) {
+            JMSServer jmsServer = (JMSServer) jmsServerIterator.next();
+            try {
+                JMSServerUpdater.update(environment, server, jmsServer, updateLog);
+            } catch (UpdateException updateException) {
+                // the JMS server update has failed
+                if (jmsServer.isBlocker()) {
+                    // JMS server is update blocker
+                    LOGGER.error("JMS server {} update failed", jmsServer.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "JMS server " + jmsServer.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "JMS server " + jmsServer.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("JMS server " + jmsServer.getName() + " update failed", updateException);
+                } else {
+                    // JMS server is not update blocker
+                    LOGGER.warn("JMS server {} update failed", jmsServer.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "JMS server " + jmsServer.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "JMS server " + jmsServer.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "JMS server " + jmsServer.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "JMS server " + jmsServer.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update JNDI name space bindings
+        LOGGER.info("Updating JNDIbindings");
+        for (Iterator jndiBindingsIterator = server.getJNDIBindings().iterator(); jndiBindingsIterator.hasNext(); ) {
+            JNDIBinding jndiBinding = (JNDIBinding) jndiBindingsIterator.next();
+            try {
+                JNDIBindingUpdater.update(environment, server, jndiBinding, updateLog);
+            } catch (UpdateException updateException) {
+                // the JNDI binding update has failed
+                if (jndiBinding.isBlocker()) {
+                    // JNDIbinding is update blocker
+                    LOGGER.error("JNDI binding {} update failed", jndiBinding.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "JNDI binding " + jndiBinding.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "JNDI binding " + jndiBinding.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("JNDI binding " + jndiBinding.getName() + " update failed", updateException);
+                } else {
+                    // JNDI binding is not update blocker
+                    LOGGER.warn("JNDI binding {} update failed", jndiBinding.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "JNDI binding " + jndiBinding.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "JNDI binding " + jndiBinding.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "JNDI binding " + jndiBinding.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "JNDI binding " + jndiBinding.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update shared libraries
+        LOGGER.info("Updating shared libraries");
+        for (Iterator sharedLibraryIterator = server.getSharedLibraries().iterator(); sharedLibraryIterator.hasNext(); ) {
+            SharedLibrary sharedLibrary = (SharedLibrary) sharedLibraryIterator.next();
+            try {
+                SharedLibraryUpdater.update(environment, server, sharedLibrary, updateLog);
+            } catch (UpdateException updateException) {
+                // the shared library update has failed
+                if (sharedLibrary.isBlocker()) {
+                    // shared library is update blocker
+                    LOGGER.error("Shared library {} update failed", sharedLibrary.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "Shared library " + sharedLibrary.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "Shared library " + sharedLibrary.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("Shared library " + sharedLibrary.getName() + " update failed", updateException);
+                } else {
+                    // shared library is not update blocker
+                    LOGGER.warn("Shared library {} update failed", sharedLibrary.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "Shared library " + sharedLibrary.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "Shared library " + sharedLibrary.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "Shared library " + sharedLibrary.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "Shared library " + sharedLibrary.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update J2EE applications
+        LOGGER.info("Updating J2EE applications");
+        for (Iterator applicationIterator = server.getJ2EEApplications().iterator(); applicationIterator.hasNext(); ) {
+            J2EEApplication application = (J2EEApplication) applicationIterator.next();
+            try {
+                JEEApplicationUpdater.update(environment, server, application, updateLog);
+            } catch (UpdateException updateException) {
+                // the J2EE application update has failed
+                if (application.isBlocker()) {
+                    // J2EE application is update blocker
+                    LOGGER.error("J2EE application {} update failed", application.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application " + application.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "J2EE application " + application.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("J2EE application " + application.getName() + " update failed", updateException);
+                } else {
+                    // J2EE application is not update blocker
+                    LOGGER.warn("J2EE application {} update failed", application.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "J2EE application " + application.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application " + application.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "J2EE application " + application.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "J2EE application " + application.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // stop J2EE server
+        LOGGER.info("Shutting down J2EE application server");
+        try {
+            J2EEApplicationServerUpdater.stop(environment, server, updateLog);
+        } catch (UpdateException updateException) {
+            // the J2EE application server stop has failed
+            if (server.isBlocker()) {
+                // J2EE application server is update blocker
+                LOGGER.error("J2EE application server {} shutdown failed", server.getName(), updateException);
+                updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application server " + server.getName() + " shutdown failed: " + updateException.getMessage()));
+                EventUtils.post(environment, "ERROR", "J2EE application server " + server.getName() + " shutdown failed: " + updateException.getMessage());
+                throw new UpdateException("J2EE application server " + server.getName() + " shutdown failed: " + updateException.getMessage(), updateException);
+            } else {
+                // J2EE application server is not update blocker
+                LOGGER.warn("J2EE application server {} shutdown failed", server.getName(), updateException);
+                updateLog.addUpdateMessage(new UpdateMessage("warn", "J2EE application server " + server.getName() + " shutdown failed: " + updateException.getMessage()));
+                updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " is not update blocker, update continues"));
+                EventUtils.post(environment, "WARN", "J2EE application server " + server.getName() + " shutdown failed: " + updateException.getMessage());
+                EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " is not update blocker, update continues");
+            }
+        }
+
+        // clean the JEE application server cache
+        LOGGER.info("Clean J2EE application server cache directories");
+        try {
+            J2EEApplicationServerUpdater.cleanCaches(environment, server, updateLog);
+        } catch (UpdateException updateException) {
+            // the J2EE application server cache directories cleaning has failed
+            if (server.isBlocker()) {
+                // J2EE application server is update blocker
+                LOGGER.error("J2EE application server {} cache directories cleanup failed", server.getName(), updateException);
+                updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application server " + server.getName() + " cache directories cleanup failed: " + updateException.getMessage()));
+                EventUtils.post(environment, "ERROR", "J2EE application server " + server.getName() + " cache directories cleanup failed: " + updateException.getMessage());
+                throw new UpdateException("J2EE application server " + server.getName() + " cache directories cleanup failed", updateException);
+            } else {
+                // J2EE application server is not update blocker
+                LOGGER.warn("J2EE application server {} cache directories cleanup failed", server.getName(), updateException);
+                updateLog.addUpdateMessage(new UpdateMessage("warn", "J2EE application server " + server.getName() + " cache directories cleanup failed: " + updateException.getMessage()));
+                updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " is not update blocker, update continues"));
+                EventUtils.post(environment, "WARN", "J2EE application server " + server.getName() + " cache directories cleanup failed: " + updateException.getMessage());
+                EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " is not update blocker, update continues");
+            }
+        }
+
+        // start J2EE application server
+        LOGGER.info("Starting J2EE application server");
+        try {
+            J2EEApplicationServerUpdater.start(kalumet, environment, server, updateLog);
+        } catch (UpdateException updateException) {
+            // the J2EE application server start has failed
+            if (server.isBlocker()) {
+                // J2EE application server is update blocker
+                LOGGER.error("J2EE application server {} start failed", server.getName(), updateException);
+                updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application server " + server.getName() + " start failed: " + updateException.getMessage()));
+                EventUtils.post(environment, "ERROR", "J2EE application server " + server.getName() + " start failed: " + updateException.getMessage());
+                throw new UpdateException("J2EE application server " + server.getName() + " start failed", updateException);
+            } else {
+                // J2EE application server is not update blocker
+                LOGGER.warn("J2EE application server " + server.getName() + " start failed", updateException);
+                updateLog.addUpdateMessage(new UpdateMessage("warn", "J2EE application server " + server.getName() + " start failed: " + updateException.getMessage()));
+                updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " is not update blocker, update continues"));
+                EventUtils.post(environment, "WARN", "J2EE application server " + server.getName() + " start failed: " + updateException.getMessage());
+                EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " is not update blocker, update continues");
+            }
+        }
+
+        // update completed
+        EventUtils.post(environment, "UPDATE", "J2EE application server updated");
+    }
+
+    /**
+     * Shutdown a JEE server.
+     *
+     * @param environment the target <code>Environment</code>.
+     * @param server      the <code>J2EEApplicationServer</code> to stop.
+     * @param updateLog   the <code>UpdateLog</code> to use.
+     */
+    protected static void stop(Environment environment, J2EEApplicationServer server, UpdateLog updateLog) throws UpdateException {
+        // TODO delegate the JEE server stop to another agent is required
+        try {
+            if (!server.isUpdateRequireRestart() || !updateLog.isUpdated()) {
+                LOGGER.info("J2EE application server {} shutdown is not required", server.getName());
+                updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " shutdown is not required"));
+                EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " shutdown is not required");
+                return;
+            }
+            // the server restart is required
+            LOGGER.info("J2EE application server {} shutdown is required", server.getName());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " shutdown is required"));
+            EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " shutdown is required");
+            if (server.isUsejmxstop()) {
+                LOGGER.debug("J2EE application server shutdown is performed using JMX controller");
+                LOGGER.debug("Getting J2EE application server JMX controller");
+                J2EEApplicationServerController controller = J2EEApplicationServerControllerFactory.getController(environment, server);
+                controller.shutdown();
+                LOGGER.info("J2EE application server {} shutdown completed", server.getName());
+                EventUtils.post(environment, "UPDATE", "J2EE server " + server.getName() + " shutdown completed");
+                updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE server " + server.getName() + " shutdown completed"));
+                return;
+            }
+            LOGGER.debug("J2EE application server shutdown is performed using system command");
+            String output = CommandUtils.execute(VariableUtils.replace(server.getShutdowncommand(), environment.getVariables()));
+            LOGGER.info("J2EE application server " + server.getName() + " shutdown completed: " + output);
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " shutdown completed: " + output));
+            EventUtils.post(environment, "UPDATE", "JEE server " + server.getName() + " shutdown completed: " + output);
+        } catch (Exception exception) {
+            LOGGER.error("J2EE application server " + server.getName() + " shutdown failed", exception);
+            updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE server " + server.getName() + " shutdown failed: " + exception.getMessage()));
+            EventUtils.post(environment, "ERROR", "J2EE application server " + server.getName() + " shutdown failed: " + exception.getMessage());
+            throw new UpdateException("J2EE application server " + server.getName() + " shutdown failed", exception);
+        }
+    }
+
+    /**
+     * Start a J2EE application server.
+     *
+     * @param kalumet     the configuration.
+     * @param environment the target <code>Environment</code>.
+     * @param server      the <code>J2EEApplicationServer</code> to start.
+     * @param updateLog   the <code>UpdateLog</code> to use.
+     */
+    protected static void start(Kalumet kalumet, Environment environment, J2EEApplicationServer server, UpdateLog updateLog) throws UpdateException {
+        // TODO delegate the JEE server start to another agent is required
+        try {
+            if (!server.isUpdateRequireRestart() || !updateLog.isUpdated()) {
+                LOGGER.info("J2EE application server {} start is not required", server.getName());
+                EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " start is not required");
+                updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " start is required"));
+                return;
+            }
+
+            LOGGER.info("J2EE application server {} start is required", server.getName());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " start is required"));
+            EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " start is required");
+
+            // get the agent configuration
+            Agent agent = kalumet.getAgent(Configuration.AGENT_ID);
+
+            // check the agent max environment active
+            if (agent.getMaxj2eeapplicationserversstarted() > 0) {
+                // get the environments managed by the agent
+                List agentEnvironments = kalumet.getEnvironmentsByAgent(Configuration.AGENT_ID);
+                int applicationServersStarted = 0;
+                for (Iterator agentEnvironmentsIterator = agentEnvironments.iterator(); agentEnvironmentsIterator.hasNext(); ) {
+                    Environment agentEnvironment = (Environment) agentEnvironmentsIterator.next();
+                    // check if the application server started into the environment
+                    for (Iterator agentEnvironmentApplicationServersIterator = agentEnvironment.getJ2EEApplicationServers().getJ2EEApplicationServers().iterator(); agentEnvironmentApplicationServersIterator.hasNext(); ) {
+                        J2EEApplicationServer agentEnvironmentApplicationServer = (J2EEApplicationServer) agentEnvironmentApplicationServersIterator.next();
+                        // get the controller
+                        J2EEApplicationServerController controller = J2EEApplicationServerControllerFactory.getController(environment, server);
+                        if (!controller.isStopped()) {
+                            applicationServersStarted++;
+                            if (applicationServersStarted >= agent.getMaxj2eeapplicationserversstarted()) {
+                                // the max number of application servers started is raised
+                                throw new UpdateException("The maximum number of started J2EE application servers has been raised for the agent");
+                            }
+                        }
+                    }
+                }
+            }
+
+            // the start is performed using system command
+            String output = CommandUtils.execute(VariableUtils.replace(server.getStartupcommand(), environment.getVariables()));
+            // application server start has been performed
+            LOGGER.info("J2EE application server {} start completed: {}", server.getName(), output);
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " start completed: " + output));
+            EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " start completed: " + output);
+        } catch (Exception exception) {
+            LOGGER.error("J2EE application server {} start failed", server.getName(), exception);
+            updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application server " + server.getName() + " start failed: " + exception.getMessage()));
+            EventUtils.post(environment, "ERROR", "J2EE application server " + server.getName() + " start failed: " + exception.getMessage());
+            throw new UpdateException("J2EE application server " + server.getName() + " start failed", exception);
+        }
+    }
+
+    /**
+     * Cleanup J2EE application server caches.
+     *
+     * @param environment the <code>Environment</code>.
+     * @param server      the target <code>J2EEApplicationServer</code>.
+     * @param updateLog   the <code>UpdateLog</code> to use.
+     */
+    protected static void cleanCaches(Environment environment, J2EEApplicationServer server, UpdateLog updateLog) throws UpdateException {
+        try {
+            if (!server.isUpdateRequireCacheCleaning() || !updateLog.isUpdated()) {
+                LOGGER.info("J2EE application server {} caches cleaning is not required", server.getName());
+                updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " caches cleaning is not required"));
+                EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " caches cleaning is not required");
+                return;
+            }
+            // the application server caches cleaning is required
+            LOGGER.info("J2EE application server {} caches cleaning is required", server.getName());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application server " + server.getName() + " caches cleaning is required"));
+            EventUtils.post(environment, "UPDATE", "J2EE application server " + server.getName() + " caches cleaning is required");
+            // initializes the file manipulator instance
+            FileManipulator fileManipulator = FileManipulator.getInstance();
+            for (Iterator cacheIterator = server.getCaches().iterator(); cacheIterator.hasNext(); ) {
+                Cache cache = (Cache) cacheIterator.next();
+                String path = VariableUtils.replace(cache.getPath(), environment.getVariables());
+                fileManipulator.delete(path);
+            }
+        } catch (Exception exception) {
+            LOGGER.error("J2EE application server {} cache directories cleanup failed", server.getName(), exception);
+            updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application server" + server.getName() + " cache directories cleanup failed: " + exception.getMessage()));
+            EventUtils.post(environment, "ERROR", "J2EE application server " + server.getName() + " cache directories cleanup failed: " + exception.getMessage());
+            throw new UpdateException("J2EE application server " + server.getName() + " caches cleanup failed", exception);
+        }
+    }
+
+    /**
+     * Wrapper method to start J2EE application server (via WS).
+     *
+     * @param environmentName the target environment name.
+     * @param serverName      the target J2EE application server name.
+     */
+    public static void start(String environmentName, String serverName) throws KalumetException {
+        LOGGER.info("J2EE application server {} start requested by WS", serverName);
+
+        LOGGER.debug("Loading configuration");
+        Kalumet kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION);
+        Environment environment = kalumet.getEnvironment(environmentName);
+        if (environment == null) {
+            LOGGER.error("Environment {} is not found in the configuration", environmentName);
+            throw new UpdateException("Environment " + environmentName + " is not found in the configuration");
+        }
+        J2EEApplicationServer server = environment.getJ2EEApplicationServers().getJ2EEApplicationServer(serverName);
+        if (server == null) {
+            LOGGER.error("J2EE application server {} is not found in environment {}", serverName, environmentName);
+            throw new UpdateException("J2EE application server " + serverName + " is not found in environment " + environmentName);
+        }
+
+        // get the agent configuration
+        Agent agent = kalumet.getAgent(Configuration.AGENT_ID);
+
+        // check the agent max environment active
+        if (agent.getMaxj2eeapplicationserversstarted() > 0) {
+            // get the environments managed by the agent
+            List agentEnvironments = kalumet.getEnvironmentsByAgent(Configuration.AGENT_ID);
+            int applicationServersStarted = 0;
+            for (Iterator agentEnvironmentsIterator = agentEnvironments.iterator(); agentEnvironmentsIterator.hasNext(); ) {
+                Environment agentEnvironment = (Environment) agentEnvironmentsIterator.next();
+                // check if the application server started into the environment
+                for (Iterator agentEnvironmentApplicationServersIterator = agentEnvironment.getJ2EEApplicationServers().getJ2EEApplicationServers().iterator(); agentEnvironmentApplicationServersIterator.hasNext(); ) {
+                    J2EEApplicationServer agentEnvironmentApplicationServer = (J2EEApplicationServer) agentEnvironmentApplicationServersIterator.next();
+                    // get the controller
+                    J2EEApplicationServerController controller = J2EEApplicationServerControllerFactory.getController(environment, server);
+                    if (!controller.isStopped()) {
+                        applicationServersStarted++;
+                        if (applicationServersStarted >= agent.getMaxj2eeapplicationserversstarted()) {
+                            // the max number of application servers started is raised
+                            throw new KalumetException("The maximum number of started J2EE application servers has been raised for the agent");
+                        }
+                    }
+                }
+            }
+        }
+
+        EventUtils.post(environment, "INFO", "J2EE application server " + serverName + " start requested by WS");
+        // the start is performed using system command
+        String output = CommandUtils.execute(VariableUtils.replace(server.getStartupcommand(), environment.getVariables()));
+        // application server start has been performed
+        LOGGER.info("J2EE application server {} STARTED: {}", serverName, output);
+        EventUtils.post(environment, "INFO", "J2EE application server " + serverName + " started: " + output);
+    }
+
+    /**
+     * Wrapper method to stop J2EE application server (via WS).
+     *
+     * @param environmentName the environment name.
+     * @param serverName      the J2EE application server name.
+     */
+    public static void stop(String environmentName, String serverName) throws UpdateException {
+        LOGGER.info("J2EE application server {} shutdown requested by WS", serverName);
+        Kalumet kalumet;
+        try {
+            kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION);
+        } catch (KalumetException e) {
+            LOGGER.error("Can't load configuration", e);
+            throw new UpdateException("Can't load configuration", e);
+        }
+        Environment environment = kalumet.getEnvironment(environmentName);
+        if (environment == null) {
+            LOGGER.error("Environment {} is not found in the configuration", environmentName);
+            throw new UpdateException("Environment " + environmentName + " is not found in the configuration");
+        }
+        J2EEApplicationServer server = environment.getJ2EEApplicationServers().getJ2EEApplicationServer(serverName);
+        if (server == null) {
+            LOGGER.error("J2EE application server {} is not found in environment {}", serverName, environmentName);
+            throw new UpdateException("J2EE application server " + serverName + " is not found in environment " + environmentName);
+        }
+        EventUtils.post(environment, "INFO", "J2EE application server " + serverName + " shutdown requested by WS");
+        // check if the stop is made using JMX
+        try {
+            if (server.isUsejmxstop()) {
+                J2EEApplicationServerController controller = J2EEApplicationServerControllerFactory.getController(environment, server);
+                controller.shutdown();
+                LOGGER.info("J2EE application server {} shutdown using the controller", serverName);
+                EventUtils.post(environment, "INFO", "J2EE application server " + serverName + " shutdown using the controller");
+                return;
+            }
+        } catch (Exception e) {
+            LOGGER.error("J2EE application server {} shutdown failed", serverName, e);
+            throw new UpdateException("J2EE application server " + serverName + " shutdown failed", e);
+        }
+        // no JMX stop, use system command call
+        String shutdownCommand = VariableUtils.replace(server.getShutdowncommand(), environment.getVariables());
+        String output = null;
+        try {
+            output = CommandUtils.execute(shutdownCommand);
+        } catch (KalumetException e) {
+            LOGGER.error("J2EE application server {} shutdown FAILED.", serverName, e);
+            throw new UpdateException("J2EE application server " + serverName + " shutdown failed", e);
+        }
+        LOGGER.info("J2EE application server {} shutdown using system command: {}", serverName, output);
+        EventUtils.post(environment, "INFO", "J2EE application server " + serverName + " shutdown using system command: " + output);
+    }
+
+    /**
+     * Wrapper method to get J2EE application server status (via WS).
+     *
+     * @param environmentName       the environment name.
+     * @param applicationServerName the J2EE application server name.
+     * @return the J2EE application server current status.
+     */
+    public static String status(String environmentName, String applicationServerName) throws UpdateException {
+        // TODO delegate the JEE server status to another agnt if required
+        LOGGER.info("J2EE application server {} status check requested by WS", applicationServerName);
+
+        LOGGER.debug("Loading configuration");
+        Kalumet kalumet;
+        try {
+            kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION);
+        } catch (KalumetException e) {
+            LOGGER.error("Can't load configuration", e);
+            throw new UpdateException("Can't load configuration", e);
+        }
+        Environment environment = kalumet.getEnvironment(environmentName);
+        if (environment == null) {
+            LOGGER.error("Environment {} is not found in the configuration", environmentName);
+            throw new UpdateException("Environment " + environmentName + " is not found in the configuration");
+        }
+        J2EEApplicationServer server = environment.getJ2EEApplicationServers().getJ2EEApplicationServer(applicationServerName);
+        if (server == null) {
+            LOGGER.error("J2EE application server {} is not found in environment {}", applicationServerName, environmentName);
+            throw new UpdateException("J2EE application server " + applicationServerName + " is not found in environment " + environmentName);
+        }
+        EventUtils.post(environment, "INFO", "J2EE application server " + applicationServerName + " status requested by WS");
+        try {
+            // get the controller
+            J2EEApplicationServerController controller = J2EEApplicationServerControllerFactory.getController(environment, server);
+            // get the application server status
+            return controller.status();
+        } catch (Exception e) {
+            LOGGER.error("J2EE application server {} status check failed", applicationServerName, e);
+            throw new UpdateException("J2EE application server " + applicationServerName + " status check failed", e);
+        }
+    }
+
+}

Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationUpdater.java
URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationUpdater.java?rev=1194852&view=auto
==============================================================================
--- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationUpdater.java (added)
+++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/J2EEApplicationUpdater.java Sat Oct 29 09:37:44 2011
@@ -0,0 +1,282 @@
+/*
+ * 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.updater;
+
+import org.apache.kalumet.FileManipulator;
+import org.apache.kalumet.FileManipulatorException;
+import org.apache.kalumet.KalumetException;
+import org.apache.kalumet.agent.Configuration;
+import org.apache.kalumet.agent.utils.EventUtils;
+import org.apache.kalumet.model.*;
+import org.apache.kalumet.model.update.UpdateLog;
+import org.apache.kalumet.model.update.UpdateMessage;
+import org.apache.kalumet.utils.NotifierUtils;
+import org.apache.kalumet.utils.PublisherUtils;
+import org.apache.kalumet.utils.VariableUtils;
+import org.apache.kalumet.ws.client.ClientException;
+import org.apache.kalumet.ws.client.J2EEApplicationClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Iterator;
+
+/**
+ * J2EE application updater.
+ */
+public class J2EEApplicationUpdater {
+
+    private static final transient Logger LOGGER = LoggerFactory.getLogger(J2EEApplicationUpdater.class);
+
+    /**
+     * Update a J2EE application.
+     *
+     * @param environment the target <code>Environment</code>.
+     * @param server      the target <code>J2EEApplicationServer</code>.
+     * @param application the target <code>J2EEApplication</code>.
+     * @param updateLog   the <code>UpdateLog</code> to use.
+     * @throws UpdateException if the update failed.
+     */
+    public static void update(Environment environment, J2EEApplicationServer server, J2EEApplication application, UpdateLog updateLog) throws UpdateException {
+        LOGGER.info("Updating J2EE application {}", application.getName());
+
+        String applicationUri = VariableUtils.replace(application.getUri(), environment.getVariables());
+
+        updateLog.addUpdateMessage(new UpdateMessage("info", "Updating J2EE application " + application.getName()));
+        updateLog.addUpdateMessage(new UpdateMessage("summary", "J2EE application " + application.getName() + " located " + applicationUri));
+        EventUtils.post(environment, "UPDATE", "Updating J2EE application " + application.getName());
+
+        if (!application.isActive()) {
+            // the application is inactive, not updated
+            LOGGER.info("J2EE application {} is inactive, so not updated", application.getName());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application " + application.getName() + " is inactive, not updated"));
+            EventUtils.post(environment, "UPDATE", "J2EE application " + application.getName() + " is inactive, not updated");
+            return;
+        }
+
+        if (application.getAgent() != null && application.getAgent().trim().length() > 0
+                && !application.getAgent().equals(Configuration.AGENT_ID)) {
+            // delegates the application update to another agent
+            LOGGER.info("Delegating J2EE application {} update to agent {}", application.getName(), application.getAgent());
+            Agent delegationAgent = Configuration.CONFIG_CACHE.getAgent(application.getAgent());
+            EventUtils.post(environment, "UPDATE", "Delegating J2EE application " + application.getName() + " update to agent " + application.getAgent());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "Delegating J2EE application " + application.getName() + " update to agent " + application.getAgent()));
+            if (delegationAgent == null) {
+                LOGGER.error("Agent " + application.getAgent() + " is not found in the configuration");
+                throw new UpdateException("Agent " + application.getAgent() + " is not found in the configuration");
+            }
+            try {
+                LOGGER.debug("Call J2EE application WS");
+                J2EEApplicationClient webServiceClient = new J2EEApplicationClient(delegationAgent.getHostname(), delegationAgent.getPort());
+                webServiceClient.update(environment.getName(), server.getName(), application.getName(), true);
+            } catch (ClientException clientException) {
+                LOGGER.error("J2EE application {} update failed", application.getName(), clientException);
+                throw new UpdateException("J2EE application " + application.getName() + " update failed", clientException);
+            }
+            return;
+        }
+
+        try {
+            // create the application directory in the environment working directory
+            // (if needed)
+            LOGGER.debug("Creating the J2EE application directory");
+            String applicationCacheDir = FileManipulator.createJ2EEApplicationCacheDir(environment, application);
+        } catch (FileManipulatorException e) {
+            LOGGER.error("Can't create J2EE application cache directory", e);
+            throw new UpdateException("Can't create J2EE application cache directory", e);
+        }
+
+        // update configuration files
+        LOGGER.info("Updating J2EE application configuration files");
+        for (Iterator configurationFileIterator = application.getConfigurationFiles().iterator(); configurationFileIterator.hasNext(); ) {
+            ConfigurationFile configurationFile = (ConfigurationFile) configurationFileIterator.next();
+            try {
+                ConfigurationFileUpdater.update(environment, server, application, configurationFile, updateLog);
+            } catch (UpdateException updateException) {
+                // the configuration file update has failed
+                if (configurationFile.isBlocker()) {
+                    // the configuration file is update blocker
+                    LOGGER.error("Configuration file {} update failed", configurationFile.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "Configuration file " + configurationFile.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "Configuration file " + configurationFile.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("Configuration file " + configurationFile.getName() + " update failed", updateException);
+                } else {
+                    // the configuration file is not update blocker
+                    LOGGER.warn("Configuration file {} update failed", configurationFile.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "Configuration file " + configurationFile.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "Configuration file " + configurationFile.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "Configuration file " + configurationFile.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "Configuration file " + configurationFile.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update database
+        LOGGER.info("Updating J2EE application databases");
+        for (Iterator databaseIterator = application.getDatabases().iterator(); databaseIterator.hasNext(); ) {
+            Database database = (Database) databaseIterator.next();
+            try {
+                DatabaseUpdater.update(environment, server, application, database, updateLog);
+            } catch (UpdateException updateException) {
+                // the database update has failed
+                if (database.isBlocker()) {
+                    // the database is update blocker
+                    LOGGER.error("Database {} update failed", database.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "Database " + database.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "Database " + database.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("Database " + database.getName() + " update failed", updateException);
+                } else {
+                    // the database is not update blocker
+                    LOGGER.warn("Database {} update failed", database.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "Database " + database.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "Database " + database.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "Database " + database.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "Database " + database.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update content managers
+        LOGGER.info("Updating J2EE application content managers");
+        for (Iterator contentManagerIterator = application.getContentManagers().iterator(); contentManagerIterator.hasNext(); ) {
+            ContentManager contentManager = (ContentManager) contentManagerIterator.next();
+            try {
+                ContentManagerUpdater.update(environment, server, application, contentManager, updateLog);
+            } catch (UpdateException updateException) {
+                // the content manager update has failed
+                if (contentManager.isBlocker()) {
+                    // the content manager is update blocker
+                    LOGGER.error("Content manager {} update failed", contentManager.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "Content manager " + contentManager.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "Content manager " + contentManager.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("Content manager " + contentManager.getName() + " update failed", updateException);
+                } else {
+                    // the content manager is not update blocker
+                    LOGGER.warn("Content manager {} update failed", contentManager.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "Content manager " + contentManager.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "Content manager " + contentManager.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "Content manager " + contentManager.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "Content manager " + contentManager.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // update archives
+        LOGGER.info("Updating J2EE application archives");
+        for (Iterator archiveIterator = application.getArchives().iterator(); archiveIterator.hasNext(); ) {
+            Archive archive = (Archive) archiveIterator.next();
+            try {
+                ArchiveUpdater.update(environment, server, application, archive, updateLog);
+            } catch (UpdateException updateException) {
+                // the archive update has failed
+                if (archive.isBlocker()) {
+                    // the archive is update blocker
+                    LOGGER.error("Archive {} update failed", archive.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("error", "Archive " + archive.getName() + " update failed: " + updateException.getMessage()));
+                    EventUtils.post(environment, "ERROR", "Archive " + archive.getName() + " update failed: " + updateException.getMessage());
+                    throw new UpdateException("Archive " + archive.getName() + " update failed", updateException);
+                } else {
+                    // the archive is not update blocker
+                    LOGGER.warn("Archive {} update failed", archive.getName(), updateException);
+                    updateLog.addUpdateMessage(new UpdateMessage("warn", "Archive " + archive.getName() + " update failed: " + updateException.getMessage()));
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "Archive " + archive.getName() + " is not update blocker, update continues"));
+                    EventUtils.post(environment, "WARN", "Archive " + archive.getName() + " update failed: " + updateException.getMessage());
+                    EventUtils.post(environment, "UPDATE", "Archive " + archive.getName() + " is not update blocker, update continues");
+                }
+            }
+        }
+
+        // J2EE application update is completed
+        LOGGER.info("J2EE application {} updated", application.getName());
+        updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application " + application.getName() + " updated"));
+        EventUtils.post(environment, "UPDATE", "J2EE application " + application.getName() + " updated");
+    }
+
+    /**
+     * Wrapper method to update a J2EE application (via WS).
+     *
+     * @param environmentName the target environment name.
+     * @param serverName      the target J2EE application server name.
+     * @param applicationName the target J2EE application name.
+     * @param delegation      flag indicating if the update is a delegation from another agent (true), or a client call (false).
+     * @throws KalumetException in case of update error.
+     */
+    public static void update(String environmentName, String serverName, String applicationName, boolean delegation) throws KalumetException {
+        LOGGER.info("J2EE application {} update requested by WS", applicationName);
+
+        LOGGER.debug("Loading the configuration");
+        Kalumet kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION);
+        Environment environment = kalumet.getEnvironment(environmentName);
+        if (environment == null) {
+            LOGGER.error("The environment {} is not found in the the configuration", environmentName);
+            throw new KalumetException("The environment " + environmentName + " is not found in the the configuration");
+        }
+        J2EEApplicationServer applicationServer = environment.getJ2EEApplicationServers().getJ2EEApplicationServer(serverName);
+        if (applicationServer == null) {
+            LOGGER.error("The J2EE application server {} is not found in the environment {}", serverName, environmentName);
+            throw new KalumetException("The J2EE application server " + serverName + " is not found in the environment " + environmentName);
+        }
+        J2EEApplication application = applicationServer.getJ2EEApplication(applicationName);
+        if (application == null) {
+            LOGGER.error("The J2EE application {} is not found in the J2EE application server {}", applicationName, serverName);
+            throw new KalumetException("The J2EE application " + applicationName + " is not found in the J2EE application server " + serverName);
+        }
+
+        // update the agent cache
+        LOGGER.debug("Updating configuration cache");
+        Configuration.CONFIG_CACHE = kalumet;
+
+        EventUtils.post(environment, "UPDATE", "J2EE application {} update requested by WS", applicationName);
+        UpdateLog updateLog = new UpdateLog("J2EE application " + applicationName + " update in progress ...", environment.getName(), environment);
+
+        if (!delegation) {
+            // it's a client call
+            LOGGER.info("Send a notification and waiting for the count down");
+            EventUtils.post(environment, "UPDATE", "Send a notification and waiting for the count down");
+            NotifierUtils.waitAndNotify(environment);
+        }
+        try {
+            LOGGER.debug("Call J2EE application updater");
+            J2EEApplicationUpdater.update(environment, applicationServer, application, updateLog);
+        } catch (Exception e) {
+            LOGGER.error("J2EE application {} update failed", applicationName, e);
+            EventUtils.post(environment, "ERROR", "J2EE application " + applicationName + " udpate failed: " + e.getMessage());
+            if (!delegation) {
+                updateLog.setStatus("J2EE application " + applicationName + " update failed");
+                updateLog.addUpdateMessage(new UpdateMessage("error", "J2EE application " + applicationName + " update failed: " + e.getMessage()));
+                PublisherUtils.publish(environment);
+            }
+            throw new UpdateException("J2EE application " + applicationName + " update failed", e);
+        }
+
+        // update completed
+        LOGGER.info("J2EE application {} updated", application.getName());
+        EventUtils.post(environment, "UPDATE", "J2EE application " + application.getName() + " updated");
+        if (!delegation) {
+            if (updateLog.isUpdated()) {
+                updateLog.setStatus("J2EE application " + application.getName() + " updated");
+            } else {
+                updateLog.setStatus("J2EE application " + application.getName() + " already up to date");
+            }
+            updateLog.addUpdateMessage(new UpdateMessage("info", "J2EE application " + application.getName() + " updated"));
+            LOGGER.info("Publishing update report");
+            PublisherUtils.publish(environment);
+        }
+    }
+
+}

Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/JDBCConnectionPoolUpdater.java
URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/JDBCConnectionPoolUpdater.java?rev=1194852&view=auto
==============================================================================
--- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/JDBCConnectionPoolUpdater.java (added)
+++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/JDBCConnectionPoolUpdater.java Sat Oct 29 09:37:44 2011
@@ -0,0 +1,230 @@
+/*
+ * 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.updater;
+
+import org.apache.kalumet.KalumetException;
+import org.apache.kalumet.agent.Configuration;
+import org.apache.kalumet.agent.utils.EventUtils;
+import org.apache.kalumet.controller.core.J2EEApplicationServerController;
+import org.apache.kalumet.controller.core.J2EEApplicationServerControllerFactory;
+import org.apache.kalumet.model.Environment;
+import org.apache.kalumet.model.J2EEApplicationServer;
+import org.apache.kalumet.model.JDBCConnectionPool;
+import org.apache.kalumet.model.Kalumet;
+import org.apache.kalumet.model.update.UpdateLog;
+import org.apache.kalumet.model.update.UpdateMessage;
+import org.apache.kalumet.utils.NotifierUtils;
+import org.apache.kalumet.utils.PublisherUtils;
+import org.apache.kalumet.utils.VariableUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * JDBC connection pool updater.
+ */
+public class JDBCConnectionPoolUpdater {
+
+    private static final transient Logger LOGGER = LoggerFactory.getLogger(JDBCConnectionPoolUpdater.class);
+
+    /**
+     * Update a JDBC connection pool.
+     *
+     * @param environment    the target <code>Environment</code>.
+     * @param server         the target <code>J2EEApplicationServer</code>.
+     * @param connectionPool the target <code>JDBCConnectionPool</code>.
+     * @param updateLog      the target <code>UpdateLog</code> to use.
+     */
+    public static void update(Environment environment, J2EEApplicationServer server, JDBCConnectionPool connectionPool, UpdateLog updateLog) throws UpdateException {
+        LOGGER.info("Updating JDBC connection pool {}", connectionPool.getName());
+        updateLog.addUpdateMessage(new UpdateMessage("info", "Updating JDBC connection pool " + connectionPool.getName()));
+        EventUtils.post(environment, "UPDATE", "Updating JDBC connection pool " + connectionPool.getName());
+
+        if (!connectionPool.isActive()) {
+            LOGGER.info("JDBC connection pool {} is inactive, so not updated", connectionPool.getName());
+            updateLog.addUpdateMessage(new UpdateMessage("info", "JDBC connection pool " + connectionPool.getName() + " is inactive, so not updated"));
+            EventUtils.post(environment, "UPDATE", "JDBC connection pool " + connectionPool.getName() + " is inactive, so not updated");
+            return;
+        }
+
+        // replace variables in connection pool data
+        LOGGER.debug("Replacing variables in connection pool data");
+        String jdbcDriver = VariableUtils.replace(connectionPool.getDriver(), environment.getVariables());
+        String jdbcUser = VariableUtils.replace(connectionPool.getUser(), environment.getVariables());
+        String jdbcPassword = VariableUtils.replace(connectionPool.getPassword(), environment.getVariables());
+        String jdbcUrl = VariableUtils.replace(connectionPool.getUrl(), environment.getVariables());
+        String jdbcClasspath = VariableUtils.replace(connectionPool.getClasspath(), environment.getVariables());
+
+        try {
+            // connect to J2EE application server controller
+            J2EEApplicationServerController controller = J2EEApplicationServerControllerFactory.getController(environment, server);
+            // test if the JDBC connection pool is already present in the JEE server
+            if (controller.isJDBCConnectionPoolDeployed(connectionPool.getName())) {
+                LOGGER.info("JDBC connection pool {} already deployed, checking for update");
+                if (controller.updateJDBCConnectionPool(connectionPool.getName(), jdbcDriver, connectionPool.getIncrement(), connectionPool.getInitial(), connectionPool.getMaximal(), jdbcUser, jdbcPassword, jdbcUrl, jdbcClasspath)) {
+                    updateLog.setStatus("Update performed");
+                    updateLog.setUpdated(true);
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "JDBC connection pool " + connectionPool.getName() + " updated"));
+                    EventUtils.post(environment, "UPDATE", "JDBC connection pool " + connectionPool.getName() + " updated");
+                    LOGGER.info("JDBC connection pool {} updated", connectionPool.getName());
+                } else {
+                    updateLog.addUpdateMessage(new UpdateMessage("info", "JDBC connection pool " + connectionPool.getName() + " already up to date"));
+                    EventUtils.post(environment, "UPDATE", "JDBC connection pool " + connectionPool.getName() + " already up to date");
+                    LOGGER.info("JDBC connection pool {} already up to date", connectionPool.getName());
+                }
+            } else {
+                // deploy the JDBC connection pool
+                controller.deployJDBCConnectionPool(connectionPool.getName(), jdbcDriver, connectionPool.getIncrement(), connectionPool.getInitial(), connectionPool.getMaximal(), jdbcUser, jdbcPassword, jdbcUrl, jdbcClasspath);
+                updateLog.setStatus("Update performed");
+                updateLog.setUpdated(true);
+                updateLog.addUpdateMessage(new UpdateMessage("info", "JDBC connection pool " + connectionPool.getName() + " deployed"));
+                EventUtils.post(environment, "UPDATE", "JDBC connection pool " + connectionPool.getName() + " deployed");
+                LOGGER.info("JDBC connection pool {} deployed", connectionPool.getName());
+            }
+        } catch (Exception e) {
+            LOGGER.error("JDBC connection pool {} update failed", connectionPool.getName(), e);
+            throw new UpdateException("JDBC connection pool " + connectionPool.getName() + " update failed", e);
+        }
+    }
+
+    /**
+     * Wrapper method to update a JDBC connection pool via WS.
+     *
+     * @param environmentName    the target environment name.
+     * @param serverName         the target J2EE application server name.
+     * @param connectionPoolName the target JDBC connection pool name.
+     * @throws KalumetException in case of update failure.
+     */
+    public static void update(String environmentName, String serverName, String connectionPoolName) throws KalumetException {
+        LOGGER.info("JDBC connection pool {} update requested by WS", connectionPoolName);
+
+        // load configuration.
+        LOGGER.debug("Loading configuration");
+        Kalumet kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION);
+
+        // looking for component objects.
+        LOGGER.debug("Looking for component objects");
+        Environment environment = kalumet.getEnvironment(environmentName);
+        if (environment == null) {
+            LOGGER.error("Environment {} is not found in the configuration", environmentName);
+            throw new KalumetException("Environment " + environmentName + " is not found in the configuration");
+        }
+        J2EEApplicationServer applicationServer = environment.getJ2EEApplicationServers().getJ2EEApplicationServer(serverName);
+        if (applicationServer == null) {
+            LOGGER.error("J2EE application server {} is not found in environment {}", serverName, environment.getName());
+            throw new KalumetException("J2EE application server " + serverName + " is not found in environment " + environment.getName());
+        }
+        JDBCConnectionPool connectionPool = applicationServer.getJDBCConnectionPool(connectionPoolName);
+        if (connectionPool == null) {
+            LOGGER.error("JDBC connection pool {} is not found in J2EE application server {}", connectionPoolName, applicationServer.getName());
+            throw new KalumetException("JDBC connection pool " + connectionPoolName + " is not found in J2EE application server " + applicationServer.getName());
+        }
+
+        // post event and create update log
+        EventUtils.post(environment, "UPDATE", "JDBC connection pool " + connectionPool.getName() + " update request by WS");
+        UpdateLog updateLog = new UpdateLog("JDBC connection pool " + connectionPool.getName() + " update in progress", environment.getName(), environment);
+
+        // send a notification and waiting for the count down.
+        LOGGER.info("Send a notification and waiting for the count down");
+        EventUtils.post(environment, "UPDATE", "Send a notification and waiting for the count down");
+        NotifierUtils.waitAndNotify(environment);
+
+        try {
+            // call the JDBC connection pool updater.
+            LOGGER.debug("Call connection pool updater");
+            JDBCConnectionPoolUpdater.update(environment, applicationServer, connectionPool, updateLog);
+        } catch (Exception e) {
+            LOGGER.error("JDBC connection pool {} update failed", connectionPool.getName(), e);
+            EventUtils.post(environment, "ERROR", "JDBC connection pool " + connectionPool.getName() + " update failed: " + e.getMessage());
+            updateLog.setStatus("JDBC connection pool " + connectionPool.getName() + " update failed");
+            updateLog.addUpdateMessage(new UpdateMessage("error", "JDBC connection pool " + connectionPool.getName() + " update failed: " + e.getMessage()));
+            PublisherUtils.publish(environment);
+            throw new UpdateException("JDBC connection pool " + connectionPool.getName() + " update failed", e);
+        }
+
+        // update completed.
+        LOGGER.info("JDBC connection pool {} updated", connectionPool.getName());
+        EventUtils.post(environment, "UPDATE", "JDBC connection pool " + connectionPool.getName() + " updated");
+        if (updateLog.isUpdated()) {
+            updateLog.setStatus("JDBC connection pool " + connectionPool.getName() + " updated");
+        } else {
+            updateLog.setStatus("JDBC connection pool " + connectionPool.getName() + " is already up to date");
+        }
+        updateLog.addUpdateMessage(new UpdateMessage("info", "JDBC connection pool " + connectionPool.getName() + " updated"));
+        LOGGER.info("Publishing update report");
+        PublisherUtils.publish(environment);
+    }
+
+    /**
+     * Wrapper method to check if a JDBC connection pool is up to date via WS.
+     *
+     * @param environmentName    the target environment name.
+     * @param serverName         the target J2EE application server name.
+     * @param connectionPoolName the target JDBC connection pool name.
+     * @return true if the JDBC connection pool is up to date, false else.
+     * @throws KalumetException in case of check failure.
+     */
+    public static boolean check(String environmentName, String serverName, String connectionPoolName) throws KalumetException {
+        LOGGER.info("JDBC connection pool {} status check requested by WS", connectionPoolName);
+
+        // load configuration.
+        LOGGER.debug("Loading configuration");
+        Kalumet kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION);
+
+        // looking for component objects
+        LOGGER.debug("Looking for component objects.");
+        Environment environment = kalumet.getEnvironment(environmentName);
+        if (environment == null) {
+            LOGGER.error("Environment {} is not found in the configuration", environmentName);
+            throw new KalumetException("Environment " + environmentName + " is not found in the configuration");
+        }
+        J2EEApplicationServer applicationServer = environment.getJ2EEApplicationServers().getJ2EEApplicationServer(serverName);
+        if (applicationServer == null) {
+            LOGGER.error("J2EE application server {} is not found in environment {}", serverName, environment.getName());
+            throw new KalumetException("J2EE application server " + serverName + " is not found in environment " + environment.getName());
+        }
+        JDBCConnectionPool connectionPool = applicationServer.getJDBCConnectionPool(connectionPoolName);
+        if (connectionPool == null) {
+            LOGGER.error("JDBC connection pool {} is not found in J2EE server {}", connectionPoolName, applicationServer.getName());
+            throw new KalumetException("JDBC connection pool " + connectionPoolName + " is not found in J2EE application server " + applicationServer.getName());
+        }
+
+        // post an event
+        EventUtils.post(environment, "INFO", "JDBC connection pool " + connectionPool.getName() + " status check requested by WS");
+
+        try {
+            // get the JEE server JMX controller.
+            LOGGER.debug("Getting the J2EE application server controller");
+            J2EEApplicationServerController controller = J2EEApplicationServerControllerFactory.getController(environment, applicationServer);
+            // replace values with environment variables
+            LOGGER.debug("Replacing variables in connection pool data");
+            String jdbcDriver = VariableUtils.replace(connectionPool.getDriver(), environment.getVariables());
+            String jdbcUser = VariableUtils.replace(connectionPool.getUser(), environment.getVariables());
+            String jdbcPassword = VariableUtils.replace(connectionPool.getPassword(), environment.getVariables());
+            String jdbcUrl = VariableUtils.replace(connectionPool.getUrl(), environment.getVariables());
+            String jdbcClasspath = VariableUtils.replace(connectionPool.getClasspath(), environment.getVariables());
+            // check JDBC connection pool using JMX controller
+            LOGGER.debug("Checking JDBC connection pool using JMX controller.");
+            return controller.isJDBCConnectionPoolUpToDate(connectionPool.getName(), jdbcDriver, connectionPool.getIncrement(), connectionPool.getInitial(), connectionPool.getMaximal(), jdbcUser, jdbcPassword, jdbcUrl, jdbcClasspath);
+        } catch (Exception e) {
+            LOGGER.error("JDBC connection pool {} check failed", connectionPool.getName(), e);
+            throw new KalumetException("JDBC connection pool " + connectionPool.getName() + " check failed", e);
+        }
+    }
+
+}