1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.script.internal.safe

File DefaultScriptSafeProvider.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

14
31
3
1
138
76
12
0.39
10.33
3
4

Classes

Class Line # Actions
DefaultScriptSafeProvider 46 31 0% 12 6
0.87587.5%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.script.internal.safe;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.lang.reflect.Type;
24    import java.util.Arrays;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.util.DefaultParameterizedType;
35    import org.xwiki.component.util.ReflectionUtils;
36   
37    /**
38    * Find the right safe provider for the passed object.
39    *
40    * @version $Id: 2872afab6e5402d7ff3ec03cd1a8aeb0196faa18 $
41    * @since 4.0M2
42    */
43    @Component
44    @Singleton
45    @SuppressWarnings("rawtypes")
 
46    public class DefaultScriptSafeProvider implements ScriptSafeProvider
47    {
48    /**
49    * Used to lookup {@link ScriptSafeProvider} implementations.
50    */
51    @Inject
52    private ComponentManager component;
53   
54    /**
55    * The logger.
56    */
57    @Inject
58    private Logger logger;
59   
 
60  8689 toggle @Override
61    public Object get(Object unsafe)
62    {
63  8689 if (unsafe == null) {
64  287 return null;
65    }
66   
67  8402 Object safe = get(unsafe, Arrays.<Type>asList(unsafe.getClass()));
68   
69  8402 if (safe == null) {
70  82 safe = unsafe;
71    }
72   
73  8402 return safe;
74    }
75   
76    /**
77    * @param unsafe the unsafe version of the object
78    * @param types the types implemented or extended by the object for which to search a provider
79    * @return a safe version of the passed object, null if none could be provided
80    */
 
81  26943 toggle private Object get(Object unsafe, List<Type> types)
82    {
83  26944 for (Type type : types) {
84  36768 Object safe = get(unsafe, type);
85   
86  36766 if (safe != null) {
87  8319 return safe;
88    }
89    }
90   
91  18623 for (Type type : types) {
92  18542 Object safe = get(unsafe, ReflectionUtils.getDirectTypes(type));
93   
94  18539 if (safe != null) {
95  17870 return safe;
96    }
97    }
98   
99  751 return null;
100    }
101   
102    /**
103    * @param unsafe the unsafe version of the object
104    * @param type the type implemented or extended by the object for which to search a provider
105    * @return a safe version of the passed object, null if none could be provided
106    */
 
107  36768 toggle private Object get(Object unsafe, Type type)
108    {
109  36768 Type completeRole = new DefaultParameterizedType(null, ScriptSafeProvider.class, type);
110   
111  36765 if (this.component.hasComponent(completeRole)) {
112  8320 try {
113  8319 ScriptSafeProvider<Object> provider = this.component.getInstance(completeRole);
114   
115  8320 return provider.get(unsafe);
116    } catch (ComponentLookupException e) {
117  0 this.logger.error("Failed to load safe provider for complete type [{}]", type, e);
118    }
119  28446 } else if (type instanceof ParameterizedType) {
120  404 Type rawType = ((ParameterizedType) type).getRawType();
121  404 Type rawRole = new DefaultParameterizedType(null, ScriptSafeProvider.class, rawType);
122   
123  404 this.logger.debug("Could not find any safe provider for type [{}]. Trying with [{}]", type, rawType);
124   
125  404 if (this.component.hasComponent(rawRole)) {
126  0 try {
127  0 ScriptSafeProvider<Object> provider = this.component.getInstance(rawRole);
128   
129  0 return provider.get(unsafe);
130    } catch (ComponentLookupException e) {
131  0 this.logger.error("Failed to load safe provider for raw type [{}]", rawType, e);
132    }
133    }
134    }
135   
136  28445 return null;
137    }
138    }