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.spring;
16:
17: import org.apache.struts.util.MessageResources;
18: import org.apache.struts.util.MessageResourcesFactory;
19: import org.apache.struts.util.PropertyMessageResourcesFactory;
20: import org.springframework.beans.factory.DisposableBean;
21: import org.springframework.beans.factory.FactoryBean;
22: import org.springframework.beans.factory.InitializingBean;
23: import org.strecks.util.Assert;
24:
25: /**
26: * <code>FactoryBean</code> which allows Strut's message resources to be exposed as a Spring bean. This allows, for
27: * example, for Struts message resources to be injected into an action using the
28: * @SpringBean annotation
29: * @author Phil Zoio
30: */
31: public class MessageResourcesFactoryBean implements FactoryBean,
32: InitializingBean, DisposableBean {
33:
34: private String config;
35:
36: private Class factory;
37:
38: private MessageResources resources;
39:
40: private MessageResourcesFactory factoryInstance;
41:
42: public String getConfig() {
43: return config;
44: }
45:
46: public void setConfig(String config) {
47: this .config = config;
48: }
49:
50: public Class getFactory() {
51: return factory;
52: }
53:
54: public void setFactory(Class factory) {
55: this .factory = factory;
56: }
57:
58: public Object getObject() throws Exception {
59: return resources;
60: }
61:
62: public Class getObjectType() {
63: return MessageResources.class;
64: }
65:
66: public boolean isSingleton() {
67: return true;
68: }
69:
70: public void afterPropertiesSet() throws Exception {
71:
72: Assert.notNull(config,
73: "config property for MessageResourcesFactoryBean");
74:
75: if (factory == null) {
76: factory = PropertyMessageResourcesFactory.class;
77: }
78:
79: Object newInstance = factory.newInstance();
80:
81: if (!(newInstance instanceof MessageResourcesFactory)) {
82: throw new IllegalStateException(
83: "Factory must be instance of "
84: + MessageResourcesFactory.class);
85: }
86:
87: factoryInstance = (MessageResourcesFactory) newInstance;
88: resources = factoryInstance.createResources(config);
89:
90: }
91:
92: public void destroy() throws Exception {
93: resources = null;
94: }
95:
96: }
|