KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.google.inject.Injector;
20 import com.google.inject.Provider;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.directwebremoting.AjaxFilter;
27 import org.directwebremoting.impl.DefaultAjaxFilterManager;
28 import org.directwebremoting.extend.*;
29 import org.directwebremoting.util.Logger;
30
31 import static org.directwebremoting.guice.DwrGuiceUtil.getInjector;
32 import static org.directwebremoting.guice.DwrGuiceUtil.getServletContext;
33
34 /**
35  * Extends an existing ajax filter manager with an injected list of ajax filters
36  * specified at Guice bind-time. Only to be used in conjection with
37  * {@link DwrGuiceServlet}.
38  * @author Tim Peierls [tim at peierls dot net]
39  */

40 public class InternalAjaxFilterManager implements AjaxFilterManager
41 {
42     /**
43      * Retrieves an underlying ajaxFilter manager from thread-local state
44      * to which this class delegates {@link AjaxFilterManager} calls.
45      */

46     public InternalAjaxFilterManager()
47     {
48         this.ajaxFilterManager = getAjaxFilterManager();
49         addAjaxFilters();
50     }
51
52     public Iterator JavaDoc<AjaxFilter> getAjaxFilters(String JavaDoc scriptname)
53     {
54         return (Iterator JavaDoc<AjaxFilter>) ajaxFilterManager.getAjaxFilters(scriptname);
55     }
56
57     public void addAjaxFilter(AjaxFilter filter)
58     {
59         ajaxFilterManager.addAjaxFilter(filter);
60     }
61
62     public void addAjaxFilter(AjaxFilter filter, String JavaDoc scriptname)
63     {
64         ajaxFilterManager.addAjaxFilter(filter, scriptname);
65     }
66
67     
68     private final AjaxFilterManager ajaxFilterManager;
69
70     private void addAjaxFilters()
71     {
72         Injector injector = getInjector();
73         for (Key<?> key : injector.getBindings().keySet())
74         {
75             Class JavaDoc<?> atype = key.getAnnotationType();
76             if (atype != null && Filtering.class.isAssignableFrom(atype))
77             {
78                 String JavaDoc scriptName = Filtering.class.cast(key.getAnnotation()).value();
79                 Provider<AjaxFilter> provider = injector.getProvider((Key<AjaxFilter>) key);
80                 if ("".equals(scriptName))
81                 {
82                     addAjaxFilter(new InternalAjaxFilter(provider));
83                 }
84                 else
85                 {
86                     addAjaxFilter(new InternalAjaxFilter(provider), scriptName);
87                 }
88             }
89         }
90     }
91
92     
93     /**
94      * Stores a type name in a thread-local variable for later retrieval by
95      * {@code getAjaxFilterManager}.
96      */

97     static void setTypeName(String JavaDoc name)
98     {
99         typeName.set(name);
100     }
101     
102     private static AjaxFilterManager getAjaxFilterManager()
103     {
104         String JavaDoc name = typeName.get();
105         try
106         {
107             Class JavaDoc<? extends AjaxFilterManager> cls =
108                 (Class JavaDoc<? extends AjaxFilterManager>) Class.forName(name);
109             return cls.newInstance();
110         }
111         catch (Exception JavaDoc e)
112         {
113             if (name != null && !"".equals(name)) {
114                 log.warn("Couldn't make AjaxFilterManager from type: " + name);
115             }
116             return new DefaultAjaxFilterManager();
117         }
118     }
119
120
121     /**
122      * Place to stash a type name for retrieval in same thread.
123      */

124     private static final ThreadLocal JavaDoc<String JavaDoc> typeName = new ThreadLocal JavaDoc<String JavaDoc>();
125
126
127     /**
128      * The log stream
129      */

130     private static final Logger log = Logger.getLogger(InternalAjaxFilterManager.class);
131 }
132
Popular Tags