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