KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > SmackConfiguration


1 /**
2  * $RCSfile$
3  * $Revision: 2633 $
4  * $Date: 2005-08-12 16:23:23 -0300 (Fri, 12 Aug 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack;
22
23 import java.io.*;
24 import java.net.*;
25 import java.util.*;
26
27 import org.xmlpull.v1.*;
28 import org.xmlpull.mxp1.MXParser;
29
30 /**
31  * Represents the configuration of Smack. The configuration is used for:
32  * <ul>
33  * <li> Initializing classes by loading them at start-up.
34  * <li> Getting the current Smack version.
35  * <li> Getting and setting global library behavior, such as the period of time
36  * to wait for replies to packets from the server. Note: setting these values
37  * via the API will override settings in the configuration file.
38  * </ul>
39  *
40  * Configuration settings are stored in META-INF/smack-config.xml (typically inside the
41  * smack.jar file).
42  *
43  * @author Gaston Dombiak
44  */

45 public final class SmackConfiguration {
46
47     private static final String JavaDoc SMACK_VERSION = "2.0.0";
48
49     private static int packetReplyTimeout = 5000;
50     private static int keepAliveInterval = 30000;
51
52     private SmackConfiguration() {
53     }
54
55     /**
56      * Loads the configuration from the smack-config.xml file.<p>
57      *
58      * So far this means that:
59      * 1) a set of classes will be loaded in order to execute their static init block
60      * 2) retrieve and set the current Smack release
61      */

62     static {
63         try {
64             // Get an array of class loaders to try loading the providers files from.
65
ClassLoader JavaDoc[] classLoaders = getClassLoaders();
66             for (int i = 0; i < classLoaders.length; i++) {
67                 Enumeration configEnum = classLoaders[i].getResources("META-INF/smack-config.xml");
68                 while (configEnum.hasMoreElements()) {
69                     URL url = (URL) configEnum.nextElement();
70                     InputStream systemStream = null;
71                     try {
72                         systemStream = url.openStream();
73                         XmlPullParser parser = new MXParser();
74                         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
75                         parser.setInput(systemStream, "UTF-8");
76                         int eventType = parser.getEventType();
77                         do {
78                             if (eventType == XmlPullParser.START_TAG) {
79                                 if (parser.getName().equals("className")) {
80                                     // Attempt to load the class so that the class can get initialized
81
parseClassToLoad(parser);
82                                 }
83                                 else if (parser.getName().equals("packetReplyTimeout")) {
84                                     packetReplyTimeout = parseIntProperty(parser, packetReplyTimeout);
85                                 }
86                                 else if (parser.getName().equals("keepAliveInterval")) {
87                                     keepAliveInterval = parseIntProperty(parser, keepAliveInterval);
88                                 }
89                             }
90                             eventType = parser.next();
91                         }
92                         while (eventType != XmlPullParser.END_DOCUMENT);
93                     }
94                     catch (Exception JavaDoc e) {
95                         e.printStackTrace();
96                     }
97                     finally {
98                         try {
99                             systemStream.close();
100                         }
101                         catch (Exception JavaDoc e) {
102                         }
103                     }
104                 }
105             }
106         }
107         catch (Exception JavaDoc e) {
108             e.printStackTrace();
109         }
110     }
111
112     /**
113      * Returns the Smack version information, eg "1.3.0".
114      *
115      * @return the Smack version information.
116      */

117     public static String JavaDoc getVersion() {
118         return SMACK_VERSION;
119     }
120
121     /**
122      * Returns the number of milliseconds to wait for a response from
123      * the server. The default value is 5000 ms.
124      *
125      * @return the milliseconds to wait for a response from the server
126      */

127     public static int getPacketReplyTimeout() {
128         // The timeout value must be greater than 0 otherwise we will answer the default value
129
if (packetReplyTimeout <= 0) {
130             packetReplyTimeout = 5000;
131         }
132         return packetReplyTimeout;
133     }
134
135     /**
136      * Sets the number of milliseconds to wait for a response from
137      * the server.
138      *
139      * @param timeout the milliseconds to wait for a response from the server
140      */

141     public static void setPacketReplyTimeout(int timeout) {
142         if (timeout <= 0) {
143             throw new IllegalArgumentException JavaDoc();
144         }
145         packetReplyTimeout = timeout;
146     }
147
148     /**
149      * Returns the number of milleseconds delay between sending keep-alive
150      * requests to the server. The default value is 30000 ms. A value of -1
151      * mean no keep-alive requests will be sent to the server.
152      *
153      * @return the milliseconds to wait between keep-alive requests, or -1 if
154      * no keep-alive should be sent.
155      */

156     public static int getKeepAliveInterval() {
157         return keepAliveInterval;
158     }
159
160     /**
161      * Sets the number of milleseconds delay between sending keep-alive
162      * requests to the server. The default value is 30000 ms. A value of -1
163      * mean no keep-alive requests will be sent to the server.
164      *
165      * @param interval the milliseconds to wait between keep-alive requests,
166      * or -1 if no keep-alive should be sent.
167      */

168     public static void setKeepAliveInterval(int interval) {
169         keepAliveInterval = interval;
170     }
171
172     private static void parseClassToLoad(XmlPullParser parser) throws Exception JavaDoc {
173         String JavaDoc className = parser.nextText();
174         // Attempt to load the class so that the class can get initialized
175
try {
176             Class.forName(className);
177         }
178         catch (ClassNotFoundException JavaDoc cnfe) {
179             System.err.println("Error! A startup class specified in smack-config.xml could " +
180                     "not be loaded: " + className);
181         }
182     }
183
184     private static int parseIntProperty(XmlPullParser parser, int defaultValue)
185             throws Exception JavaDoc
186     {
187         try {
188             return Integer.parseInt(parser.nextText());
189         }
190         catch (NumberFormatException JavaDoc nfe) {
191             nfe.printStackTrace();
192             return defaultValue;
193         }
194     }
195
196     /**
197      * Returns an array of class loaders to load resources from.
198      *
199      * @return an array of ClassLoader instances.
200      */

201     private static ClassLoader JavaDoc[] getClassLoaders() {
202         ClassLoader JavaDoc[] classLoaders = new ClassLoader JavaDoc[2];
203         classLoaders[0] = new SmackConfiguration().getClass().getClassLoader();
204         classLoaders[1] = Thread.currentThread().getContextClassLoader();
205         return classLoaders;
206     }
207 }
208
Popular Tags