View Javadoc

1   package org.apache.maven.plugin.ear.output;
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  /**
23   * Provides access to {@link FileNameMapping} implementations.
24   * <p/>
25   * Two basic implementations are provided by default:
26   * <ul>
27   * <li>standard: the default implementation</li>
28   * <li>full: an implementation that maps to a 'full' file name, i.e. containing the groupId</li>
29   * </ul>
30   * 
31   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
32   * @version $Id: FileNameMappingFactory.java 1542508 2013-11-16 13:21:35Z rfscholte $
33   */
34  public class FileNameMappingFactory
35  {
36      static final String STANDARD_FILE_NAME_MAPPING = "standard";
37  
38      static final String FULL_FILE_NAME_MAPPING = "full";
39  
40      static final String NO_VERSION_FILE_NAME_MAPPING = "no-version";
41  
42      static final String NO_VERSION_FOR_EJB_FILE_NAME_MAPPING = "no-version-for-ejb";
43  
44      private FileNameMappingFactory()
45      {
46      }
47  
48      public static FileNameMapping getDefaultFileNameMapping()
49      {
50          return new StandardFileNameMapping();
51      }
52  
53      /**
54       * Returns the file name mapping implementation based on a logical name of a fully qualified name of the class.
55       * 
56       * @param nameOrClass a name of the fqn of the implementation
57       * @return the file name mapping implementation
58       * @throws IllegalStateException if the implementation is not found
59       */
60      public static FileNameMapping getFileNameMapping( final String nameOrClass )
61          throws IllegalStateException
62      {
63          if ( STANDARD_FILE_NAME_MAPPING.equals( nameOrClass ) )
64          {
65              return getDefaultFileNameMapping();
66          }
67          if ( FULL_FILE_NAME_MAPPING.equals( nameOrClass ) )
68          {
69              return new FullFileNameMapping();
70          }
71          if ( NO_VERSION_FILE_NAME_MAPPING.equals( nameOrClass ) )
72          {
73              return new NoVersionFileNameMapping();
74          }
75          if ( NO_VERSION_FOR_EJB_FILE_NAME_MAPPING.equals( nameOrClass ) )
76          {
77              return new NoVersionForEjbFileNameMapping();
78          }
79          try
80          {
81              final Class<?> c = Class.forName( nameOrClass );
82              return (FileNameMapping) c.newInstance();
83          }
84          catch ( ClassNotFoundException e )
85          {
86              throw new IllegalStateException( "File name mapping implementation[" + nameOrClass + "] was not found "
87                  + e.getMessage() );
88          }
89          catch ( InstantiationException e )
90          {
91              throw new IllegalStateException( "Could not instantiate file name mapping implementation[" + nameOrClass
92                  + "] make sure it has a default public constructor" );
93          }
94          catch ( IllegalAccessException e )
95          {
96              throw new IllegalStateException( "Could not access file name mapping implementation[" + nameOrClass
97                  + "] make sure it has a default public constructor" );
98          }
99          catch ( ClassCastException e )
100         {
101             throw new IllegalStateException( "Specified class[" + nameOrClass + "] does not implement["
102                 + FileNameMapping.class.getName() + "]" );
103         }
104     }
105 }