KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > SchemaIterator


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.misc;
66
67 import com.jcorporate.expresso.core.dbobj.Schema;
68 import com.jcorporate.expresso.kernel.DataContext;
69 import com.jcorporate.expresso.kernel.ExpressoComponent;
70 import com.jcorporate.expresso.kernel.RootContainerInterface;
71 import com.jcorporate.expresso.kernel.management.ExpressoRuntimeMap;
72
73 import java.util.Iterator JavaDoc;
74
75 /**
76  * This class is constructed and used to iterate over top level schemas. It can
77  * be used instead of iterating over the previous SchemaList table.
78  * This iterator currently only iterates over top level Schemas, not schemas
79  * nested within other schemas.
80  * <p>Example Usage:<br/>
81  * <code>
82  * &nbsp;&nbsp;for (SchemaIterator it = new SchemaIterator("default","default");
83  * it.hasNext();) {<br/>
84  * &nbsp;&nbsp;&nbsp;&nbsp; Schema oneSchema = (Schema)it.next();<br />
85  * &nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Schema Description: " + oneSchema.getDefaultDescription());<br />
86  * &nbsp;&nbsp;}<br />
87  * </code>
88  * </p>
89  *
90  * @author Michael Rimov
91  * @see com.jcorporate.expresso.kernel.util.DataContextIterator
92  * @since Expresso 5.1
93  */

94
95 public class SchemaIterator implements Iterator JavaDoc {
96     DataContext context;
97     Schema next = null;
98     Iterator JavaDoc currentChild = null;
99
100     /**
101      * Default constructor that creates a schema for default data context,
102      * default runtime.
103      *
104      * @throws IllegalStateException if no runtime exists or if there is no
105      * default data context in the system. or if the component named 'default'
106      * is not a DataContext.
107      */

108     public SchemaIterator() {
109         RootContainerInterface expressoRuntime = ExpressoRuntimeMap.getDefaultRuntime();
110         if (expressoRuntime == null) {
111             throw new IllegalStateException JavaDoc("No runtimes are in memory!");
112         }
113
114         ExpressoComponent ec = expressoRuntime.getContainerImplementation()
115                 .locateComponent("default");
116
117         if (ec == null) {
118             throw new IllegalStateException JavaDoc("No default data context defined in the default runtime.");
119         }
120
121         if (!(ec instanceof DataContext)) {
122             throw new IllegalStateException JavaDoc("Component " + ec.getClass().getName()
123                     + " is not of type DataContext");
124         }
125
126         context = (DataContext) ec;
127         initIterator();
128     }
129
130     /**
131      * Constructor that specifies the Expresso Runtime as well as the data
132      * context name to iterate.
133      *
134      * @param runtime the name of the runtime ('default' for typical usage)
135      * @param contextName the name of the data context to iterate ('default' for example)
136      */

137     public SchemaIterator(String JavaDoc runtime, String JavaDoc contextName) {
138         RootContainerInterface expressoRuntime = ExpressoRuntimeMap.getRuntime(runtime);
139         if (expressoRuntime == null) {
140             throw new IllegalArgumentException JavaDoc("Cannot find runtime named: "
141                     + runtime);
142         }
143
144         ExpressoComponent ec = expressoRuntime.getContainerImplementation()
145                 .locateComponent(contextName);
146
147         if (ec == null) {
148             throw new IllegalArgumentException JavaDoc("Cannot find context named: "
149                     + contextName);
150         }
151
152         if (!(ec instanceof DataContext)) {
153             throw new IllegalArgumentException JavaDoc("Component " + ec.getClass().getName()
154                     + " is not of type DataContext");
155         }
156
157         context = (DataContext) ec;
158         initIterator();
159     }
160
161     /**
162      * Constructor that iterates over all schemas inside a context. Since
163      * there are unique instances of contexts, the Runtime is implied with
164      * the context.
165      *
166      * @param iteratingContext the DataContext instance to iterate.
167      */

168     public SchemaIterator(DataContext iteratingContext) {
169         if (iteratingContext == null) {
170             throw new IllegalArgumentException JavaDoc("Parameter iterating Context must not be null");
171         }
172         context = iteratingContext;
173         initIterator();
174     }
175
176     /**
177      * Checks if another object exists.
178      *
179      * @return true if there is another data context to iterate.
180      * @see java.util.Iterator#hasNext
181      */

182     public boolean hasNext() {
183         return (next != null) ? true : false;
184     }
185
186     /**
187      * Retrieve the Next Schema in the sequence.
188      *
189      * @return an Object that is of type com.jcorporate.expresso.core.dbobj.Schema
190      * @throws ArrayIndexOutOfBoundsException if we iterate past the end.
191      */

192     public Object JavaDoc next() {
193         if (next == null) {
194             throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc("No more contexts");
195         }
196         Object JavaDoc returnValue = next;
197         findNext();
198         return returnValue;
199     }
200
201     /**
202      * Retrieve the Next Schema in the sequence. The only difference between
203      * this and next() is that this function doesn't require a downcast.
204      *
205      * @return an Object that is of type com.jcorporate.expresso.core.dbobj.Schema
206      * @throws ArrayIndexOutOfBoundsException if we iterate past the end.
207      */

208     public Schema nextContext() {
209         if (next == null) {
210             throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc("No more contexts");
211         }
212         Schema returnValue = next;
213         findNext();
214         return returnValue;
215     }
216
217
218     /**
219      * Not implemented. Do not use.
220      */

221     public void remove() {
222         /**@todo Implement this java.util.Iterator method*/
223         throw new java.lang.UnsupportedOperationException JavaDoc("Method remove() not yet implemented.");
224     }
225
226     /**
227      * Initialize the current child iterator with children elements of the
228      * schema
229      */

230     private void initIterator() {
231         currentChild = context.getContainerImplementation()
232                 .getChildComponents().values().iterator();
233     }
234
235     /**
236      * Find and load the next values
237      */

238     private void findNext() {
239         next = null;
240         while (currentChild.hasNext()) {
241             Object JavaDoc oneObj = currentChild.next();
242             if (oneObj instanceof Schema) {
243                 next = (Schema) oneObj;
244                 break;
245             }
246         }
247
248     }
249 }
Popular Tags