1 package org.apache.turbine.services.component; 2 3 18 19 import java.util.Iterator ; 20 import javax.servlet.ServletConfig ; 21 22 import org.apache.commons.configuration.BaseConfiguration; 23 import org.apache.commons.configuration.Configuration; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.stratum.component.ComponentLoader; 27 import org.apache.stratum.lifecycle.Disposable; 28 import org.apache.turbine.Turbine; 29 import org.apache.turbine.services.InitializationException; 30 import org.apache.turbine.services.TurbineBaseService; 31 32 40 public class TurbineComponentService 41 extends TurbineBaseService 42 implements ComponentService 43 { 44 45 46 private static Log log = LogFactory.getLog(TurbineComponentService.class); 47 48 49 private static String CONFIG = "config"; 50 51 52 private static String NAME = "name"; 53 54 55 private static String COMPONENT = "component"; 56 57 58 private Object [] components = null; 59 60 68 public void init() 69 throws InitializationException 70 { 71 ServletConfig config = Turbine.getTurbineServletConfig(); 72 Configuration loaderConf = new BaseConfiguration(); 73 74 String [] names = getConfiguration().getStringArray(NAME); 75 76 log.warn("The ComponentService is deprecated!"); 77 78 for (int i = 0; i < names.length; i++) 79 { 80 String key = names[i]; 81 82 loaderConf.addProperty(COMPONENT + "." + NAME, key); 83 84 String subProperty = COMPONENT + "." + key; 85 Configuration subConf = getConfiguration().subset(key); 86 87 for (Iterator it = subConf.getKeys(); it.hasNext();) 88 { 89 String subKey = (String ) it.next(); 90 Object subVal = subConf.getProperty(subKey); 91 92 if (subKey.equals(CONFIG)) 93 { 94 log.debug("Fixing up " + subVal); 95 String newPath = 96 config.getServletContext().getRealPath((String ) subVal); 97 98 if (newPath == null) 99 { 100 throw new InitializationException("Could not translate path " + subVal); 101 } 102 103 subVal = newPath; 104 log.debug("Now: " + subVal); 105 } 106 107 loaderConf.addProperty(subProperty + "." + subKey, 108 subVal); 109 } 110 111 log.info("Added " + key + " as a component"); 112 } 113 114 try 115 { 116 ComponentLoader cl = new ComponentLoader(loaderConf); 117 components = cl.load(); 118 setInit(true); 119 } 120 catch (Exception e) 121 { 122 log.error("Component Service failed: ", e); 123 throw new InitializationException("ComponentService failed: ", e); 124 } 125 } 126 127 136 public void init(ServletConfig config) 137 throws InitializationException 138 { 139 init(); 140 } 141 142 147 148 public void shutdown() 149 { 150 if (components != null) 151 { 152 for (int i = 0; i < components.length; i++) 153 { 154 if (components[i] instanceof Disposable) 155 { 156 log.debug("Disposing a " + components[i].getClass().getName() + " object"); 157 ((Disposable) components[i]).dispose(); 158 } 159 else 160 { 161 log.debug("Not disposing " + components[i].getClass().getName() + ", not a Disposable Object"); 162 } 163 } 164 } 165 setInit(false); 166 } 167 } 168 | Popular Tags |