SyncTranslationState.java
16.5 KB
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
374
375
376
377
378
379
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.service.GuideLocalizationService
* com.adobe.cq.launches.api.LaunchManagerFactory
* com.adobe.cq.projects.api.Project
* com.adobe.cq.projects.api.ProjectFilter
* com.adobe.cq.projects.api.ProjectManager
* com.adobe.granite.translation.api.TranslationException
* com.adobe.granite.translation.api.TranslationManager
* com.adobe.granite.translation.api.xliff.TranslationXLIFFService
* com.adobe.granite.translation.core.TranslationCloudConfigUtil
* com.adobe.granite.ui.clientlibs.HtmlLibraryManager
* com.day.cq.wcm.api.LanguageManager
* com.day.cq.wcm.api.PageManagerFactory
* com.day.cq.wcm.contentsync.PageExporter
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.ReferencePolicyOption
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.html.HtmlParser
* org.apache.sling.commons.scheduler.Job
* org.apache.sling.commons.scheduler.JobContext
* org.apache.sling.commons.scheduler.ScheduleOptions
* org.apache.sling.commons.scheduler.Scheduler
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.translation.impl.scheduler;
import com.adobe.aemds.guide.service.GuideLocalizationService;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectFilter;
import com.adobe.cq.projects.api.ProjectManager;
import com.adobe.cq.wcm.translation.impl.TranslationPlatformConfiguration;
import com.adobe.cq.wcm.translation.impl.TranslationPodImpl;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.translation.api.xliff.TranslationXLIFFService;
import com.adobe.granite.translation.core.TranslationCloudConfigUtil;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.contentsync.PageExporter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.ReferencePolicyOption;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.html.HtmlParser;
import org.apache.sling.commons.scheduler.Job;
import org.apache.sling.commons.scheduler.JobContext;
import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(name="Translation Sync Job")
public class SyncTranslationState
implements Job {
private static long iSyncTimeout = 0;
private static final Logger log = LoggerFactory.getLogger(SyncTranslationState.class);
@Reference
private SlingRepository referred_repository = null;
@Reference
private TranslationManager referred_translationManager = null;
@Reference
private PageManagerFactory refered_pageManagerFactory = null;
@Reference
private TranslationCloudConfigUtil referred_translationCloudConfigUtil = null;
@Reference
private ResourceResolverFactory referred_resolverFactory = null;
@Reference
private LanguageManager referred_languageManager = null;
@Reference
LaunchManagerFactory referred_launchesManagerFactory = null;
@Reference
private Scheduler scheduler;
@Reference
private HtmlLibraryManager referred_htmlLibraryManager = null;
@Reference
private PageExporter pageExporter = null;
@Reference
private HtmlParser htmlParser = null;
@Reference
private TranslationPlatformConfiguration translationPlatformConfiguration;
@Reference(policy=ReferencePolicy.DYNAMIC, policyOption=ReferencePolicyOption.RELUCTANT, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private TranslationXLIFFService xliffService = null;
private HtmlLibraryManager m_htmlLibraryManager = null;
private SlingRepository m_repository;
private TranslationManager m_translationManager;
private ResourceResolverFactory m_resolverFactory;
TranslationCloudConfigUtil m_translationCloudConfigUtil;
private PageManagerFactory m_pageManagerFactory;
private LanguageManager m_languageManager;
private LaunchManagerFactory m_launchesManagerFactory = null;
@Activate
private void setup(ComponentContext context) {
try {
String cronExpression = this.translationPlatformConfiguration.getCronExpressionForSync();
iSyncTimeout = this.translationPlatformConfiguration.getSyncTimeout();
ScheduleOptions scheduleOptions = this.scheduler.EXPR(cronExpression);
boolean bUnScheduled = this.scheduler.unschedule(SyncTranslationState.class.getCanonicalName());
log.info("Unscheduled old scheduled job ", (Object)bUnScheduled);
scheduleOptions = scheduleOptions.name(SyncTranslationState.class.getCanonicalName()).onSingleInstanceOnly(true);
if (!this.scheduler.schedule((Object)new SyncTranslationState(this.referred_repository, this.referred_resolverFactory, this.referred_translationManager, this.referred_translationCloudConfigUtil, this.refered_pageManagerFactory, this.referred_htmlLibraryManager), scheduleOptions)) {
log.error("Error while scheduling the sync translation state job");
}
}
catch (Exception ex) {
log.error("Error while scheduling the sync translation state job", (Throwable)ex);
}
}
public static long getSyncLockTimeout() {
return iSyncTimeout;
}
public SyncTranslationState() {
}
public static String getRulesFilePath() {
return "/etc/workflow/models/translation/translation_rules.xml";
}
public SyncTranslationState(SlingRepository referred_repository, ResourceResolverFactory referred_resolverFactory, TranslationManager referred_translationManager, TranslationCloudConfigUtil referred_translationCloudConfigUtil, PageManagerFactory referred_pageManagerFactory, HtmlLibraryManager referred_htmlLibraryManager) throws RepositoryException {
this.m_repository = referred_repository;
this.m_resolverFactory = referred_resolverFactory;
this.m_translationManager = referred_translationManager;
this.m_translationCloudConfigUtil = referred_translationCloudConfigUtil;
this.m_pageManagerFactory = referred_pageManagerFactory;
this.m_htmlLibraryManager = referred_htmlLibraryManager;
this.m_languageManager = this.referred_languageManager;
this.m_launchesManagerFactory = this.referred_launchesManagerFactory;
}
@Deactivate
private void cleanup() {
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void execute(JobContext arg) {
log.info("Starting sync process now");
ArrayList<Project> translationProjectArray = null;
ResourceResolver resourceResolver = null;
TranslationRuleConfigurationFile ruleFile = null;
try {
resourceResolver = this.m_resolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "translation-job"));
Session serviceUserSession = (Session)resourceResolver.adaptTo(Session.class);
ruleFile = new TranslationRuleConfigurationFile(resourceResolver, SyncTranslationState.getRulesFilePath(), null, null, this.m_translationManager, this.m_translationCloudConfigUtil, this.m_pageManagerFactory, this.m_htmlLibraryManager, null, this.m_languageManager, this.m_launchesManagerFactory, this.pageExporter, this.htmlParser);
ruleFile.setXLIFFService(this.xliffService);
ProjectManager pm = (ProjectManager)resourceResolver.adaptTo(ProjectManager.class);
translationProjectArray = this.getTranslationProjectList(pm);
for (Project translationProject : translationProjectArray) {
log.trace("Project: {}", (Object)translationProject.getTitle());
this.syncTranslationProject(translationProject, resourceResolver, serviceUserSession, ruleFile);
}
}
catch (Exception e) {
log.error("Error while getting translation project list", (Throwable)e);
}
finally {
if (resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
log.info("Sync process complete");
}
}
private void syncTranslationProject(Project project, ResourceResolver resourceResolver, Session adminSession, TranslationRuleConfigurationFile ruleFile) {
ArrayList<TranslationPodImpl> podList = null;
try {
podList = TranslationPodImpl.getTranslationPodList(resourceResolver, project, adminSession, this.m_translationManager);
}
catch (Exception e) {
log.error("Error while getting the pod list", (Throwable)e);
}
if (podList != null) {
for (TranslationPodImpl pod : podList) {
try {
ruleFile.setLanguageSet(pod.getSourceLanguage(), pod.getDestinationLanguage());
log.trace("Job: {}", (Object)pod.getNode().getName());
this.syncTranslationPodNow(pod, ruleFile, adminSession);
}
catch (Exception e) {
log.error("Error while syncing pod ", (Throwable)e);
}
}
}
}
private void syncTranslationPodNow(TranslationPodImpl pod, TranslationRuleConfigurationFile ruleFile, Session adminSession) throws TranslationException, RepositoryException, InterruptedException {
pod.processPendingCancellationRequest(ruleFile);
Random rand = new Random();
int MAX_SLEEP = 300000;
int iSleepBeforeSync = rand.nextInt(MAX_SLEEP);
adminSession.save();
Thread.sleep(iSleepBeforeSync);
adminSession.refresh(false);
pod.syncTranslationStateIfRequired(ruleFile, adminSession);
}
private ArrayList<Project> getTranslationProjectList(ProjectManager pm) throws PathNotFoundException, RepositoryException {
ArrayList<Project> retVal = new ArrayList<Project>();
Iterator projectList = pm.getProjects(null, 0, 0);
if (projectList != null) {
while (projectList.hasNext()) {
Project project = (Project)projectList.next();
if (!TranslationUtils.isTranslationProject(project)) continue;
retVal.add(project);
}
}
return retVal;
}
protected void bindReferred_repository(SlingRepository slingRepository) {
this.referred_repository = slingRepository;
}
protected void unbindReferred_repository(SlingRepository slingRepository) {
if (this.referred_repository == slingRepository) {
this.referred_repository = null;
}
}
protected void bindReferred_translationManager(TranslationManager translationManager) {
this.referred_translationManager = translationManager;
}
protected void unbindReferred_translationManager(TranslationManager translationManager) {
if (this.referred_translationManager == translationManager) {
this.referred_translationManager = null;
}
}
protected void bindRefered_pageManagerFactory(PageManagerFactory pageManagerFactory) {
this.refered_pageManagerFactory = pageManagerFactory;
}
protected void unbindRefered_pageManagerFactory(PageManagerFactory pageManagerFactory) {
if (this.refered_pageManagerFactory == pageManagerFactory) {
this.refered_pageManagerFactory = null;
}
}
protected void bindReferred_translationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
this.referred_translationCloudConfigUtil = translationCloudConfigUtil;
}
protected void unbindReferred_translationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
if (this.referred_translationCloudConfigUtil == translationCloudConfigUtil) {
this.referred_translationCloudConfigUtil = null;
}
}
protected void bindReferred_resolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.referred_resolverFactory = resourceResolverFactory;
}
protected void unbindReferred_resolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.referred_resolverFactory == resourceResolverFactory) {
this.referred_resolverFactory = null;
}
}
protected void bindReferred_languageManager(LanguageManager languageManager) {
this.referred_languageManager = languageManager;
}
protected void unbindReferred_languageManager(LanguageManager languageManager) {
if (this.referred_languageManager == languageManager) {
this.referred_languageManager = null;
}
}
protected void bindReferred_launchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
this.referred_launchesManagerFactory = launchManagerFactory;
}
protected void unbindReferred_launchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
if (this.referred_launchesManagerFactory == launchManagerFactory) {
this.referred_launchesManagerFactory = null;
}
}
protected void bindScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
protected void unbindScheduler(Scheduler scheduler) {
if (this.scheduler == scheduler) {
this.scheduler = null;
}
}
protected void bindReferred_htmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
this.referred_htmlLibraryManager = htmlLibraryManager;
}
protected void unbindReferred_htmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
if (this.referred_htmlLibraryManager == htmlLibraryManager) {
this.referred_htmlLibraryManager = null;
}
}
protected void bindPageExporter(PageExporter pageExporter) {
this.pageExporter = pageExporter;
}
protected void unbindPageExporter(PageExporter pageExporter) {
if (this.pageExporter == pageExporter) {
this.pageExporter = null;
}
}
protected void bindHtmlParser(HtmlParser htmlParser) {
this.htmlParser = htmlParser;
}
protected void unbindHtmlParser(HtmlParser htmlParser) {
if (this.htmlParser == htmlParser) {
this.htmlParser = null;
}
}
protected void bindTranslationPlatformConfiguration(TranslationPlatformConfiguration translationPlatformConfiguration) {
this.translationPlatformConfiguration = translationPlatformConfiguration;
}
protected void unbindTranslationPlatformConfiguration(TranslationPlatformConfiguration translationPlatformConfiguration) {
if (this.translationPlatformConfiguration == translationPlatformConfiguration) {
this.translationPlatformConfiguration = null;
}
}
protected void bindXliffService(TranslationXLIFFService translationXLIFFService) {
this.xliffService = translationXLIFFService;
}
protected void unbindXliffService(TranslationXLIFFService translationXLIFFService) {
if (this.xliffService == translationXLIFFService) {
this.xliffService = null;
}
}
}