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
}
public void addComponent(String subdomain, Component component) throws ComponentException {
ComponentLifecycleImpl componentLifecycle
= (ComponentLifecycleImpl) addComponent(subdomain, component, null);
startComponent(componentLifecycle.subdomain, componentLifecycle.component);
addComponent(subdomain, component, null);
}
private void startComponent(String subdomain, Component component) {
......@@ -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
{
ComponentLifecycleImpl componentLifecycle = new ComponentLifecycleImpl(subdomain, component);
......@@ -170,6 +169,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
throw e;
}
componentLifecycle.setJiveProperty(jiveProperty);
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;
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;
......@@ -33,6 +37,7 @@ import java.util.zip.ZipFile;
import com.google.inject.Guice;
import com.google.inject.AbstractModule;
import com.google.inject.Provider;
/**
* Loads and manages plugins. The <tt>plugins</tt> directory is monitored for any
......@@ -398,7 +403,8 @@ public class PluginManager {
moduleClazz = null;
}
//noinspection unchecked
plugin = instantiateAbstractPlugin(pluginLoader, pluginClazz, moduleClazz);
plugin = instantiateAbstractPlugin(pluginDir, pluginLoader, pluginClazz,
moduleClazz);
}
else {
plugin = (Plugin) pluginClazz.newInstance();
......@@ -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,
Class<? extends com.google.inject.Module> moduleClazz)
throws IllegalAccessException, InstantiationException
......@@ -549,6 +555,28 @@ public class PluginManager {
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);
}
}).getInstance(pluginClazz);
......@@ -679,8 +707,12 @@ public class PluginManager {
* @return the plugin's name.
*/
public String getName(Plugin plugin) {
String name = getElementValue(plugin, "/plugin/name");
String pluginName = pluginDirs.get(plugin).getName();
return getName(pluginDirs.get(plugin));
}
private String getName(File pluginDir) {
String name = getElementValue(pluginDir, "/plugin/name");
String pluginName = pluginDir.getName();
if (name != null) {
return AdminConsole.getAdminText(name, pluginName);
}
......@@ -697,8 +729,13 @@ public class PluginManager {
* @return the plugin's description.
*/
public String getDescription(Plugin plugin) {
String pluginName = pluginDirs.get(plugin).getName();
return AdminConsole.getAdminText(getElementValue(plugin, "/plugin/description"), pluginName);
return getDescription(pluginDirs.get(plugin));
}
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 {
* @return the plugin's author.
*/
public String getAuthor(Plugin plugin) {
return getElementValue(plugin, "/plugin/author");
return getElementValue(pluginDirs.get(plugin), "/plugin/author");
}
/**
......@@ -720,7 +757,7 @@ public class PluginManager {
* @return the plugin's version.
*/
public String getVersion(Plugin plugin) {
return getElementValue(plugin, "/plugin/version");
return getElementValue(pluginDirs.get(plugin), "/plugin/version");
}
/**
......@@ -731,7 +768,7 @@ public class PluginManager {
* @return the plugin's version.
*/
public String getMinServerVersion(Plugin plugin) {
return getElementValue(plugin, "/plugin/minServerVersion");
return getElementValue(pluginDirs.get(plugin), "/plugin/minServerVersion");
}
/**
......@@ -743,7 +780,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(plugin, "/plugin/databaseKey");
return getElementValue(pluginDirs.get(plugin), "/plugin/databaseKey");
}
/**
......@@ -755,7 +792,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(plugin, "/plugin/databaseVersion");
String versionString = getElementValue(pluginDirs.get(plugin), "/plugin/databaseVersion");
if (versionString != null) {
try {
return Integer.parseInt(versionString.trim());
......@@ -776,7 +813,7 @@ public class PluginManager {
* @return the plugin's license agreement.
*/
public License getLicense(Plugin plugin) {
String licenseString = getElementValue(plugin, "/plugin/licenseType");
String licenseString = getElementValue(pluginDirs.get(plugin), "/plugin/licenseType");
if (licenseString != null) {
try {
// Attempt to load the get the license type. We lower-case and
......@@ -806,12 +843,11 @@ public class PluginManager {
* Returns the value of an element selected via an xpath expression from
* a Plugin's plugin.xml file.
*
* @param plugin the plugin.
* @param pluginDir the plugin directory.
* @param xpath the xpath expression.
* @return the value of the element selected by the xpath expression.
*/
private String getElementValue(Plugin plugin, String xpath) {
File pluginDir = pluginDirs.get(plugin);
private String getElementValue(File pluginDir, String xpath) {
if (pluginDir == null) {
return null;
}
......
......@@ -9,6 +9,7 @@ package org.jivesoftware.openfire.container;
import com.google.inject.*;
import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.session.RemoteSessionLocator;
import org.jivesoftware.openfire.vcard.VCardManager;
import org.jivesoftware.openfire.stun.STUNService;
......@@ -28,6 +29,8 @@ 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;
......@@ -38,6 +41,7 @@ public class PluginModule extends AbstractModule {
protected void configure() {
bind(JiveProperties.class).toInstance(JiveProperties.getInstance());
bind(Logger.class).toInstance(getLogger());
XMPPServer server = XMPPServer.getInstance();
bind(XMPPServer.class).toInstance(server);
......@@ -72,6 +76,7 @@ public class PluginModule extends AbstractModule {
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;
......@@ -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> {
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) {
Connection con = 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
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