I think this may be a class loader issue, but am not sure as I have never 
really worked with them before. I am putting together a JAR file that contains 
a set of classes to be used throughout a suite of applications. I have a class 
called SingletonLoader that was working when it was within the main 
application, but when I broke out the reusable classes into a separate jar file 
and modified the contextInitialized() method to dynamically load the classes it 
is failing to fid the classes. The contextInitialized() method is properly 
reading the class names from the property file (e.g. 
SINGLETON_LOADER_CLASSES=com.externalApp.singleton.Countries,com.externalApp.singleton.SecurityQuestions)
 and looping through them, but I always get the following error message when 
the line 
   
  Class singletongClassObj = Class.forName(singletonClassName); 
   
  is executed:
   
  Unable to load singleton: com.externalApp.singleton.Countries
  java.lang.ClassNotFoundException: com.externalApp.singleton.Countries
   
  ALL of the singleton classes to be loaded implement the Singleton interface 
(which has nothing in it, it is just used to denote a class that MUST specify a 
"public static anyReturnVal getInstance()" method.
   
  public class SingletonLoader implements ServletContextListener, SystemConsts {
      /**
    * Logger for this class
    */
   private static final Logger log = Logger.getLogger(SingletonLoader.class);
     private static final String SINGLETON_LOADER_CLASSES
         = "SINGLETON_LOADER_CLASSES";
     private static final String DELIMITER = ",";
     // This is the method defined
   private static final String GET_INSTANCE_METHOD = "getInstance";
     private static final Object ARG_LIST[] = new Object[0];
  
   /**
    * Constructs a new SingletonLoader object.
    */
    public SingletonLoader() {
    }
  
    /**
    * Notification that the web application is ready to process requests.
    * <P>
    * Initializes all of the singletons so they are ready for immediate use
    * after the containet server starts.
    * 
    * @param sce This is the event object for notifications about changes to the
    *    servlet context of a web application.
    * 
    */
   public void contextInitialized(ServletContextEvent sce) {
   
        log.debug("contextInitialized: BEGIN");
        String singletons = ExtProperties.getProperty(SYS_PROPS, 
SINGLETON_LOADER_CLASSES);

        if (!StringUtils.isNullOrZeroLen(singletons)) {
         DelimitedString ds = new DelimitedString(singletons, DELIMITER);
         int max = ds.size();
         String singletonClassName = null;
   
           for (int ndx=0; ndx < max; ndx++) {
            try {
               singletonClassName = ds.getEntry(ndx);
               log.debug("Get singletonClassName: " + singletonClassName);
                 // Get the singleton class to load
               Class singletongClassObj = Class.forName(singletonClassName);
               log.debug("singletongClassObj: " + singletongClassObj);
                 Method method = 
singletongClassObj.getMethod(GET_INSTANCE_METHOD,
                     singletongClassObj);
               method.invoke(null, ARG_LIST);
               
            } catch (Exception e) {
               log.error("Unable to load singleton: " + singletonClassName, e);
            }
         }
      }
          log.debug("contextInitialized: END");
    }
      
    /**
     * Notification that the servlet context is about to be shut down.
     *
     * @param sce the ServletContextEvent
     */
    public void contextDestroyed(ServletContextEvent sce) {
    }
}
   
  Example of the Countries singleton
   
   
  public class Countries implements DbConsts, Singleton {
     private static Logger log = Logger.getLogger(Countries.class);
   
  <snip>
   
     /** Creates a new instance of Countries */
   private Countries() {
      refreshData();
   }
   
     /**
    * Returns the single instance of this class.
    *
    * @return the instance of this class.
    */
   public static Countries getInstance() {
        if (instance == null) {
         synchronized (Countries.class) {
            if (instance == null) {
               instance = new Countries();
            }
         }
      }
      return instance;
   }

  <snip>
  }

       
---------------------------------
Be a better Globetrotter. Get better travel answers from someone who knows.
Yahoo! Answers - Check it out.

Reply via email to