001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.tx.coordinator;
037:
038: import com.sun.istack.NotNull;
039: import com.sun.xml.ws.api.SOAPVersion;
040: import com.sun.xml.ws.api.addressing.AddressingVersion;
041: import com.sun.xml.ws.api.addressing.OneWayFeature;
042: import com.sun.xml.ws.api.addressing.WSEndpointReference;
043: import com.sun.xml.ws.api.tx.Protocol;
044: import com.sun.xml.ws.developer.MemberSubmissionEndpointReference;
045: import com.sun.xml.ws.developer.StatefulWebServiceManager;
046: import com.sun.xml.ws.tx.at.ATParticipant;
047: import com.sun.xml.ws.tx.common.ActivityIdentifier;
048: import com.sun.xml.ws.tx.common.AddressManager;
049: import com.sun.xml.ws.tx.common.StatefulWebserviceFactory;
050: import com.sun.xml.ws.tx.common.StatefulWebserviceFactoryFactory;
051: import com.sun.xml.ws.tx.common.TxFault;
052: import com.sun.xml.ws.tx.common.TxLogger;
053: import com.sun.xml.ws.tx.common.WsaHelper;
054: import com.sun.xml.ws.tx.webservice.member.coord.RegisterResponseType;
055: import com.sun.xml.ws.tx.webservice.member.coord.RegisterType;
056: import com.sun.xml.ws.tx.webservice.member.coord.RegistrationCoordinatorPortType;
057: import com.sun.xml.ws.tx.webservice.member.coord.RegistrationPortTypeRPC;
058: import com.sun.xml.ws.tx.webservice.member.coord.RegistrationRequesterPortType;
059:
060: import javax.xml.ws.EndpointReference;
061: import javax.xml.ws.WebServiceContext;
062: import javax.xml.ws.WebServiceException;
063: import javax.xml.ws.WebServiceFeature;
064: import java.net.URI;
065: import java.util.logging.Level;
066:
067: /**
068: * This singleton class handles the register and registerResponse operations for
069: * both local and remote (or external) clients. The exposed web service endpoints
070: * for register and registerResponse delegate to the methods in this class.
071: *
072: * @author Ryan.Shoemaker@Sun.COM
073: * @version $Revision: 1.16 $
074: * @since 1.0
075: */
076: public final class RegistrationManager {
077:
078: /* singleton instance */
079: private static final RegistrationManager instance = new RegistrationManager();
080:
081: private static final URI localRegistrationURI = AddressManager
082: .getPreferredAddress(RegistrationPortTypeRPC.class);
083:
084: private static final URI localAsynchronousRegistrationURI = AddressManager
085: .getPreferredAddress(RegistrationCoordinatorPortType.class);
086:
087: private static final URI localRegistrationRequesterURI = AddressManager
088: .getPreferredAddress(RegistrationRequesterPortType.class);
089:
090: // public static EndpointReference newSynchronousRegistrationEPR(ActivityIdentifier activityId) {
091: // EndpointReference registrationEPR =
092: // WsaHelper.getAddressingBuilder().newEndpointReference(localRegistrationURI);
093: // WsaHelper.addRefParam(registrationEPR, activityId.getSOAPElement());
094: // return registrationEPR;
095: // }
096:
097: public static URI getLocalRegistrationURI() {
098: return localRegistrationURI;
099: }
100:
101: public static URI getLocalAsyncRegistrationURI() {
102: return localAsynchronousRegistrationURI;
103: }
104:
105: public static URI getLocalRegistrationRequesterURI() {
106: return localRegistrationRequesterURI;
107: }
108:
109: /**
110: * Create a new EPR for our registration service.
111: * <p/>
112: * Note: as a side-effect, this method creates a stateful instance of the registration service that
113: * will handle correlation when a client actually registers with us. Should we consider moving this
114: * code to point of use (ie during coordination context creation)?
115: *
116: * @param activityId the coordination id for this activity, maintained as state in the registration service
117: * @return an EPR containing the address of our registration service
118: */
119: public static EndpointReference newRegistrationEPR(
120: ActivityIdentifier activityId) {
121: StatefulWebserviceFactory swf = StatefulWebserviceFactoryFactory
122: .getInstance();
123: return swf.createService("Coordinator",
124: "RegistrationCoordinator",
125: localAsynchronousRegistrationURI,
126: AddressingVersion.MEMBER, activityId.getValue(), null);
127: }
128:
129: public static StatefulWebServiceManager getRegistrationCoordinatorStatefulWebServiceManager() {
130: StatefulWebserviceFactory swf = StatefulWebserviceFactoryFactory
131: .getInstance();
132: return swf.getManager("Coordinator", "RegistrationCoordinator");
133: }
134:
135: private static TxLogger logger = TxLogger
136: .getCoordLogger(RegistrationManager.class);
137:
138: /**
139: * reference to the CoordinationManager
140: */
141: private static final CoordinationManager coordinationManager = CoordinationManager
142: .getInstance();
143:
144: /**
145: * Singleton constructor
146: */
147: private RegistrationManager() {
148: }
149:
150: /**
151: * Get the singleton instance of the RegistrationManager
152: *
153: * @return the RegistrationManager
154: */
155: public static RegistrationManager getInstance() {
156: return instance;
157: }
158:
159: /**
160: * Handle an incoming <register> web service request from an external Participant
161: * and send a <registerResponse> back.
162: *
163: * @param wsContext webservice context
164: * @param registerRequest the incoming <register> request
165: * @param activityId activity id
166: */
167: public void register(@NotNull
168: WebServiceContext wsContext, @NotNull
169: String activityId, @NotNull
170: RegisterType registerRequest) {
171: if (logger.isLogging(Level.FINER)) {
172: logger
173: .entering("RegistrationManager.register(WebserviceContext, RegisterType)");
174: }
175:
176: // request message must have wsa:MessageId and wsa:ReplyTo, cache them
177: String msgID = WsaHelper.getMsgID(wsContext);
178: EndpointReference registrationRequesterEPR = WsaHelper
179: .getReplyTo(wsContext);
180: WSEndpointReference faultTo = WsaHelper.getFaultTo(wsContext);
181: if (logger.isLogging(Level.FINEST)) {
182: logger.finest("register", "activityId:" + activityId
183: + " register request msg id: " + msgID
184: + " replyTo: " + registrationRequesterEPR);
185: }
186: if (registrationRequesterEPR == null) {
187: if (faultTo != null) {
188: // send fault S4.3 wscoor:Invalid Parameters
189: WsaHelper.sendFault(faultTo, null, SOAPVersion.SOAP_11,
190: TxFault.InvalidParameters,
191: "register wsa:replyTo must be set for activityId "
192: + activityId + " and msgId: " + msgID, // no I18N - spec requires xml:lang="en"
193: msgID);
194: }
195: throw new WebServiceException(LocalizationMessages
196: .REGISTER_REPLYTO_NOT_SET_3003(activityId, msgID));
197: }
198:
199: Coordinator c = coordinationManager.getCoordinator(activityId);
200: if (c == null) {
201: // send fault S4.1 wscoor:Invalid State
202: WsaHelper.sendFault(faultTo, null, SOAPVersion.SOAP_11,
203: TxFault.InvalidState,
204: "attempting to register for an unknown activity Id: "
205: + activityId + " and msgId: " + msgID, // no I18N - spec requires xml:lang="en"
206: msgID);
207: logger.warning("register", LocalizationMessages
208: .REGISTER_FOR_UNKNOWN_ACTIVITY_3004(activityId,
209: msgID));
210: }
211:
212: Registrant r = null;
213: Protocol requestProtocol = Protocol.getProtocol(registerRequest
214: .getProtocolIdentifier());
215: switch (requestProtocol) {
216: case DURABLE:
217: case VOLATILE:
218: case COMPLETION:
219: r = new ATParticipant(c, registerRequest);
220: c.addRegistrant(r, wsContext);
221: break;
222:
223: case WSAT2004:
224: case UNKNOWN:
225: // send fault S4.2 wscoor:Invalid Protocol
226: WsaHelper
227: .sendFault(
228: faultTo,
229: null,
230: SOAPVersion.SOAP_11,
231: TxFault.InvalidState,
232: registerRequest.getProtocolIdentifier()
233: + " is not a recognized coordination type: activityId "
234: + // no I18N - spec requires xml:lang="en"
235: activityId + " and msgId " + msgID,
236: msgID);
237: throw new UnsupportedOperationException(
238: LocalizationMessages
239: .UNRECOGNIZED_COORDINATION_TYPE_3011(
240: registerRequest
241: .getProtocolIdentifier(),
242: activityId, msgID));
243: }
244:
245: /* send <registerResponse> to RegistrationRequesterEPR */
246:
247: // setup relatesTo and get the remote port EPR
248: OneWayFeature owf = new OneWayFeature();
249: owf.setRelatesToID(msgID);
250: RegistrationRequesterPortType registrationRequester = getCoordinatorService()
251: .getRegistrationRequester(registrationRequesterEPR, owf);
252:
253: // build the registerResponse message
254: RegisterResponseType registerResponse = new RegisterResponseType();
255: registerResponse
256: .setCoordinatorProtocolService((MemberSubmissionEndpointReference) r
257: .getCoordinatorProtocolService());
258:
259: // send the message
260: try {
261: registrationRequester
262: .registerResponseOperation(registerResponse);
263: } catch (WebServiceException wse) {
264: logger.warning("register", LocalizationMessages
265: .REGISTERRESPONSE_FAILED_3005(
266: registrationRequesterEPR, activityId,
267: msgID, wse.getLocalizedMessage()));
268: throw wse;
269: } catch (Exception e) {
270: logger.severe("register", LocalizationMessages
271: .REGISTERRESPONSE_FAILED_3005(
272: registrationRequesterEPR, activityId,
273: msgID, e.getLocalizedMessage()));
274: throw new WebServiceException(e);
275: }
276:
277: if (logger.isLogging(Level.FINER)) {
278: logger
279: .exiting("RegistrationManager.register(WebserviceContext, RegisterType)");
280: }
281: }
282:
283: private static final com.sun.xml.ws.tx.webservice.member.coord.Coordinator coordinatorService = new com.sun.xml.ws.tx.webservice.member.coord.Coordinator();
284:
285: @NotNull
286: private com.sun.xml.ws.tx.webservice.member.coord.Coordinator getCoordinatorService() {
287: return coordinatorService;
288: }
289:
290: /**
291: * This is the local entry point for register. Depending on the root registration
292: * service contained in the coordinator, this method will either invoke registerOperation
293: * on a remote registration service or simply register locally with our registration
294: * service.
295: *
296: * @param c Coordinator
297: * @param r registrant
298: */
299: public void register(Coordinator c, Registrant r) {
300: if (logger.isLogging(Level.FINER)) {
301: logger
302: .entering("RegistrationManager.register(Coordinator, Registrant)");
303: }
304:
305: EndpointReference registrationEPR;
306: if (c.registerWithRootRegistrationService(r)) {
307: if (logger.isLogging(Level.FINE)) {
308: logger.fine("RegistrationManager.register",
309: "register with remote coordinator");
310: }
311: // if subordinate, send <register> message to root, wait for <registerResponse>, then return
312: registrationEPR = c.getContext()
313: .getRootRegistrationService();
314: r.setRemoteCPS(true);
315: assert (registrationEPR != null);
316:
317: // Send register to remote registration coordinator to get Coordinator Protocol Service
318: assert r.getCoordinatorProtocolService() == null;
319:
320: // Set register parameter. Same for both synchronous and asynchronous.
321: RegisterType registerParam = new RegisterType();
322: registerParam.setProtocolIdentifier(r.getProtocol()
323: .getUri());
324: MemberSubmissionEndpointReference ppsEpr = (MemberSubmissionEndpointReference) r
325: .getLocalParticipantProtocolService();
326: registerParam.setParticipantProtocolService(ppsEpr);
327: if (logger.isLogging(Level.FINE)) {
328: logger.fine("register", "participant protocol service"
329: + ppsEpr.toString());
330: }
331:
332: // try synchronous register first and fallback to async if it fails
333: // if (synchronousRegister(registrationEPR, c, registerParam, r)) return;
334:
335: // Asynchronous register/wait for asynchronous registerReply
336:
337: // setup stateful ws instance for registerResponse from remote registration coordinator
338: StatefulWebserviceFactory swf = StatefulWebserviceFactoryFactory
339: .getInstance();
340: EndpointReference registrationRequesterEPR = swf
341: .createService("Coordinator",
342: "RegistrationRequester",
343: localRegistrationRequesterURI,
344: AddressingVersion.MEMBER, r
345: .getCoordinator().getIdValue(), r
346: .getIdValue());
347:
348: // set replyTo for outgoing register message
349: OneWayFeature owf = new OneWayFeature();
350: owf.setReplyTo((new WSEndpointReference(
351: registrationRequesterEPR)));
352: RegistrationCoordinatorPortType registerCoordinator = getCoordinatorService()
353: .getRegistrationCoordinator(registrationEPR,
354: (WebServiceFeature) owf);
355:
356: if (logger.isLogging(Level.FINEST)) {
357: logger.finest("RegistrationManager.register",
358: "send wscoor:register to epr:"
359: + registrationEPR + " replyTo EPR: "
360: + owf.getReplyTo());
361: }
362:
363: boolean timedOut;
364: r.setRegistrationCompleted(false);
365: try {
366: // prefer to try synchronouos register/register response than busy wait.
367: registerCoordinator.registerOperation(registerParam);
368:
369: // next line is necessary. race condition that register did not complete before
370: // tranaction initiator committed/rolled back tranaction.
371: // This is why synchronous register preferable to asynchronous register.
372: timedOut = r.waitForRegistrationResponse();
373: if (logger.isLogging(Level.FINEST)) {
374: logger.finest("register(Coordinator, Registrant)",
375: "timedOut = " + timedOut);
376: }
377: } catch (WebServiceException wse) {
378: logger.warning("register", LocalizationMessages
379: .REGISTER_FAILED_3006(registrationEPR, c
380: .getIdValue(), wse
381: .getLocalizedMessage()));
382: throw wse;
383: }
384:
385: if (r.isRegistrationCompleted()) {
386: if (logger.isLogging(Level.FINE)) {
387: logger
388: .fine(
389: "register",
390: "asynch registration succeeded. Coordinator Protocol Service is "
391: + r
392: .getCoordinatorProtocolService());
393: }
394: }
395:
396: if (timedOut) {
397: // send fault S4.4 wscoor:No Activity
398: WsaHelper
399: .sendFault(null, registrationEPR,
400: SOAPVersion.SOAP_11,
401: TxFault.NoActivity,
402: "registration timed out for activity id: "
403: + c.getIdValue(), // no I18N - spec requires xml:lang="en"
404: null /* TODO: what is RelatesTo in this case? */);
405: logger.warning("register", LocalizationMessages
406: .REGISTRATION_TIMEOUT_3007(c.getIdValue(),
407: registrationEPR));
408: }
409:
410: // reply processed in #registerResponse(WebServiceContext, RegisterResponseType) gets CPS for registrant.
411: } else {
412: if (logger.isLogging(Level.FINE)) {
413: logger.fine("RegistrationManager.register",
414: "register with local coordinator");
415: }
416: // else root coordinator, simply register and return
417: r.setCoordinatorProtocolService(c
418: .getCoordinatorProtocolServiceForRegistrant(r));
419: c.addRegistrant(r, null);
420: }
421:
422: if (logger.isLogging(Level.FINER)) {
423: logger
424: .exiting("RegistrationManager.register(Coordinator, Registrant)");
425: }
426: }
427:
428: /**
429: * Process an incoming <registerResponse> message.
430: *
431: * @param activityId activity id
432: * @param registrantId registrant id
433: * @param registerResponse <registerResponse> message
434: * @param wsContext context of the inbound web service invocation
435: */
436: public void registerResponse(@NotNull
437: WebServiceContext wsContext, @NotNull
438: String activityId, @NotNull
439: String registrantId, @NotNull
440: RegisterResponseType registerResponse) {
441: if (logger.isLogging(Level.FINER)) {
442: logger.entering("RegistrationManager.registerResponse");
443: }
444:
445: // look up the registrant and remove it from outstanding Registrants
446: Registrant r = Registrant
447: .getOutstandingRegistrant(registrantId);
448: if (r == null) {
449: // send fault S4.1 wscoor:Invalid State
450: WsaHelper.sendFault(wsContext, SOAPVersion.SOAP_11,
451: TxFault.InvalidState,
452: "received registerResponse for non-existent registrant : "
453: + // no I18N - spec requires xml:lang="en"
454: registrantId + " for activityId:"
455: + activityId);
456: logger.warning("registerResponse", LocalizationMessages
457: .NONEXISTENT_REGISTRANT_3008(registrantId,
458: activityId));
459: } else {
460: // set coordinator protocol service on registrant
461: r.setCoordinatorProtocolService(registerResponse
462: .getCoordinatorProtocolService());
463: r.setRemoteCPS(true);
464: r.getCoordinator().addRegistrant(r, wsContext);
465: Registrant.removeOutstandingRegistrant(registrantId);
466: if (logger.isLogging(Level.FINEST)) {
467: logger.finest("registerResponse",
468: "Completed registration for CoordId:"
469: + activityId + "registrantId:"
470: + registrantId);
471: }
472: }
473:
474: if (logger.isLogging(Level.FINER)) {
475: logger.exiting("RegistrationManager.registerResponse");
476: }
477: }
478:
479: /**
480: * Synchronously register with remote coordinator. Not all
481: * providers will support the synchronous register/registerReply
482: * since it is optional in the 2004 OASIS version of WS-COOR.
483: *
484: * @param registrationEPR epr of remote registration service
485: * @param c coordinator
486: * @param registerParam <register> message
487: * @param r registrant
488: * @return true if registration suceeded, false otherwise
489: */
490: private boolean synchronousRegister(
491: EndpointReference registrationEPR, Coordinator c,
492: RegisterType registerParam, Registrant r) {
493: // RegistrationPortTypeRPC registerRPC = getCoordinatorService().getRegistration();
494: // WsaHelper.initializeAsDestination((BindingProvider) registerRPC, registrationEPR);
495: // ap = WsaHelper.getAddressingProperties((BindingProvider) registerRPC,
496: // WsaHelper.BindingProviderContextType.REQUEST_CONTEXT);
497: // ap.setReplyTo(newSynchronousRegistrationEPR((ActivityIdentifier)c.getId()));
498: // try {
499: // RegisterResponseType response = registerRPC.registerOperation(registerParam);
500: // if (response == null) {
501: // logger.warning("register", "synchronousRegisterRPC failed to get a non-null response.");
502: // } else {
503: // registerResponse(c.getIdValue(), r.getIdValue(), response);
504: // logger.exiting("register", "synchronous register succeeded. Coordination Protocol Service:" +
505: // r.getCoordinatorProtocolService());
506: // return true;
507: // }
508: // } catch (WebServiceException wse) {
509: // // very likely that some WS-AT implementations might not implement this optional
510: // // binding. be prepared to use async version of register/register response.
511: // logger.warning("register", "synchronous register failed, trying required registration protocol");
512: // wse.printStackTrace();
513: // }
514: // logger.warning("register", "synchronous register failed, trying required registration protocol");
515: return false;
516: }
517:
518: /**
519: * Handling incoming synchronous <register> and return <registerResponse>.
520: *
521: * @param activityId activity id
522: * @param registerRequest <register> request
523: * @return a new <registerResponse>
524: * @param wsContext context for incoming web service invocation
525: */
526: @NotNull
527: public static RegisterResponseType synchronousRegister(@NotNull
528: WebServiceContext wsContext, @NotNull
529: String activityId, @NotNull
530: RegisterType registerRequest) {
531: Protocol requestProtocol = Protocol.getProtocol(registerRequest
532: .getProtocolIdentifier());
533: if (logger.isLogging(Level.FINER)) {
534: logger.entering("synchronousRegister", "protocol="
535: + requestProtocol + " coordId=" + activityId);
536: }
537:
538: Coordinator c = coordinationManager.getCoordinator(activityId);
539: if (c == null) {
540: // send fault S4.3 wscoor:Invalid Parameters
541: WsaHelper.sendFault(wsContext, SOAPVersion.SOAP_11,
542: TxFault.InvalidParameters,
543: "Received RegisterResponse for unknown activity id: "
544: + activityId); // no I18N - spec requires xml:lang="en"
545: logger.warning("synchronousRegister", LocalizationMessages
546: .NONEXISTENT_ACTIVITY_3010(activityId));
547: }
548:
549: Registrant r = null;
550: switch (requestProtocol) {
551: case DURABLE:
552: case VOLATILE:
553: case COMPLETION:
554: r = new ATParticipant(c, registerRequest);
555: c.addRegistrant(r, wsContext);
556: break;
557:
558: case WSAT2004:
559: case UNKNOWN:
560: // send fault S4.3 wscoor:Invalid Parameters
561: WsaHelper.sendFault(wsContext, SOAPVersion.SOAP_11,
562: TxFault.InvalidParameters, requestProtocol.getUri()
563: + " is not a recognized coordination type"); // no I18N - spec requires xml:lang="en"
564: throw new UnsupportedOperationException(
565: LocalizationMessages
566: .UNRECOGNIZED_COORDINATION_TYPE_3011(
567: requestProtocol, activityId,
568: WsaHelper.getMsgID(wsContext)));
569: }
570:
571: // build the registerResponse message
572: RegisterResponseType registerResponse = new RegisterResponseType();
573: registerResponse
574: .setCoordinatorProtocolService((MemberSubmissionEndpointReference) r
575: .getCoordinatorProtocolService());
576: if (logger.isLogging(Level.FINER)) {
577: logger.exiting("synchronousRegister", r
578: .getCoordinatorProtocolService());
579: }
580:
581: return registerResponse;
582: }
583: }
|