1 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 45 public final class SmackConfiguration { 46 47 private static final String 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 62 static { 63 try { 64 ClassLoader [] 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 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 e) { 95 e.printStackTrace(); 96 } 97 finally { 98 try { 99 systemStream.close(); 100 } 101 catch (Exception e) { 102 } 103 } 104 } 105 } 106 } 107 catch (Exception e) { 108 e.printStackTrace(); 109 } 110 } 111 112 117 public static String getVersion() { 118 return SMACK_VERSION; 119 } 120 121 127 public static int getPacketReplyTimeout() { 128 if (packetReplyTimeout <= 0) { 130 packetReplyTimeout = 5000; 131 } 132 return packetReplyTimeout; 133 } 134 135 141 public static void setPacketReplyTimeout(int timeout) { 142 if (timeout <= 0) { 143 throw new IllegalArgumentException (); 144 } 145 packetReplyTimeout = timeout; 146 } 147 148 156 public static int getKeepAliveInterval() { 157 return keepAliveInterval; 158 } 159 160 168 public static void setKeepAliveInterval(int interval) { 169 keepAliveInterval = interval; 170 } 171 172 private static void parseClassToLoad(XmlPullParser parser) throws Exception { 173 String className = parser.nextText(); 174 try { 176 Class.forName(className); 177 } 178 catch (ClassNotFoundException 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 186 { 187 try { 188 return Integer.parseInt(parser.nextText()); 189 } 190 catch (NumberFormatException nfe) { 191 nfe.printStackTrace(); 192 return defaultValue; 193 } 194 } 195 196 201 private static ClassLoader [] getClassLoaders() { 202 ClassLoader [] classLoaders = new ClassLoader [2]; 203 classLoaders[0] = new SmackConfiguration().getClass().getClassLoader(); 204 classLoaders[1] = Thread.currentThread().getContextClassLoader(); 205 return classLoaders; 206 } 207 } 208 | Popular Tags |