View Javadoc

1   package org.modeshape.graph.connector.path;
2   
3   import java.util.Set;
4   import java.util.UUID;
5   import java.util.concurrent.ConcurrentHashMap;
6   import java.util.concurrent.ConcurrentMap;
7   import net.jcip.annotations.ThreadSafe;
8   import org.modeshape.common.util.CheckArg;
9   import org.modeshape.graph.ExecutionContext;
10  import org.modeshape.graph.connector.RepositoryContext;
11  import org.modeshape.graph.observe.Observer;
12  
13  @ThreadSafe
14  public abstract class PathRepository {
15  
16      protected final UUID rootNodeUuid;
17      private final String sourceName;
18      private final String defaultWorkspaceName;
19  
20      protected final ConcurrentMap<String, PathWorkspace> workspaces = new ConcurrentHashMap<String, PathWorkspace>();
21  
22      /**
23       * Creates a {@code PathRepository} with the given repository source, root node UUID, and a default workspace with the given
24       * name.
25       * 
26       * @param source the path repository source; may not be null
27       */
28      protected PathRepository( PathRepositorySource source ) {
29          CheckArg.isNotNull(source, "source");
30          CheckArg.isNotEmpty(source.getName(), "source.name");
31          CheckArg.isNotNull(source.getRootNodeUuid(), "source.rootNodeUUID");
32          this.rootNodeUuid = source.getRootNodeUuid();
33          this.sourceName = source.getName();
34          this.defaultWorkspaceName = source.getDefaultWorkspaceName() != null ? source.getDefaultWorkspaceName() : "";
35      }
36  
37      /**
38       * Returns the UUID used by the root nodes in each workspace.
39       * <p>
40       * Note that the root nodes themselves are distinct objects in each workspace and a change to the root node of one workspace
41       * does not imply a change to the root nodes of any other workspaces. However, the JCR specification mandates that all
42       * referenceable root nodes in a repository use a common UUID (in support of node correspondence); therefore this must be
43       * supported by ModeShape.
44       * </p>
45       * 
46       * @return the root node UUID
47       */
48      public final UUID getRootNodeUuid() {
49          return rootNodeUuid;
50      }
51  
52      /**
53       * Returns the logical name (as opposed to the class name) of the repository source that defined this instance of the
54       * repository for use in error, informational, and other contextual messages.
55       * 
56       * @return sourceName the logical name for the repository source name
57       */
58      public String getSourceName() {
59          return sourceName;
60      }
61  
62      protected String getDefaultWorkspaceName() {
63          return defaultWorkspaceName;
64      }
65  
66      /**
67       * Returns a list of the names of the currently created workspaces
68       * 
69       * @return a list of the names of the currently created workspaces
70       */
71      public Set<String> getWorkspaceNames() {
72          return workspaces.keySet();
73      }
74  
75      /**
76       * Returns the workspace with the given name
77       * 
78       * @param name the name of the workspace to return
79       * @return the workspace with the given name; may be null if no workspace with the given name exists
80       */
81      public PathWorkspace getWorkspace( String name ) {
82          if (name == null) name = defaultWorkspaceName;
83          return workspaces.get(name);
84      }
85  
86      /**
87       * Initializes the repository by creating the default workspace.
88       * <p>
89       * Due to the ordering restrictions on constructor chaining, this method cannot be called until the repository is fully
90       * initialized. <b>This method MUST be called at the end of the constructor by any class that implements {@code MapRepository}
91       * .</b>
92       */
93      protected abstract void initialize();
94  
95      public boolean isWritable() {
96          return false;
97      }
98  
99  
100     PathRequestProcessor createRequestProcessor( ExecutionContext context,
101                                                  PathRepositorySource source ) {
102         RepositoryContext repositoryContext = source.getRepositoryContext();
103         Observer observer = repositoryContext != null ? repositoryContext.getObserver() : null;
104         boolean updatesAllowed = source.areUpdatesAllowed();
105 
106         /**
107          * Read-only implementations can use a NOP transaction.
108          */
109         PathRepositoryTransaction txn = new PathRepositoryTransaction() {
110 
111             public void commit() {
112             }
113 
114             public void rollback() {
115             }
116 
117         };
118 
119         return new PathRequestProcessor(context, this, observer, updatesAllowed, txn);
120     }
121 }