KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > cluster > BackingManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.cluster;
30
31 import com.caucho.util.LruCache;
32
33 import java.util.Hashtable JavaDoc;
34
35 /**
36  * Manages the object backing for a context.
37  */

38 public class BackingManager {
39   private Hashtable JavaDoc<String JavaDoc,BackingContext> _contextMap =
40     new Hashtable JavaDoc<String JavaDoc,BackingContext>();
41   
42   private LruCache<BackingKey,ObjectBacking> _backingCache =
43     new LruCache<BackingKey,ObjectBacking>(1024);
44   
45   private BackingKey _key = new BackingKey();
46
47   /**
48    * Returns the object backing for a given contextId, objectId pair.
49    */

50   public ObjectBacking getBacking(String JavaDoc contextId, String JavaDoc objectId)
51   {
52     synchronized (this) {
53       _key.init(contextId, objectId);
54       
55       ObjectBacking backing = _backingCache.get(_key);
56
57       if (backing != null)
58         return backing;
59
60       BackingContext context = getContext(contextId);
61       
62       if (context == null)
63         return null;
64       
65       backing = context.getBacking(objectId);
66
67       _backingCache.put(new BackingKey(contextId, objectId), backing);
68
69       return backing;
70     }
71   }
72
73   /**
74    * Returns the named backing context.
75    */

76   public BackingContext getContext(String JavaDoc contextId)
77   {
78     return _contextMap.get(contextId);
79   }
80   
81   /**
82    * Sets the named backing context.
83    */

84   public void setContext(String JavaDoc contextId, BackingContext context)
85   {
86     _contextMap.put(contextId, context);
87   }
88   
89   /**
90    * Removes the named backing context.
91    */

92   public void removeContext(String JavaDoc contextId)
93   {
94     _contextMap.remove(contextId);
95   }
96
97   static class BackingKey {
98     private String JavaDoc _contextId;
99     private String JavaDoc _objectId;
100
101     BackingKey()
102     {
103     }
104     
105     BackingKey(String JavaDoc contextId, String JavaDoc objectId)
106     {
107       _contextId = contextId;
108       _objectId = objectId;
109     }
110
111     void init(String JavaDoc contextId, String JavaDoc objectId)
112     {
113       _contextId = contextId;
114       _objectId = objectId;
115     }
116
117     public int hashCode()
118     {
119       return _contextId.hashCode() * 65521 + _objectId.hashCode();
120     }
121
122     public boolean equals(Object JavaDoc o)
123     {
124       if (! (o instanceof BackingKey))
125         return false;
126
127       BackingKey key = (BackingKey) o;
128
129       return (_contextId.equals(key._contextId) &&
130               _objectId.equals(key._objectId));
131     }
132   }
133 }
134
Popular Tags