1313import org .apache .log4j .Logger ;
1414import org .xmpp .packet .JID ;
1515
16- public class Configuration extends Properties
17- {
16+ public class Configuration extends Properties {
1817 private static final Logger LOGGER = Logger .getLogger (Configuration .class );
19-
18+
2019 private static final long serialVersionUID = 1L ;
21-
20+
2221 private static final String ARRAY_PROPERTY_SEPARATOR = ";" ;
2322
2423 public static final String CONFIGURATION_SERVER_DOMAIN = "server.domain" ;
2524 public static final String CONFIGURATION_SERVER_CHANNELS_DOMAIN = "server.domain.channels" ;
2625 public static final String CONFIGURATION_SERVER_TOPICS_DOMAIN = "server.domain.topics" ;
27-
26+
2827 public static final String CONFIGURATION_ADMIN_USERS = "users.admin" ;
2928
3029 public static final String CONFIGURATION_CHANNELS_AUTOSUBSCRIBE = "channels.autosubscribe" ;
31-
30+ public static final String CONFIGURATION_CHANNELS_AUTOSUBSCRIBE_AUTOAPPROVE = "channels.autosubscribe.autoapprove" ;
31+
3232 private static final String CONFIGURATION_FILE = "configuration.properties" ;
33- private static Configuration instance = null ;
34-
33+ private static Configuration instance = null ;
34+
3535 private Collection <JID > adminUsers = new ArrayList <JID >();
3636 private Collection <JID > autosubscribeChannels = new ArrayList <JID >();
37-
37+
3838 private Properties conf ;
39-
40- private Configuration ()
41- {
42- try {
43- conf = new Properties ();
44- File f = new File (CONFIGURATION_FILE );
45-
46- if (f .exists ()) {
47- LOGGER .info ("Found " + CONFIGURATION_FILE + " in working directory." );
48- load (new FileInputStream (f ));
49- } else {
50- // Otherwise attempt to load it from the classpath
51- LOGGER .info ("No " + CONFIGURATION_FILE + " found in working directory. Attempting to load from classpath." );
52- load (this .getClass ().getClassLoader ().getResourceAsStream (CONFIGURATION_FILE ));
53- }
54- } catch (Exception e ) {
55- System .out .println (e .getMessage ());
56- System .exit (1 );
57- }
58- }
59-
60- private void setupCollections () {
61- adminUsers = getJIDArrayProperty (CONFIGURATION_ADMIN_USERS );
62- autosubscribeChannels = getJIDArrayProperty (CONFIGURATION_CHANNELS_AUTOSUBSCRIBE );
39+
40+ private Configuration () {
41+ try {
42+ conf = new Properties ();
43+ File f = new File (CONFIGURATION_FILE );
44+
45+ if (f .exists ()) {
46+ LOGGER .info ("Found " + CONFIGURATION_FILE
47+ + " in working directory." );
48+ load (new FileInputStream (f ));
49+ } else {
50+ // Otherwise attempt to load it from the classpath
51+ LOGGER .info ("No "
52+ + CONFIGURATION_FILE
53+ + " found in working directory. Attempting to load from classpath." );
54+ load (this .getClass ().getClassLoader ()
55+ .getResourceAsStream (CONFIGURATION_FILE ));
56+ }
57+ } catch (Exception e ) {
58+ System .out .println (e .getMessage ());
59+ System .exit (1 );
60+ }
61+ }
62+
63+ private void setupCollections () {
64+ adminUsers = getJIDArrayProperty (CONFIGURATION_ADMIN_USERS );
65+ autosubscribeChannels = getJIDArrayProperty (CONFIGURATION_CHANNELS_AUTOSUBSCRIBE );
66+ }
67+
68+ public Collection <JID > getAdminUsers () {
69+ return adminUsers ;
70+ }
71+
72+ public Collection <JID > getAutosubscribeChannels () {
73+ return autosubscribeChannels ;
74+ }
75+
76+ public static Configuration getInstance () {
77+ if (null == instance ) {
78+ instance = new Configuration ();
79+ }
80+ return instance ;
81+ }
82+
83+ public String getProperty (String key ) {
84+ return conf .getProperty (key );
85+ }
86+
87+ public String getProperty (String key , String defaultValue ) {
88+ return conf .getProperty (key , defaultValue );
89+ }
90+
91+ public void load (InputStream inputStream ) throws IOException {
92+ conf .load (inputStream );
93+ setupCollections ();
94+ }
95+
96+ private Collection <String > getStringArrayProperty (String key ) {
97+ String prop = getProperty (key );
98+
99+ if (null == prop ) {
100+ return Collections .emptyList ();
101+ }
102+
103+ return Arrays .asList (prop .split (ARRAY_PROPERTY_SEPARATOR ));
104+ }
105+
106+ private Collection <JID > getJIDArrayProperty (String key ) {
107+ Collection <String > props = getStringArrayProperty (key );
108+
109+ Collection <JID > jids = new ArrayList <JID >(props .size ());
110+
111+ for (String prop : props ) {
112+ jids .add (new JID (prop ));
113+ }
114+
115+ return jids ;
116+ }
117+
118+ public String getServerDomain () {
119+ return getProperty (CONFIGURATION_SERVER_DOMAIN );
120+ }
121+
122+ public String getServerChannelsDomain () {
123+ return getProperty (CONFIGURATION_SERVER_CHANNELS_DOMAIN );
124+ }
125+
126+ public String getServerTopicsDomain () {
127+ return getProperty (CONFIGURATION_SERVER_TOPICS_DOMAIN );
128+ }
129+
130+ public boolean getBooleanProperty (
131+ final String key , final boolean defaultValue ) {
132+ String value = getProperty (key );
133+
134+ if (value != null ) {
135+ if (value .equalsIgnoreCase ("true" )) {
136+ return true ;
137+ }
138+ if (value .equalsIgnoreCase ("false" )) {
139+ return false ;
140+ }
141+ LOGGER .warn ("Invalid boolean property value for " + key + ": " + value );
142+ }
143+
144+ return defaultValue ;
63145 }
64-
65- public Collection <JID > getAdminUsers () {
66- return adminUsers ;
67- }
68-
69- public Collection <JID > getAutosubscribeChannels () {
70- return autosubscribeChannels ;
71- }
72-
73- public static Configuration getInstance ()
74- {
75- if (null == instance ) {
76- instance = new Configuration ();
77- }
78- return instance ;
79- }
80-
81- public String getProperty (String key )
82- {
83- return conf .getProperty (key );
84- }
85-
86- public String getProperty (String key , String defaultValue )
87- {
88- return conf .getProperty (key , defaultValue );
89- }
90-
91- public void load (InputStream inputStream ) throws IOException
92- {
93- conf .load (inputStream );
94- setupCollections ();
95- }
96-
97- private Collection <String > getStringArrayProperty (String key ) {
98- String prop = getProperty (key );
99-
100- if (null == prop ) {
101- return Collections .emptyList ();
102- }
103-
104- return Arrays .asList (prop .split (ARRAY_PROPERTY_SEPARATOR ));
105- }
106-
107- private Collection <JID > getJIDArrayProperty (String key ) {
108- Collection <String > props = getStringArrayProperty (key );
109-
110- Collection <JID > jids = new ArrayList <JID >(props .size ());
111-
112- for (String prop : props ) {
113- jids .add (new JID (prop ));
114- }
115-
116- return jids ;
117- }
118-
119- public String getServerDomain () {
120- return getProperty (CONFIGURATION_SERVER_DOMAIN );
121- }
122-
123- public String getServerChannelsDomain () {
124- return getProperty (CONFIGURATION_SERVER_CHANNELS_DOMAIN );
125- }
126-
127- public String getServerTopicsDomain () {
128- return getProperty (CONFIGURATION_SERVER_TOPICS_DOMAIN );
129- }
130146}
0 commit comments