| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="de"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>TurbineYaafiComponentService.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> > <a href="index.source.html" class="el_package">org.apache.turbine.services.avaloncomponent</a> > <span class="el_source">TurbineYaafiComponentService.java</span></div><h1>TurbineYaafiComponentService.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.avaloncomponent; |
| |
| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| import java.io.File; |
| import java.io.IOException; |
| |
| import org.apache.avalon.framework.activity.Disposable; |
| import org.apache.avalon.framework.activity.Initializable; |
| import org.apache.avalon.framework.logger.Logger; |
| import org.apache.avalon.framework.service.ServiceException; |
| import org.apache.commons.configuration2.Configuration; |
| import org.apache.fulcrum.yaafi.framework.container.ServiceContainer; |
| import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration; |
| import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory; |
| import org.apache.fulcrum.yaafi.framework.logger.Log4j2Logger; |
| import org.apache.logging.log4j.LogManager; |
| import org.apache.turbine.services.InitializationException; |
| import org.apache.turbine.services.InstantiationException; |
| import org.apache.turbine.services.TurbineBaseService; |
| |
| /** |
| * An implementation of Turbine service initializing the YAAFI container |
| * |
| * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> |
| */ |
| <span class="fc" id="L44">public class TurbineYaafiComponentService</span> |
| extends TurbineBaseService |
| implements AvalonComponentService, Initializable, Disposable |
| { |
| /** the logger to be used */ |
| <span class="fc" id="L49"> private static org.apache.logging.log4j.Logger log = LogManager.getLogger(AVALON_LOG_CATEGORY);</span> |
| |
| /** property to lookup the container configuration file */ |
| public static final String CONTAINER_CONFIGURATION_KEY = "containerConfiguration"; |
| |
| /** the default value for the container configuration file */ |
| public static final String CONTAINER_CONFIGURATION_VALUE = "/WEB-INF/conf/containerConfiguration.xml"; |
| |
| /** property to lookup the properties file */ |
| public static final String COMPONENT_PARAMETERS_KEY = "parameters"; |
| |
| /** the default value for the parameter file */ |
| public static final String COMPONENT_PARAMETERS_VALUE = "/WEB-INF/conf/parameters.properties"; |
| |
| /** YAFFI container */ |
| private ServiceContainer container; |
| |
| /** |
| * Load all configured components and initialize them. This is a zero parameter variant which |
| * queries the Turbine Servlet for its config. |
| * |
| * @throws InitializationException Something went wrong in the init stage |
| */ |
| @Override |
| public void init() throws InitializationException |
| { |
| try |
| { |
| <span class="fc" id="L77"> log.info( "Initializing TurbineYaafiComponentService ..." );</span> |
| <span class="fc" id="L78"> initialize();</span> |
| <span class="fc" id="L79"> setInit(true);</span> |
| } |
| <span class="nc" id="L81"> catch (Exception e)</span> |
| { |
| <span class="nc" id="L83"> log.error("Exception caught initialising service: ", e);</span> |
| <span class="nc" id="L84"> throw new InitializationException("Initializing TurbineYaafiComponentService failed", e);</span> |
| <span class="fc" id="L85"> }</span> |
| <span class="fc" id="L86"> }</span> |
| |
| /** |
| * Shuts the Component Service down, calls dispose on the components that implement this |
| * interface |
| * |
| */ |
| @Override |
| public void shutdown() |
| { |
| <span class="fc" id="L96"> log.info( "Disposing TurbineYaafiComponentService ..." );</span> |
| <span class="fc" id="L97"> dispose();</span> |
| <span class="fc" id="L98"> setInit(false);</span> |
| <span class="fc" id="L99"> }</span> |
| |
| // ------------------------------------------------------------- |
| // Avalon lifecycle interfaces |
| // ------------------------------------------------------------- |
| |
| /** |
| * Initializes the container |
| * |
| * @throws Exception generic exception |
| */ |
| @Override |
| public void initialize() throws Exception |
| { |
| // get the configuration from the baseclass |
| <span class="fc" id="L114"> Configuration conf = this.getConfiguration();</span> |
| |
| // determine the home directory |
| <span class="fc" id="L117"> File home = new File(getServiceBroker().getApplicationRoot());</span> |
| <span class="fc" id="L118"> log.info("Using the following home : {}", home::getAbsolutePath);</span> |
| |
| // create the configuration for YAAFI |
| <span class="fc" id="L121"> ServiceContainerConfiguration config =</span> |
| <span class="fc" id="L122"> this.createServiceContainerConfiguration(conf, home);</span> |
| |
| // initialize the container |
| try |
| { |
| <span class="fc" id="L127"> this.container = ServiceContainerFactory.create(</span> |
| config |
| ); |
| //this.container .service( manager ); |
| } |
| <span class="nc" id="L132"> catch (Exception e)</span> |
| { |
| <span class="nc" id="L134"> log.error("Initializing YAAFI failed", e);</span> |
| <span class="nc" id="L135"> throw e;</span> |
| <span class="fc" id="L136"> }</span> |
| <span class="fc" id="L137"> }</span> |
| |
| /** |
| * Disposes of the container and releases resources |
| */ |
| @Override |
| public void dispose() |
| { |
| <span class="pc bpc" id="L145" title="1 of 2 branches missed."> if (this.container != null)</span> |
| { |
| <span class="fc" id="L147"> this.container.dispose();</span> |
| <span class="fc" id="L148"> this.container = null;</span> |
| } |
| <span class="fc" id="L150"> }</span> |
| |
| /** |
| * Returns an instance of the named component |
| * |
| * @param roleName Name of the role the component fills. |
| * @return an instance of the named component |
| */ |
| @Override |
| public Object lookup(String roleName) throws ServiceException |
| { |
| <span class="fc" id="L161"> return this.container.lookup(roleName);</span> |
| } |
| |
| /** |
| * Releases the component. |
| * |
| * @param component the component to release |
| */ |
| @Override |
| public void release(Object component) |
| { |
| <span class="nc" id="L172"> this.container.release( component );</span> |
| <span class="nc" id="L173"> }</span> |
| |
| /** |
| * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String) |
| */ |
| @Override |
| public boolean hasService(String roleName) |
| { |
| <span class="fc" id="L181"> return this.container.hasService(roleName);</span> |
| } |
| |
| /** |
| * Create a ServiceContainerConfiguration based on the Turbine configuration |
| * |
| * @param conf the Turbine configuration |
| * @param applicationRoot the application root directory |
| * |
| * @return the YAAFI configuration |
| * @throws IOException creating the YAAFI configuration failed |
| */ |
| protected ServiceContainerConfiguration createServiceContainerConfiguration( Configuration conf, File applicationRoot ) |
| throws IOException |
| { |
| <span class="fc" id="L196"> ServiceContainerConfiguration result = new ServiceContainerConfiguration();</span> |
| |
| <span class="fc" id="L198"> result.setApplicationRootDir(applicationRoot.getAbsolutePath());</span> |
| <span class="fc" id="L199"> result.setLogger( this.createAvalonLogger() );</span> |
| |
| // are we using a "containerConfiguration.xml" ?! |
| |
| <span class="fc bfc" id="L203" title="All 2 branches covered."> if( conf.containsKey(CONTAINER_CONFIGURATION_KEY) )</span> |
| { |
| // determine the container configuration file |
| |
| <span class="fc" id="L207"> String containerConfiguration = conf.getString(</span> |
| CONTAINER_CONFIGURATION_KEY |
| ); |
| |
| <span class="fc" id="L211"> result.loadContainerConfiguration(containerConfiguration);</span> |
| <span class="fc" id="L212"> }</span> |
| <span class="pc bpc" id="L213" title="1 of 2 branches missed."> else if( conf.containsKey(COMPONENT_ROLE_KEY) )</span> |
| { |
| // determine the location of the role configuraton file |
| |
| <span class="fc" id="L217"> String roleConfigurationFileName = conf.getString(</span> |
| COMPONENT_ROLE_KEY, |
| COMPONENT_ROLE_VALUE |
| ); |
| |
| // determine the location of component configuration file |
| |
| <span class="fc" id="L224"> String componentConfigurationFileName = conf.getString(</span> |
| COMPONENT_CONFIG_KEY, |
| COMPONENT_CONFIG_VALUE |
| ); |
| |
| // determine the location of parameters file |
| |
| <span class="fc" id="L231"> String parametersFileName = conf.getString(</span> |
| COMPONENT_PARAMETERS_KEY, |
| COMPONENT_PARAMETERS_VALUE |
| ); |
| |
| <span class="fc" id="L236"> result.setComponentRolesLocation( roleConfigurationFileName );</span> |
| <span class="fc" id="L237"> result.setComponentConfigurationLocation( componentConfigurationFileName );</span> |
| <span class="fc" id="L238"> result.setParametersLocation( parametersFileName );</span> |
| <span class="fc" id="L239"> }</span> |
| else |
| { |
| // determine the container configuration file |
| |
| <span class="nc" id="L244"> String containerConfiguration = conf.getString(</span> |
| CONTAINER_CONFIGURATION_KEY, |
| CONTAINER_CONFIGURATION_VALUE |
| ); |
| |
| <span class="nc" id="L249"> result.loadContainerConfiguration(containerConfiguration);</span> |
| } |
| |
| <span class="fc" id="L252"> return result;</span> |
| } |
| |
| /** |
| * Create the Avalon logger to be passed to YAAFI. |
| * @return an Avalon Logger |
| */ |
| protected Logger createAvalonLogger() |
| { |
| <span class="fc" id="L261"> return new Log4j2Logger(log);</span> |
| } |
| |
| // ------------------------------------------------------------- |
| // TurbineServiceProvider |
| // ------------------------------------------------------------- |
| |
| /** |
| * @see org.apache.turbine.services.TurbineServiceProvider#exists(java.lang.String) |
| */ |
| @Override |
| public boolean exists(String roleName) |
| { |
| <span class="fc" id="L274"> return this.hasService(roleName);</span> |
| } |
| |
| /** |
| * @see org.apache.turbine.services.TurbineServiceProvider#get(java.lang.String) |
| */ |
| @Override |
| public Object get(String roleName) throws InstantiationException |
| { |
| try |
| { |
| <span class="fc" id="L285"> return this.lookup(roleName);</span> |
| } |
| <span class="nc" id="L287"> catch (Throwable t)</span> |
| { |
| <span class="nc" id="L289"> String msg = "Unable to get the following service : " + roleName;</span> |
| <span class="nc" id="L290"> log.error(msg,t);</span> |
| <span class="nc" id="L291"> throw new InstantiationException(msg,t);</span> |
| } |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html> |