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

File XarScriptService.java

 

Coverage histogram

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

Code metrics

8
23
5
1
174
89
14
0.61
4.6
5
2.8

Classes

Class Line # Actions
XarScriptService 53 23 0% 14 19
0.472222247.2%
 

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.xar.script;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.io.InputStream;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.configuration.ConfigurationSource;
34    import org.xwiki.context.Execution;
35    import org.xwiki.filter.input.InputFilterStreamFactory;
36    import org.xwiki.filter.output.OutputFilterStreamFactory;
37    import org.xwiki.filter.type.FilterStreamType;
38    import org.xwiki.script.service.ScriptService;
39    import org.xwiki.stability.Unstable;
40    import org.xwiki.xar.XarPackage;
41   
42    import com.xpn.xwiki.XWikiContext;
43   
44    /**
45    * Provide APIs to manipulate XAR files.
46    *
47    * @version $Id: 5421a8295b7c6b7f14c76c497d1b60814872a918 $
48    * @since 5.4RC1
49    */
50    @Component
51    @Named("xar")
52    @Singleton
 
53    public class XarScriptService implements ScriptService
54    {
55    /**
56    * The key under which the last encountered error is stored in the current execution context.
57    */
58    public static final String ERROR_KEY = "scriptservice.xar.error";
59   
60    private static final String EXPORT_USEFILTER_KEY = "xwiki.action.export.xar.usefilter";
61   
62    /**
63    * Provides access to the current context.
64    */
65    @Inject
66    protected Execution execution;
67   
68    @Inject
69    @Named("xwikicfg")
70    private ConfigurationSource xwikiCfgConfigurationSource;
71   
72    @Inject
73    @Named("context")
74    private Provider<ComponentManager> contextComponentManagerProvider;
75   
76    // Error management
77   
78    /**
79    * Get the error generated while performing the previously called action.
80    *
81    * @return an eventual exception or {@code null} if no exception was thrown
82    */
 
83  0 toggle public Exception getLastError()
84    {
85  0 return (Exception) this.execution.getContext().getProperty(ERROR_KEY);
86    }
87   
88    /**
89    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
90    *
91    * @param e the exception to store, can be {@code null} to clear the previously stored exception
92    * @see #getLastError()
93    */
 
94  0 toggle protected void setError(Exception e)
95    {
96  0 this.execution.getContext().setProperty(ERROR_KEY, e);
97    }
98   
99    // ScriptService
100   
101    /**
102    * Generate a {@link XarPackage} from the passed XAR file.
103    *
104    * @param file the XAR file
105    * @return the package
106    */
 
107  0 toggle public XarPackage getXarPackage(File file)
108    {
109  0 try {
110  0 return new XarPackage(file);
111    } catch (Exception e) {
112  0 setError(e);
113    }
114   
115  0 return null;
116    }
117   
118    /**
119    * Generate a {@link XarPackage} from the passed XAR file.
120    *
121    * @param stream the XAR file
122    * @param close indicate if the passed stream should be closed at the end
123    * @return the package
124    */
 
125  4 toggle public XarPackage getXarPackage(InputStream stream, boolean close)
126    {
127  4 try {
128  4 return new XarPackage(stream);
129    } catch (Exception e) {
130  0 setError(e);
131    } finally {
132  4 if (close) {
133  4 try {
134  4 stream.close();
135    } catch (IOException e) {
136    // TODO: log something ?
137    }
138    }
139    }
140   
141  0 return null;
142    }
143   
144    /**
145    * @return true if the XAR export feature is available in the current XWiki instance, false otherwise
146    * @since 8.3RC1
147    */
 
148  271 toggle @Unstable
149    public boolean isXARExportAvailable()
150    {
151  271 boolean available = false;
152   
153    // Check the value of the xwiki.action.export.xar.usefilter config parameter in xwiki.cfg
154  271 int useFilter = this.xwikiCfgConfigurationSource.getProperty(EXPORT_USEFILTER_KEY, (Integer) 1);
155  271 if (useFilter == 1) {
156    // Are the following 2 components available?
157  271 ComponentManager cm = this.contextComponentManagerProvider.get();
158  271 if (cm.hasComponent(InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize())
159    && cm.hasComponent(OutputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize()))
160    {
161  271 available = true;
162    }
163    } else {
164    // We're using the old packager plugin, verify it's there
165  0 XWikiContext xcontext =
166    (XWikiContext) this.execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
167  0 if (xcontext != null && xcontext.getWiki().getPlugin("package", xcontext) != null) {
168  0 available = true;
169    }
170    }
171   
172  271 return available;
173    }
174    }