1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid;
12
13 import java.util.Hashtable ;
14 import org.jconfig.* ;
15 import org.jconfig.handler.* ;
16 import org.apache.log4j.Logger;
17 import org.astrogrid.i18n.AstroGridMessage;
18
19 public abstract class Configurator {
20
21 /*** Compile-time switch used to turn tracing on/off.
22 * Set this to false to eliminate all trace statements within the byte code.*/
23 private static final boolean
24 TRACE_ENABLED = true ;
25
26 private static final String
27 ASTROGRIDERROR_COULD_NOT_READ_CONFIGFILE = "AG{0}Z00001:{1}: Could not read my configuration file {2}",
28 ASTROGRIDERROR_COMPONENT_NOT_INITIALIZED = "AG{0}Z00002:{1}: Not initialized. Perhaps my configuration file is missing." ;
29
30 public static final String
31 GENERAL_CATEGORY = "GENERAL" ,
32 GENERAL_VERSION_NUMBER = "VERSION" ;
33
34 private static final String
35 TEMPLATE = "TEMPLATE" ;
36
37 /*** Log4J logger for this class. */
38 private static Logger
39 logger = Logger.getLogger( Configurator.class ) ;
40
41 private static Hashtable
42 loadedConfigurations = new Hashtable() ;
43
44
45 protected Configurator() { this.init() ; }
46
47
48 private void init() {
49 if( TRACE_ENABLED ) logger.debug( "Configurator.init(): entry") ;
50
51 try{
52
53
54 if( Configurator.getConfig( getSubsystemAcronym()
55 , getConfigFileName() ) == null ) {
56
57
58 AstroGridMessage
59 message = new AstroGridMessage( ASTROGRIDERROR_COULD_NOT_READ_CONFIGFILE
60 , getSubsystemAcronym()
61 , Configurator.getClassName( Configurator.class )
62 , getConfigFileName() ) ;
63 logger.error( message.toString() ) ;
64
65 }
66 else {
67
68
69
70 AstroGridMessage.loadMessages( this.getSubsystemAcronym() ) ;
71 }
72
73 }
74 finally {
75 if( TRACE_ENABLED ) logger.debug( "Configurator.init(): exit") ;
76 }
77
78 }
79
80
81 public void checkPropertiesLoaded() throws AstroGridException {
82 if( TRACE_ENABLED ) logger.debug( "checkPropertiesLoaded() entry") ;
83
84 String
85 check = "NOT LOADED" ;
86 Configuration
87 config = getConfig( this.getSubsystemAcronym(), this.getConfigFileName() ) ;
88
89 try{
90 if( config == null
91 ||
92 config.getProperty( GENERAL_VERSION_NUMBER, check, GENERAL_CATEGORY ).equals( check ) ) {
93
94 AstroGridMessage
95 message = new AstroGridMessage( ASTROGRIDERROR_COMPONENT_NOT_INITIALIZED
96 , this.getSubsystemAcronym()
97 , Configurator.getClassName( Configurator.class ) ) ;
98 logger.error( message.toString() ) ;
99 throw new AstroGridException( message ) ;
100 }
101 }
102 finally {
103 if( TRACE_ENABLED ) logger.debug( "checkPropertiesLoaded() exit") ;
104 }
105
106 }
107
108
109 /***
110 *
111 * Static getter for properties from the component's configuration.
112 * <p>
113 *
114 * @param key - the property key within category
115 * @param category - the category within the configuration
116 * @return the String value of the property, or the empty string if null
117 *
118 * @see org.jconfig.jConfig
119 **/
120 public static String getProperty( String subsystemAcronym, String key, String category ) {
121 if( TRACE_ENABLED ) logger.debug( "getProperty() entry") ;
122
123 String
124 targetProperty = null ;
125
126 try {
127
128 Configuration
129 config = ConfigurationManager.getConfiguration( subsystemAcronym ) ;
130
131 targetProperty = config.getProperty( key
132 , ""
133 , category )
134 .trim() ;
135
136 if( key.startsWith( TEMPLATE ) ) {
137 targetProperty = TemplateManager.getInstance().getTemplate( targetProperty ) ;
138 }
139
140 }
141 finally {
142 if( TRACE_ENABLED ) logger.debug( "getProperty() exit") ;
143 }
144
145 return targetProperty ;
146
147 }
148
149
150 abstract protected String getConfigFileName() ;
151 abstract protected String getSubsystemAcronym() ;
152
153 private static Configuration getConfig( String subsystemAcronym, String configFileName ) {
154 if( TRACE_ENABLED ) logger.debug( "Configurator.getConfig(): entry") ;
155
156 Configuration
157 configuration = null ;
158
159 try {
160
161 if( loadedConfigurations.containsKey( subsystemAcronym ) == false ) {
162
163 InputStreamHandler
164 streamHandler = new InputStreamHandler( configFileName ) ;
165
166 ConfigurationManager.getInstance().load( streamHandler, subsystemAcronym ) ;
167 loadedConfigurations.put( subsystemAcronym, configFileName ) ;
168 }
169
170 configuration = ConfigurationManager.getConfiguration( subsystemAcronym ) ;
171 }
172 catch ( ConfigurationManagerException cme ) {
173 logger.error( cme ) ;
174 }
175 finally {
176 if( TRACE_ENABLED ) logger.debug( "Configurator.getConfig(): exit") ;
177 }
178
179 return configuration ;
180
181 }
182
183
184 public static String getClassName ( java.lang.Class cls ) {
185
186 String
187 componentName = cls.getName() ;
188 int
189 iLastPoint = componentName.lastIndexOf('.') ;
190 return componentName.substring( iLastPoint + 1) ;
191
192 }
193
194 }