1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 22:32:06 +01:00

Reworked Smack initialization

Move extension relevant configuration options from SmackConfiguration to
the extension. Introduced disabledSmackClasses that can be configured
via a system property or configuration file.
This commit is contained in:
Florian Schmaus 2014-02-23 17:48:07 +01:00
parent 4121ec2c0e
commit 3093333533
11 changed files with 193 additions and 338 deletions

View file

@ -22,7 +22,9 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -55,16 +57,12 @@ public final class SmackConfiguration {
private static final Logger log = Logger.getLogger(SmackConfiguration.class.getName()); private static final Logger log = Logger.getLogger(SmackConfiguration.class.getName());
private static InputStream configFileStream;
private static int defaultPacketReplyTimeout = 5000; private static int defaultPacketReplyTimeout = 5000;
private static List<String> defaultMechs = new ArrayList<String>();
private static boolean localSocks5ProxyEnabled = true;
private static int localSocks5ProxyPort = 7777;
private static int packetCollectorSize = 5000; private static int packetCollectorSize = 5000;
private static boolean initialized = false; private static List<String> defaultMechs = new ArrayList<String>();
private static Set<String> disabledSmackClasses = new HashSet<String>();
static { static {
String smackVersion; String smackVersion;
@ -78,6 +76,34 @@ public final class SmackConfiguration {
smackVersion = "unkown"; smackVersion = "unkown";
} }
SMACK_VERSION = smackVersion; SMACK_VERSION = smackVersion;
String disabledClasses = System.getProperty("smack.disabledClasses");
if (disabledClasses != null) {
String[] splitDisabledClasses = disabledClasses.split(",");
for (String s : splitDisabledClasses) disabledSmackClasses.add(s);
}
try {
FileUtils.addLines("classpath:org.jivesoftware.smack/disabledClasses", disabledSmackClasses);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
InputStream configFileStream;
try {
configFileStream = FileUtils.getStreamForUrl(DEFAULT_CONFIG_FILE, null);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
try {
processConfigFile(configFileStream, null);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
} }
/** /**
@ -86,11 +112,6 @@ public final class SmackConfiguration {
*/ */
private static ParsingExceptionCallback defaultCallback = new ExceptionThrowingCallback(); private static ParsingExceptionCallback defaultCallback = new ExceptionThrowingCallback();
/**
* This automatically enables EntityCaps for new connections if it is set to true
*/
private static boolean autoEnableEntityCaps = true;
private SmackConfiguration() { private SmackConfiguration() {
} }
@ -102,38 +123,6 @@ public final class SmackConfiguration {
* 2) retrieve and set the current Smack release * 2) retrieve and set the current Smack release
*/ */
/**
* Sets the location of the config file on the classpath. Only required if changing from the default location of <i>classpath:org.jivesoftware.smack/smack-config.xml</i>.
*
* <p>
* This method must be called before accessing any other class in Smack.
*
* @param configFileUrl The location of the config file.
* @param loader The classloader to use if the URL has a protocol of <b>classpath</> and the file is not located on the default classpath.
* This can be set to null to use defaults and is ignored for all other protocols.
* @throws IllegalArgumentException If the config URL is invalid in that it cannot open an {@link InputStream}
*/
public static void setConfigFileUrl(String configFileUrl, ClassLoader loader) {
try {
configFileStream = FileUtils.getStreamForUrl(configFileUrl, loader);
}
catch (Exception e) {
throw new IllegalArgumentException("Failed to create input stream from specified file URL ["+ configFileUrl + "]", e);
}
initialize();
}
/**
* Sets the {@link InputStream} representing the smack configuration file. This can be used to override the default with something that is not on the classpath.
* <p>
* This method must be called before accessing any other class in Smack.
* @param configFile
*/
public static void setConfigFileStream(InputStream configFile) {
configFileStream = configFile;
initialize();
}
/** /**
* Returns the Smack version information, eg "1.3.0". * Returns the Smack version information, eg "1.3.0".
* *
@ -150,8 +139,6 @@ public final class SmackConfiguration {
* @return the milliseconds to wait for a response from the server * @return the milliseconds to wait for a response from the server
*/ */
public static int getDefaultPacketReplyTimeout() { public static int getDefaultPacketReplyTimeout() {
initialize();
// The timeout value must be greater than 0 otherwise we will answer the default value // The timeout value must be greater than 0 otherwise we will answer the default value
if (defaultPacketReplyTimeout <= 0) { if (defaultPacketReplyTimeout <= 0) {
defaultPacketReplyTimeout = 5000; defaultPacketReplyTimeout = 5000;
@ -166,8 +153,6 @@ public final class SmackConfiguration {
* @param timeout the milliseconds to wait for a response from the server * @param timeout the milliseconds to wait for a response from the server
*/ */
public static void setDefaultPacketReplyTimeout(int timeout) { public static void setDefaultPacketReplyTimeout(int timeout) {
initialize();
if (timeout <= 0) { if (timeout <= 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@ -181,7 +166,6 @@ public final class SmackConfiguration {
* @return The number of packets to queue before deleting older packets. * @return The number of packets to queue before deleting older packets.
*/ */
public static int getPacketCollectorSize() { public static int getPacketCollectorSize() {
initialize();
return packetCollectorSize; return packetCollectorSize;
} }
@ -192,7 +176,6 @@ public final class SmackConfiguration {
* @param The number of packets to queue before deleting older packets. * @param The number of packets to queue before deleting older packets.
*/ */
public static void setPacketCollectorSize(int collectorSize) { public static void setPacketCollectorSize(int collectorSize) {
initialize();
packetCollectorSize = collectorSize; packetCollectorSize = collectorSize;
} }
@ -202,8 +185,6 @@ public final class SmackConfiguration {
* @param mech the SASL mechanism to be added * @param mech the SASL mechanism to be added
*/ */
public static void addSaslMech(String mech) { public static void addSaslMech(String mech) {
initialize();
if(! defaultMechs.contains(mech) ) { if(! defaultMechs.contains(mech) ) {
defaultMechs.add(mech); defaultMechs.add(mech);
} }
@ -215,8 +196,6 @@ public final class SmackConfiguration {
* @param mechs the Collection of SASL mechanisms to be added * @param mechs the Collection of SASL mechanisms to be added
*/ */
public static void addSaslMechs(Collection<String> mechs) { public static void addSaslMechs(Collection<String> mechs) {
initialize();
for(String mech : mechs) { for(String mech : mechs) {
addSaslMech(mech); addSaslMech(mech);
} }
@ -228,7 +207,6 @@ public final class SmackConfiguration {
* @param mech the SASL mechanism to be removed * @param mech the SASL mechanism to be removed
*/ */
public static void removeSaslMech(String mech) { public static void removeSaslMech(String mech) {
initialize();
defaultMechs.remove(mech); defaultMechs.remove(mech);
} }
@ -238,7 +216,6 @@ public final class SmackConfiguration {
* @param mechs the Collection of SASL mechanisms to be removed * @param mechs the Collection of SASL mechanisms to be removed
*/ */
public static void removeSaslMechs(Collection<String> mechs) { public static void removeSaslMechs(Collection<String> mechs) {
initialize();
defaultMechs.removeAll(mechs); defaultMechs.removeAll(mechs);
} }
@ -253,66 +230,6 @@ public final class SmackConfiguration {
return Collections.unmodifiableList(defaultMechs); return Collections.unmodifiableList(defaultMechs);
} }
/**
* Returns true if the local Socks5 proxy should be started. Default is true.
*
* @return if the local Socks5 proxy should be started
*/
public static boolean isLocalSocks5ProxyEnabled() {
initialize();
return localSocks5ProxyEnabled;
}
/**
* Sets if the local Socks5 proxy should be started. Default is true.
*
* @param localSocks5ProxyEnabled if the local Socks5 proxy should be started
*/
public static void setLocalSocks5ProxyEnabled(boolean localSocks5ProxyEnabled) {
initialize();
SmackConfiguration.localSocks5ProxyEnabled = localSocks5ProxyEnabled;
}
/**
* Return the port of the local Socks5 proxy. Default is 7777.
*
* @return the port of the local Socks5 proxy
*/
public static int getLocalSocks5ProxyPort() {
initialize();
return localSocks5ProxyPort;
}
/**
* Sets the port of the local Socks5 proxy. Default is 7777. If you set the port to a negative
* value Smack tries the absolute value and all following until it finds an open port.
*
* @param localSocks5ProxyPort the port of the local Socks5 proxy to set
*/
public static void setLocalSocks5ProxyPort(int localSocks5ProxyPort) {
initialize();
SmackConfiguration.localSocks5ProxyPort = localSocks5ProxyPort;
}
/**
* Check if Entity Caps are enabled as default for every new connection
* @return
*/
public static boolean autoEnableEntityCaps() {
initialize();
return autoEnableEntityCaps;
}
/**
* Set if Entity Caps are enabled or disabled for every new connection
*
* @param true if Entity Caps should be auto enabled, false if not
*/
public static void setAutoEnableEntityCaps(boolean b) {
initialize();
autoEnableEntityCaps = b;
}
/** /**
* Set the default parsing exception callback for all newly created connections * Set the default parsing exception callback for all newly created connections
* *
@ -320,7 +237,6 @@ public final class SmackConfiguration {
* @see ParsingExceptionCallback * @see ParsingExceptionCallback
*/ */
public static void setDefaultParsingExceptionCallback(ParsingExceptionCallback callback) { public static void setDefaultParsingExceptionCallback(ParsingExceptionCallback callback) {
initialize();
defaultCallback = callback; defaultCallback = callback;
} }
@ -331,11 +247,35 @@ public final class SmackConfiguration {
* @see ParsingExceptionCallback * @see ParsingExceptionCallback
*/ */
public static ParsingExceptionCallback getDefaultParsingExceptionCallback() { public static ParsingExceptionCallback getDefaultParsingExceptionCallback() {
initialize();
return defaultCallback; return defaultCallback;
} }
public static void parseClassesToLoad(XmlPullParser parser, boolean optional) throws XmlPullParserException, IOException, Exception { public static void processConfigFile(InputStream cfgFileStream, Collection<Exception> exceptions) throws Exception {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(cfgFileStream, "UTF-8");
int eventType = parser.getEventType();
do {
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("startupClasses")) {
parseClassesToLoad(parser, false, exceptions);
}
else if (parser.getName().equals("optionalStartupClasses")) {
parseClassesToLoad(parser, true, exceptions);
}
}
eventType = parser.next();
}
while (eventType != XmlPullParser.END_DOCUMENT);
try {
cfgFileStream.close();
}
catch (IOException e) {
log.log(Level.SEVERE, "Error while closing config file input stream", e);
}
}
private static void parseClassesToLoad(XmlPullParser parser, boolean optional, Collection<Exception> exceptions) throws XmlPullParserException, IOException, Exception {
final String startName = parser.getName(); final String startName = parser.getName();
int eventType; int eventType;
String name; String name;
@ -343,13 +283,22 @@ public final class SmackConfiguration {
eventType = parser.next(); eventType = parser.next();
name = parser.getName(); name = parser.getName();
if (eventType == XmlPullParser.START_TAG && "className".equals(name)) { if (eventType == XmlPullParser.START_TAG && "className".equals(name)) {
if (disabledSmackClasses.contains(name)) {
log.info("Not loading disabled Smack class " + name);
}
else {
String classToLoad = parser.nextText(); String classToLoad = parser.nextText();
try {
loadSmackClass(classToLoad, optional); loadSmackClass(classToLoad, optional);
} catch (Exception e) {
exceptions.add(e);
}
}
} }
} while (! (eventType == XmlPullParser.END_TAG && startName.equals(name))); } while (! (eventType == XmlPullParser.END_TAG && startName.equals(name)));
} }
public static void loadSmackClass(String className, boolean optional) throws Exception { private static void loadSmackClass(String className, boolean optional) throws Exception {
// Attempt to load the class so that the class can get initialized // Attempt to load the class so that the class can get initialized
try { try {
Class<?> initClass = Class.forName(className); Class<?> initClass = Class.forName(className);
@ -373,101 +322,4 @@ public final class SmackConfiguration {
throw cnfe; throw cnfe;
} }
} }
private static int parseIntProperty(XmlPullParser parser, int defaultValue)
throws Exception
{
try {
return Integer.parseInt(parser.nextText());
}
catch (NumberFormatException nfe) {
log.log(Level.SEVERE, "Could not parse integer", nfe);
return defaultValue;
}
}
/*
* Order of precedence for config file is VM arg, setConfigXXX methods and embedded default file location.
*/
private static void initialize() {
if (initialized) {
return;
}
initialized = true;
String configFileLocation = System.getProperty("smack.config.file");
if (configFileLocation != null) {
try {
configFileStream = FileUtils.getStreamForUrl(configFileLocation, null);
}
catch (Exception e) {
log.log(Level.SEVERE, "Error creating input stream for config file [" + configFileLocation + "] from VM argument", e);
}
}
if (configFileStream == null) {
try {
configFileStream = FileUtils.getStreamForUrl(DEFAULT_CONFIG_FILE, null);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
if (configFileStream != null) {
try {
readFile(configFileStream);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
else {
log.log(Level.INFO, "No configuration file found");
}
}
private static void readFile(InputStream cfgFileStream) throws Exception {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(cfgFileStream, "UTF-8");
int eventType = parser.getEventType();
do {
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("startupClasses")) {
parseClassesToLoad(parser, false);
}
else if (parser.getName().equals("optionalStartupClasses")) {
parseClassesToLoad(parser, true);
}
else if (parser.getName().equals("defaultPacketReplyTimeout")) {
defaultPacketReplyTimeout = parseIntProperty(parser, defaultPacketReplyTimeout);
}
else if (parser.getName().equals("mechName")) {
defaultMechs.add(parser.nextText());
}
else if (parser.getName().equals("localSocks5ProxyEnabled")) {
localSocks5ProxyEnabled = Boolean.parseBoolean(parser.nextText());
}
else if (parser.getName().equals("localSocks5ProxyPort")) {
localSocks5ProxyPort = parseIntProperty(parser, localSocks5ProxyPort);
}
else if (parser.getName().equals("packetCollectorSize")) {
packetCollectorSize = parseIntProperty(parser, packetCollectorSize);
}
else if (parser.getName().equals("autoEnableEntityCaps")) {
autoEnableEntityCaps = Boolean.parseBoolean(parser.nextText());
}
}
eventType = parser.next();
}
while (eventType != XmlPullParser.END_DOCUMENT);
try {
cfgFileStream.close();
}
catch (IOException e) {
log.log(Level.SEVERE, "Error while closing config file input stream", e);
}
}
} }

View file

@ -16,18 +16,18 @@
*/ */
package org.jivesoftware.smack.util; package org.jivesoftware.smack.util;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
public final class FileUtils { public final class FileUtils {
private FileUtils() {
}
public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException { public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException {
URI fileUri = URI.create(url); URI fileUri = URI.create(url);
@ -72,4 +72,14 @@ public final class FileUtils {
return loaders.toArray(new ClassLoader[loaders.size()]); return loaders.toArray(new ClassLoader[loaders.size()]);
} }
public static boolean addLines(String url, Set<String> set) throws MalformedURLException, IOException {
InputStream is = getStreamForUrl(url, null);
if (is == null) return false;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
set.add(line);
}
return true;
}
} }

View file

@ -1,22 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<!-- Smack configuration file. --> <!-- Smack configuration file. -->
<smack> <smack>
<!-- Default Packet reply timeout in milliseconds -->
<defaultPacketReplyTimeout>5000</defaultPacketReplyTimeout>
<!-- Enable/Disable local Socks5 proxy -->
<localSocks5ProxyEnabled>true</localSocks5ProxyEnabled>
<!-- Port of the local Socks5 proxy -->
<localSocks5ProxyPort>7777</localSocks5ProxyPort>
<!-- Port of the local Socks5 proxy -->
<packetCollectorSize>10000</packetCollectorSize>
<!-- Automatic enable Entity Caps (XEP-0115) for new connections -->
<autoEnableEntityCaps>false</autoEnableEntityCaps>
<!-- Classes that will be loaded when Smack starts --> <!-- Classes that will be loaded when Smack starts -->
<startupClasses> <startupClasses>
<className>org.jivesoftware.smack.initializer.VmArgInitializer</className> <className>org.jivesoftware.smack.initializer.VmArgInitializer</className>

View file

@ -29,12 +29,25 @@ to be as small as possible. The library ships as several JAR files to provide mo
over which features applications require: over which features applications require:
<ul> <ul>
<li><tt>smack.jar</tt> -- provides core XMPP functionality and is the only <b>required</b> <li><tt>smack-core.jar</tt> -- provides core XMPP functionality and is the only <b>required</b>
library. All XMPP features that are part of the XMPP RFCs are included.</li> library. All XMPP features that are part of the XMPP RFCs are included.</li>
<li><tt>smackx.jar</tt> -- support for many of the the extensions (XEPs) defined <li><tt>smack-extensions.jar</tt> -- support for many of the extensions (XEPs) defined
by the XMPP Standards Foundation, including multi-user chat, file transfer, user search, etc. by the XMPP Standards Foundation, including multi-user chat, file transfer, user search, etc.
The extensions are documented in the <a href="extensions/index.html">extensions manual</a>.</li> The extensions are documented in the <a href="extensions/index.html">extensions manual</a>.</li>
<li><tt>smackx-debug.jar</tt> -- an enhanced GUI debugger for protocol traffic. It will <li><tt>smack-experimental.jar</tt> -- support for experimental extensions (XEPs) defined
by the XMPP Standards Foundation. The API and functionality of those extensions should be
considered as unstable.</li>
<li><tt>smack-legacy.jar</tt> -- support for legacy extensions (XEPs) defined
by the XMPP Standards Foundation.</li>
<li><tt>smack-bosh.jar</tt> -- support for BOSH (XEP-0124). This code should be considered
as beta.</li>
<li><tt>smack-jingle.jar</tt> -- support for Jingle. This code is old and currenlty
unmaintained.</li>
<li><tt>smack-resolver-dnsjava.jar</tt> -- support for resolving DNS SRV records with the
help of dnsjava. Ideal for platforms that do not support the javax.naming API.</li>
<li><tt>smack-resolver-javax.jar</tt> -- support for resolving DNS SRV records with the
javax namespace API.</li>
<li><tt>smack-debug.jar</tt> -- an enhanced GUI debugger for protocol traffic. It will
automatically be used when found in the classpath and when <a href="debugging.html">debugging</a> automatically be used when found in the classpath and when <a href="debugging.html">debugging</a>
is enabled.</li> is enabled.</li>
</ul> </ul>
@ -51,25 +64,13 @@ If it does not extend this interface, then initialization will have to take plac
which is automatically executed when the class is loaded. which is automatically executed when the class is loaded.
</ul> </ul>
<p> <p>
Initialization is accomplished via a configuration file. By default, Smack will load the one embedded in Initialization is accomplished via a configuration file. By default,
the Smack jar at <i>META-INF/smack-config.xml</i>. This particular configuration contains all the default Smack will load the one embedded in the Smack jar
property values as well as a list of initializer classes to load. All manager type classes are contained at <i>org.jivesoftware.smack/smack-config.xml</i>. This particular
in this list of initializers. If your application does not use all the features provided by Smack via the configuration contains a list of initializer classes to load. All
aforementioned managers, you may want to 'turn them off' by providing a custom config file that does not manager type classes that need to be initialized are contained in this
include that feature. list of initializers.
<p> </p>
If you want to change the configuration file used, you have two options:
<ul>
<li>Programmatically - Call the <i>setConfigFileUrl</i> method of <b>SmackConfiguration</b> with the location
of a new config file.
<pre>SmackConfiguration.setConfigFileUrl("classpath:test/smack-config.xml", null)</pre>
<li>VM Argument - Set the VM argument <i>smack.config.file</i> to the location of a new config file.
<pre>-Dsmack.config.file=file:///c:/com/myco/provider/myco_custom_config.xml</pre>
</ul>
<p>
Please note, there is a copy of the <b>smack-config.xml</b> in the <i>samples</i> directory of the deployment
archive file (zip or tar).
<p class="subheader"> <p class="subheader">
Establishing a Connection Establishing a Connection

View file

@ -24,8 +24,6 @@ import java.util.List;
import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SmackInitializer; import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.FileUtils;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
public class ExtensionsStartupClasses implements SmackInitializer { public class ExtensionsStartupClasses implements SmackInitializer {
@ -37,36 +35,9 @@ public class ExtensionsStartupClasses implements SmackInitializer {
@Override @Override
public void initialize() { public void initialize() {
InputStream is; InputStream is;
XmlPullParser parser;
int eventType;
try { try {
is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null); is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null);
parser = XmlPullParserFactory.newInstance().newPullParser(); SmackConfiguration.processConfigFile(is, exceptions);;
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(is, "UTF-8");
eventType = parser.getEventType();
}
catch (Exception e) {
exceptions.add(e);
return;
}
try {
do {
String name = parser.getName();
if (eventType == XmlPullParser.START_TAG) {
if ("startupClasses".equals(name)) {
try {
SmackConfiguration.parseClassesToLoad(parser, false);
}
catch (Exception e) {
exceptions.add(e);
}
}
}
eventType = parser.next();
}
while (eventType != XmlPullParser.END_DOCUMENT);
is.close();
} }
catch (Exception e) { catch (Exception e) {
exceptions.add(e); exceptions.add(e);

View file

@ -77,6 +77,9 @@ public class Socks5Proxy {
/* SOCKS5 proxy singleton */ /* SOCKS5 proxy singleton */
private static Socks5Proxy socks5Server; private static Socks5Proxy socks5Server;
private static boolean localSocks5ProxyEnabled = true;
private static int localSocks5ProxyPort = 7777;
/* reusable implementation of a SOCKS5 proxy server process */ /* reusable implementation of a SOCKS5 proxy server process */
private Socks5ServerProcess serverProcess; private Socks5ServerProcess serverProcess;
@ -110,6 +113,43 @@ public class Socks5Proxy {
} }
/**
* Returns true if the local Socks5 proxy should be started. Default is true.
*
* @return if the local Socks5 proxy should be started
*/
public static boolean isLocalSocks5ProxyEnabled() {
return localSocks5ProxyEnabled;
}
/**
* Sets if the local Socks5 proxy should be started. Default is true.
*
* @param localSocks5ProxyEnabled if the local Socks5 proxy should be started
*/
public static void setLocalSocks5ProxyEnabled(boolean localSocks5ProxyEnabled) {
Socks5Proxy.localSocks5ProxyEnabled = localSocks5ProxyEnabled;
}
/**
* Return the port of the local Socks5 proxy. Default is 7777.
*
* @return the port of the local Socks5 proxy
*/
public static int getLocalSocks5ProxyPort() {
return localSocks5ProxyPort;
}
/**
* Sets the port of the local Socks5 proxy. Default is 7777. If you set the port to a negative
* value Smack tries the absolute value and all following until it finds an open port.
*
* @param localSocks5ProxyPort the port of the local Socks5 proxy to set
*/
public static void setLocalSocks5ProxyPort(int localSocks5ProxyPort) {
Socks5Proxy.localSocks5ProxyPort = localSocks5ProxyPort;
}
/** /**
* Returns the local SOCKS5 proxy server. * Returns the local SOCKS5 proxy server.
* *
@ -119,7 +159,7 @@ public class Socks5Proxy {
if (socks5Server == null) { if (socks5Server == null) {
socks5Server = new Socks5Proxy(); socks5Server = new Socks5Proxy();
} }
if (SmackConfiguration.isLocalSocks5ProxyEnabled()) { if (isLocalSocks5ProxyEnabled()) {
socks5Server.start(); socks5Server.start();
} }
return socks5Server; return socks5Server;
@ -133,8 +173,8 @@ public class Socks5Proxy {
return; return;
} }
try { try {
if (SmackConfiguration.getLocalSocks5ProxyPort() < 0) { if (getLocalSocks5ProxyPort() < 0) {
int port = Math.abs(SmackConfiguration.getLocalSocks5ProxyPort()); int port = Math.abs(getLocalSocks5ProxyPort());
for (int i = 0; i < 65535 - port; i++) { for (int i = 0; i < 65535 - port; i++) {
try { try {
this.serverSocket = new ServerSocket(port + i); this.serverSocket = new ServerSocket(port + i);
@ -146,7 +186,7 @@ public class Socks5Proxy {
} }
} }
else { else {
this.serverSocket = new ServerSocket(SmackConfiguration.getLocalSocks5ProxyPort()); this.serverSocket = new ServerSocket(getLocalSocks5ProxyPort());
} }
if (this.serverSocket != null) { if (this.serverSocket != null) {
@ -156,7 +196,7 @@ public class Socks5Proxy {
} }
catch (IOException e) { catch (IOException e) {
// couldn't setup server // couldn't setup server
log.log(Level.SEVERE, "couldn't setup local SOCKS5 proxy on port " + SmackConfiguration.getLocalSocks5ProxyPort(), e); log.log(Level.SEVERE, "couldn't setup local SOCKS5 proxy on port " + getLocalSocks5ProxyPort(), e);
} }
} }

View file

@ -21,7 +21,6 @@ import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketInterceptor; import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
@ -78,6 +77,8 @@ public class EntityCapsManager {
protected static EntityCapsPersistentCache persistentCache; protected static EntityCapsPersistentCache persistentCache;
private static boolean autoEnableEntityCaps = true;
private static Map<Connection, EntityCapsManager> instances = Collections private static Map<Connection, EntityCapsManager> instances = Collections
.synchronizedMap(new WeakHashMap<Connection, EntityCapsManager>()); .synchronizedMap(new WeakHashMap<Connection, EntityCapsManager>());
@ -249,7 +250,7 @@ public class EntityCapsManager {
// This calculates the local entity caps version // This calculates the local entity caps version
updateLocalEntityCaps(); updateLocalEntityCaps();
if (SmackConfiguration.autoEnableEntityCaps()) if (autoEnableEntityCaps)
enableEntityCaps(); enableEntityCaps();
PacketFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class), new PacketExtensionFilter( PacketFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class), new PacketExtensionFilter(

View file

@ -25,7 +25,6 @@ import java.io.OutputStream;
import java.net.ConnectException; import java.net.ConnectException;
import org.jivesoftware.smack.Connection; import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
@ -169,7 +168,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfNoSocks5ProxyFound1() { public void shouldFailIfNoSocks5ProxyFound1() {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -220,7 +219,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfNoSocks5ProxyFound2() { public void shouldFailIfNoSocks5ProxyFound2() {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -284,7 +283,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldBlacklistNonSocks5Proxies() { public void shouldBlacklistNonSocks5Proxies() {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -375,7 +374,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfTargetDoesNotAcceptSocks5Bytestream() { public void shouldFailIfTargetDoesNotAcceptSocks5Bytestream() {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -465,7 +464,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfTargetUsesInvalidSocks5Proxy() { public void shouldFailIfTargetUsesInvalidSocks5Proxy() {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -547,7 +546,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfInitiatorCannotConnectToSocks5Proxy() { public void shouldFailIfInitiatorCannotConnectToSocks5Proxy() {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -641,7 +640,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldNegotiateSocks5BytestreamAndTransferData() throws Exception { public void shouldNegotiateSocks5BytestreamAndTransferData() throws Exception {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -754,8 +753,8 @@ public class Socks5ByteStreamManagerTest {
public void shouldUseMultipleAddressesForLocalSocks5Proxy() throws Exception { public void shouldUseMultipleAddressesForLocalSocks5Proxy() throws Exception {
// enable clients local SOCKS5 proxy on port 7778 // enable clients local SOCKS5 proxy on port 7778
SmackConfiguration.setLocalSocks5ProxyEnabled(true); Socks5Proxy.setLocalSocks5ProxyEnabled(true);
SmackConfiguration.setLocalSocks5ProxyPort(7778); Socks5Proxy.setLocalSocks5ProxyPort(7778);
// start a local SOCKS5 proxy // start a local SOCKS5 proxy
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
@ -837,7 +836,7 @@ public class Socks5ByteStreamManagerTest {
// reset proxy settings // reset proxy settings
socks5Proxy.stop(); socks5Proxy.stop();
socks5Proxy.removeLocalAddress("localAddress"); socks5Proxy.removeLocalAddress("localAddress");
SmackConfiguration.setLocalSocks5ProxyPort(7777); Socks5Proxy.setLocalSocks5ProxyPort(7777);
} }
@ -852,7 +851,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldPrioritizeSecondSocks5ProxyOnSecondAttempt() throws Exception { public void shouldPrioritizeSecondSocks5ProxyOnSecondAttempt() throws Exception {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -935,7 +934,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled() throws Exception { public void shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled() throws Exception {
// disable clients local SOCKS5 proxy // disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection // get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -1093,7 +1092,7 @@ public class Socks5ByteStreamManagerTest {
@After @After
public void cleanUp() { public void cleanUp() {
Socks5TestProxy.stopProxy(); Socks5TestProxy.stopProxy();
SmackConfiguration.setLocalSocks5ProxyEnabled(true); Socks5Proxy.setLocalSocks5ProxyEnabled(true);
} }
} }

View file

@ -24,7 +24,6 @@ import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import org.jivesoftware.smack.Connection; import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
@ -427,7 +426,7 @@ public class Socks5ByteStreamRequestTest {
@After @After
public void cleanUp() { public void cleanUp() {
Socks5TestProxy.stopProxy(); Socks5TestProxy.stopProxy();
SmackConfiguration.setLocalSocks5ProxyEnabled(true); Socks5Proxy.setLocalSocks5ProxyEnabled(true);
} }
} }

View file

@ -23,7 +23,6 @@ import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import org.jivesoftware.smack.Connection; import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
@ -87,7 +86,7 @@ public class Socks5ClientForInitiatorTest {
public void shouldFailIfTargetIsNotConnectedToLocalSocks5Proxy() throws Exception { public void shouldFailIfTargetIsNotConnectedToLocalSocks5Proxy() throws Exception {
// start a local SOCKS5 proxy // start a local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyPort(proxyPort); Socks5Proxy.setLocalSocks5ProxyPort(proxyPort);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy.start(); socks5Proxy.start();
@ -125,7 +124,7 @@ public class Socks5ClientForInitiatorTest {
public void shouldSuccessfullyConnectThroughLocalSocks5Proxy() throws Exception { public void shouldSuccessfullyConnectThroughLocalSocks5Proxy() throws Exception {
// start a local SOCKS5 proxy // start a local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyPort(proxyPort); Socks5Proxy.setLocalSocks5ProxyPort(proxyPort);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy.start(); socks5Proxy.start();
@ -308,7 +307,7 @@ public class Socks5ClientForInitiatorTest {
*/ */
@After @After
public void cleanup() { public void cleanup() {
SmackConfiguration.setLocalSocks5ProxyPort(7777); Socks5Proxy.setLocalSocks5ProxyPort(7777);
} }
} }

View file

@ -32,7 +32,6 @@ import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.SmackConfiguration;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
@ -48,7 +47,7 @@ public class Socks5ProxyTest {
*/ */
@Test @Test
public void shouldBeASingleton() { public void shouldBeASingleton() {
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy1 = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy1 = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy2 = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy2 = Socks5Proxy.getSocks5Proxy();
@ -63,7 +62,7 @@ public class Socks5ProxyTest {
*/ */
@Test @Test
public void shouldNotBeRunningIfDisabled() { public void shouldNotBeRunningIfDisabled() {
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
assertFalse(proxy.isRunning()); assertFalse(proxy.isRunning());
} }
@ -75,12 +74,12 @@ public class Socks5ProxyTest {
*/ */
@Test @Test
public void shouldUseFreePortOnNegativeValues() throws Exception { public void shouldUseFreePortOnNegativeValues() throws Exception {
SmackConfiguration.setLocalSocks5ProxyEnabled(false); Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
assertFalse(proxy.isRunning()); assertFalse(proxy.isRunning());
ServerSocket serverSocket = new ServerSocket(0); ServerSocket serverSocket = new ServerSocket(0);
SmackConfiguration.setLocalSocks5ProxyPort(-serverSocket.getLocalPort()); Socks5Proxy.setLocalSocks5ProxyPort(-serverSocket.getLocalPort());
proxy.start(); proxy.start();
@ -155,7 +154,7 @@ public class Socks5ProxyTest {
public void shouldOnlyStartOneServerThread() { public void shouldOnlyStartOneServerThread() {
int threadCount = Thread.activeCount(); int threadCount = Thread.activeCount();
SmackConfiguration.setLocalSocks5ProxyPort(7890); Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start(); proxy.start();
@ -189,7 +188,7 @@ public class Socks5ProxyTest {
*/ */
@Test @Test
public void shouldCloseSocketIfNoSocks5Request() throws Exception { public void shouldCloseSocketIfNoSocks5Request() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890); Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start(); proxy.start();
@ -219,7 +218,7 @@ public class Socks5ProxyTest {
*/ */
@Test @Test
public void shouldRespondWithErrorIfNoSupportedAuthenticationMethod() throws Exception { public void shouldRespondWithErrorIfNoSupportedAuthenticationMethod() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890); Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start(); proxy.start();
@ -249,7 +248,7 @@ public class Socks5ProxyTest {
*/ */
@Test @Test
public void shouldRespondWithErrorIfConnectionIsNotAllowed() throws Exception { public void shouldRespondWithErrorIfConnectionIsNotAllowed() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890); Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start(); proxy.start();
@ -290,7 +289,7 @@ public class Socks5ProxyTest {
*/ */
@Test @Test
public void shouldSuccessfullyEstablishConnection() throws Exception { public void shouldSuccessfullyEstablishConnection() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890); Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start(); proxy.start();
@ -355,8 +354,8 @@ public class Socks5ProxyTest {
*/ */
@After @After
public void cleanup() { public void cleanup() {
SmackConfiguration.setLocalSocks5ProxyEnabled(true); Socks5Proxy.setLocalSocks5ProxyEnabled(true);
SmackConfiguration.setLocalSocks5ProxyPort(7777); Socks5Proxy.setLocalSocks5ProxyPort(7777);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
try { try {
String address = InetAddress.getLocalHost().getHostAddress(); String address = InetAddress.getLocalHost().getHostAddress();