001 package org.springframework.richclient.application; 002 003 import org.apache.commons.logging.Log; 004 import org.apache.commons.logging.LogFactory; 005 import org.springframework.util.Assert; 006 007 /** 008 * <p> 009 * This class provides a singleton model for accessing the configured 010 * ApplicationServices object. Create your {@link ApplicationServices} and load 011 * them into the locator within your Spring context to make the services available 012 * throughout the application. 013 * </p> 014 * 015 * <pre> 016 * <bean id="serviceLocator" class="org.springframework.richclient.application.ApplicationServicesLocator"> 017 * <property name="applicationServices" ref="applicationServices"/> 018 * </bean> 019 * </pre> 020 * 021 * or by using construtor arguments: 022 * 023 * <pre> 024 * <bean id="serviceLocator" class="org.springframework.richclient.application.ApplicationServicesLocator"> 025 * <constructor-arg index="0" ref="applicationServices"/> 026 * </bean> 027 * </pre> 028 * 029 * 030 * @author Larry Streepy 031 * 032 */ 033 public class ApplicationServicesLocator { 034 035 private static final Log logger = LogFactory.getLog(ApplicationServicesLocator.class); 036 037 /** The singleton instance. */ 038 private static ApplicationServicesLocator INSTANCE; 039 040 /** The configured ApplicationServices. */ 041 private ApplicationServices applicationServices; 042 043 /** 044 * Default Constructor. 045 */ 046 public ApplicationServicesLocator() { 047 load(this); 048 } 049 050 /** 051 * Convenience constructor to add ApplicationServices at construction time. 052 */ 053 public ApplicationServicesLocator(ApplicationServices applicationServices) { 054 setApplicationServices(applicationServices); 055 load(this); 056 } 057 058 /** 059 * Return the single ApplicationServicesLocator instance. 060 */ 061 public static ApplicationServicesLocator instance() { 062 Assert.state(INSTANCE != null, "The application services locator instance has not yet been initialized."); 063 return INSTANCE; 064 } 065 066 /** 067 * Check if an instance is available. 068 * 069 * @return <code>true</code> if an ApplicationServicesLocator is loaded. 070 */ 071 public static boolean isLoaded() { 072 return INSTANCE != null; 073 } 074 075 /** 076 * Load the single ApplicationServicesLocator instance. 077 */ 078 public static void load(ApplicationServicesLocator instance) { 079 if (INSTANCE != null) { 080 logger.info("Replacing existing ApplicationServicesLocator instance with: " + instance); 081 } 082 INSTANCE = instance; 083 } 084 085 /** 086 * Convenience method to get the ApplicationServices by querying the 087 * currently loaded ApplicationServicesLocator. 088 */ 089 public static ApplicationServices services() { 090 return instance().getApplicationServices(); 091 } 092 093 /** 094 * Set the ApplicationServices instance. 095 */ 096 public void setApplicationServices(ApplicationServices applicationServices) { 097 this.applicationServices = applicationServices; 098 } 099 100 /** 101 * Return the ApplicationServices instance. 102 */ 103 public ApplicationServices getApplicationServices() { 104 return applicationServices; 105 } 106 107 }