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

File StandardEnvironment.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

8
19
4
1
113
56
10
0.53
4.75
4
2.5

Classes

Class Line # Actions
StandardEnvironment 40 19 0% 10 7
0.774193577.4%
 

Contributing tests

This file is covered by 7 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.environment.internal;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.net.MalformedURLException;
26    import java.net.URL;
27   
28    import javax.inject.Singleton;
29   
30    import org.xwiki.component.annotation.Component;
31   
32    /**
33    * Defines what an Environment means in Java SE.
34    *
35    * @version $Id: e7672ae7a287905009bddce13fab26de9c72bdfa $
36    * @since 3.5M1
37    */
38    @Component
39    @Singleton
 
40    public class StandardEnvironment extends AbstractEnvironment
41    {
42    /**
43    * @see #setResourceDirectory(java.io.File)
44    */
45    private File resourceDirectory;
46   
47    /**
48    * @see #setResourceClassLoader(ClassLoader)
49    */
50    private ClassLoader resourceClassLoader = StandardEnvironment.class.getClassLoader();
51   
52    /**
53    * @param resourceDirectory the directory where resources such as configuration files (actually any content that
54    * is neither temporary nor permanent data) which will point to the permanent directory
55    * if not set
56    */
 
57  33 toggle public void setResourceDirectory(File resourceDirectory)
58    {
59  33 this.resourceDirectory = resourceDirectory;
60    }
61   
62    /**
63    * @param classLoader The Class Loader used to load resources when they're not found in the Resources directory
64    * (or if it's not set). Defaults to the Class Loader used to load this class if not set.
65    */
 
66  0 toggle public void setResourceClassLoader(ClassLoader classLoader)
67    {
68  0 this.resourceClassLoader = classLoader;
69    }
70   
 
71  135 toggle @Override
72    public InputStream getResourceAsStream(String resourceName)
73    {
74  135 InputStream resourceStream = null;
75   
76  135 URL resourceURL = getResource(resourceName);
77  135 if (resourceURL != null) {
78  0 try {
79  0 resourceStream = resourceURL.openStream();
80    } catch (IOException e) {
81  0 this.logger.debug("Failed to get Input stream for resource [{}]", resourceName, e);
82    }
83    }
84   
85  135 return resourceStream;
86    }
87   
 
88  195 toggle @Override
89    public URL getResource(String resourceName)
90    {
91  195 URL resourceURL = null;
92   
93    // Try to find the resource in the Resources directory (if set)
94  195 if (this.resourceDirectory != null) {
95  2 try {
96    // Verify if the File exists
97  2 File resourceFile = new File(this.resourceDirectory, resourceName);
98  2 if (resourceFile.exists()) {
99  1 resourceURL = resourceFile.toURI().toURL();
100    }
101    } catch (MalformedURLException e) {
102  0 this.logger.debug("Failed to access resource [{}]", resourceName, e);
103    }
104    }
105   
106    // If not found, try in the Resource Class Loader
107  195 if (resourceURL == null) {
108  194 resourceURL = this.resourceClassLoader.getResource(resourceName);
109    }
110   
111  195 return resourceURL;
112    }
113    }