01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.injection.handler;
16:
17: import javax.servlet.ServletContext;
18: import javax.servlet.http.HttpServletRequest;
19:
20: import org.apache.struts.Globals;
21: import org.apache.struts.config.ModuleConfig;
22: import org.apache.struts.util.MessageResources;
23: import org.apache.struts.util.ModuleUtils;
24: import org.strecks.context.ActionContext;
25: import org.strecks.util.Assert;
26: import org.strecks.util.StringUtils;
27:
28: /**
29: * Injection handler for obtaining <code>MessageResources</code> from <code>ActionContext</code>
30: * @author Phil Zoio
31: */
32: public class MessageResourcesInjectionHandler implements
33: InjectionHandler {
34:
35: private String bundle;
36:
37: public MessageResourcesInjectionHandler(String bundle) {
38: Assert.notNull(bundle);
39: if (StringUtils.notBlankOrNull(bundle))
40: this .bundle = bundle;
41: else
42: this .bundle = Globals.MESSAGES_KEY;
43: }
44:
45: public Object getValue(ActionContext injectionContext) {
46: return getMessageResources(injectionContext);
47: }
48:
49: protected MessageResources getMessageResources(
50: ActionContext injectionContext) {
51: HttpServletRequest request = injectionContext.getRequest();
52:
53: ServletContext context = injectionContext.getContext();
54: ModuleConfig moduleConfig = ModuleUtils.getInstance()
55: .getModuleConfig(request, context);
56:
57: // Return the requested message resources instance
58: return (MessageResources) context.getAttribute(bundle
59: + moduleConfig.getPrefix());
60: }
61:
62: }
|