MaintenanceTaskManagerImpl.java
6.47 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.Service
* org.apache.sling.event.jobs.Job
* org.apache.sling.event.jobs.JobManager
* org.osgi.framework.BundleContext
* org.osgi.framework.Filter
* org.osgi.framework.InvalidSyntaxException
* org.osgi.framework.ServiceReference
* org.osgi.util.tracker.ServiceTracker
* org.osgi.util.tracker.ServiceTrackerCustomizer
*/
package com.adobe.granite.maintenance.impl;
import com.adobe.granite.maintenance.MaintenanceTaskInfo;
import com.adobe.granite.maintenance.MaintenanceTaskManager;
import com.adobe.granite.maintenance.impl.MaintenanceTaskInfoImpl;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
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.Service;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
@Component
@Service(value={MaintenanceTaskManager.class})
public class MaintenanceTaskManagerImpl
implements MaintenanceTaskManager {
private ServiceTracker taskTracker;
private final Map<Long, MaintenanceTaskInfoImpl> infos = new HashMap<Long, MaintenanceTaskInfoImpl>();
private final Map<String, MaintenanceTaskInfo> infoCache = new HashMap<String, MaintenanceTaskInfo>();
@Reference
private JobManager jobManager;
@Activate
protected void activate(BundleContext btx) throws InvalidSyntaxException {
this.taskTracker = new ServiceTracker(btx, this.createFilter(btx), new ServiceTrackerCustomizer(){
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void removedService(ServiceReference reference, Object service) {
Long serviceId = (Long)reference.getProperty("service.id");
Map map = MaintenanceTaskManagerImpl.this.infos;
synchronized (map) {
if (MaintenanceTaskManagerImpl.this.infos.remove(serviceId) != null) {
MaintenanceTaskManagerImpl.this.rebuildCache();
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void modifiedService(ServiceReference reference, Object service) {
Map map = MaintenanceTaskManagerImpl.this.infos;
synchronized (map) {
Long serviceId = (Long)reference.getProperty("service.id");
MaintenanceTaskManagerImpl.this.infos.remove(serviceId);
this.addingService(reference);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Object addingService(ServiceReference reference) {
var2_2 = this;
synchronized (var2_2) {
Long serviceId = (Long)reference.getProperty("service.id");
MaintenanceTaskInfoImpl info = new MaintenanceTaskInfoImpl(MaintenanceTaskManagerImpl.this.jobManager, reference);
MaintenanceTaskManagerImpl.this.infos.put(serviceId, info);
MaintenanceTaskManagerImpl.this.rebuildCache();
return info;
}
}
});
this.taskTracker.open();
}
@Deactivate
protected void deactivate() {
if (this.taskTracker != null) {
this.taskTracker.close();
this.taskTracker = null;
}
}
private Filter createFilter(BundleContext btx) throws InvalidSyntaxException {
return btx.createFilter(String.format("(&(%s=*)(%s=*)(%s=*))", "granite.maintenance.name", "granite.maintenance.title", "job.topics"));
}
private void rebuildCache() {
this.infoCache.clear();
for (MaintenanceTaskInfoImpl info : this.infos.values()) {
String name = info.getName();
MaintenanceTaskInfoImpl other = (MaintenanceTaskInfoImpl)this.infoCache.get(name);
if (other != null && other.getServiceReference().compareTo((Object)info.getServiceReference()) >= 0) continue;
this.infoCache.put(name, info);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public Collection<MaintenanceTaskInfo> getMaintenanceTaskInfos() {
Map<Long, MaintenanceTaskInfoImpl> map = this.infos;
synchronized (map) {
return new HashSet<MaintenanceTaskInfo>(this.infoCache.values());
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public MaintenanceTaskInfo getMaintenanceTaskInfo(String name) {
Map<Long, MaintenanceTaskInfoImpl> map = this.infos;
synchronized (map) {
return this.infoCache.get(name);
}
}
@Override
public void startMaintenanceTask(String name) {
MaintenanceTaskInfo info = this.getMaintenanceTaskInfo(name);
if (info != null) {
this.jobManager.addJob(info.getTaskTopic(), null);
}
}
@Override
public void stopMaintenanceTask(String name) {
Job job;
MaintenanceTaskInfo info = this.getMaintenanceTaskInfo(name);
if (info != null && (job = this.jobManager.getJob(info.getTaskTopic(), null)) != null) {
this.jobManager.stopJobById(job.getId());
if (job.getFinishedDate() == null) {
this.jobManager.removeJobById(job.getId());
}
}
}
protected void bindJobManager(JobManager jobManager) {
this.jobManager = jobManager;
}
protected void unbindJobManager(JobManager jobManager) {
if (this.jobManager == jobManager) {
this.jobManager = null;
}
}
}