org.osgi.service.log.LogReaderService Java Examples

The following examples show how to use org.osgi.service.log.LogReaderService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OsgiLogServiceIntegrationTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void requireThatAllSupportedLogFrameworksAreConfigured() throws Exception {
    // need to explicitly set log level of root logger since integration suite now provides a logger config file,
    // which disables that setLevel() call of the OsgiLogManager.
    Logger.getLogger("").setLevel(Level.INFO);

    long now = System.currentTimeMillis();
    TestDriver driver = TestDriver.newApplicationBundleInstance("app-h-log.jar", false);
    BundleContext ctx = driver.osgiFramework().bundleContext();
    ServiceReference<?> ref = ctx.getServiceReference(LogReaderService.class.getName());
    LogReaderService reader = (LogReaderService)ctx.getService(ref);
    ArrayList<LogEntry> logEntries = Collections.list(reader.getLog());
    assertTrue(logEntries.size() >= 4);

    assertLogContainsEntry("[jdk14] hello world", logEntries, now);
    assertLogContainsEntry("[slf4j] hello world", logEntries, now);
    assertLogContainsEntry("[log4j] hello world", logEntries, now);
    assertLogContainsEntry("[jcl] hello world", logEntries, now);

    assertTrue(driver.close());
}
 
Example #2
Source File: OsgiLogServiceTestCase.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
public void requireThatLogServiceIsRegistered() throws BundleException, InterruptedException {
    OsgiFramework osgi = TestDriver.newOsgiFramework();
    osgi.start();

    ServiceTracker<?,?> logs = newTracker(osgi, LogService.class);
    ServiceTracker<?,?> logReaders = newTracker(osgi, LogReaderService.class);
    assertEquals(1, logs.getTrackingCount());
    assertEquals(1, logReaders.getTrackingCount());

    OsgiLogService service = new OsgiLogService();
    service.start(osgi.bundleContext());

    assertEquals(2, logs.getTrackingCount());
    assertEquals(2, logReaders.getTrackingCount());
    osgi.stop();
}
 
Example #3
Source File: LoggingApplication.java    From osgi.enroute.examples with Apache License 2.0 6 votes vote down vote up
@Reference
void setLogReader(LogReaderService reader) {
	reader.addLogListener(e -> {
		switch (e.getLevel()) {
		case LogService.LOG_DEBUG:
			System.out.println("DEBUG:::: " + e.getMessage());
			break;

		case LogService.LOG_INFO:
			System.out.println("INFO::::: " + e.getMessage());
			break;
		case LogService.LOG_WARNING:
			System.out.println("WARNING:: " + e.getMessage());
			break;
		case LogService.LOG_ERROR:
			System.out.println("ERROR:::: " + e.getMessage());
			break;
		}
	});
}
 
Example #4
Source File: LogTracker.java    From osgi.iot.contest.sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Tracks Log Services. Will merge all records if there are several.
 */
public Object addingService(ServiceReference ref) {
	LogReaderService lrs = (LogReaderService) super.addingService(ref);
	lrs.addLogListener(new LogListener() {

		public void logged(LogEntry entry) {
			queue.offer(entry);
		}

	});
	Enumeration<LogEntry> e = lrs.getLog();
	while (e.hasMoreElements())
		queue.offer(e.nextElement());

	return lrs;
}
 
Example #5
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void open()
{
  final String filter = "(objectclass=" + LogReaderService.class.getName() + ")";

  try {
    final ServiceReference<?>[] srl =
      bc.getServiceReferences((String) null, filter);
    for (int i = 0; srl != null && i < srl.length; i++) {

      serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, srl[i]));
    }
    bc.addServiceListener(this, filter);
  } catch (final Exception e) {
    e.printStackTrace();
  }
}
 
Example #6
Source File: ConsoleLogManager.java    From vespa with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void install(final BundleContext osgiContext) {
    if (tracker != null) {
        throw new IllegalStateException("ConsoleLogManager already installed.");
    }
    tracker = new ServiceTracker<LogReaderService,LogReaderService>(osgiContext, LogReaderService.class.getName(),
        new ServiceTrackerCustomizer<LogReaderService,LogReaderService>() {

        @Override
        public LogReaderService addingService(ServiceReference<LogReaderService> reference) {
            LogReaderService service = osgiContext.getService(reference);
            service.addLogListener(listener);
            return service;
        }

        @Override
        public void modifiedService(ServiceReference<LogReaderService> reference, LogReaderService service) {

        }

        @Override
        public void removedService(ServiceReference<LogReaderService> reference, LogReaderService service) {
            service.removeLogListener(listener);
        }
    });
    tracker.open();
}
 
Example #7
Source File: ConsoleLogManagerTestCase.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Test
public void requireThatLogReaderServicesAreTracked() throws BundleException {
    FelixFramework felix = TestDriver.newOsgiFramework();
    felix.start();
    BundleContext ctx = felix.bundleContext();

    LogReaderService foo = Mockito.mock(LogReaderService.class);
    ctx.registerService(LogReaderService.class.getName(), foo, null);
    Mockito.verify(foo).addLogListener(Mockito.any(LogListener.class));

    LogReaderService bar = Mockito.mock(LogReaderService.class);
    ctx.registerService(LogReaderService.class.getName(), bar, null);
    Mockito.verify(bar).addLogListener(Mockito.any(LogListener.class));

    ConsoleLogManager manager = new ConsoleLogManager();
    manager.install(felix.bundleContext());

    Mockito.verify(foo, Mockito.times(2)).addLogListener(Mockito.any(LogListener.class));
    Mockito.verify(bar, Mockito.times(2)).addLogListener(Mockito.any(LogListener.class));

    LogReaderService baz = Mockito.mock(LogReaderService.class);
    ctx.registerService(LogReaderService.class.getName(), baz, null);
    Mockito.verify(baz, Mockito.times(2)).addLogListener(Mockito.any(LogListener.class));

    assertTrue(manager.uninstall());

    Mockito.verify(foo).removeLogListener(Mockito.any(LogListener.class));
    Mockito.verify(bar).removeLogListener(Mockito.any(LogListener.class));
    Mockito.verify(baz).removeLogListener(Mockito.any(LogListener.class));

    felix.stop();

    Mockito.verify(foo, Mockito.times(2)).removeLogListener(Mockito.any(LogListener.class));
    Mockito.verify(bar, Mockito.times(2)).removeLogListener(Mockito.any(LogListener.class));
    Mockito.verify(baz, Mockito.times(2)).removeLogListener(Mockito.any(LogListener.class));
}
 
Example #8
Source File: LogReaderServiceFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Each Bundle gets its own LogReaderServiceImpl, and the
 * LogReaderServiceFactory keeps track of the created LogReaderServices.
 */
public LogReaderService getService(Bundle bc,
                                   ServiceRegistration<LogReaderService> sd)
{
  LogReaderServiceImpl lrsi = new LogReaderServiceImpl(this);
  logReaderServicies.put(lrsi, bc);
  return lrsi;
}
 
Example #9
Source File: LogReaderServiceTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testExtendedLogReaderServiceAvailable() throws Exception {
    Framework f = IntegrationTest.findFramework();
    BundleContext bc = f.getBundleContext();
    
    ServiceTracker logReaderTracker = new ServiceTracker(bc, ExtendedLogReaderService.class.getName(), null);
    logReaderTracker.open();
            
    LogReaderService logReader = (ExtendedLogReaderService) logReaderTracker.getService();
    assertNotNull(logReader);
        
}
 
Example #10
Source File: MultiListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LogReaderService addingService(ServiceReference<LogReaderService> sr) {
  LogReaderService logReader = (LogReaderService) Activator.bc.getService(sr);
  if (logReader != null) {
    logReader.addLogListener(MultiListener.this);
  }
  return logReader;
}
 
Example #11
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void getAll()
{
  // Note that the enumeration of log entries returned by LogReader.getLog()
  // is in the reverse order (latest entry first).
  final ArrayList <LogEntry> entries = new ArrayList<LogEntry>();
  for (final LogReaderService lr : logReaders.values()) {
    for (@SuppressWarnings("unchecked")
    final Enumeration<LogEntry> e = lr.getLog(); e.hasMoreElements();) {
      final LogEntry entry = e.nextElement();
      entries.add(entry);
    }
  }
  Collections.reverse(entries);
  logged(entries);
}
 
Example #12
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void close()
{
  bc.removeServiceListener(this);

  for (final ServiceReference<LogReaderService> sr : logReaders.keySet()) {
    serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, sr));
  }
  logReaders.clear();
}
 
Example #13
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void serviceChanged(ServiceEvent ev)
{
  @SuppressWarnings("unchecked")
  final
  ServiceReference<LogReaderService> sr =
    (ServiceReference<LogReaderService>) ev.getServiceReference();

  final LogReaderService lr =
    logReaders.containsKey(sr) ? logReaders.get(sr) : (LogReaderService) bc
        .getService(sr);

  if (null != lr) {
    switch (ev.getType()) {
    case ServiceEvent.REGISTERED:
      lr.addLogListener(this);
      logReaders.put(sr, lr);
      break;
    case ServiceEvent.MODIFIED:
      break;
    case ServiceEvent.UNREGISTERING:
      lr.removeLogListener(this);
      logReaders.remove(sr);
      bc.ungetService(sr);
      break;
    }
  }
}
 
Example #14
Source File: LogTracker.java    From osgi.iot.contest.sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 * 
 */
public LogTracker(BundleContext context) {
	super(context, LogReaderService.class.getName(), null);
	this.context = context;
	super.open();
	thread.start();
}
 
Example #15
Source File: OsgiSl4fjLoggingAdapter.java    From elexis-3-core with Eclipse Public License 1.0 4 votes vote down vote up
public void unbindLogReaderService(LogReaderService logReaderService){
	logReaderService.removeLogListener(this);
}
 
Example #16
Source File: OsgiSl4fjLoggingAdapter.java    From elexis-3-core with Eclipse Public License 1.0 4 votes vote down vote up
@Reference
public void bindLogReaderService(LogReaderService logReaderService){
	logReaderService.addLogListener(this);
}
 
Example #17
Source File: ComponentTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void runTest() {
  Bundle c1 = null;
  ServiceReference<?> sr = null;
  ServiceRegistration<?> reg = null;
  ServiceRegistration<?> reg2 = null;
  try {
    counter = 0;
    gotCircularError = false;
    sr = bc.getServiceReference(LogReaderService.class.getName());
    LogReaderService lrs = (LogReaderService)bc.getService(sr);
    lrs.addLogListener(this);
    c1 = Util.installBundle(bc, "componentA_test-1.0.1.jar");
    c1.start();

    Thread.sleep(SLEEP_TIME);

    assertNull("Should be null (1)", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentA"));
    assertNull("Should be null (2)", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
    assertNull("Should be null (3)", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
    assertEquals("Should not have been bumped", 0, counter);
    assertTrue("Should have got circular error message", gotCircularError);
    lrs.removeLogListener(this);

    reg2 = bc.registerService(TestService2.class.getName(), new TestService2(), new Hashtable<String, Object>());
    reg = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String, Object>());

    Thread.sleep(SLEEP_TIME);

    ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentA");
    assertNotNull("Should get service A", bc.getService(ref));

    ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB");
    assertNotNull("Should get service B", bc.getService(ref));

    ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC");
    assertNotNull("Should get service C", bc.getService(ref));

    assertEquals("Should have been activate/bind bumped", 103, counter);
    reg.unregister();
    reg = null;

    Thread.sleep(SLEEP_TIME);
    assertNull("Should be null (1(2))", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentA"));
    assertNull("Should be null (2(2))", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
    assertNull("Should be null (3(2))", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
    assertEquals("Should have been bind/2*unbind and deactive bumped", 2233, counter);

    counter = 0;
  } catch (Exception e) {
    e.printStackTrace();
    fail("Test2b: got unexpected exception " + e);
  } finally {
    if (c1 != null) {
      try {
        c1.uninstall();
      } catch (BundleException be) {
        be.printStackTrace();
        fail("Test2b: got uninstall exception " + be);
      }
    }
    if (sr != null) {
      bc.ungetService(sr);
    }
    if (reg != null) {
      reg.unregister();
    }
    if (reg2 != null) {
      reg2.unregister();
    }
  }
}
 
Example #18
Source File: MultiListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * A listener for service events
 * @param serviceEvent the event sent by the service
 * @author Johnny Baveras
 */
public void serviceChanged(ServiceEvent serviceEvent) {
  String topic = null;
  boolean knownMessageType = true;

  switch (serviceEvent.getType()) {
  case ServiceEvent.REGISTERED:
    topic = SERVICE_EVENT_REGISTERED_TOPIC;

    
    String[] objectClass = (String[]) serviceEvent.getServiceReference().getProperty(Constants.OBJECTCLASS);
    boolean isLogReaderService = false;
    if (objectClass != null) {
      for (int i=0; i<objectClass.length; i++) {
        if (LogReaderService.class.getName().equals(objectClass[i])) {
          isLogReaderService = true;
        }
      }
    }

    if (isLogReaderService) {
      LogReaderService logReader = (LogReaderService)
        Activator.bc.getService(serviceEvent.getServiceReference());
      if (logReader != null) {
        logReader.addLogListener(this);
      }
    }
    break;
  case ServiceEvent.MODIFIED:
    topic = SERVICE_EVENT_MODIFIED_TOPIC;
    break;
  case ServiceEvent.UNREGISTERING:
    topic = SERVICE_EVENT_UNREGISTERING_TOPIC;
    break;
  default:
    /* Setting the boolean to false if an unknown event arrives */
    knownMessageType = false;
    break;
  }



  /* Stores the properties of the event in the dictionary, if the event is known */
  if (knownMessageType) {
    if(!Activator.handlerTracker.anyHandlersMatching(topic)) {
      return;
    }
    Map<String,Object> props = new HashMap<String,Object>();
    putProp(props, EventConstants.EVENT, serviceEvent);
    putProp(props, EventConstants.SERVICE, serviceEvent.getServiceReference());
    putProp(props, EventConstants.SERVICE_PID, serviceEvent.getServiceReference().getProperty(Constants.SERVICE_PID));
    putProp(props, EventConstants.SERVICE_ID, serviceEvent.getServiceReference().getProperty(Constants.SERVICE_ID));
    putProp(props, EventConstants.SERVICE_OBJECTCLASS, serviceEvent.getServiceReference().getProperty(Constants.OBJECTCLASS));

    /* Tries posting the event once the properties are set */
    try {
      Activator.eventAdmin.postEvent(new Event(topic, props));
    } catch (Exception e) {
      Activator.log.error("EXCEPTION in serviceChanged() :", e);
    }
  } else {
    /* Logs an error if the event, which arrived, were of an unknown type */
    Activator.log.error("Recieved unknown service event message (type="
              +serviceEvent.getType() +"), discarding");
  }
}
 
Example #19
Source File: MultiListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void removedService(ServiceReference<LogReaderService> sr, LogReaderService o) {
  o.removeLogListener(MultiListener.this);
}
 
Example #20
Source File: MultiListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void modifiedService(ServiceReference<LogReaderService> sr, LogReaderService o) {
}
 
Example #21
Source File: LogReaderServiceFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void ungetService(Bundle bc,
                         ServiceRegistration<LogReaderService> sd,
                         LogReaderService s)
{
  logReaderServicies.remove(s);
}