001
014
015 package com.liferay.portal.repository.registry;
016
017 import com.liferay.portal.kernel.repository.RepositoryFactory;
018 import com.liferay.portal.kernel.repository.registry.RepositoryDefiner;
019 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
020 import com.liferay.portal.repository.external.LegacyExternalRepositoryDefiner;
021 import com.liferay.portal.repository.util.ExternalRepositoryFactory;
022 import com.liferay.portal.repository.util.ExternalRepositoryFactoryImpl;
023 import com.liferay.portal.repository.util.ExternalRepositoryFactoryUtil;
024 import com.liferay.portal.util.PropsValues;
025 import com.liferay.registry.Registry;
026 import com.liferay.registry.RegistryUtil;
027 import com.liferay.registry.ServiceReference;
028 import com.liferay.registry.ServiceRegistration;
029 import com.liferay.registry.ServiceTracker;
030 import com.liferay.registry.ServiceTrackerCustomizer;
031 import com.liferay.registry.collections.StringServiceRegistrationMap;
032 import com.liferay.registry.collections.StringServiceRegistrationMapImpl;
033
034 import java.util.Collection;
035 import java.util.List;
036 import java.util.Map;
037 import java.util.concurrent.ConcurrentHashMap;
038
039
042 public class RepositoryClassDefinitionCatalogImpl
043 implements RepositoryClassDefinitionCatalog {
044
045 @Override
046 public Iterable<RepositoryClassDefinition>
047 getExternalRepositoryClassDefinitions() {
048
049 return _externalRepositoryClassDefinitions.values();
050 }
051
052 @Override
053 public Collection<String> getExternalRepositoryClassNames() {
054 return _externalRepositoryClassDefinitions.keySet();
055 }
056
057 @Override
058 public RepositoryClassDefinition getRepositoryClassDefinition(
059 String className) {
060
061 return _repositoryClassDefinitions.get(className);
062 }
063
064 public void loadDefaultRepositoryDefiners() {
065 Registry registry = RegistryUtil.getRegistry();
066
067 _serviceTracker = registry.trackServices(
068 RepositoryDefiner.class,
069 new RepositoryDefinerServiceTrackerCustomizer());
070
071 _serviceTracker.open();
072
073 for (RepositoryDefiner repositoryDefiner : _repositoryDefiners) {
074 registerRepositoryDefiner(repositoryDefiner);
075 }
076
077 ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
078
079 for (String className : PropsValues.DL_REPOSITORY_IMPL) {
080 ExternalRepositoryFactory externalRepositoryFactory =
081 new ExternalRepositoryFactoryImpl(className, classLoader);
082
083 registerLegacyExternalRepositoryFactory(
084 className, externalRepositoryFactory);
085 }
086 }
087
088 @Override
089 public void registerLegacyExternalRepositoryFactory(
090 String className, ExternalRepositoryFactory externalRepositoryFactory) {
091
092 ExternalRepositoryFactoryUtil.registerExternalRepositoryFactory(
093 className, externalRepositoryFactory);
094
095 RepositoryDefiner repositoryDefiner =
096 new LegacyExternalRepositoryDefiner(
097 className, _legacyExternalRepositoryFactory);
098
099 ServiceRegistration<RepositoryDefiner> serviceRegistration =
100 registerRepositoryDefiner(repositoryDefiner);
101
102 _serviceRegistrations.put(className, serviceRegistration);
103 }
104
105 public void setLegacyExternalRepositoryFactory(
106 RepositoryFactory legacyExternalRepositoryFactory) {
107
108 _legacyExternalRepositoryFactory = legacyExternalRepositoryFactory;
109 }
110
111 public void setRepositoryDefiners(
112 List<RepositoryDefiner> repositoryDefiners) {
113
114 _repositoryDefiners = repositoryDefiners;
115 }
116
117 @Override
118 public void unregisterLegacyExternalRepositoryFactory(String className) {
119 ExternalRepositoryFactoryUtil.unregisterExternalRepositoryFactory(
120 className);
121
122 ServiceRegistration<RepositoryDefiner> serviceRegistration =
123 _serviceRegistrations.remove(className);
124
125 serviceRegistration.unregister();
126
127 unregisterRepositoryDefiner(className);
128 }
129
130 protected ServiceRegistration<RepositoryDefiner>
131 registerRepositoryDefiner(
132 RepositoryDefiner repositoryDefiner) {
133
134 Registry registry = RegistryUtil.getRegistry();
135
136 return registry.registerService(
137 RepositoryDefiner.class, repositoryDefiner);
138 }
139
140 protected void unregisterRepositoryDefiner(String className) {
141 _externalRepositoryClassDefinitions.remove(className);
142
143 _repositoryClassDefinitions.remove(className);
144 }
145
146 private final Map<String, RepositoryClassDefinition>
147 _externalRepositoryClassDefinitions = new ConcurrentHashMap<>();
148 private RepositoryFactory _legacyExternalRepositoryFactory;
149 private final Map<String, RepositoryClassDefinition>
150 _repositoryClassDefinitions = new ConcurrentHashMap<>();
151 private List<RepositoryDefiner> _repositoryDefiners;
152 private final StringServiceRegistrationMap<RepositoryDefiner>
153 _serviceRegistrations = new StringServiceRegistrationMapImpl<>();
154 private ServiceTracker<RepositoryDefiner, RepositoryDefiner>
155 _serviceTracker;
156
157 private class RepositoryDefinerServiceTrackerCustomizer
158 implements ServiceTrackerCustomizer
159 <RepositoryDefiner, RepositoryDefiner> {
160
161 @Override
162 public RepositoryDefiner addingService(
163 ServiceReference<RepositoryDefiner> serviceReference) {
164
165 Registry registry = RegistryUtil.getRegistry();
166
167 RepositoryDefiner repositoryDefiner = registry.getService(
168 serviceReference);
169
170 String className = repositoryDefiner.getClassName();
171 RepositoryClassDefinition repositoryClassDefinition =
172 RepositoryClassDefinition.fromRepositoryDefiner(
173 repositoryDefiner);
174
175 if (repositoryDefiner.isExternalRepository()) {
176 _externalRepositoryClassDefinitions.put(
177 className, repositoryClassDefinition);
178 }
179
180 _repositoryClassDefinitions.put(
181 className, repositoryClassDefinition);
182
183 return repositoryDefiner;
184 }
185
186 @Override
187 public void modifiedService(
188 ServiceReference<RepositoryDefiner> serviceReference,
189 RepositoryDefiner repositoryDefiner) {
190
191 String className = repositoryDefiner.getClassName();
192 RepositoryClassDefinition repositoryClassDefinition =
193 RepositoryClassDefinition.fromRepositoryDefiner(
194 repositoryDefiner);
195
196 if (repositoryDefiner.isExternalRepository()) {
197 _externalRepositoryClassDefinitions.put(
198 className, repositoryClassDefinition);
199 }
200 else {
201 _externalRepositoryClassDefinitions.remove(className);
202 }
203
204 _repositoryClassDefinitions.put(
205 className, repositoryClassDefinition);
206 }
207
208 @Override
209 public void removedService(
210 ServiceReference<RepositoryDefiner> serviceReference,
211 RepositoryDefiner repositoryDefiner) {
212
213 unregisterRepositoryDefiner(repositoryDefiner.getClassName());
214 }
215
216 }
217
218 }