|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.looks; import java.util.*; //import javax.naming.*; // UNCOMENT IF USING JNDI //import org.netbeans.api.naming.NamingSupport; // UNCOMENT IF USING JNDI import org.netbeans.api.registry.*; import org.netbeans.spi.looks.LookSelector; // USING Registry API import org.netbeans.spi.registry.*; // USING Registry API import org.openide.ErrorManager; import org.openide.filesystems.FileObject; import org.openide.filesystems.Repository; import org.openide.util.enum.AlterEnumeration; import org.openide.util.enum.EmptyEnumeration; import org.openide.util.enum.SingletonEnumeration; /** Class for resolving names of looks. Allowes for switching the registry * implementation. e.g. switching from JNDI to registry API. * * @author Petr Hrebejk */ public abstract class RegistryBridge { public static RegistryBridge getDefault( FileObject fo ) { // return JNDI.getBridge( fo ); // UNCOMENT IF USING JNDI return RAPI.getBridge( fo ); // UNCOMENT IF USING RAPI } /** For testing purposes only */ public static void setDefault( FileObject fo ) { //JNDI.setBridge( fo ); // UNCOMENT IF USING JNDI RAPI.setBridge( fo ); // UNCOMENT IF USING RegistryAPI } public abstract Object resolve( String name ); public abstract Enumeration getObjects( String name, Class type ); public abstract Enumeration getNames( String name ); public abstract void addListener( String name, Listener listener ); public abstract void removeListener( String name, Listener listener ); /** Class for use with RegistryAPI. Notice that there are lot of ugly * hacks to get it work with current RegistryAPI. */ public static class RAPI extends RegistryBridge { private static final Map cache = new HashMap( ); private org.netbeans.api.registry.Context rootCtx; private static RegistryBridge defaultBridge; // For testing only private RAPI( org.netbeans.api.registry.Context rootCtx ) { this.rootCtx = rootCtx; } /** Parameter fo is ignored can only be called from * methodvalues due to unfinished registry API */ public static RegistryBridge getBridge( FileObject fo ) { org.netbeans.api.registry.Context ctx = null; if ( fo == null ) { if ( defaultBridge != null ) { return defaultBridge; } // Context over system filesystem; ctx = org.netbeans.api.registry.Context.getDefault(); } else { // deafult context if ( defaultBridge != null ) { return defaultBridge; } } RegistryBridge rb = (RegistryBridge)cache.get( ctx ); if ( rb == null ) { rb = new RAPI( ctx ); cache.put( ctx, rb ); } return rb; } /** Creates new registry for given file object. For testing purposes only. */ public static void setBridge( FileObject fo ) { org.netbeans.api.registry.Context ctx = SpiUtils.createContext( org.netbeans.api.registry.fs.FileSystemContextFactory.createContext( fo ) ); defaultBridge = new RAPI( ctx ); // cache.put( ctx, rb ); } public Object resolve( String name ) { int lastSlashIndex = name.lastIndexOf( '/' ); // NOI18N org.netbeans.api.registry.Context ctx = lastSlashIndex == -1 ? rootCtx : rootCtx.getSubcontext( name.substring( 0, lastSlashIndex ) ); if ( ctx == null ) { return null; } return ctx.getObject( name.substring( lastSlashIndex + 1), null ); } public Enumeration getNames( String name ) { org.netbeans.api.registry.Context ctx = rootCtx.getSubcontext( name ); return Collections.enumeration( ctx.getOrderedNames() ); } public Enumeration getObjects( String name, final Class type ) { final org.netbeans.api.registry.Context subContext = rootCtx.getSubcontext( name ); if ( subContext == null ) { // OK there is no such subcontext maybe there could be the object Object o = resolve( name ); if ( o != null && ( type == null || type.isInstance( o ) ) ) { return new SingletonEnumeration( o ); } // Not even an object return EmptyEnumeration.EMPTY; } Enumeration en = Collections.enumeration( subContext.getOrderedNames() ); return new AlterEnumeration( en ) { public Object alter( Object object ) { String bindingName = (String)object; if ( bindingName.endsWith( "/" ) ) { //NOI18N return null; // Don't return contexts } Object l = subContext.getObject(bindingName, null); if ( type == null ) { return l; } else { return type.isInstance( l ) ? l : null; } } }; } public void addListener( String name, Listener listener ) { final org.netbeans.api.registry.Context subContext = rootCtx.getSubcontext( name ); if ( subContext == null ) { throw new IllegalArgumentException( "Context " + name + " does not exist" ); //NOI18N } listener.setContext( subContext ); subContext.addContextListener( listener ); } public void removeListener( String name, Listener listener ) { if ( listener.context == null ) { throw new IllegalArgumentException( "Context " + name + " does not exist" ); //NOI18N } listener.context.removeContextListener( listener ); } /** Finds a root context for given context */ private static org.netbeans.api.registry.Context getRootContext( org.netbeans.api.registry.Context ctx ) { while( ctx.getParentContext() != null ) { ctx = ctx.getParentContext(); } return ctx; } } public static abstract class Listener implements ContextListener { private org.netbeans.api.registry.Context context; public void setContext( org.netbeans.api.registry.Context context ) { this.context = context; } public abstract void selectorChanged(); public void attributeChanged( AttributeEvent evt ) { selectorChanged(); } public void bindingChanged( BindingEvent evt ) { selectorChanged(); } public void subcontextChanged( SubcontextEvent evt ) { selectorChanged(); } } /** Class for use with JNDI */ /* public static class JNDI extends RegistryBridge { javax.naming.Context rootCtx; public JNDI( FileObject rootFo ) { try { if ( rootFo == null ) { rootCtx = NamingSupport.createSFSInitialContext( null ); } else { Hashtable env = new Hashtable(); env.put("rootObject", rootFo); //NOI18N rootCtx = NamingSupport.createSFSInitialContext( env ); } } catch( NamingException e ) { throw new IllegalArgumentException( "Can't create inital context for " + rootFo ); //NOI18N } } public Object resolve( String name ) { try { return rootCtx.lookup( name ); } catch ( NameNotFoundException e ) { return null; } catch ( NamingException e ) { throw new IllegalArgumentException( "Can't find " + name + " in " + rootCtx ); } } public Enumeration getObjects( String name, final Class type ) { try { NamingEnumeration en = rootCtx.listBindings( name ); return new AlterEnumeration( en ) { public Object alter( Object object ) { Binding b = (Binding)object; Object l = b.getObject(); if ( type == null ) { return l; } else { return type.isInstance( l ) ? l : null; } } }; } catch (NamingException ev) { ErrorManager.getDefault ().notify (ev); return EmptyEnumeration.EMPTY; } } } */ } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.