001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ExchangeFactory.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.messaging;
030:
031: import com.sun.jbi.messaging.util.Translator;
032: import com.sun.jbi.messaging.util.WSDLHelper;
033:
034: import java.net.URI;
035: import java.util.HashMap;
036: import java.util.logging.Logger;
037: import javax.xml.namespace.QName;
038:
039: import javax.jbi.messaging.MessageExchange;
040: import javax.jbi.messaging.InOnly;
041: import javax.jbi.messaging.InOptionalOut;
042: import javax.jbi.messaging.InOut;
043: import javax.jbi.messaging.RobustInOnly;
044: import javax.jbi.servicedesc.ServiceEndpoint;
045:
046: import org.w3c.dom.Document;
047:
048: /** A message exchange factory is used used to create new instances of
049: * MessageExchange. Service consumers use these factories to create message
050: * exchanges when initiating a new service request. Message exchange factories
051: * are created using the javax.jbi.component.ComponentContext given to the
052: * component during its initialization (see javax.jbi.component.ComponentLifeCycle ).
053: * There are a variety of ways to creating such factories, each of which creates
054: * a context that is used to provide some of the default properties of
055: * MessageExchange instances created by the factory instances. For example,
056: * a factory can be created for a particular endpoint, ensuring that all
057: * exchanges created by the factory have that endpoint set as the default
058: * endpoint property of the exchange. This allows components to retain
059: * factories as a way of aligning internal processing context with the context
060: * contained in the factory, ensuring that the exchanges created consistently
061: * reflect that context.
062: * @author Sun Microsystems, Inc.
063: */
064: public class ExchangeFactory implements
065: javax.jbi.messaging.MessageExchangeFactory {
066: private MessageService mMsgSvc;
067: private QName mInterface;
068: private QName mService;
069: private ServiceEndpoint mEndpoint;
070: private EndpointRegistry mRegistry;
071:
072: ExchangeFactory(MessageService msgSvc) {
073: mMsgSvc = msgSvc;
074: mRegistry = EndpointRegistry.getInstance();
075: }
076:
077: static ExchangeFactory newInterfaceFactory(MessageService msgSvc,
078: QName interfaceName) {
079: ExchangeFactory ef;
080:
081: ef = new ExchangeFactory(msgSvc);
082: ef.setInterface(interfaceName);
083:
084: return ef;
085: }
086:
087: static ExchangeFactory newServiceFactory(MessageService msgSvc,
088: QName serviceName) {
089: ExchangeFactory ef;
090:
091: ef = new ExchangeFactory(msgSvc);
092: ef.setService(serviceName);
093:
094: return ef;
095: }
096:
097: static ExchangeFactory newEndpointFactory(MessageService msgSvc,
098: ServiceEndpoint endpoint) {
099: ExchangeFactory ef;
100:
101: ef = new ExchangeFactory(msgSvc);
102: ef.setEndpoint(endpoint);
103:
104: return ef;
105: }
106:
107: public MessageExchange createExchange(URI pattern)
108: throws javax.jbi.messaging.MessagingException {
109: MessageExchangeProxy proxy;
110: MessageExchangeImpl impl;
111: String uriStr;
112:
113: // Decide which pattern to create
114: switch (ExchangePattern.valueOf(pattern)) {
115: case IN_ONLY:
116: proxy = new InOnlyImpl();
117: break;
118:
119: case IN_OUT:
120: proxy = new InOutImpl();
121: break;
122:
123: case IN_OPTIONAL_OUT:
124: proxy = new InOptionalOutImpl();
125: break;
126:
127: case ROBUST_IN_ONLY:
128: proxy = new RobustInOnlyImpl();
129: break;
130:
131: default:
132: throw new javax.jbi.messaging.MessagingException(Translator
133: .translate(LocalStringKeys.INVALID_MEP_URI,
134: new Object[] { pattern }));
135:
136: }
137:
138: // Associate proxy with exchange impl
139: impl = new MessageExchangeImpl(proxy, mMsgSvc);
140:
141: // Apply any defaults from the factory
142: proxy.setInterfaceName(mInterface);
143: proxy.setService(mService);
144: proxy.setEndpoint(mEndpoint);
145:
146: return proxy;
147: }
148:
149: public MessageExchange createExchange(QName serviceName,
150: QName operationName)
151: throws javax.jbi.messaging.MessagingException {
152: MessageExchange exchange;
153: RegisteredEndpoint[] endpoints;
154: Document desc;
155: HashMap operations;
156: URI opURI;
157:
158: // try and find an endpoint for this service
159: endpoints = mRegistry.getInternalEndpointsForService(
160: serviceName, true);
161:
162: if (endpoints.length == 0) {
163: throw new javax.jbi.messaging.MessagingException(
164: Translator
165: .translate(
166: LocalStringKeys.CANT_FIND_ENDPOINT_FOR_SERVICE,
167: new Object[] { serviceName }));
168: }
169:
170: operations = WSDLHelper.getOperationsForService(mMsgSvc
171: .queryDescriptor(endpoints[0]), serviceName);
172:
173: if (operations == null) {
174: throw new javax.jbi.messaging.MessagingException(Translator
175: .translate(LocalStringKeys.ENDPOINT_NO_DESCRIPTOR,
176: new Object[] { endpoints[0]
177: .getEndpointName() }));
178: }
179:
180: if (!operations.containsKey(operationName.toString())) {
181: throw new javax.jbi.messaging.MessagingException(Translator
182: .translate(LocalStringKeys.NONEXISTENT_OPERATION,
183: new Object[] { operationName.toString() }));
184: }
185:
186: opURI = createURI((String) operations.get(operationName
187: .toString()));
188: exchange = createExchange(opURI);
189:
190: exchange.setService(serviceName);
191: exchange.setOperation(operationName);
192:
193: return exchange;
194: }
195:
196: public InOnly createInOnlyExchange()
197: throws javax.jbi.messaging.MessagingException {
198: return (InOnly) createExchange(ExchangePattern.IN_ONLY.getURI());
199: }
200:
201: public InOptionalOut createInOptionalOutExchange()
202: throws javax.jbi.messaging.MessagingException {
203: return (InOptionalOut) createExchange(ExchangePattern.IN_OPTIONAL_OUT
204: .getURI());
205: }
206:
207: public InOut createInOutExchange()
208: throws javax.jbi.messaging.MessagingException {
209: return (InOut) createExchange(ExchangePattern.IN_OUT.getURI());
210: }
211:
212: public RobustInOnly createRobustInOnlyExchange()
213: throws javax.jbi.messaging.MessagingException {
214: return (RobustInOnly) createExchange(ExchangePattern.ROBUST_IN_ONLY
215: .getURI());
216: }
217:
218: private void setInterface(QName interfaceName) {
219: mInterface = interfaceName;
220: }
221:
222: private void setService(QName serviceName) {
223: mService = serviceName;
224: }
225:
226: private void setEndpoint(ServiceEndpoint endpoint) {
227: mEndpoint = endpoint;
228: }
229:
230: /** Used to construct the URI flavor of the pattern constants.
231: */
232: private static URI createURI(String uri) {
233: try {
234: return (new URI(uri));
235: } catch (java.net.URISyntaxException uriEx) {
236: throw new java.lang.IllegalArgumentException();
237: }
238: }
239:
240: }
|