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.cms.beans.config;
35  
36  import info.magnolia.cms.core.SystemProperty;
37  import info.magnolia.cms.security.AccessDeniedException;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.repository.Provider;
40  import info.magnolia.repository.RepositoryConstants;
41  import info.magnolia.repository.RepositoryManager;
42  import info.magnolia.repository.RepositoryNotInitializedException;
43  import info.magnolia.repository.definition.RepositoryDefinition;
44  import info.magnolia.repository.definition.WorkspaceMappingDefinition;
45  
46  import java.util.Collection;
47  import java.util.Iterator;
48  
49  import javax.jcr.Repository;
50  import javax.jcr.RepositoryException;
51  
52  import org.apache.commons.lang.StringUtils;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  
57  /**
58   * Legacy type - will be dropped with one of the next release.
59   *
60   * @version $Id$
61   *
62   * @deprecated since 4.5 - Use {@link info.magnolia.repository.RepositoryConstants}, {@link info.magnolia.repository.WorkspaceMapping}, {@link info.magnolia.jcr.registry.SessionProviderRegistry} instead.
63   */
64  public final class ContentRepository {
65  
66      private static final Logger log = LoggerFactory.getLogger(ContentRepository.class);
67  
68      /**
69       * @deprecated Use {@link RepositoryConstants#WEBSITE} instead
70       */
71      public static final String WEBSITE = RepositoryConstants.WEBSITE;
72  
73      /**
74       * @deprecated Use {@link RepositoryConstants#USERS} instead
75       */
76      public static final String USERS = RepositoryConstants.USERS;
77  
78      /**
79       * @deprecated Use {@link RepositoryConstants#USER_ROLES} instead
80       */
81      public static final String USER_ROLES = RepositoryConstants.USER_ROLES;
82  
83      /**
84       * @deprecated Use {@link RepositoryConstants#USER_GROUPS} instead
85       */
86      public static final String USER_GROUPS = RepositoryConstants.USER_GROUPS;
87  
88      /**
89       * @deprecated Use {@link RepositoryConstants#CONFIG} instead
90       */
91      public static final String CONFIG = RepositoryConstants.CONFIG;
92  
93      /**
94       * @deprecated Use {@link RepositoryConstants#VERSION_STORE} instead
95       */
96      public static final String VERSION_STORE = RepositoryConstants.VERSION_STORE;
97  
98      /**
99       * magnolia namespace.
100      * @deprecated Use {@link RepositoryConstants#NAMESPACE_PREFIX} instead
101      */
102     public static final String NAMESPACE_PREFIX = RepositoryConstants.NAMESPACE_PREFIX;
103 
104     /**
105      * @deprecated Use {@link RepositoryConstants#NAMESPACE_URI} instead
106      */
107     public static final String NAMESPACE_URI = RepositoryConstants.NAMESPACE_URI;
108 
109     public static final String DEFAULT_WORKSPACE = "default";
110 
111     /**
112      * Name of the repository admin user.
113      *
114      * @deprecated since 4.5 - do not use
115      */
116     public static String REPOSITORY_USER;
117 
118     /**
119      * Password for the repository admin user.
120      *
121      * @deprecated since 4.5 - do not use
122      */
123     public static String REPOSITORY_PSWD;
124 
125     static {
126         REPOSITORY_USER = SystemProperty.getProperty("magnolia.connection.jcr.userId");
127         REPOSITORY_PSWD = SystemProperty.getProperty("magnolia.connection.jcr.password");
128     }
129 
130     /**
131      * Utility class, don't instantiate.
132      */
133     private ContentRepository() {
134         // unused constructor
135     }
136 
137     /**
138      * Loads all configured repositories.
139      *
140      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#init()} directly.
141      */
142     public static void init() {
143         Components.getComponent(RepositoryManager.class).init();
144     }
145 
146     /**
147      * Shuts down all repositories (through Provider instances) and clears all mappings.
148      *
149      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#shutdown()} directly.
150      */
151     public static void shutdown() {
152         Components.getComponent(RepositoryManager.class).shutdown();
153     }
154 
155     /**
156      * Verify the initialization state of all the workspaces. This methods returns <code>false</code> only if
157      * <strong>all</strong> the workspaces are empty (no node else than the root one).
158      *
159      * @return <code>false</code> if all the workspaces are empty, <code>true</code> if at least one of them has content.
160      * @throws AccessDeniedException repository authentication failed
161      * @throws RepositoryException exception while accessing the repository
162      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#checkIfInitialized()} directly.
163      */
164     public static boolean checkIfInitialized() throws AccessDeniedException, RepositoryException {
165         return Components.getComponent(RepositoryManager.class).checkIfInitialized();
166     }
167 
168     /**
169      * Checks if a workspace has been initialized.
170      *
171      * @throws RepositoryException
172      * @throws AccessDeniedException
173      * @throws RuntimeException if the workspace doesn't exist or has not yet been loaded
174      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#checkIfInitialized(String)} directly.
175      */
176     public static boolean checkIfInitialized(String logicalWorkspaceName) throws RepositoryException, AccessDeniedException {
177         return Components.getComponent(RepositoryManager.class).checkIfInitialized(logicalWorkspaceName);
178     }
179 
180     /**
181      * Reload all configured repositories.
182      *
183      * @see #init()
184      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#reload()} directly.
185      */
186     public static void reload() {
187         Components.getComponent(RepositoryManager.class).reload();
188     }
189 
190     /**
191      * Adds a repository definition and loads it. You must not call this method twice.
192      *
193      * @param repositoryDefinition
194      * @throws RepositoryNotInitializedException
195      * @throws InstantiationException
196      * @throws IllegalAccessException
197      * @throws ClassNotFoundException
198      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#loadRepository(info.magnolia.repository.definition.RepositoryDefinition)} directly.
199      */
200     public static void loadRepository(RepositoryDefinition repositoryDefinition) throws RepositoryNotInitializedException, InstantiationException, IllegalAccessException, ClassNotFoundException {
201         Components.getComponent(RepositoryManager.class).loadRepository(repositoryDefinition);
202     }
203 
204     /**
205      * Loads a previously not loaded workspace.
206      *
207      * If there's no logical workspace defined with the name it is added, if there's no physical workspace name by this name it is added.
208      *
209      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#loadWorkspace(String, String)} directly.
210      * */
211     public static void loadWorkspace(String repositoryId, String workspaceName) throws RepositoryException {
212         Components.getComponent(RepositoryManager.class).loadWorkspace(repositoryId, workspaceName);
213     }
214 
215     /**
216      * Returns magnolia specific Repository name where this workspace is registered within <Repository/>.
217      *
218      * Note: if more than one repository has a physical workspace by this name this method is ambigious as to which it returns
219      *
220      * @throws RepositoryException if no physical workspace exists by that name
221      * @deprecated since 4.5 - do not use.
222      */
223     public static String getParentRepositoryName(String physicalWorkspaceName) throws RepositoryException {
224         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
225         for (WorkspaceMappingDefinition mapping : repositoryManager.getWorkspaceMappings()) {
226             if (physicalWorkspaceName.equalsIgnoreCase(mapping.getPhysicalWorkspaceName())) {
227                 return mapping.getRepositoryName();
228             }
229         }
230         throw new RepositoryException("No mapping found for "+physicalWorkspaceName+" repository in magnolia repositories.xml");
231     }
232 
233     /**
234      * Returns the name of the repository for a logical workspace name.
235      *
236      * @returns the repository name or if the logical name doesn't exist it is returned
237      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#getWorkspaceMapping(String)}.
238      */
239     public static String getMappedRepositoryName(String logicalWorkspaceName) {
240         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
241         WorkspaceMappingDefinition mapping = repositoryManager.getWorkspaceMapping(logicalWorkspaceName);
242         return mapping != null ? mapping.getRepositoryName() : logicalWorkspaceName;
243     }
244 
245     /**
246      * Returns the physical workspace name given a logical workspace name.
247      *
248      * @returns the physical name or if the logical name doesn't exist it is returned
249      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#getWorkspaceMapping(String)}.
250      */
251     public static String getMappedWorkspaceName(String logicalWorkspaceName) {
252         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
253         WorkspaceMappingDefinition mapping = repositoryManager.getWorkspaceMapping(logicalWorkspaceName);
254         return mapping != null ? mapping.getPhysicalWorkspaceName() : logicalWorkspaceName;
255     }
256 
257     /**
258      * Adds a workspace mapping.
259      *
260      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#addWorkspaceMapping(info.magnolia.repository.definition.WorkspaceMappingDefinition)}  directly.
261      */
262     public static void addMappedRepositoryName(String logicalWorkspaceName, String repositoryName) {
263         addMappedRepositoryName(logicalWorkspaceName, repositoryName, null);
264     }
265 
266     /**
267      * Adds a workspace mapping.
268      *
269      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#addWorkspaceMapping(info.magnolia.repository.definition.WorkspaceMappingDefinition)}  directly.
270      */
271     public static void addMappedRepositoryName(String logicalWorkspaceName, String repositoryName, String workspaceName) {
272         if (StringUtils.isEmpty(workspaceName)) {
273             workspaceName = logicalWorkspaceName;
274         }
275         Components.getComponent(RepositoryManager.class).addWorkspaceMapping(new WorkspaceMappingDefinition(logicalWorkspaceName, repositoryName, workspaceName));
276     }
277 
278     /**
279      * Get default workspace name.
280      *
281      * @return default name if there are no workspaces defined or there is no workspace present with name "default", otherwise return same name as repository name.
282      */
283     public static String getDefaultWorkspace(String repositoryId) {
284         RepositoryDefinition mapping = getRepositoryMapping(repositoryId);
285         if (mapping == null) {
286             return DEFAULT_WORKSPACE;
287         }
288         Collection<String> workspaces = mapping.getWorkspaces();
289         if (workspaces.contains(getMappedWorkspaceName(repositoryId))) {
290             return repositoryId;
291         }
292         return DEFAULT_WORKSPACE;
293     }
294 
295     /**
296      * Returns the repository for a repository or a workspace.
297      *
298      * @throws IllegalArgumentException if there is no repository or workspace by this name
299      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#getRepository(String)}  directly.
300      */
301     public static Repository getRepository(String repositoryOrLogicalWorkspace) {
302         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
303         if (!repositoryManager.hasRepository(repositoryOrLogicalWorkspace)) {
304             repositoryOrLogicalWorkspace = getMappedRepositoryName(repositoryOrLogicalWorkspace);
305         }
306         return repositoryManager.getRepository(repositoryOrLogicalWorkspace);
307     }
308 
309     /**
310      * Returns the repository provider for either a repository or the provider for the repository which contains the workspace.
311      *
312      * @throws IllegalArgumentException if there is no repository or workspace by this name
313      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#getRepositoryProvider(String)}  directly.
314      */
315     public static Provider getRepositoryProvider(String repositoryOrLogicalWorkspace) {
316         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
317         if (!repositoryManager.hasRepository(repositoryOrLogicalWorkspace)) {
318             repositoryOrLogicalWorkspace = getMappedRepositoryName(repositoryOrLogicalWorkspace);
319         }
320         return repositoryManager.getRepositoryProvider(repositoryOrLogicalWorkspace);
321     }
322 
323     /**
324      * Returns the repository definition for a repository or the repository for a given workspace.
325      *
326      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#getRepositoryDefinition(String)}  directly.
327      */
328     public static RepositoryDefinition getRepositoryMapping(String repositoryOrLogicalWorkspace) {
329         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
330         if (!repositoryManager.hasRepository(repositoryOrLogicalWorkspace)) {
331             repositoryOrLogicalWorkspace = getMappedRepositoryName(repositoryOrLogicalWorkspace);
332         }
333         return repositoryManager.getRepositoryDefinition(repositoryOrLogicalWorkspace);
334     }
335 
336     /**
337      * Returns <code>true</code> if a mapping for the given repository name or workspace name exist.
338      *
339      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#hasRepository(String)} directly.
340      */
341     public static boolean hasRepositoryMapping(String repositoryOrLogicalWorkspace) {
342         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
343         if (!repositoryManager.hasRepository(repositoryOrLogicalWorkspace)) {
344             repositoryOrLogicalWorkspace = getMappedRepositoryName(repositoryOrLogicalWorkspace);
345         }
346         return repositoryManager.hasRepository(repositoryOrLogicalWorkspace);
347     }
348 
349     /**
350      * Gets the names of all logical workspaces.
351      * 
352      * @deprecated since 4.5 - use {@link info.magnolia.repository.RepositoryManager#getWorkspaceNames()} directly.
353      */
354     public static Iterator<String> getAllRepositoryNames() {
355         return Components.getComponent(RepositoryManager.class).getWorkspaceNames().iterator();
356     }
357 
358     /**
359      * Returns the logical workspace name given a physical workspace name. Note that this method will return the FIRST
360      * physical workspace it finds with this name. If there are more workspaces with this name it is not defined which
361      * is returned.
362      *
363      * @return the logical workspace name or if no such logical workspace exists returns the physical workspace name given as an argument
364      * @deprecated since 4.5 - do not use.
365      * */
366     public static String getInternalWorkspaceName(String physicalWorkspaceName) {
367         RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
368         Collection<WorkspaceMappingDefinition> repositoryNameMap = repositoryManager.getWorkspaceMappings();
369         for (WorkspaceMappingDefinition mappingDefinition : repositoryNameMap) {
370             if (mappingDefinition.getPhysicalWorkspaceName().equalsIgnoreCase(physicalWorkspaceName)) {
371                 return mappingDefinition.getLogicalWorkspaceName();
372             }
373         }
374         log.error("No Repository/Workspace name mapping defined for "+physicalWorkspaceName);
375         return physicalWorkspaceName;
376     }
377 
378 
379 
380 }