001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.runtime.model.impl; 018 019import java.util.Set; 020 021import org.nuxeo.runtime.model.ComponentName; 022import org.nuxeo.runtime.model.RegistrationInfo; 023 024/** 025 * Deactivate components in the proper order to avoid exceptions at shutdown. 026 * 027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 028 */ 029public class ShutdownTask { 030 031 final static void shutdown(ComponentManagerImpl mgr) { 032 RegistrationInfoImpl[] ris = mgr.reg.getComponentsArray(); 033 for (RegistrationInfoImpl ri : ris) { 034 shutdown(mgr, ri); 035 } 036 } 037 038 private static void shutdown(ComponentManagerImpl mgr, RegistrationInfoImpl ri) { 039 ComponentName name = ri.getName(); 040 if (name == null) { 041 return; // already destroyed 042 } 043 if (ri.getState() <= RegistrationInfo.RESOLVED) { 044 // not yet activated so we can destroy it right now 045 mgr.unregister(name); 046 return; 047 } 048 // an active component - get the components depending on it 049 Set<ComponentName> reqs = mgr.reg.requirements.get(name); 050 if (reqs != null && !reqs.isEmpty()) { 051 // there are some components depending on me - cannot shutdown 052 for (ComponentName req : reqs.toArray(new ComponentName[reqs.size()])) { 053 RegistrationInfoImpl parentRi = mgr.reg.components.get(req); 054 if (parentRi != null) { 055 shutdown(mgr, parentRi); 056 } 057 } 058 } 059 // no components are depending on me - shutdown now 060 mgr.unregister(name); 061 } 062 063}