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

File AbstractExtension.java

 

Coverage histogram

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

Code metrics

44
174
63
1
760
481
109
0.63
2.76
63
1.73

Classes

Class Line # Actions
AbstractExtension 43 174 0% 109 35
0.875444887.5%
 

Contributing tests

This file is covered by 189 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;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.LinkedHashMap;
26    import java.util.LinkedHashSet;
27    import java.util.List;
28    import java.util.Map;
29    import java.util.Set;
30    import java.util.concurrent.locks.ReentrantLock;
31   
32    import org.apache.commons.lang3.ObjectUtils;
33    import org.xwiki.extension.internal.converter.ExtensionIdConverter;
34    import org.xwiki.extension.repository.ExtensionRepository;
35    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
36   
37    /**
38    * Base class for {@link Extension} implementations.
39    *
40    * @version $Id: 8610c4955e90c7f420c442c8fb1d65b859d86d52 $
41    * @since 4.0M1
42    */
 
43    public abstract class AbstractExtension implements MutableExtension
44    {
45    /**
46    * @see #getId()
47    */
48    protected ExtensionId id;
49   
50    /**
51    * @see #getExtensionFeatures()
52    * @deprecated since 8.0M1, use {@link #featuresMap} instead
53    */
54    @Deprecated
55    protected Set<String> features;
56   
57    /**
58    * @see #getExtensionFeatures()
59    */
60    protected Map<String, ExtensionId> featuresMap;
61   
62    /**
63    * @see #getType()
64    */
65    protected String type;
66   
67    /**
68    * @see #getName()
69    */
70    protected String name;
71   
72    /**
73    * @see #getLicenses()
74    */
75    protected List<ExtensionLicense> licenses;
76   
77    /**
78    * @see #getSummary()
79    */
80    protected String summary;
81   
82    /**
83    * @see #getDescription()
84    */
85    protected String description;
86   
87    /**
88    * @see #getAuthors()
89    */
90    protected List<ExtensionAuthor> authors;
91   
92    /**
93    * @see #getWebSite()
94    */
95    protected String website;
96   
97    /**
98    * @see #getAllowedNamespaces()
99    */
100    protected Set<String> allowedNamespaces;
101   
102    /**
103    * @see #getRepository()
104    */
105    protected ExtensionRepository repository;
106   
107    /**
108    * @see #getProperties()
109    */
110    protected Map<String, Object> properties;
111   
112    /**
113    * Used to protect properties from concurrent write;
114    */
115    protected ReentrantLock propertiesLock = new ReentrantLock();
116   
117    /**
118    * @see #getDependencies()
119    */
120    protected List<ExtensionDependency> dependencies;
121   
122    /**
123    * @see #getManagedDependencies()
124    * @since 8.1M1
125    */
126    protected List<ExtensionDependency> managedDependencies;
127   
128    /**
129    * @see #getScm()
130    */
131    protected ExtensionScm scm;
132   
133    /**
134    * @see #getIssueManagement()
135    */
136    protected ExtensionIssueManagement issueManagement;
137   
138    /**
139    * @see #getCategory()
140    */
141    protected String category;
142   
143    /**
144    * @see #getRepositories()
145    */
146    protected List<ExtensionRepositoryDescriptor> repositories;
147   
148    /**
149    * The file of the extension.
150    */
151    protected ExtensionFile file;
152   
153    /**
154    * @param repository the repository where this extension comes from
155    * @param id the extension identifier
156    * @param type the extension type
157    */
 
158  49939 toggle public AbstractExtension(ExtensionRepository repository, ExtensionId id, String type)
159    {
160  49939 this.repository = repository;
161   
162  49939 this.id = id;
163  49939 this.type = type;
164    }
165   
166    /**
167    * Create new extension descriptor by copying provided one.
168    *
169    * @param repository the repository where this extension comes from
170    * @param extension the extension to copy
171    */
 
172  8090 toggle public AbstractExtension(ExtensionRepository repository, Extension extension)
173    {
174  8090 this(repository, extension.getId(), extension.getType());
175   
176  8090 set(extension);
177    }
178   
 
179  8195 toggle @Override
180    public void set(Extension extension)
181    {
182  8195 setName(extension.getName());
183  8195 setDescription(extension.getDescription());
184  8195 setAuthors(extension.getAuthors());
185  8195 setWebsite(extension.getWebSite());
186  8195 setLicenses(extension.getLicenses());
187  8195 setSummary(extension.getSummary());
188  8195 setIssueManagement(extension.getIssueManagement());
189  8195 setScm(extension.getScm());
190  8195 setCategory(extension.getCategory());
191  8195 setExtensionFeatures(extension.getExtensionFeatures());
192  8195 setAllowedNamespaces(extension.getAllowedNamespaces());
193   
194  8195 setDependencies(extension.getDependencies());
195  8195 setManagedDependencies(extension.getManagedDependencies());
196   
197  8195 setRepositories(extension.getRepositories());
198   
199  8195 setProperties(extension.getProperties());
200    }
201   
202    /**
203    * Get an extension field by name. Fallback on properties.
204    *
205    * @param fieldName the field name;
206    * @return the field value or null if none could be found
207    */
 
208  27161 toggle @Override
209    public <T> T get(String fieldName)
210    {
211  27161 switch (fieldName.toLowerCase()) {
212  1 case FIELD_REPOSITORY:
213  1 return (T) getRepository();
214  58 case FIELD_ID:
215  58 return (T) getId().getId();
216  1 case FIELD_VERSION:
217  1 return (T) getId().getVersion();
218  0 case FIELD_FEATURE:
219  0 case FIELD_FEATURES:
220  0 return (T) ExtensionIdConverter.toStringList(getExtensionFeatures());
221  0 case FIELD_EXTENSIONFEATURE:
222  0 case FIELD_EXTENSIONFEATURES:
223  0 return (T) getExtensionFeatures();
224  0 case FIELD_SUMMARY:
225  0 return (T) getSummary();
226  0 case FIELD_DESCRIPTION:
227  0 return (T) getDescription();
228  0 case FIELD_AUTHOR:
229  0 case FIELD_AUTHORS:
230  0 return (T) getAuthors();
231  1915 case FIELD_CATEGORY:
232  1915 return (T) getCategory();
233  0 case FIELD_LICENSE:
234  0 case FIELD_LICENSES:
235  0 return (T) getLicenses();
236  0 case FIELD_NAME:
237  0 return (T) getName();
238  1 case FIELD_TYPE:
239  1 return (T) getType();
240  24927 case FIELD_WEBSITE:
241  24927 return (T) getWebSite();
242  0 case FIELD_NAMESPACES:
243  0 case FIELD_ALLOWEDNAMESPACE:
244  0 case FIELD_ALLOWEDNAMESPACES:
245  0 return (T) getAllowedNamespaces();
246  0 case FIELD_SCM:
247  0 return (T) getScm();
248  0 case FIELD_REPOSITORIES:
249  0 return (T) getRepositories();
250  0 case FIELD_PROPERTIES:
251  0 return (T) getProperties();
252   
253  258 default:
254    // Unknown field, probably a property
255  258 return getProperty(fieldName);
256    }
257    }
258   
 
259  3120258 toggle @Override
260    public ExtensionId getId()
261    {
262  3120256 return this.id;
263    }
264   
265    /**
266    * @param id the extension id
267    * @see #getId()
268    */
 
269  533 toggle protected void setId(ExtensionId id)
270    {
271  533 this.id = id;
272    }
273   
 
274  20498 toggle @Override
275    @Deprecated
276    public Collection<String> getFeatures()
277    {
278  20498 return this.features != null ? this.features : Collections.<String>emptyList();
279    }
280   
 
281  399 toggle @Override
282    @Deprecated
283    public void setFeatures(Collection<String> features)
284    {
285  399 List<ExtensionId> extensionFeatures = new ArrayList<ExtensionId>(features.size());
286  399 for (String feature : features) {
287  400 extensionFeatures.add(new ExtensionId(feature, getId().getVersion()));
288    }
289   
290  399 setExtensionFeatures(extensionFeatures);
291    }
292   
 
293  2 toggle @Override
294    @Deprecated
295    public void addFeature(String feature)
296    {
297  2 addExtensionFeature(new ExtensionId(feature, getId().getVersion()));
298    }
299   
 
300  71091 toggle @Override
301    public Collection<ExtensionId> getExtensionFeatures()
302    {
303  71091 return this.featuresMap != null ? this.featuresMap.values() : Collections.<ExtensionId>emptyList();
304    }
305   
 
306  14732 toggle @Override
307    public ExtensionId getExtensionFeature(String featureId)
308    {
309  14732 ExtensionId feature = null;
310   
311    // Search in the extension features
312  14732 if (this.featuresMap != null) {
313  13811 feature = this.featuresMap.get(featureId);
314    }
315   
316    // Fallback on extension id
317  14732 if (feature == null && featureId.equals(getId().getId())) {
318  14563 feature = getId();
319    }
320   
321  14732 return feature;
322    }
323   
324    /**
325    * @since 8.0M1
326    */
 
327  28157 toggle @Override
328    public void setExtensionFeatures(Collection<ExtensionId> features)
329    {
330  28157 Map<String, ExtensionId> map = new LinkedHashMap<>();
331  28157 for (ExtensionId feature : features) {
332  3185 map.put(feature.getId(), feature);
333    }
334   
335  28157 setFeatureMap(map);
336    }
337   
338    /**
339    * @since 8.0M1
340    */
 
341  2126 toggle @Override
342    public void addExtensionFeature(ExtensionId feature)
343    {
344  2126 Map<String, ExtensionId> map = this.featuresMap != null
345    ? new LinkedHashMap<String, ExtensionId>(this.featuresMap) : new LinkedHashMap<String, ExtensionId>();
346  2126 map.put(feature.getId(), feature);
347   
348  2126 setFeatureMap(map);
349    }
350   
 
351  30283 toggle private void setFeatureMap(Map<String, ExtensionId> map)
352    {
353  30283 this.featuresMap = Collections.unmodifiableMap(map);
354   
355    // Retro compatibility
356  30283 Set<String> list = new LinkedHashSet<String>(this.featuresMap.size());
357  30283 for (ExtensionId extensionId : this.featuresMap.values()) {
358  7241 list.add(extensionId.getId());
359    }
360  30283 this.features = Collections.unmodifiableSet(list);
361    }
362   
 
363  37710 toggle @Override
364    public String getType()
365    {
366  37710 return this.type;
367    }
368   
369    /**
370    * @param type the type of the extension
371    * @see #getType()
372    */
 
373  533 toggle protected void setType(String type)
374    {
375  533 this.type = type;
376    }
377   
 
378  138162 toggle @Override
379    public String getName()
380    {
381  138162 return this.name;
382    }
383   
 
384  47576 toggle @Override
385    public void setName(String name)
386    {
387  47576 this.name = name;
388    }
389   
 
390  98329 toggle @Override
391    public Collection<ExtensionLicense> getLicenses()
392    {
393  98329 return this.licenses != null ? this.licenses : Collections.<ExtensionLicense>emptyList();
394    }
395   
 
396  8195 toggle @Override
397    public void setLicenses(Collection<ExtensionLicense> licenses)
398    {
399  8195 this.licenses = Collections.unmodifiableList(new ArrayList<ExtensionLicense>(licenses));
400    }
401   
 
402  39561 toggle @Override
403    public void addLicense(ExtensionLicense license)
404    {
405  39561 List<ExtensionLicense> newLicenses = new ArrayList<ExtensionLicense>(getLicenses());
406  39561 newLicenses.add(license);
407   
408  39561 this.licenses = Collections.unmodifiableList(newLicenses);
409    }
410   
 
411  32607 toggle @Override
412    public String getSummary()
413    {
414  32607 return this.summary;
415    }
416   
 
417  47603 toggle @Override
418    public void setSummary(String summary)
419    {
420  47603 this.summary = summary;
421    }
422   
 
423  30205 toggle @Override
424    public String getDescription()
425    {
426  30205 return this.description;
427    }
428   
 
429  8878 toggle @Override
430    public void setDescription(String description)
431    {
432  8878 this.description = description;
433    }
434   
 
435  168841 toggle @Override
436    public List<ExtensionAuthor> getAuthors()
437    {
438  168841 return this.authors != null ? this.authors : Collections.<ExtensionAuthor>emptyList();
439    }
440   
 
441  8195 toggle @Override
442    public void setAuthors(Collection<? extends ExtensionAuthor> authors)
443    {
444  8195 this.authors = Collections.unmodifiableList(new ArrayList<ExtensionAuthor>(authors));
445    }
446   
 
447  142071 toggle @Override
448    public void addAuthor(ExtensionAuthor author)
449    {
450  142071 List<ExtensionAuthor> newAuthors = new ArrayList<ExtensionAuthor>(getAuthors());
451  142071 newAuthors.add(author);
452   
453  142071 this.authors = Collections.unmodifiableList(newAuthors);
454    }
455   
 
456  51207 toggle @Override
457    public String getWebSite()
458    {
459  51207 return this.website;
460    }
461   
 
462  47859 toggle @Override
463    public void setWebsite(String website)
464    {
465  47859 this.website = website;
466    }
467   
 
468  26727 toggle @Override
469    public Collection<String> getAllowedNamespaces()
470    {
471  26727 return this.allowedNamespaces;
472    }
473   
474    /**
475    * @since 8.0M1
476    */
 
477  3 toggle @Override
478    public void addAllowedNamespace(String namespace)
479    {
480  3 Set<String> newNamespaces = this.allowedNamespaces != null ? new LinkedHashSet<String>(this.allowedNamespaces)
481    : new LinkedHashSet<String>();
482  3 newNamespaces.add(namespace);
483   
484  3 this.allowedNamespaces = Collections.unmodifiableSet(newNamespaces);
485    }
486   
487    /**
488    * @since 8.0M1
489    */
 
490  4839 toggle @Override
491    public void setAllowedNamespaces(Collection<String> namespaces)
492    {
493  4839 this.allowedNamespaces =
494  4839 namespaces != null ? Collections.unmodifiableSet(new LinkedHashSet<>(namespaces)) : null;
495    }
496   
 
497  64787 toggle @Override
498    public void addDependency(ExtensionDependency dependency)
499    {
500  64787 List<ExtensionDependency> newDependencies = new ArrayList<ExtensionDependency>(getDependencies());
501  64787 newDependencies.add(dependency);
502   
503  64787 this.dependencies = Collections.unmodifiableList(newDependencies);
504    }
505   
 
506  123661 toggle @Override
507    public List<ExtensionDependency> getDependencies()
508    {
509  123661 return this.dependencies != null ? this.dependencies : Collections.<ExtensionDependency>emptyList();
510    }
511   
 
512  30197 toggle @Override
513    public void setDependencies(Collection<? extends ExtensionDependency> dependencies)
514    {
515  30197 this.dependencies = dependencies != null
516    ? Collections.unmodifiableList(new ArrayList<ExtensionDependency>(dependencies)) : null;
517    }
518   
519    /**
520    * @since 8.1M1
521    */
 
522  1476340 toggle @Override
523    public void addManagedDependency(ExtensionDependency managedDependency)
524    {
525  1476340 List<ExtensionDependency> newManagedDependencies = new ArrayList<ExtensionDependency>(getManagedDependencies());
526  1476340 newManagedDependencies.add(managedDependency);
527   
528  1476340 this.managedDependencies = Collections.unmodifiableList(newManagedDependencies);
529    }
530   
 
531  1511894 toggle @Override
532    public List<ExtensionDependency> getManagedDependencies()
533    {
534  1511894 return this.managedDependencies != null ? this.managedDependencies
535    : Collections.<ExtensionDependency>emptyList();
536    }
537   
 
538  7947 toggle @Override
539    public void setManagedDependencies(Collection<? extends ExtensionDependency> managedDependencies)
540    {
541  7947 this.managedDependencies = managedDependencies != null
542    ? Collections.unmodifiableList(new ArrayList<ExtensionDependency>(managedDependencies)) : null;
543    }
544   
 
545  3000 toggle @Override
546    public ExtensionRepository getRepository()
547    {
548  3000 return this.repository;
549    }
550   
551    /**
552    * @param repository the repository of the extension
553    * @see #getRepository()
554    */
 
555  0 toggle protected void setRepository(ExtensionRepository repository)
556    {
557  0 this.repository = repository;
558    }
559   
 
560  26120 toggle @Override
561    public ExtensionScm getScm()
562    {
563  26120 return this.scm;
564    }
565   
566    /**
567    * @since 6.3M1
568    */
 
569  46738 toggle @Override
570    public void setScm(ExtensionScm scm)
571    {
572  46738 this.scm = scm;
573    }
574   
 
575  25945 toggle @Override
576    public ExtensionIssueManagement getIssueManagement()
577    {
578  25945 return this.issueManagement;
579    }
580   
581    /**
582    * @since 6.3M1
583    */
 
584  42297 toggle @Override
585    public void setIssueManagement(ExtensionIssueManagement issueManagement)
586    {
587  42297 this.issueManagement = issueManagement;
588    }
589   
 
590  6333 toggle @Override
591    public ExtensionFile getFile()
592    {
593  6333 return this.file;
594    }
595   
596    /**
597    * @param file the file of the extension
598    */
 
599  31118 toggle protected void setFile(ExtensionFile file)
600    {
601  31118 this.file = file;
602    }
603   
 
604  26822 toggle @Override
605    public String getCategory()
606    {
607  26822 return this.category;
608    }
609   
610    /**
611    * @since 7.0M2
612    */
 
613  8746 toggle @Override
614    public void setCategory(String categrory)
615    {
616  8746 this.category = categrory;
617    }
618   
 
619  9757 toggle @Override
620    public Collection<ExtensionRepositoryDescriptor> getRepositories()
621    {
622  9757 return this.repositories != null ? this.repositories : Collections.<ExtensionRepositoryDescriptor>emptyList();
623    }
624   
625    /**
626    * @since 7.3M1
627    */
 
628  28252 toggle @Override
629    public void setRepositories(Collection<? extends ExtensionRepositoryDescriptor> repositories)
630    {
631  28252 this.repositories = repositories != null ? Collections.unmodifiableList(new ArrayList<>(repositories)) : null;
632    }
633   
634    /**
635    * @since 7.3M1
636    */
 
637  467 toggle @Override
638    public void addRepository(ExtensionRepositoryDescriptor repository)
639    {
640  467 List<ExtensionRepositoryDescriptor> newrepositories =
641    new ArrayList<ExtensionRepositoryDescriptor>(getRepositories());
642  467 newrepositories.add(repository);
643   
644  467 this.repositories = Collections.unmodifiableList(newrepositories);
645    }
646   
 
647  266362 toggle @Override
648    public Map<String, Object> getProperties()
649    {
650  266362 return this.properties != null ? this.properties : Collections.<String, Object>emptyMap();
651    }
652   
 
653  43117 toggle @Override
654    public <T> T getProperty(String key)
655    {
656  43117 return (T) getProperties().get(key);
657    }
658   
 
659  30482 toggle @Override
660    public <T> T getProperty(String key, T def)
661    {
662  30482 return getProperties().containsKey(key) ? (T) getProperties().get(key) : def;
663    }
664   
 
665  144671 toggle @Override
666    public void putProperty(String key, Object value)
667    {
668  144671 synchronized (this.propertiesLock) {
669  144671 Map<String, Object> newProperties = new LinkedHashMap<String, Object>(getProperties());
670  144671 newProperties.put(key, value);
671   
672  144671 this.properties = Collections.unmodifiableMap(newProperties);
673    }
674    }
675   
 
676  29573 toggle @Override
677    public void setProperties(Map<String, Object> properties)
678    {
679  29573 this.properties = Collections.unmodifiableMap(new LinkedHashMap<String, Object>(properties));
680    }
681   
682    /**
683    * @since 8.3M1
684    */
 
685  1860 toggle @Override
686    public <T> T removeProperty(String key)
687    {
688  1860 T previous;
689   
690  1860 synchronized (this.propertiesLock) {
691  1860 Map<String, Object> newProperties = new LinkedHashMap<String, Object>(getProperties());
692  1860 previous = (T) newProperties.remove(key);
693   
694  1860 this.properties = Collections.unmodifiableMap(newProperties);
695    }
696   
697  1860 return previous;
698    }
699   
700    // Object
701   
 
702  692166 toggle @Override
703    public String toString()
704    {
705  692166 return getId().toString();
706    }
707   
 
708  10252 toggle @Override
709    public boolean equals(Object obj)
710    {
711  10252 return this == obj || (obj instanceof Extension && getId().equals(((Extension) obj).getId()));
712    }
713   
 
714  75142 toggle @Override
715    public int hashCode()
716    {
717  75142 return getId().hashCode();
718    }
719   
 
720  277 toggle @Override
721    public int compareTo(Extension o)
722    {
723  277 if (o == null) {
724  1 return -1;
725    }
726   
727    // Try to find this into provided extension
728  276 Integer comparizon = compareTo(this, o);
729  276 if (comparizon != null) {
730  275 return comparizon;
731    }
732   
733    // Try to find provided extension into this
734  1 comparizon = compareTo(o, this);
735  1 if (comparizon != null) {
736  0 return comparizon;
737    }
738   
739  1 return ObjectUtils.compare(getId(), o.getId());
740    }
741   
 
742  277 toggle private static Integer compareTo(Extension e1, Extension e2)
743    {
744    // Try to find e1 id in e2
745  277 ExtensionId feature = e2.getExtensionFeature(e1.getId().getId());
746  277 if (feature != null) {
747  254 return ObjectUtils.compare(e1.getId().getVersion(), feature.getVersion());
748    }
749   
750    // Try to find e1 features in e2
751  23 for (ExtensionId feature1 : e1.getExtensionFeatures()) {
752  21 feature = e2.getExtensionFeature(feature1.getId());
753  21 if (feature != null) {
754  21 return ObjectUtils.compare(feature1.getVersion(), feature.getVersion());
755    }
756    }
757   
758  2 return null;
759    }
760    }