Author: jbonofre Date: Mon Oct 24 15:07:18 2011 New Revision: 1188165 URL: http://svn.apache.org/viewvc?rev=1188165&view=rev Log: Add new utils and start the refactoring of the updaters.
Added: incubator/kalumet/trunk/agent/ (with props) incubator/kalumet/trunk/agent/pom.xml incubator/kalumet/trunk/agent/src/ incubator/kalumet/trunk/agent/src/main/ incubator/kalumet/trunk/agent/src/main/java/ incubator/kalumet/trunk/agent/src/main/java/org/ incubator/kalumet/trunk/agent/src/main/java/org/apache/ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Configuration.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJob.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJobListener.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/WsServer.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/EnvironmentUpdater.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/UpdateException.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/AgentUtils.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EmailUtils.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EventUtils.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/NotifierUtils.java incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/VariableUtils.java Modified: incubator/kalumet/trunk/pom.xml Propchange: incubator/kalumet/trunk/agent/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Mon Oct 24 15:07:18 2011 @@ -0,0 +1,3 @@ +*.iml +*.ipr +*.iws Added: incubator/kalumet/trunk/agent/pom.xml URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/pom.xml?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/pom.xml (added) +++ incubator/kalumet/trunk/agent/pom.xml Mon Oct 24 15:07:18 2011 @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <!-- + + 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. + --> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.kalumet</groupId> + <artifactId>kalumet</artifactId> + <version>0.6-incubating</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>org.apache.kalumet.agent</artifactId> + <packaging>jar</packaging> + <name>Apache Kalumet :: Agent</name> + + <dependencies> + <dependency> + <groupId>axis</groupId> + <artifactId>axis</artifactId> + </dependency> + <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-email</artifactId> + </dependency> + <dependency> + <groupId>commons-httpclient</groupId> + <artifactId>commons-httpclient</artifactId> + </dependency> + <dependency> + <groupId>opensymphony</groupId> + <artifactId>quartz</artifactId> + </dependency> + <dependency> + <groupId>org.apache.kalumet</groupId> + <artifactId>org.apache.kalumet.common</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + </dependencies> + +</project> \ No newline at end of file Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Configuration.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Configuration.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Configuration.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Configuration.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,32 @@ +/* + * 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; + +import org.apache.kalumet.model.Kalumet; + +/** + * Static class to store Kalumet agent configuration. + */ +public class Configuration { + + public static String CONFIG_LOCATION; + public static String AGENT_ID; + public static Kalumet CONFIG_CACHE = null; + +} Added: 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=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/Main.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,128 @@ +/* + * 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; + +import org.apache.commons.cli.*; +import org.apache.kalumet.agent.utils.AgentUtils; +import org.apache.kalumet.model.Kalumet; +import org.quartz.*; +import org.quartz.impl.StdSchedulerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Main class for Kalumet agent launch. + */ +public final class Main { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(Main.class); + + /** + * Main agent launcher. + * + * @param args + */ + public final static void main(String[] args) { + System.out.println("Starting Apache Kalumet agent " + AgentUtils.getVersion()); + System.out.println(); + + Options options = new Options(); + Option config = OptionBuilder.withArgName("config").hasArg().withDescription("The location URL (local: or http:) to the Kalumet configuration (e.g. http://hostname/kalumet/ConfigurationWrapper)").isRequired() + .create("config"); + options.addOption(config); + Option agentid = OptionBuilder.withArgName("id").hasArg().withDescription("The Kalumet agent identification as defined in the configuration").isRequired().create("id"); + options.addOption(agentid); + CommandLineParser parser = new GnuParser(); + CommandLine cmd = null; + + try { + // parse the command line + cmd = parser.parse(options, args); + } catch (ParseException parseException) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("Apache Kalumet", options); + System.exit(1); + } + + String configLocation = cmd.getOptionValue("config"); + LOGGER.info("Loading configuration from {}", configLocation); + String agentId = cmd.getOptionValue("id"); + LOGGER.info("Agent ID is {}", agentId); + + // parse the Kalumet configuration to get the the agent cron + Kalumet kalumet = null; + String cronString = null; + try { + kalumet = Kalumet.digeste(configLocation); + } catch (Exception e) { + LOGGER.error("Can't load Apache Kalumet configuration", e); + System.err.println("Can't load Apache Kalumet configuration"); + e.printStackTrace(); + System.exit(1); + } + + if (kalumet.getAgent(agentId) == null) { + LOGGER.error("Agent ID {} is not found in the Kalumet configuration", agentId); + System.err.println("Agent ID " + agentId + " is not found in the Kalumet configuration"); + System.exit(1); + } + + // init the agent configuration store + Configuration.CONFIG_LOCATION = configLocation; + Configuration.AGENT_ID = agentId; + + cronString = kalumet.getAgent(agentId).getCron(); + LOGGER.debug("Cron definition: " + cronString); + + // start the WS server + try { + int port = kalumet.getAgent(agentId).getPort(); + WsServer wsServer = new WsServer(port, "/kalumet-ws-server.wsdd"); + wsServer.start(); + LOGGER.info("WS server started on {}", port); + } catch (Exception e) { + LOGGER.error("Can't start WS server", e); + System.err.println("Can't start WS server"); + e.printStackTrace(); + System.exit(2); + } + + // start the scheduler + try { + SchedulerFactory schedulerFactory = new StdSchedulerFactory(); + Scheduler scheduler = schedulerFactory.getScheduler(); + scheduler.addGlobalJobListener(new SchedulerJobListener()); + LOGGER.debug("Scheduler job listener plugged"); + scheduler.start(); + JobDetail job = new JobDetail("Apache Kalumet agent job", Scheduler.DEFAULT_GROUP, SchedulerJob.class); + CronTrigger cron = new CronTrigger("Apache Kalumet agent trigger", Scheduler.DEFAULT_GROUP, cronString); + LOGGER.debug("{} cron created", cronString); + scheduler.scheduleJob(job, cron); + LOGGER.info("Scheduler started with {} trigger", cronString); + } catch (Exception e) { + LOGGER.error("Can't start scheduler", e); + System.err.println("Can't start scheduler"); + e.printStackTrace(); + System.exit(3); + } + + LOGGER.info("Apache Kalumet agent started"); + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJob.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJob.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJob.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJob.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,74 @@ +/* + * 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; + +import org.apache.kalumet.agent.updater.EnvironmentUpdater; +import org.apache.kalumet.model.Environment; +import org.apache.kalumet.model.Kalumet; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.quartz.StatefulJob; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.security.krb5.Config; + +import java.util.Iterator; + +/** + * Kalumet job in the quartz scheduler. + */ +public class SchedulerJob implements StatefulJob { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(SchedulerJob.class); + + public SchedulerJob() { } + + /** + * Launch the main agent job. + * + * @param path the Kalumet configuration file location. + * @param agentId the Kalumet agent ID. + */ + public static void perform(String path, String agentId) { + Kalumet kalumet = null; + try { + LOGGER.debug("Loading Kalumet configuration"); + kalumet = Kalumet.digeste(path); + } catch (Exception e) { + LOGGER.error("Can't load Apache Kalumet configuration from {}", path, e); + throw new RuntimeException("Can't load Apache Kalumet configuration from " + path, e); + } + // loop to update all environments managed by the agent + for (Iterator environmentIterator = kalumet.getEnvironmentsByAgent(agentId).iterator(); environmentIterator.hasNext(); ) { + try { + EnvironmentUpdater.update((Environment) environmentIterator.next()); + } catch (Exception e) { + // ignore + } + } + } + + /** + * @see org.quartz.StatefulJob#execute(org.quartz.JobExecutionContext) + */ + public void execute(JobExecutionContext context) throws JobExecutionException { + SchedulerJob.perform(Configuration.CONFIG_LOCATION, Configuration.AGENT_ID); + } + +} \ No newline at end of file Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJobListener.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJobListener.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJobListener.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/SchedulerJobListener.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,68 @@ +/* + * 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; + +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.quartz.JobListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A global job listener. + */ +public class SchedulerJobListener implements JobListener { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(SchedulerJobListener.class); + + public SchedulerJobListener() { } + + /** + * Get the listener name. + * + * @return the listener name. + */ + public String getName() { + return "AgentJobListener"; + } + + /** + * @see org.quartz.JobListener#jobToBeExecuted(org.quartz.JobExecutionContext) + */ + public void jobToBeExecuted(JobExecutionContext jobExecutionContext) { + LOGGER.info("Executing scheduler job ..."); + } + + /** + * @see org.quartz.JobListener#jobExecutionVetoed(org.quartz.JobExecutionContext) + */ + public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) { } + + /** + * @see org.quartz.JobListener#jobWasExecuted(org.quartz.JobExecutionContext, org.quartz.JobExecutionException) + */ + public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException jobExecutionException) { + if (jobExecutionException != null) { + LOGGER.warn("Scheduler job executed with error", jobExecutionException); + } else { + LOGGER.info("Scheduler job executed"); + } + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/WsServer.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/WsServer.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/WsServer.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/WsServer.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,91 @@ +/* + * 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; + +import org.apache.axis.EngineConfiguration; +import org.apache.axis.configuration.FileProvider; +import org.apache.axis.transport.http.SimpleAxisServer; +import org.apache.kalumet.KalumetException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.ServerSocket; + +/** + * Kalumet agent embedded HTTP WS server. + * This embedded server listens for incoming SOAP messages. These messages come from Kalumet console or other WS clients. + */ +public class WsServer { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(WsServer.class); + + private final static int MAX_POOL_SIZE = 500; + private final static int MAX_SESSIONS_NUMBER = 500; + + private SimpleAxisServer simpleAxisServer; + + /** + * Create an embedded WS server. + * + * @param port the WS server listen port. + * @param wsddFile the WebService Deployment Descriptor. + * @throws KalumetException in case of WS server creation failure. + */ + public WsServer(int port, String wsddFile) throws KalumetException { + simpleAxisServer = new SimpleAxisServer(MAX_POOL_SIZE, MAX_SESSIONS_NUMBER); + LOGGER.debug("Creating WS server"); + LOGGER.debug(" Max pool size: " + MAX_POOL_SIZE); + LOGGER.debug(" Max sessions number: " + MAX_SESSIONS_NUMBER); + try { + simpleAxisServer.setServerSocket(new ServerSocket(port)); + LOGGER.debug("WS server started on port {}", port); + } catch (IOException e) { + LOGGER.error("Can't create WS server on port {}", port, e); + throw new KalumetException("Can't create WS server on port " + port, e); + } + } + + /** + * Start the WS server. + * + * @throws KalumetException in case of WS server startup failure. + */ + public void start() throws KalumetException { + try { + LOGGER.debug("Starting WS server in daemon mode"); + simpleAxisServer.start(true); + } catch (Exception e) { + LOGGER.error("Can't start WS server", e); + throw new KalumetException("Can't start WS server", e); + } + } + + /** + * Define the WS server configuration. + * + * @param wsddFile the WebService Deployment Descriptor file. + * @return the engine configuration of the WS server. + * @throws KalumetException in case of configuration failure. + */ + private EngineConfiguration getEngineConfiguration(String wsddFile) throws KalumetException { + return new FileProvider(getClass().getResourceAsStream(wsddFile)); + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/EnvironmentUpdater.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/EnvironmentUpdater.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/EnvironmentUpdater.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/EnvironmentUpdater.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,119 @@ +/* + * 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.agent.utils.NotifierUtils; +import org.apache.kalumet.model.Environment; +import org.apache.kalumet.model.Kalumet; +import org.apache.kalumet.model.update.UpdateLog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.security.krb5.Config; + +/** + * Environment updater. + */ +public class EnvironmentUpdater { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(EnvironmentUpdater.class); + + /** + * Updates an environment identified by a given name. + * The update is forced even if the auto update flag is set to false. + * + * @param name the environment name. + * @throws KalumetException + */ + public static void update(String name) throws KalumetException { + // load the Kalumet configuration + Kalumet kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION); + // get the environment + Environment environment = kalumet.getEnvironment(name); + if (environment == null) { + LOGGER.error("Environment {} is not found in the configuration", name); + throw new KalumetException("Environment " + name + " is not found in the configuration"); + } + try { + EnvironmentUpdater.update(environment, true); + } catch (Exception e) { + throw new KalumetException(e); + } + } + + /** + * Updates an environment. + * + * @param environment the environment to update. + * @throws UpdateException in case of update failure. + */ + public static void update(Environment environment) throws UpdateException { + EnvironmentUpdater.update(environment, false); + } + + /** + * Updates an environment. + * + * @param environment the environment to update. + * @param force true force the update (even if the autoupdate flag is false), false else + * @throws UpdateException in case of update failure. + */ + public static void update(Environment environment, boolean force) throws UpdateException { + LOGGER.info("Updating environment {}", environment.getName()); + + LOGGER.debug("Loading configuration and updating the cache"); + Kalumet kalumet = null; + try { + kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION); + Configuration.CONFIG_CACHE = kalumet; + } catch (Exception e) { + LOGGER.error("Can't load configuration", e); + EventUtils.post(environment, "ERROR", "Can't load configuration: " + e.getMessage()); + throw new UpdateException("Can't load configuration", e); + } + + if (!force && !environment.isAutoupdate()) { + LOGGER.info("Update is not forced and environment {} is not auto update", environment.getName()); + LOGGER.info("Update is not performed"); + return; + } + + LOGGER.debug("Creating a update logger"); + UpdateLog updateLog = null; + try { + updateLog = new UpdateLog("Environment " + environment.getName() + " update in progress ...", environment.getName(), environment); + } catch (Exception e) { + LOGGER.error("Can't create the update logger", e); + EventUtils.post(environment, "ERROR", "Can't create the update logger: " + e.getMessage()); + throw new UpdateException("Can't create the update logger", e); + } + + // posting start update event + EventUtils.post(environment, "UPDATE", "Starting to update ..."); + + LOGGER.info("Sending a notification and waiting for the update count down"); + EventUtils.post(environment, "UPDATE", "Sending a notification and waiting for the update count donw"); + NotifierUtils.waitAndNotify(environment); + + // TODO complete + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/UpdateException.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/UpdateException.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/UpdateException.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/updater/UpdateException.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,56 @@ +/* + * 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; + +/** + * Exception wrapper for updaters. + */ +public class UpdateException extends KalumetException { + + /** + * Update exception with the explanation message. + * + * @param message the explanation message. + */ + public UpdateException(String message) { + super(message); + } + + /** + * Update exception with the cause. + * + * @param cause the cause. + */ + public UpdateException(Throwable cause) { + super(cause); + } + + /** + * Update exception with the explanation message and cause. + * + * @param message the explanation message. + * @param cause the cause. + */ + public UpdateException(String message, Throwable cause) { + super(message, cause); + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/AgentUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/AgentUtils.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/AgentUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/AgentUtils.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,40 @@ +/* + * 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; + +/** + * Util class to get version from the MANIFEST package specification. + */ +public class AgentUtils { + + /** + * Get the version from Kalumet package. + * + * @return the Kalumet version. + */ + public static String getVersion() { + String version = ""; + Package p = Package.getPackage("org.apache.kalumet.agent"); + if (p != null) { + version = p.getImplementationVersion(); + } + return version; + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EmailUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EmailUtils.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EmailUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EmailUtils.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,132 @@ +/* + * 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.mail.HtmlEmail; +import org.apache.kalumet.KalumetException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.List; + +/** + * Util class to send e-mails. + */ +public class EmailUtils { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(EmailUtils.class); + + /** + * Send a HTML email to a list of destination. + * + * @param host the mail host name or IP address. + * @param from the from e-mail address. + * @param subject the e-mail subject. + * @param addresses the e-mail address list. + * @param content the e-mail content. + */ + public static void sendHTMLEmail(String host, String from, String subject, List addresses, String content) throws KalumetException { + try { + HtmlEmail email = new HtmlEmail(); + email.setHostName(host); + email.setFrom(from); + email.setSubject(subject); + for (Iterator addressIterator = addresses.iterator(); addressIterator.hasNext(); ) { + String address = (String) addressIterator.next(); + email.addTo(address); + } + email.setHtmlMsg(content); + email.send(); + } catch (Exception e) { + throw new KalumetException(e); + } + } + + /** + * Send a text email to a list of destination. + * + * @param host the mail host name or IP address. + * @param from the from e-mail address. + * @param subject the e-mail subject. + * @param addresses the e-mail address list. + * @param content the e-mail content. + */ + public static void sendTextEmail(String host, String from, String subject, List addresses, String content) throws KalumetException { + try { + HtmlEmail email = new HtmlEmail(); + email.setHostName(host); + email.setFrom(from); + email.setSubject(subject); + for (Iterator addressIterator = addresses.iterator(); addressIterator.hasNext(); ) { + String address = (String) addressIterator.next(); + email.addTo(address); + } + email.setTextMsg(content); + email.send(); + } catch (Exception e) { + throw new KalumetException(e); + } + } + + /** + * Format a given e-mail template with value. + * + * @param template the template filename path/location. + * @param values the <code>Object[]</code> values. + * @return the formatted string. + */ + public static String format(String template, Object[] values) throws KalumetException { + try { + return EmailUtils.format(new FileReader(template), values); + } catch (FileNotFoundException fileNotFoundException) { + LOGGER.error("Can't format the e-mail template", fileNotFoundException); + throw new KalumetException("Can't format the e-mail template", fileNotFoundException); + } + } + + /** + * Format a given e-mail template with value. + * + * @param values the <code>Object[]</code> values. + * @return the formatted string. + * @pmram template the template reader. + */ + public static String format(Reader templateReader, Object[] values) throws KalumetException { + try { + BufferedReader templateBufferedReader = new BufferedReader(templateReader); + StringWriter writer = new StringWriter(); + BufferedWriter buffer = new BufferedWriter(writer); + String templateLine = templateBufferedReader.readLine(); + while (templateLine != null) { + buffer.write(MessageFormat.format(templateLine, values)); + buffer.newLine(); + templateLine = templateBufferedReader.readLine(); + } + buffer.flush(); + return writer.toString(); + } catch (Exception e) { + LOGGER.error("Can't format the e-mail template", e); + throw new KalumetException("Can't format the e-mail template", e); + } + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EventUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EventUtils.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EventUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/EventUtils.java Mon Oct 24 15:07:18 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.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.kalumet.agent.Configuration; +import org.apache.kalumet.model.Environment; +import org.apache.kalumet.model.Kalumet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Util class to post event to the Kalumet journal. + */ +public class EventUtils { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(EventUtils.class); + + /** + * Post an event to the Kalumet journal. + * + * @param environment the target environment.. + * @param author the author of the event. + * @param severity the severity severity of the event. + * @param event the event event. + */ + public static void post(Environment environment, String author, String severity, String event) { + LOGGER.debug("Loading configuration from cache"); + Kalumet kalumet = Configuration.CONFIG_CACHE; + if (kalumet == null) { + LOGGER.debug("Configuration not in cache, loading it"); + try { + kalumet = Kalumet.digeste(Configuration.CONFIG_LOCATION); + Configuration.CONFIG_CACHE = kalumet; + } catch (Exception e) { + LOGGER.warn("Can't post event", e); + return; + } + } + + LOGGER.debug("Getting LogEventAppender property in Kalumet configuration"); + if (kalumet.getProperty("LogEventAppender") == null) { + LOGGER.warn("Can't post event because the LogEventAppender is not define in the configuration"); + return; + } + String logEventAppender = kalumet.getProperty("LogEventAppender").getValue(); + + // creating the HTTP client + HttpClient httpClient = new HttpClient(); + // create the post method + PostMethod postMethod = new PostMethod(logEventAppender); + // add the HTTP parameters + postMethod.addParameter("environment", environment.getName()); + postMethod.addParameter("author", author); + postMethod.addParameter("severity", severity); + postMethod.addParameter("event", event); + try { + httpClient.executeMethod(postMethod); + } catch (Exception e) { + // ignore + } finally { + postMethod.releaseConnection(); + } + } + + /** + * Post an event to the Kalumet journal. + * + * @param environment the target environment. + * @param severity the event severity level. + * @param event the event message. + */ + public static void post(Environment environment, String severity, String event) { + EventUtils.post(environment, Configuration.AGENT_ID, severity, event); + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/NotifierUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/NotifierUtils.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/NotifierUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/NotifierUtils.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,80 @@ +/* + * 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.model.Destination; +import org.apache.kalumet.model.Email; +import org.apache.kalumet.model.Environment; +import org.apache.kalumet.model.Notifiers; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStreamReader; +import java.util.Iterator; +import java.util.LinkedList; + +/** + * Util class to notify that an update will start. + */ +public class NotifierUtils { + + private final static transient Logger LOGGER = LoggerFactory.getLogger(NotifierUtils.class); + + /** + * Wait the count down and send e-mail to notifiers. + * + * @param environment the target environment. + */ + public static void waitAndNotify(Environment environment) { + Notifiers notifiers = environment.getNotifiers(); + LOGGER.debug("Send e-mail to notify people for the update of the environment {} and wait the count down ({} minute(s)).", environment.getName(), notifiers.getCountdown()); + LOGGER.debug("Construct the e-mail content."); + LOGGER.debug("Load the e-mail template."); + InputStreamReader notifyTemplate = new InputStreamReader(NotifierUtils.class.getResourceAsStream("/templates/notifier.html")); + Object[] values = new Object[2]; + values[0] = environment.getName(); + values[1] = new Integer(notifiers.getCountdown()).toString(); + String notifyContent = null; + try { + notifyContent = EmailUtils.format(notifyTemplate, values); + // send the notification + LOGGER.debug("Send the notification."); + LOGGER.debug("Iterator on the notifier list."); + for (Iterator notifierIterator = notifiers.getNotifiers().iterator(); notifierIterator.hasNext(); ) { + Email email = (Email) notifierIterator.next(); + LOGGER.debug("Construct the address 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())); + } + EmailUtils.sendHTMLEmail(VariableUtils.replace(email.getMailhost(), environment.getVariables()), VariableUtils.replace(email.getFrom(), environment.getVariables()), "AutoDeploy Update Notification - Environment " + environment.getName(), addresses, notifyContent); + } + } catch (Exception e) { + LOGGER.warn("Can't send notification.", e); + } + LOGGER.debug("Waiting for the countdown (" + notifiers.getCountdown() + " minute(s)) ..."); + try { + Thread.sleep(notifiers.getCountdown() * 60 * 1000); + } catch (InterruptedException interruptedException) { + LOGGER.warn("Can't process notification count down.", interruptedException); + } + } + +} Added: incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/VariableUtils.java URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/VariableUtils.java?rev=1188165&view=auto ============================================================================== --- incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/VariableUtils.java (added) +++ incubator/kalumet/trunk/agent/src/main/java/org/apache/kalumet/agent/utils/VariableUtils.java Mon Oct 24 15:07:18 2011 @@ -0,0 +1,48 @@ +/* + * 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.lang.StringUtils; +import org.apache.kalumet.model.Variable; + +import java.util.Iterator; +import java.util.List; + +/** + * Util class to search and replace variables in string. + */ +public class VariableUtils { + + /** + * Replace all given variables in a string. + * + * @param source the source string. + * @param variables the variables list. + * @return the string with variables replaced. + */ + public final static String replace(String source, List variables) { + String replaced = source; + for (Iterator variableIterator = variables.iterator(); variableIterator.hasNext(); ) { + Variable variable = (Variable) variableIterator.next(); + replaced = StringUtils.replace(replaced, "${" + variable.getName() + "}", variable.getValue()); + } + return replaced; + } + +} Modified: incubator/kalumet/trunk/pom.xml URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/pom.xml?rev=1188165&r1=1188164&r2=1188165&view=diff ============================================================================== --- incubator/kalumet/trunk/pom.xml (original) +++ incubator/kalumet/trunk/pom.xml Mon Oct 24 15:07:18 2011 @@ -36,6 +36,7 @@ <modules> <module>common</module> + <module>agent</module> </modules> <scm> @@ -146,7 +147,9 @@ <axis.version>1.4</axis.version> <axis-wsdl4j.version>1.5.1</axis-wsdl4j.version> <concurrent.version>1.3.4</concurrent.version> + <commons-cli.version>1.2</commons-cli.version> <commons-digester.version>1.8.1</commons-digester.version> + <commons-email.version>1.1</commons-email.version> <commons-httpclient.version>3.1</commons-httpclient.version> <commons-io.version>2.1</commons-io.version> <commons-lang.version>2.6</commons-lang.version> @@ -154,6 +157,7 @@ <junit.version>4.9</junit.version> <log4j.version>1.2.16</log4j.version> <oro.version>2.0.8</oro.version> + <quartz.version>1.6.3</quartz.version> <slf4j.version>1.6.3</slf4j.version> <xerces.version>2.9.1</xerces.version> </properties> @@ -186,11 +190,21 @@ <version>${concurrent.version}</version> </dependency> <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + <version>${commons-cli.version}</version> + </dependency> + <dependency> <groupId>commons-digester</groupId> <artifactId>commons-digester</artifactId> <version>${commons-digester.version}</version> </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-email</artifactId> + <version>${commons-email.version}</version> + </dependency> + <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>${commons-httpclient.version}</version> @@ -221,11 +235,21 @@ <version>${log4j.version}</version> </dependency> <dependency> + <groupId>opensymphony</groupId> + <artifactId>quartz</artifactId> + <version>${quartz.version}</version> + </dependency> + <dependency> <groupId>oro</groupId> <artifactId>oro</artifactId> <version>${oro.version}</version> </dependency> <dependency> + <groupId>org.apache.kalumet</groupId> + <artifactId>org.apache.kalumet.common</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> @@ -261,6 +285,18 @@ <target>1.5</target> </configuration> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <archive> + <manifest> + <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> + <addDefaultImplementationEntries>true</addDefaultImplementationEntries> + </manifest> + </archive> + </configuration> + </plugin> </plugins> </build>