001package jmri.managers; 002 003import java.lang.reflect.InvocationTargetException; 004import java.util.Set; 005 006import org.openide.util.lookup.ServiceProvider; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010import jmri.InstanceInitializer; 011import jmri.ShutDownManager; 012import jmri.implementation.AbstractInstanceInitializer; 013 014/** 015 * An initializer for the {@link jmri.ShutDownManager} that allows the 016 * ShutDownManager to be used to be specified as a Java property. 017 * <p> 018 * This InstanceInitializer provides a 019 * {@link jmri.managers.DefaultShutDownManager} unless the name of the class to 020 * use as the ShutDownManager is specified in the {@code jmri.shutdownmanager} 021 * Java System Property. If the property is specified, it must be a complete 022 * name of a class that implements jmri.ShutDownManager and has a public default 023 * constructor. 024 */ 025@ServiceProvider(service = InstanceInitializer.class) 026public class ShutDownManagerInitializer extends AbstractInstanceInitializer { 027 028 private static final Logger log = LoggerFactory.getLogger(ShutDownManagerInitializer.class); 029 030 /** 031 * {@inheritDoc} 032 */ 033 @Override 034 public <T> Object getDefault(Class<T> type) { 035 if (type.equals(ShutDownManager.class)) { 036 String property = System.getProperty("jmri.shutdownmanager"); 037 if (property != null) { 038 try { 039 Class<?> c = Class.forName(property); 040 if (ShutDownManager.class.isAssignableFrom(c)) { 041 return c.getConstructor().newInstance(); 042 } 043 log.error("Specified jmri.shutdownmanager value {} is not a jmri.ShutDownManager subclass", property); 044 } catch ( 045 ClassNotFoundException | 046 InstantiationException | 047 IllegalAccessException | 048 InvocationTargetException | 049 NoSuchMethodException | 050 SecurityException e) { 051 log.error("Unable to instanciate ShutDownManager class {} with default constructor", property); 052 } 053 } 054 return new DefaultShutDownManager(); 055 } 056 return super.getDefault(type); 057 } 058 059 /** 060 * {@inheritDoc} 061 */ 062 @Override 063 public Set<Class<?>> getInitalizes() { 064 Set<Class<?>> set = super.getInitalizes(); 065 set.add(ShutDownManager.class); 066 return set; 067 } 068 069}