View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2008 STMicroelectronics
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   * Contact: fractal@objectweb.org
20   *
21   * Author: Matthieu Leclercq
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   * {@link ImplementationCodeLoader} implementation for C source code. The
40   * {@link #loadImplementation(String, String, Map) loadImplementation} method
41   * returns a {@link SourceFile} object of the corresponding file.
42   */
43  public class CCodeLoader implements ImplementationCodeLoader {
44  
45    /*
46     * Req #24905 Implementation code loader should be able to load ".h" file: in
47     * case of multi module the content.getClassName refers to a .h file
48     * containing the DECLARE_DATA
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      // Search the implementation file in the classpath
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          // the given signature ends with the file extension. Consider that it is
76          // a path.
77          path = s;
78          // convert s to correct signature
79          s = s.substring(0, s.length() - extension.length()).replace('/', '.');
80        } else {
81          path = s.replace('.', '/') + extension;
82        }
83  
84        // Search all the resources that match this path and return the first one
85        // that is a file (i.e. not a jar entry).
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           // This URL is not a valid URI.
101           // Fall back to new File(String), hoping that it will work properly...
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 }