TransportInstance.java 6.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2006 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.
 */

11
package org.jivesoftware.wildfire.gateway;
12 13

import org.jivesoftware.util.JiveGlobals;
Daniel Henninger's avatar
Daniel Henninger committed
14
import org.jivesoftware.util.Log;
15
import org.jivesoftware.util.PropertyEventListener;
16
import org.jivesoftware.util.PropertyEventDispatcher;
Daniel Henninger's avatar
Daniel Henninger committed
17
import org.xmpp.component.ComponentManager;
18

19 20
import java.util.Map;

21
/**
22
 * Transport Instance
23 24 25 26 27
 *
 * Represents all information that needs to be tracked about a gateway instance.
 *
 * @author Daniel Henninger
 */
28
public class TransportInstance implements PropertyEventListener {
29

30
    private ComponentManager componentManager;
Daniel Henninger's avatar
Daniel Henninger committed
31
    private String description = null;
32
    private String nameOfClass = null;
33
    public BaseTransport transport = null;
Daniel Henninger's avatar
Daniel Henninger committed
34
    private TransportType type = null;
35 36
    private Boolean enabled = false;
    private Boolean running = false;
37
    private String subDomain;
38

39
    /**
40
     *  Creates a new transport instance.
41
     *
Daniel Henninger's avatar
Daniel Henninger committed
42
     *  @param type Type of transport.
43
     *  @param description Short description of transport.
44 45 46
     *  @param classname Full name/path of class associated with instance.
     *  @param componentManager Component manager managing this instance.
     */
Daniel Henninger's avatar
Daniel Henninger committed
47 48 49
    public TransportInstance(TransportType type, String description, String classname, ComponentManager componentManager) {
        this.description = description;
        this.type = type;
50 51
        this.nameOfClass = classname;
        this.componentManager = componentManager;
52
        enabled = JiveGlobals.getBooleanProperty("plugin.gateway."+this.type.toString()+".enabled", false);
53
        subDomain = JiveGlobals.getProperty("plugin.gateway."+this.type.toString()+".subdomain", this.type.toString());
54 55
    }

56 57 58 59 60
    /**
     *  Retrieves the name of the service (aka, subdomain)
     *
     *  @return name of the service
     */
61
    public String getName() {
Daniel Henninger's avatar
Daniel Henninger committed
62
        return this.type.toString();
63 64
    }

65
    /**
66
     *  Returns whether this transport instance is enabled.
67 68 69
     *
     *  @return true or false if instance is enabled
     */
70 71 72 73
    public Boolean isEnabled() {
        return enabled;
    }

74
    /**
75
     *  Returns whether this transport instance is currently running.
76 77 78
     *
     *  @return true or false if instance is currently running
     */
79 80 81 82
    public Boolean isRunning() {
        return running;
    }

83
    /**
84
     *  Enables the transport instance and starts it if it's not already running.
85
     */
86 87
    public void enable() {
        enabled = true;
88
        JiveGlobals.setProperty("plugin.gateway."+this.type.toString()+".enabled", "true");
89 90 91 92 93
        if (!running) {
            startInstance();
        }
    }

94
    /**
95
     *  Disables the transport instance and stops it if it's running.
96
     */
97 98
    public void disable() {
        enabled = false;
99
        JiveGlobals.setProperty("plugin.gateway."+this.type.toString()+".enabled", "false");
100 101 102 103 104
        if (running) {
            stopInstance();
        }
    }

105
    /**
106
     *  Starts the transport instance if it's enabled and not already running.
107
     */
108 109 110 111 112
    public void startInstance() {
        if (!enabled || running) {
            return;
        }

113 114
        Log.info("Starting transport service: "+type.toString());

115
        transport = null;
116

117
        //Log.debug("Loading class "+nameOfClass);
118 119

        try {
120
            transport = (BaseTransport)Class.forName(nameOfClass).newInstance();
Daniel Henninger's avatar
Daniel Henninger committed
121
            transport.setup(this.type, this.description);
122 123 124 125 126 127 128 129 130 131 132 133
        }
        catch (ClassNotFoundException e) {
            Log.error("Unable to find class: "+nameOfClass);
        }
        catch (InstantiationException e) {
            Log.error("Unable to instantiate class: "+nameOfClass);
        }
        catch (IllegalAccessException e) {
            Log.error("Unable to access class: "+nameOfClass);
        }

        try {
134
            componentManager.addComponent(this.subDomain, transport);
135
            PropertyEventDispatcher.addListener(this);
136 137 138 139 140 141 142
            running = true;
        }
        catch (Exception e) {
            componentManager.getLog().error(e);
        }
    }

143
    /**
144
     *  Stops the transport instance if it's running.
145
     */
146 147 148 149 150
    public void stopInstance() {
        if (!running) {
            return;
        }

151 152
        Log.info("Stopping transport service: "+type.toString());

153
        PropertyEventDispatcher.removeListener(this);
154
        try {
155
            componentManager.removeComponent(this.subDomain);
156 157 158 159
        }
        catch (Exception e) {
            componentManager.getLog().error(e);
        }
160
        transport = null;
161 162
        running = false;
    }
163

164 165 166 167 168 169 170
    /**
     * Retrieves actual transport associated with this instance.
     */
    public BaseTransport getTransport() {
        return transport;
    }

171
    public void propertySet(String property, Map params) {
172 173 174 175 176 177 178 179 180 181 182 183
        if (property.startsWith("plugin.gateway.")) {
            if (property.equals("plugin.gateway."+this.type.toString()+".enabled")) {
                enabled = Boolean.parseBoolean((String)params.get("value"));
                if (enabled) {
                    if (!running) {
                        startInstance();
                    }
                }
                else {
                    if (running) {
                        stopInstance();
                    }
184 185
                }
            }
186 187 188 189 190 191 192 193
            else if (property.equals("plugin.gateway."+this.type.toString()+".subdomain")) {
                String newSubDomain = (String)params.get("value");
                if (!newSubDomain.equals(this.subDomain)) {
                    if (running) {
                        stopInstance();
                        this.subDomain = newSubDomain;
                        startInstance();
                    }
194 195 196 197 198 199
                }
            }
        }
    }

    public void propertyDeleted(String property, Map params) {
200 201
        if (property.startsWith("plugin.gateway.")) {
            if (property.equals("plugin.gateway."+this.type.toString()+".enabled")) {
202 203 204 205
                if (running) {
                    stopInstance();
                }
            }
206 207 208 209 210 211 212 213 214 215
            else if (property.equals("plugin.gateway."+this.type.toString()+".subdomain")) {
                String newSubDomain = this.type.toString();
                if (!newSubDomain.equals(this.subDomain)) {
                    if (running) {
                        stopInstance();
                        this.subDomain = newSubDomain;
                        startInstance();
                    }
                }
            }
216
        }
217 218
    }

219 220 221 222
    public void xmlPropertySet(String property, Map params) {
        propertySet(property, params);
    }

223
    public void xmlPropertyDeleted(String property, Map params) {
224
        propertyDeleted(property, params);
225 226
    }

227
}