1 package org.apache.tomcat.maven.common.messages;
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 org.codehaus.plexus.component.annotations.Component;
23
24 import java.text.MessageFormat;
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
27
28 /**
29 * @author Olivier Lamy
30 * @component role="org.apache.tomcat.maven.common.messages.MessagesProvider" role-hint="default"
31 * @since 2.0
32 */
33 @Component( role = MessagesProvider.class )
34 public class DefaultMessagesProvider
35 implements MessagesProvider
36 {
37
38 /**
39 * plugin messages
40 */
41 private ResourceBundle messages;
42
43
44 public DefaultMessagesProvider()
45 {
46 String packageName = getClass().getPackage().getName();
47
48 messages = ResourceBundle.getBundle( packageName + ".messages" );
49 }
50
51 public ResourceBundle getResourceBundle()
52 {
53 return this.messages;
54 }
55
56 /**
57 * Gets the message for the given key from this packages resource bundle.
58 *
59 * @param key the key for the required message
60 * @return the message
61 */
62 public String getMessage( String key )
63 {
64 try
65 {
66 return getResourceBundle().getString( key );
67 }
68 catch ( NullPointerException exception )
69 {
70 return "???" + key + "???";
71 }
72 catch ( MissingResourceException exception )
73 {
74 return "???" + key + "???";
75 }
76 catch ( ClassCastException exception )
77 {
78 return "???" + key + "???";
79 }
80 }
81
82 /**
83 * Gets the message for the given key from this packages resource bundle and formats it with the given parameter.
84 *
85 * @param key the key for the required message
86 * @param params the parameters to be used to format the message with
87 * @return the formatted message
88 */
89 public String getMessage( String key, Object... params )
90 {
91 return MessageFormat.format( getMessage( key ), params );
92 }
93
94 }