KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > guice > AbstractSimpleContextScope


1 /*
2  * Copyright 2007 Tim Peierls
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.guice;
17
18 import com.google.inject.Key;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22
23 /**
24  * A specialization of {@link AbstractContextScope} for the case when
25  * the context identifier itself can serve as a string-keyed instance registry
26  * using synchronization on the context to provide atomic put-if-absent
27  * and remove-specific-value behavior.
28  * @author Tim Peierls [tim at peierls dot net]
29  */

30 public abstract class AbstractSimpleContextScope<C> extends AbstractContextScope<C, C>
31 {
32     protected AbstractSimpleContextScope(Class JavaDoc<C> type, String JavaDoc scopeName)
33     {
34         super(type, scopeName);
35     }
36
37     public abstract C get();
38     
39     
40     //
41
// These methods are restricted to String lookup of plain Objects.
42
//
43

44     public abstract Object JavaDoc get(C registry, String JavaDoc keyString);
45     
46     public abstract void put(C registry, String JavaDoc keyString, Object JavaDoc creator);
47     
48     
49     //
50
// ContextRegistry methods
51
//
52

53     public C registryFor(C context)
54     {
55         return context;
56     }
57
58     @SuppressWarnings JavaDoc("unchecked")
59     public <T> InstanceProvider<T> get(C registry, Key<T> key, String JavaDoc keyString)
60     {
61         return (InstanceProvider<T>) get(registry, keyString);
62     }
63     
64     public <T> void put(C registry, Key<T> key, String JavaDoc keyString, InstanceProvider<T> creator)
65     {
66         put(registry, keyString, creator);
67     }
68     
69     public <T> InstanceProvider<T> putIfAbsent(C registry, Key<T> key, String JavaDoc keyString,
70                                                InstanceProvider<T> creator)
71     {
72         synchronized (registry)
73         {
74             InstanceProvider<T> t = get(registry, key, keyString);
75             if (t != null)
76             {
77                 return t;
78             }
79             else
80             {
81                 put(registry, key, keyString, creator);
82                 return null;
83             }
84         }
85     }
86     
87     public <T> boolean remove(C registry, Key<T> key, String JavaDoc keyString,
88                               InstanceProvider<T> creator)
89     {
90         synchronized (registry)
91         {
92             InstanceProvider<T> t = get(registry, key, keyString);
93             if (t == creator)
94             {
95                 // Assumes put(..., null) is equivalent to remove(...)
96
put(registry, keyString, null);
97                 return true;
98             }
99             else
100             {
101                 return false;
102             }
103         }
104     }
105 }
106
Popular Tags