AssetVersionManagerImpl.java
6.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.asset.api.Asset
* com.adobe.granite.asset.api.AssetException
* com.adobe.granite.asset.api.AssetVersion
* com.adobe.granite.asset.api.AssetVersionManager
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.version.Version
* javax.jcr.version.VersionHistory
* javax.jcr.version.VersionIterator
* javax.jcr.version.VersionManager
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.asset.core.impl;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetException;
import com.adobe.granite.asset.api.AssetVersion;
import com.adobe.granite.asset.api.AssetVersionManager;
import com.adobe.granite.asset.core.impl.AssetVersionImpl;
import java.util.ArrayList;
import java.util.Iterator;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
import javax.jcr.version.VersionManager;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class AssetVersionManagerImpl
implements AssetVersionManager {
private static final Logger log = LoggerFactory.getLogger(AssetVersionManagerImpl.class);
private final ResourceResolver resolver;
private final Session session;
public AssetVersionManagerImpl(ResourceResolver resolver) {
this.resolver = resolver;
this.session = (Session)resolver.adaptTo(Session.class);
}
public AssetVersion createVersion(String assetPath, String label) {
Node node = (Node)this.resolver.getResource(assetPath).adaptTo(Node.class);
if (null != node) {
try {
if (this.session.hasPendingChanges()) {
throw new AssetException("Version cannot be created - current session has pending changes");
}
if (node.isLocked()) {
throw new AssetException("Asset at [ " + assetPath + " ] is locked, version can't be created.");
}
if (node.canAddMixin("mix:versionable")) {
node.addMixin("mix:versionable");
this.session.save();
}
VersionManager versionManager = this.session.getWorkspace().getVersionManager();
Version version = versionManager.checkin(node.getPath());
versionManager.checkout(node.getPath());
if (StringUtils.isEmpty((String)label)) {
label = version.getName();
}
this.addVersionLabel(version, label);
return new AssetVersionImpl(version, this.resolver);
}
catch (RepositoryException e) {
try {
if (this.session.hasPendingChanges()) {
this.session.refresh(false);
}
}
catch (RepositoryException re) {
log.error("unable to revert changes after createVersion() failure for asset [{}]: ", (Object)assetPath, (Object)re);
}
throw new AssetException("Unable to create version for asset [ " + assetPath + " ]: ", (Throwable)e);
}
}
throw new AssetException("Unable to create version for asset. [ " + assetPath + " ] is not an Asset");
}
public Asset restore(String versionId) {
try {
Version version = (Version)this.session.getNodeByIdentifier(versionId);
String currentNodePath = this.session.getNodeByIdentifier(version.getContainingHistory().getVersionableIdentifier()).getPath();
Node node = (Node)this.session.getItem(currentNodePath);
VersionManager versionManager = node.getSession().getWorkspace().getVersionManager();
versionManager.restore(version, true);
versionManager.checkout(node.getPath());
return (Asset)this.resolver.getResource(node.getPath()).adaptTo(Asset.class);
}
catch (RepositoryException e) {
throw new AssetException("Failed to restore Asset version with ID [ " + versionId + " ]", (Throwable)e);
}
}
public AssetVersion getVersion(String versionId) {
try {
Version version = (Version)this.session.getNodeByIdentifier(versionId);
if (version == null) {
return null;
}
return new AssetVersionImpl(version, this.resolver);
}
catch (RepositoryException e) {
throw new AssetException("Failed to lookup Asset version with ID [ " + versionId + " ]", (Throwable)e);
}
}
public Iterator<? extends AssetVersion> listVersions(String path) {
ArrayList<AssetVersionImpl> versions = new ArrayList<AssetVersionImpl>();
try {
Node node = (Node)this.session.getItem(path);
if (node.isNodeType("mix:versionable")) {
VersionHistory history = this.session.getWorkspace().getVersionManager().getVersionHistory(node.getPath());
Version root = history.getRootVersion();
VersionIterator iter = history.getAllVersions();
while (iter.hasNext()) {
Version version = iter.nextVersion();
AssetVersionImpl r = new AssetVersionImpl(version, this.resolver);
if (version.isSame((Item)root)) continue;
versions.add(r);
}
}
}
catch (RepositoryException e) {
throw new AssetException("Failed to retrieve versions at Path [ " + path + " ]", (Throwable)e);
}
return versions.iterator();
}
private void addVersionLabel(Version version, String label) {
try {
version.getContainingHistory().addVersionLabel(version.getName(), label, true);
}
catch (RepositoryException e) {
log.warn("Failed to add version label", (Throwable)e);
}
}
}