LockAssetUtilsImpl.java
3.25 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl.importer;
import com.day.cq.dam.scene7.api.importer.LockAssetUtils;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
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.Service;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="%cq.dam.scene7.lockassetutils.name", description="%cq.dam.scene7.lockassetutils.description")
@Service
@Properties(value={@Property(name="cq.dam.scene7.import.locktimeout", intValue={180}, label="%cq.dam.scene7.import.locktimeout.label", description="%cq.dam.scene7.import.locktimeout.description")})
public class LockAssetUtilsImpl
implements LockAssetUtils {
private static final HashMap<String, Lock> assets = new HashMap();
private static final Logger LOG = LoggerFactory.getLogger(LockAssetUtilsImpl.class);
static final String SCR_PROP_NAME_LOCK_TIMEOUT = "cq.dam.scene7.import.locktimeout";
static final int SCR_PROP_DEFAULT_LOCK_TIMEOUT = 180;
private int timeout;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void lockAsset(String assetPath) throws Exception {
Lock lock = null;
HashMap<String, Lock> hashMap = assets;
synchronized (hashMap) {
lock = assets.get(assetPath);
if (lock == null) {
lock = new ReentrantLock();
assets.put(assetPath, lock);
}
}
if (!lock.tryLock(this.timeout, TimeUnit.SECONDS)) {
throw new Exception("Unable to obtain lock on " + assetPath + " after a timeout of " + this.timeout + " seconds");
}
assets.put(assetPath, lock);
LOG.debug("Obtained lock for " + assetPath);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void unlockAsset(String assetPath) {
HashMap<String, Lock> hashMap = assets;
synchronized (hashMap) {
Lock lock = assets.get(assetPath);
if (lock != null) {
assets.remove(assetPath);
lock.unlock();
LOG.debug("Removed lock for " + assetPath);
}
}
}
protected void activate(ComponentContext componentContext) {
Dictionary properties = componentContext.getProperties();
this.timeout = OsgiUtil.toInteger(properties.get("cq.dam.scene7.import.locktimeout"), (int)180);
if (this.timeout < 0) {
this.timeout = 180;
}
}
}