How to discovering types exported by OSGi bundle without installing/activating?

前端 未结 2 1219
执笔经年
执笔经年 2020-12-19 20:26

Basically i want to discover if a jar implements any number of interfaces wihtout activating or starting the bundle. Is it possible to read the meta data from the meta-inf f

相关标签:
2条回答
  • 2020-12-19 20:42

    you may try something like below. Find the ".class" files in the exported packages using bundle.findResource(...) method.

     BundleContext context = bundle.getBundleContext();
      ServiceReference ref = context.getServiceReference(PackageAdmin.class.getName());
      PackageAdmin packageAdmin = (PackageAdmin)context.getService(ref);
      List<Class> agentClasses = new ArrayList<Class>();
      ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle);
      for(ExportedPackage ePackage : exportedPackages){
          String packageName = ePackage.getName();
          String packagePath = "/"+packageName.replace('.', '/');
          //find all the class files in current exported package
          Enumeration clazzes = bundle.findEntries(packagePath, "*.class", false);
          while(clazzes.hasMoreElements()){
           URL url = (URL)clazzes.nextElement();
           String path = url.getPath();
           int index = path.lastIndexOf("/");
           int endIndex = path.length()-6;//Strip ".class" substring
           String className = path.substring(index+1, endIndex);
           String fullClassName=packageName+"."+className;
           try {
         Class clazz = bundle.loadClass(fullClassName);
         //check whether the class is annotated with Agent tag.
         if(clazz.isAnnotationPresent(Agent.class))
          agentClasses.add(clazz);
        } catch (ClassNotFoundException e) { 
         e.printStackTrace();
        }
          }
      }
    
    0 讨论(0)
  • 2020-12-19 20:46

    I do not think it is possible to discover what services a bundle is going to provide, because this can happen from inside the Java code, without any meta-data about it. Of course, if you use Declarative Services, there is a meta-data file. Also, the bundle needs to import (or provide) the service interface, which may give you a hint (but not more).

    You can inspect what Java packages a bundles imports and exports without activating it. If you are willing to install (not resolve, not activate) it, you can query it. The Felix or Equinox shells can list those packages after all.

    Here is the relevant source from Felix' shell. It uses the PackageAdmin service:

     public void execute(String s, PrintStream out, PrintStream err)
    {
        // Get package admin service.
        ServiceReference ref = m_context.getServiceReference(
            org.osgi.service.packageadmin.PackageAdmin.class.getName());
        PackageAdmin pa = (ref == null) ? null : 
            (PackageAdmin) m_context.getService(ref);
    
        // ... 
    
        Bundle bundle = m_context.getBundle( bundleId );
        ExportedPackage[] exports = pa.getExportedPackages(bundle);
    
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题