FileBundle.java
16.8 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
* org.apache.commons.io.IOUtils
* org.apache.jackrabbit.commons.JcrUtils
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.ResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.ui.clientlibs.impl;
import com.adobe.granite.ui.clientlibs.impl.CompilerContextImpl;
import com.adobe.granite.ui.clientlibs.impl.CompilerProvider;
import com.adobe.granite.ui.clientlibs.impl.HtmlLibraryManagerImpl;
import com.adobe.granite.ui.clientlibs.impl.JcrPropertyResource;
import com.adobe.granite.ui.clientlibs.impl.JcrResourceProvider;
import com.adobe.granite.ui.clientlibs.script.CompilerContext;
import com.adobe.granite.ui.clientlibs.script.ScriptCompiler;
import com.adobe.granite.ui.clientlibs.script.ScriptResource;
import com.adobe.granite.ui.clientlibs.script.ScriptResourceProvider;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileBundle {
private static final String PN_DEPENDENCIES = "dependencies";
private static final String GENERATED_PATH = "/generated/";
private static final Logger log = LoggerFactory.getLogger(FileBundle.class);
private final CompiledScript files;
private final String rootPath;
private final String filePrefix;
private boolean dirty;
public FileBundle(Node root, String includeFileName, String filePrefix, CompilerProvider compilerProvider) throws RepositoryException {
this.filePrefix = filePrefix == null ? "" : filePrefix;
this.rootPath = root.getPath();
this.files = new CompiledScript(this.rootPath, 0, "");
try {
if (root.hasNode(includeFileName)) {
this.scanIncludeList(root, includeFileName, compilerProvider);
} else {
this.scanFiles(root, compilerProvider);
}
}
catch (Exception e) {
log.error("Error occurred while scanning files for: " + this.files.getPath(), (Throwable)e);
}
log.debug("getFiles(): {} files found", (Object)this.files.getScripts().size());
}
public Set<String> getFiles() {
return this.files.getScripts().keySet();
}
public void addSourcePaths(Set<String> sourcePaths) {
this.files.dumpDependencies(sourcePaths);
}
public boolean isDirty() {
return this.dirty;
}
public void setDirty(boolean dirty) {
this.dirty = dirty;
}
public String getRootPath() {
return this.rootPath;
}
public long getLastModified() {
return this.files.getLastModified();
}
public void addResources(Session session, CompilerProvider provider, List<ScriptResource> resources) throws RepositoryException {
try {
this.files.refresh(session, provider);
try {
this.files.addScriptsAsResources(session, resources);
}
catch (RepositoryException e) {
this.files.invalidate();
this.files.refresh(session, provider);
this.files.addScriptsAsResources(session, resources);
}
}
catch (IOException e) {
throw new RepositoryException((Throwable)e);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void scanIncludeList(Node root, String fileName, CompilerProvider compilerProvider) throws RepositoryException, IOException {
Node includeFile = root.getNode(fileName);
Script includeScript = FileBundle.createScript(includeFile, null);
this.files.addScript(includeScript, true);
BufferedReader reader = new BufferedReader(includeScript.getResource(root.getSession()).getReader());
try {
String line;
String basePath = this.rootPath + this.filePrefix;
while ((line = reader.readLine()) != null) {
Node node;
String path = line.trim();
if (path.length() == 0 || path.startsWith("#")) {
if (!path.startsWith("#base=") || (basePath = path.substring(6)).startsWith("/")) continue;
basePath = this.rootPath + this.filePrefix + "/" + basePath;
continue;
}
if (!path.startsWith("/")) {
path = basePath + "/" + path;
}
path = ResourceUtil.normalize((String)path);
if (!root.getSession().itemExists(path)) {
log.warn("Referenced path in {} does not exist: {}", (Object)includeScript.getPath(), (Object)path);
continue;
}
if (path.startsWith("/libs/")) {
String altPath = "/apps/" + path.substring(6);
if (root.getSession().nodeExists(altPath)) {
log.info("Library file {} overlayed by {}.", (Object)path, (Object)altPath);
path = altPath;
}
}
if (!(node = root.getSession().getNode(path)).hasProperty("jcr:content/jcr:data")) {
log.warn("Referenced node has no data: {}", (Object)path);
continue;
}
this.files.addScript(FileBundle.createScript(node, compilerProvider), false);
}
}
finally {
IOUtils.closeQuietly((Reader)reader);
}
}
private static Script createScript(Node node, CompilerProvider compilerProvider) throws RepositoryException {
String path = node.getPath();
long lastMod = FileBundle.getLastModified(node);
Script s = new FileScript(path, lastMod);
if (compilerProvider == null) {
return s;
}
String extension = Text.getName((String)path, (char)'.');
ScriptCompiler compiler = compilerProvider.findCompiler(extension);
if (compiler != null) {
String name = Text.getName((String)path);
String generatedPath = HtmlLibraryManagerImpl.WRITE_LOCATION + Text.getRelativeParent((String)path, (int)1) + "/generated/" + name.substring(0, name.lastIndexOf(46)) + "." + compiler.getOutputExtension();
Node generatedNode = null;
long generatedLastMod = 0;
Session session = node.getSession();
if (session.nodeExists(generatedPath)) {
generatedNode = session.getNode(generatedPath);
generatedLastMod = FileBundle.getLastModified(generatedNode);
}
CompiledScript cs = new CompiledScript(generatedPath, generatedLastMod, compiler.getName());
cs.addScript(s, false);
if (generatedNode != null && generatedNode.hasProperty("{http://www.jcp.org/jcr/1.0}content/dependencies")) {
for (Value v : generatedNode.getProperty("{http://www.jcp.org/jcr/1.0}content/dependencies").getValues()) {
Node depNode;
String depPath = v.getString();
if (!session.nodeExists(depPath) || !(depNode = session.getNode(depPath)).hasProperty("jcr:content/jcr:data")) continue;
long depLastMod = FileBundle.getLastModified(depNode);
FileScript depScript = new FileScript(depPath, depLastMod);
cs.addScript(depScript, true);
}
}
s = cs;
} else if (!"js".equals(extension) && !"css".equals(extension)) {
log.warn("No script compiler found for {}.", (Object)path);
s = new StaleScript(s);
}
return s;
}
private static long getLastModified(Node fileNode) {
try {
if (fileNode.hasProperty("jcr:content/jcr:lastModified")) {
return fileNode.getProperty("jcr:content/jcr:lastModified").getLong();
}
}
catch (RepositoryException e) {
log.error("Error while updating last modified time for file: {}", (Object)fileNode);
}
return 0;
}
private void scanFiles(Node parent, CompilerProvider compilerProvider) throws RepositoryException {
NodeIterator iter = parent.getNodes();
while (iter.hasNext()) {
Node node = iter.nextNode();
if (node.isNodeType("nt:file")) {
this.files.addScript(FileBundle.createScript(node, compilerProvider), false);
continue;
}
if (!node.isNodeType("nt:folder")) continue;
this.scanFiles(node, compilerProvider);
}
}
private static class StaleScript
implements Script {
private Script base;
public StaleScript(Script base) {
this.base = base;
}
@Override
public String getPath() {
return this.base.getPath();
}
@Override
public long getLastModified() {
return this.base.getLastModified();
}
@Override
public ScriptResource getResource(Session session) throws RepositoryException {
return this.base.getResource(session);
}
@Override
public void refresh(Session s, CompilerProvider compiler) throws RepositoryException, IOException {
Script script;
Node node;
if (this.base instanceof FileScript && (script = FileBundle.createScript(node = s.getNode(this.getPath()), compiler)) instanceof CompiledScript) {
log.info("Refresh resolved stale script at {}", (Object)this.getPath());
this.base = script;
}
this.base.refresh(s, compiler);
}
@Override
public void dumpDependencies(Set<String> deps) {
this.base.dumpDependencies(deps);
}
@Override
public void invalidate() {
this.base.invalidate();
}
}
private static class CompiledScript
extends FileScript {
private final String compilerName;
private long compiledLastModified;
private final LinkedHashMap<String, Script> files = new LinkedHashMap();
private Collection<String> dependencies = new HashSet<String>();
private CompiledScript(String path, long lastModified, String compiler) {
super(path, 0);
this.compiledLastModified = lastModified;
this.compilerName = compiler;
}
public void addScript(Script s, boolean transitive) {
this.touch(s.getLastModified());
if (transitive) {
this.dependencies.add(s.getPath());
} else {
this.files.put(s.getPath(), s);
}
}
public void touch(long lastMod) {
if (lastMod > this.lastModified) {
this.lastModified = lastMod;
}
}
public Map<String, Script> getScripts() {
return this.files;
}
public void addScriptsAsResources(Session session, List<ScriptResource> resources) throws RepositoryException {
for (Script s : this.files.values()) {
resources.add(s.getResource(session));
}
}
@Override
public void refresh(Session session, CompilerProvider compilerProvider) throws RepositoryException, IOException {
if (this.lastModified > this.compiledLastModified) {
for (Script s : this.files.values()) {
s.refresh(session, compilerProvider);
this.touch(s.getLastModified());
}
ScriptCompiler compiler = compilerProvider.getCompiler(this.compilerName);
if (compiler != null) {
Node content;
if (session.nodeExists(this.path)) {
content = session.getNode(this.path + "/" + "jcr:content");
} else {
Node baseNode = session.getNode(Text.getAbsoluteParent((String)this.path, (int)1));
String relativeFilePath = this.path.substring(baseNode.getPath().length() + 1);
Node fileNode = JcrUtils.getOrCreateByPath((Node)baseNode, (String)relativeFilePath, (boolean)false, (String)"{http://www.jcp.org/jcr/nt/1.0}folder", (String)"{http://www.jcp.org/jcr/nt/1.0}file", (boolean)false);
content = fileNode.addNode("jcr:content", "{http://www.jcp.org/jcr/nt/1.0}unstructured");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter w = new OutputStreamWriter((OutputStream)out, "utf-8");
LinkedList<ScriptResource> resources = new LinkedList<ScriptResource>();
this.addScriptsAsResources(session, resources);
CompilerContextImpl ctx = new CompilerContextImpl(new JcrResourceProvider(session), this.path);
compiler.compile(resources, w, ctx);
w.close();
Binary bin = session.getValueFactory().createBinary((InputStream)new ByteArrayInputStream(out.toByteArray()));
content.setProperty("jcr:data", bin);
bin.dispose();
content.setProperty("jcr:lastModified", this.lastModified);
content.setProperty("jcr:mimeType", compiler.getMimeType());
this.dependencies = ctx.getDependencies();
this.dependencies.add(Text.getRelativeParent((String)this.path, (int)1));
try {
content.setProperty("dependencies", this.dependencies.toArray(new String[this.dependencies.size()]));
}
catch (RepositoryException e) {
log.warn("Unable to update dependency list.", (Throwable)e);
}
}
this.compiledLastModified = this.lastModified;
}
}
@Override
public void invalidate() {
this.compiledLastModified = 0;
for (Script s : this.files.values()) {
s.invalidate();
}
}
@Override
public void dumpDependencies(Set<String> deps) {
super.dumpDependencies(deps);
for (Script s : this.files.values()) {
s.dumpDependencies(deps);
}
deps.addAll(this.dependencies);
}
}
private static class FileScript
implements Script {
protected final String path;
protected long lastModified;
private FileScript(String path, long lastModified) {
this.path = path;
this.lastModified = lastModified;
}
@Override
public String getPath() {
return this.path;
}
@Override
public long getLastModified() {
return this.lastModified;
}
@Override
public ScriptResource getResource(Session session) throws RepositoryException {
Property p = session.getProperty(this.path + "/jcr:content/jcr:data");
return new JcrPropertyResource(p, p.getParent().getParent().getPath());
}
@Override
public void refresh(Session s, CompilerProvider compiler) throws RepositoryException, IOException {
}
@Override
public void invalidate() {
}
@Override
public void dumpDependencies(Set<String> deps) {
deps.add(this.path);
}
}
private static interface Script {
public String getPath();
public long getLastModified();
public ScriptResource getResource(Session var1) throws RepositoryException;
public void refresh(Session var1, CompilerProvider var2) throws RepositoryException, IOException;
public void dumpDependencies(Set<String> var1);
public void invalidate();
}
}