dew/src/main/java/org/apidesign/bck2brwsr/dew/ClassLoaderFileManager.java
author tzezula
Wed, 02 Oct 2013 21:00:24 +0200
changeset 1324 263482b074e9
child 1325 f7f25ea1bbec
permissions -rw-r--r--
ClassLoaderFileManager
tzezula@1324
     1
/*
tzezula@1324
     2
 * To change this license header, choose License Headers in Project Properties.
tzezula@1324
     3
 * To change this template file, choose Tools | Templates
tzezula@1324
     4
 * and open the template in the editor.
tzezula@1324
     5
 */
tzezula@1324
     6
tzezula@1324
     7
package org.apidesign.bck2brwsr.dew;
tzezula@1324
     8
tzezula@1324
     9
import java.io.BufferedReader;
tzezula@1324
    10
import java.io.File;
tzezula@1324
    11
import java.io.IOException;
tzezula@1324
    12
import java.io.InputStreamReader;
tzezula@1324
    13
import java.util.ArrayList;
tzezula@1324
    14
import java.util.Collections;
tzezula@1324
    15
import java.util.EnumSet;
tzezula@1324
    16
import java.util.Enumeration;
tzezula@1324
    17
import java.util.HashMap;
tzezula@1324
    18
import java.util.Iterator;
tzezula@1324
    19
import java.util.List;
tzezula@1324
    20
import java.util.Map;
tzezula@1324
    21
import java.util.Set;
tzezula@1324
    22
import java.util.zip.ZipEntry;
tzezula@1324
    23
import java.util.zip.ZipFile;
tzezula@1324
    24
import javax.tools.FileObject;
tzezula@1324
    25
import javax.tools.JavaFileManager;
tzezula@1324
    26
import javax.tools.JavaFileObject;
tzezula@1324
    27
import javax.tools.StandardLocation;
tzezula@1324
    28
tzezula@1324
    29
/**
tzezula@1324
    30
 *
tzezula@1324
    31
 * @author Tomas Zezula
tzezula@1324
    32
 */
tzezula@1324
    33
public class ClassLoaderFileManager implements JavaFileManager {
tzezula@1324
    34
tzezula@1324
    35
    private static final Location[] READ_LOCATIONS = {
tzezula@1324
    36
        StandardLocation.PLATFORM_CLASS_PATH,
tzezula@1324
    37
        StandardLocation.CLASS_PATH,
tzezula@1324
    38
        StandardLocation.SOURCE_PATH
tzezula@1324
    39
    };
tzezula@1324
    40
tzezula@1324
    41
    private static final Location[] WRITE_LOCATIONS = {
tzezula@1324
    42
        StandardLocation.CLASS_OUTPUT,
tzezula@1324
    43
        StandardLocation.SOURCE_OUTPUT
tzezula@1324
    44
    };
tzezula@1324
    45
tzezula@1324
    46
    private static final Location[] CLASS_LOADER_LOCATIONS = {
tzezula@1324
    47
        StandardLocation.ANNOTATION_PROCESSOR_PATH
tzezula@1324
    48
    };
tzezula@1324
    49
tzezula@1324
    50
    private Map<Location, Map<String,List<MemoryFileObject>>> generated;
tzezula@1324
    51
tzezula@1324
    52
tzezula@1324
    53
    ClassLoaderFileManager() {
tzezula@1324
    54
        generated = new HashMap<>();
tzezula@1324
    55
        for (Location l : WRITE_LOCATIONS) {
tzezula@1324
    56
            generated.put(l, new HashMap<String, List<MemoryFileObject>>());
tzezula@1324
    57
        }
tzezula@1324
    58
    }
tzezula@1324
    59
tzezula@1324
    60
tzezula@1324
    61
    @Override
tzezula@1324
    62
    public ClassLoader getClassLoader(Location location) {
tzezula@1324
    63
        if (canClassLoad(location)) {
tzezula@1324
    64
            return getClass().getClassLoader();
tzezula@1324
    65
        } else {
tzezula@1324
    66
            return null;
tzezula@1324
    67
        }
tzezula@1324
    68
    }
tzezula@1324
    69
tzezula@1324
    70
    @Override
tzezula@1324
    71
    public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
tzezula@1324
    72
        if (canRead(location)) {
tzezula@1324
    73
            final List<JavaFileObject> res = new ArrayList<JavaFileObject>();
tzezula@1324
    74
            for (String resource : getResources(convertFQNToResource(packageName))) {
tzezula@1324
    75
                final JavaFileObject jfo = new ClassLoaderJavaFileObject(resource);
tzezula@1324
    76
                if (kinds.contains(jfo.getKind())) {
tzezula@1324
    77
                    res.add(jfo);
tzezula@1324
    78
                }
tzezula@1324
    79
            }
tzezula@1324
    80
            return res;
tzezula@1324
    81
        } else if (canWrite(location)) {
tzezula@1324
    82
            Map<String,List<MemoryFileObject>> folders = generated.get(location);
tzezula@1324
    83
            List<MemoryFileObject> files = folders.get(convertFQNToResource(packageName));
tzezula@1324
    84
            if (files != null) {
tzezula@1324
    85
                final List<JavaFileObject> res = new ArrayList<JavaFileObject>();
tzezula@1324
    86
                for (JavaFileObject file : files) {
tzezula@1324
    87
                    if (kinds.contains(file.getKind()) && file.getLastModified() >= 0) {
tzezula@1324
    88
                        res.add(file);
tzezula@1324
    89
                    }
tzezula@1324
    90
                }
tzezula@1324
    91
                return res;
tzezula@1324
    92
            }
tzezula@1324
    93
        }
tzezula@1324
    94
        return Collections.<JavaFileObject>emptyList();
tzezula@1324
    95
    }
tzezula@1324
    96
tzezula@1324
    97
    @Override
tzezula@1324
    98
    public String inferBinaryName(Location location, JavaFileObject file) {
tzezula@1324
    99
        return ((InferableJavaFileObject)file).infer();
tzezula@1324
   100
    }
tzezula@1324
   101
tzezula@1324
   102
    @Override
tzezula@1324
   103
    public boolean isSameFile(FileObject a, FileObject b) {
tzezula@1324
   104
        return a.toUri().equals(b.toUri());
tzezula@1324
   105
    }
tzezula@1324
   106
tzezula@1324
   107
    @Override
tzezula@1324
   108
    public boolean handleOption(String current, Iterator<String> remaining) {
tzezula@1324
   109
        return false;
tzezula@1324
   110
    }
tzezula@1324
   111
tzezula@1324
   112
    @Override
tzezula@1324
   113
    public boolean hasLocation(Location location) {
tzezula@1324
   114
        for (Location l : StandardLocation.values()) {
tzezula@1324
   115
            if (l.equals(location)) {
tzezula@1324
   116
                return true;
tzezula@1324
   117
            }
tzezula@1324
   118
        }
tzezula@1324
   119
        return false;
tzezula@1324
   120
    }
tzezula@1324
   121
tzezula@1324
   122
    @Override
tzezula@1324
   123
    public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException {
tzezula@1324
   124
        if (canRead(location)) {
tzezula@1324
   125
            return new ClassLoaderJavaFileObject(convertFQNToResource(className) + kind.extension);
tzezula@1324
   126
        } else {
tzezula@1324
   127
            throw new UnsupportedOperationException("Unsupported location for reading: " + location);   //NOI18N
tzezula@1324
   128
        }
tzezula@1324
   129
    }
tzezula@1324
   130
tzezula@1324
   131
    @Override
tzezula@1324
   132
    public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
tzezula@1324
   133
        if (canWrite(location)) {
tzezula@1324
   134
            final String resource = convertFQNToResource(className) + kind.extension;
tzezula@1324
   135
            final MemoryFileObject res = new MemoryFileObject(resource, null);
tzezula@1324
   136
            register(location, resource, res);
tzezula@1324
   137
            return res;
tzezula@1324
   138
        } else {
tzezula@1324
   139
            throw new UnsupportedOperationException("Unsupported location for reading: " + location);   //NOI18N
tzezula@1324
   140
        }
tzezula@1324
   141
    }
tzezula@1324
   142
tzezula@1324
   143
    @Override
tzezula@1324
   144
    public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
tzezula@1324
   145
        if (canRead(location)) {
tzezula@1324
   146
            return new ClassLoaderJavaFileObject(convertFQNToResource(packageName) + '/' + relativeName); //NOI18N
tzezula@1324
   147
        } else {
tzezula@1324
   148
            throw new UnsupportedOperationException("Unsupported location for reading: " + location);   //NOI18N
tzezula@1324
   149
        }
tzezula@1324
   150
    }
tzezula@1324
   151
tzezula@1324
   152
    @Override
tzezula@1324
   153
    public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
tzezula@1324
   154
        if (canWrite(location)) {
tzezula@1324
   155
            final String resource = convertFQNToResource(packageName) + '/' + relativeName; //NOI18N
tzezula@1324
   156
            final MemoryFileObject res = new MemoryFileObject(resource, null);
tzezula@1324
   157
            register(location, resource, res);
tzezula@1324
   158
            return res;
tzezula@1324
   159
        } else {
tzezula@1324
   160
            throw new UnsupportedOperationException("Unsupported location for reading: " + location);   //NOI18N
tzezula@1324
   161
        }
tzezula@1324
   162
    }
tzezula@1324
   163
tzezula@1324
   164
    @Override
tzezula@1324
   165
    public void flush() throws IOException {
tzezula@1324
   166
    }
tzezula@1324
   167
tzezula@1324
   168
    @Override
tzezula@1324
   169
    public void close() throws IOException {        
tzezula@1324
   170
    }
tzezula@1324
   171
tzezula@1324
   172
    @Override
tzezula@1324
   173
    public int isSupportedOption(String option) {
tzezula@1324
   174
        return -1;
tzezula@1324
   175
    }
tzezula@1324
   176
tzezula@1324
   177
//    private List<String> getResources(String folder) throws IOException {
tzezula@1324
   178
//        final List<String> result = new ArrayList<String>();
tzezula@1324
   179
//        final BufferedReader in = new BufferedReader(new InputStreamReader(
tzezula@1324
   180
//                this.getClass().getClassLoader().getResourceAsStream(String.format("%s/pkg-list", folder.substring(0))),    //NOI18N
tzezula@1324
   181
//                "UTF-8"));  //NOI18N
tzezula@1324
   182
//        try {
tzezula@1324
   183
//            String line;
tzezula@1324
   184
//            while ((line = in.readLine()) != null) {
tzezula@1324
   185
//                result.add(line);
tzezula@1324
   186
//            }
tzezula@1324
   187
//        } finally {
tzezula@1324
   188
//            in.close();
tzezula@1324
   189
//        }
tzezula@1324
   190
//        return result;
tzezula@1324
   191
//    }
tzezula@1324
   192
tzezula@1324
   193
    //MOCK IMPL
tzezula@1324
   194
    private List<String> getResources(String folder) throws IOException {
tzezula@1324
   195
        if (classPathContent == null) {
tzezula@1324
   196
            classPathContent = new HashMap<>();
tzezula@1324
   197
//            final String boot = System.getProperty("sun.boot.class.path");  //NOI18N
tzezula@1324
   198
            final String cp = System.getProperty("java.class.path");
tzezula@1324
   199
            for (String entry : cp.split(File.pathSeparator)) {
tzezula@1324
   200
                File f = new File (entry);
tzezula@1324
   201
                if (f.canRead()) {
tzezula@1324
   202
                    if (f.isFile()) {
tzezula@1324
   203
                        ZipFile zf = new ZipFile(f);
tzezula@1324
   204
                        try {
tzezula@1324
   205
                            Enumeration<? extends ZipEntry> entries = zf.entries();
tzezula@1324
   206
                            while (entries.hasMoreElements()) {
tzezula@1324
   207
                                ZipEntry e = entries.nextElement();
tzezula@1324
   208
                                if (e.isDirectory()) {
tzezula@1324
   209
                                    continue;
tzezula@1324
   210
                                }
tzezula@1324
   211
                                final String name = String.format("/%s",e.getName());
tzezula@1324
   212
                                final String owner = getOwner(name);
tzezula@1324
   213
                                List<String> content = classPathContent.get(owner);
tzezula@1324
   214
                                if (content == null) {
tzezula@1324
   215
                                    content = new ArrayList<>();
tzezula@1324
   216
                                    classPathContent.put(owner, content);
tzezula@1324
   217
                                }
tzezula@1324
   218
                                content.add(name);
tzezula@1324
   219
                            }
tzezula@1324
   220
                        } finally {
tzezula@1324
   221
                            zf.close();
tzezula@1324
   222
                        }
tzezula@1324
   223
                    } else if (f.isDirectory()) {
tzezula@1324
   224
                        addFiles(f,"/", classPathContent);
tzezula@1324
   225
                    }
tzezula@1324
   226
                }
tzezula@1324
   227
            }                                    
tzezula@1324
   228
        }
tzezula@1324
   229
        List<String> content = classPathContent.get(folder);
tzezula@1324
   230
        return content == null ? Collections.<String>emptyList() : content;
tzezula@1324
   231
    }
tzezula@1324
   232
tzezula@1324
   233
    private void addFiles(File folder, String path, Map<String,List<String>> into) {
tzezula@1324
   234
        for (File f : folder.listFiles()) {
tzezula@1324
   235
            String fname = path + (path.length() == 1 ? "" : "/") +  f.getName();
tzezula@1324
   236
            if (f.isDirectory()) {
tzezula@1324
   237
                addFiles(f, fname, into);
tzezula@1324
   238
            } else {
tzezula@1324
   239
                List<String> content = into.get(path);
tzezula@1324
   240
                if (content == null) {
tzezula@1324
   241
                    content = new ArrayList<>();
tzezula@1324
   242
                    classPathContent.put(path, content);
tzezula@1324
   243
                }
tzezula@1324
   244
                content.add(fname);
tzezula@1324
   245
            }
tzezula@1324
   246
        }
tzezula@1324
   247
    }
tzezula@1324
   248
    
tzezula@1324
   249
    private Map<String,List<String>> classPathContent;
tzezula@1324
   250
tzezula@1324
   251
    private void register(Location loc, String resource, MemoryFileObject jfo) {
tzezula@1324
   252
        Map<String,List<MemoryFileObject>> folders = generated.get(loc);
tzezula@1324
   253
        final String folder = getOwner(resource);
tzezula@1324
   254
        List<MemoryFileObject> content = folders.get(folder);
tzezula@1324
   255
        if (content == null) {
tzezula@1324
   256
            content = new ArrayList<>();
tzezula@1324
   257
            folders.put(folder, content);
tzezula@1324
   258
        }
tzezula@1324
   259
        content.add(jfo);
tzezula@1324
   260
    }
tzezula@1324
   261
tzezula@1324
   262
    private static String getOwner(String resource) {
tzezula@1324
   263
        int lastSlash = resource.lastIndexOf('/');
tzezula@1324
   264
        assert lastSlash >= 0;
tzezula@1324
   265
        return resource.substring(0, lastSlash);
tzezula@1324
   266
    }
tzezula@1324
   267
tzezula@1324
   268
    private static boolean canRead(Location loc) {
tzezula@1324
   269
        for (Location rl : READ_LOCATIONS) {
tzezula@1324
   270
            if (rl.equals(loc)) {
tzezula@1324
   271
                return true;
tzezula@1324
   272
            }
tzezula@1324
   273
        }
tzezula@1324
   274
        return false;
tzezula@1324
   275
    }
tzezula@1324
   276
tzezula@1324
   277
    private static boolean canWrite(Location loc) {
tzezula@1324
   278
        for (Location wl : WRITE_LOCATIONS) {
tzezula@1324
   279
            if (wl.equals(loc)) {
tzezula@1324
   280
                return true;
tzezula@1324
   281
            }
tzezula@1324
   282
        }
tzezula@1324
   283
        return false;
tzezula@1324
   284
    }
tzezula@1324
   285
tzezula@1324
   286
    private static boolean canClassLoad(Location loc) {
tzezula@1324
   287
        for (Location cll : CLASS_LOADER_LOCATIONS) {
tzezula@1324
   288
            if (cll.equals(loc)) {
tzezula@1324
   289
                return true;
tzezula@1324
   290
            }
tzezula@1324
   291
        }
tzezula@1324
   292
        return false;
tzezula@1324
   293
    }
tzezula@1324
   294
tzezula@1324
   295
    static String convertFQNToResource(String fqn) {
tzezula@1324
   296
        return '/' + fqn.replace('.', '/');   //NOI18N
tzezula@1324
   297
    }
tzezula@1324
   298
tzezula@1324
   299
    static String convertResourceToFQN(String resource) {
tzezula@1324
   300
        assert resource.startsWith("/");    //NOI18N
tzezula@1324
   301
        int lastSlash = resource.lastIndexOf('/');  //NOI18N
tzezula@1324
   302
        int lastDot = resource.lastIndexOf('.');    //NOI18N
tzezula@1324
   303
        int stop = lastSlash < lastDot ?
tzezula@1324
   304
            lastDot :
tzezula@1324
   305
            resource.length();
tzezula@1324
   306
        return resource.substring(1, stop).replace('/', '.');    //NOI18N
tzezula@1324
   307
    }
tzezula@1324
   308
tzezula@1324
   309
tzezula@1324
   310
    JavaFileObject createMemoryFileObject (String resourceName, JavaFileObject.Kind kind, byte[] content) {
tzezula@1324
   311
        final InferableJavaFileObject jfo  = new MemoryFileObject(resourceName, kind, content);
tzezula@1324
   312
        return jfo;
tzezula@1324
   313
    }
tzezula@1324
   314
tzezula@1324
   315
    Iterable<? extends MemoryFileObject> getGeneratedFiles(JavaFileObject.Kind... kinds) {
tzezula@1324
   316
        final Set<JavaFileObject.Kind> ks = EnumSet.noneOf(JavaFileObject.Kind.class);
tzezula@1324
   317
        Collections.addAll(ks, kinds);
tzezula@1324
   318
        final List<MemoryFileObject> res = new ArrayList<>();
tzezula@1324
   319
        for (Map<String,List<MemoryFileObject>> folders : generated.values()) {
tzezula@1324
   320
            for (List<MemoryFileObject> content : folders.values()) {
tzezula@1324
   321
                for (MemoryFileObject fo : content) {
tzezula@1324
   322
                    if (ks.contains(fo.getKind()) && fo.getLastModified() >= 0) {
tzezula@1324
   323
                        res.add(fo);
tzezula@1324
   324
                    }
tzezula@1324
   325
                }
tzezula@1324
   326
            }
tzezula@1324
   327
        }
tzezula@1324
   328
        return res;
tzezula@1324
   329
    }
tzezula@1324
   330
tzezula@1324
   331
}