Commit 7cfc4ad5 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

broadcast plugin is now using IOC

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8598 b35dd754-fafc-0310-a699-88a17e54d16e
parent 9c833a72
...@@ -110,9 +110,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -110,9 +110,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
} }
public void addComponent(String subdomain, Component component) throws ComponentException { public void addComponent(String subdomain, Component component) throws ComponentException {
ComponentLifecycleImpl componentLifecycle addComponent(subdomain, component, null);
= (ComponentLifecycleImpl) addComponent(subdomain, component, null);
startComponent(componentLifecycle.subdomain, componentLifecycle.component);
} }
private void startComponent(String subdomain, Component component) { private void startComponent(String subdomain, Component component) {
...@@ -148,7 +146,8 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -148,7 +146,8 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
} }
public ComponentLifecycle addComponent(String subdomain, Component component, String jiveProperty) public ComponentLifecycle addComponent(String subdomain, Component component,
String jiveProperty)
throws ComponentException throws ComponentException
{ {
ComponentLifecycleImpl componentLifecycle = new ComponentLifecycleImpl(subdomain, component); ComponentLifecycleImpl componentLifecycle = new ComponentLifecycleImpl(subdomain, component);
...@@ -170,6 +169,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -170,6 +169,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
throw e; throw e;
} }
componentLifecycle.setJiveProperty(jiveProperty);
return componentLifecycle; return componentLifecycle;
} }
......
/**
* $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.AbstractModule;
import com.google.inject.Inject;
import java.io.File;
import org.xmpp.component.ComponentManager;
/**
*
*/
public abstract class AbstractPlugin implements Plugin {
protected ComponentManager componentManager;
@Inject
public void setComponentManager(ComponentManager componentManager) {
this.componentManager = componentManager;
}
public void initializePlugin(PluginManager manager, File pluginDirectory) {
initializePlugin();
}
public abstract void initializePlugin();
public void destroyPlugin() {
}
}
...@@ -18,6 +18,10 @@ import org.dom4j.io.SAXReader; ...@@ -18,6 +18,10 @@ import org.dom4j.io.SAXReader;
import org.jivesoftware.admin.AdminConsole; import org.jivesoftware.admin.AdminConsole;
import org.jivesoftware.database.DbConnectionManager; import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.plugin.AbstractPlugin;
import org.jivesoftware.openfire.container.plugin.PluginName;
import org.jivesoftware.openfire.container.plugin.PluginDescription;
import org.jivesoftware.openfire.container.plugin.PluginDir;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.util.Version; import org.jivesoftware.util.Version;
...@@ -33,6 +37,7 @@ import java.util.zip.ZipFile; ...@@ -33,6 +37,7 @@ import java.util.zip.ZipFile;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provider;
/** /**
* Loads and manages plugins. The <tt>plugins</tt> directory is monitored for any * Loads and manages plugins. The <tt>plugins</tt> directory is monitored for any
...@@ -398,7 +403,8 @@ public class PluginManager { ...@@ -398,7 +403,8 @@ public class PluginManager {
moduleClazz = null; moduleClazz = null;
} }
//noinspection unchecked //noinspection unchecked
plugin = instantiateAbstractPlugin(pluginLoader, pluginClazz, moduleClazz); plugin = instantiateAbstractPlugin(pluginDir, pluginLoader, pluginClazz,
moduleClazz);
} }
else { else {
plugin = (Plugin) pluginClazz.newInstance(); plugin = (Plugin) pluginClazz.newInstance();
...@@ -526,7 +532,7 @@ public class PluginManager { ...@@ -526,7 +532,7 @@ public class PluginManager {
} }
} }
private Plugin instantiateAbstractPlugin(PluginClassLoader pluginLoader, private Plugin instantiateAbstractPlugin(final File pluginDir, PluginClassLoader pluginLoader,
final Class<? extends AbstractPlugin> pluginClazz, final Class<? extends AbstractPlugin> pluginClazz,
Class<? extends com.google.inject.Module> moduleClazz) Class<? extends com.google.inject.Module> moduleClazz)
throws IllegalAccessException, InstantiationException throws IllegalAccessException, InstantiationException
...@@ -549,6 +555,28 @@ public class PluginManager { ...@@ -549,6 +555,28 @@ public class PluginManager {
if (pluginModule != null) { if (pluginModule != null) {
install(pluginModule); install(pluginModule);
} }
bind(String.class).annotatedWith(PluginName.class).toProvider(
new Provider<String>() {
public String get() {
return getName(pluginDir);
}
}
);
bind(String.class).annotatedWith(PluginDescription.class).toProvider(
new Provider<String>() {
public String get() {
return getDescription(pluginDir);
}
}
);
bind(File.class).annotatedWith(PluginDir.class).toProvider(
new Provider<File>() {
public File get() {
return pluginDir;
}
}
);
bind(pluginClazz); bind(pluginClazz);
} }
}).getInstance(pluginClazz); }).getInstance(pluginClazz);
...@@ -679,8 +707,12 @@ public class PluginManager { ...@@ -679,8 +707,12 @@ public class PluginManager {
* @return the plugin's name. * @return the plugin's name.
*/ */
public String getName(Plugin plugin) { public String getName(Plugin plugin) {
String name = getElementValue(plugin, "/plugin/name"); return getName(pluginDirs.get(plugin));
String pluginName = pluginDirs.get(plugin).getName(); }
private String getName(File pluginDir) {
String name = getElementValue(pluginDir, "/plugin/name");
String pluginName = pluginDir.getName();
if (name != null) { if (name != null) {
return AdminConsole.getAdminText(name, pluginName); return AdminConsole.getAdminText(name, pluginName);
} }
...@@ -697,8 +729,13 @@ public class PluginManager { ...@@ -697,8 +729,13 @@ public class PluginManager {
* @return the plugin's description. * @return the plugin's description.
*/ */
public String getDescription(Plugin plugin) { public String getDescription(Plugin plugin) {
String pluginName = pluginDirs.get(plugin).getName(); return getDescription(pluginDirs.get(plugin));
return AdminConsole.getAdminText(getElementValue(plugin, "/plugin/description"), pluginName); }
private String getDescription(File pluginDir) {
String pluginName = pluginDir.getName();
return AdminConsole.getAdminText(getElementValue(pluginDirs.get(pluginDir),
"/plugin/description"), pluginName);
} }
/** /**
...@@ -709,7 +746,7 @@ public class PluginManager { ...@@ -709,7 +746,7 @@ public class PluginManager {
* @return the plugin's author. * @return the plugin's author.
*/ */
public String getAuthor(Plugin plugin) { public String getAuthor(Plugin plugin) {
return getElementValue(plugin, "/plugin/author"); return getElementValue(pluginDirs.get(plugin), "/plugin/author");
} }
/** /**
...@@ -720,7 +757,7 @@ public class PluginManager { ...@@ -720,7 +757,7 @@ public class PluginManager {
* @return the plugin's version. * @return the plugin's version.
*/ */
public String getVersion(Plugin plugin) { public String getVersion(Plugin plugin) {
return getElementValue(plugin, "/plugin/version"); return getElementValue(pluginDirs.get(plugin), "/plugin/version");
} }
/** /**
...@@ -731,7 +768,7 @@ public class PluginManager { ...@@ -731,7 +768,7 @@ public class PluginManager {
* @return the plugin's version. * @return the plugin's version.
*/ */
public String getMinServerVersion(Plugin plugin) { public String getMinServerVersion(Plugin plugin) {
return getElementValue(plugin, "/plugin/minServerVersion"); return getElementValue(pluginDirs.get(plugin), "/plugin/minServerVersion");
} }
/** /**
...@@ -743,7 +780,7 @@ public class PluginManager { ...@@ -743,7 +780,7 @@ public class PluginManager {
* @return the plugin's database schema key or <tt>null</tt> if it doesn't exist. * @return the plugin's database schema key or <tt>null</tt> if it doesn't exist.
*/ */
public String getDatabaseKey(Plugin plugin) { public String getDatabaseKey(Plugin plugin) {
return getElementValue(plugin, "/plugin/databaseKey"); return getElementValue(pluginDirs.get(plugin), "/plugin/databaseKey");
} }
/** /**
...@@ -755,7 +792,7 @@ public class PluginManager { ...@@ -755,7 +792,7 @@ public class PluginManager {
* @return the plugin's database schema version or <tt>-1</tt> if it doesn't exist. * @return the plugin's database schema version or <tt>-1</tt> if it doesn't exist.
*/ */
public int getDatabaseVersion(Plugin plugin) { public int getDatabaseVersion(Plugin plugin) {
String versionString = getElementValue(plugin, "/plugin/databaseVersion"); String versionString = getElementValue(pluginDirs.get(plugin), "/plugin/databaseVersion");
if (versionString != null) { if (versionString != null) {
try { try {
return Integer.parseInt(versionString.trim()); return Integer.parseInt(versionString.trim());
...@@ -776,7 +813,7 @@ public class PluginManager { ...@@ -776,7 +813,7 @@ public class PluginManager {
* @return the plugin's license agreement. * @return the plugin's license agreement.
*/ */
public License getLicense(Plugin plugin) { public License getLicense(Plugin plugin) {
String licenseString = getElementValue(plugin, "/plugin/licenseType"); String licenseString = getElementValue(pluginDirs.get(plugin), "/plugin/licenseType");
if (licenseString != null) { if (licenseString != null) {
try { try {
// Attempt to load the get the license type. We lower-case and // Attempt to load the get the license type. We lower-case and
...@@ -806,12 +843,11 @@ public class PluginManager { ...@@ -806,12 +843,11 @@ public class PluginManager {
* Returns the value of an element selected via an xpath expression from * Returns the value of an element selected via an xpath expression from
* a Plugin's plugin.xml file. * a Plugin's plugin.xml file.
* *
* @param plugin the plugin. * @param pluginDir the plugin directory.
* @param xpath the xpath expression. * @param xpath the xpath expression.
* @return the value of the element selected by the xpath expression. * @return the value of the element selected by the xpath expression.
*/ */
private String getElementValue(Plugin plugin, String xpath) { private String getElementValue(File pluginDir, String xpath) {
File pluginDir = pluginDirs.get(plugin);
if (pluginDir == null) { if (pluginDir == null) {
return null; return null;
} }
......
...@@ -9,6 +9,7 @@ package org.jivesoftware.openfire.container; ...@@ -9,6 +9,7 @@ package org.jivesoftware.openfire.container;
import com.google.inject.*; import com.google.inject.*;
import org.jivesoftware.openfire.*; import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.session.RemoteSessionLocator; import org.jivesoftware.openfire.session.RemoteSessionLocator;
import org.jivesoftware.openfire.vcard.VCardManager; import org.jivesoftware.openfire.vcard.VCardManager;
import org.jivesoftware.openfire.stun.STUNService; import org.jivesoftware.openfire.stun.STUNService;
...@@ -28,6 +29,8 @@ import org.jivesoftware.openfire.handler.PresenceUpdateHandler; ...@@ -28,6 +29,8 @@ import org.jivesoftware.openfire.handler.PresenceUpdateHandler;
import org.jivesoftware.openfire.handler.PresenceSubscribeHandler; import org.jivesoftware.openfire.handler.PresenceSubscribeHandler;
import org.jivesoftware.openfire.roster.RosterManager; import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.util.JiveProperties; import org.jivesoftware.util.JiveProperties;
import org.jivesoftware.util.Logger;
import org.jivesoftware.util.Log;
import org.xmpp.component.ComponentManager; import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory; import org.xmpp.component.ComponentManagerFactory;
...@@ -38,6 +41,7 @@ public class PluginModule extends AbstractModule { ...@@ -38,6 +41,7 @@ public class PluginModule extends AbstractModule {
protected void configure() { protected void configure() {
bind(JiveProperties.class).toInstance(JiveProperties.getInstance()); bind(JiveProperties.class).toInstance(JiveProperties.getInstance());
bind(Logger.class).toInstance(getLogger());
XMPPServer server = XMPPServer.getInstance(); XMPPServer server = XMPPServer.getInstance();
bind(XMPPServer.class).toInstance(server); bind(XMPPServer.class).toInstance(server);
...@@ -72,6 +76,7 @@ public class PluginModule extends AbstractModule { ...@@ -72,6 +76,7 @@ public class PluginModule extends AbstractModule {
bind(MediaProxyService.class).toInstance(server.getMediaProxyService()); bind(MediaProxyService.class).toInstance(server.getMediaProxyService());
bind(STUNService.class).toInstance(server.getSTUNService()); bind(STUNService.class).toInstance(server.getSTUNService());
bind(VCardManager.class).toInstance(server.getVCardManager()); bind(VCardManager.class).toInstance(server.getVCardManager());
bind(GroupManager.class).toInstance(GroupManager.getInstance());
bind(RemoteSessionLocator.class).toProvider(new Provider<RemoteSessionLocator>() { bind(RemoteSessionLocator.class).toProvider(new Provider<RemoteSessionLocator>() {
private XMPPServer xmppServer; private XMPPServer xmppServer;
...@@ -91,4 +96,57 @@ public class PluginModule extends AbstractModule { ...@@ -91,4 +96,57 @@ public class PluginModule extends AbstractModule {
} }
}); });
} }
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.Set;
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.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;
@Inject
public void setComponentManager(ComponentManager componentManager) {
this.componentManager = componentManager;
}
@Inject
public void setLog(Logger log) {
this.log = log;
}
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 {
}
...@@ -203,6 +203,30 @@ public class JiveProperties implements Map<String, String> { ...@@ -203,6 +203,30 @@ public class JiveProperties implements Map<String, String> {
return result; return result;
} }
public String getProperty(String name, String defaultValue) {
String value = properties.get(name);
if (value != null) {
return value;
}
else {
return defaultValue;
}
}
public boolean getBooleanProperty(String name) {
return Boolean.valueOf(get(name));
}
public boolean getBooleanProperty(String name, boolean defaultValue) {
String value = get(name);
if (value != null) {
return Boolean.valueOf(value);
}
else {
return defaultValue;
}
}
private void insertProperty(String name, String value) { private void insertProperty(String name, String value) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
......
/**
* Copyright (C) 2004-2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.util;
/**
* A simple logging service for components. Four log levels are provided:<ul>
*
* <li>Error -- an error occured in the component.
* <li>Warn -- a condition occured that an administrator should be warned about.
* <li>Info -- used to send information messages, such as a version or license notice.
* <li>Debug -- used to send debugging information. Most Log implementations will
* disable debug output by default.
* </ul>
*
* Log implementations will attempt use the native logging service of the component host
* server. However, this may not be possible in some cases -- for example, when using an
* external component that is not currently connected to the server.
*
* @author Matt Tucker
*/
public interface Logger {
/**
* Logs an error.
*
* @param message the error message.
*/
public void error(String message);
/**
* Logs an error.
*
* @param message the error message.
* @param throwable the Throwable that caused the error.
*/
public void error(String message, Throwable throwable);
/**
* Logs an error.
*
* @param throwable the Throwable that caused the error.
*/
public void error(Throwable throwable);
/**
* Logs a warning.
*
* @param message the warning message.
*/
public void warn(String message);
/**
* Logs a warning.
*
* @param message the warning message.
* @param throwable the Throwable that caused the error.
*/
public void warn(String message, Throwable throwable);
/**
* Logs a warning.
*
* @param throwable the Throwable that caused the error.
*/
public void warn(Throwable throwable);
/**
* Logs an info message.
*
* @param message the info message.
*/
public void info(String message);
/**
* Logs an info message.
*
* @param message the info message.
* @param throwable the Throwable that caused the info message.
*/
public void info(String message, Throwable throwable);
/**
* Logs an info message.
*
* @param throwable the Throwable that caused the info message.
*/
public void info(Throwable throwable);
/**
* Logs a debug message.
*
* @param message the debug message.
*/
public void debug(String message);
/**
* Logs a debug message.
*
* @param message the debug message.
* @param throwable the Throwable that caused the debug message.
*/
public void debug(String message, Throwable throwable);
/**
* Logs a debug message.
*
* @param throwable the Throwable the caused the debug message.
*/
public void debug(Throwable throwable);
}
\ No newline at end of file
...@@ -14,23 +14,26 @@ package org.jivesoftware.openfire.plugin; ...@@ -14,23 +14,26 @@ package org.jivesoftware.openfire.plugin;
import org.dom4j.Element; import org.dom4j.Element;
import org.jivesoftware.openfire.SessionManager; import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin; import org.jivesoftware.openfire.container.plugin.AbstractPlugin;
import org.jivesoftware.openfire.container.PluginManager; import org.jivesoftware.openfire.container.plugin.PluginName;
import org.jivesoftware.openfire.container.plugin.PluginDescription;
import org.jivesoftware.openfire.group.Group; import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager; import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException; import org.jivesoftware.openfire.group.GroupNotFoundException;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventDispatcher; import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener; import org.jivesoftware.util.PropertyEventListener;
import org.jivesoftware.util.JiveProperties;
import org.xmpp.component.Component; import org.xmpp.component.Component;
import org.xmpp.component.ComponentException; import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManager; import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.packet.*; import org.xmpp.packet.*;
import java.io.File;
import java.util.*; import java.util.*;
import com.google.inject.Inject;
/** /**
* Broadcast service plugin. It accepts messages and broadcasts them out to * Broadcast service plugin. It accepts messages and broadcasts them out to
* groups of connected users. The address <tt>all@[serviceName].[server]</tt> is * groups of connected users. The address <tt>all@[serviceName].[server]</tt> is
...@@ -40,61 +43,54 @@ import java.util.*; ...@@ -40,61 +43,54 @@ import java.util.*;
* *
* @author Matt Tucker * @author Matt Tucker
*/ */
public class BroadcastPlugin implements Plugin, Component, PropertyEventListener { public class BroadcastPlugin extends AbstractPlugin implements Component, PropertyEventListener {
private String serviceName;
private SessionManager sessionManager; private SessionManager sessionManager;
private GroupManager groupManager; private GroupManager groupManager;
private List<JID> allowedUsers;
private boolean groupMembersAllowed; private final String pluginName;
private boolean disableGroupPermissions; private final String pluginDescription;
private ComponentManager componentManager; private final JiveProperties jiveProperties;
private PluginManager pluginManager; private String serviceName;
/** /**
* Constructs a new broadcast plugin. * Constructs a new broadcast plugin.
*
* @param pluginName the name configured for this plugin.
* @param pluginDescription the description configured for this plugin.
* @param jiveProperties system properties which stores the configuration paramters for the
* broadcast plugin.
*/ */
public BroadcastPlugin() { @Inject
serviceName = JiveGlobals.getProperty("plugin.broadcast.serviceName", "broadcast"); public BroadcastPlugin(@PluginName String pluginName,
disableGroupPermissions = JiveGlobals.getBooleanProperty( @PluginDescription String pluginDescription,
"plugin.broadcast.disableGroupPermissions"); JiveProperties jiveProperties)
groupMembersAllowed = JiveGlobals.getBooleanProperty( {
"plugin.broadcast.groupMembersAllowed", true); this.pluginName = pluginName;
allowedUsers = stringToList(JiveGlobals.getProperty("plugin.broadcast.allowedUsers", "")); this.pluginDescription = pluginDescription;
this.jiveProperties = jiveProperties;
this.serviceName = jiveProperties.getProperty("plugin.broadcast.serviceName", "broadcast");
} }
// Plugin Interface @Inject
public void setSessionManager(SessionManager sessionManager) {
public void initializePlugin(PluginManager manager, File pluginDirectory) { this.sessionManager = sessionManager;
pluginManager = manager; }
sessionManager = SessionManager.getInstance();
groupManager = GroupManager.getInstance();
// Register as a component. @Inject
componentManager = ComponentManagerFactory.getComponentManager(); public void setGroupManager(GroupManager groupManager) {
try { this.groupManager = groupManager;
componentManager.addComponent(serviceName, this);
}
catch (Exception e) {
componentManager.getLog().error(e);
}
PropertyEventDispatcher.addListener(this);
} }
public void destroyPlugin() { public void initialize() {
PropertyEventDispatcher.removeListener(this); // Register as a component.
// Unregister component.
try { try {
componentManager.removeComponent(serviceName); addComponent(getServiceName(), this);
} }
catch (Exception e) { catch (Exception e) {
componentManager.getLog().error(e); throw new RuntimeException("Error initializing internal broadcast component", e);
} }
componentManager = null; addPropertyEventListener(this);
pluginManager = null;
sessionManager = null;
groupManager = null;
allowedUsers.clear();
} }
public void initialize(JID jid, ComponentManager componentManager) { public void initialize(JID jid, ComponentManager componentManager) {
...@@ -107,15 +103,12 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -107,15 +103,12 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
} }
// Component Interface // Component Interface
public String getName() { public String getName() {
// Get the name from the plugin.xml file. return pluginName;
return pluginManager.getName(this);
} }
public String getDescription() { public String getDescription() {
// Get the description from the plugin.xml file. return pluginDescription;
return pluginManager.getDescription(this);
} }
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
...@@ -124,6 +117,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -124,6 +117,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
String toNode = packet.getTo().getNode(); String toNode = packet.getTo().getNode();
// Check if user is allowed to send packet to this service[+group] // Check if user is allowed to send packet to this service[+group]
boolean targetAll = "all".equals(toNode); boolean targetAll = "all".equals(toNode);
Collection<JID> allowedUsers = getGlobalAllowedUsers();
if (targetAll) { if (targetAll) {
// See if the user is allowed to send the packet. // See if the user is allowed to send the packet.
JID address = new JID(packet.getFrom().toBareJID()); JID address = new JID(packet.getFrom().toBareJID());
...@@ -137,7 +131,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -137,7 +131,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
group = groupManager.getGroup(toNode); group = groupManager.getGroup(toNode);
boolean isGroupUser = group.isUser(packet.getFrom()) || boolean isGroupUser = group.isUser(packet.getFrom()) ||
group.isUser(new JID(packet.getFrom().toBareJID())); group.isUser(new JID(packet.getFrom().toBareJID()));
if (disableGroupPermissions || (groupMembersAllowed && isGroupUser) || if (isGroupPermissionsDisabled() || (isGroupMembersAllowed() && isGroupUser) ||
allowedUsers.contains(new JID(packet.getFrom().toBareJID()))) { allowedUsers.contains(new JID(packet.getFrom().toBareJID()))) {
canProceed = true; canProceed = true;
} }
...@@ -344,6 +338,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -344,6 +338,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
// Return the list of groups hosted by the service that can be used by the user // Return the list of groups hosted by the service that can be used by the user
Collection<Group> groups; Collection<Group> groups;
JID address = new JID(iq.getFrom().toBareJID()); JID address = new JID(iq.getFrom().toBareJID());
Collection<JID> allowedUsers = getGlobalAllowedUsers();
if (allowedUsers.contains(address)) { if (allowedUsers.contains(address)) {
groups = groupManager.getGroups(); groups = groupManager.getGroups();
} }
...@@ -409,7 +404,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -409,7 +404,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
* @return the users allowed to send broadcast messages. * @return the users allowed to send broadcast messages.
*/ */
public Collection<JID> getGlobalAllowedUsers() { public Collection<JID> getGlobalAllowedUsers() {
return allowedUsers; return stringToList(JiveGlobals.getProperty("plugin.broadcast.allowedUsers", ""));
} }
/** /**
...@@ -437,7 +432,8 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -437,7 +432,8 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
* @return true if group permission checking is disabled. * @return true if group permission checking is disabled.
*/ */
public boolean isGroupPermissionsDisabled() { public boolean isGroupPermissionsDisabled() {
return disableGroupPermissions; return jiveProperties.getBooleanProperty(
"plugin.broadcast.disableGroupPermissions");
} }
/** /**
...@@ -447,8 +443,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -447,8 +443,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
* @param disableGroupPermissions true if group permission checking should be disabled. * @param disableGroupPermissions true if group permission checking should be disabled.
*/ */
public void setGroupPermissionsDisabled(boolean disableGroupPermissions) { public void setGroupPermissionsDisabled(boolean disableGroupPermissions) {
this.disableGroupPermissions = disableGroupPermissions; jiveProperties.put("plugin.broadcast.disableGroupPermissions",
JiveGlobals.setProperty("plugin.broadcast.disableGroupPermissions",
Boolean.toString(disableGroupPermissions)); Boolean.toString(disableGroupPermissions));
} }
...@@ -462,7 +457,8 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -462,7 +457,8 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
* group admins are allowed. * group admins are allowed.
*/ */
public boolean isGroupMembersAllowed() { public boolean isGroupMembersAllowed() {
return groupMembersAllowed; return jiveProperties.getBooleanProperty(
"plugin.broadcast.groupMembersAllowed", true);
} }
/** /**
...@@ -475,38 +471,19 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -475,38 +471,19 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
* group admins are allowed. * group admins are allowed.
*/ */
public void setGroupMembersAllowed(boolean allowed) { public void setGroupMembersAllowed(boolean allowed) {
this.groupMembersAllowed = allowed; jiveProperties.put("plugin.broadcast.groupMembersAllowed", Boolean.toString(allowed));
JiveGlobals.setProperty("plugin.broadcast.groupMembersAllowed", Boolean.toString(allowed));
} }
// PropertyEventListener Methods // PropertyEventListener Methods
public void propertySet(String property, Map params) { public void propertySet(String property, Map params) {
if (property.equals("plugin.broadcast.groupMembersAllowed")) { if (property.equals("plugin.broadcast.serviceName")) {
this.groupMembersAllowed = Boolean.parseBoolean((String)params.get("value"));
}
else if (property.equals("plugin.broadcast.disableGroupPermissions")) {
this.disableGroupPermissions = Boolean.parseBoolean((String)params.get("value"));
}
else if (property.equals("plugin.broadcast.allowedUsers")) {
this.allowedUsers = stringToList((String)params.get("value"));
}
else if (property.equals("plugin.broadcast.serviceName")) {
changeServiceName((String)params.get("value")); changeServiceName((String)params.get("value"));
} }
} }
public void propertyDeleted(String property, Map params) { public void propertyDeleted(String property, Map params) {
if (property.equals("plugin.broadcast.groupMembersAllowed")) { if (property.equals("plugin.broadcast.serviceName")) {
this.groupMembersAllowed = true;
}
else if (property.equals("plugin.broadcast.disableGroupPermissions")) {
this.disableGroupPermissions = false;
}
else if (property.equals("plugin.broadcast.allowedUsers")) {
this.allowedUsers = Collections.emptyList();
}
else if (property.equals("plugin.broadcast.serviceName")) {
changeServiceName("broadcast"); changeServiceName("broadcast");
} }
} }
......
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