/
Activator.java
199 lines (178 loc) · 9.23 KB
/
Activator.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
* Copyright (c) 2010 Nicolas Roduit.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Nicolas Roduit - initial API and implementation
******************************************************************************/
package org.weasis.core.api.internal;
import java.awt.image.renderable.RenderedImageFactory;
import java.io.File;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.media.jai.JAI;
import javax.media.jai.OperationDescriptorImpl;
import javax.media.jai.OperationRegistry;
import javax.media.jai.RegistryElementDescriptor;
import javax.media.jai.registry.RIFRegistry;
import javax.media.jai.util.ImagingListener;
import org.apache.felix.prefs.BackingStore;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.weasis.core.api.gui.util.AbstractProperties;
import org.weasis.core.api.image.op.FormatBinaryDescriptor;
import org.weasis.core.api.image.op.ImageStatistics2Descriptor;
import org.weasis.core.api.image.op.ImageStatisticsDescriptor;
import org.weasis.core.api.image.op.NotBinaryDescriptor;
import org.weasis.core.api.image.op.RectifySignedShortDataDescriptor;
import org.weasis.core.api.image.op.RectifyUShortToShortDataDescriptor;
import org.weasis.core.api.image.op.ShutterDescriptor;
import org.weasis.core.api.image.op.ThresholdToBinDescriptor;
import org.weasis.core.api.image.util.ImageToolkit;
import org.weasis.core.api.media.data.Codec;
import org.weasis.core.api.service.AuditLog;
import org.weasis.core.api.service.BundleTools;
import org.weasis.core.api.service.DataFileBackingStoreImpl;
import org.weasis.core.api.util.ProxyDetector;
public class Activator implements BundleActivator, ServiceListener {
private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
private static final String codecFilter = String.format("(%s=%s)", Constants.OBJECTCLASS, Codec.class.getName()); //$NON-NLS-1$
private static BundleContext bundleContext;
@Override
public void start(final BundleContext context) throws Exception {
Activator.bundleContext = context;
context.registerService(BackingStore.class.getName(), new DataFileBackingStoreImpl(context), null);
context.addServiceListener(this, codecFilter);
ServiceTracker m_tracker = new ServiceTracker(context, Codec.class.getName(), null);
// Do not close tracker. It will uregister services
m_tracker.open();
Object[] services = m_tracker.getServices();
for (int i = 0; (services != null) && (i < services.length); i++) {
if (!BundleTools.CODEC_PLUGINS.contains(services[i]) && services[i] instanceof Codec) {
Codec codec = (Codec) services[i];
BundleTools.CODEC_PLUGINS.add(codec);
LOGGER.info("Register Codec Plug-in: {}", codec.getCodecName()); //$NON-NLS-1$
}
}
JAI jai = getJAI();
OperationRegistry or = jai.getOperationRegistry();
jai.setImagingListener(new ImagingListener() {
@Override
public boolean errorOccurred(String message, Throwable thrown, Object where, boolean isRetryable)
throws RuntimeException {
LOGGER.error("JAI error ocurred: {}", message); //$NON-NLS-1$
return false;
}
});
registerOp(or, new FormatBinaryDescriptor());
registerOp(or, new NotBinaryDescriptor());
registerOp(or, new ImageStatisticsDescriptor());
registerOp(or, new ImageStatistics2Descriptor());
registerOp(or, new ShutterDescriptor());
registerOp(or, new ThresholdToBinDescriptor());
registerOp(or, new RectifySignedShortDataDescriptor());
registerOp(or, new RectifyUShortToShortDataDescriptor());
// TODO manage memory setting ?
ImageToolkit.setJaiCacheMemoryCapacity(128);
// Allows to connect through a proxy initialized by Java Webstart
ProxyDetector.setProxyFromJavaWebStart();
initLoggerAndAudit();
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
Activator.bundleContext = null;
}
public static JAI getJAI() {
// Issue Resolution: necessary when jai already exist in JRE
// Change to the bundle classloader for loading the services providers (spi) correctly.
ClassLoader bundleClassLoader = JAI.class.getClassLoader();
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(bundleClassLoader);
JAI jai = JAI.getDefaultInstance();
Thread.currentThread().setContextClassLoader(originalClassLoader);
return jai;
}
public static void registerOp(OperationRegistry or, OperationDescriptorImpl descriptor) {
String name = descriptor.getName();
String[] mode = descriptor.getSupportedModes();
RegistryElementDescriptor val = or.getDescriptor(mode[0], name);
if (val == null) {
or.registerDescriptor(descriptor);
RIFRegistry.register(null, name, "org.weasis.core.api.image.op", (RenderedImageFactory) descriptor); //$NON-NLS-1$
}
}
@Override
public synchronized void serviceChanged(ServiceEvent event) {
ServiceReference m_ref = event.getServiceReference();
Codec codec = (Codec) bundleContext.getService(m_ref);
if (codec == null) {
return;
}
// TODO manage when several identical MimeType, register the default one
if (event.getType() == ServiceEvent.REGISTERED) {
if (!BundleTools.CODEC_PLUGINS.contains(codec)) {
BundleTools.CODEC_PLUGINS.add(codec);
LOGGER.info("Register Codec Plug-in: {}", codec.getCodecName()); //$NON-NLS-1$
}
} else if (event.getType() == ServiceEvent.UNREGISTERING) {
if (BundleTools.CODEC_PLUGINS.contains(codec)) {
LOGGER.info("Unregister Codec Plug-in: {}", codec.getCodecName()); //$NON-NLS-1$
BundleTools.CODEC_PLUGINS.remove(codec);
// Unget service object and null references.
bundleContext.ungetService(m_ref);
}
}
}
public static BundleContext getBundleContext() {
return bundleContext;
}
private void initLoggerAndAudit() throws IOException {
// Audit log for giving statistics about usage of Weasis
String loggerKey = "audit.log";
String[] loggerVal = new String[] { "org.weasis.core.api.service.AuditLog" };
// Activate audit log by adding an entry "audit.log=true" in Weasis.
String audit = bundleContext.getProperty(loggerKey);
if (audit != null && audit.equalsIgnoreCase("true")) {
String user = System.getProperty("weasis.user", System.getProperty("user.name", "local"));
AuditLog.createOrUpdateLogger(loggerKey, loggerVal, "DEBUG", AbstractProperties.WEASIS_PATH
+ File.separator + "log" + File.separator + "audit-" + user + ".log",
"{0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}* {5}", null, null);
AuditLog.LOGGER.info("Start audit log session");
} else {
ServiceReference configurationAdminReference =
bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
if (configurationAdminReference != null) {
ConfigurationAdmin confAdmin =
(ConfigurationAdmin) bundleContext.getService(configurationAdminReference);
if (confAdmin != null) {
Configuration logConfiguration = AuditLog.getLogConfiguration(confAdmin, loggerKey, loggerVal[0]);
if (logConfiguration == null) {
logConfiguration =
confAdmin.createFactoryConfiguration(
"org.apache.sling.commons.log.LogManager.factory.config", null);
Dictionary<String, Object> loggingProperties = new Hashtable<String, Object>();
loggingProperties.put("org.apache.sling.commons.log.level", "ERROR");
// loggingProperties.put("org.apache.sling.commons.log.file", "logs.log");
loggingProperties.put("org.apache.sling.commons.log.names", loggerVal);
// add this property to give us something unique to re-find this configuration
loggingProperties.put(loggerKey, loggerVal[0]);
logConfiguration.update(loggingProperties);
}
}
}
}
}
}