All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kohsuke.stapler.jelly.JellyClassLoaderTearOff Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 *
 * Copyright (c) 2004-2010 Oracle Corporation.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: 
 *
 *    Kohsuke Kawaguchi
 *     
 *******************************************************************************/ 

package org.kohsuke.stapler.jelly;

import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.TagLibrary;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.Tag;
import org.apache.commons.jelly.impl.TagScript;
import org.apache.commons.jelly.impl.TagFactory;
import org.apache.commons.jelly.expression.ExpressionFactory;
import org.apache.commons.jelly.expression.Expression;
import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
import org.kohsuke.stapler.MetaClassLoader;
import org.xml.sax.Attributes;

import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.Map;

import com.google.common.collect.MapMaker;
import com.google.common.base.Function;

/**
 * {@link MetaClassLoader} tear-off for Jelly support.
 *
 * @author Kohsuke Kawaguchi
 */
public class JellyClassLoaderTearOff {
    private final MetaClassLoader owner;

    /**
     * See {@link JellyClassTearOff#scripts} for why we use {@link WeakReference} here.
     */
    private volatile WeakReference> taglibs;

    public static ExpressionFactory EXPRESSION_FACTORY = new JexlExpressionFactory();

    public JellyClassLoaderTearOff(MetaClassLoader owner) {
        this.owner = owner;
    }

    public TagLibrary getTagLibrary(String nsUri) {
        Map m=null;
        if(taglibs!=null)
            m = taglibs.get();
        if(m==null) {
            m = new MapMaker().makeComputingMap(new Function() {
                public TagLibrary apply(String nsUri) {
                    if(owner.parent!=null) {
                        // parent first
                        TagLibrary tl = owner.parent.loadTearOff(JellyClassLoaderTearOff.class).getTagLibrary(nsUri);
                        if(tl!=null)    return tl;
                    }

                    String taglibBasePath = trimHeadSlash(nsUri);
                    try {
                        URL res = owner.loader.getResource(taglibBasePath +"/taglib");
                        if(res!=null)
                        return new CustomTagLibrary(createContext(),owner.loader,nsUri,taglibBasePath);
                    } catch (IllegalArgumentException e) {
                        // if taglibBasePath doesn't even look like an URL, getResource throws IllegalArgumentException.
                        // see http://old.nabble.com/bug-1.331-to26145963.html
                    }

                    return NO_SUCH_TAGLIBRARY;    // "not found" is also cached.
                }
            });
            taglibs = new WeakReference>(m);
        }

        TagLibrary tl = m.get(nsUri);
        if (tl==NO_SUCH_TAGLIBRARY)     return null;
        return tl;
    }

    private String trimHeadSlash(String nsUri) {
        if(nsUri.startsWith("/"))
            return nsUri.substring(1);
        else
            return nsUri;
    }

    /**
     * Creates {@link JellyContext} for compiling view scripts
     * for classes in this classloader.
     */
    public JellyContext createContext() {
        JellyContext context = new CustomJellyContext(ROOT_CONTEXT);
        context.setClassLoader(owner.loader);
        context.setExportLibraries(false);
        return context;
    }

    /**
     * Used as the root context for compiling scripts.
     */
    private static final JellyContext ROOT_CONTEXT = new CustomJellyContext();

    static {
        ROOT_CONTEXT.registerTagLibrary("jelly:stapler",new StaplerTagLibrary());
    }

    /**
     * Place holder in the cache to indicate "no such taglib"
     */
    private static final TagLibrary NO_SUCH_TAGLIBRARY = new TagLibrary() {};

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy