? X Index: src/org/openide/util/Utilities.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/Utilities.java,v retrieving revision 1.90 diff -c -r1.90 Utilities.java *** src/org/openide/util/Utilities.java 11 Apr 2002 16:31:44 -0000 1.90 --- src/org/openide/util/Utilities.java 18 Apr 2002 13:59:26 -0000 *************** *** 13,22 **** --- 13,26 ---- package org.openide.util; + import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; + import java.beans.PropertyChangeListener; + import java.beans.PropertyChangeEvent; import java.util.*; + import java.util.HashSet; import java.util.List; import java.lang.reflect.*; import java.lang.ref.Reference; *************** *** 31,44 **** import javax.swing.KeyStroke; import javax.swing.Timer; ! import java.awt.*; import org.openide.ErrorManager; import org.openide.TopManager; import org.openide.modules.ModuleInfo; - import java.beans.PropertyChangeListener; - import java.beans.PropertyChangeEvent; - import java.util.HashSet; import org.openide.util.enum.SingletonEnumeration; import org.openide.modules.Dependency; --- 35,48 ---- import javax.swing.KeyStroke; import javax.swing.Timer; ! import javax.naming.Context; ! import javax.naming.InitialContext; ! import javax.naming.NamingException; ! import org.openide.ErrorManager; import org.openide.TopManager; import org.openide.modules.ModuleInfo; import org.openide.util.enum.SingletonEnumeration; import org.openide.modules.Dependency; *************** *** 253,258 **** --- 257,276 ---- return true; } + + /** Central method for obtaining PropertyEditor for a potential property classes. + * This implementation enhances the behviour of the default one in java.beans package. + * + * @param type type of property to find editor for + * @return the editor found or null if no available + */ + public static java.beans.PropertyEditor findEditor (Class type) { + java.beans.PropertyEditor e = (java.beans.PropertyEditor)findJNDI ( + "/Providers/Editors/", type, java.beans.PropertyEditor.class // NOI18N + ); + return e != null ? e : java.beans.PropertyEditorManager.findEditor (type); + } + /** Central method for obtaining BeanInfo for potential JavaBean classes. * This implementation provides additional functionality for Swing bean infos. *************** *** 262,272 **** * @see java.beans.Introspector#getBeanInfo(Class) */ public static java.beans.BeanInfo getBeanInfo(Class clazz) throws java.beans.IntrospectionException { ! java.beans.BeanInfo bi; ! if (clazz.getName().startsWith("javax.swing")) // NOI18N ! bi = SwingEditors.scanAndSetBeanInfo(java.beans.Introspector.getBeanInfo(clazz)); ! else ! bi = java.beans.Introspector.getBeanInfo(clazz); if (java.awt.Component.class.isAssignableFrom (clazz)) { java.beans.PropertyDescriptor[] pds = bi.getPropertyDescriptors (); for (int i = 0; i < pds.length; i++) { --- 280,298 ---- * @see java.beans.Introspector#getBeanInfo(Class) */ public static java.beans.BeanInfo getBeanInfo(Class clazz) throws java.beans.IntrospectionException { ! java.beans.BeanInfo bi = (java.beans.BeanInfo)findJNDI ( ! "/Providers/BeanInfo/", clazz, java.beans.BeanInfo.class // NOI18N ! ); ! ! ! if (bi == null) { ! if (clazz.getName().startsWith("javax.swing")) // NOI18N ! bi = SwingEditors.scanAndSetBeanInfo(java.beans.Introspector.getBeanInfo(clazz)); ! else ! bi = java.beans.Introspector.getBeanInfo(clazz); ! } ! ! if (java.awt.Component.class.isAssignableFrom (clazz)) { java.beans.PropertyDescriptor[] pds = bi.getPropertyDescriptors (); for (int i = 0; i < pds.length; i++) { *************** *** 1962,1965 **** --- 1988,2059 ---- */ public String[] readPair (String line); } + + + // + // Part of code to work JNDI and lookup BeanInfos and PropertyEditors + // + + /** holds references to initial context or exception thrown when + * initilizing it. + */ + private static Object jndi; + + /** Finds an object in JNDI context. + * @param prefix the prefix to add to the search + * @param searchFor class we are searching for + * @param result safety check to return correct results + */ + private static Object findJNDI (String prefix, Class searchFor, Class result) { + if (jndi instanceof Exception) { + // jndi not initialized + return null; + } + + if (! (jndi instanceof InitialContext)) { + // ok, let's try to look for one + try { + // locate the naming context + jndi = new InitialContext (); + } catch (NamingException ex) { + // ok, log the exception + jndi = ex; + + org.openide.ErrorManager.getDefault ().annotate (ex, + org.openide.ErrorManager.INFORMATIONAL, + "Cannot initialize the dynamic introspection system", // NOI18N + null, null, null + ); + org.openide.ErrorManager.getDefault ().notify (ex); + + // nothing found again + return null; + } + } + + // ok, do the search + InitialContext context = (InitialContext)jndi; + + + // name with prefix to search for + String n = prefix + searchFor.getName().replace ('.', '/'); + + Object obj = null; + try { + obj = context.lookup(n); + if (obj instanceof Context) { + // ok we have to take the first element + Context cont = (Context)obj; + javax.naming.NamingEnumeration en = cont.list (""); // NOI18N + obj = en.hasMore () ? en.next() : null; + } + } catch (NamingException ex) { + org.openide.ErrorManager.getDefault ().notify ( + org.openide.ErrorManager.INFORMATIONAL, ex + ); + } + + return result.isInstance (obj) ? obj : null; + } + } Index: test/unit/src/org/openide/util/UtilitiesBeanInfoEditorTest.java =================================================================== RCS file: test/unit/src/org/openide/util/UtilitiesBeanInfoEditorTest.java diff -N test/unit/src/org/openide/util/UtilitiesBeanInfoEditorTest.java *** /dev/null 1 Jan 1970 00:00:00 -0000 --- test/unit/src/org/openide/util/UtilitiesBeanInfoEditorTest.java 18 Apr 2002 13:59:27 -0000 *************** *** 0 **** --- 1,273 ---- + /* + * 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-2000 Sun + * Microsystems, Inc. All Rights Reserved. + */ + + package org.openide.util; + + import java.beans.*; + import javax.naming.*; + + import junit.framework.*; + import org.netbeans.junit.*; + import java.util.Hashtable; + + /** Tests of the proper implementation of JNDI beaninfo & properyeditor + * extensions. + * + * @author Jaroslav Tulach + */ + public class UtilitiesBeanInfoEditorTest extends NbTestCase { + /** the hashtable that influences the content of the context */ + private static Hashtable jndi = new Hashtable (); + + public UtilitiesBeanInfoEditorTest (java.lang.String testName) { + super(testName); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite = new NbTestSuite(UtilitiesBeanInfoEditorTest.class); + + return suite; + } + + /** Initializes our extension to the bean lookup patterns. + */ + protected void setUp () { + System.setProperty ("java.naming.factory.initial", Fac.class.getName ()); + + try { + Context c = new InitialContext (); + } catch (NamingException ex) { + ex.printStackTrace(); + fail ("Naming not initialized"); + } + } + + protected void tearDown () throws Exception { + System.setProperty ("java.naming.factory.initial", ""); + } + + /** Tests the usual functionality */ + public void testDefaultBeanInfo () throws Exception { + BeanInfo info = Utilities.getBeanInfo(Sample.class); + + assertNotNull ("Has to find something", info); + Object o = info.getBeanDescriptor ().getValue ("class"); + assertNotNull ("Attribute class should be there", o); + assertEquals ("And it is the one that we set", SampleBeanInfo.class, o); + + } + + /** Tests default property editor functionality */ + public void testDefaultPropertyEditor () { + PropertyEditor ed = Utilities.findEditor(Sample.class); + + assertNotNull ("Has to find something", ed); + assertEquals ("And that is the right class", SampleEditor.class, ed.getClass ()); + } + + /** Finds the right property editor in JNDI */ + public void testJNDIPropertyEditor () throws Exception { + PropertyEditor info = new PropertyEditorSupport () {}; + + String n = "/Providers/Editors/" + Sample.class.getName ().replace ('.', '/'); + jndi.put (n, info); + + PropertyEditor res = Utilities.findEditor (Sample.class); + + assertNotNull ("Something found", res); + assertEquals ("Found the registered editor", info, res); + + // clearing the infos + jndi.put (n, n); + + res = Utilities.findEditor (Sample.class); + assertNotNull ("Still something found", res); + assertTrue ("But not the info", res != info); + + } + + /** Finds the right bean info in JNDI */ + public void testJNDIBeanInfo () throws Exception { + BeanInfo info = new SampleBeanInfo (); + info.getBeanDescriptor().setValue ("info", info); + + String n = "/Providers/BeanInfo/" + Sample.class.getName ().replace ('.', '/'); + jndi.put (n, info); + + BeanInfo res = Utilities.getBeanInfo (Sample.class); + + assertNotNull ("Something found", res); + assertEquals ("Found the registered info", info, res.getBeanDescriptor ().getValue("info")); + + // clearing the infos + jndi.put (n, n); + Introspector.flushFromCaches (Sample.class); + + res = Utilities.getBeanInfo (Sample.class); + assertNotNull ("Still something found", res); + assertNull ("But not the info", res.getBeanDescriptor ().getValue("info")); + + } + + + /** Sample class. + */ + public static final class Sample { + } + + /** Sample beaninfo */ + public static final class SampleBeanInfo extends java.beans.SimpleBeanInfo { + private BeanDescriptor bean; + + public SampleBeanInfo () { + bean = new BeanDescriptor (Sample.class); + bean.setValue ("class", SampleBeanInfo.class); + } + + public BeanDescriptor getBeanDescriptor () { + return bean; + } + } + + /** Sample property editor. + */ + public static final class SampleEditor extends java.beans.PropertyEditorSupport { + } + + /** Default implementation of JNDI */ + public static final class Fac implements javax.naming.spi.InitialContextFactory { + public Context getInitialContext(Hashtable environment) { + return new Cnt (environment); + } + } // end of Fac + + /** Context based on hashtable */ + private static final class Cnt implements javax.naming.Context { + private Hashtable table; + + /** Constructor. + */ + public Cnt (Hashtable table) { + this.table = table; + } + + public Object addToEnvironment(String propName, Object propVal) throws NamingException { + throw new NamingException (); + } + + public void bind(Name name, Object obj) throws NamingException { + } + + public void bind(String name, Object obj) throws NamingException { + } + + public void close() throws NamingException { + } + + public Name composeName(Name name, Name prefix) throws NamingException { + throw new NamingException (); + } + + public String composeName(String name, String prefix) throws NamingException { + throw new NamingException (); + } + + public Context createSubcontext(Name name) throws NamingException { + throw new NamingException (); + } + + public Context createSubcontext(String name) throws NamingException { + throw new NamingException (); + } + + public void destroySubcontext(Name name) throws NamingException { + throw new NamingException (); + } + + public void destroySubcontext(String name) throws NamingException { + throw new NamingException (); + } + + public Hashtable getEnvironment() throws NamingException { + return table; + } + + public String getNameInNamespace() throws NamingException { + throw new NamingException (); + } + + public NameParser getNameParser(Name name) throws NamingException { + throw new NamingException (); + } + + public NameParser getNameParser(String name) throws NamingException { + throw new NamingException (); + } + + public NamingEnumeration list(String name) throws NamingException { + throw new NamingException (); + } + + public NamingEnumeration list(Name name) throws NamingException { + return list (name.toString ()); + } + + public NamingEnumeration listBindings(Name name) throws NamingException { + throw new NamingException (); + } + + public NamingEnumeration listBindings(String name) throws NamingException { + throw new NamingException (); + } + + public Object lookup(String name) throws NamingException { + return jndi.get (name); + } + + public Object lookup(Name name) throws NamingException { + return lookup (name.toString ()); + } + + public Object lookupLink(String name) throws NamingException { + return lookup (name); + } + public Object lookupLink(Name name) throws NamingException { + return lookup (name); + } + public void rebind(Name name, Object obj) throws NamingException { + throw new NamingException (); + } + public void rebind(String name, Object obj) throws NamingException { + throw new NamingException (); + } + public Object removeFromEnvironment(String propName) throws NamingException { + throw new NamingException (); + } + public void rename(Name oldName, Name newName) throws NamingException { + throw new NamingException (); + } + public void rename(String oldName, String newName) throws NamingException { + throw new NamingException (); + } + public void unbind(String name) throws NamingException { + throw new NamingException (); + } + public void unbind(Name name) throws NamingException { + throw new NamingException (); + } + } + }