All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cayenne.remote.hessian.service.HessianServlet Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
/*****************************************************************
 *   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.cayenne.remote.hessian.service;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.apache.cayenne.remote.RemoteService;

/**
 * An extension of the com.caucho.hessian.server.HessianServlet that
 * installs default Cayenne handlers, simplifying web.xml configuration.
 * Here is a sample configuration:
 * 
 * 
 *        <servlet>
 *          <servlet-name>cayenne</servlet-name>
 *          <servlet-class>org.apache.cayenne.remote.hessian.service.HessianServlet</servlet-class>
 *        </servlet>
 *                        
 *        <servlet-mapping>
 *          <servlet-name>cayenne</servlet-name>
 *          <url-pattern>/cayenne</url-pattern>
 *        </servlet-mapping>
 * 
* * Custom service class and interface can be specified in a manner compatible with Hessian * recommendations, namely via service-class and api-class servlet * parameters. * * @since 1.2 * @author Andrus Adamchik */ public class HessianServlet extends _HessianServlet { // config parameters compatible with Hessian parameter names static final String API_CLASS_PARAMETER = "api-class"; static final String SERVICE_CLASS_PARAMETER = "service-class"; /** * Installs {@link HessianService} to respond to {@link RemoteService} requests. */ public void init(ServletConfig config) throws ServletException { Class apiClass = createAPIClass(config); if (apiClass == null) { throw new ServletException("Can't configure service API class"); } setAPIClass(apiClass); HessianService service = createService(config); if (service == null) { throw new ServletException("Error configuring service "); } service.init(config); setSerializerFactory(service.createSerializerFactory()); setService(service); // proceed to super super.init(config); } protected HessianService createService(ServletConfig config) throws ServletException { String className = config.getInitParameter(SERVICE_CLASS_PARAMETER); if (className == null) { return new HessianService(); } try { Class serviceClass = Class.forName(className, true, Thread .currentThread() .getContextClassLoader()); if (!HessianService.class.isAssignableFrom(serviceClass)) { throw new ServletException( "Service class must be a subclass of HessianService: " + className); } return (HessianService) serviceClass.newInstance(); } catch (ServletException e) { throw e; } catch (Exception e) { throw new ServletException( "Error instantiating service class " + className, e); } } protected Class createAPIClass(ServletConfig config) throws ServletException { String interfaceName = config.getInitParameter(API_CLASS_PARAMETER); if (interfaceName == null) { return RemoteService.class; } try { Class serviceInterface = Class.forName(interfaceName, true, Thread .currentThread() .getContextClassLoader()); if (!RemoteService.class.isAssignableFrom(serviceInterface)) { throw new ServletException( "Service interface must be a subinterface of RemoteService: " + interfaceName); } return serviceInterface; } catch (ServletException e) { throw e; } catch (Exception e) { throw new ServletException("Error instantiating service interface " + interfaceName, e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy