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

com.sun.jersey.server.spi.component.ResourceComponentConstructor Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
package com.sun.jersey.server.spi.component;

import com.sun.jersey.api.core.HttpContext;
import com.sun.jersey.api.model.AbstractResource;
import com.sun.jersey.api.model.AbstractResourceConstructor;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable;
import com.sun.jersey.server.impl.inject.ServerInjectableProviderContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.Errors;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * A constructor of a resource class.
 * 
 * @author [email protected]
 */
public class ResourceComponentConstructor {
    private final Class c;

    private final ResourceComponentInjector rci;
    
    private final Constructor constructor;

    private final List postConstructs = new ArrayList();
    
    private final List injectables;

    /**
     * A tuple of a constructor and the list of injectables associated with
     * the parameters of the constructor.
     *
     * @param  the type to construct.
     */
    private static class ConstructorInjectablePair {
        /**
         * The constructor.
         */
        private final Constructor con;

        /**
         * The list of injectables associated with the parameters of the
         * constructor;
         */
        private final List is;

        /**
         * Create a new tuple of a constructor and list of injectables.
         *
         * @param con the constructor
         * @param is the list of injectables.
         */
        private ConstructorInjectablePair(Constructor con, List is) {
            this.con = con;
            this.is = is;
        }
    }

    private static class ConstructorComparator implements Comparator {
        public int compare(ConstructorInjectablePair o1, ConstructorInjectablePair o2) {
            int p = Collections.frequency(o1.is, null) - Collections.frequency(o2.is, null);
            if (p != 0)
                return p;

            return o2.con.getParameterTypes().length - o1.con.getParameterTypes().length;
        }
    }

    public ResourceComponentConstructor(ServerInjectableProviderContext sipc,
            ComponentScope scope, AbstractResource ar) {
        this.c = ar.getResourceClass();

        final int modifiers = c.getModifiers();
        if (!Modifier.isPublic(modifiers)) {
            Errors.nonPublicClass(c);
        }

        if (Modifier.isAbstract(modifiers)) {
            if (Modifier.isInterface(modifiers)) {
                Errors.interfaceClass(c);
            } else {
                Errors.abstractClass(c);
            }
        }

        if (c.getEnclosingClass() != null && !Modifier.isStatic(modifiers)) {
            Errors.innerClass(c);
        }

        if (Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers)) {
            if (c.getConstructors().length == 0) {
                Errors.nonPublicConstructor(c);
            }
        }

        this.rci = new ResourceComponentInjector(
                sipc,
                scope,
                ar);

        this.postConstructs.addAll(ar.getPostConstructMethods());
        
        ConstructorInjectablePair cip = getConstructor(sipc, scope, ar);
        if (cip == null) {
            this.constructor = null;
            this.injectables = null;
        } else if (cip.is.isEmpty()) {
            this.constructor = cip.con;
            this.injectables = null;
        } else {
            if (cip.is.contains(null)) {
                // Missing dependency
                for (int i = 0; i < cip.is.size(); i++) {
                    if (cip.is.get(i) == null) {
                        Errors.missingDependency(cip.con, i);
                    }
                }
            }
            this.constructor = cip.con;
            this.injectables = AbstractHttpContextInjectable.transform(cip.is);
        }
    }

    public Class getResourceClass() {
        return c;
    }
    
    public Object construct(HttpContext hc)
            throws InstantiationException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        final Object o = _construct(hc);
        rci.inject(hc, o);
        for (Method postConstruct : postConstructs) {
            postConstruct.invoke(o);
        }
        return o;
    }
    
    private Object _construct(HttpContext hc)
            throws InstantiationException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        if (injectables == null) {
            return (constructor != null) ? constructor.newInstance() : c.newInstance();
        } else {
            Object[] params = new Object[injectables.size()];
            int i = 0;
            for (AbstractHttpContextInjectable injectable : injectables) {
                params[i++] = (injectable != null) ? injectable.getValue(hc) : null;
            }
            return constructor.newInstance(params);
        }
    }

    /**
     * Get the most suitable constructor. The constructor with the most
     * parameters and that has the most parameters associated with 
     * Injectable instances will be chosen.
     * 
     * @param  the type of the resource.
     * @param c the class to instantiate.
     * @param ar the abstract resource.
     * @param s the scope for which the injectables will be used.
     * @return a list constructor and list of injectables for the constructor
     *         parameters.
     */
    @SuppressWarnings("unchecked")
    private  ConstructorInjectablePair getConstructor(
            ServerInjectableProviderContext sipc,
            ComponentScope scope,
            AbstractResource ar) {
        if (ar.getConstructors().isEmpty())
            return null;

        SortedSet cs = new TreeSet(
                new ConstructorComparator());        
        for (AbstractResourceConstructor arc : ar.getConstructors()) {
            List is = sipc.getInjectable(arc.getCtor(), arc.getParameters(), scope);
            cs.add(new ConstructorInjectablePair(arc.getCtor(), is));
        }

        return cs.first();        
    }
 }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy