001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.providers.ejb;
059:
060: import java.lang.reflect.Method;
061: import java.util.HashMap;
062: import java.util.Hashtable;
063: import java.util.Iterator;
064: import java.util.Map;
065:
066: import javax.ejb.EJBHome;
067: import javax.ejb.EJBObject;
068: import javax.naming.InitialContext;
069: import javax.rmi.PortableRemoteObject;
070: import javax.wsdl.BindingOperation;
071: import javax.wsdl.Definition;
072: import javax.wsdl.Port;
073: import javax.wsdl.extensions.ExtensibilityElement;
074:
075: import org.apache.wsif.WSIFException;
076: import org.apache.wsif.WSIFOperation;
077: import org.apache.wsif.logging.Trc;
078: import org.apache.wsif.providers.WSIFDynamicTypeMap;
079: import org.apache.wsif.util.WSIFUtils;
080: import org.apache.wsif.wsdl.extensions.ejb.EJBAddress;
081:
082: /**
083: * EJB WSIF Port.
084: * @author <a href="mailto:gpfau@de.ibm.com">Gerhard Pfau</a>
085: * Partially based on WSIFPort_ApacheSOAP from Alekander Slominski,
086: * Paul Fremantle, Sanjiva Weerawarana and Matthew J. Duftler
087: * @author Owen Burroughs <owenb@apache.org>
088: * @author Jeremy Hughes <hughesj@apache.org>
089: */
090: public class WSIFPort_EJB extends org.apache.wsif.base.WSIFDefaultPort
091: implements java.io.Serializable {
092:
093: private static final long serialVersionUID = 1L;
094:
095: private javax.wsdl.Definition fieldDefinition = null;
096: private javax.wsdl.Port fieldPortModel = null;
097: private EJBHome fieldEjbHome = null; // 'factory for physical connection'
098: private EJBObject fieldEjbObject = null; // 'physical connection'
099:
100: protected Map operationInstances = new HashMap();
101:
102: public WSIFPort_EJB(Definition def, Port port,
103: WSIFDynamicTypeMap typeMap) {
104: Trc.entry(this , def, port, typeMap);
105:
106: fieldDefinition = def;
107: fieldPortModel = port;
108:
109: Trc.exit();
110: }
111:
112: public Definition getDefinition() {
113: return fieldDefinition;
114: }
115:
116: public WSIFOperation_EJB getDynamicWSIFOperation(String name,
117: String inputName, String outputName) throws WSIFException {
118: Trc.entry(this , name, inputName, outputName);
119:
120: WSIFOperation_EJB tempOp = (WSIFOperation_EJB) operationInstances
121: .get(getKey(name, inputName, outputName));
122:
123: WSIFOperation_EJB operation = null;
124: if (tempOp != null) {
125: operation = tempOp.copy();
126: }
127:
128: if (operation == null) {
129: BindingOperation bindingOperationModel = WSIFUtils
130: .getBindingOperation(fieldPortModel.getBinding(),
131: name, inputName, outputName);
132:
133: if (bindingOperationModel != null) {
134: operation = new WSIFOperation_EJB(fieldPortModel,
135: bindingOperationModel, this );
136: setDynamicWSIFOperation(name, inputName, outputName,
137: operation);
138: }
139: }
140:
141: Trc.exit(operation);
142: return operation;
143: }
144:
145: public EJBHome getEjbHome() throws WSIFException {
146: Trc.entry(this );
147:
148: if (fieldEjbHome == null) {
149: EJBAddress address = null;
150:
151: try {
152: ExtensibilityElement portExtension = (ExtensibilityElement) fieldPortModel
153: .getExtensibilityElements().get(0);
154:
155: if (portExtension == null) {
156: throw new WSIFException("missing port extension");
157: }
158:
159: address = (EJBAddress) portExtension;
160:
161: Hashtable hash = new Hashtable();
162:
163: // If the initial context factory is specified in the wsdl use it
164: String icf = address.getInitialContextFactory();
165: if (icf != null) {
166: hash.put(InitialContext.INITIAL_CONTEXT_FACTORY,
167: icf);
168: }
169:
170: // If the jndi provider url is specified in the wsdl use it
171: String providerURL = address.getJndiProviderURL();
172: if (providerURL != null) {
173: hash.put(InitialContext.PROVIDER_URL, providerURL);
174: }
175:
176: // Lookup from an authoritative source
177: hash.put(InitialContext.AUTHORITATIVE, "true");
178:
179: InitialContext initContext;
180: initContext = new InitialContext(hash);
181:
182: Class homeClass = null;
183: try {
184: if (address.getClassName() != null) {
185: homeClass = Class.forName(address
186: .getClassName(), true, Thread
187: .currentThread()
188: .getContextClassLoader());
189: if (!(EJBHome.class.isAssignableFrom(homeClass))) {
190: homeClass = null;
191: }
192: }
193: } catch (ClassNotFoundException cnf) {
194: Trc.ignoredException(cnf);
195: }
196:
197: if (homeClass != null) {
198: fieldEjbHome = (EJBHome) PortableRemoteObject
199: .narrow(initContext.lookup(address
200: .getJndiName()), homeClass);
201: } else {
202: fieldEjbHome = (EJBHome) initContext.lookup(address
203: .getJndiName());
204: }
205:
206: } catch (Exception ex) {
207: Trc.exception(ex);
208: throw new WSIFException(
209: "Failed to lookup EJB home using JNDI name '"
210: + address.getJndiName() + "'", ex);
211: }
212: }
213: Trc.exit(fieldEjbHome);
214: return fieldEjbHome;
215: }
216:
217: public EJBObject getEjbObject() throws WSIFException {
218: Trc.entry(this );
219:
220: if (fieldEjbObject == null) {
221: EJBHome ejbHome = getEjbHome();
222: try {
223: Method createMethod = ejbHome.getClass()
224: .getDeclaredMethod("create", null);
225: fieldEjbObject = (EJBObject) createMethod.invoke(
226: ejbHome, null);
227: } catch (Exception ex) {
228: Trc.exception(ex);
229: throw new WSIFException(
230: "Could not create instance for home '"
231: + ejbHome + "'", ex);
232: }
233: }
234: Trc.exit(fieldEjbObject);
235: return fieldEjbObject;
236: }
237:
238: public Port getPortModel() {
239: Trc.entry(this );
240: Trc.exit(fieldPortModel);
241: return fieldPortModel;
242: }
243:
244: public void setDefinition(Definition value) {
245: Trc.entry(this , value);
246: fieldDefinition = value;
247: Trc.exit();
248: }
249:
250: // WSIF: keep list of operations available in this port
251: public void setDynamicWSIFOperation(String name, String inputName,
252: String outputName, WSIFOperation_EJB value) {
253: Trc.entry(this , name, inputName, outputName);
254:
255: operationInstances.put(getKey(name, inputName, outputName),
256: value);
257: Trc.exit();
258: }
259:
260: public void setEjbHome(EJBHome newEjbHome) {
261: Trc.entry(this , newEjbHome);
262: fieldEjbHome = newEjbHome;
263: Trc.exit();
264: }
265:
266: public void setEjbObject(EJBObject newEjbObject) {
267: Trc.entry(this , newEjbObject);
268: fieldEjbObject = newEjbObject;
269: Trc.exit();
270: }
271:
272: public void setPortModel(Port value) {
273: Trc.entry(this , value);
274: fieldPortModel = value;
275: Trc.exit();
276: }
277:
278: public WSIFOperation createOperation(String operationName)
279: throws WSIFException {
280: Trc.entry(this , operationName);
281: WSIFOperation wo = createOperation(operationName, null, null);
282: Trc.exit(wo);
283: return wo;
284: }
285:
286: public WSIFOperation createOperation(String operationName,
287: String inputName, String outputName) throws WSIFException {
288: Trc.entry(this , operationName, inputName, outputName);
289: WSIFOperation_EJB op = getDynamicWSIFOperation(operationName,
290: inputName, outputName);
291: if (op == null) {
292: throw new WSIFException("Could not create operation: "
293: + operationName + ":" + inputName + ":"
294: + outputName);
295: }
296: WSIFOperation wo = op.copy();
297: Trc.exit(wo);
298: return wo;
299: }
300:
301: public String deep() {
302: String buff = "";
303: try {
304: buff = new String(super .toString() + ":\n");
305:
306: buff += "definition:" + Trc.brief(fieldDefinition);
307: buff += " portModel:" + Trc.brief(fieldPortModel);
308: buff += " ejbHome:" + fieldEjbHome;
309: buff += " ejbObject" + fieldEjbObject;
310:
311: buff += " operationInstances:";
312: if (operationInstances == null)
313: buff += "null";
314: else {
315: buff += " size:" + operationInstances.size();
316: Iterator it = operationInstances.keySet().iterator();
317: int i = 0;
318: while (it.hasNext()) {
319: String key = (String) it.next();
320: WSIFOperation_EJB woejb = (WSIFOperation_EJB) operationInstances
321: .get(key);
322: buff += "\noperationInstances[" + i + "]:" + key
323: + " " + woejb + " ";
324: i++;
325: }
326: }
327: } catch (Exception e) {
328: Trc.exceptionInTrace(e);
329: }
330:
331: return buff;
332: }
333: }
|