001    package hirondelle.web4jtools.metrics.base;
002    
003    import hirondelle.web4j.model.ModelCtorException;
004    import hirondelle.web4j.request.RequestParameter;
005    import hirondelle.web4j.security.SafeText;
006    import hirondelle.web4j.util.Util;
007    import hirondelle.web4j.util.WebUtil;
008    
009    import javax.servlet.ServletConfig;
010    import javax.servlet.http.HttpServletRequest;
011    
012    /**
013    * Save and fetch the current {@link BaseInfo}.
014    * 
015    * <P>This implementation does not store items in a database. Rather, the following 
016    * mechanism is used : 
017    *<ul>
018    * <li>upon startup, store a default {@link BaseInfo} in application scope, using default settings in <tt>web.xml</tt>.
019    * <li>allow the user to override the default settings, and store an 'override' {@link BaseInfo} in <em>session</em> scope.
020    *</ul> 
021    */
022    public final class BaseInfoDAO {
023    
024      /** 
025      * Read in config from web.xml.  
026      * Called only upon startup. Place a default {@link BaseInfo} object in application scope. 
027      */
028      public static void readConfig(ServletConfig aConfig) {
029        try {
030          BaseInfo defaultBaseInfo = new BaseInfo(
031            get(BaseInfoAction.PROJECT_NAME, aConfig), 
032            get(BaseInfoAction.BASE_DIRECTORY, aConfig),
033            get(BaseInfoAction.BASE_URI_FOR_FETCHING_IMAGES, aConfig)
034          );
035          aConfig.getServletContext().setAttribute(BASE_INFO_KEY, defaultBaseInfo);
036        }
037        catch (ModelCtorException ex) {
038          throw new RuntimeException(ex);
039        }
040      }
041        
042      /** Full constructor  */
043      public BaseInfoDAO(HttpServletRequest aRequest) {
044        fRequest = aRequest;
045      }
046      
047      /** Key under which {@link BaseInfo} objects are stored (in both application and session scopes.) */
048      public static final String BASE_INFO_KEY = "baseInfo";
049    
050      /**
051      * Fetch a {@link BaseInfo} from either session scope (preferred) or application scope (default).
052      */
053      public BaseInfo fetch(){
054        return (BaseInfo)WebUtil.findAttribute(BASE_INFO_KEY, fRequest);
055      }
056      
057      /** Save an 'override' {@link BaseInfo} into session scope.  */
058      public void save(BaseInfo aDirInfo){
059        fRequest.getSession(true).setAttribute(BASE_INFO_KEY, aDirInfo); //for later lookup
060      }
061    
062      // PRIVATE //
063      private HttpServletRequest fRequest;
064      
065      private static SafeText get(RequestParameter aRequestParameter, ServletConfig aConfig){
066        String value = aConfig.getInitParameter(aRequestParameter.getName());
067        if ( ! Util.textHasContent(value) ) {
068          String message = "In web.xml, please specify a value for parameter named " + aRequestParameter;
069          throw new RuntimeException(message);
070        }
071        return new SafeText(value);
072      }
073    }