yet another rewrite of the producers...
[mir.git] / source / mircoders / global / MirGlobal.java
1 package mircoders.global;
2
3 import mir.misc.*;
4 import mircoders.localizer.*;
5
6 public class MirGlobal {
7   static private MirConfig configuration;
8   static private MirLocalizer localizer;
9   static private ProducerEngine producerEngine;
10
11   public static MirLocalizer localizer() {
12     String localizerClassName;
13     Class localizerClass;
14
15     if (localizer == null ) {
16       synchronized(MirGlobal.class) {
17         if (localizer == null ) {
18           localizerClassName = getConfigPropertyWithDefault("Mir.Localizer", "mirlocal.loaclizer.basic.MirBasicLocalizer");
19
20           try {
21             localizerClass = Class.forName(localizerClassName);
22           }
23           catch (Throwable t) {
24             throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());
25           }
26
27           if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))
28             throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");
29
30           try {
31             localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());
32           }
33           catch (Throwable t) {
34             throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());
35           }
36         }
37       }
38     }
39
40     return localizer;
41   }
42
43   public static MirConfig config() {
44     if (configuration == null) {
45       configuration = new MirConfig();
46     }
47
48     return configuration;
49   }
50
51   public static ProducerEngine producerEngine() {
52     if (producerEngine == null) {
53       producerEngine = new ProducerEngine();
54     }
55
56     return producerEngine;
57   }
58
59   public static String getConfigPropertyWithDefault(String aPropertyName, String aDefault) {
60     String result;
61
62     result = config().getProp(aPropertyName);
63
64     if (result==null)
65       result = aDefault;
66
67     return result;
68   }
69
70   public static String getConfigProperty(String aPropertyName) {
71     String result;
72
73     result = config().getProp(aPropertyName);
74
75     if (result==null)
76       throw new ConfigException("Property '" + aPropertyName + "' not present");
77
78     return result;
79   }
80
81   public static int getConfigIntegerProperty(String aPropertyName) {
82     String result;
83
84     result = config().getProp(aPropertyName);
85
86     return Integer.parseInt(result);
87   }
88
89   public static boolean getConfigBooleanProperty(String aPropertyName) {
90     String result;
91
92     result = config().getProp(aPropertyName);
93
94     if (result==null)
95       throw new ConfigException("Boolean property '" + aPropertyName + "' not present");
96
97     return (result.equals("yes") || result.equals("1"));
98   }
99 }