View Javadoc

1   /***
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * For more information visit
11   *         http://72miles.com and
12   *         http://architecturerules.googlecode.com/svn/docs/index.html
13   */
14  
15  package com.seventytwomiles.architecturerules.ant;
16  
17  
18  import com.seventytwomiles.architecturerules.configuration.Configuration;
19  import com.seventytwomiles.architecturerules.configuration.ConfigurationFactory;
20  import com.seventytwomiles.architecturerules.configuration.UnmodifiableConfiguration;
21  import com.seventytwomiles.architecturerules.configuration.xml.DigesterConfigurationFactory;
22  import com.seventytwomiles.architecturerules.services.CyclicRedundancyService;
23  import com.seventytwomiles.architecturerules.services.CyclicRedundancyServiceImpl;
24  import com.seventytwomiles.architecturerules.services.RulesService;
25  import com.seventytwomiles.architecturerules.services.RulesServiceImpl;
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.Task;
28  
29  
30  
31  /***
32   * <p>Ant task to assert architecture.</p>
33   *
34   * <p>Usage looks like:</p>
35   *
36   * <pre>
37   * &lt;taskdef name="assertArchitecture"
38   *          classname="com.seventytwomiles.architecturerules.ant.AssertArchitectureTask">
39   *    &lt;classpath refid="class.path"/>
40   * &lt;/taskdef>
41   *
42   * &lt;target name="assert-architecture" depends="compile">
43   *    &lt;assertArchitecture configurationFileName="architecture-rules-pass.xml"/>
44   * &lt;/target>
45   * </pre>
46   *
47   * <p>Requires the optional dependency</p>
48   *
49   * <pre>
50   *  &lt;dependency>
51   *      &lt;groupId>org.apache.ant&lt;/groupId>
52   *      &lt;artifactId>ant&lt;/artifactId>
53   *      &lt;version>1.7.0&lt;/version>
54   *  &lt;/dependency>
55   * </pre>
56   *
57   * @author mikenereson
58   */
59  public class AssertArchitectureTask extends Task {
60  
61  
62      /***
63       * <p>The name of the configuration file that is in the classpath that holds
64       * the xml configuration. Recommend <samp>architecture-rules.xml</samp></p>
65       *
66       * @parameter configurationFileName String
67       */
68      private String configurationFileName;
69  
70      /***
71       * <p>Reference the configuration that is built by the
72       * <code>ConfiguraitonFactory</code> that reads the configurationFile. This
73       * configuration may be modified.</p>
74       *
75       * @parameter configuration Configuration
76       */
77      final private Configuration configuration = new Configuration();
78  
79  
80      /***
81       * Setter for property 'configurationFileName'.
82       *
83       * @param configurationFileName Value to set for property
84       * 'configurationFileName'.
85       */
86      public void setConfigurationFileName(final String configurationFileName) {
87          this.configurationFileName = configurationFileName;
88      }
89  
90  
91      /***
92       * @throws BuildException
93       * @see Task#execute()
94       */
95      @Override
96      public void execute() throws BuildException {
97  
98          super.execute();
99  
100         if (null == configurationFileName || "".equals(configurationFileName))
101             throw new IllegalStateException(
102                     "set configurationFileName property");
103 
104         /***
105          * 1. load configuration
106          */
107         final ConfigurationFactory configurationFactory
108                 = new DigesterConfigurationFactory(configurationFileName);
109 
110         this.configuration.getRules().addAll(configurationFactory.getRules());
111 
112         this.configuration
113                 .getSources().addAll(configurationFactory.getSources());
114 
115         this.configuration.setDoCyclicDependencyTest(
116                 configurationFactory.doCyclicDependencyTest());
117 
118         this.configuration.setThrowExceptionWhenNoPackages(
119                 configurationFactory.throwExceptionWhenNoPackages());
120 
121         /***
122          * 2. assert configuration rules
123          */
124         Configuration configuration
125                 = new UnmodifiableConfiguration(this.configuration);
126 
127         final RulesService rulesService
128                 = new RulesServiceImpl(configuration);
129 
130         rulesService.performRulesTest();
131 
132         /***
133          * 3. check for cyclic dependency, if requested
134          */
135         if (this.configuration.shouldDoCyclicDependencyTest()) {
136 
137             configuration = new UnmodifiableConfiguration(this.configuration);
138 
139             final CyclicRedundancyService redundancyService
140                     = new CyclicRedundancyServiceImpl(configuration);
141 
142             redundancyService.performCyclicRedundancyCheck();
143         }
144     }
145 }