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

File DefaultClassLoaderManager.java

 

Coverage histogram

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

Code metrics

16
31
6
1
164
96
16
0.52
5.17
6
2.67

Classes

Class Line # Actions
DefaultClassLoaderManager 47 31 0% 16 5
0.905660490.6%
 

Contributing tests

This file is covered by 23 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.internal;
21   
22    import java.io.IOException;
23    import java.net.URI;
24    import java.util.Map;
25    import java.util.concurrent.ConcurrentHashMap;
26   
27    import javax.inject.Inject;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.xwiki.classloader.ClassLoaderManager;
32    import org.xwiki.classloader.NamespaceURLClassLoader;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.manager.ComponentLifecycleException;
35    import org.xwiki.component.phase.Disposable;
36    import org.xwiki.component.phase.Initializable;
37    import org.xwiki.component.phase.InitializationException;
38   
39    /**
40    * Default implementation of {@link ClassLoaderManager}.
41    *
42    * @version $Id: dffcb55c02b57c30baeedf7c5db84533eb12a935 $
43    * @since 4.0M1
44    */
45    @Component
46    @Singleton
 
47    public class DefaultClassLoaderManager implements ClassLoaderManager, Initializable, Disposable
48    {
49    /**
50    * The readonly container loader that should be used as parent of all the namespace classloader.
51    */
52    protected ClassLoader containerClassLoader;
53   
54    /**
55    * The class loader corresponding to null namespace.
56    */
57    private volatile NamespaceURLClassLoader rootClassLoader;
58   
59    @Inject
60    private Logger logger;
61   
62    /**
63    * The classloaders stored by namespace.
64    */
65    private Map<String, NamespaceURLClassLoader> wikiClassLoaderMap =
66    new ConcurrentHashMap<String, NamespaceURLClassLoader>();
67   
 
68  63 toggle @Override
69    public void initialize() throws InitializationException
70    {
71  63 this.containerClassLoader = Thread.currentThread().getContextClassLoader();
72    }
73   
74    /**
75    * Allow overriding the system classloader during tests.
76    *
77    * @return a ClassLoader to be used as the system parent
78    */
 
79  93 toggle protected ClassLoader getContainerClassLoader()
80    {
81  93 return this.containerClassLoader;
82    }
83   
 
84  4177 toggle @Override
85    public NamespaceURLClassLoader getURLClassLoader(String namespace, boolean create)
86    {
87    // Make sure root classloader exist
88  4169 if (this.rootClassLoader == null) {
89  93 synchronized (this) {
90  93 if (this.rootClassLoader == null) {
91  93 this.rootClassLoader = new NamespaceURLClassLoader(new URI[] {}, getContainerClassLoader(), null);
92    }
93    }
94    }
95   
96    // Return root classloader by default
97  4172 NamespaceURLClassLoader wikiClassLoader = this.rootClassLoader;
98   
99    // Find classloader corresponding to passed namespace
100  4164 if (namespace != null) {
101  4026 wikiClassLoader = this.wikiClassLoaderMap.get(namespace);
102   
103  4020 if (wikiClassLoader == null) {
104  590 if (create) {
105    // Create classloader
106  93 wikiClassLoader = new NamespaceURLClassLoader(new URI[] {}, this.rootClassLoader, namespace);
107   
108    // Store new classloader
109  93 this.wikiClassLoaderMap.put(namespace, wikiClassLoader);
110    } else {
111  497 wikiClassLoader = this.rootClassLoader;
112    }
113    }
114    }
115   
116  4174 return wikiClassLoader;
117    }
118   
 
119  95 toggle @Override
120    public void dropURLClassLoaders()
121    {
122  95 if (this.rootClassLoader != null) {
123  93 for (String namespace : this.wikiClassLoaderMap.keySet()) {
124  69 dropURLClassLoader(namespace);
125    }
126   
127    // Reset and close root classloader
128  93 NamespaceURLClassLoader classloader = this.rootClassLoader;
129  93 this.rootClassLoader = null;
130  93 try {
131  93 classloader.close();
132    } catch (IOException e) {
133  0 this.logger.warn("Failed to close root Classloader", e);
134    }
135    }
136    }
137   
 
138  90 toggle @Override
139    public void dropURLClassLoader(String namespace)
140    {
141  90 if (namespace == null) {
142    // If root namespace drop all namespaces
143  0 dropURLClassLoaders();
144    } else {
145    // Remove the classloader from the map
146  90 NamespaceURLClassLoader classloader = this.wikiClassLoaderMap.remove(namespace);
147   
148  90 if (classloader != null) {
149    // Close the classloader
150  89 try {
151  89 classloader.close();
152    } catch (IOException e) {
153  0 this.logger.warn("Failed to close Classloader", e);
154    }
155    }
156    }
157    }
158   
 
159  86 toggle @Override
160    public void dispose() throws ComponentLifecycleException
161    {
162  86 dropURLClassLoaders();
163    }
164    }