View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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 java.util.Collection;
37  import java.util.Hashtable;
38  import java.util.Map;
39  import javax.jcr.Repository;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import info.magnolia.repository.definition.RepositoryDefinition;
45  import info.magnolia.repository.definition.WorkspaceMappingDefinition;
46  
47  /**
48   * Maintains a registry of repositories and a list of mappings from logical workspaces to the real workspaces in the
49   * repositories. It also keeps lookup maps of providers and {@link Repository} instances for each repository.
50   *
51   * @version $Id$
52   */
53  public class WorkspaceMapping {
54  
55      private static final Logger log = LoggerFactory.getLogger(WorkspaceMapping.class);
56  
57      /**
58       * Map of Repository instances for each repository.
59       */
60      private Map<String, Repository> repositories = new Hashtable<String, Repository>();
61  
62      /**
63       * Map of Provider instances for each repository.
64       */
65      private Map<String, Provider> providers = new Hashtable<String, Provider>();
66  
67      /**
68       * Map of configured repositories.
69       */
70      private Map<String, RepositoryDefinition> repositoryDefinitions = new Hashtable<String, RepositoryDefinition>();
71  
72      /**
73       * Map of configured workspace mappings.
74       */
75      private Map<String, WorkspaceMappingDefinition> workspaceMappingDefinitions = new Hashtable<String, WorkspaceMappingDefinition>();
76  
77  
78      public void clearRepositories() {
79          repositories.clear();
80      }
81  
82      public void clearAll() {
83          providers.clear();
84          repositoryDefinitions.clear();
85          workspaceMappingDefinitions.clear();
86          clearRepositories();
87      }
88  
89      public void addRepositoryDefinition(RepositoryDefinition repositoryDefinition) {
90          repositoryDefinitions.put(repositoryDefinition.getName(), repositoryDefinition);
91      }
92  
93      /**
94       * Adds workspace mapping, if the physical workspace doesn't exist it is added.
95       */
96      public void addWorkspaceMapping(WorkspaceMappingDefinition mapping) {
97          if (workspaceMappingDefinitions.containsKey(mapping.getLogicalWorkspaceName())) {
98              return;
99          }
100 
101         addWorkspaceMappingDefinition(mapping);
102 
103         RepositoryDefinition repositoryDefinition = getRepositoryDefinition(mapping.getRepositoryName());
104         if (!repositoryDefinition.getWorkspaces().contains(mapping.getPhysicalWorkspaceName())) {
105             repositoryDefinition.addWorkspace(mapping.getPhysicalWorkspaceName());
106         }
107     }
108 
109     public void addWorkspaceMappingDefinition(WorkspaceMappingDefinition definition) {
110         workspaceMappingDefinitions.put(definition.getLogicalWorkspaceName(), definition);
111     }
112 
113     /**
114      * Returns a configured repository definition.
115      */
116     public RepositoryDefinition getRepositoryDefinition(String repositoryId) {
117         return repositoryDefinitions.get(repositoryId);
118     }
119 
120     public Collection<String> getLogicalWorkspaceNames() {
121         return workspaceMappingDefinitions.keySet();
122     }
123 
124     public WorkspaceMappingDefinition getWorkspaceMapping(String logicalWorkspaceName) {
125         return workspaceMappingDefinitions.get(logicalWorkspaceName);
126     }
127 
128     public Collection<WorkspaceMappingDefinition> getWorkspaceMappings() {
129         return workspaceMappingDefinitions.values();
130     }
131 
132     public void setRepository(String repositoryId, Repository repository) {
133         repositories.put(repositoryId, repository);
134     }
135 
136     public void setRepositoryProvider(String repositoryId, Provider provider) {
137         providers.put(repositoryId, provider);
138     }
139 
140     /**
141      * Returns repository specified by the <code>repositoryID</code> as configured in repository config.
142      */
143     public Repository getRepository(String repositoryId) {
144         Repository repository = repositories.get(repositoryId);
145         if (repository == null) {
146             final String s = "Failed to retrieve repository '" + repositoryId + "'. Your Magnolia instance might not have been initialized properly.";
147             log.warn(s);
148             throw new IllegalArgumentException(s);
149         }
150         return repository;
151     }
152 
153     /**
154      * Returns repository provider specified by the <code>repositoryID</code> as configured in repository config.
155      */
156     public Provider getRepositoryProvider(String repositoryId) {
157         Provider provider = providers.get(repositoryId);
158         if (provider == null) {
159             final String s = "Failed to retrieve repository provider '" + repositoryId + "'. Your Magnolia instance might not have been initialized properly.";
160             log.warn(s);
161             throw new IllegalArgumentException(s);
162         }
163         return provider;
164     }
165 
166     public Collection<RepositoryDefinition> getRepositoryDefinitions() {
167         return repositoryDefinitions.values();
168     }
169 }