/jEdit/tags/jedit-4-3-1/org/gjt/sp/jedit/bsh/classpath/BshClassPath.java

# · Java · 887 lines · 534 code · 120 blank · 233 comment · 103 complexity · 267e60029dd982d68a98c5dd6ebd0b9e MD5 · raw file

  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package org.gjt.sp.jedit.bsh.classpath;
  34. import java.util.*;
  35. import java.util.zip.*;
  36. import java.io.*;
  37. import java.net.*;
  38. import java.io.File;
  39. import org.gjt.sp.jedit.bsh.StringUtil;
  40. import org.gjt.sp.jedit.bsh.ClassPathException;
  41. import java.lang.ref.WeakReference;
  42. import org.gjt.sp.jedit.bsh.NameSource;
  43. /**
  44. A BshClassPath encapsulates knowledge about a class path of URLs.
  45. It can maps all classes the path which may include:
  46. jar/zip files and base dirs
  47. A BshClassPath may composite other BshClassPaths as components of its
  48. path and will reflect changes in those components through its methods
  49. and listener interface.
  50. Classpath traversal is done lazily when a call is made to
  51. getClassesForPackage() or getClassSource()
  52. or can be done explicitily through insureInitialized().
  53. Feedback on mapping progress is provided through the MappingFeedback
  54. interface.
  55. Design notes:
  56. Several times here we traverse ourselves and our component paths to
  57. produce a composite view of some thing relating to the path. This would
  58. be an opportunity for a visitor pattern.
  59. */
  60. public class BshClassPath
  61. implements ClassPathListener, NameSource
  62. {
  63. String name;
  64. /** The URL path components */
  65. private List path;
  66. /** Ordered list of components BshClassPaths */
  67. private List compPaths;
  68. /** Set of classes in a package mapped by package name */
  69. private Map packageMap;
  70. /** Map of source (URL or File dir) of every clas */
  71. private Map classSource;
  72. /** The packageMap and classSource maps have been built. */
  73. private boolean mapsInitialized;
  74. private UnqualifiedNameTable unqNameTable;
  75. /**
  76. This used to be configurable, but now we always include them.
  77. */
  78. private boolean nameCompletionIncludesUnqNames = true;
  79. Vector listeners = new Vector();
  80. // constructors
  81. public BshClassPath( String name ) {
  82. this.name = name;
  83. reset();
  84. }
  85. public BshClassPath( String name, URL [] urls ) {
  86. this( name );
  87. add( urls );
  88. }
  89. // end constructors
  90. // mutators
  91. public void setPath( URL[] urls ) {
  92. reset();
  93. add( urls );
  94. }
  95. /**
  96. Add the specified BshClassPath as a component of our path.
  97. Changes in the bcp will be reflected through us.
  98. */
  99. public void addComponent( BshClassPath bcp ) {
  100. if ( compPaths == null )
  101. compPaths = new ArrayList();
  102. compPaths.add( bcp );
  103. bcp.addListener( this );
  104. }
  105. public void add( URL [] urls ) {
  106. path.addAll( Arrays.asList(urls) );
  107. if ( mapsInitialized )
  108. map( urls );
  109. }
  110. public void add( URL url ) throws IOException {
  111. path.add(url);
  112. if ( mapsInitialized )
  113. map( url );
  114. }
  115. /**
  116. Get the path components including any component paths.
  117. */
  118. public URL [] getPathComponents() {
  119. return (URL[])getFullPath().toArray( new URL[0] );
  120. }
  121. /**
  122. Return the set of class names in the specified package
  123. including all component paths.
  124. */
  125. synchronized public Set getClassesForPackage( String pack ) {
  126. insureInitialized();
  127. Set set = new HashSet();
  128. Collection c = (Collection)packageMap.get( pack );
  129. if ( c != null )
  130. set.addAll( c );
  131. if ( compPaths != null )
  132. for (int i=0; i<compPaths.size(); i++) {
  133. c = ((BshClassPath)compPaths.get(i)).getClassesForPackage(
  134. pack );
  135. if ( c != null )
  136. set.addAll( c );
  137. }
  138. return set;
  139. }
  140. /**
  141. Return the source of the specified class which may lie in component
  142. path.
  143. */
  144. synchronized public ClassSource getClassSource( String className )
  145. {
  146. // Before triggering classpath mapping (initialization) check for
  147. // explicitly set class sources (e.g. generated classes). These would
  148. // take priority over any found in the classpath anyway.
  149. ClassSource cs = (ClassSource)classSource.get( className );
  150. if ( cs != null )
  151. return cs;
  152. insureInitialized(); // trigger possible mapping
  153. cs = (ClassSource)classSource.get( className );
  154. if ( cs == null && compPaths != null )
  155. for (int i=0; i<compPaths.size() && cs==null; i++)
  156. cs = ((BshClassPath)compPaths.get(i)).getClassSource(className);
  157. return cs;
  158. }
  159. /**
  160. Explicitly set a class source. This is used for generated classes, but
  161. could potentially be used to allow a user to override which version of
  162. a class from the classpath is located.
  163. */
  164. synchronized public void setClassSource( String className, ClassSource cs )
  165. {
  166. classSource.put( className, cs );
  167. }
  168. /**
  169. If the claspath map is not initialized, do it now.
  170. If component maps are not do them as well...
  171. Random note:
  172. Should this be "insure" or "ensure". I know I've seen "ensure" used
  173. in the JDK source. Here's what Webster has to say:
  174. Main Entry:ensure Pronunciation:in-'shur
  175. Function:transitive verb Inflected
  176. Form(s):ensured; ensuring : to make sure,
  177. certain, or safe : GUARANTEE synonyms ENSURE,
  178. INSURE, ASSURE, SECURE mean to make a thing or
  179. person sure. ENSURE, INSURE, and ASSURE are
  180. interchangeable in many contexts where they
  181. indicate the making certain or inevitable of an
  182. outcome, but INSURE sometimes stresses the
  183. taking of necessary measures beforehand, and
  184. ASSURE distinctively implies the removal of
  185. doubt and suspense from a person's mind. SECURE
  186. implies action taken to guard against attack or
  187. loss.
  188. */
  189. public void insureInitialized()
  190. {
  191. insureInitialized( true );
  192. }
  193. /**
  194. @param topPath indicates that this is the top level classpath
  195. component and it should send the startClassMapping message
  196. */
  197. protected synchronized void insureInitialized( boolean topPath )
  198. {
  199. // If we are the top path and haven't been initialized before
  200. // inform the listeners we are going to do expensive map
  201. if ( topPath && !mapsInitialized )
  202. startClassMapping();
  203. // initialize components
  204. if ( compPaths != null )
  205. for (int i=0; i< compPaths.size(); i++)
  206. ((BshClassPath)compPaths.get(i)).insureInitialized( false );
  207. // initialize ourself
  208. if ( !mapsInitialized )
  209. map( (URL[])path.toArray( new URL[0] ) );
  210. if ( topPath && !mapsInitialized )
  211. endClassMapping();
  212. mapsInitialized = true;
  213. }
  214. /**
  215. Get the full path including component paths.
  216. (component paths listed first, in order)
  217. Duplicate path components are removed.
  218. */
  219. protected List getFullPath()
  220. {
  221. List list = new ArrayList();
  222. if ( compPaths != null ) {
  223. for (int i=0; i<compPaths.size(); i++) {
  224. List l = ((BshClassPath)compPaths.get(i)).getFullPath();
  225. // take care to remove dups
  226. // wish we had an ordered set collection
  227. Iterator it = l.iterator();
  228. while ( it.hasNext() ) {
  229. Object o = it.next();
  230. if ( !list.contains(o) )
  231. list.add( o );
  232. }
  233. }
  234. }
  235. list.addAll( path );
  236. return list;
  237. }
  238. /**
  239. Support for super import "*";
  240. Get the full name associated with the unqualified name in this
  241. classpath. Returns either the String name or an AmbiguousName object
  242. encapsulating the various names.
  243. */
  244. public String getClassNameByUnqName( String name )
  245. throws ClassPathException
  246. {
  247. insureInitialized();
  248. UnqualifiedNameTable unqNameTable = getUnqualifiedNameTable();
  249. Object obj = unqNameTable.get( name );
  250. if ( obj instanceof AmbiguousName )
  251. throw new ClassPathException("Ambigous class names: "+
  252. ((AmbiguousName)obj).get() );
  253. return (String)obj;
  254. }
  255. /*
  256. Note: we could probably do away with the unqualified name table
  257. in favor of a second name source
  258. */
  259. private UnqualifiedNameTable getUnqualifiedNameTable() {
  260. if ( unqNameTable == null )
  261. unqNameTable = buildUnqualifiedNameTable();
  262. return unqNameTable;
  263. }
  264. private UnqualifiedNameTable buildUnqualifiedNameTable()
  265. {
  266. UnqualifiedNameTable unqNameTable = new UnqualifiedNameTable();
  267. // add component names
  268. if ( compPaths != null )
  269. for (int i=0; i<compPaths.size(); i++) {
  270. Set s = ((BshClassPath)compPaths.get(i)).classSource.keySet();
  271. Iterator it = s.iterator();
  272. while(it.hasNext())
  273. unqNameTable.add( (String)it.next() );
  274. }
  275. // add ours
  276. Iterator it = classSource.keySet().iterator();
  277. while(it.hasNext())
  278. unqNameTable.add( (String)it.next() );
  279. return unqNameTable;
  280. }
  281. public String [] getAllNames()
  282. {
  283. insureInitialized();
  284. List names = new ArrayList();
  285. Iterator it = getPackagesSet().iterator();
  286. while( it.hasNext() ) {
  287. String pack = (String)it.next();
  288. names.addAll(
  289. removeInnerClassNames( getClassesForPackage( pack ) ) );
  290. }
  291. if ( nameCompletionIncludesUnqNames )
  292. names.addAll( getUnqualifiedNameTable().keySet() );
  293. return (String [])names.toArray(new String[0]);
  294. }
  295. /**
  296. call map(url) for each url in the array
  297. */
  298. synchronized void map( URL [] urls )
  299. {
  300. for(int i=0; i< urls.length; i++)
  301. try{
  302. map( urls[i] );
  303. } catch ( IOException e ) {
  304. String s = "Error constructing classpath: " +urls[i]+": "+e;
  305. errorWhileMapping( s );
  306. }
  307. }
  308. synchronized void map( URL url )
  309. throws IOException
  310. {
  311. String name = url.getFile();
  312. File f = new File( name );
  313. if ( f.isDirectory() ) {
  314. classMapping( "Directory "+ f.toString() );
  315. map( traverseDirForClasses( f ), new DirClassSource(f) );
  316. } else if ( isArchiveFileName( name ) ) {
  317. classMapping("Archive: "+url );
  318. map( searchJarForClasses( url ), new JarClassSource(url) );
  319. }
  320. /*
  321. else if ( isClassFileName( name ) )
  322. map( looseClass( name ), url );
  323. */
  324. else {
  325. String s = "Not a classpath component: "+ name ;
  326. errorWhileMapping( s );
  327. }
  328. }
  329. private void map( String [] classes, Object source ) {
  330. for(int i=0; i< classes.length; i++) {
  331. //System.out.println( classes[i] +": "+ source );
  332. mapClass( classes[i], source );
  333. }
  334. }
  335. private void mapClass( String className, Object source )
  336. {
  337. // add to package map
  338. String [] sa = splitClassname( className );
  339. String pack = sa[0];
  340. String clas = sa[1];
  341. Set set = (Set)packageMap.get( pack );
  342. if ( set == null ) {
  343. set = new HashSet();
  344. packageMap.put( pack, set );
  345. }
  346. set.add( className );
  347. // Add to classSource map
  348. Object obj = classSource.get( className );
  349. // don't replace previously set (found earlier in classpath or
  350. // explicitly set via setClassSource() )
  351. if ( obj == null )
  352. classSource.put( className, source );
  353. }
  354. /**
  355. Clear everything and reset the path to empty.
  356. */
  357. synchronized private void reset() {
  358. path = new ArrayList();
  359. compPaths = null;
  360. clearCachedStructures();
  361. }
  362. /**
  363. Clear anything cached. All will be reconstructed as necessary.
  364. */
  365. synchronized private void clearCachedStructures() {
  366. mapsInitialized = false;
  367. packageMap = new HashMap();
  368. classSource = new HashMap();
  369. unqNameTable = null;
  370. nameSpaceChanged();
  371. }
  372. public void classPathChanged() {
  373. clearCachedStructures();
  374. notifyListeners();
  375. }
  376. /*
  377. public void setNameCompletionIncludeUnqNames( boolean b ) {
  378. if ( nameCompletionIncludesUnqNames != b ) {
  379. nameCompletionIncludesUnqNames = b;
  380. nameSpaceChanged();
  381. }
  382. }
  383. */
  384. // Begin Static stuff
  385. static String [] traverseDirForClasses( File dir )
  386. throws IOException
  387. {
  388. List list = traverseDirForClassesAux( dir, dir );
  389. return (String[])list.toArray( new String[0] );
  390. }
  391. static List traverseDirForClassesAux( File topDir, File dir )
  392. throws IOException
  393. {
  394. List list = new ArrayList();
  395. String top = topDir.getAbsolutePath();
  396. File [] children = dir.listFiles();
  397. for (int i=0; i< children.length; i++) {
  398. File child = children[i];
  399. if ( child.isDirectory() )
  400. list.addAll( traverseDirForClassesAux( topDir, child ) );
  401. else {
  402. String name = child.getAbsolutePath();
  403. if ( isClassFileName( name ) ) {
  404. /*
  405. Remove absolute (topdir) portion of path and leave
  406. package-class part
  407. */
  408. if ( name.startsWith( top ) )
  409. name = name.substring( top.length()+1 );
  410. else
  411. throw new IOException( "problem parsing paths" );
  412. name = canonicalizeClassName(name);
  413. list.add( name );
  414. }
  415. }
  416. }
  417. return list;
  418. }
  419. /**
  420. Get the class file entries from the Jar
  421. */
  422. static String [] searchJarForClasses( URL jar )
  423. throws IOException
  424. {
  425. Vector v = new Vector();
  426. InputStream in = jar.openStream();
  427. ZipInputStream zin = new ZipInputStream(in);
  428. ZipEntry ze;
  429. while( (ze= zin.getNextEntry()) != null ) {
  430. String name=ze.getName();
  431. if ( isClassFileName( name ) )
  432. v.addElement( canonicalizeClassName(name) );
  433. }
  434. zin.close();
  435. String [] sa = new String [v.size()];
  436. v.copyInto(sa);
  437. return sa;
  438. }
  439. public static boolean isClassFileName( String name ){
  440. return ( name.toLowerCase().endsWith(".class") );
  441. //&& (name.indexOf('$')==-1) );
  442. }
  443. public static boolean isArchiveFileName( String name ){
  444. name = name.toLowerCase();
  445. return ( name.endsWith(".jar") || name.endsWith(".zip") );
  446. }
  447. /**
  448. Create a proper class name from a messy thing.
  449. Turn / or \ into ., remove leading class and trailing .class
  450. Note: this makes lots of strings... could be faster.
  451. */
  452. public static String canonicalizeClassName( String name )
  453. {
  454. String classname=name.replace('/', '.');
  455. classname=classname.replace('\\', '.');
  456. if ( classname.startsWith("class ") )
  457. classname=classname.substring(6);
  458. if ( classname.endsWith(".class") )
  459. classname=classname.substring(0,classname.length()-6);
  460. return classname;
  461. }
  462. /**
  463. Split class name into package and name
  464. */
  465. public static String [] splitClassname ( String classname ) {
  466. classname = canonicalizeClassName( classname );
  467. int i=classname.lastIndexOf(".");
  468. String classn, packn;
  469. if ( i == -1 ) {
  470. // top level class
  471. classn = classname;
  472. packn="<unpackaged>";
  473. } else {
  474. packn = classname.substring(0,i);
  475. classn = classname.substring(i+1);
  476. }
  477. return new String [] { packn, classn };
  478. }
  479. /**
  480. Return a new collection without any inner class names
  481. */
  482. public static Collection removeInnerClassNames( Collection col ) {
  483. List list = new ArrayList();
  484. list.addAll(col);
  485. Iterator it = list.iterator();
  486. while(it.hasNext()) {
  487. String name =(String)it.next();
  488. if (name.indexOf("$") != -1 )
  489. it.remove();
  490. }
  491. return list;
  492. }
  493. /**
  494. The user classpath from system property
  495. java.class.path
  496. */
  497. static URL [] userClassPathComp;
  498. public static URL [] getUserClassPathComponents()
  499. throws ClassPathException
  500. {
  501. if ( userClassPathComp != null )
  502. return userClassPathComp;
  503. String cp=System.getProperty("java.class.path");
  504. String [] paths=StringUtil.split(cp, File.pathSeparator);
  505. URL [] urls = new URL[ paths.length ];
  506. try {
  507. for ( int i=0; i<paths.length; i++)
  508. // We take care to get the canonical path first.
  509. // Java deals with relative paths for it's bootstrap loader
  510. // but JARClassLoader doesn't.
  511. urls[i] = new File(
  512. new File(paths[i]).getCanonicalPath() ).toURL();
  513. } catch ( IOException e ) {
  514. throw new ClassPathException("can't parse class path: "+e);
  515. }
  516. userClassPathComp = urls;
  517. return urls;
  518. }
  519. /**
  520. Get a list of all of the known packages
  521. */
  522. public Set getPackagesSet()
  523. {
  524. insureInitialized();
  525. Set set = new HashSet();
  526. set.addAll( packageMap.keySet() );
  527. if ( compPaths != null )
  528. for (int i=0; i<compPaths.size(); i++)
  529. set.addAll(
  530. ((BshClassPath)compPaths.get(i)).packageMap.keySet() );
  531. return set;
  532. }
  533. public void addListener( ClassPathListener l ) {
  534. listeners.addElement( new WeakReference(l) );
  535. }
  536. public void removeListener( ClassPathListener l ) {
  537. listeners.removeElement( l );
  538. }
  539. /**
  540. */
  541. void notifyListeners() {
  542. for (Enumeration e = listeners.elements(); e.hasMoreElements(); ) {
  543. WeakReference wr = (WeakReference)e.nextElement();
  544. ClassPathListener l = (ClassPathListener)wr.get();
  545. if ( l == null ) // garbage collected
  546. listeners.removeElement( wr );
  547. else
  548. l.classPathChanged();
  549. }
  550. }
  551. static BshClassPath userClassPath;
  552. /**
  553. A BshClassPath initialized to the user path
  554. from java.class.path
  555. */
  556. public static BshClassPath getUserClassPath()
  557. throws ClassPathException
  558. {
  559. if ( userClassPath == null )
  560. userClassPath = new BshClassPath(
  561. "User Class Path", getUserClassPathComponents() );
  562. return userClassPath;
  563. }
  564. static BshClassPath bootClassPath;
  565. /**
  566. Get the boot path including the lib/rt.jar if possible.
  567. */
  568. public static BshClassPath getBootClassPath()
  569. throws ClassPathException
  570. {
  571. if ( bootClassPath == null )
  572. {
  573. try
  574. {
  575. //String rtjar = System.getProperty("java.home")+"/lib/rt.jar";
  576. String rtjar = getRTJarPath();
  577. URL url = new File( rtjar ).toURL();
  578. bootClassPath = new BshClassPath(
  579. "Boot Class Path", new URL[] { url } );
  580. } catch ( MalformedURLException e ) {
  581. throw new ClassPathException(" can't find boot jar: "+e);
  582. }
  583. }
  584. return bootClassPath;
  585. }
  586. private static String getRTJarPath()
  587. {
  588. String urlString =
  589. Class.class.getResource("/java/lang/String.class").toExternalForm();
  590. if ( !urlString.startsWith("jar:file:") )
  591. return null;
  592. int i = urlString.indexOf("!");
  593. if ( i == -1 )
  594. return null;
  595. return urlString.substring( "jar:file:".length(), i );
  596. }
  597. public abstract static class ClassSource {
  598. Object source;
  599. abstract byte [] getCode( String className );
  600. }
  601. public static class JarClassSource extends ClassSource {
  602. JarClassSource( URL url ) { source = url; }
  603. public URL getURL() { return (URL)source; }
  604. /*
  605. Note: we should implement this for consistency, however our
  606. BshClassLoader can natively load from a JAR because it is a
  607. URLClassLoader... so it may be better to allow it to do it.
  608. */
  609. public byte [] getCode( String className ) {
  610. throw new Error("Unimplemented");
  611. }
  612. public String toString() { return "Jar: "+source; }
  613. }
  614. public static class DirClassSource extends ClassSource
  615. {
  616. DirClassSource( File dir ) { source = dir; }
  617. public File getDir() { return (File)source; }
  618. public String toString() { return "Dir: "+source; }
  619. public byte [] getCode( String className ) {
  620. return readBytesFromFile( getDir(), className );
  621. }
  622. public static byte [] readBytesFromFile( File base, String className )
  623. {
  624. String n = className.replace( '.', File.separatorChar ) + ".class";
  625. File file = new File( base, n );
  626. if ( file == null || !file.exists() )
  627. return null;
  628. byte [] bytes;
  629. try {
  630. FileInputStream fis = new FileInputStream(file);
  631. DataInputStream dis = new DataInputStream( fis );
  632. bytes = new byte [ (int)file.length() ];
  633. dis.readFully( bytes );
  634. dis.close();
  635. } catch(IOException ie ) {
  636. throw new RuntimeException("Couldn't load file: "+file);
  637. }
  638. return bytes;
  639. }
  640. }
  641. public static class GeneratedClassSource extends ClassSource
  642. {
  643. GeneratedClassSource( byte [] bytecode ) { source = bytecode; }
  644. public byte [] getCode( String className ) {
  645. return (byte [])source;
  646. }
  647. }
  648. public static void main( String [] args ) throws Exception {
  649. URL [] urls = new URL [ args.length ];
  650. for(int i=0; i< args.length; i++)
  651. urls[i] = new File(args[i]).toURL();
  652. BshClassPath bcp = new BshClassPath( "Test", urls );
  653. }
  654. public String toString() {
  655. return "BshClassPath "+name+"("+super.toString()+") path= "+path +"\n"
  656. + "compPaths = {" + compPaths +" }";
  657. }
  658. /*
  659. Note: we could probably do away with the unqualified name table
  660. in favor of a second name source
  661. */
  662. static class UnqualifiedNameTable extends HashMap {
  663. void add( String fullname ) {
  664. String name = splitClassname( fullname )[1];
  665. Object have = super.get( name );
  666. if ( have == null )
  667. super.put( name, fullname );
  668. else
  669. if ( have instanceof AmbiguousName )
  670. ((AmbiguousName)have).add( fullname );
  671. else // String
  672. {
  673. AmbiguousName an = new AmbiguousName();
  674. an.add( (String)have );
  675. an.add( fullname );
  676. super.put( name, an );
  677. }
  678. }
  679. }
  680. public static class AmbiguousName {
  681. List list = new ArrayList();
  682. public void add( String name ) {
  683. list.add( name );
  684. }
  685. public List get() {
  686. //return (String[])list.toArray(new String[0]);
  687. return list;
  688. }
  689. }
  690. /**
  691. Fire the NameSourceListeners
  692. */
  693. void nameSpaceChanged()
  694. {
  695. if ( nameSourceListeners == null )
  696. return;
  697. for(int i=0; i<nameSourceListeners.size(); i++)
  698. ((NameSource.Listener)(nameSourceListeners.get(i)))
  699. .nameSourceChanged( this );
  700. }
  701. List nameSourceListeners;
  702. /**
  703. Implements NameSource
  704. Add a listener who is notified upon changes to names in this space.
  705. */
  706. public void addNameSourceListener( NameSource.Listener listener ) {
  707. if ( nameSourceListeners == null )
  708. nameSourceListeners = new ArrayList();
  709. nameSourceListeners.add( listener );
  710. }
  711. /** only allow one for now */
  712. static MappingFeedback mappingFeedbackListener;
  713. /**
  714. */
  715. public static void addMappingFeedback( MappingFeedback mf )
  716. {
  717. if ( mappingFeedbackListener != null )
  718. throw new RuntimeException("Unimplemented: already a listener");
  719. mappingFeedbackListener = mf;
  720. }
  721. void startClassMapping() {
  722. if ( mappingFeedbackListener != null )
  723. mappingFeedbackListener.startClassMapping();
  724. else
  725. System.err.println( "Start ClassPath Mapping" );
  726. }
  727. void classMapping( String msg ) {
  728. if ( mappingFeedbackListener != null ) {
  729. mappingFeedbackListener.classMapping( msg );
  730. } else
  731. System.err.println( "Mapping: "+msg );
  732. }
  733. void errorWhileMapping( String s ) {
  734. if ( mappingFeedbackListener != null )
  735. mappingFeedbackListener.errorWhileMapping( s );
  736. else
  737. System.err.println( s );
  738. }
  739. void endClassMapping() {
  740. if ( mappingFeedbackListener != null )
  741. mappingFeedbackListener.endClassMapping();
  742. else
  743. System.err.println( "End ClassPath Mapping" );
  744. }
  745. public static interface MappingFeedback
  746. {
  747. public void startClassMapping();
  748. /**
  749. Provide feedback on the progress of mapping the classpath
  750. @param msg is a message about the path component being mapped
  751. @perc is an integer in the range 0-100 indicating percentage done
  752. public void classMapping( String msg, int perc );
  753. */
  754. /**
  755. Provide feedback on the progress of mapping the classpath
  756. */
  757. public void classMapping( String msg );
  758. public void errorWhileMapping( String msg );
  759. public void endClassMapping();
  760. }
  761. }