View Javadoc
1   /*
2    *   Copyright (C) 2005 Christian Schulte <cs@schulte.it>
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: TestInstanceShowMojo.java 5228 2016-04-24 11:08:06Z schulte $
29   *
30   */
31  package org.jomc.mojo;
32  
33  import javax.xml.bind.JAXBElement;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.apache.maven.plugins.annotations.ResolutionScope;
38  import org.jomc.model.Instance;
39  import org.jomc.model.Modules;
40  import org.jomc.model.modlet.ModelHelper;
41  import org.jomc.modlet.Model;
42  import org.jomc.modlet.ModelContext;
43  
44  /**
45   * Displays an instance from the project's test model.
46   *
47   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
48   * @version $JOMC: TestInstanceShowMojo.java 5228 2016-04-24 11:08:06Z schulte $
49   * @since 1.1
50   */
51  @Mojo( name = "show-test-instance",
52         requiresDependencyResolution = ResolutionScope.TEST,
53         threadSafe = true )
54  public final class TestInstanceShowMojo extends AbstractModelShowMojo
55  {
56  
57      /**
58       * Identifier of the instance to show.
59       */
60      @Parameter( name = "identifier",
61                  property = "jomc.identifier",
62                  required = true )
63      private String identifier;
64  
65      /**
66       * Execution strategy of the goal ({@code always} or {@code once-per-session}).
67       */
68      @Parameter( name = "showTestInstanceExecutionStrategy",
69                  property = "jomc.showTestInstanceExecutionStrategy",
70                  defaultValue = "once-per-session" )
71      private String showTestInstanceExecutionStrategy;
72  
73      /**
74       * Creates a new {@code TestInstanceShowMojo} instance.
75       */
76      public TestInstanceShowMojo()
77      {
78          super();
79      }
80  
81      @Override
82      protected JAXBElement<?> getDisplayModel( final ModelContext modelContext ) throws MojoExecutionException
83      {
84          final Model model = this.getModel( modelContext );
85          final Modules modules = ModelHelper.getModules( model );
86          final Instance instance = modules != null ? modules.getInstance( this.identifier ) : null;
87          JAXBElement<?> displayModel = null;
88  
89          if ( instance != null )
90          {
91              displayModel = new org.jomc.model.ObjectFactory().createInstance( instance );
92          }
93  
94          return displayModel;
95      }
96  
97      @Override
98      protected ClassLoader getDisplayClassLoader() throws MojoExecutionException
99      {
100         return this.getTestClassLoader();
101     }
102 
103     @Override
104     protected String getGoal() throws MojoExecutionException
105     {
106         return "show-test-instance";
107     }
108 
109     @Override
110     protected String getExecutionStrategy() throws MojoExecutionException
111     {
112         return this.showTestInstanceExecutionStrategy;
113     }
114 
115 }