001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.remote.server;
031:
032: import com.caucho.config.*;
033: import com.caucho.remote.*;
034:
035: import javax.ejb.*;
036: import javax.jws.*;
037: import javax.servlet.*;
038:
039: /**
040: * Abstract factory for creating @WebService and @Remote servlets.
041: */
042: abstract public class AbstractProtocolServletFactory implements
043: ProtocolServletFactory {
044: /**
045: * Creates a new servlet skeleton based on an API and an object
046: *
047: * @param serviceApi the remoteApi exposed to the server
048: * @param service the managed service object
049: */
050: abstract public Servlet createServlet(Class serviceApi,
051: Object service) throws ServiceException;
052:
053: /**
054: * Returns the remote interface to expose as a service.
055: */
056: protected Class getRemoteAPI(Class serviceClass) {
057: Remote remote = (Remote) serviceClass
058: .getAnnotation(Remote.class);
059:
060: if (remote != null)
061: return remote.value()[0];
062:
063: for (Class ifc : serviceClass.getInterfaces()) {
064: if (ifc.isAnnotationPresent(Remote.class))
065: return ifc;
066: }
067:
068: WebService webService = (WebService) serviceClass
069: .getAnnotation(WebService.class);
070:
071: if (webService != null
072: && !"".equals(webService.endpointInterface())) {
073: ClassLoader loader = Thread.currentThread()
074: .getContextClassLoader();
075:
076: try {
077: Class api = Class.forName(webService
078: .endpointInterface(), false, loader);
079:
080: return api;
081: } catch (Exception e) {
082: throw ConfigException.create(e);
083: }
084: }
085:
086: Class remoteAPI = null;
087:
088: for (Class ifc : serviceClass.getInterfaces()) {
089: if (ifc.getName().startsWith("java.io"))
090: continue;
091: else if (ifc.getName().startsWith("javax.ejb"))
092: continue;
093:
094: if (remoteAPI != null) {
095: // XXX: possible warning
096: return serviceClass;
097: } else
098: remoteAPI = ifc;
099: }
100:
101: if (remoteAPI != null)
102: return remoteAPI;
103: else
104: return serviceClass;
105: }
106: }
|