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

File CoreExtensionCache.java

 

Coverage histogram

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

Code metrics

20
47
5
1
177
106
23
0.49
9.4
5
4.6

Classes

Class Line # Actions
CoreExtensionCache 48 47 0% 23 9
0.87587.5%
 

Contributing tests

This file is covered by 1 test. .

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.extension.repository.internal.core;
21   
22    import java.io.File;
23    import java.io.FileInputStream;
24    import java.io.FileOutputStream;
25    import java.io.IOException;
26    import java.net.URL;
27   
28    import javax.inject.Inject;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.codec.digest.DigestUtils;
32    import org.slf4j.Logger;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.phase.Initializable;
35    import org.xwiki.component.phase.InitializationException;
36    import org.xwiki.environment.Environment;
37    import org.xwiki.extension.internal.PathUtils;
38    import org.xwiki.extension.repository.internal.ExtensionSerializer;
39   
40    /**
41    * Store resolve core extension to not have to resolve it again at next restart.
42    *
43    * @version $Id: 0ae7e1b5024b093b0584f254ff44703cc5db2be1 $
44    * @since 6.4M1
45    */
46    @Component(roles = CoreExtensionCache.class)
47    @Singleton
 
48    public class CoreExtensionCache implements Initializable
49    {
50    /**
51    * The String to search in a descriptor URL to know if it's inside a jar or another packaged file.
52    */
53    private static final String PACKAGE_MARKER = "!/";
54   
55    @Inject
56    private Environment environment;
57   
58    @Inject
59    private ExtensionSerializer serializer;
60   
61    @Inject
62    private Logger logger;
63   
64    private File folder;
65   
 
66  87 toggle @Override
67    public void initialize() throws InitializationException
68    {
69  87 File permanentDirectory = this.environment.getPermanentDirectory();
70  87 if (permanentDirectory != null) {
71  83 this.folder = new File(permanentDirectory, "cache/extension/core/");
72    }
73    }
74   
75    /**
76    * @param extension the extension to store
77    * @throws Exception when failing to store the extension
78    */
 
79  274 toggle public void store(DefaultCoreExtension extension) throws Exception
80    {
81  274 if (this.folder == null) {
82  20 return;
83    }
84   
85  254 URL descriptorURL = extension.getDescriptorURL();
86   
87  254 if (!descriptorURL.getPath().contains(PACKAGE_MARKER)) {
88    // Usually mean jars are not kept, don't cache that or it's going to be a nightmare when upgrading
89  1 return;
90    }
91   
92  253 File file = getFile(descriptorURL);
93   
94    // Make sure the file parents exist
95  253 if (!file.exists()) {
96  253 file.getParentFile().mkdirs();
97    }
98   
99  253 try (FileOutputStream stream = new FileOutputStream(file)) {
100  253 this.serializer.saveExtensionDescriptor(extension, stream);
101    }
102    }
103   
104    /**
105    * @param repository the repository to set in the new extension instance
106    * @param descriptorURL the extension descriptor URL
107    * @return the extension corresponding to the passed descriptor URL, null if none could be found
108    */
 
109  11467 toggle public DefaultCoreExtension getExtension(DefaultCoreExtensionRepository repository, URL descriptorURL)
110    {
111  11467 if (this.folder == null) {
112  284 return null;
113    }
114   
115  11183 if (!descriptorURL.getPath().contains(PACKAGE_MARKER)) {
116    // Usually mean jars are not kept, make sure to not take into account such a wrongly cached descriptor
117  0 return null;
118    }
119   
120  11183 File file = getFile(descriptorURL);
121   
122  11183 if (file.exists()) {
123  600 try (FileInputStream stream = new FileInputStream(file)) {
124  600 DefaultCoreExtension coreExtension =
125    this.serializer.loadCoreExtensionDescriptor(repository, descriptorURL, stream);
126   
127  600 return coreExtension;
128    } catch (Exception e) {
129  0 this.logger.warn("Failed to parse cached core extension", e);
130    }
131    }
132   
133  10583 return null;
134    }
135   
 
136  11436 toggle private String getExtensionFileName(URL url)
137    {
138  11436 URL extensionURL;
139  11436 try {
140  11436 extensionURL = PathUtils.getExtensionURL(url);
141    } catch (IOException e) {
142  0 return null;
143    }
144   
145  11436 String extensionPath = extensionURL.toExternalForm();
146  11436 int index = extensionPath.lastIndexOf('/');
147  11436 if (index > 0 && index < extensionPath.length()) {
148  11436 extensionPath = extensionPath.substring(index + 1);
149   
150  11436 index = extensionPath.lastIndexOf('.');
151  11436 if (index > 0 && index < extensionPath.length()) {
152  11436 extensionPath = extensionPath.substring(0, index);
153    }
154   
155  11436 return extensionPath;
156    }
157   
158  0 return null;
159    }
160   
 
161  11436 toggle private File getFile(URL url)
162    {
163  11436 StringBuilder builder = new StringBuilder();
164   
165  11436 String fileName = getExtensionFileName(url);
166  11436 if (fileName != null) {
167  11436 builder.append(fileName);
168  11436 builder.append('-');
169    }
170   
171  11436 builder.append(DigestUtils.md5Hex(url.toExternalForm()));
172   
173  11436 builder.append(".xed");
174   
175  11436 return new File(this.folder, builder.toString());
176    }
177    }