-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ImporterFactory.java
373 lines (289 loc) · 12 KB
/
ImporterFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.exportimport.resources.importer.internal.util;
import com.liferay.asset.kernel.service.AssetTagLocalService;
import com.liferay.document.library.kernel.service.DLAppLocalService;
import com.liferay.document.library.kernel.service.DLFileEntryLocalService;
import com.liferay.document.library.kernel.service.DLFolderLocalService;
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializer;
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializerTracker;
import com.liferay.dynamic.data.mapping.service.DDMStructureLocalService;
import com.liferay.dynamic.data.mapping.service.DDMTemplateLocalService;
import com.liferay.dynamic.data.mapping.util.DDMXML;
import com.liferay.exportimport.resources.importer.internal.constants.ResourcesImporterConstants;
import com.liferay.exportimport.resources.importer.portlet.preferences.PortletPreferencesTranslator;
import com.liferay.journal.service.JournalArticleLocalService;
import com.liferay.journal.service.JournalFolderLocalService;
import com.liferay.journal.util.JournalConverter;
import com.liferay.portal.kernel.deploy.DeployManagerUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.plugin.PluginPackage;
import com.liferay.portal.kernel.portlet.PortletPreferencesFactory;
import com.liferay.portal.kernel.search.IndexerRegistry;
import com.liferay.portal.kernel.service.GroupLocalService;
import com.liferay.portal.kernel.service.LayoutLocalService;
import com.liferay.portal.kernel.service.LayoutPrototypeLocalService;
import com.liferay.portal.kernel.service.LayoutSetLocalService;
import com.liferay.portal.kernel.service.LayoutSetPrototypeLocalService;
import com.liferay.portal.kernel.service.PortletPreferencesLocalService;
import com.liferay.portal.kernel.service.RepositoryLocalService;
import com.liferay.portal.kernel.service.ThemeLocalService;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.MimeTypes;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.TextFormatter;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.xml.SAXReader;
import com.liferay.portal.search.index.IndexStatusManager;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContext;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;
/**
* @author Michael C. Han
*/
@Component(service = ImporterFactory.class)
public class ImporterFactory {
public static final String RESOURCES_DIR =
"/WEB-INF/classes/resources-importer/";
public static final String TEMPLATES_DIR =
"/WEB-INF/classes/templates-importer/";
public Importer createImporter(
long companyId, ServletContext servletContext,
PluginPackageProperties pluginPackageProperties)
throws Exception {
String resourcesDir = pluginPackageProperties.getResourcesDir();
Set<String> resourcePaths = servletContext.getResourcePaths(
RESOURCES_DIR);
Set<String> templatePaths = servletContext.getResourcePaths(
TEMPLATES_DIR);
URL privateLARURL = null;
URL publicLARURL = servletContext.getResource(
RESOURCES_DIR.concat("archive.lar"));
if (publicLARURL == null) {
privateLARURL = servletContext.getResource(
RESOURCES_DIR.concat("private.lar"));
publicLARURL = servletContext.getResource(
RESOURCES_DIR.concat("public.lar"));
}
Importer importer = null;
if ((privateLARURL != null) || (publicLARURL != null)) {
LARImporter larImporter = getLARImporter();
URLConnection privateLARURLConnection = null;
if (privateLARURL != null) {
privateLARURLConnection = privateLARURL.openConnection();
larImporter.setPrivateLARInputStream(
privateLARURLConnection.getInputStream());
}
URLConnection publicLARURLConnection = null;
if (publicLARURL != null) {
publicLARURLConnection = publicLARURL.openConnection();
larImporter.setPublicLARInputStream(
publicLARURLConnection.getInputStream());
}
importer = larImporter;
}
else if ((resourcePaths != null) && !resourcePaths.isEmpty()) {
importer = getResourceImporter();
importer.setJournalConverter(_journalConverter);
importer.setResourcesDir(RESOURCES_DIR);
}
else if ((templatePaths != null) && !templatePaths.isEmpty()) {
importer = getResourceImporter();
Group group = _groupLocalService.getCompanyGroup(companyId);
importer.setGroupId(group.getGroupId());
importer.setJournalConverter(_journalConverter);
importer.setResourcesDir(TEMPLATES_DIR);
}
else if (Validator.isNotNull(resourcesDir)) {
importer = getFileSystemImporter();
importer.setJournalConverter(_journalConverter);
importer.setResourcesDir(resourcesDir);
}
if (importer == null) {
throw new ImporterException("No valid importer found");
}
configureImporter(
companyId, importer, servletContext, pluginPackageProperties);
return importer;
}
protected void configureImporter(
long companyId, Importer importer, ServletContext servletContext,
PluginPackageProperties pluginPackageProperties)
throws Exception {
importer.setAppendVersion(pluginPackageProperties.isAppendVersion());
importer.setCompanyId(companyId);
importer.setDeveloperModeEnabled(
pluginPackageProperties.isDeveloperModeEnabled());
importer.setIndexAfterImport(
pluginPackageProperties.indexAfterImport());
importer.setServletContext(servletContext);
importer.setServletContextName(servletContext.getServletContextName());
importer.setTargetClassName(
pluginPackageProperties.getTargetClassName());
String targetValue = pluginPackageProperties.getTargetValue();
if (Validator.isNull(targetValue)) {
targetValue = TextFormatter.format(
servletContext.getServletContextName(), TextFormatter.J);
}
importer.setTargetValue(targetValue);
importer.setUpdateModeEnabled(
pluginPackageProperties.isUpdateModeEnabled());
PluginPackage pluginPackage =
DeployManagerUtil.getInstalledPluginPackage(
servletContext.getServletContextName());
importer.setVersion(pluginPackage.getVersion());
importer.afterPropertiesSet();
}
protected DDMFormDeserializer getDDMFormJSONDeserializer() {
return _ddmFormDeserializerTracker.getDDMFormDeserializer("json");
}
protected DDMFormDeserializer getDDMFormXSDDeserializer() {
return _ddmFormDeserializerTracker.getDDMFormDeserializer("xsd");
}
protected FileSystemImporter getFileSystemImporter() {
return new FileSystemImporter(
_assetTagLocalService, getDDMFormJSONDeserializer(),
getDDMFormXSDDeserializer(), _ddmStructureLocalService,
_ddmTemplateLocalService, _ddmxml, _dlAppLocalService,
_dlFileEntryLocalService, _dlFolderLocalService,
_indexStatusManager, _indexerRegistry, _journalArticleLocalService,
_journalFolderLocalService, _layoutLocalService,
_layoutPrototypeLocalService, _layoutSetLocalService,
_layoutSetPrototypeLocalService, _mimeTypes, _portal,
_portletPreferencesFactory, _portletPreferencesLocalService,
_portletPreferencesTranslator, _portletPreferencesTranslators,
_repositoryLocalService, _saxReader, _themeLocalService);
}
protected LARImporter getLARImporter() {
return new LARImporter();
}
protected ResourceImporter getResourceImporter() {
return new ResourceImporter(
_assetTagLocalService, getDDMFormJSONDeserializer(),
getDDMFormXSDDeserializer(), _ddmStructureLocalService,
_ddmTemplateLocalService, _ddmxml, _dlAppLocalService,
_dlFileEntryLocalService, _dlFolderLocalService,
_indexStatusManager, _indexerRegistry, _journalArticleLocalService,
_journalFolderLocalService, _layoutLocalService,
_layoutPrototypeLocalService, _layoutSetLocalService,
_layoutSetPrototypeLocalService, _mimeTypes, _portal,
_portletPreferencesFactory, _portletPreferencesLocalService,
_portletPreferencesTranslator, _portletPreferencesTranslators,
_repositoryLocalService, _saxReader, _themeLocalService);
}
@Reference(
cardinality = ReferenceCardinality.AT_LEAST_ONE,
policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY,
target = "(!(portlet.preferences.translator.portlet.id=" + ResourcesImporterConstants.PORTLET_ID_DEFAULT + "))"
)
protected void setPortletPreferencesTranslator(
PortletPreferencesTranslator portletPreferencesTranslator,
Map<String, Object> properties) {
String rootPortletId = GetterUtil.getString(
properties.get("portlet.preferences.translator.portlet.id"));
if (Validator.isNotNull(rootPortletId)) {
_portletPreferencesTranslators.put(
rootPortletId, portletPreferencesTranslator);
}
if (_log.isWarnEnabled()) {
_log.warn(
"The property \"portlet.preferences.translator.portlet.id\" " +
"is null");
}
}
protected void unsetPortletPreferencesTranslator(
PortletPreferencesTranslator portletPreferencesTranslator,
Map<String, Object> properties) {
String rootPortletId = GetterUtil.getString(
properties.get("rootPortletId"));
if (Validator.isNull(rootPortletId)) {
if (_log.isWarnEnabled()) {
_log.warn(
"No rootPortletId defined for service: " +
portletPreferencesTranslator);
}
return;
}
_portletPreferencesTranslators.remove(rootPortletId);
}
private static final Log _log = LogFactoryUtil.getLog(
ImporterFactory.class);
@Reference
private AssetTagLocalService _assetTagLocalService;
@Reference
private DDMFormDeserializerTracker _ddmFormDeserializerTracker;
@Reference
private DDMStructureLocalService _ddmStructureLocalService;
@Reference
private DDMTemplateLocalService _ddmTemplateLocalService;
@Reference
private DDMXML _ddmxml;
@Reference
private DLAppLocalService _dlAppLocalService;
@Reference
private DLFileEntryLocalService _dlFileEntryLocalService;
@Reference
private DLFolderLocalService _dlFolderLocalService;
@Reference
private GroupLocalService _groupLocalService;
@Reference
private IndexerRegistry _indexerRegistry;
@Reference
private IndexStatusManager _indexStatusManager;
@Reference
private JournalArticleLocalService _journalArticleLocalService;
@Reference
private JournalConverter _journalConverter;
@Reference
private JournalFolderLocalService _journalFolderLocalService;
@Reference
private LayoutLocalService _layoutLocalService;
@Reference
private LayoutPrototypeLocalService _layoutPrototypeLocalService;
@Reference
private LayoutSetLocalService _layoutSetLocalService;
@Reference
private LayoutSetPrototypeLocalService _layoutSetPrototypeLocalService;
@Reference
private MimeTypes _mimeTypes;
@Reference
private Portal _portal;
@Reference
private PortletPreferencesFactory _portletPreferencesFactory;
@Reference
private PortletPreferencesLocalService _portletPreferencesLocalService;
@Reference(
target = "(portlet.preferences.translator.portlet.id=" + ResourcesImporterConstants.PORTLET_ID_DEFAULT + ")"
)
private PortletPreferencesTranslator _portletPreferencesTranslator;
private final Map<String, PortletPreferencesTranslator>
_portletPreferencesTranslators = new ConcurrentHashMap<>();
@Reference
private RepositoryLocalService _repositoryLocalService;
@Reference
private SAXReader _saxReader;
@Reference
private ThemeLocalService _themeLocalService;
}