-
Notifications
You must be signed in to change notification settings - Fork 3
/
ClassLoaderQualifiedNameResolver.java
649 lines (560 loc) · 17.2 KB
/
ClassLoaderQualifiedNameResolver.java
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*******************************************************************************
* Copyright (c) 2020, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.runtime.impl.namespace;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.namespace.ILoader;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameLookupEngine;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.acceleo.query.runtime.namespace.ISourceLocation;
/**
* Resolve from a {@link ClassLoader}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class ClassLoaderQualifiedNameResolver implements IQualifiedNameResolver {
/**
* A slash.
*/
private static final String SLASH = "/";
/**
* A dot.
*/
private static final String DOT = ".";
/**
* The {@link ClassLoader}.
*/
private final ClassLoader classLoader;
/**
* The qualified name separator.
*/
private final String qualifierSeparator;
/**
* The {@link List} of {@link ILoader}.
*/
private final List<ILoader> loaders = new ArrayList<ILoader>();
/**
* Mapping from qualifiedName to its {@link Object}.
*/
private final Map<String, Object> qualifiedNameToObject = new HashMap<String, Object>();
/**
* Mapping from an {@link Object} to its qualified name.
*/
private final Map<Object, String> objectToQualifiedName = new HashMap<Object, String>();
/**
* Mapping from an {@link Object} to its {@link URI}.
*/
private final Map<Object, URI> objectToURI = new HashMap<Object, URI>();
/**
* Mapping from qualifiedName to its imports.
*/
private final Map<String, List<String>> qualifiedNameToImports = new HashMap<String, List<String>>();
/**
* Mapping from qualifiedName to qualified names that import it.
*/
private final Map<String, List<String>> qualifiedNameImportedBy = new HashMap<String, List<String>>();
/**
* Mapping from qualifiedName to its extend.
*/
private final Map<String, String> qualifiedNameToExtend = new HashMap<String, String>();
/**
* The mapping from services to its contextual qualified name (class, module, ... qualified name).
*/
private final Map<IService<?>, String> contextQualifiedNames = new HashMap<>();
/**
* Mapping from qualifiedName to qualified names that import it.
*/
private final Map<String, List<String>> qualifiedNameExtendedBy = new HashMap<String, List<String>>();
/**
* Constructor.
*
* @param classLoader
* the {@link ClassLoader}
* @param qualifierSeparator
* the qualifier name separator
*/
public ClassLoaderQualifiedNameResolver(ClassLoader classLoader, String qualifierSeparator) {
this.classLoader = classLoader;
this.qualifierSeparator = qualifierSeparator;
}
@Override
public String getQualifiedName(URI uri) {
String res = null;
String filePath;
try {
filePath = uri.toURL().getFile();
} catch (MalformedURLException e) {
filePath = null;
}
if (filePath != null) {
int pathEnd = filePath.lastIndexOf(DOT);
if (pathEnd < 0) {
pathEnd = filePath.length();
}
final String[] segments = filePath.substring(0, pathEnd).split(SLASH);
final StringBuilder moduleQualifiedNameBuilder = new StringBuilder();
for (int i = segments.length - 1; i >= 0; i--) {
moduleQualifiedNameBuilder.insert(0, segments[i]);
final String qualifiedName = moduleQualifiedNameBuilder.toString();
if (qualifiedNameURIMatch(qualifiedName, uri)) {
res = qualifiedName;
}
moduleQualifiedNameBuilder.insert(0, qualifierSeparator);
}
}
return res;
}
/**
* Tells if the given qualified name and {@link URI} correspond to each other.
*
* @param qualifiedName
* the qualified name
* @param uri
* the {@link URI}
* @return <code>true</code> if the given qualified name and {@link URI} correspond to each other,
* <code>false</code> otherwise
*/
private boolean qualifiedNameURIMatch(final String qualifiedName, URI uri) {
final boolean res;
final Object resolved = qualifiedNameToObject.get(qualifiedName);
if (resolved != null) {
// this case is needed when the resource has already been deleted but was previously resolved
res = uri.equals(getURI(resolved));
} else {
res = uri.equals(getURI(qualifiedName));
}
return res;
}
@Override
public URI getURI(String qualifiedName) {
URI res = null;
for (ILoader loader : loaders) {
try {
res = classLoader.getResource(loader.resourceName(qualifiedName)).toURI();
} catch (Exception e) {
res = null;
}
if (res != null) {
break;
}
}
return res;
}
@Override
public URI getSourceURI(String qualifiedName) {
return getURI(qualifiedName);
}
@Override
public ISourceLocation getSourceLocation(IService<?> service) {
ISourceLocation res = null;
for (ILoader loader : loaders) {
res = loader.getSourceLocation(this, service);
if (res != null) {
break;
}
}
return res;
}
@Override
public URI getBinaryURI(URI sourceURI) {
final URI res;
if (getQualifiedName(sourceURI) != null) {
res = sourceURI;
} else {
res = null;
}
return res;
}
@Override
public ISourceLocation getSourceLocation(String qualifiedName) {
ISourceLocation res = null;
for (ILoader loader : loaders) {
res = loader.getSourceLocation(this, qualifiedName);
if (res != null) {
break;
}
}
return res;
}
/**
* Loads the {@link Object} from the given qualified name.
*
* @param qualifiedName
* the qualified name
* @return the {@link Object} from the given qualified name if any, <code>null</code> otherwise
*/
protected Object load(String qualifiedName) {
Object res = null;
boolean registered = false;
for (ILoader loader : loaders) {
res = loader.load(this, qualifiedName);
if (res != null) {
register(loader, qualifiedName, res);
registered = true;
break;
}
}
// we perform a dummy registration to prevent further loading
if (!registered) {
dummyRegistration(qualifiedName);
}
return res;
}
/**
* Register nothing for the given qualified name to prevent further {@link #load(String) loading}.
*
* @param qualifiedName
* the qualified name
*/
protected void dummyRegistration(String qualifiedName) {
qualifiedNameToImports.put(qualifiedName, Collections.emptyList());
qualifiedNameToExtend.put(qualifiedName, null);
qualifiedNameToObject.put(qualifiedName, null);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver#register(java.lang.String,
* java.lang.Object)
*/
@Override
public void register(String qualifiedName, Object object) {
final ILoader loader = getLoaderFor(object);
if (loader != null) {
register(loader, qualifiedName, object);
}
}
/**
* Registers the given {@link Object} to the given qualified name using the given {@link ILoader}.
*
* @param loader
* the {@link ILoader}
* @param qualifiedName
* the qualified name
* @param object
* the {@link Object}
*/
private void register(ILoader loader, String qualifiedName, Object object) {
final List<String> imports = loader.getImports(object);
qualifiedNameToImports.put(qualifiedName, imports);
for (String imp : imports) {
qualifiedNameImportedBy.computeIfAbsent(imp, qn -> new ArrayList<>()).add(qualifiedName);
}
final String ext = loader.getExtends(object);
qualifiedNameToExtend.put(qualifiedName, ext);
qualifiedNameExtendedBy.computeIfAbsent(ext, qn -> new ArrayList<>()).add(qualifiedName);
final Object removedObject = qualifiedNameToObject.put(qualifiedName, object);
if (removedObject != null) {
objectToQualifiedName.remove(removedObject);
objectToURI.remove(removedObject);
}
objectToQualifiedName.put(object, qualifiedName);
try {
final URL resource = classLoader.getResource(loader.resourceName(qualifiedName));
// can be null for validation and completion
if (resource != null) {
objectToURI.put(object, resource.toURI());
}
} catch (URISyntaxException e) {
// we already loaded the object from this URI so it should never happen.
}
}
/**
* Gets the {@link ILoader} for the given {@link Object}.
*
* @param object
* the {@link Object}
* @return the {@link ILoader} for the given {@link Object} if any, <code>null</code> otherwise
*/
private ILoader getLoaderFor(Object object) {
ILoader res = null;
for (ILoader loader : loaders) {
if (loader.canHandle(object)) {
res = loader;
break;
}
}
return res;
}
@Override
public void clear(Set<String> qualifiedNames) {
for (String qualifiedName : qualifiedNames) {
final Object object = qualifiedNameToObject.remove(qualifiedName);
objectToQualifiedName.remove(object);
objectToURI.remove(object);
final List<String> imports = qualifiedNameToImports.remove(qualifiedName);
if (imports != null) {
for (String imported : imports) {
final List<String> importedBy = qualifiedNameImportedBy.get(imported);
if (importedBy != null) {
importedBy.remove(qualifiedName);
}
}
}
final String extended = qualifiedNameToExtend.remove(qualifiedName);
if (extended != null) {
final List<String> extendedBy = qualifiedNameExtendedBy.get(extended);
if (extendedBy != null) {
extendedBy.remove(qualifiedName);
}
}
}
}
@Override
public Object resolve(String qualifiedName) {
if (!qualifiedNameToObject.containsKey(qualifiedName)) {
load(qualifiedName);
}
return qualifiedNameToObject.get(qualifiedName);
}
@Override
public Set<IService<?>> getServices(IQualifiedNameLookupEngine lookupEngine, Object object,
String contextQualifiedName) {
final Set<IService<?>> res = new LinkedHashSet<IService<?>>();
final ILoader loader = getLoaderFor(object);
if (loader != null) {
res.addAll(loader.getServices(lookupEngine, object, contextQualifiedName));
for (IService<?> serivce : res) {
contextQualifiedNames.put(serivce, contextQualifiedName);
}
}
return res;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver#getContextQualifiedName(org.eclipse.acceleo.query.runtime.IService)
*/
@Override
public String getContextQualifiedName(IService<?> service) {
return contextQualifiedNames.get(service);
}
@Override
public String getExtend(String qualifiedName) {
if (!qualifiedNameToExtend.containsKey(qualifiedName)) {
load(qualifiedName);
}
return qualifiedNameToExtend.get(qualifiedName);
}
@Override
public List<String> getImports(String qualifiedName) {
if (!qualifiedNameToImports.containsKey(qualifiedName)) {
load(qualifiedName);
}
return qualifiedNameToImports.getOrDefault(qualifiedName, Collections.emptyList());
}
@Override
public Set<String> getDependOn(String qualifiedName) {
final Set<String> res = new LinkedHashSet<>();
final Set<String> extendedByClosure = new LinkedHashSet<>();
Set<String> added = new LinkedHashSet<>(qualifiedNameExtendedBy.getOrDefault(qualifiedName,
Collections.emptyList()));
while (!added.isEmpty()) {
Set<String> localAdded = new LinkedHashSet<>();
for (String addedQualifiedName : added) {
if (!extendedByClosure.contains(addedQualifiedName)) {
localAdded.addAll(qualifiedNameExtendedBy.getOrDefault(addedQualifiedName, Collections
.emptyList()));
}
}
extendedByClosure.addAll(added);
added = localAdded;
}
res.addAll(extendedByClosure);
res.addAll(qualifiedNameImportedBy.getOrDefault(qualifiedName, Collections.emptyList()));
for (String extendedBy : extendedByClosure) {
res.addAll(qualifiedNameImportedBy.getOrDefault(extendedBy, Collections.emptyList()));
}
return res;
}
@Override
public InputStream getInputStream(String resourceName) {
return classLoader.getResourceAsStream(resourceName);
}
@Override
public Class<?> getClass(String qualifiedName) {
Class<?> res;
try {
res = classLoader.loadClass(qualifiedName.replace(qualifierSeparator, DOT));
} catch (ClassNotFoundException e) {
res = null;
}
return res;
}
@Override
public Set<String> getAvailableQualifiedNames() {
final Set<String> res = new LinkedHashSet<String>();
try {
if (classLoader instanceof URLClassLoader) {
for (URL url : ((URLClassLoader)classLoader).getURLs()) {
res.addAll(getQualifiedNamesFromURL(url));
}
} else {
final Enumeration<URL> rootResources = classLoader.getResources("");
while (rootResources.hasMoreElements()) {
final URL url = rootResources.nextElement();
res.addAll(getQualifiedNamesFromURL(url));
}
}
} catch (IOException e1) {
// nothing to do here
}
return res;
}
@Override
public Set<String> getResolvedQualifiedNames() {
final LinkedHashSet<String> res = new LinkedHashSet<>();
for (Entry<String, Object> entry : qualifiedNameToObject.entrySet()) {
if (entry.getValue() != null) {
res.add(entry.getKey());
}
}
return res;
}
/**
* Gets the {@link Set} of qualified names for the given {@link URL}.
*
* @param url
* the {@link URL}
* @return the {@link Set} of qualified names for the given {@link URL}
*/
protected Set<String> getQualifiedNamesFromURL(URL url) {
final Set<String> res = new LinkedHashSet<String>();
// TODO jar://
if ("file".equals(url.getProtocol())) {
try {
final File file = new File(url.toURI());
if (file.isDirectory()) {
res.addAll(getQualifiedNameFromFolder(file, ""));
} else if (file.isFile()) {
res.addAll(getQualifiedNameFromJar(file));
} else {
// can't happen
}
} catch (URISyntaxException e) {
// nothing to do here
}
}
return res;
}
/**
* Gets the {@link List} of qualified names in the given jar {@link File}.
*
* @param file
* the jar {@link File}
* @return the {@link List} of qualified names in the given jar {@link File}
*/
protected Set<String> getQualifiedNameFromJar(File file) {
final Set<String> res = new LinkedHashSet<String>();
try (ZipFile jarFile = new ZipFile(file);) {
final Enumeration<? extends ZipEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
for (ILoader loader : loaders) {
final String qualifiedName = loader.qualifiedName(entry.getName());
if (qualifiedName != null) {
res.add(qualifiedName);
break;
}
}
}
} catch (ZipException e) {
// nothing to do here
} catch (IOException e) {
// nothing to do here
}
return res;
}
/**
* Gets the {@link List} of qualified names inside the given folder and the given name space starting
* point.
*
* @param folder
* the folder
* @param nameSpace
* the name space
* @return the {@link List} of qualified names inside the given folder and the given name space starting
* point
*/
protected Set<String> getQualifiedNameFromFolder(File folder, String nameSpace) {
final Set<String> res = new LinkedHashSet<String>();
if (folder.exists() && folder.canRead()) {
for (File child : folder.listFiles()) {
if (child.isDirectory()) {
res.addAll(getQualifiedNameFromFolder(child, nameSpace + child.getName()
+ qualifierSeparator));
} else if (child.isFile()) {
for (ILoader loader : loaders) {
final String lastSegment = loader.qualifiedName(child.getName());
if (lastSegment != null) {
res.add(nameSpace + lastSegment);
break;
}
}
}
}
}
return res;
}
/**
* Gets the {@link List} of possible source resource names for the given qualified name.
*
* @param qualifiedName
* the qualified name
* @return the {@link List} of possible source resource names for the given qualified name
*/
protected List<String> getPossibleResourceNames(String qualifiedName) {
final List<String> res = new ArrayList<String>();
for (ILoader loader : loaders) {
res.add(loader.sourceResourceName(qualifiedName));
}
return res;
}
@Override
public String getQualifiedName(Object object) {
return objectToQualifiedName.get(object);
}
@Override
public URI getURI(Object object) {
return objectToURI.get(object);
}
@Override
public void addLoader(ILoader loader) {
loaders.add(loader);
}
@Override
public void removeLoader(ILoader loader) {
loaders.remove(loader);
}
@Override
public void clearLoaders() {
loaders.clear();
}
}