001: /*
002: * Copyright 2007 Tim Peierls
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.guice;
017:
018: import java.util.Iterator;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.directwebremoting.AjaxFilter;
023: import org.directwebremoting.extend.AjaxFilterManager;
024: import org.directwebremoting.impl.DefaultAjaxFilterManager;
025:
026: import com.google.inject.Injector;
027: import com.google.inject.Key;
028: import com.google.inject.Provider;
029:
030: import static org.directwebremoting.guice.DwrGuiceUtil.getInjector;
031:
032: /**
033: * Extends an existing ajax filter manager with an injected list of ajax filters
034: * specified at Guice bind-time. Only to be used in conjection with
035: * {@link DwrGuiceServlet}.
036: * @author Tim Peierls [tim at peierls dot net]
037: */
038: public class InternalAjaxFilterManager implements AjaxFilterManager {
039: /**
040: * Retrieves an underlying ajaxFilter manager from thread-local state
041: * to which this class delegates {@link AjaxFilterManager} calls.
042: */
043: public InternalAjaxFilterManager() {
044: this .ajaxFilterManager = getAjaxFilterManager();
045: addAjaxFilters();
046: }
047:
048: public Iterator<AjaxFilter> getAjaxFilters(String scriptname) {
049: return ajaxFilterManager.getAjaxFilters(scriptname);
050: }
051:
052: public void addAjaxFilter(AjaxFilter filter) {
053: ajaxFilterManager.addAjaxFilter(filter);
054: }
055:
056: public void addAjaxFilter(AjaxFilter filter, String scriptname) {
057: ajaxFilterManager.addAjaxFilter(filter, scriptname);
058: }
059:
060: private final AjaxFilterManager ajaxFilterManager;
061:
062: private void addAjaxFilters() {
063: Injector injector = getInjector();
064: for (Key<?> key : injector.getBindings().keySet()) {
065: Class<?> atype = key.getAnnotationType();
066: if (atype != null
067: && Filtering.class.isAssignableFrom(atype)) {
068: String scriptName = Filtering.class.cast(
069: key.getAnnotation()).value();
070: @SuppressWarnings("unchecked")
071: Provider<AjaxFilter> provider = injector
072: .getProvider((Key<AjaxFilter>) key);
073: if ("".equals(scriptName)) {
074: addAjaxFilter(new InternalAjaxFilter(provider));
075: } else {
076: addAjaxFilter(new InternalAjaxFilter(provider),
077: scriptName);
078: }
079: }
080: }
081: }
082:
083: /**
084: * Stores a type name in a thread-local variable for later retrieval by
085: * {@code getAjaxFilterManager}.
086: */
087: static void setTypeName(String name) {
088: typeName.set(name);
089: }
090:
091: private static AjaxFilterManager getAjaxFilterManager() {
092: String name = typeName.get();
093: try {
094: @SuppressWarnings("unchecked")
095: Class<? extends AjaxFilterManager> cls = (Class<? extends AjaxFilterManager>) Class
096: .forName(name);
097: return cls.newInstance();
098: } catch (Exception e) {
099: if (name != null && !"".equals(name)) {
100: log.warn("Couldn't make AjaxFilterManager from type: "
101: + name);
102: }
103: return new DefaultAjaxFilterManager();
104: }
105: }
106:
107: /**
108: * Place to stash a type name for retrieval in same thread.
109: */
110: private static final ThreadLocal<String> typeName = new ThreadLocal<String>();
111:
112: /**
113: * The log stream
114: */
115: private static final Log log = LogFactory
116: .getLog(InternalAjaxFilterManager.class);
117: }
|