package org.apache.velocity.tools.struts;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.MessageResources;
import org.apache.velocity.tools.view.context.ViewContext;
/**
* <p>Abstract view tool that provides access to Struts' message resources.</p>
*
* @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
* @since VelocityTools 1.1
* @version $Id: MessageResourcesTool.java 477914 2006-11-21 21:52:11Z henning $
*/
public abstract class MessageResourcesTool
{
protected static final Log LOG = LogFactory.getLog(MessageResourcesTool.class);
protected ServletContext application;
protected HttpServletRequest request;
protected Locale locale;
protected MessageResources resources;
/**
* Initializes this tool.
*
* @param obj the current ViewContext
* @throws IllegalArgumentException if the param is not a ViewContext
*/
public void init(Object obj)
{
if (!(obj instanceof ViewContext))
{
throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
}
ViewContext context = (ViewContext)obj;
this.request = context.getRequest();
this.application = context.getServletContext();
this.resources =
StrutsUtils.getMessageResources(request, application);
this.locale =
StrutsUtils.getLocale(request, request.getSession(false));
}
/**
* Retrieves the specified {@link MessageResources} bundle, or the
* application's default MessageResources if no bundle is specified.
* @since VelocityTools 1.1
*/
protected MessageResources getResources(String bundle)
{
if (bundle == null)
{
if (resources == null)
{
LOG.error("Message resources are not available.");
}
return resources;
}
MessageResources res =
StrutsUtils.getMessageResources(request, application, bundle);
if (res == null)
{
LOG.error("MessageResources bundle '" + bundle + "' is not available.");
}
return res;
}
}