View Javadoc

1   package org.apache.velocity.tools.struts;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Locale;
23  import java.util.Map;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.ServletContext;
26  import org.apache.struts.util.MessageResources;
27  import org.apache.velocity.runtime.log.Log;
28  import org.apache.velocity.tools.view.ViewContext;
29  
30  /**
31   * <p>Abstract view tool that provides access to Struts' message resources.</p>
32   *
33   * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
34   * @since VelocityTools 1.1
35   * @version $Id: MessageResourcesTool.java 595822 2007-11-16 21:07:51Z nbubna $
36   */
37  public abstract class MessageResourcesTool
38  {
39      protected Log LOG;
40      protected ServletContext application;
41      protected HttpServletRequest request;
42      private Locale locale;
43      private MessageResources resources;
44  
45      @Deprecated
46      public void init(Object obj)
47      {
48          if (obj instanceof ViewContext)
49          {
50              ViewContext ctx = (ViewContext)obj;
51              this.request = ctx.getRequest();
52              this.application = ctx.getServletContext();
53              this.LOG = ctx.getVelocityEngine().getLog();
54          }
55      }
56  
57      /**
58       * Initializes this tool.
59       *
60       * @param params the Map of configuration parameters
61       * @throws IllegalArgumentException if the param is not a ViewContext
62       */
63      public void configure(Map params)
64      {
65          this.request = (HttpServletRequest)params.get(ViewContext.REQUEST);
66          this.application = (ServletContext)params.get(ViewContext.SERVLET_CONTEXT_KEY);
67          this.LOG = (Log)params.get("log");
68      }
69  
70  
71      /**
72       * Retrieves the {@link Locale} for this request.
73       * @since VelocityTools 2.0
74       */
75      protected Locale getLocale()
76      {
77          if (this.locale == null)
78          {
79              this.locale =
80                  StrutsUtils.getLocale(request, request.getSession(false));
81          }
82          return this.locale;
83      }
84  
85  
86      /**
87       * Retrieves the specified {@link MessageResources} bundle, or the
88       * application's default MessageResources if no bundle is specified.
89       * @since VelocityTools 1.1
90       */
91      protected MessageResources getResources(String bundle)
92      {
93          if (bundle == null)
94          {
95              if (this.resources == null)
96              {
97                  this.resources =
98                      StrutsUtils.getMessageResources(request, application);
99                  if (this.resources == null)
100                 {
101                     LOG.error("MessageResourcesTool : Message resources are not available.");
102                 }
103             }
104             return resources;
105         }
106 
107         MessageResources res =
108             StrutsUtils.getMessageResources(request, application, bundle);
109         if (res == null)
110         {
111             LOG.error("MessageResourcesTool : MessageResources bundle '" + bundle + "' is not available.");
112         }
113         return res;
114     }
115 
116 }