001package org.apache.turbine.services.avaloncomponent; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import java.util.ArrayList; 025import java.util.Iterator; 026import java.util.List; 027 028import org.apache.avalon.excalibur.component.DefaultRoleManager; 029import org.apache.avalon.excalibur.component.ExcaliburComponentManager; 030import org.apache.avalon.excalibur.logger.Log4JLoggerManager; 031import org.apache.avalon.excalibur.logger.LoggerManager; 032import org.apache.avalon.framework.activity.Disposable; 033import org.apache.avalon.framework.activity.Initializable; 034import org.apache.avalon.framework.component.Component; 035import org.apache.avalon.framework.component.ComponentException; 036import org.apache.avalon.framework.configuration.Configuration; 037import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 038import org.apache.avalon.framework.context.DefaultContext; 039import org.apache.avalon.framework.logger.Logger; 040import org.apache.avalon.framework.service.ServiceException; 041import org.apache.commons.logging.Log; 042import org.apache.commons.logging.LogFactory; 043import org.apache.turbine.Turbine; 044import org.apache.turbine.services.InitializationException; 045import org.apache.turbine.services.InstantiationException; 046import org.apache.turbine.services.TurbineBaseService; 047 048/** 049 * An implementation of AvalonComponentService which loads all the 050 * components given in the TurbineResources.properties File. 051 * <p> 052 * For component which require the location of the application or 053 * context root, there are two ways to get it. 054 * <ol> 055 * <li> 056 * Implement the Contextualizable interface. The full path to the 057 * correct OS directory can be found under the ComponentAppRoot key. 058 * </li> 059 * <li> 060 * The system property "applicationRoot" is also set to the full path 061 * of the correct OS directory. 062 * </li> 063 * </ol> 064 * If you want to initialize Torque by using the AvalonComponentService, you 065 * must activate Torque at initialization time by specifying 066 * 067 * services.AvalonComponentService.lookup = org.apache.torque.Torque 068 * 069 * in your TurbineResources.properties. 070 * 071 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 072 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 073 * @version $Id: TurbineAvalonComponentService.java 1706239 2015-10-01 13:18:35Z tv $ 074 */ 075@Deprecated 076public class TurbineAvalonComponentService 077 extends TurbineBaseService 078 implements AvalonComponentService, Initializable, Disposable 079{ 080 /** Logging */ 081 private static Log log = LogFactory.getLog( 082 TurbineAvalonComponentService.class); 083 084 /** Component manager */ 085 private ExcaliburComponentManager manager = null; 086 087 // ------------------------------------------------------------- 088 // Service initialization 089 // ------------------------------------------------------------- 090 091 /** 092 * Load all configured components and initialize them. This is 093 * a zero parameter variant which queries the Turbine Servlet 094 * for its config. 095 * 096 * @throws InitializationException Something went wrong in the init 097 * stage 098 */ 099 @Override 100 public void init() 101 throws InitializationException 102 { 103 try 104 { 105 initialize(); 106 107 setInit(true); 108 } 109 catch (Exception e) 110 { 111 throw new InitializationException("init failed", e); 112 } 113 } 114 115 /** 116 * Shuts the Component Service down, calls dispose on the components that 117 * implement this interface 118 * 119 */ 120 @Override 121 public void shutdown() 122 { 123 dispose(); 124 setInit(false); 125 } 126 127 // ------------------------------------------------------------- 128 // Avalon lifecycle interfaces 129 // ------------------------------------------------------------- 130 131 /** 132 * Initializes the container 133 * 134 * @throws Exception generic exception 135 */ 136 @Override 137 public void initialize() throws Exception 138 { 139 org.apache.commons.configuration.Configuration conf 140 = getConfiguration(); 141 142 // get the filenames and expand them relative to webapp root 143 String sysConfigFilename = Turbine.getRealPath( 144 conf.getString(COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE)); 145 String roleConfigFilename = Turbine.getRealPath( 146 conf.getString(COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE)); 147 148 log.debug("Config File: " + sysConfigFilename); 149 log.debug("Role File: " + roleConfigFilename); 150 151 // process configuration files 152 153 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 154 Configuration sysConfig = builder.buildFromFile(sysConfigFilename); 155 Configuration roleConfig = builder.buildFromFile(roleConfigFilename); 156 157 // Create the LoggerManager for Log4J 158 LoggerManager lm = new Log4JLoggerManager(); 159 160 // Setup the RoleManager 161 DefaultRoleManager roles = new DefaultRoleManager(); 162 163 Logger logger = lm.getLoggerForCategory(AVALON_LOG_CATEGORY); 164 165 roles.enableLogging(logger); 166 roles.configure(roleConfig); 167 168 // Setup ECM 169 manager = new ExcaliburComponentManager(); 170 171 manager.setLoggerManager(lm); 172 manager.enableLogging(logger); 173 174 DefaultContext context = new DefaultContext(); 175 String realPath = Turbine.getRealPath("/"); 176 177 context.put(AvalonComponentService.COMPONENT_APP_ROOT, realPath); 178 // urn:avalon:home is used by Merlinized components. Makes things 179 // a bit more backwards compatible. 180 context.put("urn:avalon:home", realPath); 181 System.setProperty("applicationRoot", realPath); 182 System.setProperty("urn:avalon:home", realPath); 183 184 log.debug("Application Root is " + realPath); 185 186 manager.contextualize(context); 187 manager.setRoleManager(roles); 188 manager.configure(sysConfig); 189 190 // Init ECM!!!! 191 manager.initialize(); 192 193 List<Object> lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY, 194 new ArrayList<Object>()); 195 196 for (Iterator<Object> it = lookupComponents.iterator(); it.hasNext();) 197 { 198 String component = (String) it.next(); 199 try 200 { 201 Component c = manager.lookup(component); 202 log.info("Lookup for Component " + component + " successful"); 203 manager.release(c); 204 } 205 catch (Exception e) 206 { 207 log.error("Lookup for Component " + component + " failed!"); 208 } 209 } 210 } 211 212 /** 213 * Disposes of the container and releases resources 214 */ 215 @Override 216 public void dispose() 217 { 218 manager.dispose(); 219 } 220 221 /** 222 * Returns an instance of the named component 223 * 224 * @param roleName Name of the role the component fills. 225 * @return an instance of the named component 226 */ 227 @Override 228 public Object lookup(String roleName) 229 throws ServiceException 230 { 231 try 232 { 233 return manager.lookup(roleName); 234 } 235 catch (ComponentException e) 236 { 237 throw new ServiceException(name, e.getMessage()); 238 } 239 } 240 241 /** 242 * Releases the component 243 * 244 * @param component the component to release 245 */ 246 @Override 247 public void release(Object component) 248 { 249 if( component instanceof Component ) 250 { 251 manager.release((Component)component); 252 } 253 } 254 255 /** 256 * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String) 257 */ 258 @Override 259 public boolean hasService(String roleName) 260 { 261 return manager.hasComponent(roleName); 262 } 263 264 // ------------------------------------------------------------- 265 // TurbineServiceProvider 266 // ------------------------------------------------------------- 267 268 /** 269 * @see org.apache.turbine.services.TurbineServiceProvider#exists(java.lang.String) 270 */ 271 @Override 272 public boolean exists(String roleName) 273 { 274 return this.hasService(roleName); 275 } 276 277 /** 278 * @see org.apache.turbine.services.TurbineServiceProvider#get(java.lang.String) 279 */ 280 @Override 281 public Object get(String roleName) throws InstantiationException 282 { 283 try 284 { 285 return this.lookup(roleName); 286 } 287 catch (ServiceException e) 288 { 289 String msg = "Unable to get the following service : " + roleName; 290 log.error(msg); 291 throw new InstantiationException(msg); 292 } 293 catch (Throwable t) 294 { 295 String msg = "Unable to get the following service : " + roleName; 296 log.error(msg,t); 297 throw new InstantiationException(msg,t); 298 } 299 } 300}