Java XML JAXB Context createContext(final Class bind)

Here you can find the source of createContext(final Class bind)

Description

Returns a cached JAXB Context.

License

Apache License

Declaration

public synchronized static JAXBContext createContext(final Class<?> bind) throws JAXBException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.lang.ref.SoftReference;

import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class Main {
    private static final Map<Class<?>, SoftReference<JAXBContext>> contextCache = new HashMap<>();

    /**//  ww w  .ja va  2s .c o  m
     * Returns a cached <i>JAXB Context</i>.
     *
     * @since 3.8
     */
    public synchronized static JAXBContext createContext(final Class<?> bind) throws JAXBException {
        JAXBContext context = null;
        SoftReference<JAXBContext> ref = contextCache.get(bind);
        if (ref != null) {
            context = ref.get();
            if (context == null)
                contextCache.remove(bind);
        }

        if (context != null)
            return context;

        synchronized (bind) {
            synchronized (JAXBContext.class) {
                context = JAXBContext.newInstance(bind);
            }
        }

        contextCache.put(bind, new SoftReference<JAXBContext>(context));

        return context;
    }
}

Related

  1. clearContextCache()
  2. createJaxbContextFor(Object obj, Class[] classes)
  3. createRIContext(Class clss[], String defaultNS)
  4. fromXML(JAXBContext context, String requestXML)
  5. generateTemporarySchemaFile(JAXBContext context)