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.ejb.protocol;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.ejb.AbstractServer;
034: import com.caucho.iiop.IiopContext;
035: import com.caucho.iiop.IiopRemoteService;
036: import com.caucho.server.e_app.EnterpriseApplication;
037: import com.caucho.util.L10N;
038:
039: import javax.ejb.EJBHome;
040: import java.util.logging.Logger;
041:
042: /**
043: * Server containing all the EJBs for a given configuration.
044: *
045: * <p>Each protocol will extend the container to override Handle creation.
046: */
047: public class IiopProtocolContainer extends ProtocolContainer {
048: private static final Logger log = Logger
049: .getLogger(IiopProtocolContainer.class.getName());
050: private static final L10N L = new L10N(IiopProtocolContainer.class);
051:
052: private IiopContext _context;
053:
054: private IiopProtocolContainer(IiopContext context) {
055: _context = context;
056: }
057:
058: /**
059: * Creates the IIOP protocol server if IIOP is available.
060: */
061: public static IiopProtocolContainer createProtocolContainer() {
062: IiopContext context = IiopContext.getLocalContext();
063:
064: if (context != null)
065: return new IiopProtocolContainer(context);
066: else
067: return null;
068: }
069:
070: /**
071: * Creates the IIOP protocol server if IIOP is available.
072: */
073: public static EJBHome findRemoteEJB(String ejbName) {
074: IiopContext context = IiopContext.getLocalContext();
075:
076: if (context == null)
077: return null;
078:
079: if (!ejbName.startsWith("/"))
080: ejbName = "/" + ejbName;
081:
082: IiopRemoteService service = context.getService(ejbName);
083:
084: if (service != null)
085: return (EJBHome) service.getHome();
086: else
087: return null;
088: }
089:
090: public String getName() {
091: return "iiop";
092: }
093:
094: /**
095: * Exports a server to iiop.
096: */
097: @Override
098: public void addServer(AbstractServer server) {
099: Object obj = null;
100: Class remoteInterface = null; //server.getRemote21();
101:
102: Class homeApi = server.getRemoteHomeClass();
103: Class remoteApi = server.getRemoteObjectClass();
104: Class api = null;
105:
106: if (homeApi != null) {
107: obj = server.getRemoteObject(homeApi, "iiop");
108: api = homeApi;
109: } else if (remoteApi != null) {
110: obj = server.getRemoteObject(remoteApi, "iiop");
111: api = remoteApi;
112: }
113:
114: if (obj == null)
115: return;
116:
117: String name = getName(server);
118:
119: log.fine("iiop: add server " + name);
120:
121: EjbIiopRemoteService service = new EjbIiopRemoteService(server,
122: api);
123:
124: _context.setService(name, service);
125:
126: String remoteJndiName = name;
127:
128: // TCK (ejb/0f6f)
129: // Multiple remote interfaces
130: for (Class cl : server.getRemoteApiList()) {
131: String s = cl.getName().replace(".", "_");
132:
133: name = remoteJndiName + "#" + s;
134:
135: log.fine("iiop: add server " + name);
136:
137: service = new EjbIiopRemoteService(server, cl);
138:
139: boolean isEJB3 = true;
140:
141: if (server.getRemote21() != null) {
142: if (server.getRemote21().getName().equals(cl.getName())) {
143: isEJB3 = false;
144: }
145: }
146:
147: service.setEJB3(isEJB3);
148:
149: _context.setService(name, service);
150: }
151: }
152:
153: /**
154: * Removes a server from iiop.
155: */
156: @Override
157: public void removeServer(AbstractServer server) {
158: String name = getName(server);
159:
160: _context.removeService(name);
161: }
162:
163: @Override
164: protected HandleEncoder createHandleEncoder(AbstractServer server,
165: Class primaryKeyClass) throws ConfigException {
166: String name = getName(server);
167:
168: if (_urlPrefix != null)
169: return new HandleEncoder(server, _urlPrefix + name);
170: else
171: return new HandleEncoder(server, name);
172: }
173:
174: /**
175: * Returns the skeleton
176: */
177: @Override
178: public Skeleton getSkeleton(String uri, String queryString)
179: throws Exception {
180: throw new UnsupportedOperationException();
181: }
182:
183: private String getName(AbstractServer server) {
184: String name = server.getProtocolId();
185: if (name == null)
186: name = server.getEJBName();
187:
188: name = name.replace('.', '_'); // XXX:
189:
190: if (!name.startsWith("/"))
191: name = "/" + name;
192:
193: EnterpriseApplication eApp = EnterpriseApplication.getLocal();
194:
195: if (eApp != null)
196: return "/" + eApp.getName() + "_" + name.substring(1);
197: else
198: return name;
199: }
200: }
|