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

File ContextNamespaceURLClassLoader.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

6
26
18
1
194
117
22
0.85
1.44
18
1.22

Classes

Class Line # Actions
ContextNamespaceURLClassLoader 40 26 0% 22 24
0.5252%
 

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.classloader.xwiki.internal;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.net.URI;
25    import java.net.URL;
26    import java.util.Enumeration;
27    import java.util.List;
28    import java.util.Objects;
29   
30    import org.xwiki.classloader.ClassLoaderManager;
31    import org.xwiki.classloader.NamespaceURLClassLoader;
32    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
33   
34    /**
35    * Dynamic classloader which select the current class loader based on context wiki each time it's called.
36    *
37    * @version $Id: 6b30e095d789901db60a5a33600bbdbb8d8bcd6f $
38    * @since 6.4M2
39    */
 
40    public class ContextNamespaceURLClassLoader extends NamespaceURLClassLoader
41    {
42    private WikiDescriptorManager wikis;
43   
44    private final ClassLoaderManager classLoaderManager;
45   
46    private String cachedCurrentWiki;
47   
48    private NamespaceURLClassLoader currentClassLoader;
49   
50    /**
51    * @param wikis the component used to access current wiki
52    * @param classLoaderManager the component used to get the ClassLoader associated to current wiki
53    * @since 7.2M1
54    */
 
55  13670 toggle public ContextNamespaceURLClassLoader(WikiDescriptorManager wikis, ClassLoaderManager classLoaderManager)
56    {
57    // Note: it's important to set the parent CL to be the Context CL since some third party frameworks can use
58    // that information. For example the Apache Naming (JNDI) implementation will get the context CL to check if
59    // there's any NamingContext instance bound to it and at Tomcat's init (for example), Tomcat binds the defined
60    // DataSource to the Tomcat CL (WebappClassLoader). Thus if we loose the path from our CL to the Tomcat CL, all
61    // the DataSources defined in Tomcat will fail to be usable from our Hibernate code.
62  13678 super(new URI[] {}, Thread.currentThread().getContextClassLoader(), null);
63   
64  13674 this.wikis = wikis;
65  13645 this.classLoaderManager = classLoaderManager;
66    }
67   
68    // TODO: Add support for context user ?
 
69  135320 toggle private NamespaceURLClassLoader getCurrentClassLoader()
70    {
71  135323 String currentWiki = this.wikis.getCurrentWikiId();
72   
73  135322 if (this.currentClassLoader == null || !Objects.equals(currentWiki, this.cachedCurrentWiki)) {
74  2129 this.currentClassLoader =
75  2130 this.classLoaderManager.getURLClassLoader(currentWiki != null ? "wiki:" + currentWiki : null, false);
76   
77  2128 if (this.currentClassLoader == null) {
78    // Fallback on system classloader in the very weird edge case where ClassLoaderManager does not return
79    // any (which is already supposed to fallback on system classloader)
80  0 this.currentClassLoader = new NamespaceURLClassLoader(new URI[] {}, getSystemClassLoader(), null);
81    }
82   
83  2127 this.cachedCurrentWiki = currentWiki;
84    }
85   
86  135318 return this.currentClassLoader;
87    }
88   
 
89  0 toggle @Override
90    public String getNamespace()
91    {
92  0 return getCurrentClassLoader().getNamespace();
93    }
94   
 
95  0 toggle @Override
96    public void addURL(URL url)
97    {
98  0 getCurrentClassLoader().addURL(url);
99    }
100   
 
101  0 toggle @Override
102    public void addURLs(List<URL> urls)
103    {
104  0 getCurrentClassLoader().addURLs(urls);
105    }
106   
 
107  0 toggle @Override
108    public URL[] getURLs()
109    {
110  0 return getCurrentClassLoader().getURLs();
111    }
112   
 
113  2305 toggle @Override
114    public URL getResource(String name)
115    {
116  2305 return getCurrentClassLoader().getResource(name);
117    }
118   
 
119  15035 toggle @Override
120    public Enumeration<URL> getResources(String name) throws IOException
121    {
122  15035 return getCurrentClassLoader().getResources(name);
123    }
124   
 
125  7884 toggle @Override
126    public InputStream getResourceAsStream(String name)
127    {
128  7884 return getCurrentClassLoader().getResourceAsStream(name);
129    }
130   
 
131  0 toggle @Override
132    public URL findResource(String name)
133    {
134  0 return getCurrentClassLoader().findResource(name);
135    }
136   
 
137  0 toggle @Override
138    public Enumeration<URL> findResources(String name) throws IOException
139    {
140  0 return getCurrentClassLoader().findResources(name);
141    }
142   
 
143  0 toggle @Override
144    public void close() throws IOException
145    {
146  0 getCurrentClassLoader().close();
147    }
148   
 
149  0 toggle @Override
150    public void clearAssertionStatus()
151    {
152  0 getCurrentClassLoader().clearAssertionStatus();
153    }
154   
 
155  0 toggle @Override
156    public void setClassAssertionStatus(String className, boolean enabled)
157    {
158  0 getCurrentClassLoader().setClassAssertionStatus(className, enabled);
159    }
160   
 
161  0 toggle @Override
162    public void setDefaultAssertionStatus(boolean enabled)
163    {
164  0 getCurrentClassLoader().setDefaultAssertionStatus(enabled);
165    }
166   
 
167  0 toggle @Override
168    public void setPackageAssertionStatus(String packageName, boolean enabled)
169    {
170  0 getCurrentClassLoader().setPackageAssertionStatus(packageName, enabled);
171    }
172   
 
173  96834 toggle @Override
174    public Class<?> loadClass(String name) throws ClassNotFoundException
175    {
176  96834 return getCurrentClassLoader().loadClass(name);
177    }
178   
179    // protected
180   
181    /**
182    * {@inheritDoc}
183    * <p>
184    * Yes it's a protected method but it's sometime called directly from {@link ClassLoader#loadClass(String, String)}.
185    * </p>
186    *
187    * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
188    */
 
189  13266 toggle @Override
190    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
191    {
192  13266 return getCurrentClassLoader().loadClass(name);
193    }
194    }