1 /* 2 * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH 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 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.maven.exceptions.conf; 17 18 import java.util.Collection; 19 import java.util.Collections; 20 import java.util.List; 21 22 import de.smartics.maven.exceptions.runtime.ProjectClassLoader; 23 24 /** 25 * Default implementation. 26 */ 27 public final class DefaultJavadocProjectConfiguration implements 28 JavadocProjectConfiguration 29 { 30 // ********************************* Fields ********************************* 31 32 // --- constants ------------------------------------------------------------ 33 34 // --- members -------------------------------------------------------------- 35 36 /** 37 * The name of the project this configuration is for. 38 */ 39 private final String projectName; 40 41 /** 42 * The classpath used by the tool. 43 */ 44 private final List<String> toolClassPath; 45 46 /** 47 * The encoding of the source files in this project. 48 */ 49 private final String sourceEncoding; 50 51 /** 52 * The encoding of the source files in this project. 53 */ 54 private final String sourceVersion; 55 56 /** 57 * The list of class path root elements of this project. 58 */ 59 private final Collection<String> classRootDirectoryNames; 60 61 /** 62 * The list of source path root elements of this project. 63 */ 64 private final Collection<String> sourceRootDirectoryNames; 65 66 /** 67 * The class loader to load project classes. 68 */ 69 private final ClassLoader projectClassLoader; 70 71 /** 72 * The includes for the scanner. 73 */ 74 private final List<String> includes; 75 76 /** 77 * The excludes for the scanner. 78 */ 79 private final List<String> excludes; 80 81 // ****************************** Initializer ******************************* 82 83 // ****************************** Constructors ****************************** 84 85 /** 86 * Default constructor. 87 * 88 * @param builder the information to set to the value object. 89 */ 90 private DefaultJavadocProjectConfiguration(final Builder builder) 91 { 92 this.projectName = builder.projectName; 93 this.toolClassPath = builder.toolClassPath; 94 this.sourceEncoding = builder.sourceEncoding; 95 this.sourceVersion = builder.sourceVersion; 96 this.excludes = 97 builder.excludes != null ? Collections 98 .unmodifiableList(builder.excludes) : null; 99 this.includes = 100 builder.includes != null ? Collections 101 .unmodifiableList(builder.includes) : null; 102 this.classRootDirectoryNames = 103 Collections.unmodifiableCollection(builder.classRootDirectoryNames); 104 this.sourceRootDirectoryNames = 105 Collections.unmodifiableCollection(builder.sourceRootDirectoryNames); 106 this.projectClassLoader = builder.projectClassLoader; 107 } 108 109 // ****************************** Inner Classes ***************************** 110 111 /** 112 * The configuration builder. 113 */ 114 public static final class Builder 115 { 116 // ******************************** Fields ******************************** 117 118 // --- constants ---------------------------------------------------------- 119 120 // --- members ------------------------------------------------------------ 121 122 /** 123 * The name of the project this configuration is for. 124 */ 125 private final String projectName; 126 127 /** 128 * The classpath used by the tool. 129 */ 130 private List<String> toolClassPath; 131 132 /** 133 * The encoding of the source files in this project. 134 */ 135 private String sourceEncoding = "UTF-8"; 136 137 /** 138 * The encoding of the source files in this project. Defaults to 139 * <code>1.5</code>. 140 */ 141 private String sourceVersion = "1.5"; 142 143 /** 144 * The list of class path root elements of this project. 145 */ 146 private Collection<String> classRootDirectoryNames; 147 148 /** 149 * The list of source path root elements of this project. 150 */ 151 private Collection<String> sourceRootDirectoryNames; 152 153 /** 154 * The class loader to load project classes. 155 */ 156 private ClassLoader projectClassLoader; 157 158 /** 159 * The includes for the scanner. 160 */ 161 private List<String> includes; 162 163 /** 164 * The excludes for the scanner. 165 */ 166 private List<String> excludes; 167 168 // ***************************** Initializer ****************************** 169 170 // ***************************** Constructors ***************************** 171 172 /** 173 * Default constructor. 174 * 175 * @param projectName the name of the project this configuration is for. 176 */ 177 public Builder(final String projectName) 178 { 179 this.projectName = projectName; 180 } 181 182 // ***************************** Inner Classes **************************** 183 184 // ******************************** Methods ******************************* 185 186 // --- init --------------------------------------------------------------- 187 188 // --- get&set ------------------------------------------------------------ 189 190 /** 191 * Sets the classpath used by the tool. 192 * 193 * @param toolClassPath the classpath used by the tool. 194 */ 195 public void setToolClassPath(final List<String> toolClassPath) 196 { 197 this.toolClassPath = toolClassPath; 198 } 199 200 /** 201 * Sets the list of class path root elements of this project. 202 * 203 * @param classRootDirectoryNames the list of class path root elements of 204 * this project. 205 */ 206 public void setClassRootDirectoryNames( 207 final Collection<String> classRootDirectoryNames) 208 { 209 this.classRootDirectoryNames = classRootDirectoryNames; 210 } 211 212 /** 213 * Sets the list of source path root elements of this project. 214 * 215 * @param sourceRootDirectoryNames the list of source path root elements of 216 * this project. 217 */ 218 public void setSourceRootDirectoryNames( 219 final Collection<String> sourceRootDirectoryNames) 220 { 221 this.sourceRootDirectoryNames = sourceRootDirectoryNames; 222 } 223 224 /** 225 * Sets the includes for the scanner. 226 * 227 * @param includes the includes for the scanner. 228 */ 229 public void setIncludes(final List<String> includes) 230 { 231 this.includes = includes; 232 } 233 234 /** 235 * Sets the excludes for the scanner. 236 * 237 * @param excludes the excludes for the scanner. 238 */ 239 public void setExcludes(final List<String> excludes) 240 { 241 this.excludes = excludes; 242 } 243 244 /** 245 * Sets the encoding of the source files in this project. 246 * 247 * @param sourceEncoding the encoding of the source files in this project. 248 */ 249 public void setSourceEncoding(final String sourceEncoding) 250 { 251 this.sourceEncoding = sourceEncoding; 252 } 253 254 /** 255 * Sets the encoding of the source files in this project. Defaults to 256 * <code>1.5</code>. 257 * 258 * @param sourceVersion the encoding of the source files in this project. 259 */ 260 public void setSourceVersion(final String sourceVersion) 261 { 262 this.sourceVersion = sourceVersion; 263 } 264 265 // --- business ----------------------------------------------------------- 266 267 /** 268 * Creates the project configuration. 269 * 270 * @return the project configuration. 271 * @throws IllegalArgumentException if any property is not valid. 272 */ 273 public JavadocProjectConfiguration build() throws IllegalArgumentException 274 { 275 checkConstraints(); 276 277 // Please note that the parent class loader is required to be the one 278 // loading these classes since we have to match the ones returned by the 279 // Javadoc tool with the ones loaded from this class path. 280 projectClassLoader = 281 new ProjectClassLoader(getClass().getClassLoader(), 282 this.classRootDirectoryNames); 283 final JavadocProjectConfiguration config = 284 new DefaultJavadocProjectConfiguration(this); 285 return config; 286 } 287 288 /** 289 * Checks that the builder constraints are met. 290 * 291 * @throws IllegalArgumentException if at least one constraint is offended. 292 */ 293 private void checkConstraints() throws IllegalArgumentException 294 { 295 final StringBuilder buffer = new StringBuilder(); 296 297 ConfigUtils.checkStringValueProvided(buffer, projectName, 298 "Project name must not be 'null'."); 299 ConfigUtils.checkCollectionValueProvided(buffer, classRootDirectoryNames, 300 "Class root directory names must be provided."); 301 ConfigUtils.checkCollectionValueProvided(buffer, 302 sourceRootDirectoryNames, 303 "Source root directory names must be provided."); 304 305 if (buffer.length() > 0) 306 { 307 throw new IllegalArgumentException( 308 "The following arguments are invalid:" + buffer); 309 } 310 } 311 312 // --- object basics ------------------------------------------------------ 313 } 314 315 // ********************************* Methods ******************************** 316 317 // --- init ----------------------------------------------------------------- 318 319 // --- get&set -------------------------------------------------------------- 320 321 /** 322 * Returns the classpath used by the tool. 323 * 324 * @return the classpath used by the tool. 325 */ 326 public List<String> getToolClassPath() 327 { 328 return toolClassPath; 329 } 330 331 /** 332 * {@inheritDoc} 333 * 334 * @see de.smartics.exceptions.report.core.ExceptionCodeProjectConfiguration#getSourceEncoding() 335 */ 336 public String getSourceEncoding() 337 { 338 return sourceEncoding; 339 } 340 341 /** 342 * {@inheritDoc} 343 * 344 * @see de.smartics.exceptions.report.core.ExceptionCodeProjectConfiguration#getSourceVersion() 345 */ 346 public String getSourceVersion() 347 { 348 return sourceVersion; 349 } 350 351 /** 352 * {@inheritDoc} 353 * 354 * @see de.smartics.exceptions.report.core.ExceptionCodeProjectConfiguration#getClassRootDirectoryNames() 355 */ 356 public Collection<String> getClassRootDirectoryNames() 357 { 358 return classRootDirectoryNames; 359 } 360 361 /** 362 * Returns the class loader to load project classes. 363 * 364 * @return the class loader to load project classes. 365 */ 366 public ClassLoader getProjectClassLoader() 367 { 368 return projectClassLoader; 369 } 370 371 /** 372 * {@inheritDoc} 373 * 374 * @see de.smartics.exceptions.report.core.ExceptionCodeProjectConfiguration#getSourceRootDirectoryNames() 375 */ 376 public Collection<String> getSourceRootDirectoryNames() 377 { 378 return sourceRootDirectoryNames; 379 } 380 381 /** 382 * {@inheritDoc} 383 * 384 * @see de.smartics.exceptions.report.core.ExceptionCodeProjectConfiguration#getProjectName() 385 */ 386 public String getProjectName() 387 { 388 return projectName; 389 } 390 391 /** 392 * Returns the includes for the scanner. 393 * 394 * @return the includes for the scanner. 395 */ 396 public List<String> getIncludes() 397 { 398 return includes; 399 } 400 401 /** 402 * Returns the excludes for the scanner. 403 * 404 * @return the excludes for the scanner. 405 */ 406 public List<String> getExcludes() 407 { 408 return excludes; 409 } 410 411 // --- business ------------------------------------------------------------- 412 413 // --- object basics -------------------------------------------------------- 414 415 }