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

File RepositoryScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

2
14
4
1
108
57
7
0.5
3.5
4
1.75

Classes

Class Line # Actions
RepositoryScriptService 39 14 0% 7 11
0.4545%
 

Contributing tests

No tests hitting this source file were found.

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.repository.script;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.context.Execution;
28    import org.xwiki.extension.ExtensionException;
29    import org.xwiki.extension.repository.ExtensionRepository;
30    import org.xwiki.extension.repository.ExtensionRepositoryManager;
31    import org.xwiki.extension.version.Version;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.repository.internal.RepositoryManager;
34    import org.xwiki.script.service.ScriptService;
35   
36    @Component
37    @Named("repository")
38    @Singleton
 
39    public class RepositoryScriptService implements ScriptService
40    {
41    /**
42    * The key under which the last encountered error is stored in the current execution context.
43    */
44    private static final String REPOSITORYERROR_KEY = "scriptservice.repository.error";
45   
46    @Inject
47    private RepositoryManager repositoryManager;
48   
49    @Inject
50    private ExtensionRepositoryManager extensionRepositoryManager;
51   
52    /**
53    * Provides access to the current context.
54    */
55    @Inject
56    private Execution execution;
57   
58    /**
59    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
60    *
61    * @param e the exception to store, can be {@code null} to clear the previously stored exception
62    * @see #getLastError()
63    */
 
64  2 toggle private void setError(Exception e)
65    {
66  2 this.execution.getContext().setProperty(REPOSITORYERROR_KEY, e);
67    }
68   
69    /**
70    * Get the error generated while performing the previously called action.
71    *
72    * @return an eventual exception or {@code null} if no exception was thrown
73    */
 
74  0 toggle public Exception getLastError()
75    {
76  0 return (Exception) this.execution.getContext().getProperty(REPOSITORYERROR_KEY);
77    }
78   
 
79  0 toggle public void validateExtensions()
80    {
81  0 setError(null);
82   
83  0 try {
84  0 this.repositoryManager.validateExtensions();
85    } catch (Exception e) {
86  0 setError(e);
87    }
88    }
89   
 
90  2 toggle public DocumentReference importExtension(String extensionId, String repositoryId)
91    {
92  2 setError(null);
93   
94  2 try {
95  2 ExtensionRepository repository = this.extensionRepositoryManager.getRepository(repositoryId);
96   
97  2 if (repository == null) {
98  0 throw new ExtensionException("Can't find any registered repository with id [" + repositoryId + "]");
99    }
100   
101  2 return this.repositoryManager.importExtension(extensionId, repository, Version.Type.STABLE);
102    } catch (Exception e) {
103  0 setError(e);
104    }
105   
106  0 return null;
107    }
108    }