blob: 382e216a5a9ef6f5c6d822119ac6fefed6aad5c1 [file] [log] [blame]
<?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=""><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>AnnotationProcessor.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> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.annotation</a> &gt; <span class="el_source">AnnotationProcessor.java</span></div><h1>AnnotationProcessor.java</h1><pre class="source lang-java linenums">package org.apache.turbine.annotation;
/*
* 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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.apache.fulcrum.pool.PoolException;
import org.apache.fulcrum.pool.PoolService;
import org.apache.fulcrum.security.model.turbine.TurbineAccessControlList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.Turbine;
import org.apache.turbine.modules.Loader;
import org.apache.turbine.services.Service;
import org.apache.turbine.services.ServiceManager;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
import org.apache.turbine.util.TurbineException;
/**
* AnnotationProcessor contains static helper methods that handle the
* Turbine annotations for objects
*
* @author &lt;a href=&quot;mailto:tv@apache.org&quot;&gt;Thomas Vandahl&lt;/a&gt;
* @version $Id: TurbineAssemblerBrokerService.java 1521103 2013-09-09 13:38:07Z tv $
*/
<span class="nc" id="L53">public class AnnotationProcessor</span>
{
/** Logging */
<span class="fc" id="L56"> private static final Logger log = LogManager.getLogger(AnnotationProcessor.class);</span>
/** Annotation cache */
<span class="fc" id="L59"> private static final ConcurrentMap&lt;String, Annotation[]&gt; annotationCache = new ConcurrentHashMap&lt;&gt;();</span>
/**
* Get cached annotations for field, class or method
*
* @param object a field, class or method
*
* @return the declared annotations for the object
*/
public static Annotation[] getAnnotations(AccessibleObject object)
{
<span class="fc" id="L70"> String key = object.getClass() + object.toString();</span>
<span class="fc" id="L71"> Annotation[] annotations = annotationCache.get(key);</span>
<span class="fc bfc" id="L72" title="All 2 branches covered."> if (annotations == null)</span>
{
<span class="fc" id="L74"> Annotation[] newAnnotations = object.getDeclaredAnnotations();</span>
<span class="fc" id="L75"> annotations = annotationCache.putIfAbsent(key, newAnnotations);</span>
<span class="pc bpc" id="L76" title="1 of 2 branches missed."> if (annotations == null)</span>
{
<span class="fc" id="L78"> annotations = newAnnotations;</span>
}
}
<span class="fc" id="L81"> return annotations;</span>
}
<span class="fc" id="L84"> public enum ConditionType</span>
{
<span class="fc" id="L86"> COMPOUND, ANY;</span>
}
/**
* Check if the object given is authorized to be executed based on its annotations
*
* The method will return false if one of the annotations denies execution
*
* @see #isAuthorized(AccessibleObject, TurbineAccessControlList, ConditionType)
*
* @param &lt;A&gt; ACL instance
* @param object accessible object to test
* @param acl access control list
* @return true if the execution is allowed
*/
public static &lt;A extends TurbineAccessControlList&lt;?&gt;&gt; boolean isAuthorized(AccessibleObject object, A acl)
{
<span class="nc" id="L103"> return isAuthorized( object, acl, ConditionType.COMPOUND );</span>
}
/**
* Check if the object given is authorized to be executed based on its annotations
* The method's return value depends on the conditonType, refer to the ConditionType
*
* @param &lt;A&gt; ACL instance
* @param object the object
* @param acl access control list
* @param conditonType either {@link ConditionType#COMPOUND}: The method will return false if one of the annotations denies execution
* or {@link ConditionType#ANY} : The method will return true if one of the annotations allows execution
* @return true if the execution is allowed
*/
public static &lt;A extends TurbineAccessControlList&lt;?&gt;&gt; boolean isAuthorized(AccessibleObject object, A acl, ConditionType conditonType)
{
<span class="fc" id="L119"> Annotation[] annotations = getAnnotations(object);</span>
<span class="pc bfc" id="L121" title="All 2 branches covered."> for (Annotation annotation : annotations)</span>
{
<span class="pc bpc" id="L123" title="1 of 2 branches missed."> if (annotation instanceof TurbineRequiredRole)</span>
{
<span class="fc" id="L125"> TurbineRequiredRole trr = (TurbineRequiredRole) annotation;</span>
<span class="fc" id="L126"> String[] roleNames = trr.value();</span>
<span class="fc" id="L127"> String group = trr.group();</span>
<span class="pc bpc" id="L129" title="1 of 2 branches missed."> if (StringUtils.isEmpty(group)) // global group</span>
{
<span class="fc bfc" id="L131" title="All 2 branches covered."> for (String roleName : roleNames)</span>
{
<span class="fc bfc" id="L133" title="All 2 branches covered."> switch ( conditonType ) {</span>
case COMPOUND: default:
<span class="fc bfc" id="L135" title="All 2 branches covered."> if (!acl.hasRole(roleName))</span>
{
<span class="fc" id="L137"> return false;</span>
}
break;
case ANY:
<span class="fc bfc" id="L141" title="All 2 branches covered."> if (acl.hasRole(roleName))</span>
{
<span class="fc" id="L143"> return true;</span>
}
break;
}
}
<span class="pc bpc" id="L148" title="1 of 2 branches missed."> if (conditonType == ConditionType.ANY) { // nothing matched</span>
<span class="fc" id="L149"> return false;</span>
}
}
else
{
<span class="nc bnc" id="L154" title="All 2 branches missed."> for (String roleName : roleNames)</span>
{
<span class="nc bnc" id="L156" title="All 2 branches missed."> switch ( conditonType ) {</span>
case COMPOUND: default:
<span class="nc bnc" id="L158" title="All 2 branches missed."> if (!acl.hasRole(roleName, group))</span>
{
<span class="nc" id="L160"> return false;</span>
}
break;
case ANY:
<span class="nc bnc" id="L164" title="All 2 branches missed."> if (acl.hasRole(roleName, group))</span>
{
<span class="nc" id="L166"> return true;</span>
}
break;
}
}
}
<span class="nc" id="L172"> }</span>
<span class="nc bnc" id="L173" title="All 2 branches missed."> else if (annotation instanceof TurbineRequiredPermission)</span>
{
<span class="nc" id="L175"> TurbineRequiredPermission trp = (TurbineRequiredPermission) annotation;</span>
<span class="nc" id="L176"> String[] permissionNames = trp.value();</span>
<span class="nc" id="L177"> String group = trp.group();</span>
<span class="nc bnc" id="L179" title="All 2 branches missed."> if (StringUtils.isEmpty(group)) // global group</span>
{
<span class="nc bnc" id="L181" title="All 2 branches missed."> for (String permissionName : permissionNames)</span>
{
<span class="nc bnc" id="L183" title="All 2 branches missed."> switch ( conditonType ) {</span>
case COMPOUND: default:
<span class="nc bnc" id="L185" title="All 2 branches missed."> if (!acl.hasPermission(permissionName))</span>
{
<span class="nc" id="L187"> return false;</span>
}
break;
case ANY:
<span class="nc bnc" id="L191" title="All 2 branches missed."> if (acl.hasPermission(permissionName))</span>
{
<span class="nc" id="L193"> return true;</span>
}
break;
}
}
}
else
{
<span class="nc bnc" id="L201" title="All 2 branches missed."> for (String permissionName : permissionNames)</span>
{
<span class="nc bnc" id="L203" title="All 2 branches missed."> switch ( conditonType ) {</span>
case COMPOUND: default:
<span class="nc bnc" id="L205" title="All 2 branches missed."> if (!acl.hasPermission(permissionName, group))</span>
{
<span class="nc" id="L207"> return false;</span>
}
break;
case ANY:
<span class="nc bnc" id="L211" title="All 2 branches missed."> if (acl.hasPermission(permissionName, group))</span>
{
<span class="nc" id="L213"> return true;</span>
}
break;
}
}
}
}
}
<span class="fc" id="L223"> return true;</span>
}
/**
* Search for annotated fields of the object and inject the appropriate
* objects
*
* @param object the object
* @throws TurbineException if the objects could not be injected
*/
public static void process(Object object) throws TurbineException
{
<span class="fc" id="L235"> process(object, false);</span>
<span class="fc" id="L236"> }</span>
/**
* Search for annotated fields and optionally of method fields of the object and inject the appropriate
* objects
*
* @param object the object
* @param hasTurbineServicesInMethodFields set &lt;code&gt;true &lt;/code&gt;, if methods should be parsed
* @throws TurbineException if the objects could not be injected
*/
public static void process(Object object, Boolean hasTurbineServicesInMethodFields) throws TurbineException
{
<span class="fc" id="L248"> ServiceManager manager = null;</span>
<span class="fc" id="L249"> Configuration config = null;</span>
<span class="fc" id="L250"> AssemblerBrokerService assembler = null;</span>
<span class="fc" id="L251"> PoolService pool= null;</span>
<span class="fc" id="L252"> Class&lt;?&gt; clazz = object.getClass();</span>
<span class="fc" id="L254"> boolean isTurbineService = false;</span>
<span class="pc bpc" id="L255" title="1 of 2 branches missed."> if ( clazz.isAnnotationPresent(TurbineService.class)) {</span>
<span class="nc" id="L256"> TurbineService service = clazz.getAnnotation(TurbineService.class);</span>
<span class="nc" id="L257"> log.debug(&quot;retrieved class annotation: &quot;+ service);</span>
<span class="nc" id="L258"> isTurbineService = true;</span>
}
<span class="fc bfc" id="L261" title="All 2 branches covered."> while (clazz != null)</span>
{
<span class="fc" id="L263"> Field[] fields = clazz.getDeclaredFields();</span>
<span class="fc bfc" id="L265" title="All 2 branches covered."> for (Field field : fields)</span>
{
<span class="fc" id="L267"> Annotation[] annotations = getAnnotations(field);</span>
<span class="fc bfc" id="L269" title="All 2 branches covered."> for (Annotation a : annotations)</span>
{
<span class="fc bfc" id="L271" title="All 2 branches covered."> if (a instanceof TurbineService)</span>
{
<span class="fc bfc" id="L273" title="All 2 branches covered."> if (manager == null)</span>
{
<span class="fc" id="L275"> manager = TurbineServices.getInstance();</span>
}
<span class="fc" id="L277"> injectTurbineService(object, manager, field, (TurbineService) a);</span>
}
<span class="fc bfc" id="L279" title="All 2 branches covered."> else if (a instanceof TurbineConfiguration)</span>
{
<span class="fc bfc" id="L281" title="All 2 branches covered."> if (config == null)</span>
{
<span class="fc" id="L283"> config = Turbine.getConfiguration();</span>
}
<span class="fc" id="L285"> injectTurbineConfiguration(object, config, field, (TurbineConfiguration) a);</span>
}
<span class="fc bfc" id="L287" title="All 2 branches covered."> else if (a instanceof TurbineLoader)</span>
{
<span class="pc bpc" id="L289" title="1 of 2 branches missed."> if (assembler == null)</span>
{
<span class="fc" id="L291"> assembler = (AssemblerBrokerService) TurbineServices.getInstance().</span>
<span class="fc" id="L292"> getService(AssemblerBrokerService.SERVICE_NAME);</span>
}
<span class="fc" id="L294"> injectTurbineLoader(object, assembler, field, (TurbineLoader) a);</span>
}
<span class="pc bpc" id="L296" title="1 of 2 branches missed."> else if (a instanceof TurbineTool)</span>
{
<span class="pc bpc" id="L298" title="1 of 2 branches missed."> if (pool == null)</span>
{
<span class="fc" id="L300"> pool = (PoolService)TurbineServices.getInstance()</span>
<span class="fc" id="L301"> .getService(PoolService.ROLE);</span>
}
<span class="fc" id="L303"> injectTurbineTool(object, pool, field, (TurbineTool) a);</span>
}
}
<span class="pc bpc" id="L306" title="1 of 2 branches missed."> if (isTurbineService)</span>
{
<span class="nc bnc" id="L308" title="All 2 branches missed."> if (field.getType().isAnnotationPresent(TurbineService.class)) {</span>
<span class="nc" id="L309"> TurbineService service = field.getType().getAnnotation(TurbineService.class);</span>
<span class="nc" id="L310"> log.debug(&quot;retrieved implicit class annotation: &quot;+ service);</span>
<span class="nc bnc" id="L311" title="All 2 branches missed."> if (manager == null)</span>
{
<span class="nc" id="L313"> manager = TurbineServices.getInstance();</span>
}
<span class="nc" id="L315"> injectTurbineService(object, manager, field, service);</span>
}
}
}
<span class="fc bfc" id="L320" title="All 2 branches covered."> if (hasTurbineServicesInMethodFields) {</span>
<span class="fc" id="L321"> manager = processMethods(object, manager, clazz, isTurbineService);</span>
}
<span class="fc" id="L324"> clazz = clazz.getSuperclass();</span>
<span class="fc" id="L325"> }</span>
<span class="fc" id="L326"> }</span>
private static ServiceManager processMethods(Object object, ServiceManager manager, Class&lt;?&gt; clazz, boolean isTurbineService) throws TurbineException {
<span class="fc" id="L329"> Method[] methods = clazz.getMethods();</span>
<span class="fc bfc" id="L331" title="All 2 branches covered."> for (Method method : methods)</span>
{
<span class="fc" id="L333"> Annotation[] annotations = getAnnotations(method);</span>
<span class="fc bfc" id="L334" title="All 2 branches covered."> for (Annotation a : annotations)</span>
{
<span class="fc bfc" id="L336" title="All 2 branches covered."> if (a instanceof TurbineService)</span>
{
<span class="fc bfc" id="L339" title="All 2 branches covered."> if (manager == null)</span>
{
<span class="fc" id="L341"> manager = TurbineServices.getInstance();</span>
}
<span class="fc" id="L343"> injectTurbineService(object, manager, method, (TurbineService) a);</span>
}
}
<span class="pc bpc" id="L346" title="1 of 2 branches missed."> if (isTurbineService)</span>
{
<span class="nc bnc" id="L348" title="All 2 branches missed."> if (manager == null)</span>
{
<span class="nc" id="L350"> manager = TurbineServices.getInstance();</span>
}
<span class="nc" id="L352"> Class&lt;?&gt;[] classes = method.getParameterTypes();</span>
<span class="nc bnc" id="L353" title="All 2 branches missed."> for (Class&lt;?&gt; c : classes)</span>
{
<span class="nc bnc" id="L355" title="All 2 branches missed."> if ( c.isAnnotationPresent(TurbineService.class)) {</span>
<span class="nc" id="L356"> TurbineService service = c.getAnnotation(TurbineService.class);</span>
<span class="nc" id="L357"> log.debug(&quot;retrieved implicit service in Turbien service: &quot;+ service);</span>
<span class="nc" id="L358"> injectTurbineService(object, manager, method, service);</span>
}
}
}
}
<span class="fc" id="L364"> return manager;</span>
}
/**
* Inject Turbine loader into field of object
*
* @param object the object to process
* @param assembler AssemblerBrokerService, provides the loader
* @param field the field
* @param annotation the value of the annotation
*
* @throws TurbineException if loader cannot be set
*/
private static void injectTurbineLoader(Object object, AssemblerBrokerService assembler, Field field, TurbineLoader annotation) throws TurbineException
{
<span class="fc" id="L379"> Loader&lt;?&gt; loader = assembler.getLoader(annotation.value());</span>
<span class="fc" id="L380"> field.setAccessible(true);</span>
try
{
<span class="fc" id="L384"> log.debug(&quot;Injection of {} into object {}&quot;, loader, object);</span>
<span class="fc" id="L386"> field.set(object, loader);</span>
}
<span class="nc" id="L388"> catch (IllegalArgumentException | IllegalAccessException e)</span>
{
<span class="nc" id="L390"> throw new TurbineException(&quot;Could not inject loader &quot;</span>
+ loader + &quot; into object &quot; + object, e);
<span class="fc" id="L392"> }</span>
<span class="fc" id="L393"> }</span>
/**
* Inject Turbine tool into field of object and
* injects annotations provided in the tool.
*
* @param object the object to process
* @param pool PoolService, provides the pool
* @param field the field
* @param annotation the value of the annotation
*
* @throws TurbineException if loader cannot be set
*/
private static void injectTurbineTool(Object object, PoolService pool, Field field, TurbineTool annotation) throws TurbineException
{
<span class="fc" id="L408"> Object tool = null;</span>
try
{
<span class="fc" id="L411"> tool = pool.getInstance(annotation.value());</span>
// inject annotations in tool
<span class="fc" id="L413"> process(tool);</span>
<span class="fc" id="L415"> field.setAccessible(true);</span>
<span class="fc" id="L416"> log.debug(&quot;Injection of {} into object {}&quot;, tool, object);</span>
<span class="fc" id="L418"> field.set(object, tool);</span>
}
<span class="nc" id="L420"> catch (PoolException | IllegalArgumentException | IllegalAccessException e)</span>
{
<span class="nc" id="L422"> throw new TurbineException(&quot;Could not inject tool &quot;</span>
+ tool + &quot; into object &quot; + object, e);
<span class="fc" id="L424"> } </span>
<span class="fc" id="L425"> }</span>
/**
* Inject Turbine configuration into field of object
*
* @param object the object to process
* @param conf the configuration to use
* @param field the field
* @param annotation the value of the annotation
*
* @throws TurbineException if configuration cannot be set
*/
@SuppressWarnings(&quot;boxing&quot;)
private static void injectTurbineConfiguration(Object object, Configuration conf, Field field, TurbineConfiguration annotation) throws TurbineException
{
<span class="fc" id="L440"> Class&lt;?&gt; type = field.getType();</span>
<span class="fc" id="L441"> String key = annotation.value();</span>
try
{
<span class="fc bfc" id="L445" title="All 2 branches covered."> if (Configuration.class.isAssignableFrom(type))</span>
{
final Configuration injectConfiguration;
// Check for annotation value
<span class="fc bfc" id="L449" title="All 2 branches covered."> if (StringUtils.isNotEmpty(key))</span>
{
<span class="fc" id="L451"> injectConfiguration = conf.subset(key);</span>
}
else
{
<span class="fc" id="L455"> injectConfiguration = conf;</span>
}
<span class="fc" id="L458"> log.debug(&quot;Injection of {} into object {}&quot;, injectConfiguration, object);</span>
<span class="fc" id="L460"> field.setAccessible(true);</span>
<span class="fc" id="L461"> field.set(object, injectConfiguration);</span>
<span class="fc" id="L462"> }</span>
<span class="fc bfc" id="L463" title="All 2 branches covered."> else if (conf.containsKey(key))</span>
{
<span class="fc bfc" id="L465" title="All 2 branches covered."> if ( String.class.isAssignableFrom( type ) )</span>
{
<span class="fc" id="L467"> String value = conf.getString(key);</span>
<span class="fc" id="L468"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="fc" id="L470"> field.setAccessible(true);</span>
<span class="fc" id="L471"> field.set(object, value);</span>
<span class="fc" id="L472"> }</span>
<span class="fc bfc" id="L473" title="All 2 branches covered."> else if ( Boolean.TYPE.isAssignableFrom( type ) )</span>
{
<span class="fc" id="L475"> boolean value = conf.getBoolean(key);</span>
<span class="fc" id="L476"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="fc" id="L478"> field.setAccessible(true);</span>
<span class="fc" id="L479"> field.setBoolean(object, value);</span>
<span class="fc" id="L480"> }</span>
<span class="fc bfc" id="L481" title="All 2 branches covered."> else if ( Integer.TYPE.isAssignableFrom( type ) )</span>
{
<span class="fc" id="L483"> int value = conf.getInt(key);</span>
<span class="fc" id="L484"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="fc" id="L486"> field.setAccessible(true);</span>
<span class="fc" id="L487"> field.setInt(object, value);</span>
<span class="fc" id="L488"> }</span>
<span class="pc bpc" id="L489" title="1 of 2 branches missed."> else if ( Long.TYPE.isAssignableFrom( type ) )</span>
{
<span class="nc" id="L491"> long value = conf.getLong(key);</span>
<span class="nc" id="L492"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="nc" id="L494"> field.setAccessible(true);</span>
<span class="nc" id="L495"> field.setLong(object, value);</span>
<span class="nc" id="L496"> }</span>
<span class="pc bpc" id="L497" title="1 of 2 branches missed."> else if ( Short.TYPE.isAssignableFrom( type ) )</span>
{
<span class="nc" id="L499"> short value = conf.getShort(key);</span>
<span class="nc" id="L500"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="nc" id="L502"> field.setAccessible(true);</span>
<span class="nc" id="L503"> field.setShort(object, value);</span>
<span class="nc" id="L504"> }</span>
<span class="pc bpc" id="L505" title="1 of 2 branches missed."> else if ( Long.TYPE.isAssignableFrom( type ) )</span>
{
<span class="nc" id="L507"> long value = conf.getLong(key);</span>
<span class="nc" id="L508"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="nc" id="L510"> field.setAccessible(true);</span>
<span class="nc" id="L511"> field.setLong(object, value);</span>
<span class="nc" id="L512"> }</span>
<span class="pc bpc" id="L513" title="1 of 2 branches missed."> else if ( Float.TYPE.isAssignableFrom( type ) )</span>
{
<span class="nc" id="L515"> float value = conf.getFloat(key);</span>
<span class="nc" id="L516"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="nc" id="L518"> field.setAccessible(true);</span>
<span class="nc" id="L519"> field.setFloat(object, value);</span>
<span class="nc" id="L520"> }</span>
<span class="pc bpc" id="L521" title="1 of 2 branches missed."> else if ( Double.TYPE.isAssignableFrom( type ) )</span>
{
<span class="nc" id="L523"> double value = conf.getDouble(key);</span>
<span class="nc" id="L524"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="nc" id="L526"> field.setAccessible(true);</span>
<span class="nc" id="L527"> field.setDouble(object, value);</span>
<span class="nc" id="L528"> }</span>
<span class="pc bpc" id="L529" title="1 of 2 branches missed."> else if ( Byte.TYPE.isAssignableFrom( type ) )</span>
{
<span class="nc" id="L531"> byte value = conf.getByte(key);</span>
<span class="nc" id="L532"> log.debug(&quot;Injection of key {} into object {}&quot;, value, object);</span>
<span class="nc" id="L534"> field.setAccessible(true);</span>
<span class="nc" id="L535"> field.setByte(object, value);</span>
<span class="nc" id="L536"> }</span>
<span class="pc bpc" id="L537" title="1 of 2 branches missed."> else if ( List.class.isAssignableFrom( type ) )</span>
{
<span class="fc" id="L539"> List&lt;Object&gt; values = conf.getList(key);</span>
<span class="fc" id="L540"> log.debug(&quot;Injection of key {} into object {}&quot;, values, object);</span>
<span class="fc" id="L542"> field.setAccessible(true);</span>
<span class="fc" id="L543"> field.set(object, values);</span>
<span class="fc" id="L544"> } else {</span>
<span class="nc" id="L545"> throw new TurbineException(&quot;Could not inject type &quot; + </span>
type + &quot; into object &quot; + object + &quot;. Type &quot;+ type + &quot; not assignable in configuration &quot;
<span class="nc" id="L547"> + conf + &quot; (allowed: String, Boolean, List, Number Types, &quot;+ Configuration.class.getName() + &quot;).&quot;);</span>
}
} else {
<span class="fc" id="L550"> field.setAccessible(true);</span>
<span class="fc" id="L551"> Object defaultValue = field.get(object);</span>
// this should not throw an error as it might be set later from container e. g. session.timeout
// we might check field.get&lt;Type&gt; to show the default value of the field, but this is only a guess, it might be set even later..
<span class="fc" id="L554"> log.info(&quot;No key {} of type {} injected into object {}. Field {} is set to default {}.&quot;, key, type, object, field.getName(), defaultValue);</span>
}
}
<span class="nc" id="L557"> catch (IllegalArgumentException | IllegalAccessException e)</span>
{
<span class="nc" id="L559"> throw new TurbineException(&quot;Could not inject configuration &quot;</span>
+ conf + &quot; into object &quot; + object, e);
<span class="fc" id="L561"> }</span>
<span class="fc" id="L562"> }</span>
/**
* Inject Turbine service into field of object
*
* @param object the object to process
* @param manager the service manager
* @param field the field
* @param annotation the value of the annotation
*
* @throws TurbineException if service is not available
*/
private static void injectTurbineService(Object object, ServiceManager manager, Field field, TurbineService annotation) throws TurbineException
{
<span class="fc" id="L576"> String serviceName = null;</span>
// Check for annotation value
<span class="pc bpc" id="L578" title="2 of 4 branches missed."> if (annotation != null &amp;&amp; StringUtils.isNotEmpty(annotation.value()))</span>
{
<span class="nc" id="L580"> serviceName = annotation.value();</span>
}
// Check for fields SERVICE_NAME and ROLE
else
{
// check field level annotation
<span class="fc" id="L586"> Field[] typeFields = field.getType().getFields();</span>
<span class="fc" id="L587"> serviceName = checkServiceOrRoleInField(serviceName, typeFields);</span>
// if it is the default Service, we check class level annotation
<span class="pc bpc" id="L589" title="2 of 4 branches missed."> if ( (serviceName == null || serviceName.equals(Service.SERVICE_NAME)) &amp;&amp;</span>
<span class="nc bnc" id="L590" title="All 2 branches missed."> field.getType().isAnnotationPresent(TurbineService.class)) {</span>
<span class="nc" id="L591"> TurbineService service = field.getType().getAnnotation(TurbineService.class);</span>
<span class="nc" id="L592"> log.debug(&quot;retrieved class annotation: &quot;+ service);</span>
<span class="nc" id="L593"> serviceName = service.value();</span>
}
}
<span class="pc bpc" id="L597" title="1 of 2 branches missed."> if (StringUtils.isEmpty(serviceName))</span>
{
// Try interface class name (e.g. used by Fulcrum)
<span class="nc" id="L600"> serviceName = field.getType().getName();</span>
}
<span class="fc" id="L603"> log.debug(&quot;Looking up service for injection: {} for object {}&quot;, serviceName, object);</span>
<span class="fc" id="L605"> Object service = manager.getService(serviceName); // throws Exception on unknown service</span>
<span class="fc" id="L606"> field.setAccessible(true);</span>
try
{
<span class="fc" id="L610"> log.debug(&quot;Injection of {} into object {}&quot;, serviceName, object);</span>
<span class="fc" id="L612"> field.set(object, service);</span>
}
<span class="nc" id="L614"> catch (IllegalArgumentException | IllegalAccessException e)</span>
{
<span class="nc" id="L616"> throw new TurbineException(&quot;Could not inject service &quot;</span>
+ serviceName + &quot; into object &quot; + object, e);
<span class="fc" id="L618"> }</span>
<span class="fc" id="L619"> }</span>
/**
* Injects Turbine service into method fields
*
* @param object the object to process
* @param manager the service manager
* @param method The method
* @param annotation the value of the annotation
* @throws TurbineException - If service could not be injected.
*/
private static void injectTurbineService(Object object, ServiceManager manager, Method method, TurbineService annotation) throws TurbineException
{
<span class="fc" id="L632"> String serviceName = null;</span>
// Check for annotation value
<span class="pc bpc" id="L634" title="2 of 4 branches missed."> if (annotation != null &amp;&amp; StringUtils.isNotEmpty(annotation.value()))</span>
{
<span class="nc" id="L636"> serviceName = annotation.value();</span>
}
else
{
<span class="fc" id="L640"> Class&lt;?&gt;[] classes = method.getParameterTypes();</span>
<span class="fc bfc" id="L641" title="All 2 branches covered."> for (Class&lt;?&gt; c : classes)</span>
{
<span class="fc" id="L643"> Field[] fields = c.getFields();</span>
// Check for fields SERVICE_NAME and ROLE
<span class="fc" id="L645"> serviceName = checkServiceOrRoleInField(serviceName, fields);</span>
<span class="pc bpc" id="L647" title="2 of 4 branches missed."> if ( (serviceName == null || serviceName.equals(Service.SERVICE_NAME)) &amp;&amp;</span>
<span class="nc bnc" id="L648" title="All 2 branches missed."> c.isAnnotationPresent(TurbineService.class)) {</span>
<span class="nc" id="L649"> TurbineService service = c.getAnnotation(TurbineService.class);</span>
<span class="nc" id="L650"> log.debug(&quot;retrieved class annotation: &quot;+ service);</span>
<span class="nc" id="L651"> serviceName = service.value();</span>
}
}
}
<span class="fc" id="L657"> log.debug(&quot;Looking up service for injection: {} for object {}&quot;, serviceName, object);</span>
<span class="pc bpc" id="L658" title="1 of 2 branches missed."> if (StringUtils.isEmpty(serviceName))</span>
{
// Try interface class name
<span class="nc" id="L661"> serviceName = method.getName();</span>
}
<span class="fc" id="L664"> Object service = manager.getService(serviceName); // throws Exception on unknown service</span>
<span class="fc" id="L665"> method.setAccessible(true);</span>
try
{
<span class="fc" id="L669"> log.debug(&quot;Injection of {} into object {}&quot;, serviceName, object);</span>
<span class="fc" id="L671"> Object[] paramValues = new Object[1];</span>
<span class="fc" id="L672"> paramValues[0] = service;</span>
<span class="fc" id="L673"> method.invoke(object, paramValues);</span>
}
<span class="nc" id="L675"> catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e)</span>
{
<span class="nc" id="L677"> throw new TurbineException(&quot;Could not inject service &quot;</span>
+ serviceName + &quot; into object &quot; + object, e);
<span class="fc" id="L679"> }</span>
<span class="fc" id="L680"> }</span>
private static String checkServiceOrRoleInField(String serviceName, Field[] fields) {
<span class="pc bpc" id="L683" title="1 of 2 branches missed."> for (Field f : fields)</span>
<span class="fc bfc" id="L684" title="All 2 branches covered."> if (TurbineService.SERVICE_NAME.equals(f.getName()))</span>
{
try
{
<span class="fc" id="L688"> serviceName = (String)f.get(null);</span>
}
<span class="nc" id="L690"> catch (IllegalArgumentException | IllegalAccessException e)</span>
{
<span class="nc" id="L692"> continue;</span>
<span class="fc" id="L693"> }</span>
break;
}
<span class="pc bpc" id="L696" title="1 of 2 branches missed."> else if (TurbineService.ROLE.equals(f.getName()))</span>
{
try
{
<span class="fc" id="L700"> serviceName = (String)f.get(null);</span>
}
<span class="nc" id="L702"> catch (IllegalArgumentException | IllegalAccessException e)</span>
{
<span class="nc" id="L704"> continue;</span>
<span class="fc" id="L705"> }</span>
break;
}
<span class="fc" id="L708"> return serviceName;</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>