@Test
  public void testModuleDelegatesToFramework() throws Exception {
    ModuleIdentifier identifierF = ModuleIdentifier.create("framework");
    ModuleSpec.Builder specBuilderF = ModuleSpec.build(identifierF);
    PathFilter importFilter = getSystemFilter();
    PathFilter exportFilter = PathFilters.acceptAll();
    specBuilderF.addDependency(
        DependencySpec.createSystemDependencySpec(importFilter, exportFilter, getSystemPaths()));
    importFilter = PathFilters.in(getFrameworkPaths());
    exportFilter = PathFilters.acceptAll();
    FrameworkLocalLoader localLoader = new FrameworkLocalLoader(Bundle.class.getClassLoader());
    specBuilderF.addDependency(
        DependencySpec.createLocalDependencySpec(
            importFilter, exportFilter, localLoader, getFrameworkPaths()));
    addModuleSpec(specBuilderF.create());

    ModuleIdentifier identifierA = ModuleIdentifier.create("moduleA");
    ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
    VirtualFileResourceLoader resourceLoaderA = new VirtualFileResourceLoader(virtualFileA);
    specBuilderA.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoaderA));
    specBuilderA.addDependency(DependencySpec.createModuleDependencySpec(identifierF));
    specBuilderA.addDependency(DependencySpec.createLocalDependencySpec());
    addModuleSpec(specBuilderA.create());

    assertLoadClass(identifierA, "org.osgi.framework.Bundle");
    assertLoadClass(identifierA, "javax.security.auth.x500.X500Principal");
  }
  @Override
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final DeploymentUnit parent =
        deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent();

    final ModuleSpecification parentModuleSpec =
        parent.getAttachment(Attachments.MODULE_SPECIFICATION);

    final ModuleSpecification moduleSpec =
        deploymentUnit.getAttachment(Attachments.MODULE_SPECIFICATION);
    final ModuleLoader moduleLoader =
        deploymentUnit.getAttachment(Attachments.SERVICE_MODULE_LOADER);
    final ModuleIdentifier moduleIdentifier =
        deploymentUnit.getAttachment(Attachments.MODULE_IDENTIFIER);

    if (deploymentUnit.getParent() != null) {
      final ModuleIdentifier parentModule = parent.getAttachment(Attachments.MODULE_IDENTIFIER);
      if (parentModule != null) {
        // access to ear classes
        ModuleDependency moduleDependency =
            new ModuleDependency(moduleLoader, parentModule, false, false, true);
        moduleDependency.addImportFilter(PathFilters.acceptAll(), true);
        moduleSpec.addLocalDependency(moduleDependency);
      }
    }

    // If the sub deployments aren't isolated, then we need to set up dependencies between the sub
    // deployments
    if (!parentModuleSpec.isSubDeploymentModulesIsolated()) {
      final List<DeploymentUnit> subDeployments =
          parent.getAttachmentList(Attachments.SUB_DEPLOYMENTS);
      final List<ModuleDependency> accessibleModules = new ArrayList<ModuleDependency>();
      for (DeploymentUnit subDeployment : subDeployments) {
        final ModuleSpecification subModule =
            subDeployment.getAttachment(Attachments.MODULE_SPECIFICATION);
        if (!subModule.isPrivateModule()) {
          ModuleIdentifier identifier = subDeployment.getAttachment(Attachments.MODULE_IDENTIFIER);
          ModuleDependency dependency =
              new ModuleDependency(moduleLoader, identifier, false, false, true);
          dependency.addImportFilter(PathFilters.acceptAll(), true);
          accessibleModules.add(dependency);
        }
      }
      for (ModuleDependency identifier : accessibleModules) {
        if (!identifier.equals(moduleIdentifier)) {
          moduleSpec.addLocalDependencies(accessibleModules);
        }
      }
    }
  }
  @Test
  public void testAvailableFrameworkModule() throws Exception {
    ModuleIdentifier identifierF = ModuleIdentifier.create("framework");
    ModuleSpec.Builder specBuilderF = ModuleSpec.build(identifierF);
    PathFilter importFilter = PathFilters.in(getFrameworkPaths());
    PathFilter exportFilter = PathFilters.acceptAll();
    FrameworkLocalLoader localLoader = new FrameworkLocalLoader(Bundle.class.getClassLoader());
    specBuilderF.addDependency(
        DependencySpec.createLocalDependencySpec(
            importFilter, exportFilter, localLoader, getFrameworkPaths()));
    addModuleSpec(specBuilderF.create());

    assertLoadClass(identifierF, "org.osgi.framework.Bundle");
    assertLoadClassFail(identifierF, "javax.security.auth.x500.X500Principal");
  }
  @Override
  public ModuleSpec get(ModuleLoader loader, ModuleIdentifier id) {
    if (getId().equals(id)) {
      Builder builder = ModuleSpec.build(id);
      builder.addDependency(
          DependencySpec.createClassLoaderDependencySpec(
              PathFilters.acceptAll(),
              PathFilters.acceptAll(),
              AbstractModuleSpecProvider.class.getClassLoader(),
              getPaths()));
      builder.addDependency(
          DependencySpec.createClassLoaderDependencySpec(
              PathFilters.acceptAll(),
              PathFilters.acceptAll(),
              ClassLoader.getSystemClassLoader(),
              getPaths()));

      configure(loader, builder);

      return builder.create();
    }
    return null;
  }
  @Test
  public void testNotAvailableOnSystemModule() throws Exception {
    ModuleIdentifier identifierA = ModuleIdentifier.create("moduleA");
    ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
    VirtualFileResourceLoader resourceLoaderA = new VirtualFileResourceLoader(virtualFileA);
    specBuilderA.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoaderA));
    PathFilter importFilter = getSystemFilter();
    PathFilter exportFilter = PathFilters.acceptAll();
    specBuilderA.addDependency(
        DependencySpec.createSystemDependencySpec(importFilter, exportFilter, getSystemPaths()));
    specBuilderA.addDependency(DependencySpec.createLocalDependencySpec());
    addModuleSpec(specBuilderA.create());

    assertLoadClassFail(identifierA, "org.osgi.framework.Bundle");
    assertLoadClass(identifierA, "javax.security.auth.x500.X500Principal");
  }
 @Override
 public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
   final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
   final DeploymentUnit parent = deploymentUnit.getParent();
   // only run for sub deployments
   if (parent == null) {
     return;
   }
   final ModuleSpecification moduleSpec =
       deploymentUnit.getAttachment(Attachments.MODULE_SPECIFICATION);
   final ModuleLoader moduleLoader =
       deploymentUnit.getAttachment(Attachments.SERVICE_MODULE_LOADER);
   final ModuleIdentifier parentModule = parent.getAttachment(Attachments.MODULE_IDENTIFIER);
   if (parentModule != null) {
     // access to ear classes
     ModuleDependency moduleDependency =
         new ModuleDependency(moduleLoader, parentModule, false, false, true);
     moduleDependency.addImportFilter(PathFilters.acceptAll(), true);
     moduleSpec.addDependency(moduleDependency);
   }
 }
  @Test
  public void testTwoHopDelegation() throws Exception {

    ModuleIdentifier identifierB = ModuleIdentifier.create("moduleB");
    ModuleSpec.Builder specBuilderB = ModuleSpec.build(identifierB);
    PathFilter importFilter = getSystemFilter();
    PathFilter exportFilter = PathFilters.acceptAll();
    specBuilderB.addDependency(
        DependencySpec.createSystemDependencySpec(importFilter, exportFilter, getSystemPaths()));
    addModuleSpec(specBuilderB.create());

    ModuleIdentifier identifierA = ModuleIdentifier.create("moduleA");
    ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
    VirtualFileResourceLoader resourceLoaderA = new VirtualFileResourceLoader(virtualFileA);
    specBuilderA.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoaderA));
    specBuilderA.addDependency(DependencySpec.createModuleDependencySpec(identifierB));
    specBuilderA.addDependency(DependencySpec.createLocalDependencySpec());
    addModuleSpec(specBuilderA.create());

    assertLoadClass(identifierB, "javax.security.auth.x500.X500Principal", null);
    assertLoadClass(identifierA, "javax.security.auth.x500.X500Principal", null);
  }
Пример #8
0
 /** {@inheritDoc} */
 public PathFilter getExportFilter() {
   return PathFilters.acceptAll();
 }
 private PathFilter getSystemFilter() {
   return PathFilters.in(getSystemPaths());
 }
 private PathFilter getSystemFilter() {
   MultiplePathFilterBuilder pathBuilder = PathFilters.multiplePathFilterBuilder(false);
   pathBuilder.addFilter(PathFilters.isChildOf("javax/security"), true);
   PathFilter importFilter = pathBuilder.create();
   return importFilter;
 }
 public ModuleSpec findModule(final ModuleIdentifier identifier, final ModuleLoader delegateLoader)
     throws ModuleLoadException {
   if (identifier.equals(myIdentifier)) {
     // special root JAR module
     Manifest manifest;
     try {
       manifest = jarFile.getManifest();
     } catch (IOException e) {
       throw new ModuleLoadException("Failed to load MANIFEST from JAR", e);
     }
     ModuleSpec.Builder builder = ModuleSpec.build(identifier);
     Attributes mainAttributes = manifest.getMainAttributes();
     String mainClass = mainAttributes.getValue(Attributes.Name.MAIN_CLASS);
     if (mainClass != null) {
       builder.setMainClass(mainClass);
     }
     String classPath = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
     String dependencies = mainAttributes.getValue("Dependencies");
     MultiplePathFilterBuilder pathFilterBuilder = PathFilters.multiplePathFilterBuilder(true);
     pathFilterBuilder.addFilter(PathFilters.is("modules"), false);
     pathFilterBuilder.addFilter(PathFilters.isChildOf("modules"), false);
     builder.addResourceRoot(
         ResourceLoaderSpec.createResourceLoaderSpec(
             new JarFileResourceLoader("", jarFile), pathFilterBuilder.create()));
     String[] classPathEntries =
         classPath == null ? JarModuleLoader.NO_STRINGS : classPath.split("\\s+");
     for (String entry : classPathEntries) {
       if (!entry.isEmpty()) {
         if (entry.startsWith("../")
             || entry.startsWith("./")
             || entry.startsWith("/")
             || entry.contains("/../")) {
           // invalid
           continue;
         }
         if (entry.endsWith("/")) {
           // directory reference
           File root = new File(jarFile.getName(), entry);
           FileResourceLoader resourceLoader = new FileResourceLoader(entry, root);
           builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoader));
         } else {
           // assume a JAR
           File root = new File(jarFile.getName(), entry);
           JarFile childJarFile;
           try {
             childJarFile = new JarFile(root, true);
           } catch (IOException e) {
             // ignore and continue
             continue;
           }
           builder.addResourceRoot(
               ResourceLoaderSpec.createResourceLoaderSpec(
                   new JarFileResourceLoader(entry, childJarFile)));
         }
       }
     }
     String[] dependencyEntries =
         dependencies == null ? JarModuleLoader.NO_STRINGS : dependencies.split("\\s*,\\s*");
     for (String dependencyEntry : dependencyEntries) {
       boolean optional = false;
       boolean export = false;
       dependencyEntry = dependencyEntry.trim();
       if (!dependencyEntry.isEmpty()) {
         String[] fields = dependencyEntry.split("\\s+");
         if (fields.length < 1) {
           continue;
         }
         String moduleName = fields[0];
         for (int i = 1; i < fields.length; i++) {
           String field = fields[i];
           if (field.equals("optional")) {
             optional = true;
           } else if (field.equals("export")) {
             export = true;
           }
           // else ignored
         }
         builder.addDependency(
             DependencySpec.createModuleDependencySpec(
                 ModuleIdentifier.fromString(moduleName), export, optional));
       }
     }
     builder.addDependency(DependencySpec.createSystemDependencySpec(JDKPaths.JDK));
     builder.addDependency(DependencySpec.createLocalDependencySpec());
     return builder.create();
   } else {
     String namePath = identifier.getName().replace('.', '/');
     String basePath = "modules/" + namePath + "/" + identifier.getSlot();
     JarEntry moduleXmlEntry = jarFile.getJarEntry(basePath + "/module.xml");
     if (moduleXmlEntry == null) {
       return null;
     }
     ModuleSpec moduleSpec;
     try {
       InputStream inputStream = jarFile.getInputStream(moduleXmlEntry);
       try {
         moduleSpec =
             ModuleXmlParser.parseModuleXml(
                 new ModuleXmlParser.ResourceRootFactory() {
                   public ResourceLoader createResourceLoader(
                       final String rootPath, final String loaderPath, final String loaderName)
                       throws IOException {
                     return new JarFileResourceLoader(loaderName, jarFile, loaderPath);
                   }
                 },
                 basePath,
                 inputStream,
                 moduleXmlEntry.getName(),
                 delegateLoader,
                 identifier);
       } finally {
         JarModuleLoader.safeClose(inputStream);
       }
     } catch (IOException e) {
       throw new ModuleLoadException("Failed to read module.xml file", e);
     }
     return moduleSpec;
   }
 }