Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileNameMappingFactory |
|
| 4.666666666666667;4.667 |
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 | import java.util.HashMap; | |
23 | import java.util.Map; | |
24 | ||
25 | /** | |
26 | * Provides access to {@link FileNameMapping} implementations. | |
27 | * <p/> | |
28 | * Two basic implementations are provided by default: | |
29 | * <ul> | |
30 | * <li>standard: the default implementation</li> | |
31 | * <li>full: an implementation that maps to a 'full' file name, i.e. containing the groupId</li> | |
32 | * </ul> | |
33 | * | |
34 | * @author <a href="snicoll@apache.org">Stephane Nicoll</a> | |
35 | * @version $Id: FileNameMappingFactory.java 485857 2006-12-11 20:34:39Z snicoll $ | |
36 | */ | |
37 | public class FileNameMappingFactory | |
38 | { | |
39 | public static final String STANDARD_FILE_NAME_MAPPING = "standard"; | |
40 | ||
41 | public static final String FULL_FILE_NAME_MAPPING = "full"; | |
42 | ||
43 | 0 | public static final FileNameMappingFactory INSTANCE = new FileNameMappingFactory(); |
44 | ||
45 | private final Map mappings; | |
46 | ||
47 | private FileNameMappingFactory() | |
48 | 0 | { |
49 | 0 | mappings = new HashMap(); |
50 | 0 | mappings.put( STANDARD_FILE_NAME_MAPPING, new StandardFileNameMapping() ); |
51 | 0 | mappings.put( FULL_FILE_NAME_MAPPING, new FullFileNameMapping() ); |
52 | 0 | } |
53 | ||
54 | public FileNameMapping getDefaultFileNameMapping() | |
55 | { | |
56 | 0 | return getFileNameMapping( STANDARD_FILE_NAME_MAPPING ); |
57 | } | |
58 | ||
59 | /** | |
60 | * Returns the file name mapping implementation based on a logical name | |
61 | * of a fully qualified name of the class. | |
62 | * | |
63 | * @param nameOrClass a name of the fqn of the implementation | |
64 | * @return the file name mapping implementation | |
65 | * @throws IllegalStateException if the implementation is not found | |
66 | */ | |
67 | public FileNameMapping getFileNameMapping( final String nameOrClass ) | |
68 | throws IllegalStateException | |
69 | { | |
70 | // Check if it's there yet | |
71 | 0 | if ( mappings.containsKey( nameOrClass ) ) |
72 | { | |
73 | 0 | return (FileNameMapping) mappings.get( nameOrClass ); |
74 | } | |
75 | else | |
76 | { | |
77 | try | |
78 | { | |
79 | 0 | final Class c = Class.forName( nameOrClass ); |
80 | 0 | final FileNameMapping fnm = (FileNameMapping) c.newInstance(); |
81 | 0 | mappings.put( nameOrClass, fnm ); |
82 | 0 | return fnm; |
83 | } | |
84 | 0 | catch ( ClassNotFoundException e ) |
85 | { | |
86 | 0 | throw new IllegalStateException( |
87 | "File name mapping implementation[" + nameOrClass + "] was not found " + e.getMessage() ); | |
88 | } | |
89 | 0 | catch ( InstantiationException e ) |
90 | { | |
91 | 0 | throw new IllegalStateException( "Could not instanciate file name mapping implementation[" + |
92 | nameOrClass + "] make sure it has a default public constructor" ); | |
93 | } | |
94 | 0 | catch ( IllegalAccessException e ) |
95 | { | |
96 | 0 | throw new IllegalStateException( "Could not access file name mapping implementation[" + nameOrClass + |
97 | "] make sure it has a default public constructor" ); | |
98 | } | |
99 | 0 | catch ( ClassCastException e ) |
100 | { | |
101 | 0 | throw new IllegalStateException( "Specified class[" + nameOrClass + "] does not implement[" + |
102 | 0 | FileNameMapping.class.getName() + "]" ); |
103 | } | |
104 | } | |
105 | } | |
106 | } |