001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.configuration.spring;
019:
020: import java.lang.reflect.Method;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.apache.cxf.bus.spring.BusApplicationContext;
027: import org.apache.cxf.common.logging.LogUtils;
028: import org.apache.cxf.configuration.Configurable;
029: import org.apache.cxf.configuration.Configurer;
030: import org.springframework.beans.BeansException;
031: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
032: import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
033: import org.springframework.beans.factory.config.BeanDefinition;
034: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
035: import org.springframework.beans.factory.wiring.BeanConfigurerSupport;
036: import org.springframework.beans.factory.wiring.BeanWiringInfo;
037: import org.springframework.beans.factory.wiring.BeanWiringInfoResolver;
038: import org.springframework.context.ApplicationContext;
039: import org.springframework.core.io.ClassPathResource;
040:
041: public class ConfigurerImpl extends BeanConfigurerSupport implements
042: Configurer {
043:
044: private static final Logger LOG = LogUtils
045: .getL7dLogger(ConfigurerImpl.class);
046: private static final String DEFAULT_USER_CFG_FILE = "cxf.xml";
047:
048: private ApplicationContext appContext;
049: private final Map<String, String> wildCardBeanDefinitions = new HashMap<String, String>();
050:
051: public ConfigurerImpl() {
052: this (DEFAULT_USER_CFG_FILE);
053: }
054:
055: public ConfigurerImpl(String cfgFile) {
056: if (null == cfgFile) {
057: cfgFile = System.getProperty(USER_CFG_FILE_PROPERTY_NAME);
058: }
059: if (null == cfgFile) {
060: cfgFile = DEFAULT_USER_CFG_FILE;
061: }
062: ClassPathResource cpr = new ClassPathResource(cfgFile);
063: if (cpr.exists()) {
064: try {
065: BusApplicationContext ac = new BusApplicationContext(
066: cfgFile, false);
067: ac.refresh();
068: setApplicationContext(ac);
069: } catch (BeansException ex) {
070: LogUtils.log(LOG, Level.WARNING,
071: "APP_CONTEXT_CREATION_FAILED_MSG", ex,
072: (Object[]) null);
073: }
074: } else {
075: LogUtils.log(LOG, Level.INFO,
076: "USER_CFG_FILE_NOT_FOUND_MSG", cfgFile);
077: }
078: }
079:
080: public ConfigurerImpl(ApplicationContext ac) {
081: setApplicationContext(ac);
082: }
083:
084: private void initWildcardDefinitionMap() {
085: if (null != appContext) {
086: for (String n : appContext.getBeanDefinitionNames()) {
087: if (isWildcardBeanName(n)) {
088: AutowireCapableBeanFactory bf = appContext
089: .getAutowireCapableBeanFactory();
090: BeanDefinitionRegistry bdr = (BeanDefinitionRegistry) bf;
091: BeanDefinition bd = bdr.getBeanDefinition(n);
092: String className = bd.getBeanClassName();
093: if (null != className) {
094: if (!wildCardBeanDefinitions
095: .containsKey(className)) {
096: wildCardBeanDefinitions.put(className, n);
097: } else {
098: LogUtils
099: .log(
100: LOG,
101: Level.WARNING,
102: "ONE_WILDCARD_BEAN_ID_PER_CLASS_MSG",
103: new String[] {
104: wildCardBeanDefinitions
105: .get(className),
106: className, n });
107: }
108: } else {
109: LogUtils
110: .log(
111: LOG,
112: Level.WARNING,
113: "WILDCARD_BEAN_ID_WITH_NO_CLASS_MSG",
114: n);
115: }
116: }
117: }
118: }
119: }
120:
121: public void configureBean(Object beanInstance) {
122: configureBean(null, beanInstance);
123: }
124:
125: public void configureBean(String bn, Object beanInstance) {
126:
127: if (null == appContext) {
128: return;
129: }
130:
131: if (null == bn) {
132: bn = getBeanName(beanInstance);
133: }
134:
135: if (null == bn) {
136: return;
137: }
138:
139: configureWithWildCard(bn, beanInstance);
140:
141: final String beanName = bn;
142: setBeanWiringInfoResolver(new BeanWiringInfoResolver() {
143: public BeanWiringInfo resolveWiringInfo(Object instance) {
144: if (null != beanName && !"".equals(beanName)) {
145: return new BeanWiringInfo(beanName);
146: }
147: return null;
148: }
149: });
150:
151: try {
152: super .configureBean(beanInstance);
153: if (LOG.isLoggable(Level.FINE)) {
154: LOG.fine("Successfully performed injection.");
155: }
156: } catch (NoSuchBeanDefinitionException ex) {
157: // users often wonder why the settings in their configuration files seem
158: // to have no effect - the most common cause is that they have been using
159: // incorrect bean ids
160: if (LOG.isLoggable(Level.FINE)) {
161: LOG.log(Level.FINE, "NO_MATCHING_BEAN_MSG", beanName);
162: }
163: }
164: }
165:
166: private void configureWithWildCard(String bn, Object beanInstance) {
167: if (!wildCardBeanDefinitions.isEmpty()
168: && !isWildcardBeanName(bn)) {
169: String className = beanInstance.getClass().getName();
170: if (wildCardBeanDefinitions.containsKey(className)) {
171: String wildCardBeanId = wildCardBeanDefinitions
172: .get(className);
173: if (bn.endsWith(stripStar(wildCardBeanId))) {
174: configureBean(wildCardBeanId, beanInstance);
175: }
176: }
177: }
178: }
179:
180: private boolean isWildcardBeanName(String bn) {
181: return bn.charAt(0) == '*';
182: }
183:
184: private String stripStar(String wildCardBeanId) {
185: return wildCardBeanId.substring(1);
186: }
187:
188: protected String getBeanName(Object beanInstance) {
189: if (beanInstance instanceof Configurable) {
190: return ((Configurable) beanInstance).getBeanName();
191: }
192: String beanName = null;
193: Method m = null;
194: try {
195: m = beanInstance.getClass().getDeclaredMethod(
196: "getBeanName", (Class[]) null);
197: } catch (NoSuchMethodException ex) {
198: try {
199: m = beanInstance.getClass().getMethod("getBeanName",
200: (Class[]) null);
201: } catch (NoSuchMethodException e) {
202: //ignore
203: }
204: }
205: if (m != null) {
206: try {
207: beanName = (String) (m.invoke(beanInstance));
208: } catch (Exception ex) {
209: LogUtils.log(LOG, Level.WARNING,
210: "ERROR_DETERMINING_BEAN_NAME_EXC", ex);
211: }
212: }
213:
214: if (null == beanName) {
215: LogUtils.log(LOG, Level.INFO,
216: "COULD_NOT_DETERMINE_BEAN_NAME_MSG", beanInstance
217: .getClass().getName());
218: }
219:
220: return beanName;
221: }
222:
223: private void setApplicationContext(ApplicationContext ac) {
224: appContext = ac;
225: setBeanFactory(appContext.getAutowireCapableBeanFactory());
226: initWildcardDefinitionMap();
227: }
228: }
|