1111import java .util .Properties ;
1212
1313import org .apache .log4j .Logger ;
14+ import org .buddycloud .channelserver .channel .LocalDomainChecker ;
1415import org .xmpp .packet .JID ;
1516
1617public class Configuration extends Properties {
@@ -19,12 +20,13 @@ public class Configuration extends Properties {
1920 private static final long serialVersionUID = 1L ;
2021
2122 private static final String ARRAY_PROPERTY_SEPARATOR = ";" ;
23+ private static final String INVALID_NODE = "Illegal node format" ;
2224
2325 public static final String CONFIGURATION_SERVER_DOMAIN = "server.domain" ;
2426 public static final String CONFIGURATION_SERVER_CHANNELS_DOMAIN = "server.domain.channels" ;
2527 public static final String CONFIGURATION_SERVER_TOPICS_DOMAIN = "server.domain.topics" ;
2628 public static final String CONFIGURATION_LOCAL_DOMAIN_CHECKER = "server.domain.checker" ;
27-
29+
2830 public static final String CONFIGURATION_ADMIN_USERS = "users.admin" ;
2931
3032 public static final String CONFIGURATION_CHANNELS_AUTOSUBSCRIBE = "channels.autosubscribe" ;
@@ -33,17 +35,17 @@ public class Configuration extends Properties {
3335 public static final String CONFIGURATION_CHANNELS_DEFAULT_ACCESSMODEL = "channel.configuration.default.accessmodel" ;
3436 public static final String CONFIGURATION_CHANNELS_DEFAULT_DESCRIPTION = "channel.configuration.default.description" ;
3537 public static final String CONFIGURATION_CHANNELS_DEFAULT_TITLE = "channel.configuration.default.title" ;
36-
38+
3739 public static final String DISCOVERY_USE_DNS = "discovery.dns.enabled" ;
3840
3941 public static final String PERSIST_PRESENCE_DATA = "users.presence.persist" ;
40-
42+
4143 public static final String NOTIFICATIONS_SENDTO = "notifications.sendTo" ;
4244 public static final String NOTIFICATIONS_CONNECTED = "notifications.connected" ;
43-
45+
4446 private static final String CONFIGURATION_FILE = "configuration.properties" ;
4547
46- public static final String PURGE_REMOTE_ON_START = "sync.purge-on-start" ;
48+ public static final String PURGE_REMOTE_ON_START = "sync.purge-on-start" ;
4749
4850 public static final String XMPP_PORT = "xmpp.port" ;
4951
@@ -59,21 +61,23 @@ public class Configuration extends Properties {
5961 private Configuration () {
6062 try {
6163 conf = new Properties ();
62- InputStream confFile = this .getClass ().getClassLoader ().getResourceAsStream (CONFIGURATION_FILE );
64+ InputStream confFile = this .getClass ().getClassLoader ()
65+ .getResourceAsStream (CONFIGURATION_FILE );
6366 if (confFile != null ) {
6467 load (confFile );
6568 LOGGER .info ("Loaded " + CONFIGURATION_FILE + " from classpath." );
6669 } else {
6770 File f = new File (CONFIGURATION_FILE );
6871 load (new FileInputStream (f ));
69- LOGGER .info ("Loaded " + CONFIGURATION_FILE + " from working directory." );
72+ LOGGER .info ("Loaded " + CONFIGURATION_FILE
73+ + " from working directory." );
7074 }
7175 } catch (Exception e ) {
7276 LOGGER .error ("Could not load " + CONFIGURATION_FILE + "!" );
7377 System .exit (1 );
7478 }
7579 }
76-
80+
7781 private void setupCollections () {
7882 adminUsers = getJIDArrayProperty (CONFIGURATION_ADMIN_USERS );
7983 autosubscribeChannels = getJIDArrayProperty (CONFIGURATION_CHANNELS_AUTOSUBSCRIBE );
@@ -93,18 +97,31 @@ public static Configuration getInstance() {
9397 }
9498 return instance ;
9599 }
100+
101+ public static void reset () {
102+ instance = null ;
103+ }
96104
97105 public String getProperty (String key ) {
98106 return conf .getProperty (key );
99107 }
100-
108+
109+ @ Override
110+ public synchronized Object remove (Object key ) {
111+ return conf .remove (key );
112+ }
113+
101114 public void clear () {
102115 conf .clear ();
103116 }
104117
105118 public String getProperty (String key , String defaultValue ) {
106119 return conf .getProperty (key , defaultValue );
107120 }
121+
122+ public void putProperty (String key , String value ) {
123+ conf .put (key , value );
124+ }
108125
109126 public void load (InputStream inputStream ) throws IOException {
110127 conf .load (inputStream );
@@ -122,7 +139,6 @@ private Collection<String> getStringArrayProperty(String key) {
122139 }
123140
124141 private Collection <JID > getJIDArrayProperty (String key ) {
125- System .out .println (conf .getProperty (CONFIGURATION_CHANNELS_AUTOSUBSCRIBE ));
126142 Collection <String > props = getStringArrayProperty (key );
127143
128144 Collection <JID > jids = new ArrayList <JID >(props .size ());
@@ -137,7 +153,7 @@ private Collection<JID> getJIDArrayProperty(String key) {
137153
138154 return jids ;
139155 }
140-
156+
141157 public ArrayList <JID > getNotificationsList (String event ) {
142158 ArrayList <JID > notify = new ArrayList <JID >();
143159 if (!getBooleanProperty (event , false )) {
@@ -168,7 +184,8 @@ public String getServerTopicsDomain() {
168184 return getProperty (CONFIGURATION_SERVER_TOPICS_DOMAIN );
169185 }
170186
171- public boolean getBooleanProperty (final String key , final boolean defaultValue ) {
187+ public boolean getBooleanProperty (final String key ,
188+ final boolean defaultValue ) {
172189 String value = getProperty (key );
173190
174191 if (value != null ) {
@@ -178,17 +195,36 @@ public boolean getBooleanProperty(final String key, final boolean defaultValue)
178195 if (value .equalsIgnoreCase ("false" )) {
179196 return false ;
180197 }
181- LOGGER .warn ("Invalid boolean property value for " + key + ": " + value );
198+ LOGGER .warn ("Invalid boolean property value for " + key + ": "
199+ + value );
182200 }
183201
184202 return defaultValue ;
185203 }
186204
187- public String getComponentPort () {
205+ public String getComponentPort () {
188206 return this .getProperty (XMPP_PORT , "5347" );
189207 }
190208
191209 public String getXmppHost () {
192210 return this .getProperty (XMPP_HOST , "127.0.0.1" );
193211 }
212+
213+ public boolean isLocalDomain (String domain ) {
214+ return LocalDomainChecker .isLocal (domain , this );
215+ }
216+
217+ public boolean isLocalNode (String nodeId ) {
218+ if (false == nodeId .matches ("/user/.+@.+/.+" )) {
219+ LOGGER .debug ("Node " + nodeId + " has an invalid format" );
220+ throw new IllegalArgumentException (INVALID_NODE );
221+ }
222+ String domain = new JID (nodeId .split ("/" )[2 ]).getDomain ();
223+ return isLocalDomain (domain );
224+ }
225+
226+ public boolean isLocalJID (JID jid ) {
227+ String domain = jid .getDomain ();
228+ return isLocalDomain (domain );
229+ }
194230}
0 commit comments