KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > iterator > IteratorFactory


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.util.iterator;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedHashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import javax.sql.RowSet JavaDoc;
28
29 import org.apache.beehive.netui.util.config.ConfigUtil;
30 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument.NetuiConfig;
31 import org.apache.beehive.netui.util.config.bean.IteratorFactories;
32 import org.apache.beehive.netui.util.logging.Logger;
33
34 /**
35  * <p>
36  * This class provides a factory that can create an {@link Iterator} for various types
37  * of Java objects. Supported types include:
38  * <ul>
39  * <li>{@link java.util.Iterator}</li>
40  * <li>{@link java.util.Collection}</li>
41  * <li>{@link java.util.Map}</li>
42  * <li>{@link java.sql.ResultSet}</li>
43  * <li>{@link javax.sql.RowSet}</li>
44  * <li>{@link java.util.Enumeration}</li>
45  * <li>Any Java Object array</li>
46  * </ul>
47  * <p/>
48  * <p>
49  * If an object type not listed above is supplied the object will be wrapped in
50  * an iterator that contains only the provided object.
51  * </p>
52  */

53 public class IteratorFactory {
54
55     /**
56      * Convenience field for accessing an empty {@link Iterator}.
57      */

58     public static final Iterator JavaDoc EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator();
59
60     private static final Logger LOGGER = Logger.getInstance(IteratorFactory.class);
61     private static final LinkedHashMap JavaDoc ITERATOR_FACTORIES;
62
63     static {
64         ITERATOR_FACTORIES = new LinkedHashMap JavaDoc();
65         initialize();
66     }
67
68     /**
69      * @exclude
70      */

71     public abstract static class IteratorPlant {
72         /**
73          * If it is possible to create an iterator for this type, do so.
74          * Otherwise return null.
75          */

76         public abstract Iterator JavaDoc createIterator(Object JavaDoc value);
77     }
78
79     /**
80      * Create a new {@link Iterator} for the supplied object.
81      *
82      * @param object the object to build an iterator from
83      * @return an {@link Iterator} for the <code>object</code> or <code>null</code> if the value is null.
84      */

85     public static final Iterator JavaDoc createIterator(Object JavaDoc object) {
86         LOGGER.debug("Create an iterator for class: " + (object == null ? "null" : object.getClass().getName()));
87
88         if(object == null)
89             return null;
90
91         if(object instanceof Iterator JavaDoc) {
92             return (Iterator JavaDoc)object;
93         } else if(object instanceof Collection JavaDoc) {
94             Collection JavaDoc collection = (Collection JavaDoc)object;
95             return collection.iterator();
96         } else if(object instanceof Map JavaDoc) {
97             return new MapIterator((Map JavaDoc)object);
98         } else if(object.getClass().isArray()) {
99             return new ArrayIterator(object);
100         } else if(object instanceof Enumeration JavaDoc)
101             return new EnumerationIterator((Enumeration JavaDoc)object);
102         else if(object instanceof ResultSet JavaDoc && !(object instanceof RowSet JavaDoc))
103             return new ResultSetIterator((ResultSet JavaDoc)object);
104
105         // check to see if there is a registered IteratorPlant that can handle this type
106
Iterator JavaDoc ret = null;
107         Iterator JavaDoc factories = ITERATOR_FACTORIES.keySet().iterator();
108         while(factories.hasNext()) {
109             IteratorPlant plant = (IteratorPlant)ITERATOR_FACTORIES.get(factories.next());
110             ret = plant.createIterator(object);
111
112             if(ret != null) return ret;
113         }
114
115         return new AtomicObjectIterator(object);
116     }
117
118     /**
119      * Initialize the configuration parameters used to build Iterator objects
120      * for custom types.
121      */

122     private static final void initialize() {
123         Map JavaDoc map = readFromConfig();
124         if(map != null)
125             loadFactories(map);
126     }
127
128     private static final Map JavaDoc readFromConfig() {
129         NetuiConfig config = ConfigUtil.getConfig();
130         if(config == null)
131             return null;
132
133         IteratorFactories factories = config.getIteratorFactories();
134         if(factories == null)
135             return null;
136
137         org.apache.beehive.netui.util.config.bean.IteratorFactories.IteratorFactory[] factoryArray =
138             factories.getIteratorFactoryArray();
139         if(factoryArray == null)
140             return null;
141
142         LinkedHashMap JavaDoc map = new LinkedHashMap JavaDoc();
143         for(int i = 0; i < factoryArray.length; i++) {
144             map.put(factoryArray[i].getName(), factoryArray[i].getFactoryClass());
145         }
146
147         return map;
148     }
149
150     private static final void loadFactories(Map JavaDoc factories) {
151         Iterator JavaDoc iterator = factories.keySet().iterator();
152         while(iterator.hasNext()) {
153             String JavaDoc name = (String JavaDoc)iterator.next();
154             String JavaDoc className = (String JavaDoc)factories.get(name);
155             IteratorPlant plant = null;
156
157             try {
158                 Class JavaDoc type = Class.forName(className);
159                 plant = (IteratorPlant)type.newInstance();
160             } catch(ClassNotFoundException JavaDoc cnf) {
161                 LOGGER.warn("Could not create an IteratorPlant for type \"" + className + "\" because the implementation class could not be found.");
162                 continue;
163             } catch(Exception JavaDoc e) {
164                 assert e instanceof InstantiationException JavaDoc || e instanceof IllegalAccessException JavaDoc;
165                 LOGGER.warn("Could not create an IteratorPlant for type \"" + className + "\" because an error occurred creating the plant. Cause: " + e, e);
166                 continue;
167             }
168
169             if(ITERATOR_FACTORIES.containsKey(name)) {
170                 LOGGER.warn("Overwriting a previously defined IteratorPlant named \"" + name + "\" with a new IteratorPlant of type \"" + className + "\"");
171             } else LOGGER.info("Adding an IteratorPlant named \"" + name + "\" with implementation \"" + className + "\"");
172
173             ITERATOR_FACTORIES.put(name, plant);
174         }
175     }
176 }
177
Popular Tags