1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.imaging; |
35 |
|
|
36 |
|
import info.magnolia.context.SystemContext; |
37 |
|
import info.magnolia.module.ModuleLifecycle; |
38 |
|
import info.magnolia.module.ModuleLifecycleContext; |
39 |
|
import info.magnolia.objectfactory.Components; |
40 |
|
import info.magnolia.observation.WorkspaceEventListenerRegistration; |
41 |
|
import info.magnolia.repository.RepositoryConstants; |
42 |
|
|
43 |
|
import java.util.ArrayList; |
44 |
|
import java.util.Arrays; |
45 |
|
import java.util.Collection; |
46 |
|
import java.util.Iterator; |
47 |
|
import java.util.LinkedHashMap; |
48 |
|
import java.util.List; |
49 |
|
import java.util.Map; |
50 |
|
import java.util.TreeSet; |
51 |
|
|
52 |
|
import javax.imageio.ImageIO; |
53 |
|
import javax.imageio.spi.IIORegistry; |
54 |
|
import javax.imageio.spi.ServiceRegistry; |
55 |
|
import javax.inject.Inject; |
56 |
|
import javax.inject.Provider; |
57 |
|
import javax.jcr.RepositoryException; |
58 |
|
import javax.jcr.Session; |
59 |
|
import javax.jcr.observation.Event; |
60 |
|
|
61 |
|
import org.apache.commons.collections4.CollectionUtils; |
62 |
|
import org.apache.commons.collections4.Transformer; |
63 |
|
import org.apache.commons.lang3.StringUtils; |
64 |
|
import org.slf4j.Logger; |
65 |
|
import org.slf4j.LoggerFactory; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
|
|
| 69.6% |
Uncovered Elements: 21 (69) |
Complexity: 20 |
Complexity Density: 0.43 |
|
70 |
|
public class ImagingModule implements ModuleLifecycle { |
71 |
|
|
72 |
|
private static final Logger log = LoggerFactory.getLogger(ImagingModule.class); |
73 |
|
|
74 |
|
public static final String IMAGING = "imaging"; |
75 |
|
|
76 |
|
private static final Long GENERATOR_CACHE_FLUSH_DELAY = 1000L; |
77 |
|
|
78 |
|
private static final Long GENERATOR_CACHE_FLUSH_MAX_DELAY = 5000L; |
79 |
|
|
80 |
|
static final String GENERATORS_PATH = "/modules/imaging/config/generators"; |
81 |
|
|
82 |
|
private Provider<SystemContext> systemContextProvider; |
83 |
|
|
84 |
|
private WorkspaceEventListenerRegistration.Handle eventHandle; |
85 |
|
|
86 |
|
private Map<String, ImageGenerator> generators = new LinkedHashMap<String, ImageGenerator>(); |
87 |
|
|
88 |
|
private boolean storeGeneratedImages = true; |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
90 |
3 |
@Inject... |
91 |
|
public ImagingModule(Provider<SystemContext> systemContextProvider) { |
92 |
3 |
this.systemContextProvider = systemContextProvider; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
@deprecated@link |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
0 |
@Deprecated... |
99 |
|
public ImagingModule() { |
100 |
0 |
this(() -> Components.getComponent(SystemContext.class)); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@see |
107 |
|
|
|
|
| 76.5% |
Uncovered Elements: 8 (34) |
Complexity: 10 |
Complexity Density: 0.42 |
|
108 |
3 |
@Override... |
109 |
|
public void start(ModuleLifecycleContext moduleLifecycleContext) { |
110 |
3 |
ImageIO.scanForPlugins(); |
111 |
|
|
112 |
|
|
113 |
3 |
if (log.isDebugEnabled()) { |
114 |
0 |
log.debug("This lists the formats currently available to the javax.imageio package, as installed on this system."); |
115 |
0 |
log.debug("Supported input formats: {}", StringUtils.join(filter(ImageIO.getReaderFormatNames()), ", ")); |
116 |
0 |
log.debug("Supported input mime types: {}", StringUtils.join(filter(ImageIO.getReaderMIMETypes()), ", ")); |
117 |
0 |
log.debug("Supported output formats: {}", StringUtils.join(filter(ImageIO.getWriterFormatNames()), ", ")); |
118 |
0 |
log.debug("Supported output mime types: {}", StringUtils.join(filter(ImageIO.getWriterMIMETypes()), ", ")); |
119 |
|
} |
120 |
|
|
121 |
3 |
if (moduleLifecycleContext.getPhase() == ModuleLifecycleContext.PHASE_SYSTEM_STARTUP) { |
122 |
1 |
try { |
123 |
1 |
eventHandle = WorkspaceEventListenerRegistration.observe(RepositoryConstants.CONFIG, GENERATORS_PATH, events -> { |
124 |
19 |
while (events.hasNext()) { |
125 |
18 |
Event event = events.nextEvent(); |
126 |
18 |
String generatorPath = null; |
127 |
18 |
try { |
128 |
18 |
generatorPath = StringUtils.substringAfter(event.getPath(), GENERATORS_PATH); |
129 |
18 |
int idx = StringUtils.indexOf(generatorPath, "/", 1); |
130 |
18 |
if (idx > 0) { |
131 |
9 |
generatorPath = StringUtils.substring(generatorPath, 0, idx); |
132 |
|
} |
133 |
18 |
Session session = systemContextProvider.get().getJCRSession(IMAGING); |
134 |
18 |
if (!generatorPath.startsWith("jcr:") && !generatorPath.startsWith("mgnl:") && session.nodeExists(generatorPath)) { |
135 |
1 |
session.removeItem(generatorPath); |
136 |
1 |
session.save(); |
137 |
|
} |
138 |
|
} catch (RepositoryException e) { |
139 |
0 |
log.warn("Unable to flush imaging cache for generator [{}].", generatorPath, e); |
140 |
|
} |
141 |
|
} |
142 |
|
}).withDelay(GENERATOR_CACHE_FLUSH_DELAY, GENERATOR_CACHE_FLUSH_MAX_DELAY).withSubNodes(true).register(); |
143 |
|
} catch (RepositoryException e) { |
144 |
0 |
log.warn("Unable to register image cache flush listener.", e); |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
@link |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
|
|
| 81.8% |
Uncovered Elements: 4 (22) |
Complexity: 6 |
Complexity Density: 0.38 |
|
156 |
1 |
@Override... |
157 |
|
public void stop(ModuleLifecycleContext moduleLifecycleContext) { |
158 |
|
|
159 |
|
|
160 |
1 |
final IIORegistry registry = IIORegistry.getDefaultInstance(); |
161 |
1 |
final LocalFilter localFilter = new LocalFilter(Thread.currentThread().getContextClassLoader()); |
162 |
|
|
163 |
1 |
Iterator<Class<?>> categories = registry.getCategories(); |
164 |
|
|
165 |
6 |
while (categories.hasNext()) { |
166 |
5 |
Class<?> category = categories.next(); |
167 |
5 |
Iterator<?> providers = registry.getServiceProviders(category, localFilter, false); |
168 |
|
|
169 |
|
|
170 |
5 |
List<Object> providersCopy = new ArrayList<>(); |
171 |
9 |
while (providers.hasNext()) { |
172 |
4 |
providersCopy.add(providers.next()); |
173 |
|
} |
174 |
|
|
175 |
5 |
for (Object provider : providersCopy) { |
176 |
4 |
registry.deregisterServiceProvider(provider); |
177 |
4 |
log.debug("Unregistered locally installed provider class: {}", provider.getClass()); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
1 |
if (eventHandle != null && moduleLifecycleContext.getPhase() == ModuleLifecycleContext.PHASE_SYSTEM_SHUTDOWN) { |
182 |
0 |
try { |
183 |
0 |
eventHandle.unregister(); |
184 |
|
} catch (RepositoryException e) { |
185 |
0 |
log.warn("Unable to unregister image cache flush listener.", e); |
186 |
|
} |
187 |
|
} |
188 |
|
} |
189 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
190 |
|
public void setGenerators(Map<String, ImageGenerator> generators) {... |
191 |
|
this.generators = generators; |
192 |
|
} |
193 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
194 |
|
public Map<String, ImageGenerator> getGenerators() {... |
195 |
|
return generators; |
196 |
|
} |
197 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
198 |
|
public boolean isStoreGeneratedImages() {... |
199 |
|
return storeGeneratedImages; |
200 |
|
} |
201 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
202 |
|
public void setStoreGeneratedImages(boolean storeGeneratedImages) {... |
203 |
|
this.storeGeneratedImages = storeGeneratedImages; |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
209 |
|
static class LocalFilter implements ServiceRegistry.Filter { |
210 |
|
private final ClassLoader loader; |
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
212 |
1 |
public LocalFilter(ClassLoader loader) {... |
213 |
1 |
this.loader = loader; |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
216 |
20 |
@Override... |
217 |
|
public boolean filter(Object provider) { |
218 |
20 |
return provider.getClass().getClassLoader() == loader; |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
225 |
0 |
private Collection<String> filter(String... formats) {... |
226 |
0 |
final TreeSet<String> set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); |
227 |
0 |
set.addAll(Arrays.asList(formats)); |
228 |
0 |
CollectionUtils.transform(set, new Transformer() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
0 |
@Override... |
230 |
|
public Object transform(Object input) { |
231 |
0 |
return ((String) input).toLowerCase(); |
232 |
|
} |
233 |
|
}); |
234 |
0 |
return set; |
235 |
|
} |
236 |
|
} |