View Javadoc
1   package org.apache.maven.plugins.ejb;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  /**
25   * This class contains some helper methods which do not belong to {@link EjbMojo}.
26   * 
27   * <pre>
28   * Think about this helper class, cause i've got the impression this can be made better.
29   * </pre>
30   * 
31   * @author <a href="mailto:khmarbaise@apache.org">Karl Heinz Marbaise</a>
32   */
33  public final class EjbHelper
34  {
35      private EjbHelper()
36      {
37          // prevent instantiation
38      }
39  
40      /**
41       * Check if a <code>classifier</code> is valid or not.
42       * 
43       * @param classifier The classifier which should be checked.
44       * @return true in case of a valid <code>classifier</code> false otherwise which includes the case where
45       *         <code>classifier</code> is {@code null}.
46       */
47      public static boolean isClassifierValid( String classifier )
48      {
49          // @FIXME: Check classifier for trailing dash? "a-0" valid?
50          // What are the rules for a valid classifier? Somewhere documented? which can be used as a reference?
51          boolean result = false;
52  
53          // The following check is only based on an educated guess ;-)
54          if ( hasClassifier( classifier ) && classifier.matches( "^[a-zA-Z]+[0-9a-zA-Z\\-]*" ) )
55          {
56              result = true;
57          }
58  
59          return result;
60      }
61  
62      /**
63       * Check if the given classifier exists in the meaning of not being {@code null} and contain something else than
64       * only white spaces.
65       * 
66       * @param classifier The classifier to be used.
67       * @return true in case when the given classifier is not {@code null} and contains something else than white spaces.
68       */
69      public static boolean hasClassifier( String classifier )
70      {
71          boolean result = false;
72          if ( classifier != null && classifier.trim().length() > 0 )
73          {
74              result = true;
75          }
76          return result;
77      }
78  
79      /**
80       * Returns the Jar file to generate, based on an optional classifier.
81       *
82       * @param basedir the output directory
83       * @param finalName the name of the ear file
84       * @param classifier an optional classifier
85       * @return the file to generate
86       */
87      public static File getJarFile( File basedir, String finalName, String classifier )
88      {
89          if ( basedir == null )
90          {
91              throw new IllegalArgumentException( "basedir is not allowed to be null" );
92          }
93          if ( finalName == null )
94          {
95              throw new IllegalArgumentException( "finalName is not allowed to be null" );
96          }
97  
98          StringBuilder fileName = new StringBuilder( finalName );
99  
100         if ( hasClassifier( classifier ) )
101         {
102             fileName.append( "-" ).append( classifier );
103         }
104 
105         fileName.append( ".jar" );
106 
107         return new File( basedir, fileName.toString() );
108     }
109 
110 }