DefaultLocalCacheStrategy.java 5.5 KB
Newer Older
1
/**
2 3 4
 * $RCSfile$
 * $Revision: $
 * $Date: $
5
 *
6
 * Copyright (C) 2005-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
package org.jivesoftware.util.cache;

Gaston Dombiak's avatar
Gaston Dombiak committed
23
import org.jivesoftware.openfire.cluster.ClusterNodeInfo;
24 25 26

import java.util.Collection;
import java.util.Collections;
27
import java.util.Map;
28 29 30 31 32
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
33 34 35 36 37 38 39 40 41 42

/**
 * CacheFactoryStrategy for use in Openfire. It creates and manages local caches, and it's cluster
 * related method implementations do nothing.
 *
 * @see Cache
 * @see CacheFactory
 */
public class DefaultLocalCacheStrategy implements CacheFactoryStrategy {

43 44 45 46
    /**
     * Keep track of the locks that are currently being used.
     */
    private Map<Object, LockAndCount> locks = new ConcurrentHashMap<Object, LockAndCount>();
47 48 49 50 51 52 53 54 55 56 57 58

    public DefaultLocalCacheStrategy() {
    }

    public boolean startCluster() {
        return false;
    }

    public void stopCluster() {
    }

    public Cache createCache(String name) {
59 60 61
        // Get cache configuration from system properties or default (hardcoded) values
        long maxSize = CacheFactory.getMaxCacheSize(name);
        long lifetime = CacheFactory.getMaxCacheLifetime(name);
62
        // Create cache with located properties
63
        return new DefaultCache(name, maxSize, lifetime);
64 65 66 67
    }

    public void destroyCache(Cache cache) {
        cache.clear();
68 69 70 71
    }

    public boolean isSeniorClusterMember() {
        return true;
Gaston Dombiak's avatar
Gaston Dombiak committed
72 73 74 75
    }

    public Collection<ClusterNodeInfo> getClusterNodesInfo() {
        return Collections.emptyList();
Gaston Dombiak's avatar
Gaston Dombiak committed
76 77 78 79
    }

    public int getMaxClusterNodes() {
        return 0;
80 81
    }

82 83 84 85
    public byte[] getSeniorClusterMemberID() {
        return null;
    }

86
    public byte[] getClusterMemberID() {
87
        return new byte[0];
88 89
    }

90 91 92 93
    public long getClusterTime() {
    	return System.currentTimeMillis();
    }

94 95 96
    public void doClusterTask(final ClusterTask task) {
    }

97
    public void doClusterTask(ClusterTask task, byte[] nodeID) {
98
        throw new IllegalStateException("Cluster service is not available");
99 100
    }

101 102 103 104
    public Collection<Object> doSynchronousClusterTask(ClusterTask task, boolean includeLocalMember) {
        return Collections.emptyList();
    }

105
    public Object doSynchronousClusterTask(ClusterTask task, byte[] nodeID) {
106
        throw new IllegalStateException("Cluster service is not available");
107 108
    }

109 110 111
    public void updateCacheStats(Map<String, Cache> caches) {
    }

112 113 114 115
	public String getPluginName() {
		return "local";
	}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    public Lock getLock(Object key, Cache cache) {
        Object lockKey = key;
        if (key instanceof String) {
            lockKey = ((String) key).intern();
        }

		return new LocalLock(lockKey);
    }

	private void acquireLock(Object key) {
		ReentrantLock lock = lookupLockForAcquire(key);
		lock.lock();
	}

	private void releaseLock(Object key) {
		ReentrantLock lock = lookupLockForRelease(key);
		lock.unlock();
	}

	private ReentrantLock lookupLockForAcquire(Object key) {
        synchronized(key) {
            LockAndCount lac = locks.get(key);
            if (lac == null) {
                lac = new LockAndCount(new ReentrantLock());
                lac.count = 1;
                locks.put(key, lac);
            }
            else {
                lac.count++;
            }

            return lac.lock;
        }
149 150
    }

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	private ReentrantLock lookupLockForRelease(Object key) {
        synchronized(key) {
            LockAndCount lac = locks.get(key);
            if (lac == null) {
                throw new IllegalStateException("No lock found for object " + key);
            }

            if (lac.count <= 1) {
                locks.remove(key);
            }
            else {
                lac.count--;
            }

            return lac.lock;
        }
    }


    private class LocalLock implements Lock {
		private final Object key;

		LocalLock(Object key) {
			this.key = key;
		}

		public void lock(){
			acquireLock(key);
		}

		public void	unlock() {
			releaseLock(key);
		}

        public void	lockInterruptibly(){
			throw new UnsupportedOperationException();
		}

		public Condition newCondition(){
			throw new UnsupportedOperationException();
		}

		public boolean 	tryLock() {
			throw new UnsupportedOperationException();
		}

		public boolean 	tryLock(long time, TimeUnit unit) {
			throw new UnsupportedOperationException();
		}

	}

    private static class LockAndCount {
        final ReentrantLock lock;
        int count;

        LockAndCount(ReentrantLock lock) {
            this.lock = lock;
        }
210
    }
211 212 213 214 215

	public ClusterNodeInfo getClusterNodeInfo(byte[] nodeID) {
		// not clustered
		return null;
	}
216
}