src/share/jaxws_classes/com/sun/xml/internal/ws/util/Pool.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.util;
    28 import com.sun.xml.internal.ws.api.pipe.Tube;
    29 import com.sun.xml.internal.ws.api.pipe.TubeCloner;
    31 import javax.xml.bind.JAXBContext;
    32 import javax.xml.bind.JAXBException;
    33 import java.util.concurrent.ConcurrentLinkedQueue;
    34 import java.lang.ref.WeakReference;
    36 /**
    37  * General-purpose object pool.
    38  *
    39  * <p>
    40  * In many parts of the runtime, we need to pool instances of objects that
    41  * are expensive to create (such as JAXB objects, StAX parsers, {@link Tube} instances.)
    42  *
    43  * <p>
    44  * This class provides a default implementation of such a pool.
    45  *
    46  * TODO: improve the implementation
    47  *
    48  * @author Kohsuke Kawaguchi
    49  */
    50 public abstract class Pool<T> {
    52     // volatile since multiple threads may access queue reference
    53     private volatile WeakReference<ConcurrentLinkedQueue<T>> queue;
    55     /**
    56      * Gets a new object from the pool.
    57      *
    58      * <p>
    59      * If no object is available in the pool, this method creates a new one.
    60      *
    61      * @return
    62      *      always non-null.
    63      */
    64     public final T take() {
    65         T t = getQueue().poll();
    66         if(t==null)
    67             return create();
    68         return t;
    69     }
    71     private ConcurrentLinkedQueue<T> getQueue() {
    72         WeakReference<ConcurrentLinkedQueue<T>> q = queue;
    73         if (q != null) {
    74             ConcurrentLinkedQueue<T> d = q.get();
    75             if (d != null)
    76                 return d;
    77         }
    79         // overwrite the queue
    80         ConcurrentLinkedQueue<T> d = new ConcurrentLinkedQueue<T>();
    81         queue = new WeakReference<ConcurrentLinkedQueue<T>>(d);
    83         return d;
    84     }
    86     /**
    87      * Returns an object back to the pool.
    88      */
    89     public final void recycle(T t) {
    90         getQueue().offer(t);
    91     }
    93     /**
    94      * Creates a new instance of object.
    95      *
    96      * <p>
    97      * This method is used when someone wants to
    98      * {@link #take() take} an object from an empty pool.
    99      *
   100      * <p>
   101      * Also note that multiple threads may call this method
   102      * concurrently.
   103      */
   104     protected abstract T create();
   107     /**
   108      * JAXB {@link javax.xml.bind.Marshaller} pool.
   109      */
   110     public static final class Marshaller extends Pool<javax.xml.bind.Marshaller> {
   111         private final JAXBContext context;
   113         public Marshaller(JAXBContext context) {
   114             this.context = context;
   115         }
   117         protected javax.xml.bind.Marshaller create() {
   118             try {
   119                 return context.createMarshaller();
   120             } catch (JAXBException e) {
   121                 // impossible
   122                 throw new AssertionError(e);
   123             }
   124         }
   125     }
   127     /**
   128      * JAXB {@link javax.xml.bind.Marshaller} pool.
   129      */
   130     public static final class Unmarshaller extends Pool<javax.xml.bind.Unmarshaller> {
   131         private final JAXBContext context;
   133         public Unmarshaller(JAXBContext context) {
   134             this.context = context;
   135         }
   137         protected javax.xml.bind.Unmarshaller create() {
   138             try {
   139                 return context.createUnmarshaller();
   140             } catch (JAXBException e) {
   141                 // impossible
   142                 throw new AssertionError(e);
   143             }
   144         }
   145     }
   147     /**
   148      * {@link Tube} pool.
   149      */
   150     public static final class TubePool extends Pool<Tube> {
   151         private final Tube master;
   153         public TubePool(Tube master) {
   154             this.master = master;
   155             recycle(master);    // we'll use master as a part of the pool, too.
   156         }
   158         protected Tube create() {
   159             return TubeCloner.clone(master);
   160         }
   161     }
   162 }

mercurial