@SuppressWarnings("deprecation")
 public static ProductConfiguration read(File file) throws IOException, XmlPullParserException {
   XmlStreamReader reader = ReaderFactory.newXmlReader(file);
   try {
     return new ProductConfiguration(Xpp3DomBuilder.build(reader));
   } finally {
     reader.close();
   }
 }
  /**
   * Reads the {@link WebappStructure} from the specified file.
   *
   * @param file the file containing the webapp structure
   * @return the webapp structure
   * @throws IOException if an error occurred while reading the structure
   */
  public WebappStructure fromXml(File file) throws IOException {
    Reader reader = null;

    try {
      reader = ReaderFactory.newXmlReader(file);
      return (WebappStructure) XSTREAM.fromXML(reader);
    } finally {
      IOUtil.close(reader);
    }
  }
Exemplo n.º 3
0
  public void verifyEncodedRead() throws IOException, DocumentException {
    String path = "src/test/verifiers/dom4j/expected-encoding.xml";

    Reader reader = ReaderFactory.newXmlReader(new File(path));
    MavenDom4jReader modelReader = new MavenDom4jReader();

    Model model = modelReader.read(reader);

    Assert.assertEquals("Maven\u00A9", model.getName());
  }
  /**
   * Process any additional component configuration files that have been specified. The specified
   * directory is scanned recursively so configurations can be within nested directories to help
   * with component organization.
   */
  private void processConfigurationsDirectory() throws PlexusConfigurationException {
    String s = configuration.getChild("configurations-directory").getValue(null);

    if (s != null) {
      PlexusConfiguration componentsConfiguration = configuration.getChild("components");

      File configurationsDirectory = new File(s);

      if (configurationsDirectory.exists() && configurationsDirectory.isDirectory()) {
        List componentConfigurationFiles;
        try {
          componentConfigurationFiles =
              FileUtils.getFiles(configurationsDirectory, "**/*.conf", "**/*.xml");
        } catch (IOException e) {
          throw new PlexusConfigurationException("Unable to locate configuration files", e);
        }

        for (Iterator i = componentConfigurationFiles.iterator(); i.hasNext(); ) {
          File componentConfigurationFile = (File) i.next();

          Reader reader = null;
          try {
            reader = ReaderFactory.newXmlReader(componentConfigurationFile);
            PlexusConfiguration componentConfiguration =
                PlexusTools.buildConfiguration(
                    componentConfigurationFile.getAbsolutePath(),
                    getInterpolationConfigurationReader(reader));

            componentsConfiguration.addChild(componentConfiguration.getChild("components"));
          } catch (FileNotFoundException e) {
            throw new PlexusConfigurationException(
                "File " + componentConfigurationFile + " disappeared before processing", e);
          } catch (IOException e) {
            throw new PlexusConfigurationException(
                "IO error while reading " + componentConfigurationFile, e);
          } finally {
            IOUtil.close(reader);
          }
        }
      }
    }
  }
  protected void initializeConfiguration()
      throws ConfigurationProcessingException, ConfigurationResourceNotFoundException,
          PlexusConfigurationException, ContextException, IOException {
    // System userConfiguration

    InputStream is = containerRealm.getResourceAsStream(PlexusConstants.BOOTSTRAP_CONFIGURATION);

    if (is == null) {
      ClassRealm cr = containerRealm;
      String realmStack = "";
      while (cr != null) {
        realmStack +=
            "\n  "
                + cr.getId()
                + " parent="
                + cr.getParent()
                + " ("
                + cr.getResource(PlexusConstants.BOOTSTRAP_CONFIGURATION)
                + ")";
        cr = cr.getParentRealm();
      }

      throw new IllegalStateException(
          "The internal default plexus-bootstrap.xml is missing. "
              + "This is highly irregular, your plexus JAR is most likely corrupt. Realms:"
              + realmStack);
    }

    PlexusConfiguration bootstrapConfiguration =
        PlexusTools.buildConfiguration(
            PlexusConstants.BOOTSTRAP_CONFIGURATION, ReaderFactory.newXmlReader(is));

    // Some of this could probably be collapsed as having a plexus.xml in your
    // META-INF/plexus directory is probably a better solution then specifying
    // a configuration with an URL but I'm leaving the configuration by URL
    // as folks might be using it ... I made this change to accomodate Maven
    // but I think it's better to discover a configuration in a standard
    // place.

    configuration = bootstrapConfiguration;

    if (!containerContext.contains(PlexusConstants.IGNORE_CONTAINER_CONFIGURATION)
        || containerContext.get(PlexusConstants.IGNORE_CONTAINER_CONFIGURATION) != Boolean.TRUE) {
      PlexusXmlComponentDiscoverer discoverer = new PlexusXmlComponentDiscoverer();

      PlexusConfiguration plexusConfiguration =
          discoverer.discoverConfiguration(getContext(), containerRealm);

      if (plexusConfiguration != null) {
        configuration = PlexusConfigurationMerger.merge(plexusConfiguration, configuration);

        processConfigurationsDirectory();
      }
    }

    if (configurationReader != null) {
      // User userConfiguration

      PlexusConfiguration userConfiguration =
          PlexusTools.buildConfiguration(
              "<User Specified Configuration Reader>",
              getInterpolationConfigurationReader(configurationReader));

      // Merger of bootstrapConfiguration and user userConfiguration

      configuration = PlexusConfigurationMerger.merge(userConfiguration, configuration);

      processConfigurationsDirectory();
    }

    // ---------------------------------------------------------------------------
    // Now that we have the configuration we will use the ConfigurationProcessor
    // to inline any external configuration instructions.
    //
    // At his point the variables in the configuration have already been
    // interpolated so we can send in an empty Map because the containerContext
    // values are already there.
    // ---------------------------------------------------------------------------

    ConfigurationProcessor p = new ConfigurationProcessor();

    p.addConfigurationResourceHandler(new FileConfigurationResourceHandler());

    p.addConfigurationResourceHandler(new DirectoryConfigurationResourceHandler());

    configuration = p.process(configuration, Collections.EMPTY_MAP);
  }
  private void construct(ContainerConfiguration c) throws PlexusContainerException {
    if (c.getParentContainer() != null) {
      this.parentContainer = c.getParentContainer();

      this.loggerManager = parentContainer.getLoggerManager();

      this.containerRealm =
          getChildRealm(getName(), name, c.getParentContainer().getContainerRealm());
    }

    this.name = c.getName();

    // ----------------------------------------------------------------------------
    // ClassWorld
    // ----------------------------------------------------------------------------

    this.classWorld = c.getClassWorld();

    // Make sure we have a valid ClassWorld
    if (this.classWorld == null) {
      this.classWorld =
          new ClassWorld(DEFAULT_REALM_NAME, Thread.currentThread().getContextClassLoader());
    }

    containerRealm = c.getRealm();

    if (containerRealm == null) {
      try {
        containerRealm = this.classWorld.getRealm(DEFAULT_REALM_NAME);
      } catch (NoSuchRealmException e) {
        List realms = new LinkedList(this.classWorld.getRealms());

        containerRealm = (ClassRealm) realms.get(0);

        if (containerRealm == null) {
          System.err.println("No container realm! Expect errors.");

          new Throwable().printStackTrace();
        }
      }
    }

    setLookupRealm(containerRealm);

    // ----------------------------------------------------------------------------
    // Context
    // ----------------------------------------------------------------------------

    this.containerContext = new DefaultContext();

    if (c.getContext() != null) {
      for (Iterator it = c.getContext().entrySet().iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();

        addContextValue(entry.getKey(), entry.getValue());
      }
    }

    // ----------------------------------------------------------------------------
    // Configuration
    // ----------------------------------------------------------------------------

    // TODO: just store reference to the configuration in a String and use that in the configuration
    // initialization

    InputStream in = null;

    if (c.getContainerConfiguration() != null) {
      in = toStream(c.getContainerConfiguration());
    }

    try {
      if (c.getContainerConfigurationURL() != null) {
        in = c.getContainerConfigurationURL().openStream();
      }
    } catch (IOException e) {
      throw new PlexusContainerException("Error reading configuration URL", e);
    }

    try {
      this.configurationReader = in == null ? null : ReaderFactory.newXmlReader(in);
    } catch (IOException e) {
      throw new PlexusContainerException("Error reading configuration file", e);
    }

    try {
      // XXX this will set up the configuration and process it
      initialize();

      // XXX this will wipe out the configuration field - is this needed? If so,
      // why? can we remove the need to have a configuration field then?
      start();
    } finally {
      IOUtil.close(this.configurationReader);
    }
  }
  /** {@inheritDoc} */
  public void execute() throws MojoExecutionException, MojoFailureException {
    MavenXpp3Reader pomReader = new MavenXpp3Reader();
    Model model = null;
    Reader reader = null;
    try {
      reader = ReaderFactory.newXmlReader(project.getFile());
      model = pomReader.read(reader);
    } catch (Exception e) {
      throw new MojoExecutionException("IOException: " + e.getMessage(), e);
    } finally {
      IOUtil.close(reader);
    }

    Set<String> duplicateDependencies = new HashSet<String>();
    if (model.getDependencies() != null) {
      duplicateDependencies = findDuplicateDependencies(model.getDependencies());
    }

    Set<String> duplicateDependenciesManagement = new HashSet<String>();
    if (model.getDependencyManagement() != null
        && model.getDependencyManagement().getDependencies() != null) {
      duplicateDependenciesManagement =
          findDuplicateDependencies(model.getDependencyManagement().getDependencies());
    }

    if (getLog().isInfoEnabled()) {
      StringBuffer sb = new StringBuffer();

      if (!duplicateDependencies.isEmpty()) {
        sb.append("List of duplicate dependencies defined in <dependencies/> in your pom.xml:\n");
        for (Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); ) {
          String dup = it.next();

          sb.append("\to " + dup);
          if (it.hasNext()) {
            sb.append("\n");
          }
        }
      }

      if (!duplicateDependenciesManagement.isEmpty()) {
        if (sb.length() > 0) {
          sb.append("\n");
        }
        sb.append(
            "List of duplicate dependencies defined in <dependencyManagement/> in "
                + "your pom.xml:\n");
        for (Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); ) {
          String dup = it.next();

          sb.append("\to " + dup);
          if (it.hasNext()) {
            sb.append("\n");
          }
        }
      }

      if (sb.length() > 0) {
        getLog().info(sb.toString());
      } else {
        getLog()
            .info(
                "No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>");
      }
    }
  }