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

File DefaultLocalExtensionRepository.java

 

Coverage histogram

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

Code metrics

6
41
11
1
232
141
19
0.46
3.73
11
1.73

Classes

Class Line # Actions
DefaultLocalExtensionRepository 57 41 0% 19 8
0.8620689586.2%
 

Contributing tests

This file is covered by 114 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.extension.repository.internal.local;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.util.Collection;
25    import java.util.Collections;
26    import java.util.Map;
27   
28    import javax.inject.Inject;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.io.FileUtils;
32    import org.slf4j.Logger;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.manager.ComponentLookupException;
35    import org.xwiki.component.manager.ComponentManager;
36    import org.xwiki.component.phase.Initializable;
37    import org.xwiki.component.phase.InitializationException;
38    import org.xwiki.extension.Extension;
39    import org.xwiki.extension.ExtensionId;
40    import org.xwiki.extension.ExtensionManagerConfiguration;
41    import org.xwiki.extension.LocalExtension;
42    import org.xwiki.extension.ResolveException;
43    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
44    import org.xwiki.extension.repository.LocalExtensionRepository;
45    import org.xwiki.extension.repository.LocalExtensionRepositoryException;
46    import org.xwiki.extension.repository.internal.AbstractCachedExtensionRepository;
47   
48    /**
49    * Default implementation of {@link LocalExtensionRepository}.
50    *
51    * @version $Id: 16b94baf647e1a7023811a0d4543e74001ba1f7a $
52    * @since 4.0M1
53    */
54    @Component
55    @Singleton
56    // TODO: make it threadsafe bulletproofs
 
57    public class DefaultLocalExtensionRepository extends AbstractCachedExtensionRepository<DefaultLocalExtension>
58    implements LocalExtensionRepository, Initializable
59    {
60    private static final String ID = "local";
61   
62    /**
63    * Used to get repository path.
64    */
65    @Inject
66    private transient ExtensionManagerConfiguration configuration;
67   
68    /**
69    * The logger to log.
70    */
71    @Inject
72    private transient Logger logger;
73   
74    /**
75    * The component manager.
76    */
77    @Inject
78    private transient ComponentManager componentManager;
79   
80    /**
81    * Used to manipulate filesystem repository storage.
82    */
83    private transient LocalExtensionStorage storage;
84   
85    /**
86    * Make the repository ignore features.
87    */
 
88  231 toggle public DefaultLocalExtensionRepository()
89    {
90  231 super(true);
91    }
92   
 
93  231 toggle @Override
94    public void initialize() throws InitializationException
95    {
96  231 try {
97  231 this.storage =
98    new LocalExtensionStorage(this, this.configuration.getLocalRepository(), this.componentManager);
99    } catch (ComponentLookupException e) {
100  0 throw new InitializationException("Failed to intialize local extension storage", e);
101    }
102   
103  231 setDescriptor(new DefaultExtensionRepositoryDescriptor(ID, ID, this.storage.getRootFolder().toURI()));
104   
105  231 try {
106  231 this.storage.loadExtensions();
107    } catch (IOException e) {
108  0 throw new InitializationException("Failed to load load etensions", e);
109    }
110    }
111   
112    /**
113    * Register a new local extension.
114    *
115    * @param localExtension the new local extension
116    */
 
117  1485 toggle protected void addLocalExtension(DefaultLocalExtension localExtension)
118    {
119  1485 addCachedExtension(localExtension);
120    }
121   
122    // LocalRepository
123   
 
124  615 toggle @Override
125    public LocalExtension getLocalExtension(ExtensionId extensionId)
126    {
127  615 return this.extensions.get(extensionId);
128    }
129   
 
130  693 toggle @Override
131    public Collection<LocalExtension> getLocalExtensions()
132    {
133  693 return Collections.<LocalExtension>unmodifiableCollection(this.extensions.values());
134    }
135   
 
136  47 toggle @Override
137    public Collection<LocalExtension> getLocalExtensionVersions(String id)
138    {
139  47 Collection<DefaultLocalExtension> versions = this.extensionsVersions.get(id);
140   
141  47 return versions != null ? Collections.<LocalExtension>unmodifiableCollection(versions)
142    : Collections.<LocalExtension>emptyList();
143    }
144   
145    /**
146    * Create a new local extension from a remote extension.
147    *
148    * @param extension the extension to copy
149    * @return the new local extension
150    */
 
151  148 toggle private DefaultLocalExtension createExtension(Extension extension)
152    {
153  148 DefaultLocalExtension localExtension = new DefaultLocalExtension(this, extension);
154   
155  148 localExtension.setFile(this.storage.getNewExtensionFile(localExtension.getId(), localExtension.getType()));
156   
157  148 return localExtension;
158    }
159   
 
160  1 toggle @Override
161    public int countExtensions()
162    {
163  1 return this.extensions.size();
164    }
165   
 
166  148 toggle @Override
167    public LocalExtension storeExtension(Extension extension) throws LocalExtensionRepositoryException
168    {
169  148 DefaultLocalExtension localExtension = this.extensions.get(extension.getId());
170   
171  148 if (localExtension == null) {
172  148 try {
173  148 localExtension = createExtension(extension);
174   
175  148 InputStream is = extension.getFile().openStream();
176  148 try {
177  148 FileUtils.copyInputStreamToFile(is, localExtension.getFile().getFile());
178    } finally {
179  148 is.close();
180    }
181  148 this.storage.saveDescriptor(localExtension);
182   
183    // Cache extension
184  148 addLocalExtension(localExtension);
185    } catch (Exception e) {
186    // TODO: clean
187   
188  0 throw new LocalExtensionRepositoryException("Failed to save extension [" + extension + "] descriptor",
189    e);
190    }
191    } else {
192  0 throw new LocalExtensionRepositoryException(
193    "Extension [" + extension + "] already exists in local repository");
194    }
195   
196  148 return localExtension;
197    }
198   
 
199  326 toggle @Override
200    public void setProperties(LocalExtension localExtension, Map<String, Object> properties)
201    throws LocalExtensionRepositoryException
202    {
203  326 DefaultLocalExtension extension = this.extensions.get(localExtension.getId());
204   
205  326 if (extension != null) {
206  326 extension.setProperties(properties);
207  326 try {
208  326 this.storage.saveDescriptor(extension);
209    } catch (Exception e) {
210  0 throw new LocalExtensionRepositoryException(
211    "Failed to save descriptor for extension [" + localExtension + "]", e);
212    }
213    }
214    }
215   
 
216  13 toggle @Override
217    public void removeExtension(LocalExtension extension) throws ResolveException
218    {
219  13 DefaultLocalExtension localExtension = resolve(extension.getId());
220   
221  13 try {
222  13 this.storage.removeExtension(localExtension);
223    } catch (IOException e) {
224    // Should not happen if the local extension exists
225   
226  0 this.logger.error("Failed to remove extension [" + extension + "]", e);
227    }
228   
229    // Remove the extension from the caches
230  13 removeCachedExtension(localExtension);
231    }
232    }