aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/jabber/extensions/colibri/ShutdownIQ.java
blob: 4df251b9d00c7920bf10eea880550f319b3ff58d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.protocol.jabber.extensions.colibri;

import org.jivesoftware.smack.packet.*;

/**
 * The IQ used to trigger the graceful shutdown mode of the videobridge or force
 * shutdown the one which receives the stanza(given that source JID is
 * authorized to do so).
 *
 * @author Pawel Domas
 */
public class ShutdownIQ
    extends IQ
{
    /**
     * XML namespace name for shutdown IQs.
     */
    final static public String NAMESPACE = ColibriConferenceIQ.NAMESPACE;

    /**
     * Force shutdown IQ element name.
     */
    final static public String FORCE_ELEMENT_NAME = "force-shutdown";

    /**
     * Graceful shutdown IQ element name.
     */
    final static public String GRACEFUL_ELEMENT_NAME = "graceful-shutdown";

    /**
     * The element name of this IQ. Either {@link #FORCE_ELEMENT_NAME} or
     * {@link #GRACEFUL_ELEMENT_NAME}.
     */
    private final String elementName;

    /**
     * Checks if given element is a valid one for <tt>ShutdownIQ</tt>.
     *
     * @param elementName the name if XML element name inside of the IQ.
     *
     * @return <tt>true</tt> if given <tt>elementName</tt> is correct for
     *         <tt>ShutdownIQ</tt>.
     */
    public static boolean isValidElementName(String elementName)
    {
        return GRACEFUL_ELEMENT_NAME.equals(elementName)
            || FORCE_ELEMENT_NAME.equals(elementName);
    }

    /**
     * Creates shutdown IQ for given element name.
     *
     * @param elementName can be {@link #FORCE_ELEMENT_NAME} or
     *        {@link #GRACEFUL_ELEMENT_NAME}
     *
     * @return new <tt>ShutdownIQ</tt> instance for given element name.
     *
     * @throws IllegalArgumentException if given element name is neither
     *         {@link #FORCE_ELEMENT_NAME} nor {@link #GRACEFUL_ELEMENT_NAME}.
     */
    public static ShutdownIQ createShutdownIQ(String elementName)
    {
        if (!isValidElementName(elementName))
        {
            throw new IllegalArgumentException(
                "Invalid element name: " + elementName);
        }

        if (GRACEFUL_ELEMENT_NAME.equals(elementName))
        {
            return createGracefulShutdownIQ();
        }
        else
        {
            return createForceShutdownIQ();
        }
    }

    /**
     * Creates and returns new instance of graceful shutdown IQ.
     */
    public static ShutdownIQ createGracefulShutdownIQ()
    {
        return new ShutdownIQ(GRACEFUL_ELEMENT_NAME);
    }

    /**
     * Creates and returns new instance of force shutdown IQ.
     */
    public static ShutdownIQ createForceShutdownIQ()
    {
        return new ShutdownIQ(FORCE_ELEMENT_NAME);
    }

    private ShutdownIQ(String elementName)
    {
        this.elementName = elementName;
    }

    /**
     * Returns <tt>true</tt> if this IQ instance is a "graceful shutdown" one.
     * Otherwise it is a force shutdown IQ.
     */
    public boolean isGracefulShutdown()
    {
        return elementName.equals(GRACEFUL_ELEMENT_NAME);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getChildElementXML()
    {
        return "<" + elementName + " xmlns='" + NAMESPACE + "' />";
    }
}