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

File ExtensionInstanceOutputFilterStream.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

10
29
8
1
181
117
15
0.52
3.62
8
1.88

Classes

Class Line # Actions
ExtensionInstanceOutputFilterStream 56 29 0% 15 13
0.723404272.3%
 

Contributing tests

This file is covered by 2 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.filter.instance.output;
21   
22    import java.io.IOException;
23    import java.util.Stack;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27   
28    import org.slf4j.Logger;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.annotation.InstantiationStrategy;
31    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
32    import org.xwiki.extension.Extension;
33    import org.xwiki.extension.ExtensionId;
34    import org.xwiki.extension.LocalExtension;
35    import org.xwiki.extension.ResolveException;
36    import org.xwiki.extension.internal.ExtensionFactory;
37    import org.xwiki.extension.repository.ExtensionRepositoryManager;
38    import org.xwiki.extension.repository.InstalledExtensionRepository;
39    import org.xwiki.extension.repository.LocalExtensionRepository;
40    import org.xwiki.filter.FilterEventParameters;
41    import org.xwiki.filter.FilterException;
42    import org.xwiki.filter.event.extension.ExtensionFilter;
43    import org.xwiki.filter.event.model.WikiFilter;
44    import org.xwiki.filter.output.AbstractBeanOutputFilterStream;
45    import org.xwiki.model.EntityType;
46    import org.xwiki.model.ModelContext;
47    import org.xwiki.model.reference.EntityReference;
48   
49    /**
50    * @version $Id: 83d48c87a16d143a8381bc8f1c86c0696ee5565a $
51    * @since 6.2M1
52    */
53    @Component
54    @Named(ExtensionInstanceOutputFilterStreamFactory.ROLEHINT)
55    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
56    public class ExtensionInstanceOutputFilterStream
57    extends AbstractBeanOutputFilterStream<ExtensionInstanceOutputProperties> implements ExtensionFilter, WikiFilter
58    {
59    private static final String WIKINAMESPACE = "wiki:";
60   
61    @Inject
62    private LocalExtensionRepository localRepository;
63   
64    @Inject
65    private ExtensionRepositoryManager extensionRepository;
66   
67    @Inject
68    private InstalledExtensionRepository installedRepository;
69   
70    @Inject
71    private ModelContext modelContext;
72   
73    @Inject
74    private ExtensionFactory factory;
75   
76    @Inject
77    private Logger logger;
78   
79    private Stack<String> currentNamespace = new Stack<>();
80   
 
81  0 toggle @Override
82    public void close() throws IOException
83    {
84    // Nothing to close
85    }
86   
87    // Events
88   
 
89  3 toggle private String getCurrentNamespace()
90    {
91  3 if (!this.currentNamespace.isEmpty()) {
92  2 return this.currentNamespace.peek();
93    }
94   
95  1 String namespace = null;
96   
97    // TODO: This makes impossible to register an extension at root level through events, should find a cleaner way
98  1 EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference();
99  1 if (currentEntityReference != null) {
100  0 namespace = WIKINAMESPACE + currentEntityReference.extractReference(EntityType.WIKI).getName();
101    }
102   
103  1 return namespace;
104    }
105   
 
106  1 toggle @Override
107    public void beginWiki(String name, FilterEventParameters parameters) throws FilterException
108    {
109  1 this.currentNamespace.push(WIKINAMESPACE + name);
110    }
111   
 
112  1 toggle @Override
113    public void endWiki(String name, FilterEventParameters parameters) throws FilterException
114    {
115  1 this.currentNamespace.pop();
116    }
117   
 
118  1 toggle @Override
119    public void beginNamespace(String name, FilterEventParameters parameters) throws FilterException
120    {
121  1 this.currentNamespace.push(name);
122    }
123   
 
124  1 toggle @Override
125    public void endNamespace(String name, FilterEventParameters parameters) throws FilterException
126    {
127  1 this.currentNamespace.pop();
128    }
129   
 
130  3 toggle @Override
131    public void beginExtension(String id, String version, FilterEventParameters parameters) throws FilterException
132    {
133    // TODO: add support for complete extension
134    }
135   
 
136  3 toggle @Override
137    public void endExtension(String id, String version, FilterEventParameters parameters) throws FilterException
138    {
139    // TODO: add support for complete extension
140   
141  3 ExtensionId extensionId = new ExtensionId(id, factory.getVersion(version));
142   
143  3 try {
144  3 LocalExtension localExtension = this.localRepository.getLocalExtension(extensionId);
145  3 if (localExtension == null) {
146  3 Extension extension;
147  3 try {
148    // Try to find and download the extension from a repository
149  3 extension = this.extensionRepository.resolve(extensionId);
150    } catch (ResolveException e) {
151  0 this.logger.debug("Can't find extension [{}]", extensionId, e);
152   
153    // FIXME: Create a dummy extension. Need support for partial/lazy extension.
154  0 return;
155    }
156   
157  3 localExtension = this.localRepository.storeExtension(extension);
158    }
159   
160  3 String namespace = getCurrentNamespace();
161   
162    // Make sure it's not already there
163    // TODO: should probably make it configurable
164  3 if (installedRepository.getInstalledExtension(localExtension.getId().getId(), namespace) == null) {
165  3 for (ExtensionId feature : localExtension.getExtensionFeatures()) {
166  0 if (installedRepository.getInstalledExtension(feature.getId(), namespace) != null) {
167    // Already exist so don't register it or it could create a mess
168  0 return;
169    }
170    }
171    } else {
172  0 return;
173    }
174   
175    // Register the extension as installed
176  3 installedRepository.installExtension(localExtension, namespace, false);
177    } catch (Exception e) {
178  0 this.logger.error("Failed to register extenion [{}] from the XAR", extensionId, e);
179    }
180    }
181    }