1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.objectweb.fractal.cecilia.adl.implementations;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.URISyntaxException;
29 import java.net.URL;
30 import java.util.Enumeration;
31 import java.util.Map;
32
33 import org.objectweb.fractal.adl.ADLException;
34 import org.objectweb.fractal.adl.implementations.ImplementationCodeLoader;
35 import org.objectweb.fractal.adl.implementations.ImplementationErrors;
36 import org.objectweb.fractal.cecilia.adl.file.SourceFile;
37
38
39
40
41
42
43 public class CCodeLoader implements ImplementationCodeLoader {
44
45
46
47
48
49
50 private static final String[] FILE_EXTENSIONS = {".c", ".S", ".s"};
51
52 private static final boolean[] ASSEMBLY_FILE = {false, true, true};
53
54 static {
55 assert FILE_EXTENSIONS.length == ASSEMBLY_FILE.length;
56 }
57
58 public Object loadImplementation(final String signature,
59 final String language, final Map<Object, Object> context)
60 throws ADLException {
61 ClassLoader loader = null;
62 if (context != null) {
63 loader = (ClassLoader) context.get("classloader");
64 }
65 if (loader == null) {
66 loader = getClass().getClassLoader();
67 }
68
69
70 for (int i = 0; i < FILE_EXTENSIONS.length; i++) {
71 final String extension = FILE_EXTENSIONS[i];
72 String s = signature;
73 String path;
74 if (s.endsWith(extension)) {
75
76
77 path = s;
78
79 s = s.substring(0, s.length() - extension.length()).replace('/', '.');
80 } else {
81 path = s.replace('.', '/') + extension;
82 }
83
84
85
86 Enumeration<URL> results;
87 try {
88 results = loader.getResources(path);
89 } catch (final IOException e) {
90 continue;
91 }
92 while (results.hasMoreElements()) {
93 final URL res = results.nextElement();
94 File f;
95
96 if (!"file".equals(res.getProtocol())) continue;
97 try {
98 f = new File(res.toURI());
99 } catch(URISyntaxException e) {
100
101
102 f = new File(res.getPath());
103 }
104 if (f.exists()) return new SourceFile(s, f, ASSEMBLY_FILE[i]);
105 }
106 }
107 throw new ADLException(ImplementationErrors.IMPLEMENTATION_NOT_FOUND,
108 signature);
109 }
110 }