use of org.osgi.service.log.LogService in project eclipse.platform.runtime by eclipse.
the class ResourceBundleHelper method getResourceBundleForUri.
/**
* Parses the specified contribution URI and loads the {@link ResourceBundle} for the specified
* {@link Locale} out of an OSGi {@link Bundle}.
* <p>
* Following URIs are supported:
* <ul>
* <li>platform:/[plugin|fragment]/[Bundle-SymbolicName]<br>
* Load the OSGi resource bundle out of the bundle/fragment named [Bundle-SymbolicName]</li>
* <li>platform:/[plugin|fragment]/[Bundle-SymbolicName]/[Path]/[Basename]<br>
* Load the resource bundle specified by [Path] and [Basename] out of the bundle/fragment named
* [Bundle-SymbolicName].</li>
* <li>bundleclass://[plugin|fragment]/[Full-Qualified-Classname]<br>
* Instantiate the class specified by [Full-Qualified-Classname] out of the bundle/fragment
* named [Bundle-SymbolicName]. Note that the class needs to be a subtype of
* {@link ResourceBundle}.</li>
* </ul>
* </p>
*
* @param contributionURI
* The URI that points to a {@link ResourceBundle}
* @param locale
* The {@link Locale} to use for loading the {@link ResourceBundle}
* @param provider
* The service for retrieving a {@link ResourceBundle} for a given {@link Locale} out
* of the given {@link Bundle} which is specified by URI.
* @return
*/
public static ResourceBundle getResourceBundleForUri(String contributionURI, Locale locale, ResourceBundleProvider provider) {
if (contributionURI == null)
return null;
LogService logService = logTracker.getService();
URI uri;
try {
uri = new URI(contributionURI);
} catch (URISyntaxException e) {
if (logService != null)
logService.log(LogService.LOG_ERROR, // $NON-NLS-1$
"Invalid contribution URI: " + contributionURI);
return null;
}
String bundleName = null;
Bundle bundle = null;
String resourcePath = null;
String classPath = null;
// the uri follows the platform schema, so we search for .properties files in the bundle
if (PLATFORM_SCHEMA.equals(uri.getScheme())) {
bundleName = uri.getPath();
if (bundleName.startsWith(PLUGIN_SEGMENT))
bundleName = bundleName.substring(PLUGIN_SEGMENT.length());
else if (bundleName.startsWith(FRAGMENT_SEGMENT))
bundleName = bundleName.substring(FRAGMENT_SEGMENT.length());
// $NON-NLS-1$
resourcePath = "";
if (bundleName.contains(PATH_SEPARATOR)) {
resourcePath = bundleName.substring(bundleName.indexOf(PATH_SEPARATOR) + 1);
bundleName = bundleName.substring(0, bundleName.indexOf(PATH_SEPARATOR));
}
} else if (BUNDLECLASS_SCHEMA.equals(uri.getScheme())) {
if (uri.getAuthority() == null) {
if (logService != null)
logService.log(LogService.LOG_ERROR, // $NON-NLS-1$
"Failed to get bundle for: " + contributionURI);
}
bundleName = uri.getAuthority();
// remove the leading /
if (uri.getPath() != null && uri.getPath().length() > 0) {
classPath = uri.getPath().substring(1);
} else {
if (logService != null) {
logService.log(LogService.LOG_ERROR, "Called with invalid contribution URI: " + contributionURI);
}
}
}
ResourceBundle result = null;
if (bundleName != null) {
bundle = getBundleForName(bundleName);
if (bundle != null) {
if (resourcePath == null && classPath != null) {
// therefore we are trying to instantiate the class
try {
Class<?> resourceBundleClass = bundle.loadClass(classPath);
result = getEquinoxResourceBundle(classPath, locale, resourceBundleClass.getClassLoader());
} catch (Exception e) {
if (logService != null)
logService.log(LogService.LOG_ERROR, "Failed to load specified ResourceBundle: " + contributionURI, // $NON-NLS-1$
e);
}
} else if (resourcePath != null && resourcePath.length() > 0) {
// the specified URI points to a resource
// therefore we try to load the .properties files into a ResourceBundle
result = getEquinoxResourceBundle(resourcePath.replace('.', '/'), locale, bundle);
} else {
// There is no class and no special resource specified within the URI
// therefore we load the OSresource bundle out of the specified Bundle
// for the current Locale. Typically this will be the OSGi ResourceBundle
result = provider.getResourceBundle(bundle, locale.toString());
}
}
}
return result;
}
use of org.osgi.service.log.LogService in project vespa by vespa-engine.
the class OsgiLogManager method install.
@SuppressWarnings("unchecked")
public void install(final BundleContext osgiContext) {
if (tracker != null) {
throw new IllegalStateException("OsgiLogManager already installed.");
}
tracker = new ServiceTracker<>(osgiContext, LogService.class, new ServiceTrackerCustomizer<LogService, LogService>() {
@Override
public LogService addingService(ServiceReference<LogService> reference) {
LogService service = osgiContext.getService(reference);
services.add(service);
return service;
}
@Override
public void modifiedService(ServiceReference<LogService> reference, LogService service) {
}
@Override
public void removedService(ServiceReference<LogService> reference, LogService service) {
services.remove(service);
}
});
tracker.open();
synchronized (globalLock) {
Logger root = Logger.getLogger("");
if (configureLogLevel) {
root.setLevel(Level.ALL);
}
for (Handler handler : root.getHandlers()) {
root.removeHandler(handler);
}
root.addHandler(new OsgiLogHandler(this));
}
}
use of org.osgi.service.log.LogService in project vespa by vespa-engine.
the class OsgiLogManagerTestCase method requireThatLogManagerWritesToAllRegisteredLogServices.
@Test
public void requireThatLogManagerWritesToAllRegisteredLogServices() throws BundleException {
FelixFramework felix = TestDriver.newOsgiFramework();
felix.start();
BundleContext ctx = felix.bundleContext();
MyLogService foo = new MyLogService();
ServiceRegistration<LogService> fooReg = ctx.registerService(LogService.class, foo, null);
OsgiLogManager manager = new OsgiLogManager(true);
manager.install(ctx);
ServiceReference<?> ref1 = Mockito.mock(ServiceReference.class);
Throwable t1 = new Throwable();
manager.log(ref1, 2, "a", t1);
assertLast(foo, ref1, 2, "a", t1);
MyLogService bar = new MyLogService();
ServiceRegistration<LogService> barReg = ctx.registerService(LogService.class, bar, null);
ServiceReference<?> ref2 = Mockito.mock(ServiceReference.class);
Throwable t2 = new Throwable();
manager.log(ref2, 4, "b", t2);
assertLast(foo, ref2, 4, "b", t2);
assertLast(bar, ref2, 4, "b", t2);
MyLogService baz = new MyLogService();
ServiceRegistration<LogService> bazReg = ctx.registerService(LogService.class, baz, null);
ServiceReference<?> ref3 = Mockito.mock(ServiceReference.class);
Throwable t3 = new Throwable();
manager.log(ref3, 8, "c", t3);
assertLast(foo, ref3, 8, "c", t3);
assertLast(bar, ref3, 8, "c", t3);
assertLast(baz, ref3, 8, "c", t3);
fooReg.unregister();
ServiceReference<?> ref4 = Mockito.mock(ServiceReference.class);
Throwable t4 = new Throwable();
manager.log(ref4, 16, "d", t4);
assertLast(foo, ref3, 8, "c", t3);
assertLast(bar, ref4, 16, "d", t4);
assertLast(baz, ref4, 16, "d", t4);
barReg.unregister();
ServiceReference<?> ref5 = Mockito.mock(ServiceReference.class);
Throwable t5 = new Throwable();
manager.log(ref5, 32, "e", t5);
assertLast(foo, ref3, 8, "c", t3);
assertLast(bar, ref4, 16, "d", t4);
assertLast(baz, ref5, 32, "e", t5);
bazReg.unregister();
ServiceReference<?> ref6 = Mockito.mock(ServiceReference.class);
Throwable t6 = new Throwable();
manager.log(ref6, 64, "f", t6);
assertLast(foo, ref3, 8, "c", t3);
assertLast(bar, ref4, 16, "d", t4);
assertLast(baz, ref5, 32, "e", t5);
manager.uninstall();
felix.stop();
}
use of org.osgi.service.log.LogService in project bnd by bndtools.
the class LogTracker method log.
public void log(ServiceReference sr, int level, String message, Throwable exception) {
LogService log = getService();
if (log != null)
log.log(sr, level, message, exception);
else {
PrintStream stream = (level <= LogService.LOG_WARNING) ? System.err : System.out;
if (message == null)
message = "";
Date now = new Date();
stream.println(String.format("[%-7s] %tF %tT: %s", LogUtils.formatLogLevel(level), now, now, message));
if (exception != null)
exception.printStackTrace(stream);
}
}
use of org.osgi.service.log.LogService in project bnd by bndtools.
the class TestIndexer method testRemoveDisallowed.
public void testRemoveDisallowed() throws Exception {
LogService log = mock(LogService.class);
RepoIndex indexer = new RepoIndex(log);
indexer.addAnalyzer(new NaughtyAnalyzer(), null);
Map<String, String> props = new HashMap<String, String>();
props.put(ResourceIndexer.ROOT_URL, new File("testdata").getAbsoluteFile().toURI().toURL().toString());
StringWriter writer = new StringWriter();
indexer.indexFragment(Collections.singleton(new File("testdata/subdir/01-bsn+version.jar")), writer, props);
verify(log).log(eq(LogService.LOG_ERROR), anyString(), isA(UnsupportedOperationException.class));
}
Aggregations