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