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.ioc;
 
 import java.util.List;
An extension of com.sun.jersey.core.spi.component.ProviderFactory that defers to an IoCComponentProviderFactory.

Author(s):
Paul.Sandoz@Sun.Com
 
 public class IoCProviderFactory extends ProviderFactory {
     private final List<IoCComponentProviderFactoryfactories;

    

Parameters:
ipc the injectable provider context.
icpf the IoC component provider factory.
 
     public IoCProviderFactory(
             InjectableProviderContext ipc,
             IoCComponentProviderFactory icpf) {
         this(ipc, Collections.singletonList(icpf));
     }

    

Parameters:
ipc the injectable provider context.
factories the list of IoC component provider factory.
 
     public IoCProviderFactory(
             InjectableProviderContext ipc,
             List<IoCComponentProviderFactoryfactories) {
         super(ipc);
         this. = factories;
     }
 
     @Override
         IoCComponentProvider icp = null;
         for (IoCComponentProviderFactory f : ) {
             icp = f.getComponentProvider(c);
             if (icp != null)
                 break;
         }
         return (icp == null) ? super._getComponentProvider(c) : wrap(cicp);
     }
 
     private ComponentProvider wrap(Class cIoCComponentProvider icp) {
         if (icp instanceof IoCManagedComponentProvider) {
             IoCManagedComponentProvider imcp = (IoCManagedComponentProvider)icp;
             if (imcp.getScope() == .) {
                return new ManagedSingleton(getInjectableProviderContext(), imcpc);
            } else {
                throw new RuntimeException("The scope of the component " + c + " must be a singleton");
            }
        } else if (icp instanceof IoCFullyManagedComponentProvider) {
            return new FullyManagedSingleton(ifmcp.getInstance());
        } else if (icp instanceof IoCInstantiatedComponentProvider) {
            return new InstantiatedSingleton(getInjectableProviderContext(), iicpc);
        } else if (icp instanceof IoCProxiedComponentProvider) {
            IoCProxiedComponentProvider ipcp = (IoCProxiedComponentProvider)icp;
            ComponentProvider cp = super._getComponentProvider(c);
            // Problem creating the component provider
            if (cp == null)
                return null;
            return new ProxiedSingletonWrapper(ipcpcpc);
        }
        throw new UnsupportedOperationException();
    }
    private static class InstantiatedSingleton implements ComponentProviderDestroyable {
        private final Object o;
        private final IoCDestroyable destroyable;
        
        private final ComponentDestructor cd;
                IoCInstantiatedComponentProvider iicp,
                Class c) {
            this. = (iicp instanceof IoCDestroyable)
                    ? (IoCDestroyableiicp : null;
            
             = iicp.getInstance();
            this. = ( == null) ? new ComponentDestructor(c) : null;
            if ( == null) {
                ComponentInjector ci = new ComponentInjector(
                        ipc,
                        c);
                ci.inject(iicp.getInjectableInstance());
            }
        }
        @Override
        public Object getInstance() {
            return ;
        }
        @Override
        public void destroy() {
            if ( != null) {
                .destroy();
            } else {
                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 static class ManagedSingleton implements ComponentProvider {
        private final Object o;
                IoCInstantiatedComponentProvider iicp,
                Class c) {
            ComponentInjector rci = new ComponentInjector(
                    ipc,
                    c);
             = iicp.getInstance();
            rci.inject(iicp.getInjectableInstance());
        }
        @Override
        public Object getInstance() {
            return ;
        }
    }
    private static class FullyManagedSingleton implements ComponentProvider {
        private final Object o;
        FullyManagedSingleton(Object o) {
            this. = o;
        }
        @Override
        public Object getInstance() {
            return ;
        }
    }
    private static class ProxiedSingletonWrapper implements ComponentProviderDestroyable {
        private final Destroyable destroyable;
        private final Object proxy;
                ComponentProvider cp,
                Class c) {
            this. = (cp instanceof Destroyable)
                    ? (Destroyablecp : null;
            
            Object o = cp.getInstance();
            this. = ipcp.proxy(o);
            if (!this..getClass().isAssignableFrom(o.getClass()))
                throw new IllegalStateException("Proxied object class " + this..getClass() +
                        " is not assignable from object class " + o.getClass());
        }
        @Override
        public Object getInstance() {
            return ;
        }
        @Override
        public void destroy() {
            if ( != null) {
                .destroy();
            }
        }
    }
 }
New to GrepCode? Check out our FAQ X