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.infinispan;
25  
26  import java.util.Set;
27  import java.util.UUID;
28  import java.util.concurrent.locks.Lock;
29  import net.jcip.annotations.NotThreadSafe;
30  import org.infinispan.Cache;
31  import org.modeshape.graph.ExecutionContext;
32  import org.modeshape.graph.connector.base.MapTransaction;
33  import org.modeshape.graph.property.Property;
34  import org.modeshape.graph.property.Path.Segment;
35  import org.modeshape.graph.request.InvalidWorkspaceException;
36  
37  /**
38   * 
39   */
40  @NotThreadSafe
41  public class InfinispanTransaction extends MapTransaction<InfinispanNode, InfinispanWorkspace> {
42  
43      private final InfinispanRepository repository;
44      private final Lock lock;
45  
46      protected InfinispanTransaction( ExecutionContext context,
47                                       InfinispanRepository repository,
48                                       UUID rootNodeUuid,
49                                       Lock lock ) {
50          super(context, repository, rootNodeUuid);
51          this.repository = repository;
52          this.lock = lock;
53      }
54  
55      /**
56       * {@inheritDoc}
57       * 
58       * @see org.modeshape.graph.connector.base.Transaction#getWorkspaceNames()
59       */
60      @Override
61      public Set<String> getWorkspaceNames() {
62          return repository.getWorkspaceNames();
63      }
64  
65      /**
66       * {@inheritDoc}
67       * 
68       * @see org.modeshape.graph.connector.base.Transaction#getWorkspace(java.lang.String,
69       *      org.modeshape.graph.connector.base.Workspace)
70       */
71      public InfinispanWorkspace getWorkspace( String name,
72                                               InfinispanWorkspace originalToClone ) {
73          Cache<UUID, InfinispanNode> workspaceCache = repository.getCacheManager().getCache(name);
74          if (workspaceCache == null) {
75              String msg = InfinispanConnectorI18n.unableToCreateWorkspace.text(name, repository.getSourceName());
76              throw new InvalidWorkspaceException(msg);
77          }
78          if (originalToClone != null) {
79              return new InfinispanWorkspace(name, workspaceCache, originalToClone);
80          }
81          return new InfinispanWorkspace(name, workspaceCache, new InfinispanNode(repository.getRootNodeUuid()));
82      }
83  
84      /**
85       * {@inheritDoc}
86       * 
87       * @see org.modeshape.graph.connector.base.Transaction#destroyWorkspace(org.modeshape.graph.connector.base.Workspace)
88       */
89      public boolean destroyWorkspace( InfinispanWorkspace workspace ) {
90          // Can't seem to tell Infinispan to destroy the cache, so perhaps we should destroy all the content ...
91          workspace.destroy();
92          return true;
93      }
94  
95      /**
96       * {@inheritDoc}
97       * 
98       * @see org.modeshape.graph.connector.base.MapTransaction#createNode(java.util.UUID,
99       *      org.modeshape.graph.property.Path.Segment, java.util.UUID, java.lang.Iterable)
100      */
101     @Override
102     protected InfinispanNode createNode( UUID uuid,
103                                          Segment name,
104                                          UUID parentUuid,
105                                          Iterable<Property> properties ) {
106         return new InfinispanNode(uuid, name, parentUuid, properties, null);
107     }
108 
109     /**
110      * {@inheritDoc}
111      * 
112      * @see org.modeshape.graph.connector.base.Transaction#commit()
113      */
114     @Override
115     public void commit() {
116         try {
117             super.commit();
118         } finally {
119             lock.unlock();
120         }
121     }
122 
123     /**
124      * {@inheritDoc}
125      * 
126      * @see org.modeshape.graph.connector.base.Transaction#rollback()
127      */
128     @Override
129     public void rollback() {
130         try {
131             super.rollback();
132         } finally {
133             lock.unlock();
134         }
135     }
136 
137 }