View Javadoc

1   /**
2    * The MIT License
3    *
4    * Copyright 2006-2012 The Codehaus.
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package org.codehaus.mojo.appassembler.daemon.merge;
25  
26  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
27  import org.codehaus.mojo.appassembler.model.Daemon;
28  import org.codehaus.mojo.appassembler.model.JvmSettings;
29  import org.codehaus.mojo.appassembler.model.Classpath;
30  import org.codehaus.plexus.logging.AbstractLogEnabled;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  import java.util.List;
34  
35  /**
36   * @author <a href="mailto:trygve.laugstol@objectware.no">Trygve Laugst&oslash;l</a>
37   * @version $Id: DefaultDaemonMerger.java 16448 2012-04-22 15:47:18Z khmarbaise $
38   * @plexus.component
39   */
40  public class DefaultDaemonMerger
41          extends AbstractLogEnabled
42          implements DaemonMerger
43  {
44      // -----------------------------------------------------------------------
45      // DaemonMerger Implementation
46      // -----------------------------------------------------------------------
47  
48      public Daemon mergeDaemons ( Daemon dominant, Daemon recessive )
49              throws DaemonGeneratorException
50      {
51          if ( dominant == null )
52          {
53              return recessive;
54          }
55  
56          if ( recessive == null )
57          {
58              return dominant;
59          }
60  
61          Daemon result = new Daemon ( );
62  
63          result.setId ( select ( dominant.getId ( ), recessive.getId ( ) ) );
64          result.setMainClass ( select ( dominant.getMainClass ( ), recessive.getMainClass ( ) ) );
65          result.setClasspath ( ( Classpath ) select ( dominant.getClasspath ( ), recessive.getClasspath ( ) ) );
66          result.setCommandLineArguments (
67                  select ( dominant.getCommandLineArguments ( ), recessive.getCommandLineArguments ( ) ) );
68          // This should probably be improved
69          result.setJvmSettings ( ( JvmSettings ) select ( dominant.getJvmSettings ( ), recessive.getJvmSettings ( ) ) );
70          result.setShowConsoleWindow ( dominant.isShowConsoleWindow ( ) );
71  
72          return result;
73      }
74  
75      // -----------------------------------------------------------------------
76      // Private
77      // -----------------------------------------------------------------------
78  
79      private String select ( String dominant, String recessive )
80      {
81          if ( StringUtils.isNotEmpty ( dominant ) )
82          {
83              return dominant;
84          }
85          else
86          {
87              return recessive;
88          }
89      }
90  
91      private List select ( List dominant, List recessive )
92      {
93          // Even if the list is empty, return it. This makes it possible to clear the default list.
94  
95          // TODO: The above is not possible as long as the modello generated stuff returns an empty list on not set
96          // fields.
97          if ( dominant != null && dominant.size ( ) > 0 )
98          {
99              return dominant;
100         }
101         else
102         {
103             return recessive;
104         }
105     }
106 
107     private Object select ( Object dominant, Object recessive )
108     {
109         if ( dominant != null )
110         {
111             return dominant;
112         }
113         else
114         {
115             return recessive;
116         }
117     }
118 }