KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > looks > RegistryBridge


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.netbeans.modules.looks;
21
22 //import javax.naming.*; // UNCOMENT IF USING JNDI
23
//import org.netbeans.api.naming.NamingSupport; // UNCOMENT IF USING JNDI
24

25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import org.netbeans.api.registry.AttributeEvent;
31 import org.netbeans.api.registry.BindingEvent;
32 import org.netbeans.api.registry.ContextListener;
33 import org.netbeans.api.registry.SubcontextEvent;
34 import org.netbeans.spi.registry.SpiUtils;
35 import org.openide.filesystems.FileObject;
36 import org.openide.util.Enumerations;
37
38 /** Class for resolving names of looks. Allowes for switching the registry
39  * implementation. e.g. switching from JNDI to registry API.
40  *
41  * @author Petr Hrebejk
42  */

43 public abstract class RegistryBridge {
44         
45     public static RegistryBridge getDefault( FileObject fo ) {
46         // return JNDI.getBridge( fo ); // UNCOMENT IF USING JNDI
47
return RAPI.getBridge( fo ); // UNCOMENT IF USING RAPI
48
}
49     
50
51     /** For testing purposes only
52      */

53     public static void setDefault( FileObject fo ) {
54         //JNDI.setBridge( fo ); // UNCOMENT IF USING JNDI
55
RAPI.setBridge( fo ); // UNCOMENT IF USING RegistryAPI
56
}
57     
58     
59     public abstract Object JavaDoc resolve( String JavaDoc name );
60     
61     public abstract Enumeration JavaDoc getObjects( String JavaDoc name, Class JavaDoc type );
62     
63     public abstract Enumeration JavaDoc getNames( String JavaDoc name );
64         
65     public abstract void addListener( String JavaDoc name, Listener listener );
66             
67     public abstract void removeListener( String JavaDoc name, Listener listener );
68             
69        
70     /** Class for use with RegistryAPI. Notice that there are lot of ugly
71      * hacks to get it work with current RegistryAPI.
72      */

73     public static class RAPI extends RegistryBridge {
74         
75         private static final Map JavaDoc cache = new HashMap JavaDoc( );
76         
77         private org.netbeans.api.registry.Context rootCtx;
78         
79         private static RegistryBridge defaultBridge; // For testing only
80

81         private RAPI( org.netbeans.api.registry.Context rootCtx ) {
82             this.rootCtx = rootCtx;
83         }
84         
85         /** Parameter fo is ignored can only be called from
86          * methodvalues due to unfinished registry API
87          */

88         public static RegistryBridge getBridge( FileObject fo ) {
89             
90             org.netbeans.api.registry.Context ctx = null;
91             
92             if ( fo == null ) {
93                 
94                 if ( defaultBridge != null ) {
95                     return defaultBridge;
96                 }
97                 
98                 // Context over system filesystem;
99
ctx = org.netbeans.api.registry.Context.getDefault();
100             }
101             else {
102                 // deafult context
103
if ( defaultBridge != null ) {
104                     return defaultBridge;
105                 }
106             }
107             
108             
109             RegistryBridge rb = (RegistryBridge)cache.get( ctx );
110             if ( rb == null ) {
111                 rb = new RAPI( ctx );
112                 cache.put( ctx, rb );
113             }
114             return rb;
115         }
116         
117         /** Creates new registry for given file object. For testing purposes only.
118          */

119         public static void setBridge( FileObject fo ) {
120             org.netbeans.api.registry.Context ctx = SpiUtils.createContext(
121                 org.netbeans.api.registry.fs.FileSystemContextFactory.createContext( fo ) );
122             
123             defaultBridge = new RAPI( ctx );
124             // cache.put( ctx, rb );
125
}
126         
127         public Object JavaDoc resolve( String JavaDoc name ) {
128             
129             int lastSlashIndex = name.lastIndexOf( '/' ); // NOI18N
130

131             org.netbeans.api.registry.Context ctx = lastSlashIndex == -1 ?
132                                                      rootCtx :
133                                                      rootCtx.getSubcontext( name.substring( 0, lastSlashIndex ) );
134                                                      
135             if ( ctx == null ) {
136                 return null;
137             }
138                                                      
139             return ctx.getObject( name.substring( lastSlashIndex + 1), null );
140         }
141         
142         public Enumeration JavaDoc getNames( String JavaDoc name ) {
143             org.netbeans.api.registry.Context ctx = rootCtx.getSubcontext( name );
144             return Collections.enumeration( ctx.getOrderedNames() );
145         }
146         
147         public Enumeration JavaDoc getObjects( String JavaDoc name, final Class JavaDoc type ) {
148                                 
149             final org.netbeans.api.registry.Context subContext = rootCtx.getSubcontext( name );
150             
151             if ( subContext == null ) {
152                 // OK there is no such subcontext maybe there could be the object
153
Object JavaDoc o = resolve( name );
154                 if ( o != null && ( type == null || type.isInstance( o ) ) ) {
155                     return Enumerations.singleton(o);
156                 }
157                 // Not even an object
158
return Enumerations.empty();
159             }
160             
161             Enumeration JavaDoc en = Collections.enumeration( subContext.getOrderedNames() );
162
163             return Enumerations.convert(en, new Enumerations.Processor() {
164                 public Object JavaDoc process(Object JavaDoc object, Collection JavaDoc ignore) {
165                     String JavaDoc bindingName = (String JavaDoc)object;
166                     
167                     if ( bindingName.endsWith( "/" ) ) { //NOI18N
168
return null; // Don't return contexts
169
}
170                     
171                     Object JavaDoc l = subContext.getObject(bindingName, null);
172
173                     if ( type == null ) {
174                         return l;
175                     }
176                     else {
177                         return type.isInstance( l ) ? l : null;
178                     }
179                 }
180             });
181         }
182         
183         public void addListener( String JavaDoc name, Listener listener ) {
184             
185             final org.netbeans.api.registry.Context subContext = rootCtx.getSubcontext( name );
186             
187             if ( subContext == null ) {
188                 throw new IllegalArgumentException JavaDoc( "Context " + name + " does not exist" ); //NOI18N
189
}
190             
191             listener.setContext( subContext );
192             subContext.addContextListener( listener );
193             
194         }
195         
196         public void removeListener( String JavaDoc name, Listener listener ) {
197             
198             if ( listener.context == null ) {
199                 throw new IllegalArgumentException JavaDoc( "Context " + name + " does not exist" ); //NOI18N
200
}
201             
202             listener.context.removeContextListener( listener );
203             
204         }
205                
206         /** Finds a root context for given context
207          */

208         private static org.netbeans.api.registry.Context getRootContext( org.netbeans.api.registry.Context ctx ) {
209             while( ctx.getParentContext() != null ) {
210                 ctx = ctx.getParentContext();
211             }
212             return ctx;
213         }
214        
215     }
216     
217     
218     public static abstract class Listener implements ContextListener {
219     
220         private org.netbeans.api.registry.Context context;
221         
222         
223         public void setContext( org.netbeans.api.registry.Context context ) {
224             this.context = context;
225         }
226         
227         public abstract void selectorChanged();
228         
229         public void attributeChanged( AttributeEvent evt ) {
230             selectorChanged();
231         }
232         
233         public void bindingChanged( BindingEvent evt ) {
234             selectorChanged();
235         }
236         
237         public void subcontextChanged( SubcontextEvent evt ) {
238             selectorChanged();
239         }
240         
241     }
242     
243     
244     /** Class for use with JNDI
245      */

246     /*
247     public static class JNDI extends RegistryBridge {
248         
249         javax.naming.Context rootCtx;
250         
251         public JNDI( FileObject rootFo ) {
252             try {
253                 if ( rootFo == null ) {
254                     rootCtx = NamingSupport.createSFSInitialContext( null );
255                 }
256                 else {
257                     Hashtable env = new Hashtable();
258                     env.put("rootObject", rootFo); //NOI18N
259                     rootCtx = NamingSupport.createSFSInitialContext( env );
260                 }
261             }
262             catch( NamingException e ) {
263                 throw new IllegalArgumentException( "Can't create inital context for " + rootFo ); //NOI18N
264             }
265         }
266         
267         public Object resolve( String name ) {
268             try {
269                 return rootCtx.lookup( name );
270             }
271             catch ( NameNotFoundException e ) {
272                 return null;
273             }
274             catch ( NamingException e ) {
275                 throw new IllegalArgumentException( "Can't find " + name + " in " + rootCtx );
276             }
277         }
278         
279         public Enumeration getObjects( String name, final Class type ) {
280             try {
281                     
282                 NamingEnumeration en = rootCtx.listBindings( name );
283
284                 return new AlterEnumeration( en ) {
285                     public Object alter( Object object ) {
286                         Binding b = (Binding)object;
287                         Object l = b.getObject();
288                         
289                         if ( type == null ) {
290                             return l;
291                         }
292                         else {
293                             return type.isInstance( l ) ? l : null;
294                         }
295                     }
296                 };
297             }
298             catch (NamingException ev) {
299                 ErrorManager.getDefault ().notify (ev);
300                 return EmptyEnumeration.EMPTY;
301             }
302         }
303     }
304     */

305         
306     
307 }
308
Popular Tags