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.iiop;
031:
032: import com.caucho.ejb.AbstractEJBObject;
033: import com.caucho.ejb.AbstractServer;
034: import com.caucho.log.Log;
035: import com.caucho.management.j2ee.J2EEManagedObject;
036: import com.caucho.management.j2ee.RMI_IIOPResource;
037: import com.caucho.naming.*;
038: import com.caucho.server.connection.Connection;
039: import com.caucho.server.port.Protocol;
040: import com.caucho.server.port.ServerRequest;
041: import com.caucho.iiop.orb.*;
042:
043: import java.lang.reflect.Method;
044: import java.rmi.NoSuchObjectException;
045: import java.util.ArrayList;
046: import java.util.logging.*;
047: import javax.annotation.*;
048: import javax.naming.*;
049:
050: /**
051: * The main class for the HTTP server.
052: *
053: * <p>TcpServer handles the main thread control. HttpServer just needs
054: * to create the right kind of request when a new thread is spawned.
055: *
056: * @see com.caucho.server.TcpServer
057: */
058: public class IiopProtocol extends Protocol {
059: private static final Logger log = Logger
060: .getLogger(IiopProtocol.class.getName());
061:
062: static final String COPYRIGHT = "Copyright (c) 1998-2008 Caucho Technology. All rights reserved.";
063:
064: private String _protocolName = "iiop";
065:
066: private ORBImpl _orb = new ORBImpl();
067: private boolean _isOrbInit;
068: private CosServer _cos;
069:
070: private IiopContext _iiopContext;
071:
072: /**
073: * Creates the IIOP protocol.
074: */
075: public IiopProtocol() {
076: _iiopContext = new IiopContext();
077:
078: try {
079: Jndi.rebindDeep("java:comp/ORB", _orb);
080: } catch (NamingException e) {
081: log.log(Level.FINER, e.toString(), e);
082: }
083:
084: _cos = new CosServer(this );
085:
086: IiopContext.setLocalContext(_iiopContext);
087: }
088:
089: /**
090: * Returns the protocol name.
091: */
092: public String getProtocolName() {
093: return _protocolName;
094: }
095:
096: /**
097: * Sets the protocol name.
098: */
099: public void setProtocolName(String name) {
100: _protocolName = name;
101: }
102:
103: @PostConstruct
104: public void init() {
105: J2EEManagedObject.register(new RMI_IIOPResource(this ));
106: }
107:
108: public CosServer getCos() {
109: return _cos;
110: }
111:
112: public IiopSkeleton getService(String host, int port, String oid)
113: throws NoSuchObjectException {
114: return lookupService(host, port, oid);
115: }
116:
117: ORBImpl getOrb(String host, int port) {
118: if (!_isOrbInit) {
119: _isOrbInit = true;
120: _orb.setHost(host);
121: _orb.setPort(port);
122: }
123:
124: return _orb;
125: }
126:
127: /**
128: * Returns the service with the given URL.
129: */
130: private IiopSkeleton lookupService(String host, int port, String oid)
131: throws NoSuchObjectException {
132: String url;
133: String local;
134:
135: int p = oid.indexOf('?');
136:
137: if (p < 0) {
138: url = oid;
139: local = null;
140: } else {
141: url = oid.substring(0, p);
142: local = oid.substring(p + 1);
143: }
144:
145: IiopRemoteService service = _iiopContext.getService(url);
146:
147: if (service == null)
148: return null;
149: else if (local == null) {
150: // ArrayList<Class> apiList = new ArrayList<Class>();
151: // apiList.add(service.getHomeAPI());
152:
153: // TCK: ejb30/bb/session/stateful/busifacedd/multipleInterfacesTest2
154: // ejb/4012: multiple <business-remote> interfaces
155: ArrayList<Class> apiList = service.getHomeAPI();
156:
157: Object obj = service.getHome();
158:
159: if (obj == null)
160: throw new IllegalStateException(service
161: + " does not have a valid EJB home interface");
162:
163: // Restrict the service API to the given business interface.
164: // TCK: ejb30/getInvokedBusinessInterfaceRemoteIllegal, needs QA
165: if (service.getRemoteInterface() != null) {
166: apiList.clear();
167: apiList.add(service.getRemoteInterface());
168:
169: for (Method method : obj.getClass()
170: .getDeclaredMethods()) {
171: try {
172: // XXX TCK
173: if (method.getName().startsWith("create"))
174: obj = method.invoke(obj, null);
175: } catch (Exception e) {
176: log.config("Remote home "
177: + obj.getClass().getName()
178: + " has no create method");
179: }
180: }
181: }
182:
183: // XXX TCK: ejb30/.../remove
184: if (obj instanceof AbstractEJBObject) {
185: AbstractEJBObject ejb = (AbstractEJBObject) obj;
186:
187: AbstractServer server = ejb.__caucho_getServer();
188: local = ejb.__caucho_getId();
189:
190: url = url + "?" + local;
191: }
192:
193: return new IiopSkeleton(obj, apiList, service
194: .getClassLoader(), host, port, url);
195: } else {
196: Object obj = service.getObject(local);
197:
198: if (obj == null)
199: return null;
200:
201: ArrayList<Class> apiList = service.getObjectAPI();
202:
203: // Restrict the service API to the given business interface.
204: // TCK: ejb30/getInvokedBusinessInterfaceRemoteIllegal, needs QA
205: if (service.getRemoteInterface() != null) {
206: apiList.clear();
207: apiList.add(service.getRemoteInterface());
208:
209: for (Method method : obj.getClass()
210: .getDeclaredMethods()) {
211: try {
212: // XXX TCK
213: if (method.getName().startsWith("create"))
214: obj = method.invoke(obj, null);
215: } catch (Exception e) {
216: log.config("Remote home "
217: + obj.getClass().getName()
218: + " has no create method");
219: }
220: }
221: }
222:
223: return new IiopSkeleton(obj, apiList, service
224: .getClassLoader(), host, port, url + '?' + local);
225: }
226: }
227:
228: /**
229: * Create a HttpRequest object for the new thread.
230: */
231: public ServerRequest createRequest(Connection conn) {
232: return new IiopRequest(this, conn);
233: }
234: }
|