View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.remoting.servlet;
18  
19  import java.util.LinkedHashSet;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import javax.servlet.ServletConfig;
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.springframework.web.context.WebApplicationContext;
30  import org.springframework.web.context.support.WebApplicationContextUtils;
31  
32  import com.sun.xml.ws.transport.http.servlet.ServletAdapterList;
33  import com.sun.xml.ws.transport.http.servlet.SpringBinding;
34  import com.sun.xml.ws.transport.http.servlet.WSServletDelegate;
35  
36  import ch.elca.el4j.services.remoting.RemotingServiceExporter;
37  import ch.elca.el4j.services.remoting.protocol.Jaxws;
38  
39  /**
40   * This class represents a customized JAX-WS {@link HttpServlet}. Instead of
41   * using the {@link SpringBinding}s specified in special Spring XML format,
42   * this class searches for JAX-WS bindings in el4j-remoting-protocol form.
43   *
44   * Remark: This class cannot extend ModuleContextLoaderServlet because
45   * doPost, doGet, doPut and doDelete are final.
46   *
47   * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/remoting_jaxws/src/main/java/ch/elca/el4j/services/remoting/servlet/WSSpringServlet.java $
48   *
49   * @author Stefan Wismer (SWI)
50   */
51  public class WSSpringServlet extends HttpServlet {
52  
53  	/**
54  	 * The servlet delegate.
55  	 */
56  	private WSServletDelegate m_delegate;
57  
58  	
59  	/** {@inheritDoc} */
60  	public void init(ServletConfig servletConfig) throws ServletException {
61  		super.init(servletConfig);
62  
63  		// get the configured adapters from Spring
64  		WebApplicationContext wac = WebApplicationContextUtils
65  			.getRequiredWebApplicationContext(getServletContext());
66  
67  		Set<SpringBinding> bindings = new LinkedHashSet<SpringBinding>();
68  
69  		// search for Jaxws RemotingServiceExporter objects
70  		Map<?, ?> map = wac.getBeansOfType(RemotingServiceExporter.class);
71  		for (Object beanName : map.keySet()) {
72  			RemotingServiceExporter exporter
73  				= (RemotingServiceExporter) map.get(beanName);
74  			
75  			if (exporter.getRemoteProtocol() instanceof Jaxws) {
76  				try {
77  					SpringBinding binding = (SpringBinding) exporter.getObject();
78  					if (binding != null) {
79  						bindings.add(binding);
80  					}
81  				} catch (Exception e) {
82  					// ignore invalid binding
83  					log("Could not create JAX-WS Spring binding for bean '"
84  						+ ((String) beanName).substring(1) + "'");
85  				}
86  			}
87  		}
88  		
89  		// bindings declared in the jaxws-spring manner
90  		bindings.addAll(wac.getBeansOfType(SpringBinding.class).values());
91  
92  		// create adapters
93  		ServletAdapterList l = new ServletAdapterList();
94  		for (SpringBinding binding : bindings) {
95  			binding.create(l);
96  		}
97  
98  		m_delegate = new WSServletDelegate(l, getServletContext());
99  	}
100 
101 	/** {@inheritDoc} */
102 	protected void doPost(HttpServletRequest request,
103 		HttpServletResponse response) throws ServletException {
104 		m_delegate.doPost(request, response, getServletContext());
105 	}
106 
107 	/** {@inheritDoc} */
108 	protected void doGet(HttpServletRequest request,
109 		HttpServletResponse response) throws ServletException {
110 		m_delegate.doGet(request, response, getServletContext());
111 	}
112 
113 	/** {@inheritDoc} */
114 	protected void doPut(HttpServletRequest request,
115 		HttpServletResponse response) throws ServletException {
116 		m_delegate.doPut(request, response, getServletContext());
117 	}
118 
119 	/** {@inheritDoc} */
120 	protected void doDelete(HttpServletRequest request,
121 		HttpServletResponse response) throws ServletException {
122 		m_delegate.doDelete(request, response, getServletContext());
123 	}
124 }