View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.wsba.coordinatorcompletion.simple;
18  
19  import com.arjuna.mw.wst11.BusinessActivityManager;
20  import com.arjuna.mw.wst11.BusinessActivityManagerFactory;
21  import com.arjuna.wst.SystemException;
22  import org.jboss.as.quickstarts.wsba.coordinatorcompletion.simple.jaxws.SetServiceBA;
23  import javax.jws.HandlerChain;
24  import javax.jws.WebMethod;
25  import javax.jws.WebService;
26  import javax.jws.soap.SOAPBinding;
27  import javax.servlet.annotation.WebServlet;
28  import java.util.UUID;
29  
30  /**
31   * An adapter class that exposes a set as a transactional Web Service.
32   * 
33   * @author Paul Robinson (paul.robinson@redhat.com)
34   */
35  @WebService(serviceName = "SetServiceBAService", portName = "SetServiceBA", name = "SetServiceBA", targetNamespace = "http://www.jboss.org/jboss-jdf/jboss-as-quickstart/helloworld/wsba/coordinatorcompletion/set")
36  @HandlerChain(file = "/context-handlers.xml", name = "Context Handlers")
37  @SOAPBinding(style = SOAPBinding.Style.RPC)
38  @WebServlet("/SetServiceBA")
39  public class SetServiceBAImpl implements SetServiceBA {
40      /**
41       * Add an item to a set and enroll a Participant if necessary then pass the call through to the business logic.
42       * 
43       * @param value the value to add to the set.
44       * @throws AlreadyInSetException if value is already in the set
45       * @throws SetServiceException if an error occurred when attempting to add the item to the set.
46       */
47      @WebMethod
48      public void addValueToSet(String value) throws AlreadyInSetException, SetServiceException {
49  
50          System.out.println("[SERVICE] invoked addValueToSet('" + value + "')");
51  
52          BusinessActivityManager activityManager = BusinessActivityManagerFactory.businessActivityManager();
53  
54          /*
55           * get the transaction context of this thread:
56           */
57          String transactionId;
58          try {
59              transactionId = activityManager.currentTransaction().toString();
60          } catch (SystemException e) {
61              throw new SetServiceException("Unable to lookup existing BusinesActivity", e);
62          }
63  
64          /*
65           * Lookup existing participant or register new participant
66           */
67          SetParticipantBA participantBA = SetParticipantBA.getParticipant(transactionId);
68  
69          if (participantBA == null) {
70              try {
71                  // enlist the Participant for this service:
72                  SetParticipantBA participant = new SetParticipantBA(transactionId, value);
73                  SetParticipantBA.recordParticipant(transactionId, participant);
74  
75                  System.out.println("[SERVICE] Enlisting a participant into the BA");
76                  activityManager.enlistForBusinessAgreementWithCoordinatorCompletion(participant, "SetServiceBAImpl:"
77                          + UUID.randomUUID());
78              } catch (Exception e) {
79                  System.err.println("Participant enlistment failed");
80                  throw new SetServiceException("Error enlisting participant", e);
81              }
82          } else {
83              System.out.println("[SERVICE] Re-using the existing participant, already registered for this BA");
84              participantBA.addValue(value);
85          }
86  
87          // invoke the back-end business logic
88          System.out.println("[SERVICE] Invoking the back-end business logic");
89          MockSetManager.add(value);
90      }
91  
92      /**
93       * Query the set to see if it contains a particular value.
94       * 
95       * @param value the value to check for.
96       * @return true if the value was present, false otherwise.
97       */
98      @WebMethod
99      public boolean isInSet(String value) {
100         return MockSetManager.isInSet(value);
101     }
102 
103     /**
104      * Empty the set
105      * <p/>
106      * Note: To simplify this example, this method is not part of the compensation logic, so will not be undone if the BA is
107      * compensated. It can also be invoked outside of an active BA.
108      */
109     @WebMethod
110     public void clear() {
111         MockSetManager.clear();
112     }
113 }