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.HashMap; 022import java.util.Map; 023 024import org.nuxeo.ecm.core.api.NuxeoException; 025import org.nuxeo.runtime.RuntimeServiceEvent; 026import org.nuxeo.runtime.RuntimeServiceListener; 027import org.nuxeo.runtime.api.Framework; 028import org.nuxeo.runtime.model.ComponentContext; 029import org.nuxeo.runtime.model.ComponentInstance; 030import org.nuxeo.runtime.model.DefaultComponent; 031 032public class CodeServerComponent extends DefaultComponent { 033 034 final Map<String, CodeServerConfig> servers = new HashMap<>(); 035 036 @Override 037 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 038 if (contribution instanceof CodeServerConfig) { 039 CodeServerConfig install = (CodeServerConfig) contribution; 040 servers.put(install.module, install); 041 } 042 } 043 044 @Override 045 public void applicationStarted(ComponentContext context) { 046 Framework.addListener(new RuntimeServiceListener() { 047 048 @Override 049 public void handleEvent(RuntimeServiceEvent event) { 050 if (event.id != RuntimeServiceEvent.RUNTIME_ABOUT_TO_STOP) { 051 return; 052 } 053 Framework.removeListener(this); 054 shutdown(); 055 } 056 057 }); 058 startup(); 059 } 060 061 protected void startup() { 062 new Runner() { 063 064 @Override 065 void doRun(CodeServerConfig server) throws Exception { 066 server.startup(); 067 } 068 069 }.run(); 070 } 071 072 protected void shutdown() { 073 new Runner() { 074 075 @Override 076 void doRun(CodeServerConfig server) throws Exception { 077 server.shutdown(); 078 } 079 080 }.run(); 081 } 082 083 abstract class Runner { 084 085 void run() { 086 NuxeoException errors = new NuxeoException("Cannot shudown gwt code servers"); 087 for (CodeServerConfig server : servers.values()) { 088 try { 089 doRun(server); 090 } catch (Exception cause) { 091 errors.addSuppressed(cause); 092 } 093 } 094 if (errors.getSuppressed().length > 0) { 095 throw errors; 096 } 097 } 098 099 abstract void doRun(CodeServerConfig server) throws Exception; 100 } 101 102}