1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 02:27:58 +01:00

Reworked OSGi support of Smack (SMACK-343)

Because of OSGi, no subproject of Smack (which is the same as a OSGi
bundle) must export a package that is already exported by another
subproject.

Therefore it was necessary to move the TCP and BOSH code into their own
packages: org.jivesoftware.smack.(tcp|bosh).

OSGi classloader restrictions also made it necessary to create a
Declarative Service for smack-extensions, smack-experimental and
smack-lagacy (i.e. smack subprojects which should be initialized), in
order to initialize them accordingly, as smack-core is, when used in a
OSGi environment, unable to load and initialize classes from other smack
bundles. OSGi's "Service Component Runtime" (SCR) will now take care of
running the initialization code of the particular Smack bundle by
activating its Declarative Service.

That is also the reason why most initialization related method now have an
additional classloader argument.

Note that due the refactoring, some ugly changes in XMPPTCPConnection
and its PacketReader and PacketWriter where necessary.
This commit is contained in:
Florian Schmaus 2014-05-15 15:04:46 +02:00
parent 541b8b3798
commit 4c76f2652d
39 changed files with 413 additions and 446 deletions

View file

@ -197,6 +197,16 @@ subprojects {
}
}
['smack-extensions', 'smack-experimental', 'smack-legacy'].each { name ->
project(":$name") {
jar {
manifest {
instruction 'Service-Component', "org.jivesoftware.smackx/$name-components.xml"
}
}
}
}
def getGitCommit() {
def dotGit = new File("$projectDir/.git")
if (!dotGit.isDirectory()) return 'non-git build'

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.bosh;
import java.net.URI;
import java.net.URISyntaxException;

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.bosh;
import java.io.StringReader;
@ -153,7 +153,7 @@ public class BOSHPacketReader implements BOSHClientResponseListener {
// The server supports sessions
connection.serverSupportsSession();
} else if (parser.getName().equals("register")) {
AccountManager.getInstance(connection).setSupportsAccountCreation(true);
connection.serverSupportsAccountCreation();
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("features")) {

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.bosh;
import java.io.IOException;
import java.io.PipedReader;
@ -27,9 +27,11 @@ import java.util.logging.Logger;
import javax.security.sasl.SaslException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.AlreadyLoggedInException;
import org.jivesoftware.smack.SmackException.ConnectionException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.ConnectionListener;
@ -137,7 +139,7 @@ public class XMPPBOSHConnection extends XMPPConnection {
}
@Override
void connectInternal() throws SmackException {
protected void connectInternal() throws SmackException {
if (connected) {
throw new IllegalStateException("Already connected to a server.");
}
@ -267,7 +269,7 @@ public class XMPPBOSHConnection extends XMPPConnection {
if (response != null) {
this.user = response;
// Update the serviceName with the one returned by the server
config.setServiceName(StringUtils.parseServer(response));
setServiceName(StringUtils.parseServer(response));
} else {
this.user = username + "@" + getServiceName();
if (resource != null) {
@ -285,7 +287,7 @@ public class XMPPBOSHConnection extends XMPPConnection {
anonymous = false;
// Stores the autentication for future reconnection
config.setLoginInfo(username, password, resource);
setLoginInfo(username, password, resource);
// If debugging is enabled, change the the debug window title to include
// the
@ -316,7 +318,7 @@ public class XMPPBOSHConnection extends XMPPConnection {
// Set the user value.
this.user = response;
// Update the serviceName with the one returned by the server
config.setServiceName(StringUtils.parseServer(response));
setServiceName(StringUtils.parseServer(response));
// Set presence to online.
if (config.isSendPresence()) {
@ -337,7 +339,8 @@ public class XMPPBOSHConnection extends XMPPConnection {
callConnectionAuthenticatedListener();
}
void sendPacketInternal(Packet packet) throws NotConnectedException {
@Override
protected void sendPacketInternal(Packet packet) throws NotConnectedException {
if (done) {
throw new NotConnectedException();
}
@ -508,6 +511,30 @@ public class XMPPBOSHConnection extends XMPPConnection {
callConnectionClosedOnErrorListener(e);
}
@Override
protected void processPacket(Packet packet) {
super.processPacket(packet);
}
@Override
protected SASLAuthentication getSASLAuthentication() {
return super.getSASLAuthentication();
}
@Override
protected void serverRequiresBinding() {
super.serverRequiresBinding();
}
@Override
protected void serverSupportsSession() {
super.serverSupportsSession();
}
@Override
protected void serverSupportsAccountCreation() {
super.serverSupportsAccountCreation();
}
/**
* A listener class which listen for a successfully established connection

View file

@ -14,13 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.compression;
package org.jivesoftware.smack.compression.jzlib;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZInputStream;

View file

@ -406,7 +406,7 @@ public class SASLAuthentication {
* @param mechanisms collection of strings with the available SASL mechanism reported
* by the server.
*/
void setAvailableSASLMethods(Collection<String> mechanisms) {
public void setAvailableSASLMethods(Collection<String> mechanisms) {
this.serverMechanisms = mechanisms;
}
@ -429,7 +429,7 @@ public class SASLAuthentication {
* @throws IOException If a network error occures while authenticating.
* @throws NotConnectedException
*/
void challengeReceived(String challenge) throws IOException, NotConnectedException {
public void challengeReceived(String challenge) throws IOException, NotConnectedException {
currentMechanism.challengeReceived(challenge);
}
@ -437,7 +437,7 @@ public class SASLAuthentication {
* Notification message saying that SASL authentication was successful. The next step
* would be to bind the resource.
*/
void authenticated() {
public void authenticated() {
saslNegotiated = true;
// Wake up the thread that is waiting in the #authenticate method
synchronized (this) {
@ -452,7 +452,7 @@ public class SASLAuthentication {
* @param saslFailure the SASL failure as reported by the server
* @see <a href="https://tools.ietf.org/html/rfc6120#section-6.5">RFC6120 6.5</a>
*/
void authenticationFailed(SASLFailure saslFailure) {
public void authenticationFailed(SASLFailure saslFailure) {
this.saslFailure = saslFailure;
// Wake up the thread that is waiting in the #authenticate method
synchronized (this) {

View file

@ -311,7 +311,13 @@ public final class SmackConfiguration {
return res;
}
public static void processConfigFile(InputStream cfgFileStream, Collection<Exception> exceptions) throws Exception {
public static void processConfigFile(InputStream cfgFileStream,
Collection<Exception> exceptions) throws Exception {
processConfigFile(cfgFileStream, exceptions, SmackConfiguration.class.getClassLoader());
}
public static void processConfigFile(InputStream cfgFileStream,
Collection<Exception> exceptions, ClassLoader classLoader) throws Exception {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(cfgFileStream, "UTF-8");
@ -319,10 +325,10 @@ public final class SmackConfiguration {
do {
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("startupClasses")) {
parseClassesToLoad(parser, false, exceptions);
parseClassesToLoad(parser, false, exceptions, classLoader);
}
else if (parser.getName().equals("optionalStartupClasses")) {
parseClassesToLoad(parser, true, exceptions);
parseClassesToLoad(parser, true, exceptions, classLoader);
}
}
eventType = parser.next();
@ -336,7 +342,9 @@ public final class SmackConfiguration {
}
}
private static void parseClassesToLoad(XmlPullParser parser, boolean optional, Collection<Exception> exceptions) throws XmlPullParserException, IOException, Exception {
private static void parseClassesToLoad(XmlPullParser parser, boolean optional,
Collection<Exception> exceptions, ClassLoader classLoader)
throws XmlPullParserException, IOException, Exception {
final String startName = parser.getName();
int eventType;
String name;
@ -350,26 +358,30 @@ public final class SmackConfiguration {
}
else {
try {
loadSmackClass(classToLoad, optional);
} catch (Exception e) {
loadSmackClass(classToLoad, optional, classLoader);
}
catch (Exception e) {
// Don't throw the exception if an exceptions collection is given, instead
// record it there. This is used for unit testing purposes.
if (exceptions != null) {
exceptions.add(e);
} else {
}
else {
throw e;
}
}
}
}
} while (! (eventType == XmlPullParser.END_TAG && startName.equals(name)));
}
while (!(eventType == XmlPullParser.END_TAG && startName.equals(name)));
}
private static void loadSmackClass(String className, boolean optional) throws Exception {
private static void loadSmackClass(String className, boolean optional, ClassLoader classLoader) throws Exception {
Class<?> initClass;
try {
// Attempt to load the class so that the class can get initialized
initClass = Class.forName(className);
// Attempt to load and initialize the class so that all static initializer blocks of
// class are executed
initClass = Class.forName(className, true, classLoader);
}
catch (ClassNotFoundException cnfe) {
Level logLevel;
@ -388,8 +400,7 @@ public final class SmackConfiguration {
}
if (SmackInitializer.class.isAssignableFrom(initClass)) {
SmackInitializer initializer = (SmackInitializer) initClass.newInstance();
initializer.initialize();
List<Exception> exceptions = initializer.getExceptions();
List<Exception> exceptions = initializer.initialize();
if (exceptions.size() == 0) {
LOGGER.log(Level.FINE, "Loaded SmackInitializer " + className);
} else {

View file

@ -352,7 +352,7 @@ public abstract class XMPPConnection {
*/
public abstract boolean isSecureConnection();
abstract void sendPacketInternal(Packet packet) throws NotConnectedException;
protected abstract void sendPacketInternal(Packet packet) throws NotConnectedException;
/**
* Returns true if network traffic is being compressed. When using stream compression network
@ -398,7 +398,7 @@ public abstract class XMPPConnection {
* @throws IOException
* @throws XMPPException
*/
abstract void connectInternal() throws SmackException, IOException, XMPPException;
protected abstract void connectInternal() throws SmackException, IOException, XMPPException;
/**
* Logs in to the server using the strongest authentication mode supported by
@ -476,7 +476,7 @@ public abstract class XMPPConnection {
* Notification message saying that the server requires the client to bind a
* resource to the stream.
*/
void serverRequiresBinding() {
protected void serverRequiresBinding() {
synchronized (bindingRequired) {
bindingRequired.set(true);
bindingRequired.notify();
@ -488,11 +488,11 @@ public abstract class XMPPConnection {
* sessions the client needs to send a Session packet after successfully binding a resource
* for the session.
*/
void serverSupportsSession() {
protected void serverSupportsSession() {
sessionSupported = true;
}
String bindResourceAndEstablishSession(String resource) throws XMPPErrorException,
protected String bindResourceAndEstablishSession(String resource) throws XMPPErrorException,
ResourceBindingNotOfferedException, NoResponseException, NotConnectedException {
synchronized (bindingRequired) {
@ -537,6 +537,30 @@ public abstract class XMPPConnection {
}
}
protected Reader getReader() {
return reader;
}
protected Writer getWriter() {
return writer;
}
protected void setServiceName(String serviceName) {
config.setServiceName(serviceName);
}
protected void setLoginInfo(String username, String password, String resource) {
config.setLoginInfo(username, password, resource);
}
protected void serverSupportsAccountCreation() {
AccountManager.getInstance(this).setSupportsAccountCreation(true);
}
protected void maybeResolveDns() throws Exception {
config.maybeResolveDns();
}
/**
* Sends the specified packet to the server.
*
@ -627,6 +651,7 @@ public abstract class XMPPConnection {
}
return roster;
}
/**
* Returns the SASLAuthentication manager that is responsible for authenticating with
* the server.
@ -634,7 +659,7 @@ public abstract class XMPPConnection {
* @return the SASLAuthentication manager that is responsible for authenticating with
* the server.
*/
public SASLAuthentication getSASLAuthentication() {
protected SASLAuthentication getSASLAuthentication() {
return saslAuthentication;
}
@ -1127,13 +1152,13 @@ public abstract class XMPPConnection {
}
}
void callConnectionConnectedListener() {
protected void callConnectionConnectedListener() {
for (ConnectionListener listener : getConnectionListeners()) {
listener.connected(this);
}
}
void callConnectionAuthenticatedListener() {
protected void callConnectionAuthenticatedListener() {
for (ConnectionListener listener : getConnectionListeners()) {
listener.authenticated(this);
}
@ -1152,7 +1177,7 @@ public abstract class XMPPConnection {
}
}
void callConnectionClosedOnErrorListener(Exception e) {
protected void callConnectionClosedOnErrorListener(Exception e) {
LOGGER.log(Level.WARNING, "Connection closed with error", e);
for (ConnectionListener listener : getConnectionListeners()) {
try {

View file

@ -1,55 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.initializer;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.FileUtils;
/**
* Initializes the Java logging system.
*
* @author Robin Collier
*
*/
public class LoggingInitializer implements SmackInitializer {
private static final Logger LOGGER = Logger.getLogger(LoggingInitializer.class.getName());
private List<Exception> exceptions = new LinkedList<Exception>();
@Override
public void initialize() {
try {
LogManager.getLogManager().readConfiguration(FileUtils.getStreamForUrl("classpath:org.jivesofware.smack/jul.properties", null));
}
catch (Exception e) {
LOGGER.log(Level.WARNING, "Could not initialize Java Logging from default file.", e);
exceptions.add(e);
}
}
@Override
public List<Exception> getExceptions() {
return Collections.unmodifiableList(exceptions);
}
}

View file

@ -30,6 +30,6 @@ import org.jivesoftware.smack.SmackConfiguration;
*
*/
public interface SmackInitializer {
void initialize();
List<Exception> getExceptions();
public List<Exception> initialize();
public List<Exception> initialize(ClassLoader classLoader);
}

View file

@ -0,0 +1,99 @@
/**
*
* Copyright 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.initializer;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.provider.ProviderFileLoader;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.FileUtils;
/**
* Loads the provider file defined by the URL returned by {@link #getProvidersUrl()} and the generic
* smack configuration file returned {@link #getConfigUrl()}.
*
* @author Florian Schmaus
*/
public abstract class UrlInitializer implements SmackInitializer {
private static final Logger LOGGER = Logger.getLogger(UrlInitializer.class.getName());
/**
* A simple wrapper around {@link #initialize} for OSGi, as the activate method of a component
* must have a void return type.
*/
public final void activate() {
initialize();
}
@Override
public List<Exception> initialize() {
return initialize(this.getClass().getClassLoader());
}
@Override
public List<Exception> initialize(ClassLoader classLoader) {
InputStream is;
final List<Exception> exceptions = new LinkedList<Exception>();
final String providerUrl = getProvidersUrl();
if (providerUrl != null) {
try {
is = FileUtils.getStreamForUrl(providerUrl, classLoader);
if (is != null) {
LOGGER.log(Level.FINE, "Loading providers for providerUrl [" + providerUrl
+ "]");
ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader);
ProviderManager.addLoader(pfl);
exceptions.addAll(pfl.getLoadingExceptions());
}
else {
LOGGER.log(Level.WARNING, "No input stream created for " + providerUrl);
exceptions.add(new IOException("No input stream created for " + providerUrl));
}
}
catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUrl, e);
exceptions.add(e);
}
}
final String configUrl = getConfigUrl();
if (configUrl != null) {
try {
is = FileUtils.getStreamForUrl(configUrl, classLoader);
SmackConfiguration.processConfigFile(is, exceptions, classLoader);
}
catch (Exception e) {
exceptions.add(e);
}
}
return exceptions;
}
protected String getProvidersUrl() {
return null;
}
protected String getConfigUrl() {
return null;
}
}

View file

@ -1,81 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.initializer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.provider.ProviderFileLoader;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.FileUtils;
/**
* Loads the provider file defined by the URL returned by {@link #getFilePath()}. This file will be loaded on Smack initialization.
*
* @author Robin Collier
*
*/
public abstract class UrlProviderFileInitializer implements SmackInitializer {
private static final Logger LOGGER = Logger.getLogger(UrlProviderFileInitializer.class.getName());
private List<Exception> exceptions = new LinkedList<Exception>();
@Override
public void initialize() {
String filePath = getFilePath();
try {
InputStream is = FileUtils.getStreamForUrl(filePath, getClassLoader());
if (is != null) {
LOGGER.log(Level.INFO, "Loading providers for file [" + filePath + "]");
ProviderFileLoader pfl = new ProviderFileLoader(is);
ProviderManager.addLoader(pfl);
exceptions.addAll(pfl.getLoadingExceptions());
}
else {
LOGGER.log(Level.WARNING, "No input stream created for " + filePath);
exceptions.add(new IOException("No input stream created for " + filePath));
}
}
catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error trying to load provider file " + filePath, e);
exceptions.add(e);
}
}
@Override
public List<Exception> getExceptions() {
return Collections.unmodifiableList(exceptions);
}
protected abstract String getFilePath();
/**
* Returns an array of class loaders to load resources from.
*
* @return an array of ClassLoader instances.
*/
protected ClassLoader getClassLoader() {
return null;
}
}

View file

@ -16,6 +16,9 @@
*/
package org.jivesoftware.smack.initializer;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.provider.ProviderManager;
@ -26,16 +29,17 @@ import org.jivesoftware.smack.provider.ProviderManager;
* @author Robin Collier
*
*/
public class VmArgInitializer extends UrlProviderFileInitializer {
public class VmArgInitializer extends UrlInitializer {
protected String getFilePath() {
return System.getProperty("smack.provider.file");
}
@Override
public void initialize() {
public List<Exception> initialize() {
if (getFilePath() != null) {
super.initialize();
}
return Collections.emptyList();
}
}

View file

@ -45,8 +45,12 @@ public class ProviderFileLoader implements ProviderLoader {
private List<Exception> exceptions = new LinkedList<Exception>();
@SuppressWarnings("unchecked")
public ProviderFileLoader(InputStream providerStream) {
this(providerStream, ProviderFileLoader.class.getClassLoader());
}
@SuppressWarnings("unchecked")
public ProviderFileLoader(InputStream providerStream, ClassLoader classLoader) {
iqProviders = new ArrayList<IQProviderInfo>();
extProviders = new ArrayList<ExtensionProviderInfo>();
@ -73,13 +77,13 @@ public class ProviderFileLoader implements ProviderLoader {
String className = parser.nextText();
try {
final Class<?> provider = classLoader.loadClass(className);
// Attempt to load the provider class and then create
// a new instance if it's an IQProvider. Otherwise, if it's
// an IQ class, add the class object itself, then we'll use
// reflection later to create instances of the class.
if ("iqProvider".equals(typeName)) {
// Add the provider to the map.
Class<?> provider = Class.forName(className);
if (IQProvider.class.isAssignableFrom(provider)) {
iqProviders.add(new IQProviderInfo(elementName, namespace, (IQProvider) provider.newInstance()));
@ -94,7 +98,6 @@ public class ProviderFileLoader implements ProviderLoader {
// a PacketExtension, add the class object itself and
// then we'll use reflection later to create instances
// of the class.
Class<?> provider = Class.forName(className);
if (PacketExtensionProvider.class.isAssignableFrom(provider)) {
extProviders.add(new ExtensionProviderInfo(elementName, namespace, (PacketExtensionProvider) provider.newInstance()));
}

View file

@ -46,7 +46,10 @@ public final class FileUtils {
if (fileUri.getScheme().equals("classpath")) {
// Get an array of class loaders to try loading the providers files from.
ClassLoader[] classLoaders = getClassLoaders();
List<ClassLoader> classLoaders = getClassLoaders();
if (loader != null) {
classLoaders.add(0, loader);
}
for (ClassLoader classLoader : classLoaders) {
InputStream is = classLoader.getResourceAsStream(fileUri.getSchemeSpecificPart());
@ -64,21 +67,21 @@ public final class FileUtils {
/**
* Returns default classloaders.
*
* @return an array of ClassLoader instances.
* @return a List of ClassLoader instances.
*/
public static ClassLoader[] getClassLoaders() {
public static List<ClassLoader> getClassLoaders() {
ClassLoader[] classLoaders = new ClassLoader[2];
classLoaders[0] = FileUtils.class.getClassLoader();
classLoaders[1] = Thread.currentThread().getContextClassLoader();
// Clean up possible null values. Note that #getClassLoader may return a null value.
List<ClassLoader> loaders = new ArrayList<ClassLoader>();
// Clean up possible null values. Note that #getClassLoader may return a null value.
List<ClassLoader> loaders = new ArrayList<ClassLoader>(classLoaders.length);
for (ClassLoader classLoader : classLoaders) {
if (classLoader != null) {
loaders.add(classLoader);
}
}
return loaders.toArray(new ClassLoader[loaders.size()]);
return loaders;
}
public static boolean addLines(String url, Set<String> set) throws MalformedURLException, IOException {

View file

@ -8,12 +8,9 @@
</startupClasses>
<optionalStartupClasses>
<className>org.jivesoftware.smack.util.dns.JavaxResolver</className>
<className>org.jivesoftware.smackx.ExtensionsProviderInitializer</className>
<className>org.jivesoftware.smackx.ExtensionsStartupClasses</className>
<className>org.jivesoftware.smackx.ExperimentalProviderInitializer</className>
<className>org.jivesoftware.smackx.ExperimentalStartupClasses</className>
<className>org.jivesoftware.smackx.WorkgroupProviderInitializer</className>
<className>org.jivesoftware.smackx.LegacyProviderInitializer</className>
<className>org.jivesoftware.smack.util.dns.javax.JavaxResolver</className>
<className>org.jivesoftware.smack.initializer.extensions.ExtensionsInitializer</className>
<className>org.jivesoftware.smack.initializer.experimental.ExperimentalInitializer</className>
<className>org.jivesoftware.smack.initializer.legacy.LegacyInitializer</className>
</optionalStartupClasses>
</smack>

View file

@ -74,7 +74,7 @@ public class DummyConnection extends XMPPConnection {
}
@Override
void connectInternal() {
protected void connectInternal() {
connectionID = "dummy-" + new Random(new Date().getTime()).nextInt();
if (reconnect) {
@ -185,7 +185,7 @@ public class DummyConnection extends XMPPConnection {
}
@Override
void sendPacketInternal(Packet packet) {
protected void sendPacketInternal(Packet packet) {
if (SmackConfiguration.DEBUG_ENABLED) {
System.out.println("[SEND]: " + packet.toXML());
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013 Robin Collier
* Copyright 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,20 +14,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
package org.jivesoftware.smack.initializer.experimental;
import org.jivesoftware.smack.initializer.UrlProviderFileInitializer;
import org.jivesoftware.smack.initializer.UrlInitializer;
/**
* Initializes the providers in the experimental code stream.
*
* @author Robin Collier
*
* @author Florian Schmaus
*/
public class ExperimentalProviderInitializer extends UrlProviderFileInitializer {
public class ExperimentalInitializer extends UrlInitializer {
@Override
protected String getFilePath() {
protected String getProvidersUrl() {
return "classpath:org.jivesoftware.smackx/experimental.providers";
}
@Override
protected String getConfigUrl() {
return "classpath:org.jivesoftware.smackx/extensions.xml";
}
}

View file

@ -1,54 +0,0 @@
/**
*
* Copyright 2014 Andriy Tsykholyas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.util.FileUtils;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* {@link SmackInitializer} implementation for experimental module.
*/
public class ExperimentalStartupClasses implements SmackInitializer {
private static final String EXTENSIONS_XML = "classpath:org.jivesoftware.smackx/extensions.xml";
private List<Exception> exceptions = new LinkedList<Exception>();
// TODO log
@Override
public void initialize() {
InputStream is;
try {
is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null);
SmackConfiguration.processConfigFile(is, exceptions);;
}
catch (Exception e) {
exceptions.add(e);
}
}
@Override
public List<Exception> getExceptions() {
return Collections.unmodifiableList(exceptions);
}
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0"
enabled="true" immediate="true" name="Smack Experimental API">
<implementation
class="org.jivesoftware.smack.initializer.experimental.ExperimentalInitializer" />
</scr:component>

View file

@ -18,14 +18,17 @@ package org.jivesoftware.smackx;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.jivesoftware.smack.initializer.experimental.ExperimentalInitializer;
import org.junit.Test;
public class ExperimentalProviderInitializerTest {
public class ExperimentalInitializerTest {
@Test
public void testExperimentalProviderInitialzer() {
ExperimentalProviderInitializer epi = new ExperimentalProviderInitializer();
epi.initialize();
assertTrue(epi.getExceptions().size() == 0);
public void testExperimentalInitialzer() {
ExperimentalInitializer epi = new ExperimentalInitializer();
List<Exception> exceptions = epi.initialize();
assertTrue(exceptions.size() == 0);
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright the original author or authors
* Copyright 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,19 +14,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
package org.jivesoftware.smack.initializer.extensions;
import org.jivesoftware.smack.initializer.UrlProviderFileInitializer;
import org.jivesoftware.smack.initializer.UrlInitializer;
/**
* Loads the default provider file for the Smack extensions on initialization.
* Initializes the providers in the experimental code stream.
*
* @author Robin Collier
*
* @author Florian Schmaus
*/
public class ExtensionsProviderInitializer extends UrlProviderFileInitializer {
public class ExtensionsInitializer extends UrlInitializer {
@Override
protected String getFilePath() {
protected String getProvidersUrl() {
return "classpath:org.jivesoftware.smackx/extensions.providers";
}
@Override
protected String getConfigUrl() {
return "classpath:org.jivesoftware.smackx/extensions.xml";
}
}

View file

@ -1,52 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.util.FileUtils;
public class ExtensionsStartupClasses implements SmackInitializer {
private static final String EXTENSIONS_XML = "classpath:org.jivesoftware.smackx/extensions.xml";
private List<Exception> exceptions = new LinkedList<Exception>();
// TODO log
@Override
public void initialize() {
InputStream is;
try {
is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null);
SmackConfiguration.processConfigFile(is, exceptions);;
}
catch (Exception e) {
exceptions.add(e);
}
}
@Override
public List<Exception> getExceptions() {
return Collections.unmodifiableList(exceptions);
}
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0"
enabled="true" immediate="true" name="Smack Extensions API">
<implementation
class="org.jivesoftware.smack.initializer.extensions.ExtensionsInitializer" />
</scr:component>

View file

@ -18,15 +18,18 @@ package org.jivesoftware.smackx;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.jivesoftware.smack.initializer.extensions.ExtensionsInitializer;
import org.junit.Test;
public class ExtensionsStartupClassesTest {
public class ExtensionsInitializerTest {
@Test
public void testExtensiosnStartupClasses() {
ExtensionsStartupClasses esc = new ExtensionsStartupClasses();
esc.initialize();
assertTrue(esc.getExceptions().size() == 0);
public void testExtensionInitializer() {
ExtensionsInitializer ei = new ExtensionsInitializer();
List<Exception> exceptions = ei.initialize();
assertTrue(exceptions.size() == 0);
}
}

View file

@ -1,32 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ExtensionsProviderInitializerTest {
@Test
public void testExtensionProviderInitializer() {
ExtensionsProviderInitializer ei = new ExtensionsProviderInitializer();
ei.initialize();
assertTrue(ei.getExceptions().size() == 0);
}
}

View file

@ -16,11 +16,12 @@
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.initializer.extensions.ExtensionsInitializer;
public class InitExtensions {
static {
(new ExtensionsProviderInitializer()).initialize();
(new ExtensionsStartupClasses()).initialize();
(new ExtensionsInitializer()).initialize();
}
}

View file

@ -14,14 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
package org.jivesoftware.smack.initializer.legacy;
import org.jivesoftware.smack.initializer.UrlProviderFileInitializer;
import org.jivesoftware.smack.initializer.UrlInitializer;
public class LegacyProviderInitializer extends UrlProviderFileInitializer {
public class LegacyInitializer extends UrlInitializer {
@Override
protected String getFilePath() {
protected String getProvidersUrl() {
return "classpath:org.jivesoftware.smackx/legacy.providers";
}
}

View file

@ -1,27 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.initializer.UrlProviderFileInitializer;
public class WorkgroupProviderInitializer extends UrlProviderFileInitializer {
@Override
protected String getFilePath() {
return "classpath:org.jivesoftware.smackx/workgroup.providers";
}
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0"
enabled="true" immediate="true" name="Smack Legacy API">
<implementation
class="org.jivesoftware.smack.initializer.legacy.LegacyInitializer" />
</scr:component>

View file

@ -18,14 +18,17 @@ package org.jivesoftware.smackx;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.jivesoftware.smack.initializer.legacy.LegacyInitializer;
import org.junit.Test;
public class LegacyProviderInitializerTest {
public class LegacyInitializerTest {
@Test
public void testWorkgroupProviderInitializer() {
LegacyProviderInitializer lpi = new LegacyProviderInitializer();
lpi.initialize();
assertTrue(lpi.getExceptions().size() == 0);
LegacyInitializer lpi = new LegacyInitializer();
List<Exception> exceptions = lpi.initialize();
assertTrue(exceptions.size() == 0);
}
}

View file

@ -1,31 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class WorkgroupProviderInitializerTest {
@Test
public void testWorkgroupProviderInitializer() {
WorkgroupProviderInitializer wpi = new WorkgroupProviderInitializer();
wpi.initialize();
assertTrue(wpi.getExceptions().size() == 0);
}
}

View file

@ -14,11 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.dns;
package org.jivesoftware.smack.util.dns.dnsjava;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.util.dns.DNSResolver;
import org.jivesoftware.smack.util.dns.SRVRecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TextParseException;

View file

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.dns;
package org.jivesoftware.smack.util.dns.javax;
import java.util.ArrayList;
import java.util.Hashtable;
@ -28,6 +28,8 @@ import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.dns.DNSResolver;
import org.jivesoftware.smack.util.dns.SRVRecord;
/**
* A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namespace.

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.tcp;
import java.io.IOException;
@ -28,6 +28,8 @@ import org.jivesoftware.smack.sasl.SASLMechanism.Challenge;
import org.jivesoftware.smack.sasl.SASLMechanism.SASLFailure;
import org.jivesoftware.smack.sasl.SASLMechanism.Success;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.SecurityRequiredException;
import org.jivesoftware.smack.XMPPException.StreamErrorException;
@ -80,7 +82,7 @@ class PacketReader {
parsePackets(this);
}
};
readerThread.setName("Smack Packet Reader (" + connection.connectionCounterValue + ")");
readerThread.setName("Smack Packet Reader (" + connection.getConnectionCounter() + ")");
readerThread.setDaemon(true);
resetParser();
@ -131,7 +133,7 @@ class PacketReader {
try {
parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(connection.reader);
parser.setInput(connection.getReader());
}
catch (XmlPullParserException e) {
throw new SmackException(e);
@ -205,7 +207,7 @@ class PacketReader {
}
else if (parser.getAttributeName(i).equals("from")) {
// Use the server name that the server says that it is.
connection.config.setServiceName(parser.getAttributeValue(i));
connection.setServiceName(parser.getAttributeValue(i));
}
}
}
@ -342,7 +344,7 @@ class PacketReader {
connection.setAvailableCompressionMethods(PacketParserUtils.parseCompressionMethods(parser));
}
else if (parser.getName().equals("register")) {
AccountManager.getInstance(connection).setSupportsAccountCreation(true);
connection.serverSupportsAccountCreation();
}
}
else if (eventType == XmlPullParser.END_TAG) {

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.tcp;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.packet.Packet;
@ -67,7 +67,7 @@ class PacketWriter {
* is invoked if the connection is disconnected by an error.
*/
protected void init() {
this.writer = connection.writer;
writer = connection.getWriter();
done = false;
shutdownDone.set(false);
@ -77,7 +77,7 @@ class PacketWriter {
writePackets(this);
}
};
writerThread.setName("Smack Packet Writer (" + connection.connectionCounterValue + ")");
writerThread.setName("Smack Packet Writer (" + connection.getConnectionCounter() + ")");
writerThread.setDaemon(true);
}

View file

@ -14,11 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.tcp;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.AlreadyLoggedInException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.ConnectionException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
@ -44,7 +53,9 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.net.Socket;
import java.security.KeyStore;
@ -248,7 +259,7 @@ public class XMPPTCPConnection extends XMPPConnection {
if (response != null) {
this.user = response;
// Update the serviceName with the one returned by the server
config.setServiceName(StringUtils.parseServer(response));
setServiceName(StringUtils.parseServer(response));
}
else {
this.user = username + "@" + getServiceName();
@ -267,7 +278,7 @@ public class XMPPTCPConnection extends XMPPConnection {
}
// Stores the authentication for future reconnection
config.setLoginInfo(username, password, resource);
setLoginInfo(username, password, resource);
// If debugging is enabled, change the the debug window title to include the
// name we are now logged-in as.
@ -299,7 +310,7 @@ public class XMPPTCPConnection extends XMPPConnection {
// Set the user value.
this.user = response;
// Update the serviceName with the one returned by the server
config.setServiceName(StringUtils.parseServer(response));
setServiceName(StringUtils.parseServer(response));
// If compression is enabled then request the server to use stream compression
if (config.isCompressionEnabled()) {
@ -375,14 +386,15 @@ public class XMPPTCPConnection extends XMPPConnection {
writer = null;
}
void sendPacketInternal(Packet packet) throws NotConnectedException {
@Override
protected void sendPacketInternal(Packet packet) throws NotConnectedException {
packetWriter.sendPacket(packet);
}
private void connectUsingConfiguration(ConnectionConfiguration config) throws SmackException, IOException {
Exception exception = null;
try {
config.maybeResolveDns();
maybeResolveDns();
}
catch (Exception e) {
throw new SmackException(e);
@ -782,7 +794,7 @@ public class XMPPTCPConnection extends XMPPConnection {
* @throws IOException
*/
@Override
void connectInternal() throws SmackException, IOException, XMPPException {
protected void connectInternal() throws SmackException, IOException, XMPPException {
// Establishes the connection, readers and writers
connectUsingConfiguration(config);
// TODO is there a case where connectUsing.. does not throw an exception but connected is
@ -823,10 +835,70 @@ public class XMPPTCPConnection extends XMPPConnection {
callConnectionClosedOnErrorListener(e);
}
@Override
protected void processPacket(Packet packet) {
super.processPacket(packet);
}
@Override
protected Reader getReader() {
return super.getReader();
}
@Override
protected Writer getWriter() {
return super.getWriter();
}
@Override
protected void throwConnectionExceptionOrNoResponse() throws IOException, NoResponseException {
super.throwConnectionExceptionOrNoResponse();
}
@Override
protected void setServiceName(String serviceName) {
super.setServiceName(serviceName);
}
@Override
protected void serverRequiresBinding() {
super.serverRequiresBinding();
}
@Override
protected void setServiceCapsNode(String node) {
super.setServiceCapsNode(node);
}
@Override
protected void serverSupportsSession() {
super.serverSupportsSession();
}
@Override
protected void setRosterVersioningSupported() {
super.setRosterVersioningSupported();
}
@Override
protected void serverSupportsAccountCreation() {
super.serverSupportsAccountCreation();
}
@Override
protected SASLAuthentication getSASLAuthentication() {
return super.getSASLAuthentication();
}
@Override
protected ConnectionConfiguration getConfiguration() {
return super.getConfiguration();
}
/**
* Sends a notification indicating that the connection was reconnected successfully.
*/
protected void notifyReconnection() {
private void notifyReconnection() {
// Notify connection listeners of the reconnection.
for (ConnectionListener listener : getConnectionListeners()) {
try {

View file

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.tcp;
import java.io.IOException;
import java.io.Writer;
@ -34,7 +34,7 @@ public class PacketWriterTest {
/**
* Make sure that packet writer does block once the queue reaches
* {@link PacketWriter#QUEUE_SIZE} and that
* {@link PacketWriter#sendPacket(org.jivesoftware.smack.packet.Packet)} does unblock after the
* {@link PacketWriter#sendPacket(org.jivesoftware.smack.tcp.packet.Packet)} does unblock after the
* interrupt.
*
* @throws InterruptedException

View file

@ -14,10 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack;
package org.jivesoftware.smack.tcp;
import static org.junit.Assert.*;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.junit.Before;
import org.junit.Test;