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 * Unless otherwise indicated, all code in ModeShape is licensed
10 * 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.graph.connector.federation;
25
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.concurrent.ExecutorService;
32 import net.jcip.annotations.Immutable;
33 import org.modeshape.common.util.CheckArg;
34 import org.modeshape.graph.GraphI18n;
35 import org.modeshape.graph.cache.CachePolicy;
36 import org.modeshape.graph.connector.RepositoryConnectionFactory;
37 import org.modeshape.graph.connector.RepositorySource;
38 import org.modeshape.graph.request.InvalidWorkspaceException;
39
40 /**
41 * The configuration of a {@link FederatedRepositorySource}.
42 */
43 @Immutable
44 class FederatedRepository {
45
46 private final String sourceName;
47 private final CachePolicy defaultCachePolicy;
48 private final RepositoryConnectionFactory connectionFactory;
49 private final Map<String, FederatedWorkspace> workspacesByName;
50 private final FederatedWorkspace defaultWorkspace;
51 private final ExecutorService executor;
52
53 /**
54 * Construct a new instance of a configuration defining how the workspaces in a federated repository should project the
55 * content from one or more sources.
56 *
57 * @param sourceName the name of the federated repository source; may not be null
58 * @param connectionFactory the factory for connections to the sources being federated; may not be null
59 * @param workspaces the workspaces that make up this federated repository; may not be null or empty
60 * @param defaultCachePolicy the default cache policy for the source, or null if there is no default caching policy for the
61 * whole federated repository (each workspace may have its own)
62 * @param executor the {@link ExecutorService} that can be used to parallelize actions within this repository;may not be null
63 */
64 public FederatedRepository( String sourceName,
65 RepositoryConnectionFactory connectionFactory,
66 Iterable<FederatedWorkspace> workspaces,
67 CachePolicy defaultCachePolicy,
68 ExecutorService executor ) {
69 CheckArg.isNotNull(sourceName, "sourceName");
70 CheckArg.isNotNull(connectionFactory, "connectionFactory");
71 CheckArg.isNotNull(workspaces, "workspaces");
72 CheckArg.isNotNull(executor, "executor");
73 this.sourceName = sourceName;
74 this.connectionFactory = connectionFactory;
75 this.executor = executor;
76 this.defaultCachePolicy = defaultCachePolicy;
77 this.workspacesByName = new HashMap<String, FederatedWorkspace>();
78 FederatedWorkspace defaultWorkspace = null;
79 for (FederatedWorkspace workspace : workspaces) {
80 if (defaultWorkspace == null) defaultWorkspace = workspace;
81 this.workspacesByName.put(workspace.getName(), workspace);
82 }
83 this.defaultWorkspace = defaultWorkspace;
84 assert this.defaultWorkspace != null;
85 assert this.workspacesByName.size() > 0;
86 }
87
88 /**
89 * Get the name of the {@link RepositorySource} that owns this configuration.
90 *
91 * @return the source's name; never null
92 */
93 public String getSourceName() {
94 return sourceName;
95 }
96
97 /**
98 * Get the {@link RepositoryConnectionFactory factory for connections} to the sources that are being federated by this
99 * repository.
100 *
101 * @return the connection factory for the sources; never null
102 */
103 public RepositoryConnectionFactory getConnectionFactory() {
104 return connectionFactory;
105 }
106
107 /**
108 * Get the {@link FederatedWorkspace workspace} information, given its name.
109 *
110 * @param name the name of the workspace, or null if the default workspace should be returned
111 * @return the workspace
112 * @throws InvalidWorkspaceException if the specified workspace does not exist
113 */
114 public FederatedWorkspace getWorkspace( String name ) {
115 if (name == null) {
116 assert defaultWorkspace != null;
117 return defaultWorkspace;
118 }
119 FederatedWorkspace workspace = workspacesByName.get(name);
120 if (workspace == null) {
121 String msg = GraphI18n.workspaceDoesNotExistInFederatedRepository.text(name, getSourceName());
122 throw new InvalidWorkspaceException(msg);
123 }
124 return workspace;
125 }
126
127 /**
128 * Get the names of the available workspaces.
129 *
130 * @return the unmodifiable copy of the workspace names; never null
131 */
132 Set<String> getWorkspaceNames() {
133 return Collections.unmodifiableSet(new HashSet<String>(workspacesByName.keySet()));
134 }
135
136 /**
137 * Get the default cache policy that used for the whole repository. Note that the repository may or may not have a default
138 * caching policy, and each {@link FederatedWorkspace workspace} may have its own {@link FederatedWorkspace#getCachePolicy()
139 * caching policy}.
140 *
141 * @return the default cache policy for the repository, used if/when the workspace(s) don't have their own caching policy or
142 * when the source content does not specify the caching policy; may be null
143 */
144 public CachePolicy getDefaultCachePolicy() {
145 return defaultCachePolicy;
146 }
147
148 /**
149 * Get the executor for this repository. This executor can be used to process tasks.
150 *
151 * @return the executor; may not be null
152 */
153 public ExecutorService getExecutor() {
154 return executor;
155 }
156
157 /**
158 * {@inheritDoc}
159 *
160 * @see java.lang.Object#toString()
161 */
162 @Override
163 public String toString() {
164 return "Federated repository \"" + getSourceName() + "\" with workspaces " + workspacesByName.keySet();
165 }
166 }