TempCleanUpTask.java
8.02 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.forms.common.servlet;
import com.adobe.forms.common.utils.FormsConstants;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=1, label="AEM Forms Temporary Storage Cleaning Task", description="Cleans the temporary storage used by AEM Forms")
@Service(value={Runnable.class})
@Properties(value={@Property(name="scheduler.expression", value={"0 0 12 * * ?"}, label="Cron expression scheduling this job", description="Cron expression scheduling this job. Default is every hour. See http://www.docjar.com/docs/api/org/quartz/CronTrigger.html for a description of the format for this value"), @Property(name="scheduler.concurrent", boolValue={0}, propertyPrivate=1)})
public class TempCleanUpTask
implements Runnable {
private final Logger log = LoggerFactory.getLogger(TempCleanUpTask.class);
private Calendar todaysDate = Calendar.getInstance();
private final String[] queryPaths = FormsConstants.FD_TEMP_PATHS;
@Reference
private SlingRepository slingRepository;
@Reference
private ResourceResolverFactory resourceResolverFactory;
@Property(name="Duration for Temporary Storage", value={"24"}, label="Cleans the temporary folder older than(In Hours)", description="Service will delete temporary nodes that were created before the (duration_temp_storage) from the current time")
private static final Integer DURATION_TEMP_STORAGE = 24;
private int duration_temp_storage;
@Property(name="Duration for Anonymous Storage", value={"1"}, label="Cleans the anonymous temporary folder older than(In Hours)", description="Service will delete anonymous temporary nodes that were created before the (duration_temp_storage) from the current time")
private static final Integer DURATION_ANONYMOUS_STORAGE = 1;
private int duration_anonymous_storage;
protected void activate(ComponentContext context) {
Dictionary props = context.getProperties();
this.duration_temp_storage = OsgiUtil.toInteger(props.get("Duration for Temporary Storage"), (int)DURATION_TEMP_STORAGE);
this.duration_anonymous_storage = OsgiUtil.toInteger(props.get("Duration for Anonymous Storage"), (int)DURATION_ANONYMOUS_STORAGE);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void run() {
Session session = null;
try {
session = this.slingRepository.loginService(null, null);
ArrayList<Node> staleNodesList = new ArrayList<Node>();
this.getTempNodes(session, staleNodesList);
for (Node staleNode : staleNodesList) {
try {
staleNode.remove();
}
catch (Exception e) {
this.log.error("Error while removing the node [ " + staleNode.getPath() + "]", (Throwable)e);
}
}
session.save();
}
catch (RepositoryException e) {
this.log.error("Error while processing the uuid nodes list.", (Throwable)e);
}
finally {
if (session != null && session.isLive()) {
session.logout();
}
}
}
private void getTempNodes(Session session, List<Node> staleNodesList) {
try {
for (int i = 0; i < this.queryPaths.length; ++i) {
Node res = session.getNode(this.queryPaths[i]);
NodeIterator nit = res.getNodes();
while (nit.hasNext()) {
Node tmpNode = nit.nextNode();
if (!tmpNode.hasProperty("tmpNode")) continue;
this.isNodeStale(tmpNode, this.duration_temp_storage, staleNodesList);
}
}
}
catch (RepositoryException e) {
this.log.error("Error while querying for temp storages", (Throwable)e);
}
}
private void getAnonymousTempNodes(Session session, List<Node> staleNodesList) {
try {
for (int i = 0; i < this.queryPaths.length; ++i) {
ResourceResolver resolver = this.resourceResolverFactory.getServiceResourceResolver(null);
String query = "SELECT * FROM nt:base WHERE guideComponentType='anonymousTempStorage' AND jcr:path LIKE '" + this.queryPaths[i] + "%'";
Iterator anonymousResources = resolver.findResources(query, "sql");
while (anonymousResources.hasNext()) {
Node tmpNode = (Node)((Resource)anonymousResources.next()).adaptTo(Node.class);
if (!tmpNode.hasProperty("guideComponentType") || !"anonymousTempStorage".equals(tmpNode.getProperty("guideComponentType").getString())) continue;
this.isNodeStale(tmpNode, this.duration_anonymous_storage, staleNodesList);
}
}
}
catch (RepositoryException e) {
this.log.error("Error while querying for temp storages", (Throwable)e);
}
catch (LoginException e) {
this.log.error("Cannot provide serviceResourceResolver", (Throwable)e);
}
}
private void isNodeStale(Node tmpNode, int duration, List<Node> staleNodesList) {
try {
Calendar date = null;
date = tmpNode.hasProperty("jcr:lastModified") ? tmpNode.getProperty("jcr:lastModified").getDate() : tmpNode.getProperty("jcr:created").getDate();
date.add(11, duration);
if (date.compareTo(this.todaysDate) <= 0) {
staleNodesList.add(tmpNode);
}
}
catch (RepositoryException e) {
this.log.error("Error while comparing dates for temp storages", (Throwable)e);
}
}
protected void bindSlingRepository(SlingRepository slingRepository) {
this.slingRepository = slingRepository;
}
protected void unbindSlingRepository(SlingRepository slingRepository) {
if (this.slingRepository == slingRepository) {
this.slingRepository = null;
}
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
}