3914b6d2fe5a4b4e753c150069465d72b5d26ffb
[cfb.git] / prod / net / jaekl / cfb / CfbBundle.java
1 package net.jaekl.cfb;
2
3 // Copyright (C) 2015 Christian Jaekl
4
5 import java.text.MessageFormat;
6 import java.util.Locale;
7 import java.util.MissingResourceException;
8 import java.util.ResourceBundle;
9 import java.util.concurrent.ConcurrentHashMap;
10
11 import net.jaekl.qd.QDBundleFactory;
12
13 public class CfbBundle {
14         public static final String ANALYSIS_FAILED = "analysis.failed";
15         public static final String ANALYZED_AT = "analyzed.at";
16         public static final String BUG_CATEGORY_UNKNOWN = "bug.category.unknown";
17         public static final String BUG_TYPE_UNKNOWN = "bug.type.unknown";
18         public static final String CANNOT_CONNECT = "cannot.connect.to.db";
19         public static final String CANNOT_EXEC = "cannot.exec";
20         public static final String CANNOT_LOAD_FBMSG_FILE = "cannot.load.fbmsg.file";
21         public static final String CANNOT_SEND_MAIL = "cannot.send.mail";
22         public static final String CFB = "cfb";
23         public static final String CFB_MAIL_SUBJECT = "cfb.mail.subject";
24         public static final String CFB_REPORT = "cfb.report";
25         public static final String COMPARING_RUNS = "comparing.runs";
26         public static final String COMPARING_VERSIONS = "comparing.versions";
27         public static final String FINDBUGS_HOME_IS_NOT_SET = "findbugs.home.is.not.set";
28         public static final String FINDBUGS_HOME_IS_SET_TO = "findbugs.home.is.set.to";
29         public static final String FIXED_BUGS = "fixed.bugs";
30         public static final String NEW_BUGS = "new.bugs";
31         public static final String NEW_VERSION = "new.version";
32         public static final String NUM_BUGS = "num.bugs";
33         public static final String NUM_BUGS_OLD = "num.bugs.old";
34         public static final String OLD_BUGS = "old.bugs";
35         public static final String OLD_VERSION = "old.version";
36         public static final String STDERR_WAS = "stderr.was";
37         public static final String STDOUT_WAS = "stdout.was";
38         public static final String VERSION_NUM = "version.num";
39         
40         final static String BUNDLE_NAME = "cfb";
41         
42         static ConcurrentHashMap<Locale, CfbBundle> m_bundleMap = new ConcurrentHashMap<Locale, CfbBundle>();
43         
44         ResourceBundle m_bundle;
45         
46         public static CfbBundle getInst(Locale locale) {
47                 CfbBundle result = m_bundleMap.get(locale);
48                 if (null == result) {
49                         synchronized(CfbBundle.class) {
50                                 result = m_bundleMap.get(locale);
51                                 if (null == result) {
52                                         result = new CfbBundle(locale); 
53                                 }
54                                 m_bundleMap.put(locale, result);
55                         }
56                 }
57                 return result;
58         }
59         
60         private CfbBundle(Locale locale) {
61                 m_bundle = QDBundleFactory.getInst().getBundle(BUNDLE_NAME, locale); 
62         }
63         
64         // This constructor is intended only for use during unit testing.
65         CfbBundle() {
66                 m_bundle = null;
67         }
68         
69         public String get(String key, Object... arguments) {
70                 try {
71                         if (null != m_bundle) {
72                                 String pattern = m_bundle.getString(key);
73                                 return MessageFormat.format(pattern, arguments);
74                         }
75                 }
76                 catch (MissingResourceException exc) {
77                         // Make it clear that something has gone wrong.
78                         exc.printStackTrace();  
79                         // Fall through to the fallback behaviour below
80                 }
81                 
82                 return fallbackGet(key, arguments);
83         }
84         
85         String fallbackGet(String key, Object... arguments) {
86                 StringBuilder sb = new StringBuilder("[" + key + "]");
87                 for (Object obj : arguments) {
88                         sb.append("[" + obj + "]");
89                 }
90                 return sb.toString();
91                 
92         }
93 }