private static void assertResource(
      URLClassLoader classLoader, String file, String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
      return;
    }
    if (fileStream == null && expectedContent != null) {
      throw new RuntimeException(buildMessage(file, expectedContent, null));
    }
    try {
      String actualContent = readAsString(fileStream);

      if (fileStream != null && expectedContent == null) {
        throw new RuntimeException(buildMessage(file, null, actualContent));
      }
      if (!expectedContent.equals(actualContent)) {
        throw new RuntimeException(buildMessage(file, expectedContent, actualContent));
      }
    } finally {
      fileStream.close();
    }
  }
  public @Override void execute() throws BuildException {
    String p = getProject().getProperty("javahelpbin.exclude.modules");
    excludedModulesSet = parseExcludeModulesProperty(p);
    for (FileSet fs : filesets) {
      FileScanner scanner = fs.getDirectoryScanner(getProject());
      File dir = scanner.getBasedir();
      String[] files = scanner.getIncludedFiles();

      URLClassLoader globalClassLoader;
      Map<String, URLClassLoader> classLoaderMap;
      try {
        globalClassLoader = createGlobalClassLoader(dir, files);
        classLoaderMap = createClassLoaderMap(dir, files);
        NbDocsStreamHandler.NbDocsURLConnection.globalClassLoader.set(globalClassLoader);
        NbDocsStreamHandler.NbDocsURLConnection.classLoaderMap.set(classLoaderMap);
        CheckLinks.handlerFactory.set(new NbDocsStreamHandler.Factory());
        for (Map.Entry<String, URLClassLoader> entry : classLoaderMap.entrySet()) {
          String cnb = entry.getKey();
          if (excludedModulesSet.contains(cnb)) {
            log("skipping module: " + cnb, Project.MSG_INFO);
            continue;
          }
          URLClassLoader l = entry.getValue();
          Manifest m;
          InputStream is = l.getResourceAsStream("META-INF/MANIFEST.MF");
          if (is != null) {
            try {
              m = new Manifest(is);
            } finally {
              is.close();
            }
          } else {
            log("No manifest in " + Arrays.toString(l.getURLs()), Project.MSG_WARN);
            continue;
          }
          for (String resource :
              new String[] {
                m.getMainAttributes().getValue("OpenIDE-Module-Layer"),
                "META-INF/generated-layer.xml"
              }) {
            if (resource == null) {
              continue;
            }
            URL layer = l.getResource(resource);
            if (layer == null) {
              log("No layer " + resource, Project.MSG_VERBOSE);
              continue;
            }
            Document doc;
            try {
              doc = XMLUtil.parse(new InputSource(layer.toString()), false, false, null, NO_DTDS);
            } catch (SAXException x) {
              log("Could not parse " + layer, x, Project.MSG_WARN);
              continue;
            }
            for (Element services : XMLUtil.findSubElements(doc.getDocumentElement())) {
              if (!services.getTagName().equals("folder")
                  || !services.getAttribute("name").equals("Services")) {
                continue;
              }
              for (Element javahelp : XMLUtil.findSubElements(services)) {
                if (!javahelp.getTagName().equals("folder")
                    || !javahelp.getAttribute("name").equals("JavaHelp")) {
                  continue;
                }
                JAVAHELP:
                for (Element registration : XMLUtil.findSubElements(javahelp)) {
                  if (!registration.getTagName().equals("file")) {
                    continue;
                  }
                  InputSource input = null;
                  String url = registration.getAttribute("url");
                  if (!url.isEmpty()) {
                    input = new InputSource(new URL(layer, url).toString());
                  } else {
                    NodeList nl = registration.getChildNodes();
                    for (int i = 0; i < nl.getLength(); i++) {
                      if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
                        if (input == null) {
                          input = new InputSource(new StringReader(nl.item(i).getNodeValue()));
                        } else {
                          log(
                              "Multiple content for "
                                  + registration.getAttribute("name")
                                  + " in "
                                  + layer,
                              Project.MSG_WARN);
                          continue JAVAHELP;
                        }
                      }
                    }
                    if (input == null) {
                      log(
                          "No content for " + registration.getAttribute("name") + " in " + layer,
                          Project.MSG_WARN);
                    }
                  }
                  Document doc2;
                  try {
                    doc2 = XMLUtil.parse(input, false, false, null, NO_DTDS);
                  } catch (SAXException x) {
                    log(
                        "Could not parse " + registration.getAttribute("name") + " in " + layer,
                        x,
                        Project.MSG_WARN);
                    continue;
                  }
                  URI helpsetref = URI.create(doc2.getDocumentElement().getAttribute("url"));
                  if ("nbdocs".equals(helpsetref.getScheme())
                      && helpsetref.getAuthority() == null) {
                    try {
                      helpsetref =
                          new URI(
                              helpsetref.getScheme(),
                              cnb,
                              helpsetref.getPath(),
                              helpsetref.getQuery(),
                              helpsetref.getFragment());
                    } catch (URISyntaxException x) {
                      throw new BuildException(x);
                    }
                  }
                  log("checking: " + helpsetref, Project.MSG_INFO);
                  checkHelpSetURL(
                      CheckLinks.toURL(helpsetref), globalClassLoader, l, classLoaderMap, cnb);
                }
              }
            }
          }
        }
      } catch (IOException x) {
        throw new BuildException(x);
      } finally {
        NbDocsStreamHandler.NbDocsURLConnection.globalClassLoader.set(null);
        NbDocsStreamHandler.NbDocsURLConnection.classLoaderMap.set(null);
        CheckLinks.handlerFactory.set(null);
      }
    }
  }
Пример #3
0
  public static void prepareLanguage(
      Log log,
      MavenProject project,
      MavenProjectHelper projectHelper,
      File languageOutDir,
      File schemaOutDir,
      BuildContext buildContext)
      throws MojoExecutionException {

    File camelMetaDir = new File(languageOutDir, "META-INF/services/org/apache/camel/");

    // first we need to setup the output directory because the next check
    // can stop the build before the end and eclipse always needs to know about that directory
    if (projectHelper != null) {
      projectHelper.addResource(
          project,
          languageOutDir.getPath(),
          Collections.singletonList("**/dataformat.properties"),
          Collections.emptyList());
    }

    if (!PackageHelper.haveResourcesChanged(
        log, project, buildContext, "META-INF/services/org/apache/camel/language")) {
      return;
    }

    Map<String, String> javaTypes = new HashMap<String, String>();

    StringBuilder buffer = new StringBuilder();
    int count = 0;
    for (Resource r : project.getBuild().getResources()) {
      File f = new File(r.getDirectory());
      if (!f.exists()) {
        f = new File(project.getBasedir(), r.getDirectory());
      }
      f = new File(f, "META-INF/services/org/apache/camel/language");

      if (f.exists() && f.isDirectory()) {
        File[] files = f.listFiles();
        if (files != null) {
          for (File file : files) {
            String javaType = readClassFromCamelResource(file, buffer, buildContext);
            if (!file.isDirectory() && file.getName().charAt(0) != '.') {
              count++;
            }
            if (javaType != null) {
              javaTypes.put(file.getName(), javaType);
            }
          }
        }
      }
    }

    // find camel-core and grab the data format model from there, and enrich this model with
    // information from this artifact
    // and create json schema model file for this data format
    try {
      if (count > 0) {
        Artifact camelCore = findCamelCoreArtifact(project);
        if (camelCore != null) {
          File core = camelCore.getFile();
          if (core != null) {
            URL url = new URL("file", null, core.getAbsolutePath());
            URLClassLoader loader = new URLClassLoader(new URL[] {url});
            for (Map.Entry<String, String> entry : javaTypes.entrySet()) {
              String name = entry.getKey();
              String javaType = entry.getValue();
              String modelName = asModelName(name);

              InputStream is =
                  loader.getResourceAsStream(
                      "org/apache/camel/model/language/" + modelName + ".json");
              if (is == null) {
                // use file input stream if we build camel-core itself, and thus do not have a JAR
                // which can be loaded by URLClassLoader
                is =
                    new FileInputStream(
                        new File(core, "org/apache/camel/model/language/" + modelName + ".json"));
              }
              String json = loadText(is);
              if (json != null) {
                LanguageModel languageModel = new LanguageModel();
                languageModel.setName(name);
                languageModel.setTitle("");
                languageModel.setModelName(modelName);
                languageModel.setLabel("");
                languageModel.setDescription("");
                languageModel.setJavaType(javaType);
                languageModel.setGroupId(project.getGroupId());
                languageModel.setArtifactId(project.getArtifactId());
                languageModel.setVersion(project.getVersion());

                List<Map<String, String>> rows =
                    JSonSchemaHelper.parseJsonSchema("model", json, false);
                for (Map<String, String> row : rows) {
                  if (row.containsKey("title")) {
                    languageModel.setTitle(row.get("title"));
                  }
                  if (row.containsKey("description")) {
                    languageModel.setDescription(row.get("description"));
                  }
                  if (row.containsKey("label")) {
                    languageModel.setLabel(row.get("label"));
                  }
                  if (row.containsKey("javaType")) {
                    languageModel.setModelJavaType(row.get("javaType"));
                  }
                }
                log.debug("Model " + languageModel);

                // build json schema for the data format
                String properties = after(json, "  \"properties\": {");
                String schema = createParameterJsonSchema(languageModel, properties);
                log.debug("JSon schema\n" + schema);

                // write this to the directory
                File dir = new File(schemaOutDir, schemaSubDirectory(languageModel.getJavaType()));
                dir.mkdirs();

                File out = new File(dir, name + ".json");
                OutputStream fos = buildContext.newFileOutputStream(out);
                fos.write(schema.getBytes());
                fos.close();

                buildContext.refresh(out);

                log.debug("Generated " + out + " containing JSon schema for " + name + " language");
              }
            }
          }
        }
      }
    } catch (Exception e) {
      throw new MojoExecutionException(
          "Error loading language model from camel-core. Reason: " + e, e);
    }

    if (count > 0) {
      Properties properties = new Properties();
      String names = buffer.toString();
      properties.put("languages", names);
      properties.put("groupId", project.getGroupId());
      properties.put("artifactId", project.getArtifactId());
      properties.put("version", project.getVersion());
      properties.put("projectName", project.getName());
      if (project.getDescription() != null) {
        properties.put("projectDescription", project.getDescription());
      }

      camelMetaDir.mkdirs();
      File outFile = new File(camelMetaDir, "language.properties");

      // check if the existing file has the same content, and if so then leave it as is so we do not
      // write any changes
      // which can cause a re-compile of all the source code
      if (outFile.exists()) {
        try {
          Properties existing = new Properties();

          InputStream is = new FileInputStream(outFile);
          existing.load(is);
          is.close();

          // are the content the same?
          if (existing.equals(properties)) {
            log.debug("No language changes detected");
            return;
          }
        } catch (IOException e) {
          // ignore
        }
      }

      try {
        OutputStream os = buildContext.newFileOutputStream(outFile);
        properties.store(os, "Generated by camel-package-maven-plugin");
        os.close();

        log.info(
            "Generated "
                + outFile
                + " containing "
                + count
                + " Camel "
                + (count > 1 ? "languages: " : "language: ")
                + names);

        if (projectHelper != null) {
          projectHelper.attachArtifact(project, "properties", "camelLanguage", outFile);
        }
      } catch (IOException e) {
        throw new MojoExecutionException(
            "Failed to write properties to " + outFile + ". Reason: " + e, e);
      }
    } else {
      log.debug(
          "No META-INF/services/org/apache/camel/language directory found. Are you sure you have created a Camel language?");
    }
  }
Пример #4
0
    RunInfo getRunInfo(BenchmarkDefinition benchDef) throws Exception {

      RunInfo runInfo = new RunInfo();
      String v = xp.evaluate("fa:scale", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.scale = Integer.parseInt(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<scale> must be an integer.");
        }
      }

      v = xp.evaluate("fa:runControl/fa:rampUp", runConfigNode);
      if (v == null || v.length() == 0) {
        throw new ConfigurationException("Element <rampUp> not found.");
      }
      try {
        runInfo.rampUp = Integer.parseInt(v);
      } catch (NumberFormatException e) {
        throw new ConfigurationException("<rampUp> must be an integer.");
      }

      v = xp.evaluate("fa:runControl/fa:steadyState", runConfigNode);
      if (v == null || v.length() == 0) {
        throw new ConfigurationException("Element <steadyState> not found.");
      }
      try {
        runInfo.stdyState = Integer.parseInt(v);
      } catch (NumberFormatException e) {
        throw new ConfigurationException("<steadyState> must be an integer.");
      }

      v = xp.evaluate("fa:runControl/fa:rampDown", runConfigNode);
      if (v == null || v.length() == 0) {
        throw new ConfigurationException("Element <rampDown> not found.");
      }
      try {
        runInfo.rampDown = Integer.parseInt(v);
      } catch (NumberFormatException e) {
        throw new ConfigurationException("<rampDown> must be an integer.");
      }

      v = xp.evaluate("fa:runControl/fa:variableLoad", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.variableLoad = relaxedParseBoolean(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<variableLoad> must be true or false.");
        }
      }

      if (runInfo.variableLoad) {
        runInfo.variableLoadFile = xp.evaluate("fa:runControl/fa:variableLoadFile", runConfigNode);
      }

      runInfo.resultsDir = xp.evaluate("fd:outputDir", runConfigNode);
      if (runInfo.resultsDir == null || runInfo.resultsDir.length() == 0) {
        throw new ConfigurationException("Element <outputDir> not found.");
      }

      v = xp.evaluate("fd:audit", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.audit = relaxedParseBoolean(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<audit> must be true or false.");
        }
      }

      v = xp.evaluate("fd:threadStart/fd:delay", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.msBetweenThreadStart = Integer.parseInt(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<delay> must be an integer.");
        }
      }

      v = xp.evaluate("fd:threadStart/fd:simultaneous", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.simultaneousStart = relaxedParseBoolean(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<simultaneous> must be true or false.");
        }
      }

      v = xp.evaluate("fd:threadStart/fd:parallel", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.parallelAgentThreadStart = relaxedParseBoolean(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<parallel> must be true or false.");
        }
      }

      v = xp.evaluate("fd:stats/fd:maxRunTime", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.maxRunTime = Integer.parseInt(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<maxRunTime> must be an integer.");
        }
      }

      v = xp.evaluate("fd:stats/fd:interval", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.graphInterval = Integer.parseInt(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<interval> must be an integer.");
        }
      }

      v = xp.evaluate("fd:runtimeStats/@enabled", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.runtimeStatsEnabled = relaxedParseBoolean(v);
        } catch (Exception e) {
          throw new ConfigurationException("<runtimeStats enabled=[true|false]>");
        }
      }

      v = xp.evaluate("fd:runtimeStats/fd:interval", runConfigNode);
      if (v != null && v.length() > 0) {
        try {
          runInfo.runtimeStatsInterval = Integer.parseInt(v);
        } catch (NumberFormatException e) {
          throw new ConfigurationException("<interval> must be an integer.");
        }
      }

      runInfo.driverConfigs = new DriverConfig[benchDef.drivers.length];
      for (int i = 0; i < benchDef.drivers.length; i++) {
        DriverConfig driverConfig = new DriverConfig(benchDef.drivers[i]);
        Element driverConfigNode =
            (Element)
                xp.evaluate(
                    "fd:driverConfig[@name=\"" + driverConfig.name + "\"][1]",
                    runConfigNode,
                    XPathConstants.NODE);

        driverConfig.runControl = benchDef.runControl;
        if (driverConfigNode == null) {
          throw new ConfigurationException(
              "Element " + "<driverConfig name=\"" + driverConfig.name + "\"> not found.");
        }

        v = xp.evaluate("fd:agents", driverConfigNode);

        // Note that the agents field has two valid formats:
        // 1. A single integer
        // 2. One or more host:count fields
        // The harness is interested in the host/count. What we need
        // is a simple count. Just add'em up.
        if (v != null && v.length() > 0) {
          StringTokenizer t = new StringTokenizer(v, " ,");
          driverConfig.numAgents = 0;
          while (t.hasMoreTokens()) {
            v = t.nextToken().trim();
            if (v.length() == 0) {
              continue;
            }
            int idx = v.indexOf(':');
            if (++idx > 0) {
              v = v.substring(idx);
            }
            if (v.length() == 0) {
              continue;
            }
            try {
              driverConfig.numAgents += Integer.parseInt(v);
            } catch (NumberFormatException e) {

              throw new ConfigurationException(
                  "<agents> "
                      + "must be an integer or in the format "
                      + "host:agents where agents is an integer. "
                      + "Found: "
                      + v);
            }
          }
        }

        v = xp.evaluate("fd:threads", driverConfigNode);
        if (v != null && v.length() > 0) {
          try {
            driverConfig.numThreads = Integer.parseInt(v);
          } catch (NumberFormatException e) {
            throw new ConfigurationException("<threads> must be an integer.");
          }
        }

        v = xp.evaluate("fd:stats/fd:interval", driverConfigNode);
        if (v != null && v.length() > 0) {
          try {
            driverConfig.graphInterval = Integer.parseInt(v);
          } catch (NumberFormatException e) {
            throw new ConfigurationException("<interval> must be an integer.");
          }
        } else {
          driverConfig.graphInterval = runInfo.graphInterval;
        }

        if (runInfo.runtimeStatsEnabled) {
          driverConfig.runtimeStatsTarget =
              xp.evaluate("fd:runtimeStats/@target", driverConfigNode);
          if (driverConfig.runtimeStatsTarget != null) {
            driverConfig.runtimeStatsTarget = driverConfig.runtimeStatsTarget.trim();
            if (driverConfig.runtimeStatsTarget.length() == 0)
              driverConfig.runtimeStatsTarget = null;
          }
        }

        if (runInfo.variableLoad) {
          driverConfig.variableLoadFile = xp.evaluate("fd:variableLoadFile", driverConfigNode);
          if (driverConfig.variableLoadFile == null
              || driverConfig.variableLoadFile.length() == 0) {
            driverConfig.variableLoadFile = runInfo.variableLoadFile;
          }
          if (driverConfig.variableLoadFile == null
              || driverConfig.variableLoadFile.length() == 0) {
            throw new ConfigurationException("Element <variableLoadFile> not found.");
          }
        }

        driverConfig.rootElement = rootElement;
        driverConfig.properties =
            (Element) xp.evaluate("fd:properties", driverConfigNode, XPathConstants.NODE);
        driverConfig.mix[0].configure(driverConfigNode);
        driverConfig.mix[0].configureCycles(driverConfigNode);
        driverConfig.mix[0].normalize();
        runInfo.driverConfigs[i] = driverConfig;

        // need to store the class bytes for a remote agent
        // if the driver is an http driver

        InputStream is = null;
        String defClassName = getDefiningClassName();
        try {
          // check whether this class is defined in this class loader

          ClassLoader cl = this.getClass().getClassLoader();
          cl.loadClass(defClassName);

        } catch (ClassNotFoundException cnfe) {

          String tempDir = System.getProperty("faban.tmpdir");

          if (tempDir == null) {
            tempDir = System.getProperty("java.io.tmpdir");
          }

          File classFile = new File(tempDir);

          URL url[] = new URL[1];

          try {
            url[0] = classFile.toURI().toURL();
          } catch (MalformedURLException ex) {
            throw new ConfigurationException(ex);
          }

          URLClassLoader loader =
              new URLClassLoader(url, BenchmarkDefinition.class.getClassLoader());

          try {

            loader.loadClass(defClassName);
            is = loader.getResourceAsStream(defClassName + ".class");

            runInfo.defBytes = new byte[is.available()];
            is.read(runInfo.defBytes);
            is.close();
            System.out.println("Bytes Read from class :" + runInfo.defBytes.length);

          } catch (ClassNotFoundException cnfex) {
            throw new ConfigurationException(cnfex);
          } catch (IOException ioex) {
            throw new ConfigurationException(ioex);
          }
        }
      }
      return runInfo;
    }