Re: Using setClassLoader

Ted Hesselroth <[email protected]> Tue, 07 Sep 2004 11:04:35 -0700
Newsgroups gmane.comp.embedded.carlsbad-cubes
Message-ID <[email protected]>
My component is specified with

<xslider id="sliimg01" 
initclass="edu.caltech.ipac.spitzer.spice.actions.display.BQDDisplay"/>

The code for XSlider is attached. It is a JSlider that looks for a field 
named "sliderListener" in initclass, and adds it as a listener if found.

BQDDisplay does have sliderListener defined and has already been 
instantiated by the time that line is reached, so in BQDDisplay.java,

  public static synchronized BQDDisplay getInstance() {
    if (bqdd==null) {
      bqdd = new BQDDisplay(null);
    }
    return bqdd;
  }

getInstance should just return the singleton reference. This used to 
work when the constructor for XSlider was

public XSlider (BQDDisplay bqdp) {...}

But when I changed it to

public XSlider (Object bqdp) {...}

swix never calls getInstance because it really feels that bqdp should be 
of exactly the type specified by initclass. It seems to me that it 
should be enough that the initclass be a subclass of what is used in 
XSlider.


As for the classloader problem, I add an array of URLs to the 
classloader in order to instantiate BQDDisplay:

ucl = URLClassLoader.newInstance(urls, ucl);

because this class is going to be loaded over the network. After the 
swing engine is created, I give it the new class loader, so it can 
access getInstance of BQDDisplay:

swix = new SwingEngine(swixclient);
swix.setClassLoader(ucl);

but when it reaches the line in Parser.java

Class initClass = Class.forName( st.nextToken() );

a trace of the code shows that Parser is not actually using ucl from the 
setClassLoader call. At that point it throws a ClassNotFound exception.

Ted Hesselroth



[email protected] wrote:

>Hello Ted,
>
>may you send more code snippets reproducing the problem? Perhaps
>it is a classpath problem?
>
>The lines checking the constructor parameter types for Object
>identiy I currently do not understand -- I'm sure there is a reason
>for this ;) Your code looks much more "purposeful".
>
>As far as I understood it, you have such a getInstance method:
>public static Object getInstance() {
>...
>}
>The object returned is no Object alone, and the constructor of
>the component invoked is determined through swixml?
>Sounds like a reasonable thing to do...
>
>Yours,
>
>Frank
>
>List for Users of Carlsbad Cubes' Technologies and Products schrieb am 03.09.04 07:41:21:
>  
>
>>Trying setClassLoader in SwingEngine, I could not get it to work until 
>>changing the Class.forName line|| in Parser.java
>>
>>        Class initClass = Class.forName( classname , true, 
>>this.engine.getClassLoader() );      // load update type
>>
>>--kept getting ClassNotFoundException there. It looks like the 
>>classloader "cl" field is not getting fully propagated.
>>
>>
>>Also, I have a custom component using initclass. The component takes an 
>>Object argrument in its constructor. It wouldn't initialize until I 
>>changed the argument checking in DefaultFactory.java
>>
>>         for (int j = 0; ctor != null && j < cParams.length; j++) {
>>           //if (cParams[j].equals( Object.class )) {
>>           // if (!cParams[j].equals( pTypes[j] )) {
>>           //   ctor = null; //dismissed
>>           //  }
>>           //} else {
>>             if (!cParams[j].isAssignableFrom( pTypes[j] )) {
>>               ctor = null; //dismissed
>>             }
>>           //}
>>         } // end for j - loop Ctor's parameter
>>
>>I am using the getInstance of initclass to aquire an 
>>already-instantiated singleton, but my custom component doesn't know or 
>>need to know the type. That's why I'm giving it a subclass of Object 
>>even though it takes Object. Only the method isAssignableFrom is needed, 
>>because it also includes the "equals" case.
>>
>>By the way, I'll be giving a talk including SWIX in the upcoming ADASS 
>>conference on astronomical data reduction, and just yesterday my 
>>application using SWIX was released, see
>>
>>http://ssc.spitzer.caltech.edu/postbcd/spice.html
>>
>>Ted Hesselroth
>>
>>
>>_______________________________________________
>>Forum mailing list
>>[email protected]
>>http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com
>>    
>>
>
>
>
>  
>

_______________________________________________
Forum mailing list
[email protected]
http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com
XSlider.java (text/x-java, 719 B)
package edu.caltech.ipac.spitzer.capri.util.components;

import javax.swing.*;
import javax.swing.event.*;

public class XSlider extends JSlider {


  public XSlider (Object bqdp) {
    super();

    Object obj = bqdp;

    try {
      Object lo = obj.getClass().getField("sliderListener").get(obj);
      if(lo instanceof ChangeListener) {
      super.addChangeListener((ChangeListener) lo);
    }
    } catch (NoSuchFieldException e) {
      //System.out.println("Could not set action for slider -no such field");
    } catch (IllegalAccessException ia) {
      //System.out.println("Could not set action for slider -illegal access");
    }

  }

  public void fireStateChanged() {
    super.fireStateChanged();
  }
}