1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.repository;
35
36 import info.magnolia.repository.definition.RepositoryDefinition;
37 import info.magnolia.repository.definition.WorkspaceMappingDefinition;
38
39 import java.util.Collection;
40 import java.util.Hashtable;
41 import java.util.Map;
42
43 import javax.jcr.Repository;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52 public class WorkspaceMapping {
53
54 private static final Logger log = LoggerFactory.getLogger(WorkspaceMapping.class);
55
56
57
58
59 private Map<String, Repository> repositories = new Hashtable<String, Repository>();
60
61
62
63
64 private Map<String, Provider> providers = new Hashtable<String, Provider>();
65
66
67
68
69 private Map<String, RepositoryDefinition> repositoryDefinitions = new Hashtable<String, RepositoryDefinition>();
70
71
72
73
74 private Map<String, WorkspaceMappingDefinition> workspaceMappingDefinitions = new Hashtable<String, WorkspaceMappingDefinition>();
75
76
77 public void clearRepositories() {
78 repositories.clear();
79 }
80
81 public void clearAll() {
82 providers.clear();
83 repositoryDefinitions.clear();
84 workspaceMappingDefinitions.clear();
85 clearRepositories();
86 }
87
88 public void addRepositoryDefinition(RepositoryDefinition repositoryDefinition) {
89 repositoryDefinitions.put(repositoryDefinition.getName(), repositoryDefinition);
90 }
91
92
93
94
95 public void addWorkspaceMapping(WorkspaceMappingDefinition mapping) {
96 if (workspaceMappingDefinitions.containsKey(mapping.getLogicalWorkspaceName())) {
97 return;
98 }
99
100 addWorkspaceMappingDefinition(mapping);
101
102 RepositoryDefinition repositoryDefinition = getRepositoryDefinition(mapping.getRepositoryName());
103 if (!repositoryDefinition.getWorkspaces().contains(mapping.getPhysicalWorkspaceName())) {
104 repositoryDefinition.addWorkspace(mapping.getPhysicalWorkspaceName());
105 }
106 }
107
108 public void addWorkspaceMappingDefinition(WorkspaceMappingDefinition definition) {
109 workspaceMappingDefinitions.put(definition.getLogicalWorkspaceName(), definition);
110 }
111
112
113
114
115 public RepositoryDefinition getRepositoryDefinition(String repositoryId) {
116 return repositoryDefinitions.get(repositoryId);
117 }
118
119 public Collection<String> getLogicalWorkspaceNames() {
120 return workspaceMappingDefinitions.keySet();
121 }
122
123 public WorkspaceMappingDefinition getWorkspaceMapping(String logicalWorkspaceName) {
124 return workspaceMappingDefinitions.get(logicalWorkspaceName);
125 }
126
127 public Collection<WorkspaceMappingDefinition> getWorkspaceMappings() {
128 return workspaceMappingDefinitions.values();
129 }
130
131 public void setRepository(String repositoryId, Repository repository) {
132 repositories.put(repositoryId, repository);
133 }
134
135 public void setRepositoryProvider(String repositoryId, Provider provider) {
136 providers.put(repositoryId, provider);
137 }
138
139
140
141
142 public Repository getRepository(String repositoryId) {
143 Repository repository = repositories.get(repositoryId);
144 if (repository == null) {
145 final String s = "Failed to retrieve repository '" + repositoryId + "'. Your Magnolia instance might not have been initialized properly.";
146 log.warn(s);
147 throw new IllegalArgumentException(s);
148 }
149 return repository;
150 }
151
152
153
154
155 public Provider getRepositoryProvider(String repositoryId) {
156 Provider provider = providers.get(repositoryId);
157 if (provider == null) {
158 final String s = "Failed to retrieve repository provider '" + repositoryId + "'. Your Magnolia instance might not have been initialized properly.";
159 log.warn(s);
160 throw new IllegalArgumentException(s);
161 }
162 return provider;
163 }
164
165 public Collection<RepositoryDefinition> getRepositoryDefinitions() {
166 return repositoryDefinitions.values();
167 }
168 }