package demo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import demo.domain.LogMessage;

/*
 * sends several LogMessage objects to a Mule VM queue
 * for testing the ESB Demo
 */
public class LogMessageProducer {
	private static Log logger = LogFactory.getLog(LogMessageProducer.class);

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		LogMessage lm = null;
		
		// send four messages
		for (int i = 0; i < shortMsgs.length; i++) {
			// get the error details
			lm = new LogMessage("LogMessageProducer",
					new Date().toString(), logLevels[i], shortMsgs[i], longMsgs[i]);
			logger.info("sending: "+lm);

			// create the properties for additional header attributes
			Map props = new HashMap();
			props.put("LogLevel", lm.getLogLevel());
			
			// now, send the message
			Object response = MuleClientUtils.sendMessage("vm://LogMessage",
							lm, Object.class, props);

			if (null != response) {
				logger.warn("received unexpected result: "+response);
			}
		}
		
		logger.info("I'm done, so check the Mule logs...");
	}
	
	private static final String[] logLevels = {"INFO", "WARN", "ERROR", "FATAL"};

	private static final String[] shortMsgs =
	{
		"Network glitch",
		"SMTP server slow",
		"DB very sick",
		"Sky is falling!"
	};

	private static final String[] longMsgs =
	{
		"Network failures prevented me from checking my sports scores :-(",
		"SMTP server smtp.ourdomain.com is not sending emails quickly",
		"Oracle is either dead or dying - might a reboot fix it?  Suggest you do something, now!",
		"Chicken Little says 'The sky is falling!' - maybe a chill pill will help?"
	};

}