Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.core.spi.component;
 
 import java.util.Map;
A component provider factory for provider components (which are singletons).

A cache of component providers is managed. When a component provider for a class is obtained it is cached such that the same instance on subsequent requests.

Author(s):
Paul.Sandoz@Sun.Com
 
     protected static final Logger LOGGER = Logger.getLogger(ProviderFactory.class.getName());
 
     protected interface Destroyable {
         void destroy();
     }
 
     private static final class SingletonComponentProvider implements ComponentProviderDestroyable {
         private final Object o;
 
         private final ComponentDestructor cd;
 
         private final ComponentInjector ci;
 
             this. = new ComponentDestructor(o.getClass());
             this. = ci;
             this. = o;
         }
         
         public Object getInstance() {
             return ;
         }
 
         public void inject() {
             .inject();
         }
 
         public void destroy() {
             try {
                 .destroy();
             } catch (IllegalAccessException ex) {
                 .log(."Unable to destroy resource"ex);
             } catch (IllegalArgumentException ex) {
                 .log(."Unable to destroy resource"ex);
             } catch (InvocationTargetException ex) {
                 .log(."Unable to destroy resource"ex);
             }
         }
     }
     
    private final Map<ClassComponentProvidercache =
            new HashMap<ClassComponentProvider>();
    private final InjectableProviderContext ipc;

    
Create the provider factory.

Parameters:
ipc the injectable provider context to get injectables.
        this. = ipc;
    }

    
Get the injectable provider context.

Returns:
the injectable provider context
        return ;
    }

    
Get a component provider for a class.

Parameters:
pc the provider class.
Returns:
the component provider.
        if (!pc.isServiceClass) {
            return getComponentProvider(pc.c);
        }
        ComponentProvider cp = .get(pc.c);
        if (cp != nullreturn cp;
        cp = __getComponentProvider(pc.c);
        
        if (cp != null.put(pc.ccp);
        return cp;
    }

    
Get a component provider for a class.

Parameters:
c the class.
Returns:
the component provider.
    public final ComponentProvider getComponentProvider(Class c) {
        ComponentProvider cp = .get(c);
        if (cp != nullreturn cp;
        cp = _getComponentProvider(c);
        if (cp != null.put(ccp);
        return cp;
    }
        return __getComponentProvider(c);
    }
    
        try {
            ComponentInjector ci = new ComponentInjector(c);
            ComponentConstructor cc = new ComponentConstructor(cci);
            Object o = cc.getInstance();
            return new SingletonComponentProvider(cio);
        } catch (NoClassDefFoundError ex) {
            // Dependent class of provider not found
            // This assumes that ex.getLocalizedMessage() returns
            // the name of a dependent class that is not found
            .log(.,
                    "A dependent class, " + ex.getLocalizedMessage() +
                    ", of the component " + c + " is not found." +
                    " The component is ignored.");
            return null;
        } catch (InvocationTargetException ex) {
            if (ex.getCause() instanceof NoClassDefFoundError) {
                NoClassDefFoundError ncdf = (NoClassDefFoundError)ex.getCause();
                .log(.,
                        "A dependent class, " + ncdf.getLocalizedMessage() +
                        ", of the component " + c + " is not found." +
                        " The component is ignored.");
                return null;
            } else {
                .log(.,
                        "The provider class, " + c +
                        ", could not be instantiated. Processing will continue but the class will not be utilized"ex.getTargetException());
                return null;
            }
        } catch (Exception ex) {
            .log(.,
                    "The provider class, " + c +
                    ", could not be instantiated. Processing will continue but the class will not be utilized"ex);
            return null;
        }
    }

    
Inject on all cached components.
    public void injectOnAllComponents() {
        for (ComponentProvider cp : .values()) {
            if (cp instanceof SingletonComponentProvider) {
                SingletonComponentProvider scp = (SingletonComponentProvider)cp;
                scp.inject();
            }
        }
    }

    
Destroy all cached components.
    public void destroy() {
        for (ComponentProvider cp : .values()) {
            if (cp instanceof Destroyable) {
                Destroyable d = (Destroyable)cp;
                d.destroy();
            }
        }
    }

    
Inject on a collection of providers.

Parameters:
providers the collection of providers.
    public void injectOnProviderInstances(Collection<?> providers) {
        for (Object o : providers) {
            injectOnProviderInstance(o);
        }
    }

    
Inject on a provider.

Parameters:
provider the provider.
    public void injectOnProviderInstance(Object provider) {
        Class c = provider.getClass();
        ComponentInjector ci = new ComponentInjector(c);
        ci.inject(provider);
    }
New to GrepCode? Check out our FAQ X