001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.cluster;
030:
031: import com.caucho.util.LruCache;
032:
033: import java.util.Hashtable;
034:
035: /**
036: * Manages the object backing for a context.
037: */
038: public class BackingManager {
039: private Hashtable<String, BackingContext> _contextMap = new Hashtable<String, BackingContext>();
040:
041: private LruCache<BackingKey, ObjectBacking> _backingCache = new LruCache<BackingKey, ObjectBacking>(
042: 1024);
043:
044: private BackingKey _key = new BackingKey();
045:
046: /**
047: * Returns the object backing for a given contextId, objectId pair.
048: */
049: public ObjectBacking getBacking(String contextId, String objectId) {
050: synchronized (this ) {
051: _key.init(contextId, objectId);
052:
053: ObjectBacking backing = _backingCache.get(_key);
054:
055: if (backing != null)
056: return backing;
057:
058: BackingContext context = getContext(contextId);
059:
060: if (context == null)
061: return null;
062:
063: backing = context.getBacking(objectId);
064:
065: _backingCache.put(new BackingKey(contextId, objectId),
066: backing);
067:
068: return backing;
069: }
070: }
071:
072: /**
073: * Returns the named backing context.
074: */
075: public BackingContext getContext(String contextId) {
076: return _contextMap.get(contextId);
077: }
078:
079: /**
080: * Sets the named backing context.
081: */
082: public void setContext(String contextId, BackingContext context) {
083: _contextMap.put(contextId, context);
084: }
085:
086: /**
087: * Removes the named backing context.
088: */
089: public void removeContext(String contextId) {
090: _contextMap.remove(contextId);
091: }
092:
093: static class BackingKey {
094: private String _contextId;
095: private String _objectId;
096:
097: BackingKey() {
098: }
099:
100: BackingKey(String contextId, String objectId) {
101: _contextId = contextId;
102: _objectId = objectId;
103: }
104:
105: void init(String contextId, String objectId) {
106: _contextId = contextId;
107: _objectId = objectId;
108: }
109:
110: public int hashCode() {
111: return _contextId.hashCode() * 65521 + _objectId.hashCode();
112: }
113:
114: public boolean equals(Object o) {
115: if (!(o instanceof BackingKey))
116: return false;
117:
118: BackingKey key = (BackingKey) o;
119:
120: return (_contextId.equals(key._contextId) && _objectId
121: .equals(key._objectId));
122: }
123: }
124: }
|