org.apache.cxf.transport.servlet.CXFServlet Maven / Gradle / Ivy
/**
* 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.
*/
package org.apache.cxf.transport.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Collection;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.common.util.ReflectionUtil;
import org.apache.cxf.helpers.CastUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class CXFServlet extends CXFNonSpringServlet
implements ApplicationListener {
private static final long serialVersionUID = -5922443981969455305L;
private static final String BUS_PARAMETER = "bus";
private boolean busCreated;
private XmlWebApplicationContext createdContext;
public CXFServlet() {
}
@Override
protected void loadBus(ServletConfig servletConfig) {
ApplicationContext wac = WebApplicationContextUtils.
getWebApplicationContext(servletConfig.getServletContext());
if (wac instanceof AbstractApplicationContext) {
addListener((AbstractApplicationContext)wac);
}
String configLocation = servletConfig.getInitParameter("config-location");
if (configLocation == null) {
try {
InputStream is = servletConfig.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > 0) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}
if (configLocation != null) {
wac = createSpringContext(wac, servletConfig, configLocation);
}
if (wac != null) {
String busParam = servletConfig.getInitParameter(BUS_PARAMETER);
String busName = busParam == null ? "cxf" : busParam.trim();
setBus((Bus)wac.getBean(busName, Bus.class));
} else {
busCreated = true;
setBus(BusFactory.newInstance().createBus());
}
}
protected void addListener(AbstractApplicationContext wac) {
try {
//spring 2 vs spring 3 return type is different
Method m = wac.getClass().getMethod("getApplicationListeners");
Collection