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

File AbstractExtension.java

 

Coverage histogram

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

Code metrics

44
173
63
1
758
480
109
0.63
2.75
63
1.73

Classes

Class Line # Actions
AbstractExtension 43 173 0% 109 36
0.8714285587.1%
 

Contributing tests

This file is covered by 182 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: 34713608188f356301ceac8af1b9f7e4619333e7 $
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  52825 toggle public AbstractExtension(ExtensionRepository repository, ExtensionId id, String type)
159    {
160  52825 this.repository = repository;
161   
162  52825 this.id = id;
163  52825 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  12803 toggle public AbstractExtension(ExtensionRepository repository, Extension extension)
173    {
174  12803 this(repository, extension.getId(), extension.getType());
175   
176  12803 set(extension);
177    }
178   
 
179  12822 toggle @Override
180    public void set(Extension extension)
181    {
182  12822 setName(extension.getName());
183  12822 setDescription(extension.getDescription());
184  12822 setAuthors(extension.getAuthors());
185  12822 setWebsite(extension.getWebSite());
186  12822 setLicenses(extension.getLicenses());
187  12822 setSummary(extension.getSummary());
188  12822 setIssueManagement(extension.getIssueManagement());
189  12822 setScm(extension.getScm());
190  12822 setCategory(extension.getCategory());
191  12822 setExtensionFeatures(extension.getExtensionFeatures());
192  12822 setAllowedNamespaces(extension.getAllowedNamespaces());
193   
194  12822 setDependencies(extension.getDependencies());
195  12822 setManagedDependencies(extension.getManagedDependencies());
196   
197  12822 setProperties(extension.getProperties());
198    }
199   
200    /**
201    * Get an extension field by name. Fallback on properties.
202    *
203    * @param fieldName the field name;
204    * @return the field value or null if none could be found
205    */
 
206  541 toggle @Override
207    public <T> T get(String fieldName)
208    {
209  541 switch (fieldName.toLowerCase()) {
210  1 case FIELD_REPOSITORY:
211  1 return (T) getRepository();
212  58 case FIELD_ID:
213  58 return (T) getId().getId();
214  1 case FIELD_VERSION:
215  1 return (T) getId().getVersion();
216  0 case FIELD_FEATURE:
217  0 case FIELD_FEATURES:
218  0 return (T) ExtensionIdConverter.toStringList(getExtensionFeatures());
219  0 case FIELD_EXTENSIONFEATURE:
220  0 case FIELD_EXTENSIONFEATURES:
221  0 return (T) getExtensionFeatures();
222  0 case FIELD_SUMMARY:
223  0 return (T) getSummary();
224  0 case FIELD_DESCRIPTION:
225  0 return (T) getDescription();
226  0 case FIELD_AUTHOR:
227  0 case FIELD_AUTHORS:
228  0 return (T) getAuthors();
229  15 case FIELD_CATEGORY:
230  15 return (T) getCategory();
231  0 case FIELD_LICENSE:
232  0 case FIELD_LICENSES:
233  0 return (T) getLicenses();
234  0 case FIELD_NAME:
235  0 return (T) getName();
236  1 case FIELD_TYPE:
237  1 return (T) getType();
238  444 case FIELD_WEBSITE:
239  444 return (T) getWebSite();
240  0 case FIELD_NAMESPACES:
241  0 case FIELD_ALLOWEDNAMESPACE:
242  0 case FIELD_ALLOWEDNAMESPACES:
243  0 return (T) getAllowedNamespaces();
244  0 case FIELD_SCM:
245  0 return (T) getScm();
246  0 case FIELD_REPOSITORIES:
247  0 return (T) getRepositories();
248  0 case FIELD_PROPERTIES:
249  0 return (T) getProperties();
250   
251  21 default:
252    // Unknown field, probably a property
253  21 return getProperty(fieldName);
254    }
255    }
256   
 
257  192890 toggle @Override
258    public ExtensionId getId()
259    {
260  192890 return this.id;
261    }
262   
263    /**
264    * @param id the extension id
265    * @see #getId()
266    */
 
267  51 toggle protected void setId(ExtensionId id)
268    {
269  51 this.id = id;
270    }
271   
 
272  15810 toggle @Override
273    @Deprecated
274    public Collection<String> getFeatures()
275    {
276  15810 return this.features != null ? this.features : Collections.<String>emptyList();
277    }
278   
 
279  370 toggle @Override
280    @Deprecated
281    public void setFeatures(Collection<String> features)
282    {
283  370 List<ExtensionId> extensionFeatures = new ArrayList<ExtensionId>(features.size());
284  370 for (String feature : features) {
285  371 extensionFeatures.add(new ExtensionId(feature, getId().getVersion()));
286    }
287   
288  370 setExtensionFeatures(extensionFeatures);
289    }
290   
 
291  2 toggle @Override
292    @Deprecated
293    public void addFeature(String feature)
294    {
295  2 addExtensionFeature(new ExtensionId(feature, getId().getVersion()));
296    }
297   
 
298  61262 toggle @Override
299    public Collection<ExtensionId> getExtensionFeatures()
300    {
301  61262 return this.featuresMap != null ? this.featuresMap.values() : Collections.<ExtensionId>emptyList();
302    }
303   
 
304  1357 toggle @Override
305    public ExtensionId getExtensionFeature(String featureId)
306    {
307  1357 ExtensionId feature = null;
308   
309    // Search in the extension features
310  1357 if (this.featuresMap != null) {
311  1327 feature = this.featuresMap.get(featureId);
312    }
313   
314    // Fallback on extension id
315  1357 if (feature == null && featureId.equals(getId().getId())) {
316  1209 feature = getId();
317    }
318   
319  1357 return feature;
320    }
321   
322    /**
323    * @since 8.0M1
324    */
 
325  14182 toggle @Override
326    public void setExtensionFeatures(Collection<ExtensionId> features)
327    {
328  14182 Map<String, ExtensionId> map = new LinkedHashMap<>();
329  14182 for (ExtensionId feature : features) {
330  2470 map.put(feature.getId(), feature);
331    }
332   
333  14182 setFeatureMap(map);
334    }
335   
336    /**
337    * @since 8.0M1
338    */
 
339  496 toggle @Override
340    public void addExtensionFeature(ExtensionId feature)
341    {
342  496 Map<String, ExtensionId> map = this.featuresMap != null
343    ? new LinkedHashMap<String, ExtensionId>(this.featuresMap) : new LinkedHashMap<String, ExtensionId>();
344  496 map.put(feature.getId(), feature);
345   
346  496 setFeatureMap(map);
347    }
348   
 
349  14678 toggle private void setFeatureMap(Map<String, ExtensionId> map)
350    {
351  14678 this.featuresMap = Collections.unmodifiableMap(map);
352   
353    // Retro compatibility
354  14678 Set<String> list = new LinkedHashSet<String>(this.featuresMap.size());
355  14678 for (ExtensionId extensionId : this.featuresMap.values()) {
356  3007 list.add(extensionId.getId());
357    }
358  14678 this.features = Collections.unmodifiableSet(list);
359    }
360   
 
361  31696 toggle @Override
362    public String getType()
363    {
364  31696 return this.type;
365    }
366   
367    /**
368    * @param type the type of the extension
369    * @see #getType()
370    */
 
371  51 toggle protected void setType(String type)
372    {
373  51 this.type = type;
374    }
375   
 
376  30952 toggle @Override
377    public String getName()
378    {
379  30952 return this.name;
380    }
381   
 
382  48902 toggle @Override
383    public void setName(String name)
384    {
385  48902 this.name = name;
386    }
387   
 
388  83239 toggle @Override
389    public Collection<ExtensionLicense> getLicenses()
390    {
391  83239 return this.licenses != null ? this.licenses : Collections.<ExtensionLicense>emptyList();
392    }
393   
 
394  12822 toggle @Override
395    public void setLicenses(Collection<ExtensionLicense> licenses)
396    {
397  12822 this.licenses = Collections.unmodifiableList(new ArrayList<ExtensionLicense>(licenses));
398    }
399   
 
400  28591 toggle @Override
401    public void addLicense(ExtensionLicense license)
402    {
403  28591 List<ExtensionLicense> newLicenses = new ArrayList<ExtensionLicense>(getLicenses());
404  28591 newLicenses.add(license);
405   
406  28591 this.licenses = Collections.unmodifiableList(newLicenses);
407    }
408   
 
409  30611 toggle @Override
410    public String getSummary()
411    {
412  30612 return this.summary;
413    }
414   
 
415  48249 toggle @Override
416    public void setSummary(String summary)
417    {
418  48249 this.summary = summary;
419    }
420   
 
421  29954 toggle @Override
422    public String getDescription()
423    {
424  29954 return this.description;
425    }
426   
 
427  13092 toggle @Override
428    public void setDescription(String description)
429    {
430  13092 this.description = description;
431    }
432   
 
433  129249 toggle @Override
434    public List<ExtensionAuthor> getAuthors()
435    {
436  129249 return this.authors != null ? this.authors : Collections.<ExtensionAuthor>emptyList();
437    }
438   
 
439  12822 toggle @Override
440    public void setAuthors(Collection<? extends ExtensionAuthor> authors)
441    {
442  12822 this.authors = Collections.unmodifiableList(new ArrayList<ExtensionAuthor>(authors));
443    }
444   
 
445  101716 toggle @Override
446    public void addAuthor(ExtensionAuthor author)
447    {
448  101716 List<ExtensionAuthor> newAuthors = new ArrayList<ExtensionAuthor>(getAuthors());
449  101716 newAuthors.add(author);
450   
451  101716 this.authors = Collections.unmodifiableList(newAuthors);
452    }
453   
 
454  27699 toggle @Override
455    public String getWebSite()
456    {
457  27699 return this.website;
458    }
459   
 
460  49076 toggle @Override
461    public void setWebsite(String website)
462    {
463  49076 this.website = website;
464    }
465   
 
466  27396 toggle @Override
467    public Collection<String> getAllowedNamespaces()
468    {
469  27397 return this.allowedNamespaces;
470    }
471   
472    /**
473    * @since 8.0M1
474    */
 
475  1 toggle @Override
476    public void addAllowedNamespace(String namespace)
477    {
478  1 Set<String> newNamespaces = this.allowedNamespaces != null ? new LinkedHashSet<String>(this.allowedNamespaces)
479    : new LinkedHashSet<String>();
480  1 newNamespaces.add(namespace);
481   
482  1 this.allowedNamespaces = Collections.unmodifiableSet(newNamespaces);
483    }
484   
485    /**
486    * @since 8.0M1
487    */
 
488  1985 toggle @Override
489    public void setAllowedNamespaces(Collection<String> namespaces)
490    {
491  1986 this.allowedNamespaces =
492  1986 namespaces != null ? Collections.unmodifiableSet(new LinkedHashSet<>(namespaces)) : null;
493    }
494   
 
495  71930 toggle @Override
496    public void addDependency(ExtensionDependency dependency)
497    {
498  71930 List<ExtensionDependency> newDependencies = new ArrayList<ExtensionDependency>(getDependencies());
499  71930 newDependencies.add(dependency);
500   
501  71930 this.dependencies = Collections.unmodifiableList(newDependencies);
502    }
503   
 
504  127580 toggle @Override
505    public List<ExtensionDependency> getDependencies()
506    {
507  127580 return this.dependencies != null ? this.dependencies : Collections.<ExtensionDependency>emptyList();
508    }
509   
 
510  26253 toggle @Override
511    public void setDependencies(Collection<? extends ExtensionDependency> dependencies)
512    {
513  26253 this.dependencies = dependencies != null
514    ? Collections.unmodifiableList(new ArrayList<ExtensionDependency>(dependencies)) : null;
515    }
516   
517    /**
518    * @since 8.1M1
519    */
 
520  650501 toggle @Override
521    public void addManagedDependency(ExtensionDependency managedDependency)
522    {
523  650501 List<ExtensionDependency> newManagedDependencies = new ArrayList<ExtensionDependency>(getManagedDependencies());
524  650501 newManagedDependencies.add(managedDependency);
525   
526  650501 this.managedDependencies = Collections.unmodifiableList(newManagedDependencies);
527    }
528   
 
529  678665 toggle @Override
530    public List<ExtensionDependency> getManagedDependencies()
531    {
532  678665 return this.managedDependencies != null ? this.managedDependencies
533    : Collections.<ExtensionDependency>emptyList();
534    }
535   
 
536  3557 toggle @Override
537    public void setManagedDependencies(Collection<? extends ExtensionDependency> managedDependencies)
538    {
539  3557 this.managedDependencies = managedDependencies != null
540    ? Collections.unmodifiableList(new ArrayList<ExtensionDependency>(managedDependencies)) : null;
541    }
542   
 
543  689 toggle @Override
544    public ExtensionRepository getRepository()
545    {
546  689 return this.repository;
547    }
548   
549    /**
550    * @param repository the repository of the extension
551    * @see #getRepository()
552    */
 
553  0 toggle protected void setRepository(ExtensionRepository repository)
554    {
555  0 this.repository = repository;
556    }
557   
 
558  27202 toggle @Override
559    public ExtensionScm getScm()
560    {
561  27202 return this.scm;
562    }
563   
564    /**
565    * @since 6.3M1
566    */
 
567  41825 toggle @Override
568    public void setScm(ExtensionScm scm)
569    {
570  41825 this.scm = scm;
571    }
572   
 
573  27066 toggle @Override
574    public ExtensionIssueManagement getIssueManagement()
575    {
576  27066 return this.issueManagement;
577    }
578   
579    /**
580    * @since 6.3M1
581    */
 
582  37468 toggle @Override
583    public void setIssueManagement(ExtensionIssueManagement issueManagement)
584    {
585  37468 this.issueManagement = issueManagement;
586    }
587   
 
588  2360 toggle @Override
589    public ExtensionFile getFile()
590    {
591  2360 return this.file;
592    }
593   
594    /**
595    * @param file the file of the extension
596    */
 
597  37580 toggle protected void setFile(ExtensionFile file)
598    {
599  37580 this.file = file;
600    }
601   
 
602  27027 toggle @Override
603    public String getCategory()
604    {
605  27027 return this.category;
606    }
607   
608    /**
609    * @since 7.0M2
610    */
 
611  12903 toggle @Override
612    public void setCategory(String categrory)
613    {
614  12903 this.category = categrory;
615    }
616   
 
617  192 toggle @Override
618    public Collection<ExtensionRepositoryDescriptor> getRepositories()
619    {
620  192 return this.repositories != null ? this.repositories : Collections.<ExtensionRepositoryDescriptor>emptyList();
621    }
622   
623    /**
624    * @since 7.3M1
625    */
 
626  24457 toggle @Override
627    public void setRepositories(Collection<? extends ExtensionRepositoryDescriptor> repositories)
628    {
629  24457 this.repositories = repositories != null ? Collections.unmodifiableList(new ArrayList<>(repositories)) : null;
630    }
631   
632    /**
633    * @since 7.3M1
634    */
 
635  67 toggle @Override
636    public void addRepository(ExtensionRepositoryDescriptor repository)
637    {
638  67 List<ExtensionRepositoryDescriptor> newrepositories =
639    new ArrayList<ExtensionRepositoryDescriptor>(getRepositories());
640  67 newrepositories.add(repository);
641   
642  67 this.repositories = Collections.unmodifiableList(newrepositories);
643    }
644   
 
645  252969 toggle @Override
646    public Map<String, Object> getProperties()
647    {
648  252969 return this.properties != null ? this.properties : Collections.<String, Object>emptyMap();
649    }
650   
 
651  27299 toggle @Override
652    public <T> T getProperty(String key)
653    {
654  27299 return (T) getProperties().get(key);
655    }
656   
 
657  34932 toggle @Override
658    public <T> T getProperty(String key, T def)
659    {
660  34932 return getProperties().containsKey(key) ? (T) getProperties().get(key) : def;
661    }
662   
 
663  151388 toggle @Override
664    public void putProperty(String key, Object value)
665    {
666  151388 synchronized (this.propertiesLock) {
667  151388 Map<String, Object> newProperties = new LinkedHashMap<String, Object>(getProperties());
668  151388 newProperties.put(key, value);
669   
670  151388 this.properties = Collections.unmodifiableMap(newProperties);
671    }
672    }
673   
 
674  26025 toggle @Override
675    public void setProperties(Map<String, Object> properties)
676    {
677  26025 this.properties = Collections.unmodifiableMap(new LinkedHashMap<String, Object>(properties));
678    }
679   
680    /**
681    * @since 8.3M1
682    */
 
683  267 toggle @Override
684    public <T> T removeProperty(String key)
685    {
686  268 T previous;
687   
688  268 synchronized (this.propertiesLock) {
689  268 Map<String, Object> newProperties = new LinkedHashMap<String, Object>(getProperties());
690  267 previous = (T) newProperties.remove(key);
691   
692  268 this.properties = Collections.unmodifiableMap(newProperties);
693    }
694   
695  267 return previous;
696    }
697   
698    // Object
699   
 
700  1855 toggle @Override
701    public String toString()
702    {
703  1855 return getId().toString();
704    }
705   
 
706  250 toggle @Override
707    public boolean equals(Object obj)
708    {
709  250 return this == obj || (obj instanceof Extension && getId().equals(((Extension) obj).getId()));
710    }
711   
 
712  10484 toggle @Override
713    public int hashCode()
714    {
715  10484 return getId().hashCode();
716    }
717   
 
718  51 toggle @Override
719    public int compareTo(Extension o)
720    {
721  51 if (o == null) {
722  1 return -1;
723    }
724   
725    // Try to find this into provided extension
726  50 Integer comparizon = compareTo(this, o);
727  50 if (comparizon != null) {
728  49 return comparizon;
729    }
730   
731    // Try to find provided extension into this
732  1 comparizon = compareTo(o, this);
733  1 if (comparizon != null) {
734  0 return comparizon;
735    }
736   
737  1 return ObjectUtils.compare(getId(), o.getId());
738    }
739   
 
740  51 toggle private static Integer compareTo(Extension e1, Extension e2)
741    {
742    // Try to find e1 id in e2
743  51 ExtensionId feature = e2.getExtensionFeature(e1.getId().getId());
744  51 if (feature != null) {
745  42 return ObjectUtils.compare(e1.getId().getVersion(), feature.getVersion());
746    }
747   
748    // Try to find e1 features in e2
749  9 for (ExtensionId feature1 : e1.getExtensionFeatures()) {
750  7 feature = e2.getExtensionFeature(feature1.getId());
751  7 if (feature != null) {
752  7 return ObjectUtils.compare(feature1.getVersion(), feature.getVersion());
753    }
754    }
755   
756  2 return null;
757    }
758    }