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

File ExtensionFactory.java

 

Coverage histogram

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

Code metrics

4
20
12
1
223
89
14
0.7
1.67
12
1.17

Classes

Class Line # Actions
ExtensionFactory 51 20 0% 14 4
0.888888988.9%
 

Contributing tests

This file is covered by 115 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.internal;
21   
22    import java.net.URI;
23    import java.net.URL;
24    import java.util.Map;
25   
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.extension.DefaultExtensionAuthor;
30    import org.xwiki.extension.DefaultExtensionDependency;
31    import org.xwiki.extension.DefaultExtensionIssueManagement;
32    import org.xwiki.extension.Extension;
33    import org.xwiki.extension.ExtensionAuthor;
34    import org.xwiki.extension.ExtensionDependency;
35    import org.xwiki.extension.ExtensionIssueManagement;
36    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
37    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
38    import org.xwiki.extension.version.Version;
39    import org.xwiki.extension.version.VersionConstraint;
40    import org.xwiki.extension.version.internal.DefaultVersion;
41    import org.xwiki.extension.version.internal.DefaultVersionConstraint;
42   
43    /**
44    * Allow sharing the same instance for various part of {@link Extension}s.
45    *
46    * @version $Id: b31871acb37e1fe278ba10a164803b22801ec5fb $
47    * @since 8.4
48    */
49    @Component(roles = ExtensionFactory.class)
50    @Singleton
 
51    public class ExtensionFactory
52    {
53    private SoftCache<ExtensionDependency, ExtensionDependency> dependencies = new SoftCache<>();
54   
55    private SoftCache<ExtensionAuthor, ExtensionAuthor> authors = new SoftCache<>();
56   
57    private SoftCache<ExtensionRepositoryDescriptor, ExtensionRepositoryDescriptor> repositories = new SoftCache<>();
58   
59    private SoftCache<ExtensionIssueManagement, ExtensionIssueManagement> issueManagements = new SoftCache<>();
60   
61    private SoftCache<String, Version> versions = new SoftCache<>();
62   
63    private SoftCache<String, VersionConstraint> versionConstrains = new SoftCache<>();
64   
65    /**
66    * Store and return a weak reference equals to the passed {@link ExtensionDependency}.
67    *
68    * @param dependency the {@link ExtensionDependency} to find
69    * @return unique instance of {@link ExtensionDependency} equals to the passed one
70    */
 
71  1313607 toggle public ExtensionDependency getExtensionDependency(ExtensionDependency dependency)
72    {
73  1313607 return this.dependencies.get(dependency, dependency);
74    }
75   
76    /**
77    * Store and return a weak reference equals to the passed {@link ExtensionDependency}.
78    *
79    * @param id the id of the extension dependency
80    * @param versionConstraint the version constraint of the extension dependency
81    * @param properties the custom properties of the extension dependency
82    * @return unique instance of {@link ExtensionDependency} equals to the passed one
83    */
 
84  591188 toggle public ExtensionDependency getExtensionDependency(String id, VersionConstraint versionConstraint,
85    Map<String, Object> properties)
86    {
87  591188 return getExtensionDependency(new DefaultExtensionDependency(id, versionConstraint, properties));
88    }
89   
90    /**
91    * Store and return a weak reference equals to the passed {@link ExtensionAuthor}.
92    *
93    * @param author the {@link ExtensionAuthor} to find
94    * @return unique instance of {@link ExtensionAuthor} equals to the passed one
95    */
 
96  101712 toggle public ExtensionAuthor getExtensionAuthor(ExtensionAuthor author)
97    {
98  101712 return this.authors.get(author, author);
99    }
100   
101    /**
102    * Store and return a weak reference equals to the passed {@link ExtensionAuthor}.
103    *
104    * @param name the name of the author
105    * @param url the URL of the author public profile
106    * @return unique instance of {@link ExtensionAuthor} equals to the passed one
107    */
 
108  101712 toggle public ExtensionAuthor getExtensionAuthor(String name, URL url)
109    {
110  101712 return getExtensionAuthor(new DefaultExtensionAuthor(name, url));
111    }
112   
113    /**
114    * Store and return a weak reference equals to the passed {@link ExtensionRepositoryDescriptor}.
115    *
116    * @param repository the {@link ExtensionRepositoryDescriptor} to find
117    * @return unique instance of {@link ExtensionRepositoryDescriptor} equals to the passed one
118    */
 
119  46379 toggle public ExtensionRepositoryDescriptor getExtensionRepositoryDescriptor(ExtensionRepositoryDescriptor repository)
120    {
121  46379 return this.repositories.get(repository, repository);
122    }
123   
124    /**
125    * Store and return a weak reference equals to the passed {@link ExtensionRepositoryDescriptor} elements.
126    *
127    * @param id the unique identifier
128    * @param type the repository type (maven, xwiki, etc.)
129    * @param uri the repository address
130    * @return unique instance of {@link ExtensionRepositoryDescriptor} equals to the passed one
131    */
 
132  46379 toggle public ExtensionRepositoryDescriptor getExtensionRepositoryDescriptor(String id, String type, URI uri)
133    {
134  46379 return getExtensionRepositoryDescriptor(new DefaultExtensionRepositoryDescriptor(id, type, uri));
135    }
136   
137    /**
138    * Store and return a weak reference equals to the passed {@link ExtensionIssueManagement}.
139    *
140    * @param issueManagement the {@link ExtensionIssueManagement} to find
141    * @return unique instance of {@link ExtensionIssueManagement} equals to the passed one
142    */
 
143  19363 toggle public ExtensionIssueManagement getExtensionIssueManagement(ExtensionIssueManagement issueManagement)
144    {
145  19363 return this.issueManagements.get(issueManagement, issueManagement);
146    }
147   
148    /**
149    * Store and return a weak reference equals to the passed {@link ExtensionIssueManagement} elements.
150    *
151    * @param system the name of the issue management system (jira, bugzilla, etc.)
152    * @param url the URL of that extension in the issues management system
153    * @return unique instance of {@link ExtensionIssueManagement} equals to the passed one
154    */
 
155  19363 toggle public ExtensionIssueManagement getExtensionIssueManagement(String system, String url)
156    {
157  19363 return getExtensionIssueManagement(new DefaultExtensionIssueManagement(system, url));
158    }
159   
160    /**
161    * Store and return a weak reference equals to the passed {@link Version}.
162    *
163    * @param version the {@link Version} to find
164    * @return unique instance of {@link Version} equals to the passed one
165    */
 
166  0 toggle public Version getVersion(Version version)
167    {
168    // Use the initial value as key because it's displayed and for example displaying "1.0" instead of "1.0.0.GA"
169    // might produce a WTF effect even if it's exactly the same version.
170  0 return this.versions.get(version.getValue(), version);
171    }
172   
173    /**
174    * Store and return a weak reference equals to the passed version.
175    *
176    * @param rawVersion the version to find
177    * @return unique instance of {@link VersionConstraint} equals to the passed one
178    */
 
179  38327 toggle public Version getVersion(String rawVersion)
180    {
181  38327 Version version = this.versions.get(rawVersion);
182   
183  38327 if (version == null) {
184  13985 version = new DefaultVersion(rawVersion);
185   
186  13985 this.versions.put(rawVersion, version);
187    }
188   
189  38327 return version;
190    }
191   
192    /**
193    * Store and return a weak reference equals to the passed {@link VersionConstraint}.
194    *
195    * @param versionConstraint the {@link VersionConstraint} to find
196    * @return unique instance of {@link VersionConstraint} equals to the passed one
197    */
 
198  0 toggle public VersionConstraint getVersionConstraint(VersionConstraint versionConstraint)
199    {
200    // Use the initial value as key because it's displayed and for example displaying "[1.0]" instead of
201    // "[1.0.0.GA]" might produce a WTF effect even if it's exactly the same version constraint.
202  0 return this.versionConstrains.get(versionConstraint.getValue(), versionConstraint);
203    }
204   
205    /**
206    * Store and return a weak reference equals to the passed version constraint.
207    *
208    * @param rawConstraint the version constraint to find
209    * @return unique instance of {@link VersionConstraint} equals to the passed one
210    */
 
211  591187 toggle public VersionConstraint getVersionConstraint(String rawConstraint)
212    {
213  591187 VersionConstraint constraint = this.versionConstrains.get(rawConstraint);
214   
215  591187 if (constraint == null) {
216  12298 constraint = new DefaultVersionConstraint(rawConstraint);
217   
218  12298 this.versionConstrains.put(rawConstraint, constraint);
219    }
220   
221  591187 return constraint;
222    }
223    }