001/* 002 * (C) Copyright 2006-2010 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 * bstefanescu 018 */ 019package org.nuxeo.runtime.model.impl; 020 021import java.util.Set; 022 023import org.nuxeo.runtime.model.ComponentName; 024import org.nuxeo.runtime.model.RegistrationInfo; 025 026/** 027 * Deactivate components in the proper order to avoid exceptions at shutdown. 028 * 029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 030 */ 031public class ShutdownTask { 032 033 final static void shutdown(ComponentManagerImpl mgr) { 034 RegistrationInfoImpl[] ris = mgr.reg.getComponentsArray(); 035 for (RegistrationInfoImpl ri : ris) { 036 shutdown(mgr, ri); 037 } 038 } 039 040 private static void shutdown(ComponentManagerImpl mgr, RegistrationInfoImpl ri) { 041 ComponentName name = ri.getName(); 042 if (name == null) { 043 return; // already destroyed 044 } 045 if (ri.getState() <= RegistrationInfo.RESOLVED) { 046 // not yet activated so we can destroy it right now 047 mgr.unregister(name); 048 return; 049 } 050 // an active component - get the components depending on it 051 Set<ComponentName> reqs = mgr.reg.requirements.get(name); 052 if (reqs != null && !reqs.isEmpty()) { 053 // there are some components depending on me - cannot shutdown 054 for (ComponentName req : reqs.toArray(new ComponentName[reqs.size()])) { 055 RegistrationInfoImpl parentRi = mgr.reg.components.get(req); 056 if (parentRi != null) { 057 shutdown(mgr, parentRi); 058 } 059 } 060 } 061 // no components are depending on me - shutdown now 062 mgr.unregister(name); 063 } 064 065}