KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > integration > spring > DefaultIoFilterChainBuilderFactoryBean


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.mina.integration.spring;
21
22 import java.util.List JavaDoc;
23
24 import org.apache.mina.common.DefaultIoFilterChainBuilder;
25 import org.apache.mina.common.IoFilter;
26 import org.springframework.beans.factory.config.AbstractFactoryBean;
27 import org.springframework.util.Assert;
28
29 /**
30  * Spring {@link org.springframework.beans.factory.FactoryBean}
31  * which creates {@link DefaultIoFilterChainBuilder} instances. This
32  * factory bean makes it possible to configure the filters to be added to all the
33  * sessions created by an {@link org.apache.mina.common.IoAcceptor}
34  * or {@link org.apache.mina.common.IoConnector} using Spring.
35  * <p>
36  * The filters may be set up in two ways. By creating
37  * {@link IoFilterMapping} objects which associate a name with an {@link IoFilter}
38  * instance and set them using {@link #setFilterMappings(IoFilterMapping[])} or
39  * by using {@link #setFilters(IoFilter[])} directly which assigns automatically
40  * generated names to each {@link IoFilter}. Use the
41  * {@link #setFilterNamePrefix(String)} method to set the prefix used for
42  * auto generated names.
43  * </p>
44  *
45  * @author The Apache Directory Project (mina-dev@directory.apache.org)
46  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
47  */

48 public class DefaultIoFilterChainBuilderFactoryBean extends AbstractFactoryBean {
49     private IoFilterMapping[] filterMappings = new IoFilterMapping[0];
50
51     private String JavaDoc prefix = "filter";
52
53     protected Object JavaDoc createInstance() throws Exception JavaDoc {
54         DefaultIoFilterChainBuilder builder = new DefaultIoFilterChainBuilder();
55         for (int i = 0; i < filterMappings.length; i++) {
56             String JavaDoc name = filterMappings[i].getName();
57             if (name == null) {
58                 name = prefix + i;
59             }
60             builder.addLast(name, filterMappings[i].getFilter());
61         }
62
63         return builder;
64     }
65
66     public Class JavaDoc getObjectType() {
67         return DefaultIoFilterChainBuilder.class;
68     }
69
70     /**
71      * Sets the prefix used to create the names for automatically named filters
72      * added using {@link #setFilters(IoFilter[])}. The default prefix is
73      * <tt>filter</tt>.
74      *
75      * @param prefix the prefix.
76      * @throws IllegalArgumentException if the specified value is
77      * <code>null</code>.
78      */

79     public void setFilterNamePrefix(String JavaDoc prefix) {
80         Assert.notNull(prefix, "Property 'filterNamePrefix' may not be null");
81         this.prefix = prefix;
82     }
83
84     /**
85      * Sets a number of filters which will be added to the filter
86      * chain created by this factory bean. The specified list must contain either
87      * {@link IoFilter} or {@link IoFilterMapping} objects. Filters which
88      * haven't been wrapped in {@link IoFilterMapping} objects will be assigned
89      * automatically generated names (<code>&lt;filterNamePrefix&gt;0</code>,
90      * <code>&lt;filterNamePrefix&gt;1</code>, etc).
91      *
92      * @param filters the list of {@link IoFilter} and/or
93      * {@link IoFilterMapping} objects.
94      * @throws IllegalArgumentException if the specified value is
95      * <code>null</code> or contains objects of the wrong type.
96      * @see #setFilterNamePrefix(String)
97      */

98     public void setFilters(List JavaDoc filters) {
99         Assert.notNull(filters, "Property 'filters' may not be null");
100         IoFilterMapping[] filterMappings = new IoFilterMapping[filters.size()];
101
102         for (int i = 0; i < filterMappings.length; i++) {
103             Object JavaDoc o = filters.get(i);
104             if (o instanceof IoFilterMapping) {
105                 filterMappings[i] = (IoFilterMapping) o;
106             } else if (o instanceof IoFilter) {
107                 filterMappings[i] = new IoFilterMapping();
108                 filterMappings[i].setFilter((IoFilter) o);
109             } else {
110                 throw new IllegalArgumentException JavaDoc(
111                         "List may only contain "
112                                 + "IoFilter or IoFilterMapping objects. Found object of "
113                                 + "type " + o.getClass().getName()
114                                 + " at position " + i + ".");
115             }
116         }
117
118         this.filterMappings = filterMappings;
119     }
120
121 }
122
Popular Tags