cool. the workaround is : must instantiate BXMLSerializer while still in
the original [AWT-EventQueue-0] context


public class BxmlUtil {

    private static Logger log = LoggerFactory.getLogger(BxmlUtil.class);

    @SuppressWarnings("unchecked")
    public static <T> T loadBXML(final Class<?> reference, final String
resource) {

        final Thread thread = Thread.currentThread();
        final ClassLoader tccl = thread.getContextClassLoader();

        T result = null;

        try {

            ClassLoader loader = reference.getClassLoader();

            // must instantiate first
            BXMLSerializer bxml = new BXMLSerializer(loader);

            // then change context
            thread.setContextClassLoader(loader);

            URL url = reference.getResource(resource);

            result = (T) bxml.readObject(url);

        } catch (Exception e) {
            log.error("", e);
        } finally {
            thread.setContextClassLoader(tccl);
        }

        return result;

    }

}


-------- Original Message  --------
Subject: Re: pivot  + osgi : class loading?
From: Andrei Pozolotin <[email protected]>
To: [email protected]
Date: Sat 14 May 2011 05:05:25 PM CDT
> ok, this is even more convoluted:
>
> 1) you instantiate scriptEngineManager like this:
>
>     public BXMLSerializer() {
>         scriptEngineManager = new javax.script.ScriptEngineManager();
>
> 2) but sun is using TCCL:
>
>     public ScriptEngineManager() {
>         ClassLoader ctxtLoader =
> Thread.currentThread().getContextClassLoader();
>         if (canCallerAccessLoader(ctxtLoader)) {
>             if (DEBUG) System.out.println("using " + ctxtLoader);
>             init(ctxtLoader);
>         } else {
>             if (DEBUG) System.out.println("using bootstrap loader");
>             init(null);
>         }
>     }
>
> 3) since I switch TCCL  context before I instantiate BXMLSerializer
> (so it can see local bundle resources)
> then this switch  makes script engine provider invisible to sun
>
> -------- Original Message  --------
> Subject: Re: pivot  + osgi : class loading?
> From: Andrei Pozolotin <[email protected]>
> To: [email protected]
> Date: Sat 14 May 2011 04:37:28 PM CDT
>> another problem: can not figure out why even with all workarounds
>> mentioned before
>> BXMLSerializer still fails to init javascript engine while in osgi
>> plugin context:
>>
>> after this "###" breakpoint:
>>
>> BXMLSerializer::
>>     private void processEndElement() throws SerializationException {
>>
>>                             // Create an invocation handler for this
>> listener
>> ###                       ScriptEngine scriptEngine =
>> scriptEngineManager.getEngineByName(language);
>>                             AttributeInvocationHandler handler =
>>                                 new
>> AttributeInvocationHandler(scriptEngine, attribute.name,
>>                                     (String)attribute.value);
>>
>>
>> ScriptEngine scriptEngine == null
>>
>>
>> -------- Original Message  --------
>> Subject: pivot  + osgi : class loading?
>> From: Andrei Pozolotin <[email protected]>
>> To: [email protected]
>> Date: Fri 13 May 2011 10:35:37 PM CDT
>>>
>>>     *Greg, *hello;
>>>
>>>      
>>>     1) I am hitting in the following issue while trying to run pivot
>>>     in osgi environment:
>>>
>>>     this example
>>>           http://pivot.apache.org/tutorials/labels-and-image-views.html
>>>
>>>     |<||Window| |title||=||"Labels"| |maximized||=||"true"|
>>>     |    ||xmlns:bxml||=||"http://pivot.apache.org/bxml";|
>>>     |    ||xmlns||=||"org.apache.pivot.wtk"||>|
>>>     |    ||<||BoxPane| |styles||=||"{padding:4,
>>>     verticalAlignment:'center'}"||>|
>>>     |        ||<||ImageView| |image||=||"/clock.png"||/>|
>>>     |        ||<||Label| |text||=||"What time is it?"||/>|
>>>     |    ||</||BoxPane||>|
>>>     |</||Window||>|
>>>
>>>
>>>     2) in terms of |ImageView| properties, it is getting translated
>>>     by BXMLSerializer into a call |*setImage(String imageName)*| ,
>>>     which fails to locate resource:
>>>
>>>     |||ImageView ::
>>>         /**
>>>          * Sets the image view's image by {@linkplain
>>>     ClassLoader#getResource(String)
>>>          * resource name}.
>>>          *
>>>          * @param imageName
>>>          * The resource name of the image to set.
>>>          *
>>>          * @see #setImage(URL)
>>>          */
>>>     *    public final void setImage(String imageName) {
>>>     *        if (imageName == null) {
>>>                 throw new IllegalArgumentException("imageName is
>>>     null.");
>>>             }
>>>
>>>     *        ClassLoader classLoader =
>>>     Thread.currentThread().getContextClassLoader();
>>>             setImage(classLoader.getResource(imageName.substring(1)));
>>>     *    }
>>>
>>>     |2) note that in my case the pivot package classes and 
>>>     [AWT-EventQueue-0] event queues are instantiated in osgi host
>>>     class loader
>>>          
>>>     
>>> http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html
>>>     but the actual call to |setImage(String imageName)| is executed
>>>     in osgi plugin class loader;
>>>
>>>     just to give you an example, when this snipped gets executed
>>>     inside osgi plugin bundle:
>>>
>>>                             // class loader used by pivot
>>>                             ClassLoader loader =
>>>     Thread.currentThread().getContextClassLoader();
>>>                             URL urlPivot = loader.getResource("");
>>>     *                        log.info("urlPivot : {} ", urlPivot);
>>>     *
>>>                             // class loader used by plugin bundle
>>>                             URL urlOSGI = getClass().getResource("");
>>>     *                        log.info("urlOSGI  : {}", urlOSGI);
>>>     *
>>>     it produces this log:
>>>
>>>     22:08:46.876 [AWT-EventQueue-0] INFO 
>>>     com.carrotgarden.core.LoginComponent - urlPivot :
>>>     
>>> file:/home/user1/Workspaces/github/carrot-tester/carrot-test-osgi-ws/carrot-test-osgi-pivot-host/target/test-classes/
>>>
>>>
>>>     22:08:46.876 [AWT-EventQueue-0] INFO 
>>>     com.carrotgarden.core.LoginComponent - urlOSGI  :
>>>     bundle://7.0:1/com/carrotgarden/core/
>>>
>>>     as you can see,
>>>     *      urlPivot :
>>>     
>>> file:/home/user1/Workspaces/github/carrot-tester/carrot-test-osgi-ws/carrot-test-osgi-pivot-host/target/test-classes/
>>>
>>>     *resolves back to osgi host class loader which is not visible by
>>>     the the client plugin
>>>
>>>     and
>>>     *     urlOSGI  : bundle://7.0:1/com/carrotgarden/core/
>>>     *resolves inside calling osgi bundle name space as expected;
>>>
>>>
>>>     3) am I missing something obvious?
>>>     why do you use current class loading approach in pivot?
>>>     are you open to change this?
>>>     what would your recommend instead?
>>>
>>>
>>>     Thank you,
>>>
>>>     Andrei.
>>>
>>
>

Reply via email to