Example #1
0
  /**
   * Try to load a module from this module loader. Returns {@code null} if the module is not found.
   * The returned module may not yet be resolved. The returned module may have a different name than
   * the given identifier if the identifier is an alias for another module.
   *
   * @param identifier the module identifier
   * @return the module
   * @throws ModuleLoadException if an error occurs while loading the module
   */
  protected final Module loadModuleLocal(ModuleIdentifier identifier) throws ModuleLoadException {
    FutureModule futureModule = moduleMap.get(identifier);
    if (futureModule != null) {
      return futureModule.getModule();
    }

    FutureModule newFuture = new FutureModule(identifier);
    futureModule = moduleMap.putIfAbsent(identifier, newFuture);
    if (futureModule != null) {
      return futureModule.getModule();
    }

    boolean ok = false;
    try {
      final ModuleLogger log = Module.log;
      log.trace("Locally loading module %s from %s", identifier, this);
      final long startTime = Metrics.getCurrentCPUTime();
      final ModuleSpec moduleSpec = findModule(identifier);
      loadTimeUpdater.addAndGet(this, Metrics.getCurrentCPUTime() - startTime);
      if (moduleSpec == null) {
        log.trace("Module %s not found from %s", identifier, this);
        return null;
      }
      if (!moduleSpec.getModuleIdentifier().equals(identifier)) {
        throw new ModuleLoadException("Module loader found a module with the wrong name");
      }
      final Module module;
      if (moduleSpec instanceof AliasModuleSpec) {
        final ModuleIdentifier aliasTarget = ((AliasModuleSpec) moduleSpec).getAliasTarget();
        try {
          newFuture.setModule(module = loadModuleLocal(aliasTarget));
        } catch (RuntimeException e) {
          log.trace(e, "Failed to load module %s (alias for %s)", identifier, aliasTarget);
          throw e;
        } catch (Error e) {
          log.trace(e, "Failed to load module %s (alias for %s)", identifier, aliasTarget);
          throw e;
        }
      } else {
        module = defineModule((ConcreteModuleSpec) moduleSpec, newFuture);
      }
      log.trace("Loaded module %s from %s", identifier, this);
      ok = true;
      return module;
    } finally {
      if (!ok) {
        newFuture.setModule(null);
        moduleMap.remove(identifier, newFuture);
      }
    }
  }
Example #2
0
  private TypeSpec buildModule(ModuleSpec spec) {
    CodeBlock.Builder blockBuilder =
        CodeBlock.builder().add("return new $T(", spec.getPresenterTypeName());
    int i = 0;
    for (ParameterSpec parameterSpec : spec.getPresenterArgs()) {
      blockBuilder.add(parameterSpec.name);

      if (i++ < spec.getPresenterArgs().size() - 1) {
        blockBuilder.add(", ");
      }
    }
    blockBuilder.add(");\n");

    MethodSpec.Builder methodSpecBuilder =
        MethodSpec.methodBuilder("providesPresenter")
            .addModifiers(Modifier.PUBLIC)
            .returns(spec.getPresenterTypeName())
            .addAnnotation(Provides.class)
            .addParameters(spec.getProvideParameters())
            .addCode(blockBuilder.build());

    if (spec.getScopeAnnotationSpec() != null) {
      methodSpecBuilder.addAnnotation(spec.getScopeAnnotationSpec());
    }

    return TypeSpec.classBuilder(spec.getClassName().simpleName())
        .addModifiers(Modifier.PUBLIC)
        .addAnnotation(Module.class)
        .addMethod(methodSpecBuilder.build())
        .build();
  }
Example #3
0
  /**
   * Create an aggregate module based on a module identifier and list of dependencies to
   * import/export.
   *
   * @param moduleIdentifier The module identifier
   * @param dependencies The module identifiers to aggregate
   * @return The loaded Module
   * @throws ModuleLoadException If any dependent module can not be loaded
   */
  public Module createAggregate(
      ModuleIdentifier moduleIdentifier, List<ModuleIdentifier> dependencies)
      throws ModuleLoadException {

    final ModuleSpec moduleSpec = new ModuleSpec(moduleIdentifier);
    for (ModuleIdentifier identifier : dependencies) {
      DependencySpec dependencySpec = new DependencySpec();
      dependencySpec.setModuleIdentifier(identifier);
      dependencySpec.setExport(true);
      moduleSpec.addDependency(dependencySpec);
    }

    moduleSpec.setContentLoader(ModuleContentLoader.build().create());
    return defineModule(moduleSpec);
  }
Example #4
0
  /**
   * Initialize all members with the given number of tiles and components.
   *
   * @param nt Number of tiles
   * @param nc Number of components
   */
  public DecoderSpecs(int nt, int nc) {
    // Quantization
    qts = new QuantTypeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
    qsss = new QuantStepSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
    gbs = new GuardBitsSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

    // Wavelet transform
    wfs = new SynWTFilterSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
    dls = new IntegerSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

    // Component transformation
    cts = new CompTransfSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

    // Entropy decoder
    ecopts = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
    ers = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
    cblks = new CBlkSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

    // Precinct partition
    pss = new PrecinctSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, dls);

    // Codestream
    nls = new IntegerSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
    pos = new IntegerSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
    pcs = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
    sops = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
    ephs = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
    pphs = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
    iccs = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
    pphs.setDefault(new Boolean(false));
  }
Example #5
0
  /**
   * Defines a Module based on a specification. Use of this method is required by any ModuleLoader
   * implementations in order to fully define a Module.
   *
   * @param moduleSpec The module specification to create the Module from
   * @return The defined Module
   * @throws ModuleLoadException If any dependent modules can not be loaded
   */
  protected final Module defineModule(ModuleSpec moduleSpec) throws ModuleLoadException {

    final ModuleIdentifier moduleIdentifier = moduleSpec.getIdentifier();
    FutureModule futureModule = moduleMap.get(moduleIdentifier);
    if (futureModule == null) {
      FutureModule newFuture = new FutureModule(moduleIdentifier);
      futureModule = moduleMap.putIfAbsent(moduleIdentifier, newFuture);
      if (futureModule == null) futureModule = newFuture;
    }
    // early detect
    if (futureModule.module != null) {
      throw new ModuleAlreadyExistsException(moduleIdentifier.toString());
    }
    try {
      final List<Dependency> dependencies =
          new ArrayList<Dependency>(moduleSpec.getDependencies().size());
      for (DependencySpec dependencySpec : moduleSpec.getDependencies()) {
        final Module dependencyModule;
        try {
          dependencyModule = loadModule(dependencySpec.getModuleIdentifier());
        } catch (ModuleLoadException e) {
          if (dependencySpec.isOptional()) {
            continue;
          } else {
            throw e;
          }
        }
        final Dependency dependency = new Dependency(dependencyModule, dependencySpec.isExport());
        dependencies.add(dependency);
      }
      final Module module = new Module(moduleSpec, dependencies, moduleSpec.getModuleFlags(), this);
      synchronized (futureModule) {
        futureModule.setModule(module);
      }
      return module;
    } catch (ModuleLoadException e) {
      futureModule.setModule(null);
      throw e;
    } catch (RuntimeException e) {
      futureModule.setModule(null);
      throw e;
    } catch (Error e) {
      futureModule.setModule(null);
      throw e;
    }
  }
 private static ModuleSpec parse(
     final File root,
     InputStream source,
     final File moduleInfoFile,
     final ModuleIdentifier moduleIdentifier)
     throws ModuleLoadException {
   try {
     final XMLInputFactory inputFactory = INPUT_FACTORY;
     setIfSupported(inputFactory, XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
     setIfSupported(inputFactory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
     final XMLStreamReader streamReader = inputFactory.createXMLStreamReader(source);
     try {
       return parseDocument(root, streamReader, ModuleSpec.build(moduleIdentifier));
     } finally {
       safeClose(streamReader);
     }
   } catch (XMLStreamException e) {
     throw new ModuleLoadException("Error loading module from " + moduleInfoFile.getPath(), e);
   }
 }
 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;
   }
 }