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.bus.spring;
019:
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.List;
029: import java.util.logging.Level;
030: import java.util.logging.Logger;
031:
032: import org.apache.cxf.common.logging.LogUtils;
033: import org.apache.cxf.configuration.Configurer;
034: import org.springframework.beans.factory.xml.BeansDtdResolver;
035: import org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver;
036: import org.springframework.beans.factory.xml.PluggableSchemaResolver;
037: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
038: import org.springframework.context.ApplicationContext;
039: import org.springframework.context.support.ClassPathXmlApplicationContext;
040: import org.springframework.core.io.ClassPathResource;
041: import org.springframework.core.io.Resource;
042: import org.springframework.core.io.UrlResource;
043: import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
044:
045: public class BusApplicationContext extends
046: ClassPathXmlApplicationContext {
047:
048: private static final String DEFAULT_CXF_CFG_FILE = "META-INF/cxf/cxf.xml";
049: private static final String DEFAULT_CXF_EXT_CFG_FILE = "classpath*:META-INF/cxf/cxf.extension";
050:
051: private static final Logger LOG = LogUtils
052: .getL7dLogger(BusApplicationContext.class);
053:
054: private DefaultNamespaceHandlerResolver nsHandlerResolver;
055: private boolean includeDefaults;
056: private String cfgFile;
057: private URL cfgFileURL;
058:
059: public BusApplicationContext(String cf, boolean include) {
060: this (cf, include, null);
061: }
062:
063: public BusApplicationContext(URL url, boolean include) {
064: this (url, include, null);
065: }
066:
067: public BusApplicationContext(String cf, boolean include,
068: ApplicationContext parent) {
069: super ((String[]) null, false, parent);
070: cfgFile = cf;
071: includeDefaults = include;
072: refresh();
073: }
074:
075: public BusApplicationContext(URL url, boolean include,
076: ApplicationContext parent) {
077: super ((String[]) null, false, parent);
078: cfgFileURL = url;
079: includeDefaults = include;
080: refresh();
081: }
082:
083: @Override
084: protected Resource[] getConfigResources() {
085:
086: List<Resource> resources = new ArrayList<Resource>();
087:
088: if (includeDefaults) {
089: try {
090: PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
091: Thread.currentThread().getContextClassLoader());
092:
093: Collections.addAll(resources, resolver
094: .getResources(DEFAULT_CXF_CFG_FILE));
095:
096: Resource[] exts = resolver
097: .getResources(DEFAULT_CXF_EXT_CFG_FILE);
098: for (Resource r : exts) {
099: InputStream is = r.getInputStream();
100: BufferedReader rd = new BufferedReader(
101: new InputStreamReader(is, "UTF-8"));
102: String line = rd.readLine();
103: while (line != null) {
104: if (!"".equals(line)) {
105: resources.add(resolver.getResource(line));
106: }
107: line = rd.readLine();
108: }
109: is.close();
110: }
111:
112: } catch (IOException ex) {
113: // ignore
114: }
115: }
116:
117: boolean usingDefault = false;
118: if (null == cfgFile) {
119: cfgFile = System
120: .getProperty(Configurer.USER_CFG_FILE_PROPERTY_NAME);
121: }
122: if (null == cfgFile) {
123: cfgFile = Configurer.DEFAULT_USER_CFG_FILE;
124: usingDefault = true;
125: }
126: ClassPathResource cpr = new ClassPathResource(cfgFile);
127: if (cpr.exists()) {
128: resources.add(cpr);
129: } else {
130: if (!usingDefault) {
131: LogUtils.log(LOG, Level.INFO,
132: "USER_CFG_FILE_NOT_FOUND_MSG", cfgFile);
133: } else {
134: LogUtils.log(LOG, Level.FINE,
135: "USER_CFG_FILE_NOT_FOUND_MSG", cfgFile);
136: }
137: }
138:
139: if (null != cfgFileURL) {
140: UrlResource ur = new UrlResource(cfgFileURL);
141: if (ur.exists()) {
142: resources.add(ur);
143: } else {
144: LogUtils.log(LOG, Level.INFO,
145: "USER_CFG_FILE_URL_NOT_FOUND_MSG", cfgFileURL);
146: }
147: }
148:
149: String sysCfgFileUrl = System
150: .getProperty(Configurer.USER_CFG_FILE_PROPERTY_URL);
151: if (null != sysCfgFileUrl) {
152: try {
153: UrlResource ur = new UrlResource(sysCfgFileUrl);
154: if (ur.exists()) {
155: resources.add(ur);
156: } else {
157: LogUtils.log(LOG, Level.INFO,
158: "USER_CFG_FILE_URL_NOT_FOUND_MSG",
159: sysCfgFileUrl);
160: }
161: } catch (MalformedURLException e) {
162: LogUtils.log(LOG, Level.INFO,
163: "USER_CFG_FILE_URL_ERROR_MSG", sysCfgFileUrl);
164: }
165: }
166:
167: if (LOG.isLoggable(Level.FINE)) {
168: LOG.fine("Creating application context with resources: "
169: + resources);
170: }
171:
172: if (0 == resources.size()) {
173: return null;
174: }
175: Resource[] res = new Resource[resources.size()];
176: res = resources.toArray(res);
177: return res;
178: }
179:
180: @Override
181: protected void initBeanDefinitionReader(
182: XmlBeanDefinitionReader reader) {
183: // Spring always creates a new one of these, which takes a fair amount
184: // of time on startup (nearly 1/2 second) as it gets created for every
185: // spring context on the classpath
186: if (nsHandlerResolver == null) {
187: nsHandlerResolver = new DefaultNamespaceHandlerResolver();
188: }
189: reader.setNamespaceHandlerResolver(nsHandlerResolver);
190:
191: String mode = System.getProperty("spring.validation.mode");
192: if (null != mode) {
193: reader.setValidationModeName(mode);
194: }
195: reader.setNamespaceAware(true);
196:
197: setEntityResolvers(reader);
198: }
199:
200: void setEntityResolvers(XmlBeanDefinitionReader reader) {
201: ClassLoader cl = Thread.currentThread().getContextClassLoader();
202: reader
203: .setEntityResolver(new BusEntityResolver(
204: new BeansDtdResolver(),
205: new PluggableSchemaResolver(cl)));
206: }
207:
208: }
|