1 /* 2 * Copyright 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.plugin.jboss.modules.descriptor; 17 18 import org.apache.commons.lang.StringUtils; 19 import org.apache.commons.lang.builder.ToStringBuilder; 20 21 /** 22 * Stores information about a matcher on modules to apply dependency information 23 * to. A module descriptor stores dependencies for its resources. The 24 * dependencies are matched with Maven artifacts and mapped to module names. 25 * These module names are matched to check if the information of a dependencies 26 * descriptor is to be applied. 27 */ 28 public final class DependenciesDescriptor 29 { 30 // ********************************* Fields ********************************* 31 32 // --- constants ------------------------------------------------------------ 33 34 // --- members -------------------------------------------------------------- 35 36 /** 37 * The matcher to match modules by their name. 38 */ 39 private final ModuleMatcher matcher; 40 41 /** 42 * The slot of the dependency. 43 */ 44 private final String slot; 45 46 /** 47 * The export attribute for the dependency. 48 */ 49 private final Boolean export; 50 51 /** 52 * The services attribute for the dependency. 53 */ 54 private final String services; 55 56 /** 57 * The optional attribute for the dependency. 58 */ 59 private final Boolean optional; 60 61 /** 62 * The XML fragment specifying imports for the dependency. 63 */ 64 private final String importsXml; 65 66 /** 67 * The XML fragment specifying exports for the dependency. 68 */ 69 private final String exportsXml; 70 71 // ****************************** Initializer ******************************* 72 73 // ****************************** Constructors ****************************** 74 75 private DependenciesDescriptor(final Builder builder) 76 { 77 matcher = builder.matcher; 78 slot = builder.slot; 79 export = builder.export; 80 services = builder.services; 81 optional = builder.optional; 82 importsXml = builder.importsXml; 83 exportsXml = builder.exportsXml; 84 } 85 86 // ****************************** Inner Classes ***************************** 87 88 /** 89 * Builds instances of {@link DependenciesDescriptor}. 90 */ 91 public static final class Builder 92 { 93 // ******************************** Fields ******************************** 94 95 // --- constants ---------------------------------------------------------- 96 97 // --- members ------------------------------------------------------------ 98 99 /** 100 * The matcher to match modules by their name. 101 */ 102 private ModuleMatcher matcher; 103 104 /** 105 * The slot of the dependency. 106 */ 107 private String slot; 108 109 /** 110 * The export attribute for the dependency. 111 */ 112 private Boolean export; 113 114 /** 115 * The services attribute for the dependency. 116 */ 117 private String services; 118 119 /** 120 * The optional attribute for the dependency. 121 */ 122 private Boolean optional; 123 124 /** 125 * The XML fragment specifying imports for the dependency. 126 */ 127 private String importsXml; 128 129 /** 130 * The XML fragment specifying exports for the dependency. 131 */ 132 private String exportsXml; 133 134 // ***************************** Initializer ****************************** 135 136 // ***************************** Constructors ***************************** 137 138 // ***************************** Inner Classes **************************** 139 140 // ******************************** Methods ******************************* 141 142 // --- init --------------------------------------------------------------- 143 144 // --- get&set ------------------------------------------------------------ 145 146 /** 147 * Sets the matcher to match modules by their name. 148 * 149 * @param matcher the matcher to match modules by their name. 150 * @return a reference to this builder. 151 */ 152 public Builder with(final ModuleMatcher matcher) 153 { 154 this.matcher = matcher; 155 return this; 156 } 157 158 /** 159 * Sets the slot of the dependency. 160 * 161 * @param slot the slot of the dependency. 162 * @return a reference to this builder. 163 */ 164 public Builder withSlot(final String slot) 165 { 166 if (StringUtils.isNotBlank(slot)) 167 { 168 this.slot = slot; 169 } 170 return this; 171 } 172 173 /** 174 * Sets the export attribute for the dependency. 175 * 176 * @param export the export attribute for the dependency. 177 * @return a reference to this builder. 178 */ 179 public Builder withExport(final String export) 180 { 181 if (StringUtils.isNotBlank(export)) 182 { 183 this.export = Boolean.parseBoolean(export); 184 } 185 return this; 186 } 187 188 /** 189 * Sets the services attribute for the dependency. 190 * 191 * @param services the services attribute for the dependency. 192 * @return a reference to this builder. 193 */ 194 public Builder withServices(final String services) 195 { 196 if (StringUtils.isNotBlank(services)) 197 { 198 this.services = services; 199 } 200 return this; 201 } 202 203 /** 204 * Sets the optional attribute for the dependency. 205 * 206 * @param optional the optional attribute for the dependency. 207 * @return a reference to this builder. 208 */ 209 public Builder withOptional(final String optional) 210 { 211 if (StringUtils.isNotBlank(optional)) 212 { 213 this.optional = Boolean.parseBoolean(optional); 214 } 215 return this; 216 } 217 218 /** 219 * Sets the XML fragment specifying imports for the dependency. 220 * 221 * @param importsXml the XML fragment specifying imports for the dependency. 222 * @return a reference to this builder. 223 */ 224 public Builder withImportsXml(final String importsXml) 225 { 226 this.importsXml = importsXml; 227 return this; 228 } 229 230 /** 231 * Sets the XML fragment specifying exports for the dependency. 232 * 233 * @param exportsXml the XML fragment specifying exports for the dependency. 234 * @return a reference to this builder. 235 */ 236 public Builder withExportsXml(final String exportsXml) 237 { 238 this.exportsXml = exportsXml; 239 return this; 240 } 241 242 // --- business ----------------------------------------------------------- 243 244 /** 245 * Merges the content of the given descriptor into the builder instance. 246 * <p> 247 * Not that the matcher information is not merged. 248 * </p> 249 * 250 * @param moduleName the name of the module to merge the descriptor. 251 * @param descriptor the descriptor information to merge into this instance. 252 */ 253 public void merge(final String moduleName, 254 final DependenciesDescriptor descriptor) 255 { 256 slot = merge("slot", moduleName, slot, descriptor.slot); 257 export = merge("export", moduleName, export, descriptor.export); 258 services = merge("services", moduleName, services, descriptor.services); 259 optional = merge("optional", moduleName, optional, descriptor.optional); 260 importsXml = 261 merge("imports", moduleName, importsXml, descriptor.importsXml); 262 exportsXml = 263 merge("exports", moduleName, exportsXml, descriptor.exportsXml); 264 } 265 266 private <T> T merge(final String property, final String moduleName, 267 final T value1, final T value2) 268 { 269 if (value1 != null && value2 != null && !value1.equals(value2)) 270 { 271 throw new IllegalArgumentException(String.format( 272 "Module %s: Cannot merge %s: '%s' differs from '%s'.", moduleName, 273 property, value1, value2)); 274 } 275 276 return value1 == null ? value2 : value1; 277 } 278 279 /** 280 * Builds an instance of {@link DependenciesDescriptor}. 281 * 282 * @return the instance. 283 */ 284 public DependenciesDescriptor build() 285 { 286 if (matcher == null) 287 { 288 matcher = new ModuleMatcher.Builder().build(); 289 } 290 291 if (export == null) 292 { 293 export = Boolean.FALSE; 294 } 295 if (optional == null) 296 { 297 optional = Boolean.FALSE; 298 } 299 300 return new DependenciesDescriptor(this); 301 } 302 303 // --- object basics ------------------------------------------------------ 304 } 305 306 // ********************************* Methods ******************************** 307 308 // --- init ----------------------------------------------------------------- 309 310 // --- get&set -------------------------------------------------------------- 311 312 /** 313 * Returns the matcher to match modules by their name. 314 * 315 * @return the matcher to match modules by their name. 316 */ 317 public ModuleMatcher getMatcher() 318 { 319 return matcher; 320 } 321 322 /** 323 * Returns the slot of the dependency. 324 * 325 * @return the slot of the dependency. 326 */ 327 public String getSlot() 328 { 329 return slot; 330 } 331 332 /** 333 * Returns the export attribute for the dependency. 334 * 335 * @return the export attribute for the dependency. 336 */ 337 public Boolean getExport() 338 { 339 return export; 340 } 341 342 /** 343 * Returns the services attribute for the dependency. 344 * 345 * @return the services attribute for the dependency. 346 */ 347 public String getServices() 348 { 349 return services; 350 } 351 352 /** 353 * Returns the optional attribute for the dependency. 354 * 355 * @return the optional attribute for the dependency. 356 */ 357 public Boolean getOptional() 358 { 359 return optional; 360 } 361 362 /** 363 * Returns the XML fragment specifying imports for the dependency. 364 * 365 * @return the XML fragment specifying imports for the dependency. 366 */ 367 public String getImportsXml() 368 { 369 return importsXml; 370 } 371 372 /** 373 * Returns the XML fragment specifying exports for the dependency. 374 * 375 * @return the XML fragment specifying exports for the dependency. 376 */ 377 public String getExportsXml() 378 { 379 return exportsXml; 380 } 381 382 // --- business ------------------------------------------------------------- 383 384 /** 385 * Checks if the matcher of this descriptor matches with the given module 386 * name. 387 * 388 * @param name the module name to match. 389 * @return <code>true</code> on a match, <code>false</code> otherwise. 390 */ 391 public boolean matches(final String name) 392 { 393 return matcher.matches(name); 394 } 395 396 // --- object basics -------------------------------------------------------- 397 398 /** 399 * {@inheritDoc} 400 * <p> 401 * Provides the properties via reflection for displaying debug information. 402 * </p> 403 */ 404 @Override 405 public String toString() 406 { 407 return ToStringBuilder.reflectionToString(this); 408 } 409 410 }