Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. package com.lincesoft.imperator.displayer.provider;
  2.  
  3. import org.osgi.framework.Bundle;
  4. import org.osgi.framework.ServiceFactory;
  5. import org.osgi.framework.ServiceRegistration;
  6.  
  7. import com.lincesoft.imperator.displayer.api.Displayer;
  8.  
  9. public class DisplayerServiceFactory implements ServiceFactory<Displayer> {
  10.  
  11. public static final String NAME = "displayer_service_factory";
  12.  
  13. @Override
  14. public void ungetService(Bundle bundle, ServiceRegistration<Displayer> registration, Displayer service) {
  15. // TODO Auto-generated method stub
  16. }
  17. @Override
  18. public Displayer getService(Bundle bundle, ServiceRegistration<Displayer> registration) {
  19. return new DisplayerImplementation();
  20. }
  21. }
  22.  
  23. package com.lincesoft.imperator.displayer.launcher;
  24.  
  25. import java.util.Hashtable;
  26.  
  27. import org.osgi.framework.BundleActivator;
  28. import org.osgi.framework.BundleContext;
  29. import org.osgi.framework.ServiceFactory;
  30. import org.osgi.framework.ServiceRegistration;
  31.  
  32. import com.lincesoft.imperator.displayer.api.Displayer;
  33. import com.lincesoft.imperator.displayer.provider.DisplayerServiceFactory;
  34.  
  35. public class Activator implements BundleActivator {
  36.  
  37. private ServiceRegistration<?> DisplayerFactoryRegistration;
  38.  
  39. @Override
  40. public void start(BundleContext bundle_context) throws Exception {
  41. System.out.println("Starting the Displayer Provider Bundle");
  42. ServiceFactory<Displayer> displayer_service_factory = new DisplayerServiceFactory();
  43. Hashtable<String,String> properties = new Hashtable<String,String>();
  44. properties.put("service.vendor", DisplayerServiceFactory.NAME);
  45. DisplayerFactoryRegistration = bundle_context.registerService(Displayer.class.getName(), displayer_service_factory,properties);
  46. }
  47.  
  48. @Override
  49. public void stop(BundleContext context) throws Exception {
  50. System.out.println("Stopping the Displayer Provider Bundle");
  51. DisplayerFactoryRegistration.unregister();
  52. }
  53. }
  54.  
  55. /* Get a displayer instance from the service factory provided by the displayer provider bundle */
  56. ServiceReference<?>[] service_references = null;
  57. try {
  58. service_references = my_bundle_context.getServiceReferences(Displayer.class.getName(),"(service.vendor=displayer_service_factory)");
  59. } catch (InvalidSyntaxException e) {
  60. System.out.println("Sintaxis para obtener displayer_service_factory_reference invalida.");
  61. e.printStackTrace();
  62. }
  63. ServiceReference<?> displayer_service_factory_reference = null;
  64. if (service_references!=null && service_references.length==1) {
  65. displayer_service_factory_reference = service_references[0];
  66. } else {
  67. //Throw new NoDisplayerFactoryException();
  68. }
  69. Displayer bundle_displayer_instance = (Displayer) my_bundle_context.getService(displayer_service_factory_reference);
  70.  
  71. Exception in thread "main" java.lang.ClassCastException: com.lincesoft.imperator.displayer.provider.DisplayerImplementation cannot be cast to com.lincesoft.imperator.displayer.api.Displayer
  72. at com.lincesoft.imperator.procurator.launcher.Procurator.main(Procurator.java:94)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement