001package org.nuxeo.webengine.gwt.codeserver;
002
003import java.util.HashMap;
004import java.util.Map;
005
006import org.nuxeo.ecm.core.api.NuxeoException;
007import org.nuxeo.runtime.RuntimeServiceEvent;
008import org.nuxeo.runtime.RuntimeServiceListener;
009import org.nuxeo.runtime.api.Framework;
010import org.nuxeo.runtime.model.ComponentContext;
011import org.nuxeo.runtime.model.ComponentInstance;
012import org.nuxeo.runtime.model.DefaultComponent;
013
014public class CodeServerComponent extends DefaultComponent {
015
016        final Map<String, CodeServerConfig> servers = new HashMap<>();
017
018        @Override
019        public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 
020                if (contribution instanceof CodeServerConfig) {
021                        CodeServerConfig install = (CodeServerConfig) contribution;
022                        servers.put(install.module, install);
023                }
024        }
025
026        @Override
027        public void applicationStarted(ComponentContext context) {
028                Framework.addListener(new RuntimeServiceListener() {
029
030                        @Override
031                        public void handleEvent(RuntimeServiceEvent event) {
032                                if (event.id != RuntimeServiceEvent.RUNTIME_ABOUT_TO_STOP) {
033                                        return;
034                                }
035                                Framework.removeListener(this);
036                                shutdown();
037                        }
038
039                });
040                startup();
041        }
042
043        protected void startup()  {
044                new Runner() {
045
046                        @Override
047                        void doRun(CodeServerConfig server) throws Exception {
048                                server.startup();
049                        }
050
051                }.run();
052        }
053
054        protected void shutdown() {
055                new Runner() {
056
057                        @Override
058                        void doRun(CodeServerConfig server) throws Exception {
059                                server.shutdown();
060                        }
061
062                }.run();
063        }
064
065        abstract class Runner {
066
067                void run() {
068                        NuxeoException errors = new NuxeoException("Cannot shudown gwt code servers");
069                        for (CodeServerConfig server : servers.values()) {
070                                try {
071                                        doRun(server);
072                                } catch (Exception cause) {
073                                        errors.addSuppressed(cause);
074                                }
075                        }
076                        if (errors.getSuppressed().length > 0) {
077                                throw errors;
078                        }
079                }
080
081                abstract void doRun(CodeServerConfig server) throws Exception;
082        }
083
084}