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.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeList;
026import org.nuxeo.common.xmap.annotation.XObject;
027
028@XObject("codeserver")
029public class CodeServerConfig {
030
031        @XNode("@module")
032        String module;
033
034        @XNode("classpath")
035        CodeServerClasspath classpath;
036
037        @XNodeList(value = "option", type = ArrayList.class, componentType = CodeServerOption.class)
038        List<CodeServerOption> options = new ArrayList<>();
039
040        CodeServerLauncher launcher;
041
042        private CodeServerLoader loader;
043
044        String[] toArgs() {
045                List<String> args = new ArrayList<>();
046                for (CodeServerOption each : options) {
047                        each.toArgs(args);
048                }
049                args.add(module);
050                return args.toArray(new String[args.size()]);
051        }
052
053        void startup() throws Exception {
054                loader = new CodeServerLoader(classpath.entries);
055                launcher = loader.load();
056                launcher.startup(toArgs());
057        }
058
059        void shutdown() throws Exception {
060                try {
061                        if (launcher != null) {
062                                launcher.shutdown();
063                        }
064                } finally {
065                        launcher = null;
066                        try {
067                                if (loader != null) {
068                                        loader.close();
069                                }
070                        } finally {
071                                loader = null;
072                        }
073                }
074        }
075}