View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Maintains a registry of repositories and a list of mappings from logical workspaces to the real workspaces in the
50   * repositories. It also keeps lookup maps of providers and {@link Repository} instances for each repository.
51   */
52  public class WorkspaceMapping {
53  
54      private static final Logger log = LoggerFactory.getLogger(WorkspaceMapping.class);
55  
56      /**
57       * Map of Repository instances for each repository.
58       */
59      private Map<String, Repository> repositories = new Hashtable<String, Repository>();
60  
61      /**
62       * Map of Provider instances for each repository.
63       */
64      private Map<String, Provider> providers = new Hashtable<String, Provider>();
65  
66      /**
67       * Map of configured repositories.
68       */
69      private Map<String, RepositoryDefinition> repositoryDefinitions = new Hashtable<String, RepositoryDefinition>();
70  
71      /**
72       * Map of configured workspace mappings.
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       * Adds workspace mapping, if the physical workspace doesn't exist it is added.
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      * Returns a configured repository definition.
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      * Returns repository specified by the <code>repositoryID</code> as configured in repository config.
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      * Returns repository provider specified by the <code>repositoryID</code> as configured in repository config.
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 }