[jboss-svn-commits] JBL Code SVN: r7378 - labs/jbossesb/workspace/jokum/qa/junit/src/org/jboss/soa/esb/util

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Nov 3 14:28:07 EST 2006


Author: jokum
Date: 2006-11-03 14:28:04 -0500 (Fri, 03 Nov 2006)
New Revision: 7378

Added:
   labs/jbossesb/workspace/jokum/qa/junit/src/org/jboss/soa/esb/util/NewListenerUtils.java
Log:
ListenerUtils for the new listener architecture

Added: labs/jbossesb/workspace/jokum/qa/junit/src/org/jboss/soa/esb/util/NewListenerUtils.java
===================================================================
--- labs/jbossesb/workspace/jokum/qa/junit/src/org/jboss/soa/esb/util/NewListenerUtils.java	2006-11-03 19:27:05 UTC (rev 7377)
+++ labs/jbossesb/workspace/jokum/qa/junit/src/org/jboss/soa/esb/util/NewListenerUtils.java	2006-11-03 19:28:04 UTC (rev 7378)
@@ -0,0 +1,187 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.util;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+import org.jboss.internal.soa.esb.command.JmsCommandQueue;
+import org.jboss.internal.soa.esb.parameters.ParamFileRepository;
+import org.jboss.soa.esb.listeners.message.EsbListenerController;
+import org.jboss.soa.esb.parameters.ParamRepositoryException;
+import org.jboss.soa.esb.parameters.ParamRepositoryFactory;
+
+/**
+ * Listener utility methods.
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public abstract class NewListenerUtils {
+
+	private static final String CONFIG_TMP_EXT = ".conftmp";
+	private static Logger logger = Logger.getLogger(NewListenerUtils.class);
+
+	/**
+	 * Start a list of Listeners based on the supplied configuration.
+	 * <p/>
+	 * Note the root of the file repos is the root of the junit/src folder.
+	 * @param paramName Config classpath.
+	 * @return Thread of execution for this Listener configs.
+	 */
+	public static ListenersManagerExecThread startListeners(String paramName) throws Exception {
+		ListenersManagerExecThread manager;
+		
+		manager = new ListenersManagerExecThread(createEsbListenerController(paramName));
+		
+		logger.info("Waiting on Listener Manager the start...");
+		manager.start();
+		while(manager.listenersManager.getState() != EsbListenerController.State.Running) {
+			Thread.sleep(50);
+			if(manager.listenersManager.getState() == EsbListenerController.State.Exception_thrown) {
+				Exception e = manager.listenersManager.getState().getException();
+				logger.error("Failed to start the Listener Manager!", e);
+				TestCase.fail(e.getMessage());
+			}
+		}
+		logger.info("Listener Manager running (Thread: " + manager.getName() + ")!  Note this does not mean all the Listeners are up and running!");
+		
+		return manager;
+	}
+
+	/**
+	 * Create an instance of the {@link EsbListenerController} and instruct it to read the specified configuration
+	 * from the ParamRepository.
+	 * <p/>
+	 * Note the root of the file repos is the root of the junit/src folder.
+	 * @param paramName Config classpath.
+	 * @return The {@link EsbListenerController} instance.
+	 */
+	public static EsbListenerController createEsbListenerController(String paramName) throws Exception {		
+		//fixUpConfig(paramName);
+		EsbListenerController.setDefaultCommandQueue(new JmsCommandQueue());
+		return new EsbListenerController(paramName);
+	}
+	
+	/**
+	 * Just performs a token replacement on "@qa.build@", "@db.*@" etc tokens in the Listener config and
+	 * creates a new repository entry for the Listener Manager to read from..
+	 * @param paramName Input config paramName.
+	 * @return The file handle for the new "fixed up" config.
+	 */
+	@SuppressWarnings("unused") //I take it this will be used in the near future?
+	private static File fixUpConfig(String paramName) throws ParamRepositoryException, IOException {
+		ParamFileRepository repos = (ParamFileRepository)ParamRepositoryFactory.getInstance();
+		String configXml = repos.get(paramName);
+		File paramFile = repos.toParamFile(paramName);
+		File fixedupFile = new File(paramFile.getAbsolutePath() + CONFIG_TMP_EXT);
+		FileOutputStream fixedUpFileStream = new FileOutputStream(fixedupFile);
+		
+		try {
+			// Replace all the "@qa.build@" tokens with the qa "build" dirs file URI, and write to file...
+			configXml = configXml.replaceAll("@qa.build@", FileUtils.getEnvBuildDir().toURI().toString());
+			// Replace all the "@db.*@" tokens with the settings from the deployment.properties...
+			configXml = configXml.replaceAll("@db.driver@", DbUtils.dbDriver);
+			configXml = configXml.replaceAll("@db.url@", DbUtils.dbUrl);
+			configXml = configXml.replaceAll("@db.user@", DbUtils.dbUser);
+			configXml = configXml.replaceAll("@db.password@", DbUtils.dbPassword);
+
+			// Write the fixed up config ro a new file - this new file will be used as the listener config!
+			fixedUpFileStream.write(configXml.getBytes());
+		} finally {
+			fixedUpFileStream.flush();
+			fixedUpFileStream.close();
+		}		
+		
+		return fixedupFile;
+	}
+	
+
+	/**
+	 * Execution thread for the Listener Manager. 
+	 * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+	 */
+	public static class ListenersManagerExecThread extends Thread {
+		
+		private EsbListenerController listenersManager;
+
+		private ListenersManagerExecThread(EsbListenerController listenersManager) {
+			super(listenersManager);
+			this.listenersManager = listenersManager;
+		}
+
+		/**
+		 * Get the {@link EsbListenerController} Listeners Manager class executing in this thread.
+		 * @return The listenersManager property value.
+		 */
+		public EsbListenerController getListenersManager() {
+			return listenersManager;
+		}
+
+		/**
+		 * Assert that the listener Manager is in an Exception state..
+		 */
+		public void asserttInException() {
+			if(listenersManager.getState() != EsbListenerController.State.Exception_thrown) {
+				String errorMsg = "ListenerManager not in Exception state.  Listener Manager Thread: " + this.getName();
+				logger.error(errorMsg);
+				TestCase.fail(errorMsg);
+			}
+		}
+
+		/**
+		 * Assert that the listener Manager is not in an Exception state..
+		 */
+		public void assertNotInException() {
+			if(listenersManager.getState() == EsbListenerController.State.Exception_thrown) {
+				String errorMsg = "ListenerManager in Exception state.  See log.  Listener Manager Thread: " + this.getName();
+				logger.error(errorMsg, listenersManager.getState().getException());
+				TestCase.fail(errorMsg);
+			}
+		}
+
+		/**
+		 * Assert that the listener Manager has shutdown.
+		 * @param maxWait The maximum length of time (ms) to wait for shutdown before failing the test.
+		 */
+		public void assertShutdownOK(long maxWait) {
+			long endTime = System.currentTimeMillis() + maxWait;
+			
+			while(System.currentTimeMillis() < endTime) {
+				if(listenersManager.getState() == EsbListenerController.State.Done_OK) {
+					logger.info("Shutdown was successful.  Listener Manager Thread: " + this.getName());
+					return;
+				}
+				try {
+					Thread.sleep(100);
+				} catch (InterruptedException e) {
+					logger.error("Thread interupt...", e);
+				}
+			}
+			String errorMsg = "ListenerManager failed to shutdown as requested.  Waited for " + maxWait + "ms.  Listener Manager Thread: " + this.getName();
+			logger.error(errorMsg);
+			TestCase.fail(errorMsg);
+		}
+	}
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list