Commit 3d8eb1b4 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

Removed convinence

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8662 b35dd754-fafc-0310-a699-88a17e54d16e
parent fae2712b
...@@ -16,7 +16,8 @@ import org.jivesoftware.openfire.*; ...@@ -16,7 +16,8 @@ import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.container.BasicModule; import org.jivesoftware.openfire.container.BasicModule;
import org.jivesoftware.openfire.disco.IQDiscoItemsHandler; import org.jivesoftware.openfire.disco.IQDiscoItemsHandler;
import org.jivesoftware.openfire.session.ComponentSession; import org.jivesoftware.openfire.session.ComponentSession;
import org.jivesoftware.util.*; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;
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;
...@@ -25,7 +26,10 @@ import org.xmpp.packet.*; ...@@ -25,7 +26,10 @@ import org.xmpp.packet.*;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.*; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/** /**
* Manages the registration and delegation of Components. The ComponentManager * Manages the registration and delegation of Components. The ComponentManager
...@@ -41,8 +45,7 @@ import java.util.concurrent.*; ...@@ -41,8 +45,7 @@ import java.util.concurrent.*;
*/ */
public class InternalComponentManager extends BasicModule implements ComponentManager, RoutableChannelHandler { public class InternalComponentManager extends BasicModule implements ComponentManager, RoutableChannelHandler {
private ConcurrentMap<String, ComponentLifecycleImpl> components private Map<String, Component> components = new ConcurrentHashMap<String, Component>();
= new ConcurrentHashMap<String, ComponentLifecycleImpl>();
private Map<String, IQ> componentInfo = new ConcurrentHashMap<String, IQ>(); private Map<String, IQ> componentInfo = new ConcurrentHashMap<String, IQ>();
private Map<JID, JID> presenceMap = new ConcurrentHashMap<JID, JID>(); private Map<JID, JID> presenceMap = new ConcurrentHashMap<JID, JID>();
/** /**
...@@ -60,23 +63,11 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -60,23 +63,11 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
* in many methods. * in many methods.
*/ */
private String serverDomain; private String serverDomain;
private final RoutingTable routingTable; private RoutingTable routingTable;
private final IQDiscoItemsHandler discoItemsHandler;
private final JiveProperties jiveProperties;
public InternalComponentManager() { public InternalComponentManager() {
this(XMPPServer.getInstance().getRoutingTable(),
XMPPServer.getInstance().getIQDiscoItemsHandler(), JiveProperties.getInstance());
}
public InternalComponentManager(RoutingTable routingTable, IQDiscoItemsHandler discoItemsHandler,
JiveProperties jiveProperties)
{
super("Internal Component Manager"); super("Internal Component Manager");
instance = this; instance = this;
this.routingTable = routingTable;
this.discoItemsHandler = discoItemsHandler;
this.jiveProperties = jiveProperties;
} }
public static InternalComponentManager getInstance() { public static InternalComponentManager getInstance() {
...@@ -85,6 +76,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -85,6 +76,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
public void initialize(XMPPServer server) { public void initialize(XMPPServer server) {
super.initialize(server); super.initialize(server);
routingTable = server.getRoutingTable();
} }
public void start() { public void start() {
...@@ -110,85 +102,73 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -110,85 +102,73 @@ 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 {
addComponent(subdomain, component, null); // Check that the requested subdoman is not taken by another component
} Component existingComponent = components.get(subdomain);
if (existingComponent != null && existingComponent != component) {
throw new ComponentException("Domain (" + subdomain +
") already taken by another component: " + existingComponent);
}
Log.debug("Registering component for domain: " + subdomain);
// Register that the domain is now taken by the component
components.put(subdomain, component);
private void startComponent(String subdomain, Component component) { JID componentJID = new JID(subdomain + "." + serverDomain);
JID componentJID = new JID(subdomain + "." + serverDomain);
// Add the route to the new service provided by the component // Add the route to the new service provided by the component
routingTable.addComponentRoute(componentJID, XMPPServer.getInstance().getRoutingTable().addComponentRoute(componentJID,
new RoutableComponent(componentJID, component)); new RoutableComponent(componentJID, component));
// Initialize the new component // Initialize the new component
try { try {
component.initialize(componentJID, this);
component.start(); component.start();
// Notify listeners that a new component has been registered
for (ComponentEventListener listener : listeners) {
listener.componentRegistered(component, componentJID);
}
// Check for potential interested users. // Check for potential interested users.
checkPresences(); checkPresences();
// Send a disco#info request to the new component. If the component provides information // Send a disco#info request to the new component. If the component provides information
// then it will be added to the list of discoverable server items. // then it will be added to the list of discoverable server items.
checkDiscoSupport(component, componentJID); checkDiscoSupport(component, componentJID);
Log.debug("Component registered for domain: " + subdomain); Log.debug("Component registered for domain: " + subdomain);
// Notify listeners that a new component has been registered
for (ComponentEventListener listener : listeners) {
listener.componentRegistered(component, componentJID);
}
} }
catch (RuntimeException e) { catch (Exception e) {
// Unregister the componet's domain
components.remove(subdomain);
// Remove the route // Remove the route
routingTable.removeComponentRoute(componentJID); XMPPServer.getInstance().getRoutingTable().removeComponentRoute(componentJID);
if (e instanceof ComponentException) {
// Rethrow the exception
throw (ComponentException)e;
}
// Rethrow the exception // Rethrow the exception
throw e; throw new ComponentException(e);
}
}
public ComponentLifecycle addComponent(String subdomain, Component component,
String jiveProperty)
throws ComponentException
{
ComponentLifecycleImpl componentLifecycle = new ComponentLifecycleImpl(subdomain, component);
ComponentLifecycleImpl oldLifecycle = components.putIfAbsent(subdomain, componentLifecycle);
if(oldLifecycle != null) {
throw new IllegalArgumentException("Domain (" + subdomain +
") already taken by another component: " + oldLifecycle.component);
}
try {
component.initialize(getComponentJID(subdomain), this);
} catch (ComponentException e) {
components.remove(subdomain, componentLifecycle);
throw e;
}
catch (RuntimeException e) {
components.remove(subdomain, componentLifecycle);
throw e;
} }
componentLifecycle.setJiveProperty(jiveProperty);
return componentLifecycle;
} }
public void removeComponent(String subdomain) { public void removeComponent(String subdomain) {
Log.debug("Unregistering component for domain: " + subdomain); Log.debug("Unregistering component for domain: " + subdomain);
ComponentLifecycleImpl component = components.remove(subdomain); Component component = components.remove(subdomain);
// Remove any info stored with the component being removed // Remove any info stored with the component being removed
componentInfo.remove(subdomain); componentInfo.remove(subdomain);
stopComponent(subdomain, component.component);
}
private void stopComponent(String subdomain, Component component) { JID componentJID = new JID(subdomain + "." + serverDomain);
JID componentJID = getComponentJID(subdomain);
// Remove the route for the service provided by the component // Remove the route for the service provided by the component
routingTable.removeComponentRoute(componentJID); RoutingTable routingTable = XMPPServer.getInstance().getRoutingTable();
if (routingTable != null) {
routingTable.removeComponentRoute(componentJID);
}
// Remove the disco item from the server for the component that is being removed // Remove the disco item from the server for the component that is being removed
discoItemsHandler.removeComponentItem(componentJID.toBareJID()); IQDiscoItemsHandler iqDiscoItemsHandler = XMPPServer.getInstance().getIQDiscoItemsHandler();
if (iqDiscoItemsHandler != null) {
iqDiscoItemsHandler.removeComponentItem(componentJID.toBareJID());
}
// Ask the component to shutdown // Ask the component to shutdown
if (component != null) { if (component != null) {
...@@ -249,9 +229,9 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -249,9 +229,9 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
public void addListener(ComponentEventListener listener) { public void addListener(ComponentEventListener listener) {
listeners.add(listener); listeners.add(listener);
// Notify the new listener about existing components // Notify the new listener about existing components
for (Map.Entry<String, ComponentLifecycleImpl> entry : components.entrySet()) { for (Map.Entry<String, Component> entry : components.entrySet()) {
String subdomain = entry.getKey(); String subdomain = entry.getKey();
Component component = entry.getValue().component; Component component = entry.getValue();
JID componentJID = new JID(subdomain + "." + serverDomain); JID componentJID = new JID(subdomain + "." + serverDomain);
listener.componentRegistered(component, componentJID); listener.componentRegistered(component, componentJID);
// Check if there is disco#info stored for the component // Check if there is disco#info stored for the component
...@@ -284,10 +264,6 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -284,10 +264,6 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
return serverDomain; return serverDomain;
} }
private JID getComponentJID(String subdomain) {
return new JID(subdomain + "." + serverDomain);
}
public String getHomeDirectory() { public String getHomeDirectory() {
return JiveGlobals.getHomeDirectory(); return JiveGlobals.getHomeDirectory();
} }
...@@ -361,7 +337,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -361,7 +337,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
if (componentJID.getNode() != null) { if (componentJID.getNode() != null) {
return null; return null;
} }
Component component = components.get(componentJID.getDomain()).component; Component component = components.get(componentJID.getDomain());
if (component != null) { if (component != null) {
return component; return component;
} }
...@@ -371,7 +347,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -371,7 +347,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
String serverName = componentJID.getDomain(); String serverName = componentJID.getDomain();
int index = serverName.lastIndexOf("." + serverDomain); int index = serverName.lastIndexOf("." + serverDomain);
if (index > -1) { if (index > -1) {
return components.get(serverName.substring(0, index)).component; return components.get(serverName.substring(0, index));
} }
} }
return null; return null;
...@@ -461,7 +437,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -461,7 +437,7 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
if (childElement != null) { if (childElement != null) {
namespace = childElement.getNamespaceURI(); namespace = childElement.getNamespaceURI();
} }
if ("http://jabber.org/protocol/disco#info".equals(namespace) && childElement != null) { if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
// Add a disco item to the server for the component that supports disco // Add a disco item to the server for the component that supports disco
Element identity = childElement.element("identity"); Element identity = childElement.element("identity");
if (identity == null) { if (identity == null) {
...@@ -517,104 +493,4 @@ public class InternalComponentManager extends BasicModule implements ComponentMa ...@@ -517,104 +493,4 @@ public class InternalComponentManager extends BasicModule implements ComponentMa
component.processPacket(packet); component.processPacket(packet);
} }
} }
private class ComponentLifecycleImpl implements ComponentLifecycle, PropertyEventListener {
private String jiveProperty;
private boolean isRunning = false;
private final Component component;
private final String subdomain;
private ComponentLifecycleImpl(String subdomain, Component component) {
this.subdomain = subdomain;
this.component = component;
}
public synchronized void start() {
if(jiveProperty != null) {
jiveProperties.put(jiveProperty, Boolean.TRUE.toString());
}
else {
startComponent();
}
}
private synchronized void startComponent() {
if(isRunning) {
return;
}
InternalComponentManager.this.startComponent(subdomain, component);
isRunning = true;
}
public synchronized void stop() {
if(jiveProperty != null) {
jiveProperties.put(jiveProperty, Boolean.FALSE.toString());
}
else {
stopComponent();
}
}
private synchronized void stopComponent() {
if(!isRunning) {
return;
}
InternalComponentManager.this.stopComponent(subdomain, component);
isRunning = false;
}
public void setJiveProperty(String jiveProperty) {
if(jiveProperty == null) {
this.jiveProperty = null;
PropertyEventDispatcher.removeListener(this);
startComponent();
}
this.jiveProperty = jiveProperty;
PropertyEventDispatcher.addListener(this);
if(JiveGlobals.getBooleanProperty(jiveProperty, true)) {
startComponent();
}
else {
stopComponent();
}
}
public synchronized boolean isRunning() {
return isRunning;
}
public void propertySet(String property, Map<String, Object> params) {
if(property.equals(jiveProperty)) {
boolean enabled = Boolean.FALSE.toString().equals(params.get("value"));
if(enabled) {
startComponent();
}
else {
stopComponent();
}
}
}
public void propertyDeleted(String property, Map<String, Object> params) {
if(property.equals(jiveProperty)) {
startComponent();
}
}
public void xmlPropertySet(String property, Map<String, Object> params) {
}
public void xmlPropertyDeleted(String property, Map<String, Object> params) {
}
}
} }
\ No newline at end of file
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
package org.xmpp.component; package org.xmpp.component;
import org.jivesoftware.openfire.IQResultListener; import org.jivesoftware.openfire.IQResultListener;
import org.jivesoftware.openfire.component.ComponentLifecycle;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
...@@ -28,19 +27,14 @@ public interface ComponentManager { ...@@ -28,19 +27,14 @@ public interface ComponentManager {
* *
* @param subdomain the subdomain of the component's address. * @param subdomain the subdomain of the component's address.
* @param component the component. * @param component the component.
* @throws ComponentException
*/ */
public void addComponent(String subdomain, Component component) throws ComponentException; public void addComponent(String subdomain, Component component) throws ComponentException;
ComponentLifecycle addComponent(String subdomain, Component component, String jiveProperty)
throws ComponentException;
/** /**
* Removes a component. The {@link Component#shutdown} method will be called on the * Removes a component. The {@link Component#shutdown} method will be called on the
* component. * component.
* *
* @param subdomain the subdomain of the component's address. * @param subdomain the subdomain of the component's address.
* @throws ComponentException
*/ */
public void removeComponent(String subdomain) throws ComponentException; public void removeComponent(String subdomain) throws ComponentException;
...@@ -53,7 +47,6 @@ public interface ComponentManager { ...@@ -53,7 +47,6 @@ public interface ComponentManager {
* *
* @param component the component sending the packet. * @param component the component sending the packet.
* @param packet the packet to send. * @param packet the packet to send.
* @throws ComponentException
*/ */
public void sendPacket(Component component, Packet packet) throws ComponentException; public void sendPacket(Component component, Packet packet) throws ComponentException;
...@@ -73,7 +66,6 @@ public interface ComponentManager { ...@@ -73,7 +66,6 @@ public interface ComponentManager {
* @param timeout the number of milliseconds to wait before returning an IQ error. * @param timeout the number of milliseconds to wait before returning an IQ error.
* @return the answer sent by the server. The answer could be an IQ of type result or * @return the answer sent by the server. The answer could be an IQ of type result or
* error. * error.
* @throws ComponentException
*/ */
public IQ query(Component component, IQ packet, int timeout) throws ComponentException; public IQ query(Component component, IQ packet, int timeout) throws ComponentException;
...@@ -84,7 +76,6 @@ public interface ComponentManager { ...@@ -84,7 +76,6 @@ public interface ComponentManager {
* @param component the component sending the packet. * @param component the component sending the packet.
* @param packet the IQ packet to send. * @param packet the IQ packet to send.
* @param listener the listener that will be invoked when an answer is received. * @param listener the listener that will be invoked when an answer is received.
* @throws ComponentException
*/ */
public void query(Component component, IQ packet, IQResultListener listener) throws ComponentException; public void query(Component component, IQ packet, IQResultListener listener) throws ComponentException;
......
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