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.istack.Nullable;
040: import com.sun.xml.ws.tx.at.ATCoordinator;
041: import com.sun.xml.ws.tx.at.ATSubCoordinator;
042: import com.sun.xml.ws.tx.common.ActivityIdentifier;
043: import static com.sun.xml.ws.tx.common.Constants.WSAT_2004_PROTOCOL;
044: import static com.sun.xml.ws.tx.common.Constants.WSAT_OASIS_NSURI;
045: import com.sun.xml.ws.tx.common.TxLogger;
046: import com.sun.xml.ws.tx.webservice.member.coord.CreateCoordinationContextType;
047:
048: import java.util.HashMap;
049: import java.util.Map;
050: import java.util.logging.Level;
051:
052: /**
053: * This singleton class is responsible for managing coordinated
054: * activities for the entire appserver.
055: * <p/>
056: * Whenever a new coordinated activity is started, a new {@link Coordinator}
057: * object is constructed and managed by this class.
058: * <p/>
059: *
060: * @author Ryan.Shoemaker@Sun.COM
061: * @version $Revision: 1.7 $
062: * @since 1.0
063: */
064: public final class CoordinationManager {
065:
066: /* singleton instance */
067: private static final CoordinationManager instance = new CoordinationManager();
068:
069: /* HashMap<coordination id, Coordinator> */
070: private static final Map<String, Coordinator> coordinators = new HashMap<String, Coordinator>();
071:
072: static private TxLogger logger = TxLogger
073: .getCoordLogger(CoordinationManager.class);
074:
075: /**
076: * private constructor for singleton
077: */
078: private CoordinationManager() {
079: }
080:
081: /**
082: * Return the singleton instance of CoordinationManager.
083: *
084: * @return the CoordinationManager instance
085: */
086: public static CoordinationManager getInstance() {
087: return instance;
088: }
089:
090: /**
091: * Get the {@link Coordinator} object with the given coordination id
092: *
093: * @param id the coordination context id
094: * @return the Coordinator object or null if the id doesn't exist
095: */
096: @Nullable
097: public Coordinator getCoordinator(@NotNull
098: final String id) {
099: return coordinators.get(id);
100: }
101:
102: /**
103: * Add the specified {@link Coordinator} object to the list of managed
104: * activities.
105: * <p/>
106: * TODO: what about duplicate keys or entries?
107: *
108: * @param coordinator coordinator
109: */
110: public void putCoordinator(final Coordinator coordinator) {
111: coordinators.put(coordinator.getIdValue(), coordinator);
112: if (logger.isLogging(Level.FINEST)) {
113: logger.finest("putCoordinator", "add activity id:"
114: + coordinator.getIdValue());
115: }
116: }
117:
118: /**
119: * Remove the specified {@link Coordinator} object from the list of managed
120: * activities.
121: * <p>
122: * @param id activity id
123: */
124: public void removeCoordinator(@NotNull
125: final String id) {
126: final Coordinator c = coordinators.remove(id);
127: if (c != null) {
128: c.forget();
129: }
130: if (logger.isLogging(Level.FINEST)) {
131: logger.finest("removeCoordinator", "remove activity id:"
132: + id);
133: }
134: }
135:
136: /**
137: * Create a {@link Coordinator} object from the incoming request and
138: * add it to the list of managed activities. The actual type of the
139: * {@link Coordinator} object created will depend on the
140: * {@link com.sun.xml.ws.api.tx.Protocol} specified in the contextRequest
141: * parameter.
142: * <p/>
143: * This method is invoked when we receive a createCoordinationContext soap
144: * request.
145: * <p/>
146: *
147: * @param contextRequest the incoming wscoor:createCoordinationContext message
148: * @return the coordinator
149: */
150: @NotNull
151: public Coordinator lookupOrCreateCoordinator(@NotNull
152: final CreateCoordinationContextType contextRequest) {
153: Coordinator c;
154: if (contextRequest.getCurrentContext() == null) {
155: c = createCoordinator(null, contextRequest);
156: } else {
157: c = createSubordinateCoordinator(null, contextRequest);
158: }
159: putCoordinator(c);
160: return c;
161: }
162:
163: /**
164: * Lookup if coordinator exists for context, if not, create a {@link Coordinator} object from the given coordination
165: * context and add it to the list of managed activities. The actual
166: * type of the {@link Coordinator} object created will depend on the
167: * protocol identifier contained in the context.
168: * <p/>
169: * This method is used for direct private invocation within the
170: * appserver.
171: *
172: * @param context the coordination context
173: * @return the coordinator
174: */
175: @NotNull
176: public Coordinator lookupOrCreateCoordinator(@NotNull
177: final CoordinationContextInterface context) {
178: Coordinator c = getCoordinator(context.getIdentifier());
179: if (c == null) {
180: // If registration service EPR is not created locally, make this a subordinate coordinator
181: if (RegistrationManager
182: .getRegistrationCoordinatorStatefulWebServiceManager()
183: .resolve(context.getRegistrationService()) == null) {
184: c = createSubordinateCoordinator(context, null);
185: } else {
186: c = createCoordinator(context, null);
187: }
188: }
189: putCoordinator(c);
190: return c;
191: }
192:
193: /**
194: * create a {@link Coordinator} from either the given context or the given request.
195: * <p/>
196: * One of these parameters MUST be null, one of them MUST be non-null.
197: *
198: * @param context coordination context
199: * @param contextRequest createCoordinationContext soap msg
200: * @return a Coordinator
201: */
202: @NotNull
203: private Coordinator createCoordinator(@Nullable
204: final CoordinationContextInterface context, @Nullable
205: final CreateCoordinationContextType contextRequest) {
206:
207: assert ((context == null) ^ (contextRequest == null)); // xor
208:
209: if (logger.isLogging(Level.FINER)) {
210: logger.entering("CoordinationManager.createCoordinator");
211: }
212:
213: final String coordType;
214: if (contextRequest == null) {
215: coordType = context.getCoordinationType();
216: } else {
217: coordType = contextRequest.getCoordinationType();
218: }
219:
220: if (WSAT_2004_PROTOCOL.equals(coordType)) {
221: final Coordinator coord;
222: if (contextRequest == null) {
223: coord = context.getRootRegistrationService() == null ? new ATCoordinator(
224: context)
225: : new ATSubCoordinator(context);
226: } else {
227: coord = contextRequest.getCurrentContext() == null ? new ATCoordinator(
228: ContextFactory.createContext(contextRequest),
229: contextRequest)
230: : new ATSubCoordinator(ContextFactory
231: .createContext(contextRequest),
232: contextRequest);
233: }
234: if (logger.isLogging(Level.FINEST)) {
235: logger.finest(
236: "CoordinationManager.createCoordinator id=",
237: coord.getIdValue());
238: }
239: if (logger.isLogging(Level.FINER)) {
240: logger.exiting("CoordinationManager.createCoordinator");
241: }
242: return coord;
243: } else if (WSAT_OASIS_NSURI.equals(coordType)) {
244: throw new UnsupportedOperationException(
245: LocalizationMessages.OASIS_UNSUPPORTED_3000());
246: } else {
247: throw new UnsupportedOperationException(
248: LocalizationMessages
249: .UNRECOGNIZED_COORDINATION_TYPE_3001(coordType));
250: }
251: }
252:
253: /**
254: * Create a {@link Coordinator} that delegates to a root coordinator that is probably on a remote
255: * system not under our control.
256: * <p/>
257: * One of these parameters MUST be null, one of them MUST be non-null.
258: *
259: * @param context the original coordination context generated by the root coordinator
260: * @param contextRequest the original createCordinationContext request message
261: * @return a new subordinate coordinator that will delegate for our participants
262: */
263: @NotNull
264: private Coordinator createSubordinateCoordinator(@Nullable
265: final CoordinationContextInterface context, @Nullable
266: final CreateCoordinationContextType contextRequest) {
267:
268: assert ((context == null) ^ (contextRequest == null)); // xor
269:
270: if (logger.isLogging(Level.FINER)) {
271: logger
272: .entering("CoordinationManager.createSubordinateCoordinator");
273: }
274:
275: if (context == null) {
276: if (logger.isLogging(Level.FINER)) {
277: logger
278: .exiting("CoordinationManager.createSubordinateCoordinator");
279: }
280: // in this case, the context should already have the registration EPRs initialized
281: // properly, so we don't have to swap - just create a new coordinator
282: return createCoordinator(null, contextRequest);
283: } else {
284: // replace the registration service EPR with our own, but remember the EPR of the
285: // root coordinator's registration EPR for delegation.
286: context.setRootCoordinatorRegistrationService(context
287: .getRegistrationService());
288:
289: // delegate
290: final Coordinator coord = createCoordinator(context, null);
291: context.setRegistrationService(RegistrationManager
292: .newRegistrationEPR((ActivityIdentifier) coord
293: .getId()));
294: if (logger.isLogging(Level.FINER)) {
295: logger
296: .exiting("CoordinationManager.createSubordinateCoordinator");
297: }
298: return coord;
299: }
300: }
301:
302: }
|