org.osgi.service.url.URLConstants Java Examples

The following examples show how to use org.osgi.service.url.URLConstants. 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: ServiceContentHandlerFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
ContentHandler getServiceHandler(String mimetype) {
  try {
    final String filter = "(" + URLConstants.URL_CONTENT_MIMETYPE + "=" + mimetype + ")";
    //TODO true or false?
    @SuppressWarnings("unchecked")
    final ServiceReference<ContentHandler>[] srl
      = (ServiceReference<ContentHandler>[])
      framework.services.get(ContentHandler.class.getName(), filter, framework.systemBundle, false);

    if (srl != null && srl.length > 0) {
      ContentHandlerWrapper wrapper = wrapMap.get(mimetype);

      if (wrapper == null) {
        wrapper = new ContentHandlerWrapper(framework, mimetype);
        wrapMap.put(mimetype, wrapper);
      }
      return wrapper;
    }
  } catch (final InvalidSyntaxException e) {
    throw new RuntimeException("Failed to get service: " + e);
  }

  return null;
}
 
Example #2
Source File: ServiceURLStreamHandlerFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *
 */
private URLStreamHandler getServiceHandler(final String protocol)
{
  try {
    final String filter =
      "(" +
      URLConstants.URL_HANDLER_PROTOCOL +
      "=" + protocol +
      ")";
    @SuppressWarnings("unchecked")
    final Vector<FrameworkContext> sfws = (Vector<FrameworkContext>)framework.clone();
    for (final FrameworkContext sfw : sfws) {
      @SuppressWarnings("unchecked")
      final ServiceReference<URLStreamHandlerService>[] srl
        = (ServiceReference<URLStreamHandlerService>[]) sfw.services
          .get(URLStreamHandlerService.class.getName(), filter, sfw.systemBundle, false);

      if (srl != null && srl.length > 0) {
        synchronized (wrapMap) {
          URLStreamHandlerWrapper wrapper = wrapMap.get(protocol);
          if (wrapper == null) {
            wrapper = new URLStreamHandlerWrapper(sfw, protocol);
            wrapMap.put(protocol, wrapper);
          } else {
            wrapper.addFramework(sfw);
          }
          return wrapper;
        }
      }
    }
  } catch (final InvalidSyntaxException e) {
    throw new RuntimeException("Failed to get service: " + e);
  }

  // no handler found
  return null;
}
 
Example #3
Source File: ContentHandlerWrapper.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
ContentHandlerWrapper(FrameworkContext       framework,
	String                 mimetype) {

  this.framework = framework;
  this.mimetype  = mimetype;

  filter =
    "(&" +
    "(" + Constants.OBJECTCLASS + "=" +
    ContentHandler.class.getName() + ")" +
    "(" + URLConstants.URL_CONTENT_MIMETYPE + "=" + mimetype +
    ")" +
    ")";

  final ServiceListener serviceListener =
    new ServiceListener() {
      public void serviceChanged(ServiceEvent evt) {
        @SuppressWarnings("unchecked")
        final
        ServiceReference<ContentHandler> ref =
            (ServiceReference<ContentHandler>) evt.getServiceReference();

        switch (evt.getType()) {
        case ServiceEvent.MODIFIED:
          // fall through
        case ServiceEvent.REGISTERED:
          if (best == null) {
            updateBest();
            return ;
          }

          if (compare(best, ref) > 0) {
            best = ref;
          }
          break;
        case ServiceEvent.MODIFIED_ENDMATCH:
          // fall through
        case ServiceEvent.UNREGISTERING:
          if (best.equals(ref)) {
            best = null;
          }
        }
      }
    };

  try {
    framework.systemBundle.bundleContext.addServiceListener(serviceListener, filter);

  } catch (final Exception e) {
    throw new IllegalArgumentException("Could not register service listener for content handler: " + e);
  }

  if (framework.debug.url) {
    framework.debug.println("created wrapper for " + mimetype + ", filter=" + filter);
  }
}