plexus使用(三)-配置组件

修改前文的接口类:

public interface WebsiteMonitor
{

    /**
     * Role used to register component implementations with the container.
     */
    String ROLE = WebsiteMonitor.class.getName();

    /**
     * Monitor the specified website at the specified time intervals.
     * 
     * @throws Exception
     *             error encountered while performing the monitoring request.
     */
    public void monitor()
        throws Exception;

    /**
     * Specify a list of websites that can be monitored by this component.
     * 
     * @param websites List of websites
     */
    void addWebsites( List websites );

    /**
     * Determines if the website monitor component was properly initialized.
     *  
     * @return <code>true</code> if initiailized, else <code>false</code>.
     */
    boolean isInitialized();
}

修改相关实现类:

public class DefaultWebsiteMonitor
    extends AbstractLogEnabled
    implements WebsiteMonitor
{

    /**
     * Websites to monitor.
     * 
     * @plexus.configuration
     */
    private List websites;

    /**
     * @see org.codehaus.plexus.tutorial.WebsiteMonitor#monitor(java.lang.String)
     */
    public void monitor()
        throws Exception
    {
        HttpClient client = new HttpClient();

        for ( Iterator it = websites.iterator(); it.hasNext(); )
        {
            String website = (String) it.next();
            HttpMethod getMethod = new GetMethod( website );
            getMethod.setFollowRedirects( false );

            try
            {
                int statusCode = client.executeMethod( getMethod );

                if ( statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES )
                {
                    if ( getLogger().isErrorEnabled() )
                        getLogger().error(
                                           "HTTP request returned HTTP status code: " + statusCode + " for website: "
                                               + website );
                    throw new Exception( "HTTP request returned HTTP status code: " + statusCode + " for website: "
                        + website );
                }
                if ( getLogger().isInfoEnabled() )
                    getLogger().info(
                                      "HTTP request returned HTTP status code: " + statusCode + " for website: "
                                          + website );
            }
            catch ( UnknownHostException e )
            {
                if ( getLogger().isErrorEnabled() )
                    getLogger().error( "Specified host '" + website + "' could not be resolved." );
                throw e;
            }
            finally
            {
                if ( null != getMethod )
                    getMethod.releaseConnection();
            }
        }
    }

    public void addWebsites( List websites )
    {
        this.websites = websites;
    }

    public boolean isInitialized()
    {
        return ( null != websites && this.websites.size() > 0 );
    }

}

websites参数修改为list。注解:@plexus.configuration表明该属性是plexus的配置元素,该注解被plexus组件描述创建器(CDC)通过java代码过滤和生成。

我们将websites作为配置文件中的配置项,我们通过修改组件配置文件来添加一组websites值。plexus容器确保配置问价中的组件属性匹配及初始化。它能只能的发现及匹配不同类型如string、list等配置项。用户也可以通过配置文件中的implementation属性来定义自己的类型实现类,plexus尝试将xml配置文件中的属性注入值,每个配置元素对应上述属性中声明的实现类。以下是最新的配置文件:

<component-set>
  <components>
    <component>
      <role>org.codehaus.plexus.tutorial.WebsiteMonitor</role>
      <implementation>
        org.codehaus.plexus.tutorial.DefaultWebsiteMonitor
      </implementation>
      <configuration>
        <websites>
          <website>http://www.google.co.nz/</website>
          <website>http://www.nonexistent.yadayada3249834739547.org/</website>
          <website>http://maven.apache.org/</website>
        </websites>
      </configuration>
    </component>
  </components>
</component-set>


你可能感兴趣的:(中文,文档,教程,plexus)