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