All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.ws.util.Pool Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
 */

package com.sun.xml.ws.util;

import com.sun.xml.ws.api.pipe.Tube;
import com.sun.xml.ws.api.pipe.TubeCloner;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * General-purpose object pool.
 *
 * 

* In many parts of the runtime, we need to pool instances of objects that * are expensive to create (such as JAXB objects, StAX parsers, {@link Tube} instances.) * *

* This class provides a default implementation of such a pool. * * TODO: improve the implementation * * @author Kohsuke Kawaguchi */ public abstract class Pool { private final ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); /** * Gets a new object from the pool. * *

* If no object is available in the pool, this method creates a new one. * * @return * always non-null. */ public final T take() { T t = queue.poll(); if(t==null) return create(); return t; } /** * Returns an object back to the pool. */ public final void recycle(T t) { queue.offer(t); } /** * Creates a new instance of object. * *

* This method is used when someone wants to * {@link #take() take} an object from an empty pool. * *

* Also note that multiple threads may call this method * concurrently. */ protected abstract T create(); /** * JAXB {@link javax.xml.bind.Marshaller} pool. */ public static final class Marshaller extends Pool { private final JAXBContext context; public Marshaller(JAXBContext context) { this.context = context; } protected javax.xml.bind.Marshaller create() { try { return context.createMarshaller(); } catch (JAXBException e) { // impossible throw new AssertionError(e); } } } /** * JAXB {@link javax.xml.bind.Marshaller} pool. */ public static final class Unmarshaller extends Pool { private final JAXBContext context; public Unmarshaller(JAXBContext context) { this.context = context; } protected javax.xml.bind.Unmarshaller create() { try { return context.createUnmarshaller(); } catch (JAXBException e) { // impossible throw new AssertionError(e); } } } /** * {@link Tube} pool. */ public static final class TubePool extends Pool { private final Tube master; public TubePool(Tube master) { this.master = master; recycle(master); // we'll use master as a part of the pool, too. } protected Tube create() { return TubeCloner.clone(master); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy