KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > util > LocatorUtils


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.kernel.util;
66
67 import com.jcorporate.expresso.kernel.Containable;
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.digester.ComponentConfig;
72 import com.jcorporate.expresso.kernel.metadata.ComponentMetadata;
73
74 import java.util.ArrayList JavaDoc;
75 import java.util.ListIterator JavaDoc;
76 import java.util.Map JavaDoc;
77
78
79 /**
80  * Locator Utils are to allow walking the component hierarchy tree so you
81  * can reference components by a 'path name' instead of walking it manually
82  * yourself
83  * <p>Beacuse there are potentially more than one Runtime Environment in a Virtual
84  * Machine, you must associate a LocatorUtils object with any component within
85  * a tree. Once it has that figured out, it can locate any other component
86  * in the tree</p>
87  *
88  * @author Michael Rimov
89  * @version $Revision: 1.9 $ on $Date: 2004/11/17 20:48:17 $
90  * @since Expresso 5.1
91  */

92
93 public class LocatorUtils {
94     ExpressoComponent root = null;
95
96     /**
97      * Base constructor that locates the root component in the hierarchy as
98      * soon as it is instantiated.
99      *
100      * @param component any component in the Runtime Hierarchy
101      */

102     public LocatorUtils(ExpressoComponent component) {
103         if (component == null) {
104             throw new IllegalArgumentException JavaDoc("Must have a non-null argument for parameter 'component'");
105         }
106         boolean done = false;
107         Containable parent = null;
108         while (!done) {
109             parent = component.getParent();
110             if (parent == null) {
111                 done = true;
112                 root = component;
113             } else {
114                 component = parent;
115             }
116         }
117     }
118
119
120     /**
121      * <p>Allows you to locate the component via a 'bean utils' style string.</p>
122      * <p>Examples:</p>
123      * <p>Locate a component in default context by the name of myComponent:<br />
124      * ExpressoComponent mycomponent = locator.locateComponent("default.myComponent");</p>
125      * <p>Locate the root container:<br />
126      * ExpressoComponent rootContainer = locator.locateComponent("");</p>
127      *
128      * @param pathName The pathname to use to locate the particular component as
129      * described in the above javadocs.
130      * @return ExpressoComponent found
131      * @throws IllegalArgumentException if either the pathName is improper or
132      * if the component doesn't seem to exist in the given traversal
133      */

134     public ExpressoComponent locateComponent(String JavaDoc pathName) {
135         if (pathName == null || pathName.length() == 0) {
136             return root;
137         }
138
139         String JavaDoc stringLeft = pathName;
140         String JavaDoc singleComponent = null;
141         int splitPoint = 0;
142         ExpressoComponent currentComponent = root;
143         do {
144             splitPoint = stringLeft.indexOf(".");
145             if (splitPoint == 0) {
146                 throw new IllegalArgumentException JavaDoc("Malformed Path Request: " + pathName);
147             }
148             if (splitPoint == -1) {
149                 singleComponent = stringLeft;
150                 stringLeft = "";
151             } else {
152                 singleComponent = stringLeft.substring(0, splitPoint);
153                 stringLeft = stringLeft.substring(splitPoint + 1);
154             }
155
156             if (!(currentComponent instanceof Containable)) {
157                 throw new IllegalArgumentException JavaDoc("Can't navigate below component: "
158                         + currentComponent.getMetaData().getName() +
159                         "it is not a component container");
160             }
161
162             Containable currentContainer = (Containable) currentComponent;
163             Map JavaDoc children = currentContainer.getContainerImplementation().getChildComponents();
164             if (!children.containsKey(singleComponent)) {
165                 throw new IllegalArgumentException JavaDoc("Unable to locate subcomponent " +
166                         singleComponent + " as a subcomponent of " + currentComponent.getMetaData().getName());
167             }
168
169             currentComponent = (ExpressoComponent) children.get(singleComponent);
170
171         } while (splitPoint > -1);
172
173         return currentComponent;
174     }
175
176
177     /**
178      * <p>Allows you to locate the component via a 'bean utils' style string.</p>
179      * <p>Examples:</p>
180      * <p>Locate a components configuration in default context by the name of myComponent:<br />
181      * ComponentConfig myconfig = locator.locateComponent("default.myComponent");</p>
182      * <p>Locate the root container:<br />
183      * ComponentConfig rootContainerConfig = locator.locateComponent("");</p>
184      *
185      * @param pathName The pathname to use to locate the particular component as
186      * described in the above javadocs.
187      * @return ComponentConfig found
188      * @throws IllegalArgumentException if either the pathName is improper or
189      * if the component doesn't seem to exist in the given traversal
190      */

191     public ComponentConfig locateConfiguration(String JavaDoc pathName) {
192         if (pathName == null || pathName.length() == 0) {
193             return ((RootContainerInterface) root).getExpressoServicesConfig().getRootConfig();
194         }
195
196         String JavaDoc stringLeft = pathName;
197         String JavaDoc singleComponent = null;
198         int splitPoint = 0;
199         ComponentConfig currentComponent = ((RootContainerInterface) root)
200                 .getExpressoServicesConfig().getRootConfig();
201         do {
202             splitPoint = stringLeft.indexOf(".");
203             if (splitPoint == 0) {
204                 throw new IllegalArgumentException JavaDoc("Malformed Path Request: " + pathName);
205             }
206             if (splitPoint == -1) {
207                 singleComponent = stringLeft;
208                 stringLeft = "";
209             } else {
210                 singleComponent = stringLeft.substring(0, splitPoint);
211                 stringLeft = stringLeft.substring(splitPoint + 1);
212             }
213
214             ComponentConfig testconfig = currentComponent.getChildComponent(singleComponent);
215             if (testconfig == null) {
216                 throw new IllegalArgumentException JavaDoc("Unable to locate subcomponent configuration " +
217                         singleComponent + " as a subcomponent of " + currentComponent.getName());
218
219             }
220             currentComponent = testconfig;
221
222         } while (splitPoint > -1);
223
224         return currentComponent;
225     }
226
227     /**
228      * <p>Allows you to locate the component metadata via a 'bean utils' style string.</p>
229      * <p>Examples:</p>
230      * <p>Locate a components configuration in default context by the name of myComponent:<br />
231      * ComponentConfig myconfig = locator.locateComponent("default.myComponent");</p>
232      * <p>Locate the root container:<br />
233      * ComponentConfig rootContainerConfig = locator.locateComponent("");</p>
234      *
235      * @param pathName The pathname to use to locate the particular component as
236      * described in the above javadocs.
237      * @return ComponentMetadata found
238      * @throws IllegalArgumentException if either the pathName is improper or
239      * if the component doesn't seem to exist in the given traversal
240      */

241     public ComponentMetadata locateMetadata(String JavaDoc pathName) {
242         ExpressoComponent component = this.locateComponent(pathName);
243         return component.getMetaData();
244     }
245
246
247     /**
248      * Return a path that the Locator utils could find later to relocate this
249      * component.
250      *
251      * @param component The component to get the location string.
252      * @return java.lang.String possibly empty string if the component is root.
253      */

254     public String JavaDoc getPath(ExpressoComponent component) {
255         Containable c = component.getParent();
256         if (c == null) {
257             return "";
258         }
259
260         ExpressoComponent currentComponent = component;
261         ArrayList JavaDoc al = new ArrayList JavaDoc();
262         //Traverse up the tree
263
while (c != null) {
264             al.add(currentComponent.getMetaData().getName());
265             currentComponent = c;
266             c = currentComponent.getParent();
267         }
268
269         //Now write the info in reverse
270
FastStringBuffer path = FastStringBuffer.getInstance();
271         boolean needPeriod = false;
272         for (ListIterator JavaDoc i = al.listIterator(al.size()); i.hasPrevious();) {
273             String JavaDoc value = (String JavaDoc) i.previous();
274             if (needPeriod) {
275                 path.append(".");
276             } else {
277                 needPeriod = true;
278             }
279
280             path.append(value);
281         }
282
283         String JavaDoc returnValue = path.toString();
284         path.release();
285         return returnValue;
286     }
287
288     /**
289      * Given the component that we loaded, find the data context name we're a
290      * part of.
291      *
292      * @param component the name of the component to retrieve the context name
293      * @return java.util.String of the data context name or null if the defined
294      * component is not embedded within a data context.
295      */

296     public String JavaDoc getDataContextName(ExpressoComponent component) {
297         return getDataContext(component).getMetaData().getName();
298
299     }
300
301
302     /**
303      * Retrieve the DataContext that the given component is a part of
304      *
305      * @param component the component in the hierarchy that we want to find
306      * what data context it is a part of.
307      * @return DataContext or null if the component is not part of a data context.
308      */

309     public DataContext getDataContext(ExpressoComponent component) {
310         if (component == null) {
311             throw new IllegalArgumentException JavaDoc("Must have a non-null argument for parameter 'component'");
312         }
313         boolean done = false;
314         Containable parent = null;
315         while (!done) {
316             parent = component.getParent();
317             if (parent instanceof DataContext) {
318                 return (DataContext) parent;
319             }
320
321             if (parent == null) {
322                 done = true;
323             }
324             component = parent;
325         }
326
327         return null;
328     }
329 }
330
331
Popular Tags