Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 32295195d0b43cddd85550cfc85e7c776783fbf2 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2010, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.bidi.internal;

import java.util.Locale;
import org.eclipse.osgi.framework.log.FrameworkLog;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.service.localization.LocaleProvider;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;

/**
 * Provides services related to OSGi bundles.
 */
public class StructuredTextActivator implements BundleActivator {

	private ServiceTracker logTracker = null;
	private BundleContext bundleContext;
	private static StructuredTextActivator instance;

	public StructuredTextActivator() {
		instance = this; // there is only one bundle activator
	}

	public void start(BundleContext context) throws Exception {
		bundleContext = context;
		instance = this;
	}

	public void stop(BundleContext context) throws Exception {
		if (logTracker != null) {
			logTracker.close();
			logTracker = null;
		}
		bundleContext = null;
	}

	/**
	 * @return the bundle instance, or <code>null</code> iff OSGi is not running
	 */
	public static StructuredTextActivator getInstance() {
		return instance;
	}

	/**
	 * Returns the value of the specified property. If OSGi is not running or the key is not found in
	 * the Framework properties, the system properties are then searched.
	 * <p>
	 * This method can be used without OSGi running.
	 * </p>
	 *
	 * @param key the name of the requested property
	 * @return the value of the requested property, or {@code null} if the property is undefined
	 */
	public static String getProperty(String key) {
		if (instance != null)
			return instance.bundleContext.getProperty(key);
		return System.getProperty(key);
	}

	public Locale getDefaultLocale() {
		// use OSGi service
		ServiceReference[] references = null;
		try {
			references = bundleContext.getAllServiceReferences(null, LocaleProvider.class.getName());
		} catch (InvalidSyntaxException e) {
			// do nothing
		}
		if (references == null || references.length < 1)
			return Locale.getDefault();
		Object service = bundleContext.getService(references[0]);
		LocaleProvider localeProvider = (LocaleProvider) service;
		if (localeProvider != null) {
			Locale currentLocale = localeProvider.getLocale();
			bundleContext.ungetService(references[0]);
			if (currentLocale != null)
				return currentLocale;
		}
		return Locale.getDefault();
	}

	private FrameworkLog getFrameworkLog() {
		if (logTracker == null) {
			logTracker = new ServiceTracker(bundleContext, FrameworkLog.class.getName(), null);
			logTracker.open();
		}
		return (FrameworkLog) logTracker.getService();
	}

	static public void logError(String message, Exception e) {
		if (instance != null) {
			FrameworkLog frameworkLog = instance.getFrameworkLog();
			if (frameworkLog != null) {
				frameworkLog.log(new FrameworkLogEntry("org.eclipse.equinox.bidi", FrameworkLogEntry.ERROR, 1, message, 0, e, null)); //$NON-NLS-1$
				return;
			}
		}
		System.err.println(message);
		if (e != null)
			e.printStackTrace();
	}

}

Back to the top