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.configuration;
16
17
18 import com.seventytwomiles.architecturerules.domain.Rule;
19 import com.seventytwomiles.architecturerules.domain.SourceDirectory;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.seventytwomiles.springframework.core.io.ClassPathResource;
23 import org.seventytwomiles.springframework.io.FileUtils;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.HashSet;
30 import java.util.List;
31
32
33
34 /***
35 * <p>Abstract Factory that provides common functionality for
36 * <code>ConfigurationFactory</code> implementations.</p>
37 *
38 * @author mikenereson
39 * @see ConfigurationFactory
40 */
41 public abstract class AbstractConfigurationFactory
42 implements ConfigurationFactory {
43
44
45 protected static final Log log
46 = LogFactory.getLog(AbstractConfigurationFactory.class);
47
48 /***
49 * <p>Set of Rules read from the configuration file</p>
50 *
51 * @parameter rules Set
52 */
53 protected final Collection<Rule> rules = new HashSet<Rule>();
54
55 /***
56 * <p>Set of <code>Source</code> read from configuration file</p>
57 *
58 * @parameter sources Set
59 */
60 protected final List<SourceDirectory> sources
61 = new ArrayList<SourceDirectory>();
62
63 /***
64 * <p>Weather or not to throw exception when no packages are found for a
65 * given path.</p>
66 *
67 * @parameter throwExceptionWhenNoPackages boolean
68 */
69 protected boolean throwExceptionWhenNoPackages = false;
70
71 /***
72 * <p>Weather or not to check for cyclic dependencies.</p>
73 *
74 * @parameter doCyclicDependencyTest boolean
75 */
76 protected boolean doCyclicDependencyTest = true;
77
78
79 /***
80 * <p>Getter for property {@link #rules}.</p>
81 *
82 * @return Value for property <tt>rules</tt>.
83 */
84 public Collection<Rule> getRules() {
85 return this.rules;
86 }
87
88
89 /***
90 * <p>Getter for property {@link #sources}.</p>
91 *
92 * @return Value for property <tt>sources</tt>.
93 */
94 public List<SourceDirectory> getSources() {
95 return this.sources;
96 }
97
98
99 /***
100 * @return boolean <tt>true</tt> when <samp><cyclicalDependency
101 * test="true"/> </samp>
102 * @see ConfigurationFactory#doCyclicDependencyTest()
103 */
104 public boolean doCyclicDependencyTest() {
105 return doCyclicDependencyTest;
106 }
107
108
109 /***
110 * @return boolean <tt>true</tt> when <samp><sources
111 * no-packages="exception"> </samp>
112 * @see ConfigurationFactory#throwExceptionWhenNoPackages()
113 */
114 public boolean throwExceptionWhenNoPackages() {
115 return throwExceptionWhenNoPackages;
116 }
117
118
119 /***
120 * <p>Read Xml configuration file to String.</p>
121 *
122 * @param configurationFileName String name of the XML file in the classpath
123 * to load and read OR the complete path to the file.
124 * @return String returns the contents of the configurationFile
125 */
126 protected String getConfigurationAsXml(final String configurationFileName) {
127
128 File file = new File(configurationFileName);
129
130 if (!file.exists()) {
131
132 /***
133 * This code kinda sucks. First, an exception is thrown if the resource
134 * does not exist, then an exception could be thrown if the resource
135 * could not be read.
136 */
137 final ClassLoader classLoader = getClass().getClassLoader();
138
139 final ClassPathResource resource
140 = new ClassPathResource(configurationFileName, classLoader);
141
142 if (!resource.exists())
143 throw new IllegalArgumentException("could not load resource "
144 + configurationFileName
145 + " from classpath. File not found.");
146
147 try {
148
149 file = resource.getFile();
150
151 } catch (IOException e) {
152
153 throw new IllegalArgumentException("could not locate resource "
154 + configurationFileName
155 + " from classpath. File not found.");
156 }
157 }
158
159
160 final String xml;
161
162 try {
163
164 xml = FileUtils.readFileToString(file, null);
165
166 } catch (final IOException e) {
167
168 final String path = file.getAbsolutePath();
169
170 throw new IllegalArgumentException(
171 "could not load configuration from " + path);
172 }
173
174 return xml;
175 }
176
177
178 /***
179 * <p>Validate the configuration.</p>
180 *
181 * @param configuration String xml content to validate
182 * @see "architecture-rules.dtd"
183 */
184 protected abstract void validateConfiguration(final String configuration);
185 }