001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Stephane Lacoin
018 */
019package org.nuxeo.webengine.gwt.codeserver;
020
021import java.io.IOException;
022import java.net.URI;
023import java.net.URISyntaxException;
024import java.net.URL;
025import java.net.URLClassLoader;
026import java.nio.file.FileSystemNotFoundException;
027import java.nio.file.FileSystems;
028import java.nio.file.Files;
029import java.nio.file.Path;
030import java.nio.file.Paths;
031import java.util.Collections;
032import java.util.HashMap;
033import java.util.Map;
034
035public class CodeServerLoader extends URLClassLoader {
036
037        CodeServerLoader(URL[] jars) {
038                super(jars, CodeServerLoader.class.getClassLoader());
039        }
040
041        @Override
042        protected java.lang.Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
043                Class<?> clazz = findLoadedClass(name);
044                if (clazz != null) {
045                        return clazz;
046                }
047
048                if (name.equals(CodeServerLauncher.class.getName())) {
049                        return getParent().loadClass(name);
050                }
051
052                if (name.equals(CodeServerWrapper.class.getName())) {
053                        try {
054                                return reloadClass(CodeServerWrapper.class);
055                        } catch (URISyntaxException | IOException cause) {
056                                throw new ClassNotFoundException("Cannot reload wrapper in gwt dev class loader", cause);
057                        }
058                }
059                try {
060                        return ClassLoader.getSystemClassLoader().loadClass(name);
061                } catch (ClassNotFoundException cause) {
062                        ;
063                }
064                synchronized (getClassLoadingLock(name)) {
065                        clazz = findClass(name);
066                        if (clazz != null) {
067                                return clazz;
068                        }
069                }
070                throw new ClassNotFoundException("Cannot find " + name + " in gwt class loader");
071        };
072
073        Class<?> reloadClass(Class<?> clazz) throws URISyntaxException, IOException {
074                URI location = clazz.getResource(clazz.getSimpleName().concat(".class")).toURI();
075                byte[] content = Files.readAllBytes(getPath(location));
076                return defineClass(clazz.getName(), content, 0, content.length);
077        }
078
079        private Path getPath(URI location) throws IOException {
080                try {
081                        return Paths.get(location);
082                } catch (FileSystemNotFoundException cause) {
083                        Map<String, Object> env = Collections.emptyMap();
084                        FileSystems.newFileSystem(location, env);
085                        return Paths.get(location);
086                }
087        }
088
089        CodeServerLauncher load() throws ReflectiveOperationException {
090                return (CodeServerLauncher) loadClass(CodeServerWrapper.class.getName()).newInstance();
091        }
092
093}