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
021
022import com.google.gwt.dev.codeserver.CodeServer;
023import com.google.gwt.dev.codeserver.Options;
024import com.google.gwt.dev.codeserver.WebServer;
025
026public class CodeServerWrapper implements CodeServerLauncher {
027
028    WebServer server;
029
030    @Override
031    public void startup(String[] args) throws Exception {
032        Options options = new Options();
033
034        if (!options.parseArgs(args)) {
035            throw new RuntimeException("Cannot parse gwt code server options");
036        }
037        ClassLoader cl = Thread.currentThread().getContextClassLoader();
038        Thread.currentThread().setContextClassLoader(CodeServerWrapper.class.getClassLoader());
039        try {
040            server = CodeServer.start(options);
041        } finally {
042            Thread.currentThread().setContextClassLoader(cl);
043        }
044    }
045
046    @Override
047    public void shutdown() throws Exception {
048        if (server == null) {
049            return;
050        }
051        try {
052            server.stop();
053        } finally {
054            server = null;
055        }
056    }
057
058}