001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository.registry;
016    
017    import com.liferay.portal.kernel.repository.DocumentRepository;
018    import com.liferay.portal.kernel.repository.LocalRepository;
019    import com.liferay.portal.kernel.repository.Repository;
020    import com.liferay.portal.kernel.repository.capabilities.BaseCapabilityProvider;
021    import com.liferay.portal.kernel.repository.capabilities.Capability;
022    import com.liferay.portal.kernel.repository.capabilities.CapabilityProvider;
023    import com.liferay.portal.kernel.repository.event.RepositoryEventAware;
024    import com.liferay.portal.kernel.repository.registry.CapabilityRegistry;
025    import com.liferay.portal.kernel.repository.registry.RepositoryEventRegistry;
026    import com.liferay.portal.repository.util.RepositoryWrapperAware;
027    
028    import java.util.Map;
029    
030    /**
031     * @author Adolfo P??rez
032     */
033    public class DefaultCapabilityRegistry
034            extends BaseCapabilityProvider
035            implements CapabilityRegistry<DocumentRepository>, CapabilityProvider {
036    
037            public DefaultCapabilityRegistry(DocumentRepository documentRepository) {
038                    _documentRepository = documentRepository;
039            }
040    
041            @Override
042            public <S extends Capability> void addExportedCapability(
043                    Class<S> capabilityClass, S capability) {
044    
045                    super.addExportedCapability(capabilityClass, capability);
046            }
047    
048            @Override
049            public <S extends Capability> void addSupportedCapability(
050                    Class<S> capabilityClass, S capability) {
051    
052                    super.addSupportedCapability(capabilityClass, capability);
053            }
054    
055            @Override
056            public DocumentRepository getTarget() {
057                    return _documentRepository;
058            }
059    
060            public LocalRepository invokeCapabilityWrappers(
061                    LocalRepository localRepository) {
062    
063                    Map<Class<? extends Capability>, Capability> capabilities =
064                            getCapabilities();
065    
066                    for (Capability capability : capabilities.values()) {
067                            if (capability instanceof RepositoryWrapperAware) {
068                                    RepositoryWrapperAware repositoryWrapperAware =
069                                            (RepositoryWrapperAware)capability;
070    
071                                    localRepository = repositoryWrapperAware.wrapLocalRepository(
072                                            localRepository);
073                            }
074                    }
075    
076                    return localRepository;
077            }
078    
079            public Repository invokeCapabilityWrappers(Repository repository) {
080                    Map<Class<? extends Capability>, Capability> capabilities =
081                            getCapabilities();
082    
083                    for (Capability capability : capabilities.values()) {
084                            if (capability instanceof RepositoryWrapperAware) {
085                                    RepositoryWrapperAware repositoryWrapperAware =
086                                            (RepositoryWrapperAware)capability;
087    
088                                    repository = repositoryWrapperAware.wrapRepository(repository);
089                            }
090                    }
091    
092                    return repository;
093            }
094    
095            public void registerCapabilityRepositoryEvents(
096                    RepositoryEventRegistry repositoryEventRegistry) {
097    
098                    Map<Class<? extends Capability>, Capability> capabilities =
099                            getCapabilities();
100    
101                    for (Capability capability : capabilities.values()) {
102                            if (capability instanceof RepositoryEventAware) {
103                                    RepositoryEventAware repositoryEventAware =
104                                            (RepositoryEventAware)capability;
105    
106                                    repositoryEventAware.registerRepositoryEventListeners(
107                                            repositoryEventRegistry);
108                            }
109                    }
110            }
111    
112            @Override
113            protected String getProviderKey() {
114                    return String.valueOf(_documentRepository.getRepositoryId());
115            }
116    
117            private final DocumentRepository _documentRepository;
118    
119    }