01: package org.apache.velocity.tools.struts;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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 javax.servlet.http.HttpServletRequest;
24: import javax.servlet.ServletContext;
25: import org.apache.commons.logging.Log;
26: import org.apache.commons.logging.LogFactory;
27: import org.apache.struts.util.MessageResources;
28: import org.apache.velocity.tools.view.context.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 477914 2006-11-21 21:52:11Z henning $
36: */
37: public abstract class MessageResourcesTool {
38:
39: protected static final Log LOG = LogFactory
40: .getLog(MessageResourcesTool.class);
41:
42: protected ServletContext application;
43: protected HttpServletRequest request;
44: protected Locale locale;
45: protected MessageResources resources;
46:
47: /**
48: * Initializes this tool.
49: *
50: * @param obj the current ViewContext
51: * @throws IllegalArgumentException if the param is not a ViewContext
52: */
53: public void init(Object obj) {
54: if (!(obj instanceof ViewContext)) {
55: throw new IllegalArgumentException(
56: "Tool can only be initialized with a ViewContext");
57: }
58:
59: ViewContext context = (ViewContext) obj;
60: this .request = context.getRequest();
61: this .application = context.getServletContext();
62: this .resources = StrutsUtils.getMessageResources(request,
63: application);
64: this .locale = StrutsUtils.getLocale(request, request
65: .getSession(false));
66: }
67:
68: /**
69: * Retrieves the specified {@link MessageResources} bundle, or the
70: * application's default MessageResources if no bundle is specified.
71: * @since VelocityTools 1.1
72: */
73: protected MessageResources getResources(String bundle) {
74: if (bundle == null) {
75: if (resources == null) {
76: LOG.error("Message resources are not available.");
77: }
78: return resources;
79: }
80:
81: MessageResources res = StrutsUtils.getMessageResources(request,
82: application, bundle);
83: if (res == null) {
84: LOG.error("MessageResources bundle '" + bundle
85: + "' is not available.");
86: }
87: return res;
88: }
89:
90: }
|