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

Removing Guice, alas we barely knew you

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8649 b35dd754-fafc-0310-a699-88a17e54d16e
parent a940eeff
......@@ -18,10 +18,6 @@ import org.dom4j.io.SAXReader;
import org.jivesoftware.admin.AdminConsole;
import org.jivesoftware.database.DbConnectionManager;
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.Log;
import org.jivesoftware.util.Version;
......@@ -35,11 +31,6 @@ import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.ZipFile;
import com.google.inject.Guice;
import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.Scopes;
/**
* Loads and manages plugins. The <tt>plugins</tt> directory is monitored for any
* new plugins, and they are dynamically loaded.<p/>
......@@ -381,35 +372,7 @@ public class PluginManager {
}
String className = pluginXML.selectSingleNode("/plugin/class").getText();
Class pluginClazz = pluginLoader.loadClass(className);
if(AbstractPlugin.class.isAssignableFrom(pluginClazz)) {
String moduleClassName;
try {
moduleClassName = pluginXML.selectSingleNode("/plugin/module/class")
.getText();
}
catch (NullPointerException npe) {
moduleClassName = null;
}
Class moduleClazz;
try {
if (moduleClassName != null) {
moduleClazz = pluginLoader.loadClass(moduleClassName);
}
else {
moduleClazz = null;
}
}
catch (ClassNotFoundException cnfe) {
moduleClazz = null;
}
//noinspection unchecked
plugin = instantiateAbstractPlugin(pluginDir, pluginLoader, pluginClazz,
moduleClazz);
}
else {
plugin = (Plugin) pluginClazz.newInstance();
}
plugin = (Plugin)pluginLoader.loadClass(className).newInstance();
if (parentPluginNode != null) {
String parentPlugin = parentPluginNode.getTextTrim();
// See if the parent is already loaded.
......@@ -533,58 +496,6 @@ public class PluginManager {
}
}
private Plugin instantiateAbstractPlugin(final File pluginDir, PluginClassLoader pluginLoader,
final Class<? extends AbstractPlugin> pluginClazz,
Class<? extends com.google.inject.Module> moduleClazz)
throws IllegalAccessException, InstantiationException
{
// Init the plugin.
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(pluginLoader);
final PluginModule module = new PluginModule();
com.google.inject.Module tempModule;
if(moduleClazz == null) {
tempModule = null;
}
else {
tempModule = moduleClazz.newInstance();
}
final com.google.inject.Module pluginModule = tempModule;
AbstractPlugin plugin = Guice.createInjector(new AbstractModule() {
protected void configure() {
install(module);
if (pluginModule != null) {
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).in(Scopes.SINGLETON);
}
}).getInstance(pluginClazz);
Thread.currentThread().setContextClassLoader(oldLoader);
return plugin;
}
private void firePluginCreatedEvent(String name, Plugin plugin) {
for(PluginListener listener : pluginListeners) {
listener.pluginCreated(name, plugin);
......@@ -708,12 +619,8 @@ public class PluginManager {
* @return the plugin's name.
*/
public String getName(Plugin plugin) {
return getName(pluginDirs.get(plugin));
}
private String getName(File pluginDir) {
String name = getElementValue(pluginDir, "/plugin/name");
String pluginName = pluginDir.getName();
String name = getElementValue(plugin, "/plugin/name");
String pluginName = pluginDirs.get(plugin).getName();
if (name != null) {
return AdminConsole.getAdminText(name, pluginName);
}
......@@ -730,13 +637,8 @@ public class PluginManager {
* @return the plugin's description.
*/
public String getDescription(Plugin plugin) {
return getDescription(pluginDirs.get(plugin));
}
private String getDescription(File pluginDir) {
String pluginName = pluginDir.getName();
return AdminConsole.getAdminText(getElementValue(pluginDirs.get(pluginDir),
"/plugin/description"), pluginName);
String pluginName = pluginDirs.get(plugin).getName();
return AdminConsole.getAdminText(getElementValue(plugin, "/plugin/description"), pluginName);
}
/**
......@@ -747,7 +649,7 @@ public class PluginManager {
* @return the plugin's author.
*/
public String getAuthor(Plugin plugin) {
return getElementValue(pluginDirs.get(plugin), "/plugin/author");
return getElementValue(plugin, "/plugin/author");
}
/**
......@@ -758,7 +660,7 @@ public class PluginManager {
* @return the plugin's version.
*/
public String getVersion(Plugin plugin) {
return getElementValue(pluginDirs.get(plugin), "/plugin/version");
return getElementValue(plugin, "/plugin/version");
}
/**
......@@ -769,7 +671,7 @@ public class PluginManager {
* @return the plugin's version.
*/
public String getMinServerVersion(Plugin plugin) {
return getElementValue(pluginDirs.get(plugin), "/plugin/minServerVersion");
return getElementValue(plugin, "/plugin/minServerVersion");
}
/**
......@@ -781,7 +683,7 @@ public class PluginManager {
* @return the plugin's database schema key or <tt>null</tt> if it doesn't exist.
*/
public String getDatabaseKey(Plugin plugin) {
return getElementValue(pluginDirs.get(plugin), "/plugin/databaseKey");
return getElementValue(plugin, "/plugin/databaseKey");
}
/**
......@@ -793,7 +695,7 @@ public class PluginManager {
* @return the plugin's database schema version or <tt>-1</tt> if it doesn't exist.
*/
public int getDatabaseVersion(Plugin plugin) {
String versionString = getElementValue(pluginDirs.get(plugin), "/plugin/databaseVersion");
String versionString = getElementValue(plugin, "/plugin/databaseVersion");
if (versionString != null) {
try {
return Integer.parseInt(versionString.trim());
......@@ -814,7 +716,7 @@ public class PluginManager {
* @return the plugin's license agreement.
*/
public License getLicense(Plugin plugin) {
String licenseString = getElementValue(pluginDirs.get(plugin), "/plugin/licenseType");
String licenseString = getElementValue(plugin, "/plugin/licenseType");
if (licenseString != null) {
try {
// Attempt to load the get the license type. We lower-case and
......@@ -844,11 +746,12 @@ public class PluginManager {
* Returns the value of an element selected via an xpath expression from
* a Plugin's plugin.xml file.
*
* @param pluginDir the plugin directory.
* @param plugin the plugin.
* @param xpath the xpath expression.
* @return the value of the element selected by the xpath expression.
*/
private String getElementValue(File pluginDir, String xpath) {
private String getElementValue(Plugin plugin, String xpath) {
File pluginDir = pluginDirs.get(plugin);
if (pluginDir == null) {
return null;
}
......
......@@ -13,24 +13,24 @@ package org.jivesoftware.openfire.plugin;
import org.dom4j.Element;
import org.jivesoftware.openfire.SessionManager;
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.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
import org.jivesoftware.util.JiveProperties;
import org.xmpp.component.Component;
import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.packet.*;
import java.io.File;
import java.util.*;
import com.google.inject.Inject;
/**
* Broadcast service plugin. It accepts messages and broadcasts them out to
* groups of connected users. The address <tt>all@[serviceName].[server]</tt> is
......@@ -40,49 +40,61 @@ import com.google.inject.Inject;
*
* @author Matt Tucker
*/
public class BroadcastPlugin extends AbstractPlugin implements Component, PropertyEventListener {
private final String pluginName;
private final String pluginDescription;
private final JiveProperties jiveProperties;
private final SessionManager sessionManager;
private final GroupManager groupManager;
public class BroadcastPlugin implements Plugin, Component, PropertyEventListener {
private String serviceName;
private SessionManager sessionManager;
private GroupManager groupManager;
private List<JID> allowedUsers;
private boolean groupMembersAllowed;
private boolean disableGroupPermissions;
private ComponentManager componentManager;
private PluginManager pluginManager;
/**
* 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.
* @param sessionManager the session manager.
* @param groupManager the group manager.
*/
@Inject
public BroadcastPlugin(@PluginName String pluginName,
@PluginDescription String pluginDescription,
JiveProperties jiveProperties,
SessionManager sessionManager,
GroupManager groupManager)
{
this.pluginName = pluginName;
this.pluginDescription = pluginDescription;
this.jiveProperties = jiveProperties;
this.sessionManager = sessionManager;
this.groupManager = groupManager;
public BroadcastPlugin() {
serviceName = JiveGlobals.getProperty("plugin.broadcast.serviceName", "broadcast");
disableGroupPermissions = JiveGlobals.getBooleanProperty(
"plugin.broadcast.disableGroupPermissions");
groupMembersAllowed = JiveGlobals.getBooleanProperty(
"plugin.broadcast.groupMembersAllowed", true);
allowedUsers = stringToList(JiveGlobals.getProperty("plugin.broadcast.allowedUsers", ""));
}
public void initialize() {
// Plugin Interface
public void initializePlugin(PluginManager manager, File pluginDirectory) {
pluginManager = manager;
sessionManager = SessionManager.getInstance();
groupManager = GroupManager.getInstance();
// Register as a component.
componentManager = ComponentManagerFactory.getComponentManager();
try {
addComponent(getServiceName(), this);
componentManager.addComponent(serviceName, this);
}
catch (Exception e) {
throw new RuntimeException("Error initializing internal broadcast component", e);
componentManager.getLog().error(e);
}
addPropertyEventListener(this);
PropertyEventDispatcher.addListener(this);
}
public void destroyPlugin() {
PropertyEventDispatcher.removeListener(this);
// Unregister component.
try {
componentManager.removeComponent(serviceName);
}
catch (Exception e) {
componentManager.getLog().error(e);
}
componentManager = null;
pluginManager = null;
sessionManager = null;
groupManager = null;
allowedUsers.clear();
}
public void initialize(JID jid, ComponentManager componentManager) {
......@@ -95,12 +107,15 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
}
// Component Interface
public String getName() {
return pluginName;
// Get the name from the plugin.xml file.
return pluginManager.getName(this);
}
public String getDescription() {
return pluginDescription;
// Get the description from the plugin.xml file.
return pluginManager.getDescription(this);
}
public void processPacket(Packet packet) {
......@@ -109,7 +124,6 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
String toNode = packet.getTo().getNode();
// Check if user is allowed to send packet to this service[+group]
boolean targetAll = "all".equals(toNode);
Collection<JID> allowedUsers = getGlobalAllowedUsers();
if (targetAll) {
// See if the user is allowed to send the packet.
JID address = new JID(packet.getFrom().toBareJID());
......@@ -123,7 +137,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
group = groupManager.getGroup(toNode);
boolean isGroupUser = group.isUser(packet.getFrom()) ||
group.isUser(new JID(packet.getFrom().toBareJID()));
if (isGroupPermissionsDisabled() || (isGroupMembersAllowed() && isGroupUser) ||
if (disableGroupPermissions || (groupMembersAllowed && isGroupUser) ||
allowedUsers.contains(new JID(packet.getFrom().toBareJID()))) {
canProceed = true;
}
......@@ -330,7 +344,6 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
// Return the list of groups hosted by the service that can be used by the user
Collection<Group> groups;
JID address = new JID(iq.getFrom().toBareJID());
Collection<JID> allowedUsers = getGlobalAllowedUsers();
if (allowedUsers.contains(address)) {
groups = groupManager.getGroups();
}
......@@ -375,9 +388,6 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
* @return the service name of this component.
*/
public String getServiceName() {
if (serviceName == null) {
serviceName = jiveProperties.getProperty("plugin.broadcast.serviceName", "broadcast");
}
return serviceName;
}
......@@ -387,7 +397,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
* @param serviceName the service name of this component.
*/
public void setServiceName(String serviceName) {
jiveProperties.put("plugin.broadcast.serviceName", serviceName);
JiveGlobals.setProperty("plugin.broadcast.serviceName", serviceName);
}
/**
......@@ -399,7 +409,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
* @return the users allowed to send broadcast messages.
*/
public Collection<JID> getGlobalAllowedUsers() {
return stringToList(jiveProperties.getProperty("plugin.broadcast.allowedUsers", ""));
return allowedUsers;
}
/**
......@@ -416,7 +426,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
for (String jid : allowedUsers) {
buf.append(jid).append(",");
}
jiveProperties.put("plugin.broadcast.allowedUsers", buf.toString());
JiveGlobals.setProperty("plugin.broadcast.allowedUsers", buf.toString());
}
/**
......@@ -427,8 +437,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
* @return true if group permission checking is disabled.
*/
public boolean isGroupPermissionsDisabled() {
return jiveProperties.getBooleanProperty(
"plugin.broadcast.disableGroupPermissions");
return disableGroupPermissions;
}
/**
......@@ -438,7 +447,8 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
* @param disableGroupPermissions true if group permission checking should be disabled.
*/
public void setGroupPermissionsDisabled(boolean disableGroupPermissions) {
jiveProperties.put("plugin.broadcast.disableGroupPermissions",
this.disableGroupPermissions = disableGroupPermissions;
JiveGlobals.setProperty("plugin.broadcast.disableGroupPermissions",
Boolean.toString(disableGroupPermissions));
}
......@@ -452,8 +462,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
* group admins are allowed.
*/
public boolean isGroupMembersAllowed() {
return jiveProperties.getBooleanProperty(
"plugin.broadcast.groupMembersAllowed", true);
return groupMembersAllowed;
}
/**
......@@ -466,19 +475,38 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
* group admins are allowed.
*/
public void setGroupMembersAllowed(boolean allowed) {
jiveProperties.put("plugin.broadcast.groupMembersAllowed", Boolean.toString(allowed));
this.groupMembersAllowed = allowed;
JiveGlobals.setProperty("plugin.broadcast.groupMembersAllowed", Boolean.toString(allowed));
}
// PropertyEventListener Methods
public void propertySet(String property, Map params) {
if (property.equals("plugin.broadcast.serviceName")) {
if (property.equals("plugin.broadcast.groupMembersAllowed")) {
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"));
}
}
public void propertyDeleted(String property, Map params) {
if (property.equals("plugin.broadcast.serviceName")) {
if (property.equals("plugin.broadcast.groupMembersAllowed")) {
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");
}
}
......@@ -500,7 +528,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
if (serviceName == null) {
throw new NullPointerException("Service name cannot be null");
}
if (serviceName.equals(this.serviceName)) {
if (this.serviceName.equals(serviceName)) {
return;
}
......@@ -537,7 +565,7 @@ public class BroadcastPlugin extends AbstractPlugin implements Component, Proper
values.add(new JID(value));
}
else {
values.add(createJid(value));
values.add(XMPPServer.getInstance().createJID(value, null));
}
}
}
......
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