Commit ec8d6fd4 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

Removing Guice, reverting JM-1083 and JM-1026

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8651 b35dd754-fafc-0310-a699-88a17e54d16e
parent a7bd18d0
...@@ -453,15 +453,6 @@ ...@@ -453,15 +453,6 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../lib/dist/guice.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntryProperties /> <orderEntryProperties />
</component> </component>
</module> </module>
......
...@@ -305,69 +305,7 @@ ...@@ -305,69 +305,7 @@
<component name="ProjectRootManager" version="2" assert-keyword="true" jdk-15="true" project-jdk-name="JDK 1.5.0" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" assert-keyword="true" jdk-15="true" project-jdk-name="JDK 1.5.0" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/../../target" /> <output url="file://$PROJECT_DIR$/../../target" />
</component> </component>
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager" />
<configuration default="false" name="Unit Tests" type="JUnit" factoryName="JUnit" enabled="false" merge="false">
<module name="Admin" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" value="" />
<option name="PACKAGE_NAME" value="org.jivesoftware.openfire.handler" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<option name="VM_PARAMETERS" value="" />
<option name="PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
<option name="ADDITIONAL_CLASS_PATH" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="wholeProject" />
</option>
<RunnerSettings RunnerId="Debug">
<option name="DEBUG_PORT" value="1297" />
<option name="TRANSPORT" value="0" />
<option name="LOCAL" value="true" />
</RunnerSettings>
<RunnerSettings RunnerId="JProfiler">
<option name="WINDOW" value="false" />
<option name="JVMPI" value="false" />
<option name="INTERPRETED" value="false" />
</RunnerSettings>
<ConfigurationWrapper RunnerId="Debug" />
<method>
<option name="Make" value="true" />
</method>
</configuration>
<configuration default="false" name="Plugin Unit Tests" type="JUnit" factoryName="JUnit" enabled="true" merge="true">
<pattern value="org.jivesoftware.openfire.plugin.*" />
<module name="Plugins" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" value="" />
<option name="PACKAGE_NAME" value="org.jivesoftware.openfire.plugin" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<option name="VM_PARAMETERS" value="" />
<option name="PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
<option name="ADDITIONAL_CLASS_PATH" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<RunnerSettings RunnerId="Debug">
<option name="DEBUG_PORT" value="1578" />
<option name="TRANSPORT" value="0" />
<option name="LOCAL" value="true" />
</RunnerSettings>
<RunnerSettings RunnerId="JProfiler">
<option name="WINDOW" value="false" />
<option name="JVMPI" value="false" />
<option name="INTERPRETED" value="false" />
</RunnerSettings>
<ConfigurationWrapper RunnerId="Debug" />
<method>
<option name="Make" value="true" />
</method>
</configuration>
</component>
<component name="RmicSettings"> <component name="RmicSettings">
<option name="IS_EANABLED" value="false" /> <option name="IS_EANABLED" value="false" />
<option name="DEBUGGING_INFO" value="true" /> <option name="DEBUGGING_INFO" value="true" />
......
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.container;
import com.google.inject.*;
import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.container.plugin.ServerName;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.session.RemoteSessionLocator;
import org.jivesoftware.openfire.vcard.VCardManager;
import org.jivesoftware.openfire.stun.STUNService;
import org.jivesoftware.openfire.mediaproxy.MediaProxyService;
import org.jivesoftware.openfire.filetransfer.proxy.FileTransferProxy;
import org.jivesoftware.openfire.filetransfer.FileTransferManager;
import org.jivesoftware.openfire.commands.AdHocCommandHandler;
import org.jivesoftware.openfire.muc.MultiUserChatServer;
import org.jivesoftware.openfire.disco.IQDiscoItemsHandler;
import org.jivesoftware.openfire.audit.AuditManager;
import org.jivesoftware.openfire.update.UpdateManager;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.pubsub.PubSubModule;
import org.jivesoftware.openfire.handler.IQRegisterHandler;
import org.jivesoftware.openfire.handler.IQAuthHandler;
import org.jivesoftware.openfire.handler.PresenceUpdateHandler;
import org.jivesoftware.openfire.handler.PresenceSubscribeHandler;
import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.util.JiveProperties;
import org.jivesoftware.util.Logger;
import org.jivesoftware.util.Log;
import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory;
/**
*
*/
public class PluginModule extends AbstractModule {
protected void configure() {
bind(JiveProperties.class).toInstance(JiveProperties.getInstance());
bind(Logger.class).toInstance(getLogger());
final XMPPServer server = XMPPServer.getInstance();
bind(String.class).annotatedWith(ServerName.class).toProvider(new Provider<String>() {
public String get() {
return server.getServerInfo().getName();
}
});
bind(XMPPServer.class).toInstance(server);
bind(ConnectionManager.class).toInstance(server.getConnectionManager());
bind(RoutingTable.class).toInstance(server.getRoutingTable());
bind(PacketDeliverer.class).toInstance(server.getPacketDeliverer());
bind(RosterManager.class).toInstance(server.getRosterManager());
bind(PresenceManager.class).toInstance(server.getPresenceManager());
bind(OfflineMessageStore.class).toInstance(server.getOfflineMessageStore());
bind(OfflineMessageStrategy.class).toInstance(server.getOfflineMessageStrategy());
bind(PacketRouter.class).toInstance(server.getPacketRouter());
bind(IQRegisterHandler.class).toInstance(server.getIQRegisterHandler());
bind(IQAuthHandler.class).toInstance(server.getIQAuthHandler());
bind(PluginManager.class).toInstance(server.getPluginManager());
bind(PubSubModule.class).toInstance(server.getPubSubModule());
bind(SessionManager.class).toInstance(server.getSessionManager());
bind(PresenceUpdateHandler.class).toInstance(server.getPresenceUpdateHandler());
bind(PresenceSubscribeHandler.class).toInstance(server.getPresenceSubscribeHandler());
bind(IQRouter.class).toInstance(server.getIQRouter());
bind(MessageRouter.class).toInstance(server.getMessageRouter());
bind(PresenceRouter.class).toInstance(server.getPresenceRouter());
bind(MulticastRouter.class).toInstance(server.getMulticastRouter());
bind(UserManager.class).toInstance(server.getUserManager());
bind(UpdateManager.class).toInstance(server.getUpdateManager());
bind(AuditManager.class).toInstance(server.getAuditManager());
bind(IQDiscoItemsHandler.class).toInstance(server.getIQDiscoItemsHandler());
bind(PrivateStorage.class).toInstance(server.getPrivateStorage());
bind(MultiUserChatServer.class).toInstance(server.getMultiUserChatServer());
bind(AdHocCommandHandler.class).toInstance(server.getAdHocCommandHandler());
bind(FileTransferProxy.class).toInstance(server.getFileTransferProxy());
bind(FileTransferManager.class).toInstance(server.getFileTransferManager());
bind(MediaProxyService.class).toInstance(server.getMediaProxyService());
bind(STUNService.class).toInstance(server.getSTUNService());
bind(VCardManager.class).toInstance(server.getVCardManager());
bind(GroupManager.class).toInstance(GroupManager.getInstance());
bind(RemoteSessionLocator.class).toProvider(new Provider<RemoteSessionLocator>() {
private XMPPServer xmppServer;
@Inject
public void setXMPPServer(XMPPServer xmppServer) {
this.xmppServer = xmppServer;
}
public RemoteSessionLocator get() {
return xmppServer.getRemoteSessionLocator();
}
});
bind(ComponentManager.class).toProvider(new Provider<ComponentManager>() {
public ComponentManager get() {
return ComponentManagerFactory.getComponentManager();
}
});
}
private Logger getLogger() {
return new Logger() {
public void error(String msg) {
Log.error(msg);
}
public void error(String msg, Throwable throwable) {
Log.error(msg, throwable);
}
public void error(Throwable throwable) {
Log.error(throwable);
}
public void warn(String msg) {
Log.warn(msg);
}
public void warn(String msg, Throwable throwable) {
Log.warn(msg, throwable);
}
public void warn(Throwable throwable) {
Log.warn(throwable);
}
public void info(String msg) {
Log.info(msg);
}
public void info(String msg, Throwable throwable) {
Log.info(msg, throwable);
}
public void info(Throwable throwable) {
Log.info(throwable);
}
public void debug(String msg) {
Log.debug(msg);
}
public void debug(String msg, Throwable throwable) {
Log.debug(msg, throwable);
}
public void debug(Throwable throwable) {
Log.debug(throwable);
}
};
}
}
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.container.plugin;
import com.google.inject.Inject;
import java.io.File;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Callable;
import java.util.List;
import java.util.ArrayList;
import org.jivesoftware.util.Logger;
import org.jivesoftware.util.PropertyEventListener;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.xmpp.component.ComponentManager;
import org.xmpp.component.Component;
import org.xmpp.component.ComponentException;
import org.xmpp.packet.JID;
import org.jivesoftware.openfire.component.ComponentLifecycle;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
/**
*
*/
public abstract class AbstractPlugin implements Plugin {
private final List<FutureTask> destroyTasks = new ArrayList<FutureTask>();
protected ComponentManager componentManager;
protected Logger log;
protected String serverName;
@Inject
public void setComponentManager(ComponentManager componentManager) {
this.componentManager = componentManager;
}
@Inject
public void setLog(Logger log) {
this.log = log;
}
@Inject
public void setServerName(@ServerName String serverName) {
this.serverName = serverName;
}
protected JID createJid(String username) {
return new JID(username, serverName, null);
}
protected ComponentLifecycle addComponent(String domain, Component component)
throws ComponentException
{
return addComponent(domain, component, null);
}
protected ComponentLifecycle addComponent(final String domain, Component component,
String jiveProperty) throws ComponentException
{
ComponentLifecycle componentLifecycle
= componentManager.addComponent(domain, component, jiveProperty);
destroyTasks.add(new FutureTask<Boolean>(new Callable<Boolean>() {
public Boolean call() throws Exception {
componentManager.removeComponent(domain);
return true;
}
}));
return componentLifecycle;
}
protected void addPropertyEventListener(final PropertyEventListener listener) {
PropertyEventDispatcher.addListener(listener);
destroyTasks.add(new FutureTask<Boolean>(new Runnable() {
public void run() {
PropertyEventDispatcher.removeListener(listener);
}
}, Boolean.TRUE));
}
public final void initializePlugin(PluginManager manager, File pluginDirectory) {
initialize();
}
public abstract void initialize();
public final void destroyPlugin() {
destroy();
for(FutureTask destroyTask : destroyTasks) {
try {
destroyTask.run();
}
catch(Exception ex) {
log.error("Error unloading plugin", ex);
}
}
}
public void destroy() {
}
}
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.container.plugin;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* Binding for a plugin description.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface PluginDescription {
}
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.container.plugin;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* Binding for a plugin directory.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface PluginDir {
}
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.container.plugin;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* Binding for a plugin name.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface PluginName {
}
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.container.plugin;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* The name, or domain, of the server.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface ServerName {
}
...@@ -62,38 +62,34 @@ import java.util.Iterator; ...@@ -62,38 +62,34 @@ import java.util.Iterator;
*/ */
public class IQvCardHandler extends IQHandler { public class IQvCardHandler extends IQHandler {
private final IQHandlerInfo info = new IQHandlerInfo("vCard", "vcard-temp"); private IQHandlerInfo info;
private XMPPServer server; private XMPPServer server;
private UserManager userManager; private UserManager userManager;
private VCardManager vCardManager;
public IQvCardHandler() { public IQvCardHandler() {
super("XMPP vCard Handler"); super("XMPP vCard Handler");
info = new IQHandlerInfo("vCard", "vcard-temp");
} }
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException { public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
IQ result = IQ.createResultIQ(packet); IQ result = IQ.createResultIQ(packet);
IQ.Type type = packet.getType(); IQ.Type type = packet.getType();
if (type.equals(IQ.Type.set)) { if (type.equals(IQ.Type.set)) {
JID userJid = packet.getFrom(); try {
if(vCardManager.isReadOnly() || !server.isLocal(userJid)) { User user = userManager.getUser(packet.getFrom().getNode());
Element vcard = packet.getChildElement();
if (vcard != null) {
VCardManager.getInstance().setVCard(user.getUsername(), vcard);
}
}
catch (UserNotFoundException e) {
result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy()); result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.not_allowed); result.setError(PacketError.Condition.item_not_found);
} }
else { catch (Exception e) {
try { Log.error(e);
User user = userManager.getUser(userJid.getNode()); result.setError(PacketError.Condition.internal_server_error);
Element vcard = packet.getChildElement();
vCardManager.setVCard(user.getUsername(), vcard);
}
catch (UserNotFoundException e) {
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.item_not_found);
}
catch (Exception e) {
Log.error("Error setting user vCard: " + packet.getFrom().getNode(), e);
result.setError(PacketError.Condition.internal_server_error);
}
} }
} }
else if (type.equals(IQ.Type.get)) { else if (type.equals(IQ.Type.get)) {
...@@ -107,7 +103,8 @@ public class IQvCardHandler extends IQHandler { ...@@ -107,7 +103,8 @@ public class IQvCardHandler extends IQHandler {
// Only try to get the vCard values of non-anonymous users // Only try to get the vCard values of non-anonymous users
if (recipient != null) { if (recipient != null) {
if (recipient.getNode() != null && server.isLocal(recipient)) { if (recipient.getNode() != null && server.isLocal(recipient)) {
Element userVCard = vCardManager.getVCard(recipient.getNode()); VCardManager vManager = VCardManager.getInstance();
Element userVCard = vManager.getVCard(recipient.getNode());
if (userVCard != null) { if (userVCard != null) {
// Check if the requester wants to ignore some vCard's fields // Check if the requester wants to ignore some vCard's fields
Element filter = packet.getChildElement() Element filter = packet.getChildElement()
...@@ -146,7 +143,6 @@ public class IQvCardHandler extends IQHandler { ...@@ -146,7 +143,6 @@ public class IQvCardHandler extends IQHandler {
super.initialize(server); super.initialize(server);
this.server = server; this.server = server;
userManager = server.getUserManager(); userManager = server.getUserManager();
this.vCardManager = server.getVCardManager();
} }
public IQHandlerInfo getInfo() { public IQHandlerInfo getInfo() {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* $Revision: 1217 $ * $Revision: 1217 $
* $Date: 2005-04-11 14:11:06 -0700 (Mon, 11 Apr 2005) $ * $Date: 2005-04-11 14:11:06 -0700 (Mon, 11 Apr 2005) $
* *
* Copyright (C) 2007 Jive Software. All rights reserved. * Copyright (C) 2005 Jive Software. All rights reserved.
* *
* This software is published under the terms of the GNU Public License (GPL), * This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution. * a copy of which is included in this distribution.
...@@ -17,7 +17,6 @@ import org.dom4j.Node; ...@@ -17,7 +17,6 @@ import org.dom4j.Node;
import org.jivesoftware.util.*; import org.jivesoftware.util.*;
import org.jivesoftware.openfire.vcard.VCardManager; import org.jivesoftware.openfire.vcard.VCardManager;
import org.jivesoftware.openfire.vcard.VCardProvider; import org.jivesoftware.openfire.vcard.VCardProvider;
import org.jivesoftware.openfire.XMPPServer;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import javax.naming.directory.Attributes; import javax.naming.directory.Attributes;
...@@ -100,17 +99,11 @@ import java.util.*; ...@@ -100,17 +99,11 @@ import java.util.*;
*/ */
public class LdapVCardProvider implements VCardProvider, PropertyEventListener { public class LdapVCardProvider implements VCardProvider, PropertyEventListener {
private final LdapManager manager; private LdapManager manager;
private final VCardManager vCardManager;
private VCardTemplate template; private VCardTemplate template;
public LdapVCardProvider() { public LdapVCardProvider() {
this(LdapManager.getInstance(), XMPPServer.getInstance().getVCardManager()); manager = LdapManager.getInstance();
}
public LdapVCardProvider(LdapManager ldapManager, VCardManager vCardManager) {
manager = ldapManager;
this.vCardManager = vCardManager;
initTemplate(); initTemplate();
// Listen to property events so that the template is always up to date // Listen to property events so that the template is always up to date
PropertyEventDispatcher.addListener(this); PropertyEventDispatcher.addListener(this);
...@@ -195,11 +188,6 @@ public class LdapVCardProvider implements VCardProvider, PropertyEventListener { ...@@ -195,11 +188,6 @@ public class LdapVCardProvider implements VCardProvider, PropertyEventListener {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public void setVCard(String username, Element vCardElement) {
throw new UnsupportedOperationException();
}
public void deleteVCard(String username) { public void deleteVCard(String username) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
...@@ -221,7 +209,7 @@ public class LdapVCardProvider implements VCardProvider, PropertyEventListener { ...@@ -221,7 +209,7 @@ public class LdapVCardProvider implements VCardProvider, PropertyEventListener {
if ("ldap.vcard-mapping".equals(property)) { if ("ldap.vcard-mapping".equals(property)) {
initTemplate(); initTemplate();
// Reset cache of vCards // Reset cache of vCards
vCardManager.reset(); VCardManager.getInstance().reset();
} }
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* $Revision: 3062 $ * $Revision: 3062 $
* $Date: 2005-11-11 13:26:30 -0300 (Fri, 11 Nov 2005) $ * $Date: 2005-11-11 13:26:30 -0300 (Fri, 11 Nov 2005) $
* *
* Copyright (C) 2007 Jive Software. All rights reserved. * Copyright (C) 2004 Jive Software. All rights reserved.
* *
* This software is published under the terms of the GNU Public License (GPL), * This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution. * a copy of which is included in this distribution.
...@@ -15,8 +15,8 @@ import org.dom4j.Element; ...@@ -15,8 +15,8 @@ import org.dom4j.Element;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
import org.jivesoftware.database.DbConnectionManager; import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.AlreadyExistsException; import org.jivesoftware.util.AlreadyExistsException;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.NotFoundException; import org.jivesoftware.util.NotFoundException;
import org.jivesoftware.util.InternalServerErrorException;
import java.io.StringReader; import java.io.StringReader;
import java.sql.Connection; import java.sql.Connection;
...@@ -27,30 +27,32 @@ import java.util.concurrent.BlockingQueue; ...@@ -27,30 +27,32 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
/** /**
* Default implementation of the VCardProvider interface, which reads and writes data from the * Default implementation of the VCardProvider interface, which reads and writes data
* <tt>jiveVCard</tt> database table. * from the <tt>jiveVCard</tt> database table.
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class DefaultVCardProvider implements VCardProvider { public class DefaultVCardProvider implements VCardProvider {
private static final String LOAD_PROPERTIES = private static final String LOAD_PROPERTIES =
"SELECT value FROM jiveVCard WHERE username=?"; "SELECT value FROM jiveVCard WHERE username=?";
private static final String DELETE_PROPERTIES = private static final String DELETE_PROPERTIES =
"DELETE FROM jiveVCard WHERE username=?"; "DELETE FROM jiveVCard WHERE username=?";
private static final String UPDATE_PROPERTIES = private static final String UPDATE_PROPERTIES =
"UPDATE jiveVCard SET value=? WHERE username=?"; "UPDATE jiveVCard SET value=? WHERE username=?";
private static final String INSERT_PROPERTY = private static final String INSERT_PROPERTY =
"INSERT INTO jiveVCard (username, value) VALUES (?, ?)"; "INSERT INTO jiveVCard (username, value) VALUES (?, ?)";
/** Pool of SAX Readers. SAXReader is not thread safe so we need to have a pool of readers. */ /**
* Pool of SAX Readers. SAXReader is not thread safe so we need to have a pool of readers.
*/
private BlockingQueue<SAXReader> xmlReaders = new LinkedBlockingQueue<SAXReader>(); private BlockingQueue<SAXReader> xmlReaders = new LinkedBlockingQueue<SAXReader>();
public DefaultVCardProvider() { public DefaultVCardProvider() {
super(); super();
// Initialize the pool of sax readers // Initialize the pool of sax readers
for (int i = 0; i < 10; i++) { for (int i=0; i<10; i++) {
SAXReader xmlReader = new SAXReader(); SAXReader xmlReader = new SAXReader();
xmlReader.setEncoding("UTF-8"); xmlReader.setEncoding("UTF-8");
xmlReaders.add(xmlReader); xmlReaders.add(xmlReader);
...@@ -58,40 +60,44 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -58,40 +60,44 @@ public class DefaultVCardProvider implements VCardProvider {
} }
public Element loadVCard(String username) { public Element loadVCard(String username) {
Element vCardElement = null; synchronized (username.intern()) {
java.sql.Connection con = null; Element vCardElement = null;
PreparedStatement pstmt = null; java.sql.Connection con = null;
SAXReader xmlReader = null; PreparedStatement pstmt = null;
ResultSet rs = null; SAXReader xmlReader = null;
try { try {
// Get a sax reader from the pool // Get a sax reader from the pool
xmlReader = xmlReaders.take(); xmlReader = xmlReaders.take();
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setString(1, username); pstmt.setString(1, username);
rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
vCardElement = vCardElement =
xmlReader.read(new StringReader(rs.getString(1))).getRootElement(); xmlReader.read(new StringReader(rs.getString(1))).getRootElement();
}
} }
} catch (Exception e) {
catch (Exception e) { Log.error("Error loading vCard of username: " + username, e);
throw new InternalServerErrorException("Error loading vCard of username: " }
+ username, e); finally {
} // Return the sax reader to the pool
finally { if (xmlReader != null) {
// Return the sax reader to the pool xmlReaders.add(xmlReader);
if (xmlReader != null) { }
xmlReaders.add(xmlReader); try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
DbConnectionManager.closeConnection(rs, pstmt, con); return vCardElement;
} }
return vCardElement;
} }
public void createVCard(String username, Element vCardElement) throws AlreadyExistsException { public void createVCard(String username, Element vCardElement) throws AlreadyExistsException {
if (username == null || vCardElement == null) { if (loadVCard(username) != null) {
throw new NullPointerException("Parameters cannot be null."); // The user already has a vCard
throw new AlreadyExistsException("Username " + username + " already has a vCard");
} }
Connection con = null; Connection con = null;
...@@ -104,18 +110,20 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -104,18 +110,20 @@ public class DefaultVCardProvider implements VCardProvider {
pstmt.executeUpdate(); pstmt.executeUpdate();
} }
catch (SQLException e) { catch (SQLException e) {
throw new InternalServerErrorException("Error creating vCard for username: " Log.error("Error creating vCard for username: " + username, e);
+ username, e);
} }
finally { finally {
DbConnectionManager.closeConnection(pstmt, con); try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
public void updateVCard(String username, Element vCardElement) throws NotFoundException { public void updateVCard(String username, Element vCardElement) throws NotFoundException {
if (username == null || vCardElement == null) { if (loadVCard(username) == null) {
throw new NullPointerException("Parameters cannot be null."); // The user already has a vCard
throw new NotFoundException("Username " + username + " does not have a vCard");
} }
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
...@@ -127,43 +135,17 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -127,43 +135,17 @@ public class DefaultVCardProvider implements VCardProvider {
pstmt.executeUpdate(); pstmt.executeUpdate();
} }
catch (SQLException e) { catch (SQLException e) {
throw new InternalServerErrorException("Error updating vCard of username: " Log.error("Error updating vCard of username: " + username, e);
+ username, e);
} }
finally { finally {
DbConnectionManager.closeConnection(pstmt, con); try { if (pstmt != null) { pstmt.close(); } }
} catch (Exception e) { Log.error(e); }
} try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
public void setVCard(String username, Element vCardElement) {
if (username == null || vCardElement == null) {
throw new NullPointerException("Parameters cannot be null.");
}
Element oldVcard = loadVCard(username);
try {
if (oldVcard == null) {
createVCard(username, vCardElement);
}
else {
updateVCard(username, vCardElement);
}
}
catch (AlreadyExistsException e) {
throw new InternalServerErrorException("Unable to set vCard for user:"
+ username, e);
}
catch (NotFoundException e) {
throw new InternalServerErrorException("Unable to set vCard for user:"
+ username, e);
} }
} }
public void deleteVCard(String username) { public void deleteVCard(String username) {
if (username == null) {
throw new NullPointerException("Username cannot be null.");
}
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
...@@ -173,11 +155,13 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -173,11 +155,13 @@ public class DefaultVCardProvider implements VCardProvider {
pstmt.executeUpdate(); pstmt.executeUpdate();
} }
catch (SQLException e) { catch (SQLException e) {
throw new InternalServerErrorException("Error deleting vCard of username: " Log.error("Error deleting vCard of username: " + username, e);
+ username, e);
} }
finally { finally {
DbConnectionManager.closeConnection(pstmt, con); try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
......
...@@ -23,7 +23,6 @@ public interface VCardListener { ...@@ -23,7 +23,6 @@ public interface VCardListener {
* A vCard was created. * A vCard was created.
* *
* @param user the user for which the vCard was created. * @param user the user for which the vCard was created.
* @deprecated use {@link #vCardSet(String)}
*/ */
public void vCardCreated(String user); public void vCardCreated(String user);
...@@ -31,17 +30,9 @@ public interface VCardListener { ...@@ -31,17 +30,9 @@ public interface VCardListener {
* A vCard was updated. * A vCard was updated.
* *
* @param user the user for which the vCard was updated. * @param user the user for which the vCard was updated.
* @deprecated use {@link #vCardSet(String)}
*/ */
public void vCardUpdated(String user); public void vCardUpdated(String user);
/**
* A vCard was set.
*
* @param username the user for which the vCard was set
*/
public void vCardSet(String username);
/** /**
* A vCard was deleted. * A vCard was deleted.
* *
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* $Revision: 1651 $ * $Revision: 1651 $
* $Date: 2005-07-20 00:20:39 -0300 (Wed, 20 Jul 2005) $ * $Date: 2005-07-20 00:20:39 -0300 (Wed, 20 Jul 2005) $
* *
* Copyright (C) 2007 Jive Software. All rights reserved. * Copyright (C) 2004 Jive Software. All rights reserved.
* *
* This software is published under the terms of the GNU Public License (GPL), * This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution. * a copy of which is included in this distribution.
...@@ -36,7 +36,7 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -36,7 +36,7 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
private static VCardManager instance; private static VCardManager instance;
private Cache<String, Element> vcardCache; private Cache<String, Element> vcardCache;
private final EventHandler eventHandler = new EventHandler(); private EventHandler eventHandler;
/** /**
* List of listeners that will be notified when vCards are created, updated or deleted. * List of listeners that will be notified when vCards are created, updated or deleted.
*/ */
...@@ -62,6 +62,7 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -62,6 +62,7 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
super("VCard Manager"); super("VCard Manager");
String cacheName = "VCard"; String cacheName = "VCard";
vcardCache = CacheFactory.createCache(cacheName); vcardCache = CacheFactory.createCache(cacheName);
this.eventHandler = new EventHandler();
} }
/** /**
...@@ -113,16 +114,51 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -113,16 +114,51 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
* *
* @param username The username of the user to set his new vCard. * @param username The username of the user to set his new vCard.
* @param vCardElement The DOM element sent by the user as his new vcard. * @param vCardElement The DOM element sent by the user as his new vcard.
* @throws Exception if an error occured while storing the new vCard.
*/ */
public void setVCard(String username, Element vCardElement) { public void setVCard(String username, Element vCardElement) throws Exception {
boolean created = false;
boolean updated = false;
if (provider.isReadOnly()) { if (provider.isReadOnly()) {
throw new UnsupportedOperationException("VCard provider is read-only."); throw new UnsupportedOperationException("VCard provider is read-only.");
} }
synchronized (getLock(username)) { Element oldVCard = getOrLoadVCard(username);
provider.setVCard(username, vCardElement); // See if we need to update the vCard or insert a new one.
vcardCache.put(username, vCardElement); if (oldVCard != null) {
// Only update the vCard in the database if the vCard has changed.
if (!oldVCard.equals(vCardElement)) {
try {
provider.updateVCard(username, vCardElement);
updated = true;
}
catch (NotFoundException e) {
Log.warn("Tried to update a vCard that does not exist", e);
provider.createVCard(username, vCardElement);
created = true;
}
}
}
else {
try {
provider.createVCard(username, vCardElement);
created = true;
}
catch (AlreadyExistsException e) {
Log.warn("Tried to create a vCard when one already exist", e);
provider.updateVCard(username, vCardElement);
updated = true;
}
}
vcardCache.put(username, vCardElement);
// Dispatch vCard events
if (created) {
// Alert listeners that a new vCard has been created
dispatchVCardCreated(username);
} else if (updated) {
// Alert listeners that a vCard has been updated
dispatchVCardUpdated(username);
} }
dispatchVCardSet(username);
} }
/** /**
...@@ -136,17 +172,11 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -136,17 +172,11 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
if (provider.isReadOnly()) { if (provider.isReadOnly()) {
throw new UnsupportedOperationException("VCard provider is read-only."); throw new UnsupportedOperationException("VCard provider is read-only.");
} }
boolean deleted = false; Element oldVCard = getOrLoadVCard(username);
synchronized (getLock(username)) { if (oldVCard != null) {
Element oldVCard = getOrLoadVCard(username); vcardCache.remove(username);
if (oldVCard != null) { // Delete the property from the DB if it was present in memory
// Delete the property from the DB if it was present in memory provider.deleteVCard(username);
provider.deleteVCard(username);
vcardCache.remove(username);
deleted = true;
}
}
if (deleted) {
// Alert listeners that a vCard has been deleted // Alert listeners that a vCard has been deleted
dispatchVCardDeleted(username); dispatchVCardDeleted(username);
} }
...@@ -158,7 +188,6 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -158,7 +188,6 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
* read-only vCard. * read-only vCard.
* *
* @return the vCard of a given user. * @return the vCard of a given user.
* @param username the username of the user whose vCard is being retrieved.
*/ */
public Element getVCard(String username) { public Element getVCard(String username) {
Element vCardElement = getOrLoadVCard(username); Element vCardElement = getOrLoadVCard(username);
...@@ -166,20 +195,14 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -166,20 +195,14 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
} }
private Element getOrLoadVCard(String username) { private Element getOrLoadVCard(String username) {
synchronized (getLock(username)) { Element vCardElement = vcardCache.get(username);
Element vCardElement = vcardCache.get(username); if (vCardElement == null) {
if (vCardElement == null) { vCardElement = provider.loadVCard(username);
vCardElement = provider.loadVCard(username); if (vCardElement != null) {
if (vCardElement != null) { vcardCache.put(username, vCardElement);
vcardCache.put(username, vCardElement);
}
} }
return vCardElement;
} }
} return vCardElement;
private Object getLock(String username) {
return (username + VCardManager.class.toString()).intern();
} }
/** /**
...@@ -196,7 +219,7 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -196,7 +219,7 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
/** /**
* Unregisters a listener to receive events. * Unregisters a listener to receive events.
* *
* @param listener the listener. * @param listener the listener.
*/ */
public void removeListener(VCardListener listener) { public void removeListener(VCardListener listener) {
...@@ -204,23 +227,24 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider ...@@ -204,23 +227,24 @@ public class VCardManager extends BasicModule implements ServerFeaturesProvider
} }
/** /**
* Returns true if vCards are read-only. When vCards are read-only and #setVCard is called an * Dispatches that a vCard was updated to all listeners.
* unsupported operation exception is thrown. *
* * @param user the user for which the vCard was set.
* @return true if vCards are read-only.
*/ */
public boolean isReadOnly() { private void dispatchVCardUpdated(String user) {
return provider.isReadOnly(); for (VCardListener listener : listeners) {
listener.vCardUpdated(user);
}
} }
/** /**
* Dispatches that a vCard was updated to all listeners. * Dispatches that a vCard was created to all listeners.
* *
* @param user the user for which the vCard was set. * @param user the user for which the vCard was created.
*/ */
private void dispatchVCardSet(String user) { private void dispatchVCardCreated(String user) {
for (VCardListener listener : listeners) { for (VCardListener listener : listeners) {
listener.vCardSet(user); listener.vCardCreated(user);
} }
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* $Revision: 1651 $ * $Revision: 1651 $
* $Date: 2005-07-20 00:20:39 -0300 (Wed, 20 Jul 2005) $ * $Date: 2005-07-20 00:20:39 -0300 (Wed, 20 Jul 2005) $
* *
* Copyright (C) 2007 Jive Software. All rights reserved. * Copyright (C) 2004 Jive Software. All rights reserved.
* *
* This software is published under the terms of the GNU Public License (GPL), * This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution. * a copy of which is included in this distribution.
...@@ -41,7 +41,6 @@ public interface VCardProvider { ...@@ -41,7 +41,6 @@ public interface VCardProvider {
* @throws AlreadyExistsException if the user already has a vCard. * @throws AlreadyExistsException if the user already has a vCard.
* @throws UnsupportedOperationException if the provider does not support the * @throws UnsupportedOperationException if the provider does not support the
* operation. * operation.
* @deprecated use {@link #setVCard(String, org.dom4j.Element)}
*/ */
void createVCard(String username, Element vCardElement) throws AlreadyExistsException; void createVCard(String username, Element vCardElement) throws AlreadyExistsException;
...@@ -55,18 +54,9 @@ public interface VCardProvider { ...@@ -55,18 +54,9 @@ public interface VCardProvider {
* @throws NotFoundException if the vCard to update does not exist. * @throws NotFoundException if the vCard to update does not exist.
* @throws UnsupportedOperationException if the provider does not support the * @throws UnsupportedOperationException if the provider does not support the
* operation. * operation.
* @deprecated use {@link #setVCard(String, org.dom4j.Element)}
*/ */
void updateVCard(String username, Element vCardElement) throws NotFoundException; void updateVCard(String username, Element vCardElement) throws NotFoundException;
/**
* Sets the vCard of the user to the passed in element.
*
* @param username the username of the user whose vCard is being set
* @param vCardElement the vCard being set.
*/
void setVCard(String username, Element vCardElement);
/** /**
* Delets a user vcard. This method should throw an UnsupportedOperationException * Delets a user vcard. This method should throw an UnsupportedOperationException
* if this operation is not supported by the backend vcard store. * if this operation is not supported by the backend vcard store.
......
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.plugin;
import org.jmock.lib.legacy.ClassImposteriser;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.Mockery;
import org.jmock.Expectations;
import org.jivesoftware.openfire.plugin.AbstractPluginSupport;
import org.jivesoftware.openfire.plugin.BroadcastPlugin;
import org.jivesoftware.openfire.container.plugin.PluginName;
import org.jivesoftware.openfire.container.plugin.PluginDescription;
import org.jivesoftware.openfire.component.ComponentLifecycle;
import org.jivesoftware.util.JiveProperties;
import org.junit.Test;
import org.xmpp.component.ComponentManager;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.AbstractModule;
import static org.junit.Assert.*;
/**
*
*/
public class BroadcastPluginTest {
private static final Mockery context = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
private static final Module pluginModule = new AbstractModule() {
protected void configure() {
// not a singleton so we get a fresh one every time.
bind(BroadcastPlugin.class);
bind(String.class).annotatedWith(PluginName.class).toInstance("broadcast");
bind(String.class).annotatedWith(PluginDescription.class)
.toInstance("the broadcast plugin");
}
};
private static final Injector injector = AbstractPluginSupport.createInjector(context,
pluginModule);
@Test
public void testPluginInitialize() {
final BroadcastPlugin broadcastPlugin = injector.getInstance(BroadcastPlugin.class);
final ComponentLifecycle componentLifecycle = context.mock(ComponentLifecycle.class);
context.checking(new Expectations() {{
try {
allowing(injector.getInstance(JiveProperties.class))
.getProperty("plugin.broadcast.serviceName", "broadcast");
will(returnValue("broadcast"));
allowing(injector.getInstance(ComponentManager.class))
.addComponent("broadcast", broadcastPlugin, null);
will(returnValue(componentLifecycle));
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}});
broadcastPlugin.initialize();
}
@Test
public void testNameAndDescription() {
BroadcastPlugin broadcastPlugin = injector.getInstance(BroadcastPlugin.class);
assertEquals(broadcastPlugin.getName(), "broadcast");
assertEquals(broadcastPlugin.getDescription(), "the broadcast plugin");
}
@Test
public void testGetAndSetProperties() {
final BroadcastPlugin broadcastPlugin = injector.getInstance(BroadcastPlugin.class);
final JiveProperties jiveProperties = injector.getInstance(JiveProperties.class);
context.checking(new Expectations() {{
allowing(jiveProperties).put("plugin.broadcast.serviceName", "broadcast");
allowing(jiveProperties)
.getProperty("plugin.broadcast.serviceName", "broadcast");
will(returnValue("broadcast"));
}});
broadcastPlugin.setServiceName("broadcast");
assertEquals(broadcastPlugin.getServiceName(), "broadcast");
}
}
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.handler;
import org.jmock.Mockery;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.integration.junit4.JMock;
import org.junit.runner.RunWith;
import org.junit.BeforeClass;
import org.junit.Test;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.PacketDeliverer;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.openfire.vcard.VCardManager;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import static org.junit.Assert.*;
import org.dom4j.Element;
/**
* Test for the IQvCardHandler.
*/
@RunWith(JMock.class)
public class IQvCardHandlerTest {
private static Mockery context = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
private static XMPPServer xmppServer;
private static UserManager userManager;
private static PacketDeliverer packetDeliverer;
private static SessionManager sessionManager;
@BeforeClass
public static void doBeforeClass() {
xmppServer = context.mock(XMPPServer.class);
userManager = context.mock(UserManager.class);
packetDeliverer = context.mock(PacketDeliverer.class);
sessionManager = context.mock(SessionManager.class);
context.checking(new Expectations() {{
allowing(xmppServer).getUserManager();
will(returnValue(userManager));
allowing(xmppServer).getPacketDeliverer();
will(returnValue(packetDeliverer));
allowing(xmppServer).getSessionManager();
will(returnValue(sessionManager));
}});
}
@Test
public void testVCardHandlerSetReadOnly() throws UnauthorizedException {
IQ vCardSet = new IQ(IQ.Type.set);
vCardSet.setChildElement("vCard", "vcard-temp");
final VCardManager vCardManager = context.mock(VCardManager.class);
IQvCardHandler vCardHandler = new IQvCardHandler();
context.checking(new Expectations() {{
one(xmppServer).getVCardManager();
will(returnValue(vCardManager));
one(vCardManager).isReadOnly();
will(returnValue(true));
}});
vCardHandler.initialize(xmppServer);
IQ result = vCardHandler.handleIQ(vCardSet);
assertEquals(result.getType(), IQ.Type.error);
}
@Test
public void testVCardHandlerSetUserNotLocal() throws UnauthorizedException {
IQ vCardSet = new IQ(IQ.Type.set);
vCardSet.setFrom("test@example.com");
vCardSet.setChildElement("vCard", "vcard-temp");
final VCardManager vCardManager = context.mock(VCardManager.class);
IQvCardHandler vCardHandler = new IQvCardHandler();
context.checking(new Expectations() {{
one(xmppServer).getVCardManager();
will(returnValue(vCardManager));
one(vCardManager).isReadOnly();
will(returnValue(false));
one(xmppServer).isLocal(with(a(JID.class)));
will(returnValue(false));
}});
vCardHandler.initialize(xmppServer);
IQ result = vCardHandler.handleIQ(vCardSet);
assertEquals(result.getType(), IQ.Type.error);
}
@Test
public void testVCardHandlerSet() throws UnauthorizedException {
IQ vCardSet = new IQ(IQ.Type.set);
vCardSet.setFrom("test@test.com");
vCardSet.setChildElement("vCard", "vcard-temp");
final Element vCardElement = vCardSet.getChildElement();
final VCardManager vCardManager = context.mock(VCardManager.class);
final User user = context.mock(User.class);
final IQvCardHandler vCardHandler = new IQvCardHandler();
context.checking(new Expectations() {{
one(xmppServer).getVCardManager();
will(returnValue(vCardManager));
one(vCardManager).isReadOnly();
will(returnValue(false));
one(xmppServer).isLocal(new JID("test@test.com"));
will(returnValue(true));
try {
one(userManager).getUser(with(a(String.class)));
}
catch (UserNotFoundException e) {
fail("User not found");
}
will(returnValue(user));
one(user).getUsername();
will(returnValue("test"));
try {
one(vCardManager).setVCard("test", vCardElement);
}
catch (Exception e) {
fail("Vcard exception");
}
}});
vCardHandler.initialize(xmppServer);
IQ result = vCardHandler.handleIQ(vCardSet);
assertEquals(result.getType(), IQ.Type.result);
}
//
// @Test
// public void testVCardHandlerGetNotFound() {
// IQ vCardSet = new IQ(IQ.Type.get);
// vCardSet.setFrom("test@test.com");
// vCardSet.setChildElement("vCard", "vcard-temp");
//
// context.checking(new Expectations(){{
// one(xmppServer).isLocal(new JID("test@test.com"));
// will(returnValue(true));
// }});
// }
}
/**
* $Revision:$
* $Date:$
*
* Copyright (C) 2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.openfire.plugin;
import com.google.inject.*;
import org.jivesoftware.util.JiveProperties;
import org.jivesoftware.util.Logger;
import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.session.RemoteSessionLocator;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.vcard.VCardManager;
import org.jivesoftware.openfire.stun.STUNService;
import org.jivesoftware.openfire.mediaproxy.MediaProxyService;
import org.jivesoftware.openfire.filetransfer.proxy.FileTransferProxy;
import org.jivesoftware.openfire.filetransfer.FileTransferManager;
import org.jivesoftware.openfire.commands.AdHocCommandHandler;
import org.jivesoftware.openfire.muc.MultiUserChatServer;
import org.jivesoftware.openfire.disco.IQDiscoItemsHandler;
import org.jivesoftware.openfire.audit.AuditManager;
import org.jivesoftware.openfire.update.UpdateManager;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.pubsub.PubSubModule;
import org.jivesoftware.openfire.handler.IQRegisterHandler;
import org.jivesoftware.openfire.handler.IQAuthHandler;
import org.jivesoftware.openfire.handler.PresenceUpdateHandler;
import org.jivesoftware.openfire.handler.PresenceSubscribeHandler;
import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.openfire.container.plugin.ServerName;
import org.jivesoftware.openfire.container.PluginManager;
import org.xmpp.component.ComponentManager;
import org.jmock.Mockery;
/**
*
*/
public class AbstractPluginSupport {
private AbstractPluginSupport() {
}
public static Injector createInjector(Mockery context) {
return createInjector(context, new Module() {
public void configure(Binder binder) {
}
});
}
public static Injector createInjector(Mockery context, Module pluginModule) {
return Guice.createInjector(Stage.DEVELOPMENT, new MockPluginModule(context), pluginModule);
}
public static class MockPluginModule extends AbstractModule {
private final Mockery context;
public MockPluginModule(Mockery context) {
this.context = context;
}
protected void configure() {
bind(JiveProperties.class).toInstance(context.mock(JiveProperties.class));
bind(Logger.class).toInstance(context.mock(Logger.class));
bind(String.class).annotatedWith(ServerName.class).toProvider(new Provider<String>() {
public String get() {
return "example.com";
}
});
bind(XMPPServer.class).toInstance(context.mock(XMPPServer.class));
bind(ConnectionManager.class).toInstance(context.mock(ConnectionManager.class));
bind(RoutingTable.class).toInstance(context.mock(RoutingTable.class));
bind(PacketDeliverer.class).toInstance(context.mock(PacketDeliverer.class));
bind(RosterManager.class).toInstance(context.mock(RosterManager.class));
bind(PresenceManager.class).toInstance(context.mock(PresenceManager.class));
bind(OfflineMessageStore.class).toInstance(context.mock(OfflineMessageStore.class));
bind(OfflineMessageStrategy.class).toInstance(
context.mock(OfflineMessageStrategy.class));
bind(PacketRouter.class).toInstance(context.mock(PacketRouter.class));
bind(IQRegisterHandler.class).toInstance(context.mock(IQRegisterHandler.class));
bind(IQAuthHandler.class).toInstance(context.mock(IQAuthHandler.class));
bind(PluginManager.class).toInstance(context.mock(PluginManager.class));
bind(PubSubModule.class).toInstance(context.mock(PubSubModule.class));
bind(SessionManager.class).toInstance(context.mock(SessionManager.class));
bind(PresenceUpdateHandler.class).toInstance(context.mock(PresenceUpdateHandler.class));
bind(PresenceSubscribeHandler.class).toInstance(
context.mock(PresenceSubscribeHandler.class));
bind(IQRouter.class).toInstance(context.mock(IQRouter.class));
bind(MessageRouter.class).toInstance(context.mock(MessageRouter.class));
bind(PresenceRouter.class).toInstance(context.mock(PresenceRouter.class));
bind(MulticastRouter.class).toInstance(context.mock(MulticastRouter.class));
bind(UserManager.class).toInstance(context.mock(UserManager.class));
bind(UpdateManager.class).toInstance(context.mock(UpdateManager.class));
bind(AuditManager.class).toInstance(context.mock(AuditManager.class));
bind(IQDiscoItemsHandler.class).toInstance(context.mock(IQDiscoItemsHandler.class));
bind(PrivateStorage.class).toInstance(context.mock(PrivateStorage.class));
bind(MultiUserChatServer.class).toInstance(context.mock(MultiUserChatServer.class));
bind(AdHocCommandHandler.class).toInstance(context.mock(AdHocCommandHandler.class));
bind(FileTransferProxy.class).toInstance(context.mock(FileTransferProxy.class));
bind(FileTransferManager.class).toInstance(context.mock(FileTransferManager.class));
bind(MediaProxyService.class).toInstance(context.mock(MediaProxyService.class));
bind(STUNService.class).toInstance(context.mock(STUNService.class));
bind(VCardManager.class).toInstance(context.mock(VCardManager.class));
bind(GroupManager.class).toInstance(context.mock(GroupManager.class));
bind(RemoteSessionLocator.class).toInstance(context.mock(RemoteSessionLocator.class));
bind(ComponentManager.class).toInstance(context.mock(ComponentManager.class));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment