1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/Pool.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,162 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.util; 1.30 + 1.31 +import com.sun.xml.internal.ws.api.pipe.Tube; 1.32 +import com.sun.xml.internal.ws.api.pipe.TubeCloner; 1.33 + 1.34 +import javax.xml.bind.JAXBContext; 1.35 +import javax.xml.bind.JAXBException; 1.36 +import java.util.concurrent.ConcurrentLinkedQueue; 1.37 +import java.lang.ref.WeakReference; 1.38 + 1.39 +/** 1.40 + * General-purpose object pool. 1.41 + * 1.42 + * <p> 1.43 + * In many parts of the runtime, we need to pool instances of objects that 1.44 + * are expensive to create (such as JAXB objects, StAX parsers, {@link Tube} instances.) 1.45 + * 1.46 + * <p> 1.47 + * This class provides a default implementation of such a pool. 1.48 + * 1.49 + * TODO: improve the implementation 1.50 + * 1.51 + * @author Kohsuke Kawaguchi 1.52 + */ 1.53 +public abstract class Pool<T> { 1.54 + 1.55 + // volatile since multiple threads may access queue reference 1.56 + private volatile WeakReference<ConcurrentLinkedQueue<T>> queue; 1.57 + 1.58 + /** 1.59 + * Gets a new object from the pool. 1.60 + * 1.61 + * <p> 1.62 + * If no object is available in the pool, this method creates a new one. 1.63 + * 1.64 + * @return 1.65 + * always non-null. 1.66 + */ 1.67 + public final T take() { 1.68 + T t = getQueue().poll(); 1.69 + if(t==null) 1.70 + return create(); 1.71 + return t; 1.72 + } 1.73 + 1.74 + private ConcurrentLinkedQueue<T> getQueue() { 1.75 + WeakReference<ConcurrentLinkedQueue<T>> q = queue; 1.76 + if (q != null) { 1.77 + ConcurrentLinkedQueue<T> d = q.get(); 1.78 + if (d != null) 1.79 + return d; 1.80 + } 1.81 + 1.82 + // overwrite the queue 1.83 + ConcurrentLinkedQueue<T> d = new ConcurrentLinkedQueue<T>(); 1.84 + queue = new WeakReference<ConcurrentLinkedQueue<T>>(d); 1.85 + 1.86 + return d; 1.87 + } 1.88 + 1.89 + /** 1.90 + * Returns an object back to the pool. 1.91 + */ 1.92 + public final void recycle(T t) { 1.93 + getQueue().offer(t); 1.94 + } 1.95 + 1.96 + /** 1.97 + * Creates a new instance of object. 1.98 + * 1.99 + * <p> 1.100 + * This method is used when someone wants to 1.101 + * {@link #take() take} an object from an empty pool. 1.102 + * 1.103 + * <p> 1.104 + * Also note that multiple threads may call this method 1.105 + * concurrently. 1.106 + */ 1.107 + protected abstract T create(); 1.108 + 1.109 + 1.110 + /** 1.111 + * JAXB {@link javax.xml.bind.Marshaller} pool. 1.112 + */ 1.113 + public static final class Marshaller extends Pool<javax.xml.bind.Marshaller> { 1.114 + private final JAXBContext context; 1.115 + 1.116 + public Marshaller(JAXBContext context) { 1.117 + this.context = context; 1.118 + } 1.119 + 1.120 + protected javax.xml.bind.Marshaller create() { 1.121 + try { 1.122 + return context.createMarshaller(); 1.123 + } catch (JAXBException e) { 1.124 + // impossible 1.125 + throw new AssertionError(e); 1.126 + } 1.127 + } 1.128 + } 1.129 + 1.130 + /** 1.131 + * JAXB {@link javax.xml.bind.Marshaller} pool. 1.132 + */ 1.133 + public static final class Unmarshaller extends Pool<javax.xml.bind.Unmarshaller> { 1.134 + private final JAXBContext context; 1.135 + 1.136 + public Unmarshaller(JAXBContext context) { 1.137 + this.context = context; 1.138 + } 1.139 + 1.140 + protected javax.xml.bind.Unmarshaller create() { 1.141 + try { 1.142 + return context.createUnmarshaller(); 1.143 + } catch (JAXBException e) { 1.144 + // impossible 1.145 + throw new AssertionError(e); 1.146 + } 1.147 + } 1.148 + } 1.149 + 1.150 + /** 1.151 + * {@link Tube} pool. 1.152 + */ 1.153 + public static final class TubePool extends Pool<Tube> { 1.154 + private final Tube master; 1.155 + 1.156 + public TubePool(Tube master) { 1.157 + this.master = master; 1.158 + recycle(master); // we'll use master as a part of the pool, too. 1.159 + } 1.160 + 1.161 + protected Tube create() { 1.162 + return TubeCloner.clone(master); 1.163 + } 1.164 + } 1.165 +}