MUCServiceProperties.java 11.3 KB
Newer Older
1 2
/**
 * $RCSfile$
3 4
 * $Revision: 3144 $
 * $Date: 2005-12-01 14:20:11 -0300 (Thu, 01 Dec 2005) $
5
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * 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.
19 20 21 22 23 24 25 26
 */

package org.jivesoftware.openfire.muc.spi;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
27 28 29 30 31 32
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
33
import java.util.concurrent.ConcurrentHashMap;
34 35 36 37 38 39 40

import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.cluster.MUCServicePropertyClusterEventTask;
import org.jivesoftware.util.cache.CacheFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
41 42 43 44 45 46 47 48

/**
 * Retrieves and stores MUC service properties. Properties are stored in the database.
 *
 * @author Daniel Henninger
 */
public class MUCServiceProperties implements Map<String, String> {

49 50
	private static final Logger Log = LoggerFactory.getLogger(MUCServiceProperties.class);

51 52 53 54
    private static final String LOAD_PROPERTIES = "SELECT name, propValue FROM ofMucServiceProp WHERE serviceID=?";
    private static final String INSERT_PROPERTY = "INSERT INTO ofMucServiceProp(serviceID, name, propValue) VALUES(?,?,?)";
    private static final String UPDATE_PROPERTY = "UPDATE ofMucServiceProp SET propValue=? WHERE serviceID=? AND name=?";
    private static final String DELETE_PROPERTY = "DELETE FROM ofMucServiceProp WHERE serviceID=? AND name=?";
55 56 57 58 59 60 61 62

    private String subdomain;
    private Long serviceID;
    private Map<String, String> properties;

    public MUCServiceProperties(String subdomain) {
        this.subdomain = subdomain;
        if (properties == null) {
63
            properties = new ConcurrentHashMap<>();
64 65 66 67 68
        }
        else {
            properties.clear();
        }

69 70
        serviceID = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatServiceID(subdomain);
        if (serviceID == null) {
71 72
            Log.debug("MUCServiceProperties: Unable to find service ID for subdomain "+subdomain);
        }
73 74 75
        else {
            loadProperties();
        }
76 77
    }

78
    @Override
79 80 81 82
    public int size() {
        return properties.size();
    }

83
    @Override
84 85 86 87
    public void clear() {
        throw new UnsupportedOperationException();
    }

88
    @Override
89 90 91 92
    public boolean isEmpty() {
        return properties.isEmpty();
    }

93
    @Override
94 95 96 97
    public boolean containsKey(Object key) {
        return properties.containsKey(key);
    }

98
    @Override
99 100 101 102
    public boolean containsValue(Object value) {
        return properties.containsValue(value);
    }

103
    @Override
104 105 106 107
    public Collection<String> values() {
        return Collections.unmodifiableCollection(properties.values());
    }

108
    @Override
109 110 111 112 113 114
    public void putAll(Map<? extends String, ? extends String> t) {
        for (Map.Entry<? extends String, ? extends String> entry : t.entrySet() ) {
            put(entry.getKey(), entry.getValue());
        }
    }

115
    @Override
116 117 118 119
    public Set<Map.Entry<String, String>> entrySet() {
        return Collections.unmodifiableSet(properties.entrySet());
    }

120
    @Override
121 122 123 124
    public Set<String> keySet() {
        return Collections.unmodifiableSet(properties.keySet());
    }

125
    @Override
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    public String get(Object key) {
        return properties.get(key);
    }

    /**
     * Return all children property names of a parent property as a Collection
     * of String objects. For example, given the properties <tt>X.Y.A</tt>,
     * <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>, then the child properties of
     * <tt>X.Y</tt> are <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>. The method
     * is not recursive; ie, it does not return children of children.
     *
     * @param parentKey the name of the parent property.
     * @return all child property names for the given parent.
     */
    public Collection<String> getChildrenNames(String parentKey) {
141
        Collection<String> results = new HashSet<>();
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        for (String key : properties.keySet()) {
            if (key.startsWith(parentKey + ".")) {
                if (key.equals(parentKey)) {
                    continue;
                }
                int dotIndex = key.indexOf(".", parentKey.length()+1);
                if (dotIndex < 1) {
                    if (!results.contains(key)) {
                        results.add(key);
                    }
                }
                else {
                    String name = parentKey + key.substring(parentKey.length(), dotIndex);
                    results.add(name);
                }
            }
        }
        return results;
    }

    /**
     * Returns all property names as a Collection of String values.
     *
     * @return all property names.
     */
    public Collection<String> getPropertyNames() {
        return properties.keySet();
    }

171
    @Override
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    public String remove(Object key) {
        String value;
        synchronized (this) {
            value = properties.remove(key);
            // Also remove any children.
            Collection<String> propNames = getPropertyNames();
            for (String name : propNames) {
                if (name.startsWith((String)key)) {
                    properties.remove(name);
                }
            }
            deleteProperty((String)key);
        }

        // Generate event.
        Map<String, Object> params = Collections.emptyMap();
        MUCServicePropertyEventDispatcher.dispatchEvent(subdomain, (String)key, MUCServicePropertyEventDispatcher.EventType.property_deleted, params);

190 191
        // Send update to other cluster members.
        CacheFactory.doClusterTask(MUCServicePropertyClusterEventTask.createDeleteTask(subdomain, (String) key));
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

        return value;
    }

    void localRemove(String key) {
        properties.remove(key);
        // Also remove any children.
        Collection<String> propNames = getPropertyNames();
        for (String name : propNames) {
            if (name.startsWith(key)) {
                properties.remove(name);
            }
        }

        // Generate event.
        Map<String, Object> params = Collections.emptyMap();
        MUCServicePropertyEventDispatcher.dispatchEvent(subdomain, key, MUCServicePropertyEventDispatcher.EventType.property_deleted, params);
    }

211
    @Override
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    public String put(String key, String value) {
        if (key == null || value == null) {
            throw new NullPointerException("Key or value cannot be null. Key=" +
                    key + ", value=" + value);
        }
        if (key.endsWith(".")) {
            key = key.substring(0, key.length()-1);
        }
        key = key.trim();
        String result;
        synchronized (this) {
            if (properties.containsKey(key)) {
                if (!properties.get(key).equals(value)) {
                    updateProperty(key, value);
                }
            }
            else {
                insertProperty(key, value);
            }

            result = properties.put(key, value);
        }

        // Generate event.
236
        Map<String, Object> params = new HashMap<>();
237 238 239
        params.put("value", value);
        MUCServicePropertyEventDispatcher.dispatchEvent(subdomain, key, MUCServicePropertyEventDispatcher.EventType.property_set, params);

240 241
        // Send update to other cluster members.
        CacheFactory.doClusterTask(MUCServicePropertyClusterEventTask.createPutTask(subdomain, key, value));
242 243 244 245 246 247 248 249

        return result;
    }

    void localPut(String key, String value) {
        properties.put(key, value);

        // Generate event.
250
        Map<String, Object> params = new HashMap<>();
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        params.put("value", value);
        MUCServicePropertyEventDispatcher.dispatchEvent(subdomain, key, MUCServicePropertyEventDispatcher.EventType.property_set, params);
    }

    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;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_PROPERTY);
            pstmt.setLong(1, serviceID);
            pstmt.setString(2, name);
            pstmt.setString(3, value);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
291
            Log.error(e.getMessage(), e);
292 293
        }
        finally {
294
            DbConnectionManager.closeConnection(pstmt, con);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        }
    }

    private void updateProperty(String name, String value) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_PROPERTY);
            pstmt.setString(1, value);
            pstmt.setLong(2, serviceID);
            pstmt.setString(3, name);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
310
            Log.error(e.getMessage(), e);
311 312
        }
        finally {
313
            DbConnectionManager.closeConnection(pstmt, con);
314 315 316 317 318 319 320 321 322 323 324 325 326 327
        }
    }

    private void deleteProperty(String name) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_PROPERTY);
            pstmt.setLong(1, serviceID);
            pstmt.setString(2, name);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
328
            Log.error(e.getMessage(), e);
329 330
        }
        finally {
331
            DbConnectionManager.closeConnection(pstmt, con);
332 333 334 335 336 337
        }
    }

    private void loadProperties() {
        Connection con = null;
        PreparedStatement pstmt = null;
338
        ResultSet rs = null;
339 340 341 342
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_PROPERTIES);
            pstmt.setLong(1, serviceID);
343
            rs = pstmt.executeQuery();
344 345 346 347 348 349 350
            while (rs.next()) {
                String name = rs.getString(1);
                String value = rs.getString(2);
                properties.put(name, value);
            }
        }
        catch (Exception e) {
351
            Log.error(e.getMessage(), e);
352 353
        }
        finally {
354
            DbConnectionManager.closeConnection(rs, pstmt, con);
355 356 357
        }
    }
}