1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02: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

@ -24,8 +24,6 @@ import java.util.List;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.util.FileUtils;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
public class ExtensionsStartupClasses implements SmackInitializer {
@ -37,36 +35,9 @@ public class ExtensionsStartupClasses implements SmackInitializer {
@Override
public void initialize() {
InputStream is;
XmlPullParser parser;
int eventType;
try {
is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null);
parser = XmlPullParserFactory.newInstance().newPullParser();
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();
SmackConfiguration.processConfigFile(is, exceptions);;
}
catch (Exception e) {
exceptions.add(e);

View file

@ -77,6 +77,9 @@ public class Socks5Proxy {
/* SOCKS5 proxy singleton */
private static Socks5Proxy socks5Server;
private static boolean localSocks5ProxyEnabled = true;
private static int localSocks5ProxyPort = 7777;
/* reusable implementation of a SOCKS5 proxy server process */
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.
*
@ -119,7 +159,7 @@ public class Socks5Proxy {
if (socks5Server == null) {
socks5Server = new Socks5Proxy();
}
if (SmackConfiguration.isLocalSocks5ProxyEnabled()) {
if (isLocalSocks5ProxyEnabled()) {
socks5Server.start();
}
return socks5Server;
@ -133,8 +173,8 @@ public class Socks5Proxy {
return;
}
try {
if (SmackConfiguration.getLocalSocks5ProxyPort() < 0) {
int port = Math.abs(SmackConfiguration.getLocalSocks5ProxyPort());
if (getLocalSocks5ProxyPort() < 0) {
int port = Math.abs(getLocalSocks5ProxyPort());
for (int i = 0; i < 65535 - port; i++) {
try {
this.serverSocket = new ServerSocket(port + i);
@ -146,7 +186,7 @@ public class Socks5Proxy {
}
}
else {
this.serverSocket = new ServerSocket(SmackConfiguration.getLocalSocks5ProxyPort());
this.serverSocket = new ServerSocket(getLocalSocks5ProxyPort());
}
if (this.serverSocket != null) {
@ -156,7 +196,7 @@ public class Socks5Proxy {
}
catch (IOException e) {
// 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.PacketInterceptor;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
@ -78,6 +77,8 @@ public class EntityCapsManager {
protected static EntityCapsPersistentCache persistentCache;
private static boolean autoEnableEntityCaps = true;
private static Map<Connection, EntityCapsManager> instances = Collections
.synchronizedMap(new WeakHashMap<Connection, EntityCapsManager>());
@ -249,7 +250,7 @@ public class EntityCapsManager {
// This calculates the local entity caps version
updateLocalEntityCaps();
if (SmackConfiguration.autoEnableEntityCaps())
if (autoEnableEntityCaps)
enableEntityCaps();
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 org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
@ -169,7 +168,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfNoSocks5ProxyFound1() {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -220,7 +219,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfNoSocks5ProxyFound2() {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -284,7 +283,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldBlacklistNonSocks5Proxies() {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -375,7 +374,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfTargetDoesNotAcceptSocks5Bytestream() {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -465,7 +464,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfTargetUsesInvalidSocks5Proxy() {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -547,7 +546,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldFailIfInitiatorCannotConnectToSocks5Proxy() {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -641,7 +640,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldNegotiateSocks5BytestreamAndTransferData() throws Exception {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -754,8 +753,8 @@ public class Socks5ByteStreamManagerTest {
public void shouldUseMultipleAddressesForLocalSocks5Proxy() throws Exception {
// enable clients local SOCKS5 proxy on port 7778
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
SmackConfiguration.setLocalSocks5ProxyPort(7778);
Socks5Proxy.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.setLocalSocks5ProxyPort(7778);
// start a local SOCKS5 proxy
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
@ -837,7 +836,7 @@ public class Socks5ByteStreamManagerTest {
// reset proxy settings
socks5Proxy.stop();
socks5Proxy.removeLocalAddress("localAddress");
SmackConfiguration.setLocalSocks5ProxyPort(7777);
Socks5Proxy.setLocalSocks5ProxyPort(7777);
}
@ -852,7 +851,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldPrioritizeSecondSocks5ProxyOnSecondAttempt() throws Exception {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -935,7 +934,7 @@ public class Socks5ByteStreamManagerTest {
public void shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled() throws Exception {
// disable clients local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
@ -1093,7 +1092,7 @@ public class Socks5ByteStreamManagerTest {
@After
public void cleanUp() {
Socks5TestProxy.stopProxy();
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.setLocalSocks5ProxyEnabled(true);
}
}

View file

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

View file

@ -23,7 +23,6 @@ import java.io.OutputStream;
import java.net.Socket;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
@ -87,7 +86,7 @@ public class Socks5ClientForInitiatorTest {
public void shouldFailIfTargetIsNotConnectedToLocalSocks5Proxy() throws Exception {
// start a local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyPort(proxyPort);
Socks5Proxy.setLocalSocks5ProxyPort(proxyPort);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy.start();
@ -125,7 +124,7 @@ public class Socks5ClientForInitiatorTest {
public void shouldSuccessfullyConnectThroughLocalSocks5Proxy() throws Exception {
// start a local SOCKS5 proxy
SmackConfiguration.setLocalSocks5ProxyPort(proxyPort);
Socks5Proxy.setLocalSocks5ProxyPort(proxyPort);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy.start();
@ -308,7 +307,7 @@ public class Socks5ClientForInitiatorTest {
*/
@After
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.List;
import org.jivesoftware.smack.SmackConfiguration;
import org.junit.After;
import org.junit.Test;
@ -48,7 +47,7 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldBeASingleton() {
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy1 = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy2 = Socks5Proxy.getSocks5Proxy();
@ -63,7 +62,7 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldNotBeRunningIfDisabled() {
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
assertFalse(proxy.isRunning());
}
@ -75,12 +74,12 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldUseFreePortOnNegativeValues() throws Exception {
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
assertFalse(proxy.isRunning());
ServerSocket serverSocket = new ServerSocket(0);
SmackConfiguration.setLocalSocks5ProxyPort(-serverSocket.getLocalPort());
Socks5Proxy.setLocalSocks5ProxyPort(-serverSocket.getLocalPort());
proxy.start();
@ -155,7 +154,7 @@ public class Socks5ProxyTest {
public void shouldOnlyStartOneServerThread() {
int threadCount = Thread.activeCount();
SmackConfiguration.setLocalSocks5ProxyPort(7890);
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start();
@ -189,7 +188,7 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldCloseSocketIfNoSocks5Request() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890);
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start();
@ -219,7 +218,7 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldRespondWithErrorIfNoSupportedAuthenticationMethod() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890);
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start();
@ -249,7 +248,7 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldRespondWithErrorIfConnectionIsNotAllowed() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890);
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start();
@ -290,7 +289,7 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldSuccessfullyEstablishConnection() throws Exception {
SmackConfiguration.setLocalSocks5ProxyPort(7890);
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.start();
@ -355,8 +354,8 @@ public class Socks5ProxyTest {
*/
@After
public void cleanup() {
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
SmackConfiguration.setLocalSocks5ProxyPort(7777);
Socks5Proxy.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.setLocalSocks5ProxyPort(7777);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
try {
String address = InetAddress.getLocalHost().getHostAddress();