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 java.util.HashSet;
20  import java.util.Set;
21  
22  /**
23   * This class represents a simple Set collection.
24   * 
25   * It is beyond the scope of this quickstart to provide a full bullet-proof implementation of a Resource Manager. Therefore a
26   * simple Mock implementation is provided.
27   * 
28   * You should refer to the XTS demo project, shipped as part of the JBossTS project, for a more complete example.
29   * 
30   * @author paul.robinson@redhat.com, 2011-12-21
31   */
32  public class MockSetManager {
33  
34      private static final Set<String> set = new HashSet<String>();
35  
36      /**
37       * Add a value to the set
38       * 
39       * @param item Item to add to the set.
40       * @throws AlreadyInSetException if the item is already in the set.
41       */
42      public static void add(String item) throws AlreadyInSetException {
43          synchronized (set) {
44  
45              if (set.contains(item)) {
46                  throw new AlreadyInSetException("item '" + item + "' is already in the set.");
47              }
48              set.add(item);
49          }
50      }
51  
52      /**
53       * Persist sufficient data, such that the add operation can be undone or made permanent when told to do so by a call to
54       * commit or rollback.
55       * 
56       * As this is a mock implementation, the method does nothing and always returns true.
57       * 
58       * @return true if the SetManager is able to commit and the required state was persisted. False otherwise.
59       */
60      public static boolean prepare() {
61          return true;
62      }
63  
64      /**
65       * Make the outcome of the add operation permanent.
66       * 
67       * As this is a mock implementation, the method does nothing.
68       */
69      public static void commit() {
70          System.out
71                  .println("[SERVICE] Commit the backend resource (e.g. commit any changes to databases so that they are visible to others)");
72      }
73  
74      /**
75       * Undo any changes made by the add operation.
76       * 
77       * As this is a mock implementation, the method needs to be informed of how to undo the work of the add operation. Typically
78       * resource managers will already know this information.
79       * 
80       * @param item The item to remove from the set in order to undo the effects of the add operation.
81       */
82      public static void rollback(String item) {
83          System.out.println("[SERVICE] Compensate the backend resource by removing '" + item
84                  + "' from the set (e.g. undo any changes to databases that were previously made visible to others)");
85          synchronized (set) {
86  
87              set.remove(item);
88          }
89  
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      public static boolean isInSet(String value) {
99          return set.contains(value);
100     }
101 
102     /**
103      * Empty the set
104      */
105     public static void clear() {
106         set.clear();
107     }
108 }