1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.repository.internal.local

File DefaultLocalExtensionRepository.java

 

Coverage histogram

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

Code metrics

6
39
11
1
228
137
19
0.49
3.55
11
1.73

Classes

Class Line # Actions
DefaultLocalExtensionRepository 57 39 0% 19 8
0.8571428785.7%
 

Contributing tests

This file is covered by 121 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: dd11ac2e6a2dd53e7ae994f59402b71a78e66df0 $
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  257 toggle public DefaultLocalExtensionRepository()
89    {
90  257 super(true);
91    }
92   
 
93  257 toggle @Override
94    public void initialize() throws InitializationException
95    {
96  257 try {
97  257 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  257 setDescriptor(new DefaultExtensionRepositoryDescriptor(ID, ID, this.storage.getRootFolder().toURI()));
104   
105  257 try {
106  257 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  3016 toggle protected void addLocalExtension(DefaultLocalExtension localExtension)
118    {
119  3016 addCachedExtension(localExtension);
120    }
121   
122    // LocalRepository
123   
 
124  1288 toggle @Override
125    public LocalExtension getLocalExtension(ExtensionId extensionId)
126    {
127  1288 return this.extensions.get(extensionId);
128    }
129   
 
130  771 toggle @Override
131    public Collection<LocalExtension> getLocalExtensions()
132    {
133  771 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  514 toggle private DefaultLocalExtension createExtension(Extension extension)
152    {
153  514 DefaultLocalExtension localExtension = new DefaultLocalExtension(this, extension);
154   
155  514 localExtension.setFile(this.storage.getNewExtensionFile(localExtension.getId(), localExtension.getType()));
156   
157  514 return localExtension;
158    }
159   
 
160  1 toggle @Override
161    public int countExtensions()
162    {
163  1 return this.extensions.size();
164    }
165   
 
166  514 toggle @Override
167    public LocalExtension storeExtension(Extension extension) throws LocalExtensionRepositoryException
168    {
169  514 DefaultLocalExtension localExtension = this.extensions.get(extension.getId());
170   
171  514 if (localExtension == null) {
172  514 try {
173  514 localExtension = createExtension(extension);
174   
175  514 InputStream is = extension.getFile().openStream();
176  514 FileUtils.copyInputStreamToFile(is, localExtension.getFile().getFile());
177  514 this.storage.saveDescriptor(localExtension);
178   
179    // Cache extension
180  514 addLocalExtension(localExtension);
181    } catch (Exception e) {
182    // TODO: clean
183   
184  0 throw new LocalExtensionRepositoryException("Failed to save extension [" + extension + "] descriptor",
185    e);
186    }
187    } else {
188  0 throw new LocalExtensionRepositoryException(
189    "Extension [" + extension + "] already exists in local repository");
190    }
191   
192  514 return localExtension;
193    }
194   
 
195  850 toggle @Override
196    public void setProperties(LocalExtension localExtension, Map<String, Object> properties)
197    throws LocalExtensionRepositoryException
198    {
199  850 DefaultLocalExtension extension = this.extensions.get(localExtension.getId());
200   
201  850 if (extension != null) {
202  850 extension.setProperties(properties);
203  850 try {
204  850 this.storage.saveDescriptor(extension);
205    } catch (Exception e) {
206  0 throw new LocalExtensionRepositoryException(
207    "Failed to save descriptor for extension [" + localExtension + "]", e);
208    }
209    }
210    }
211   
 
212  13 toggle @Override
213    public void removeExtension(LocalExtension extension) throws ResolveException
214    {
215  13 DefaultLocalExtension localExtension = resolve(extension.getId());
216   
217  13 try {
218  13 this.storage.removeExtension(localExtension);
219    } catch (IOException e) {
220    // Should not happen if the local extension exists
221   
222  0 this.logger.error("Failed to remove extension [" + extension + "]", e);
223    }
224   
225    // Remove the extension from the caches
226  13 removeCachedExtension(localExtension);
227    }
228    }