View Javadoc

1   /*
2      Copyright 2010 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package mirrormap.commands;
17  
18  import java.io.Serializable;
19  import java.util.logging.Level;
20  import java.util.logging.Logger;
21  
22  import mirrormap.MirrorMap;
23  
24  /**
25   * Encapsulates a command to put a key-value entry into a {@link MirrorMap}.
26   * 
27   * @author Ramon Servadei
28   */
29  public class AddCommand extends AbstractCommand
30  {
31      private final static Logger LOGGER =
32          Logger.getLogger(AddCommand.class.getName());
33  
34      private static final long serialVersionUID = 1L;
35  
36      /** The value the command applies to */
37      protected Serializable value;
38  
39      public AddCommand()
40      {
41          super();
42      }
43  
44      public AddCommand(String mirrorMapName, Serializable key, Serializable value)
45      {
46          super();
47          this.mirrorMapName = mirrorMapName;
48          this.key = key;
49          this.value = value;
50      }
51  
52      public final void execute()
53      {
54          final MirrorMap<Serializable, Serializable> mirrorMap =
55              MirrorMap.get(this.mirrorMapName);
56          if (mirrorMap != null)
57          {
58              if (shouldExecute(mirrorMap))
59              {
60                  mirrorMap.put(this.key, this.value);
61              }
62              else
63              {
64                  if (LOGGER.isLoggable(Level.FINEST))
65                  {
66                      LOGGER.finest("Ignoring " + this + " for "
67                          + this.mirrorMapName);
68                  }
69              }
70          }
71          else
72          {
73              if (LOGGER.isLoggable(Level.FINEST))
74              {
75                  LOGGER.finest("No map found for " + this.mirrorMapName);
76              }
77          }
78      }
79  
80      /**
81       * Determines whether the command should be executed. This exists to prevent
82       * the source of a change from re-applying it when it receives the same
83       * change back from the mirror map server (all peers receive the same
84       * change, even the originating one)
85       * 
86       * @param mirrorMap
87       *            the mirror map affected
88       * @return <code>true</code> if this command should be executed
89       */
90      protected boolean shouldExecute(
91          final MirrorMap<Serializable, Serializable> mirrorMap)
92      {
93          return !mirrorMap.containsKey(this.key);
94      }
95  }