Exemple #1
0
  public List<Map<String, Object>> getPackages() throws IOException {
    List<Map<String, Object>> packages = new ArrayList<Map<String, Object>>();

    for (SPK spk : spks) {
      log("Include SPK: " + spk.file.getName());

      // make sure file is cached locally
      if (spk.url != null) {
        log("Using " + spk.url);
        if (!spk.file.exists()) {
          spk.file.getParentFile().mkdirs();
        }
        if (spk.url == null) {
          spk.url = spk.url;
        }
        Get get = new Get();
        get.bindToOwner(this);
        get.setQuiet(true);
        get.setUseTimestamp(true);
        get.setSrc(spk.url);
        get.setDest(spk.file);
        get.execute();
      } else {
        log("Using " + spk.file);
      }

      // import SPK INFO
      Map<String, Object> info = new LinkedHashMap<String, Object>();

      TarFileSet tar = new TarFileSet();
      tar.setProject(getProject());
      tar.setSrc(spk.file);
      tar.setIncludes(INFO);
      for (Resource resource : tar) {
        if (INFO.equals(resource.getName())) {
          String text =
              FileUtils.readFully(
                  new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
          for (String line : text.split("\\R")) {
            String[] s = line.split("=", 2);
            if (s.length == 2) {
              if (s[1].startsWith("\"") && s[1].endsWith("\"")) {
                s[1] = s[1].substring(1, s[1].length() - 1);
              }
              importSpkInfo(info, s[0], s[1]);
            }
          }
        }
      }
      log(String.format("Imported %d fields from SPK: %s", info.size(), info.keySet()));

      // add thumbnails and snapshots
      if (spk.thumbnail.size() > 0) {
        info.put(THUMBNAIL, spk.thumbnail.toArray(new String[0]));
      }
      if (spk.snapshot.size() > 0) {
        info.put(SNAPSHOT, spk.snapshot.toArray(new String[0]));
      }

      // add user-defined fields
      info.putAll(spk.infoList);

      // automatically generate file size and checksum fields
      if (!info.containsKey(LINK)) {
        info.put(LINK, spk.url);
      }
      info.put(MD5, md5(spk.file));
      info.put(SIZE, spk.file.length());

      packages.add(info);
    }

    return packages;
  }
Exemple #2
0
  private List<Dependency> addModuleDependencies(
      List<Dependency> dependencies, String moduleName, String moduleLocation) {
    log("Adding dependencies from " + moduleName + " module...");

    // Read dependencies from module's pom.xml
    URL pomLocation = null;
    File newDir = new File("target", "appfuse-" + moduleName);

    if (!newDir.exists()) {
      newDir.mkdirs();
    }

    File pom = new File("target/appfuse-" + moduleName + "/pom.xml");

    try {
      // replace github.com with raw.github.com and trunk with master
      trunk = trunk.replace("https://github.com", "https://raw.github.com");
      tag = tag.replace("trunk", "master");
      pomLocation = new URL(trunk + tag + moduleLocation + "/pom.xml");
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }

    Get get = (Get) AntUtils.createProject().createTask("get");
    get.setSrc(pomLocation);
    get.setDest(pom);
    get.execute();

    MavenProject p = createProjectFromPom(pom);

    List moduleDependencies = p.getOriginalModel().getDependencies();

    // set the properties for appfuse if root module
    if (moduleName.equalsIgnoreCase("root")) {
      appfuseProperties = p.getOriginalModel().getProperties();
    }

    // create a list of artifactIds to check for duplicates (there's no equals() on Dependency)
    Set<String> artifactIds = new LinkedHashSet<String>();

    for (Dependency dep : dependencies) {
      artifactIds.add(dep.getArtifactId());
    }

    // add all non-appfuse dependencies
    for (Object moduleDependency : moduleDependencies) {
      Dependency dep = (Dependency) moduleDependency;

      if (dep.getGroupId().equals("javax.servlet")
          && dep.getArtifactId().equals("jsp-api")
          && "jsf".equals(project.getProperties().getProperty("web.framework"))) {
        // skip adding dependency for old group id of jsp-api
        continue;
      }

      if (!artifactIds.contains(dep.getArtifactId()) && !dep.getArtifactId().contains("appfuse")) {
        dependencies.add(dep);
      }
    }

    return dependencies;
  }
  @Override
  public Set<String> fetchImages() {
    if (pageName == null || pageName.length() == 0) {
      throw new BuildException("please specify @pageName"); // $NON-NLS-1$
    }
    if (!pageName.equals(pageName.trim())) {
      throw new BuildException(
          "@pageName must not have leading or trailing whitespace"); //$NON-NLS-1$
    }

    String base;
    try {
      base = url.toURI().toString();
    } catch (URISyntaxException e) {
      throw new BuildException(e);
    }
    if (!base.endsWith("/")) { // $NON-NLS-1$
      base += "/"; // $NON-NLS-1$
    }

    ImageFetchingContentHandler contentHandler = new ImageFetchingContentHandler();
    String gimcontinue = null;
    Set<String> filenames = new HashSet<String>();
    final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setNamespaceAware(true);
    parserFactory.setValidating(false);
    int maxloop = 100;
    do {
      contentHandler.setGimcontinue(null);
      URL apiUrl;
      try {
        String queryString =
            String.format(
                "action=query&titles=%s&generator=images&prop=imageinfo&iiprop=url&format=xml%s", //$NON-NLS-1$
                URLEncoder.encode(pageName, "UTF-8"),
                (gimcontinue == null
                    ? ""
                    : "&gimcontinue="
                        + URLEncoder.encode(
                            gimcontinue,
                            "UTF-8"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        apiUrl = new URL(base + "api.php?" + queryString); // $NON-NLS-1$
      } catch (Exception e) {
        throw new BuildException("Cannot compose API URL", e); // $NON-NLS-1$
      }

      Reader input;
      try {

        log("Fetching " + apiUrl, Project.MSG_VERBOSE); // $NON-NLS-1$

        input =
            new InputStreamReader(
                new BufferedInputStream(apiUrl.openStream()), "UTF-8"); // $NON-NLS-1$
      } catch (IOException e) {
        throw new BuildException(
            String.format("Cannot contact %s: %s", apiUrl, e.getMessage()), e); // $NON-NLS-1$
      }

      try {
        SAXParser saxParser = parserFactory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setEntityResolver(IgnoreDtdEntityResolver.getInstance());

        xmlReader.setContentHandler(contentHandler);

        try {
          xmlReader.parse(new InputSource(input));
          gimcontinue = contentHandler.getGimcontinue();
        } catch (IOException e) {
          throw new BuildException(
              String.format("Unexpected exception retrieving data from %s", apiUrl),
              e); //$NON-NLS-1$
        } finally {
          try {
            input.close();
          } catch (IOException e) {
            // ignore
          }
        }
      } catch (SAXException e) {
        throw new BuildException("Unexpected error in XML content", e); // $NON-NLS-1$
      } catch (ParserConfigurationException e) {
        throw new BuildException("Cannot configure SAX parser", e); // $NON-NLS-1$
      }

    } while (gimcontinue != null && maxloop-- > 0);
    int fileCount = 0;
    for (Map.Entry<String, String> ent : contentHandler.imageTitleToUrl.entrySet()) {
      String title = ent.getKey();
      String imageUrl = ent.getValue();
      Matcher titleMatcher = imageTitlePattern.matcher(title);
      if (titleMatcher.matches()) {
        String name = titleMatcher.group(1);
        name = name.replace(' ', '_');
        String qualifiedUrl = base;
        if (imageUrl.matches("https?://.*")) { // $NON-NLS-1$
          qualifiedUrl = imageUrl;
        } else {
          if (imageUrl.startsWith("/")) { // $NON-NLS-1$
            qualifiedUrl += imageUrl.substring(1);
          } else {
            qualifiedUrl += imageUrl;
          }
        }

        log("Fetching " + qualifiedUrl, Project.MSG_INFO); // $NON-NLS-1$
        Get get = new Get();
        get.setProject(getProject());
        get.setLocation(getLocation());
        try {
          get.setSrc(new URL(qualifiedUrl));
        } catch (MalformedURLException e) {
          log(
              "Skipping " + url + ": " + e.getMessage(),
              Project.MSG_WARN); // $NON-NLS-1$ //$NON-NLS-2$
          continue;
        }
        get.setDest(new File(dest, name));
        get.execute();

        filenames.add(name);
        ++fileCount;
      } else {
        log(String.format("Unexpected title format: %s", title), Project.MSG_WARN); // $NON-NLS-1$
      }
    }
    log(
        "Fetched " + fileCount + " image files for " + pageName,
        Project.MSG_INFO); // $NON-NLS-1$ //$NON-NLS-2$

    return filenames;
  }