KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > Environment


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import java.util.Iterator JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import org.openide.filesystems.FileObject;
25 import org.openide.util.Lookup;
26
27 /** Utilities that define the "settings hierarchy".
28  *
29  * @author Jaroslav Tulach
30  * @since 1.9
31  */

32 public final class Environment extends Object JavaDoc {
33     /** Result of query for all instances of Environment.Provider */
34     private static Lookup.Result result;
35
36     /** Constructor
37      */

38     private Environment() {
39     }
40
41     /** Finds a lookup for given data object.
42      * @param obj the object
43      * @return the lookup for this data object
44      */

45     public static Lookup find (DataObject obj) {
46         while (obj != null) {
47             Lookup l = findForOne (obj);
48             if (l != null) {
49                 return l;
50             }
51             
52             FileObject fo = obj.getPrimaryFile ().getParent ();
53             if (fo == null) {
54                 break;
55             }
56             
57             try {
58                 obj = DataObject.find (fo);
59             } catch (DataObjectNotFoundException ex) {
60                 break;
61             }
62         }
63         
64         // ok, use empty lookup
65
return Lookup.EMPTY;
66     }
67     
68     /** Finds a JNDI context for a given data object.
69      * This method is probably unused and useless.
70      * @param obj the data object
71      * @return the JNDI context for this data object
72      * @since 3.13
73      */

74     public static javax.naming.Context JavaDoc findSettingsContext(DataObject obj) {
75         Iterator JavaDoc it = getProviders().allInstances().iterator();
76         while (it.hasNext()) {
77             Environment.Provider ep = (Environment.Provider) it.next();
78             Lookup lookup = ep.getEnvironment(obj);
79             if (lookup != null) {
80                 Context JavaDoc ctx = (Context JavaDoc) lookup.lookup(Context JavaDoc.class);
81                 if (ctx != null) return ctx;
82             }
83         }
84         
85         // default impl
86
return new DefaultSettingsContext(obj);
87     }
88     
89     /** Find method that tries to scan for lookup of one data object.
90      * @param obj the object
91      * @return the lookup or null
92      */

93     static Lookup findForOne (DataObject obj) {
94         /*
95         DataLoader loader = obj.getLoader ();
96         Provider provider = loader instanceof Provider ? (Provider)loader : null;
97         if (provider != null) {
98             Lookup lookup = provider.getEnvironment (obj);
99             if (lookup != null) {
100                 return lookup;
101             } else {
102                 return Lookup.EMPTY;
103             }
104             }
105         */

106         
107         Iterator JavaDoc it = getProviders().allInstances().iterator();
108         while (it.hasNext ()) {
109             Environment.Provider ep = (Environment.Provider)it.next ();
110             Lookup lookup = ep.getEnvironment (obj);
111             if (lookup != null) {
112                 return lookup;
113             }
114         }
115         
116         // not found for this data object
117
return null;
118     }
119     
120     static Lookup.Result getProviders() {
121         if (result == null) {
122             result = Lookup.getDefault().lookupResult(Environment.Provider.class);
123         }
124         return result;
125     }
126     
127     /** Cookie for objects that plan to provide environment.
128      * @since 1.9
129      */

130     public static interface Provider {
131         /** Returns a lookup that represents environment.
132          * @return the lookup
133          */

134         public Lookup getEnvironment (DataObject obj);
135     } // end of Provider
136
}
137
Popular Tags