View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.connector.jbosscache;
25  
26  import java.util.UUID;
27  import org.jboss.cache.Cache;
28  import org.jboss.cache.Fqn;
29  import org.jboss.cache.Node;
30  import org.modeshape.graph.connector.base.MapWorkspace;
31  
32  /**
33   * 
34   */
35  public class JBossCacheWorkspace extends MapWorkspace<JBossCacheNode> {
36  
37      protected static Node<UUID, JBossCacheNode> findOrCreateWorkspaceRoot( String workspaceName,
38                                                                             Cache<UUID, JBossCacheNode> workspaceCache,
39                                                                             UUID rootNodeUuid ) {
40          assert workspaceName != null;
41          assert workspaceCache != null;
42          assert rootNodeUuid != null;
43  
44          // Make sure the cache has a node for the workspace ...
45          Node<UUID, JBossCacheNode> workspace = workspaceCache.getRoot().getChild(Fqn.fromElements(workspaceName));
46          if (workspace == null) {
47              workspace = workspaceCache.getRoot().addChild(Fqn.fromElements(workspaceName));
48          }
49          // Make sure there is a root node ...
50          JBossCacheNode rootNode = workspace.get(rootNodeUuid);
51          if (rootNode == null) {
52              rootNode = new JBossCacheNode(rootNodeUuid);
53              workspace.put(rootNodeUuid, rootNode);
54          }
55          return workspace;
56      }
57  
58      private Cache<UUID, JBossCacheNode> workspaceCache;
59      private Node<UUID, JBossCacheNode> workspaceNode;
60  
61      /**
62       * Create a new workspace instance.
63       * 
64       * @param name the name of the workspace
65       * @param workspaceCache the Infinispan cache containing the workspace content
66       * @param rootNode the root node for the workspace
67       */
68      public JBossCacheWorkspace( String name,
69                                  Cache<UUID, JBossCacheNode> workspaceCache,
70                                  JBossCacheNode rootNode ) {
71          super(name, rootNode);
72          this.workspaceCache = workspaceCache;
73          this.workspaceNode = findOrCreateWorkspaceRoot(this.name, this.workspaceCache, this.rootNodeUuid);
74      }
75  
76      /**
77       * Create a new workspace instance.
78       * 
79       * @param name the name of the workspace
80       * @param workspaceCache the Infinispan cache containing the workspace content
81       * @param originalToClone the workspace that is to be cloned
82       */
83      public JBossCacheWorkspace( String name,
84                                  Cache<UUID, JBossCacheNode> workspaceCache,
85                                  JBossCacheWorkspace originalToClone ) {
86          super(name, originalToClone);
87          this.workspaceCache = workspaceCache;
88          this.workspaceNode = findOrCreateWorkspaceRoot(this.name, this.workspaceCache, this.rootNodeUuid);
89      }
90  
91      /**
92       * {@inheritDoc}
93       * 
94       * @see org.modeshape.graph.connector.base.MapWorkspace#getNode(java.util.UUID)
95       */
96      @Override
97      public JBossCacheNode getNode( UUID uuid ) {
98          assert uuid != null;
99          return workspaceNode.get(uuid);
100     }
101 
102     /**
103      * {@inheritDoc}
104      * 
105      * @see org.modeshape.graph.connector.base.MapWorkspace#putNode(org.modeshape.graph.connector.base.MapNode)
106      */
107     @Override
108     public JBossCacheNode putNode( JBossCacheNode node ) {
109         assert node != null;
110         JBossCacheNode result = workspaceNode.put(node.getUuid(), node);
111         workspaceNode = findOrCreateWorkspaceRoot(this.name, this.workspaceCache, this.rootNodeUuid);
112         return result;
113     }
114 
115     /**
116      * {@inheritDoc}
117      * 
118      * @see org.modeshape.graph.connector.base.MapWorkspace#removeNode(java.util.UUID)
119      */
120     @Override
121     public JBossCacheNode removeNode( UUID uuid ) {
122         assert uuid != null;
123         JBossCacheNode result = workspaceNode.remove(uuid);
124         workspaceNode = findOrCreateWorkspaceRoot(this.name, this.workspaceCache, this.rootNodeUuid);
125         return result;
126     }
127 
128     /**
129      * {@inheritDoc}
130      * 
131      * @see org.modeshape.graph.connector.base.MapWorkspace#removeAll()
132      */
133     @Override
134     public void removeAll() {
135         workspaceNode.clearData();
136         workspaceNode = findOrCreateWorkspaceRoot(this.name, this.workspaceCache, this.rootNodeUuid);
137     }
138 
139     /**
140      * This method shuts down the workspace and makes it no longer usable. This method should also only be called once.
141      */
142     public void shutdown() {
143         this.workspaceCache.stop();
144     }
145 
146 }