/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package org.jboss.netty.util.internal;

import java.util.Collection;
import java.util.concurrent.BlockingQueue;

import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;

/**
 * This factory should be used to create the "optimal" {@link BlockingQueue}
 * instance for the running JVM.
 */
public final class QueueFactory {

    private static final boolean useUnsafe = DetectionUtil.hasUnsafe();
    private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(QueueFactory.class);

    private QueueFactory() {
        // only use static methods!
    }


    /**
     * Create a new unbound {@link BlockingQueue}
     *
     * @param itemClass  the {@link Class} type which will be used as {@link BlockingQueue} items
     * @return queue     the {@link BlockingQueue} implementation
     */
    public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
        // if we run in java >=7 its the best to just use the LinkedTransferQueue which
        // comes with java bundled. See #273
        if (DetectionUtil.javaVersion() >= 7)  {
            return new java.util.concurrent.LinkedTransferQueue<T>();
        }

        try {
            if (useUnsafe) {
                return new LinkedTransferQueue<T>();
            }
        } catch (Throwable t) {
            // For whatever reason an exception was thrown while loading the LinkedTransferQueue
            //
            // This mostly happens because of a custom classloader or security policy that did not
            // allow us to access the com.sun.Unmisc class. So just log it and fallback to the old
            // LegacyLinkedTransferQueue that works in all cases
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Unable to instance LinkedTransferQueue, fallback to LegacyLinkedTransferQueue", t);
            }
        }

        return new LegacyLinkedTransferQueue<T>();

    }

    /**
     * Create a new unbound {@link BlockingQueue}
     *
     * @param collection  the collection which should get copied to the newly created {@link BlockingQueue}
     * @param itemClass   the {@link Class} type which will be used as {@link BlockingQueue} items
     * @return queue      the {@link BlockingQueue} implementation
     */
    public static <T> BlockingQueue<T> createQueue(Collection<? extends T> collection, Class<T> itemClass) {
        // if we run in java >=7 its the best to just use the LinkedTransferQueue which
        // comes with java bundled. See #273
        if (DetectionUtil.javaVersion() >= 7)  {
            return new java.util.concurrent.LinkedTransferQueue<T>();
        }

        try {
            if (useUnsafe) {
                return new LinkedTransferQueue<T>(collection);
            }
        } catch (Throwable t) {
            // For whatever reason an exception was thrown while loading the LinkedTransferQueue
            //
            // This mostly happens because of a custom classloader or security policy that did not
            // allow us to access the com.sun.Unmisc class. So just log it and fallback to the old
            // LegacyLinkedTransferQueue that works in all cases
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Unable to instance LinkedTransferQueue, fallback to LegacyLinkedTransferQueue", t);
            }
        }

        return new LegacyLinkedTransferQueue<T>(collection);

    }
}

@@ -1475,32 +1475,67 @@
 ntation%0A     */%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     public stati
@@ -1583,32 +1583,47 @@
 T%3E itemClass) %7B%0A
+        try %7B%0A 
         // if we
@@ -1694,32 +1694,36 @@
 e which%0A        
+    
 // comes with ja
@@ -1743,32 +1743,36 @@
 ee #273%0A        
+   
+ 
 if (DetectionUti
@@ -1804,35 +1804,203 @@
             
-return new 
+    // Use reflect as android otherwise will panic out. See #458%0A                return (BlockingQueue%3CT%3E) QueueFactory.class.getClassLoader()%0A                        .loadClass(%22
 java.util.co
@@ -2019,35 +2019,46 @@
 kedTransferQueue
-%3CT%3E
+%22).newInstance
 ();%0A        %7D%0A%0A 
@@ -2053,32 +2053,21 @@
         
-%7D%0A%0A
     
-    try %7B
+%7D
 %0A       
@@ -3251,32 +3251,50 @@
 T%3E itemClass) %7B%0A
+        try %7B%0A    
         // if we
@@ -3365,32 +3365,36 @@
 e which%0A        
+    
 // comes with ja
@@ -3414,32 +3414,36 @@
 ee #273%0A        
+ 
+   
 if (DetectionUti
@@ -3479,27 +3479,274 @@
         
-return new 
+    // Use reflect as android otherwise will panic out. See #458%0A                @SuppressWarnings(%22unchecked%22)%0A                Class%3CBlockingQueue%3CT%3E%3E clazz = (Class%3CBlockingQueue%3CT%3E%3E) QueueFactory.class.getClassLoader()%0A                        .loadClass(%22
 java.uti
@@ -3769,36 +3769,33 @@
 kedTransferQueue
-%3CT%3E(
+%22
 );%0A        %7D%0A%0A  
@@ -3789,27 +3789,24 @@
         
-%7D%0A%0A
         
 try %7B%0A  
@@ -3789,37 +3789,116 @@
                 
-try %7B
+return clazz.getConstructor(Collection.class).newInstance(collection);%0A            %7D
 %0A            if 
@@ -4645,10 +4645,11 @@
 %0A%0A    %7D%0A
+%0A
 %7D%0A

Use reflection to instance java.util.concurrent.LinkedTransferQueue to make Android not fail. See #458