src/org/ohmage/spring/ConfigurationSingletonValidityTester.java
private void validate(ApplicationContext applicationContext) {
		Object tmp = null;
		Object[] noArgs = new Object[] {};
		
		for(String className : classNames) {
			try {
				String javaStyleClassName = className.substring(0, className.length() - 6).replace('/', '.');
				
				// Ignore inner classes
				if(javaStyleClassName.contains("$")) {
					if(logger.isDebugEnabled()) {
						logger.debug("Skipping inner class " + javaStyleClassName);
					}
					continue;
				}
				
				// Ignore classes that are ignorable
				if(classesToIgnore != null && classesToIgnore.contains(javaStyleClassName)) {
					if(logger.isDebugEnabled()) {
						logger.debug("Ignoring " + javaStyleClassName);
					}
					continue;
				}
				
				Class<?> clazz = Class.forName(javaStyleClassName);
				
				if(logger.isDebugEnabled()) {
					logger.debug("Sanity check for the existence of " + clazz + " in the ApplicationContext");
				}
				
				tmp = applicationContext.getBean(clazz);
				Method m = tmp.getClass().getDeclaredMethod("instance");
				
				// Invoke the static instance() method with no arguments.
				// If nothing is returned, the class is missing from the XML
				// config (i.e., no singleton exists in the application context 
				if(m.invoke(null, noArgs) == null) {
					throw new IllegalStateException("Invalid singleton configuration. No instance exists in the ApplicationContext"
						+ " for class " + className);
				}
			}
			catch (ClassNotFoundException e) {
				throw new IllegalStateException("Could not find class in ApplicationContext: " + className, e);
			}
			catch (BeansException e) {
				throw new IllegalStateException("Could not find bean in ApplicationContext for class: " + className , e);
			}
			catch (NoSuchMethodException e) {
				throw new IllegalStateException("No instance() method found on class: " + className, e);
			}
			catch (IllegalAccessException e) {
				throw new IllegalStateException("Could not access class: " + className, e);
			}
			catch (InvocationTargetException e) {
				throw new IllegalStateException("Exception thrown when invoking instance() on class: " + className, e.getCause());
			}
		}
		
		logger.info("Done with singleton instantion check for the following packages: " + Arrays.toString(packageDirectoryNames));
	}
/**
	 * Makes sure that the packages set on construction contain beans in the
	 * ApplicationContext that have been instantiated. Requires that the classes
	 * have a static instance() method to invoke.
	 * 
	 * @param applicationContext  The Spring ApplicationContext to query for
	 * class instance existence.
	 * 
	 * @throws IllegalStateException  if any class in classNames cannot be
	 * found in the ApplicationContext, does not have a static instance() 
	 * method, is not accessible due to security constraints, or if the 
	 * underlying instance() method throws an Exception.
	 */
Makes sure that the packages set on construction contain beans in the
ApplicationContext that have been instantiated. Requires that the classes
have a static instance() method to invoke.

@param applicationContext  The Spring ApplicationContext to query for
class instance existence.

@throws IllegalStateException  if any class in classNames cannot be
found in the ApplicationContext, does not have a static instance()
method, is not accessible due to security constraints, or if the
underlying instance() method throws an Exception.
Makes sure that the packages set on construction contain beans in the ApplicationContext that have been instantiated . Requires that the classes have a static instance () method to invoke . @param applicationContext The Spring ApplicationContext to query for class instance existence . @throws IllegalStateException if any class in classNames cannot be found in the ApplicationContext does not have a static instance () method is not accessible due to security constraints or if the underlying instance () method throws an Exception .
private void validate(ApplicationContext applicationContext) {
		Object tmp = null;
		Object[] noArgs = new Object[] {};
		for(String className : classNames) {
			try {
				String javaStyleClassName = className.substring(0, className.length() - 6).replace('/', '.');
				if(javaStyleClassName.contains("$")) {
					if(logger.isDebugEnabled()) {
						logger.debug("Skipping inner class " + javaStyleClassName);
					}
					continue;
				}
				if(classesToIgnore != null && classesToIgnore.contains(javaStyleClassName)) {
					if(logger.isDebugEnabled()) {
						logger.debug("Ignoring " + javaStyleClassName);
					}
					continue;
				}
				Class<?> clazz = Class.forName(javaStyleClassName);
				if(logger.isDebugEnabled()) {
					logger.debug("Sanity check for the existence of " + clazz + " in the ApplicationContext");
				}
				tmp = applicationContext.getBean(clazz);
				Method m = tmp.getClass().getDeclaredMethod("instance");
				if(m.invoke(null, noArgs) == null) {
					throw new IllegalStateException("Invalid singleton configuration. No instance exists in the ApplicationContext"
						+ " for class " + className);
				}
			}
			catch (ClassNotFoundException e) {
				throw new IllegalStateException("Could not find class in ApplicationContext: " + className, e);
			}
			catch (BeansException e) {
				throw new IllegalStateException("Could not find bean in ApplicationContext for class: " + className , e);
			}
			catch (NoSuchMethodException e) {
				throw new IllegalStateException("No instance() method found on class: " + className, e);
			}
			catch (IllegalAccessException e) {
				throw new IllegalStateException("Could not access class: " + className, e);
			}
			catch (InvocationTargetException e) {
				throw new IllegalStateException("Exception thrown when invoking instance() on class: " + className, e.getCause());
			}
		}
		logger.info("Done with singleton instantion check for the following packages: " + Arrays.toString(packageDirectoryNames));
	}
private void validate ( ApplicationContext applicationContext ) { Object tmp = null ; Object [ ] noArgs = new Object [ ] { } ; for ( String className : classNames ) { try { String javaStyleClassName = className . substring ( 0 , className . length ( ) - 6 ) . replace ( '/' , '.' ) ; if ( javaStyleClassName . contains ( "$" ) ) { if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Skipping inner class " + javaStyleClassName ) ; } continue ; } if ( classesToIgnore != null && classesToIgnore . contains ( javaStyleClassName ) ) { if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Ignoring " + javaStyleClassName ) ; } continue ; } Class < ? > clazz = Class . forName ( javaStyleClassName ) ; if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Sanity check for the existence of " + clazz + " in the ApplicationContext" ) ; } tmp = applicationContext . getBean ( clazz ) ; Method m = tmp . getClass ( ) . getDeclaredMethod ( "instance" ) ; if ( m . invoke ( null , noArgs ) == null ) { throw new IllegalStateException ( "Invalid singleton configuration. No instance exists in the ApplicationContext" + " for class " + className ) ; } } catch ( ClassNotFoundException e ) { throw new IllegalStateException ( "Could not find class in ApplicationContext: " + className , e ) ; } catch ( BeansException e ) { throw new IllegalStateException ( "Could not find bean in ApplicationContext for class: " + className , e ) ; } catch ( NoSuchMethodException e ) { throw new IllegalStateException ( "No instance() method found on class: " + className , e ) ; } catch ( IllegalAccessException e ) { throw new IllegalStateException ( "Could not access class: " + className , e ) ; } catch ( InvocationTargetException e ) { throw new IllegalStateException ( "Exception thrown when invoking instance() on class: " + className , e . getCause ( ) ) ; } } logger . info ( "Done with singleton instantion check for the following packages: " + Arrays . toString ( packageDirectoryNames ) ) ; }
Makes sure that the packages set on construction contain beans in the
 ApplicationContext that have been instantiated.
Makes sure that the packages set on construction contain beans in the ApplicationContext that have been instantiated .
// Ignore inner classes // Ignore classes that are ignorable // Invoke the static instance() method with no arguments. // If nothing is returned, the class is missing from the XML // config (i.e., no singleton exists in the application context 