Example usage for javax.xml.ws Holder Holder

List of usage examples for javax.xml.ws Holder Holder

Introduction

In this page you can find the example usage for javax.xml.ws Holder Holder.

Prototype

public Holder() 

Source Link

Document

Creates a new holder with a null value.

Usage

From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java

/**
 * @param ClassLoader/*from w w w  .ja va 2 s  .co  m*/
 * @param forceArrays boolean (if true, then JAXBContext will automatically contain arrays)
 * @return get the JAXBContext
 * @throws JAXBException
 */
public JAXBContext getJAXBContext(ClassLoader cl, boolean forceArrays) throws JAXBException {
    if (customerJAXBContext != null) {
        return customerJAXBContext;
    }

    // Get the weakly cached JAXBContext
    JAXBContext jc = null;
    if (autoJAXBContext != null) {
        jc = autoJAXBContext.get();
    }

    if (forceArrays && jc != null
            && constructionType != JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS) {
        if (log.isDebugEnabled()) {
            log.debug("A JAXBContext exists but it was not constructed with array class.  "
                    + "The JAXBContext will be rebuilt.");
        }
        jc = null;
    }

    if (jc == null) {
        if (log.isDebugEnabled()) {
            log.debug("Creating a JAXBContext with the context packages.");
        }
        Holder<JAXBUtils.CONSTRUCTION_TYPE> constructType = new Holder<JAXBUtils.CONSTRUCTION_TYPE>();
        Map<String, Object> properties = null;

        /*
         * We set the default namespace to the web service namespace to fix an
         * obscure bug.
         * 
         * If the class representing a JAXB data object does not define a namespace
         * (via an annotation like @XmlType or via ObjectFactory or schema gen information)
         * then the namespace information is defaulted.
         * 
         * The xjc tool defaults the namespace information to unqualified.
         * However the wsimport tool defaults the namespace to the namespace of the
         * webservice.
         * 
         * To "workaround" this issue, a default namespace equal to the webservice
         * namespace is set on the JAXB marshaller.  This has the effect of changing the
         * "unqualified namespaces" into the namespace used by the webservice.
         * 
         */
        if (this.webServiceNamespace != null) {
            properties = new HashMap<String, Object>();
            properties.put(JAXBUtils.DEFAULT_NAMESPACE_REMAP, this.webServiceNamespace);
        }
        jc = JAXBUtils.getJAXBContext(contextPackages, constructType, forceArrays, contextPackagesKey, cl,
                properties);
        constructionType = constructType.value;
        autoJAXBContext = new WeakReference<JAXBContext>(jc);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Using an existing JAXBContext");
        }
    }
    return jc;
}

From source file:org.apache.axis2.jaxws.client.proxy.JAXWSProxyHandler.java

/**
 * Choose a classloader most likely to marshal the message
 * successfully//  w  ww . j av  a  2  s.  com
 * @param cls
 * @return ClassLoader
 */
private static ClassLoader chooseClassLoader(Class cls, ServiceDescription serviceDesc) {
    if (log.isDebugEnabled()) {
        log.debug("Choose Classloader for " + cls);
    }
    ClassLoader cl = null;
    ClassLoader contextCL = getContextClassLoader();
    ClassLoader classCL = getClassLoader(cls);
    if (log.isDebugEnabled()) {
        log.debug("Context ClassLoader is " + contextCL);
        log.debug("Class ClassLoader is " + classCL);
    }

    if (classCL == null || contextCL == classCL) {
        // Normal case: Use the context ClassLoader
        cl = contextCL;
    } else {
        // Choose the better of the JAXBContexts
        MarshalServiceRuntimeDescription marshalDesc = MarshalServiceRuntimeDescriptionFactory.get(serviceDesc);

        // Get the JAXBContext for the context classloader 
        Holder<JAXBUtils.CONSTRUCTION_TYPE> holder_contextCL = new Holder<JAXBUtils.CONSTRUCTION_TYPE>();
        JAXBContext jbc_contextCL = null;
        try {
            jbc_contextCL = JAXBUtils.getJAXBContext(marshalDesc.getPackages(), holder_contextCL,
                    marshalDesc.getPackagesKey(), contextCL, null);
        } catch (Throwable t) {
            if (log.isDebugEnabled()) {
                log.debug("Error occured..Processing continues " + t);
            }
        }

        // Get the JAXBContext using the class's ClassLoader
        Holder<JAXBUtils.CONSTRUCTION_TYPE> holder_classCL = new Holder<JAXBUtils.CONSTRUCTION_TYPE>();
        JAXBContext jbc_classCL = null;
        try {
            jbc_classCL = JAXBUtils.getJAXBContext(marshalDesc.getPackages(), holder_classCL,
                    marshalDesc.getPackagesKey(), contextCL, null);
        } catch (Throwable t) {
            if (log.isDebugEnabled()) {
                log.debug("Error occured..Processing continues " + t);
            }
        }

        // Heuristic to choose the better classloader to marshal the
        // data.  Slight priority given to the classloader that loaded
        // the sei class.
        if (jbc_classCL == null) {
            // A JAXBContext could not be loaded for the class's classlaoder,
            // choose the context ClassLoader
            if (log.isDebugEnabled()) {
                log.debug("Could not load JAXBContext for Class ClassLoader");
            }
            cl = contextCL;
        } else if (jbc_contextCL == null) {
            // A JAXBContext could not be loaded for the context's classloader,
            // choose the class ClassLoader
            if (log.isDebugEnabled()) {
                log.debug("Could not load JAXBContext for Context ClassLoader");
            }
            cl = classCL;
        } else if (holder_contextCL.value == JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH
                && holder_classCL.value == JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH) {
            // Both were successfully built with the context path.
            // Choose the one associated with the proxy class
            if (log.isDebugEnabled()) {
                log.debug("Loaded both JAXBContexts with BY_CONTEXT_PATH.  Choose Class ClassLoader");
            }
            cl = classCL;
        } else if (holder_contextCL.value == JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH) {
            // Successfully found all classes with classloader, use this one
            if (log.isDebugEnabled()) {
                log.debug(
                        "Successfully loaded JAXBContext with Contxst ClassLoader.  Choose Context ClassLoader");
            }
            cl = contextCL;
        } else if (holder_classCL.value == JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH) {
            // Successfully found all classes with classloader, use this one
            if (log.isDebugEnabled()) {
                log.debug("Successfully loaded JAXBContext with Class ClassLoader.  Choose Class ClassLoader");
            }
            cl = classCL;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Default to Class ClassLoader");
            }
            cl = classCL;
        }
    }

    if (log.isDebugEnabled()) {
        log.debug("Chosen ClassLoader is " + cls);
    }
    return cl;
}

From source file:org.apache.axis2.jaxws.marshaller.factory.MethodMarshallerFactory.java

/**
 * @param op/*from ww  w  .  ja va  2s  .c o m*/
 * @return true if JAXBContext constructed using CONTEXT PATH
 */
private static boolean isContextPathConstruction(OperationDescription op, ClassLoader cl) {
    ServiceDescription serviceDesc = op.getEndpointInterfaceDescription().getEndpointDescription()
            .getServiceDescription();
    MarshalServiceRuntimeDescription marshalDesc = MarshalServiceRuntimeDescriptionFactory.get(serviceDesc);
    // Get the JAXBContext...Since this is a cached object we incur no penalty by looking at this point.
    Holder<JAXBUtils.CONSTRUCTION_TYPE> holder = new Holder<JAXBUtils.CONSTRUCTION_TYPE>();
    try {
        JAXBContext context = JAXBUtils.getJAXBContext(marshalDesc.getPackages(), holder,
                marshalDesc.getPackagesKey(), cl, null);
    } catch (JAXBException e) {
        throw ExceptionFactory.makeWebServiceException(e);
    }
    if (holder.value == JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH) {
        // If JAXBContext was constructed with a context path, this indicates that ObjectFactory (or other
        // objects) are available.  
        return true;
    } else {
        // If JAXBContext was constructed using a class[] or we don't know how it was constructed, then assume
        // that we need to do the specialized "minimal" marshalling.
        return false;
    }
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

/**
 * Get a JAXBContext for the class//from w w w .jav  a2s . c  o  m
 *
 * @param contextPackage Set<Package>
 * @return JAXBContext
 * @throws JAXBException
 * @deprecated
 */
public static JAXBContext getJAXBContext(TreeSet<String> contextPackages) throws JAXBException {
    return getJAXBContext(contextPackages, new Holder<CONSTRUCTION_TYPE>(), contextPackages.toString(), null,
            null);
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

/**
 * Get a JAXBContext for the class//from   w  w  w .j  a v a 2 s. co m
 * 
 * Note: The contextPackage object is used by multiple threads.  It should be considered immutable
 * and not altered by this method.
 *
 * @param contextPackage Set<Package>
 * @param cacheKey ClassLoader
 * @return JAXBContext
 * @throws JAXBException
 * @deprecated
 */
public static JAXBContext getJAXBContext(TreeSet<String> contextPackages, ClassLoader cacheKey)
        throws JAXBException {
    return getJAXBContext(contextPackages, new Holder<CONSTRUCTION_TYPE>(), contextPackages.toString(),
            cacheKey, null);
}

From source file:org.apache.cxf.systest.jaxrs.JAXRS20ClientServerBookTest.java

@Test
public void testPostCollectionGenericEntity() throws Exception {

    String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3";
    WebClient wc = WebClient.create(endpointAddress);
    wc.accept("application/xml").type("application/xml");

    GenericEntity<List<Book>> collectionEntity = createGenericEntity();
    final Holder<Book> holder = new Holder<Book>();
    InvocationCallback<Book> callback = createCallback(holder);

    Future<Book> future = wc.post(collectionEntity, callback);
    Book book = future.get();//from   w  ww .  ja  v a2s .c o  m
    assertEquals(200, wc.getResponse().getStatus());
    assertSame(book, holder.value);
    assertNotSame(collectionEntity.getEntity().get(0), book);
    assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName());
}

From source file:org.apache.cxf.systest.jaxrs.JAXRS20ClientServerBookTest.java

@Test
public void testPostCollectionGenericEntityGenericCallback() throws Exception {

    String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3";
    WebClient wc = WebClient.create(endpointAddress);
    wc.accept("application/xml").type("application/xml");

    GenericEntity<List<Book>> collectionEntity = createGenericEntity();
    final Holder<Book> holder = new Holder<Book>();
    InvocationCallback<Book> callback = new GenericInvocationCallback<Book>(holder) {
    };//w ww  . jav  a  2  s . c  om

    Future<Book> future = wc.post(collectionEntity, callback);
    Book book = future.get();
    assertEquals(200, wc.getResponse().getStatus());
    assertSame(book, holder.value);
    assertNotSame(collectionEntity.getEntity().get(0), book);
    assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName());
}

From source file:org.apache.cxf.systest.jaxrs.JAXRS20ClientServerBookTest.java

@Test
public void testPostCollectionGenericEntityAsEntity() throws Exception {

    String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3";
    WebClient wc = WebClient.create(endpointAddress);
    wc.accept("application/xml");

    GenericEntity<List<Book>> collectionEntity = createGenericEntity();

    final Holder<Book> holder = new Holder<Book>();
    InvocationCallback<Book> callback = createCallback(holder);

    Future<Book> future = wc.async().post(Entity.entity(collectionEntity, "application/xml"), callback);
    Book book = future.get();/*from  w w  w  .jav  a  2  s.  c o  m*/
    assertEquals(200, wc.getResponse().getStatus());
    assertSame(book, holder.value);
    assertNotSame(collectionEntity.getEntity().get(0), book);
    assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName());
}

From source file:org.apache.cxf.systest.jaxrs.JAXRS20ClientServerBookTest.java

private void doTestPostGetCollectionGenericEntityAndType(WebClient wc, String mt) throws Exception {

    wc.accept(mt).type(mt);/*from   w ww.j  a v  a2 s  .  c  o m*/
    GenericEntity<List<Book>> collectionEntity = createGenericEntity();
    final Holder<List<Book>> holder = new Holder<List<Book>>();
    InvocationCallback<List<Book>> callback = new CustomInvocationCallback(holder);

    Future<List<Book>> future = wc.async().post(Entity.entity(collectionEntity, mt), callback);

    List<Book> books2 = future.get();
    assertNotNull(books2);

    List<Book> books = collectionEntity.getEntity();
    assertNotSame(books, books2);
    assertEquals(2, books2.size());
    Book b11 = books.get(0);
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    Book b22 = books.get(1);
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
    assertEquals(200, wc.getResponse().getStatus());
}

From source file:org.apache.cxf.systest.jaxrs.JAXRS20ClientServerBookTest.java

private void doTestGetBookAsync(String address, boolean asyncInvoker)
        throws InterruptedException, ExecutionException {

    WebClient wc = createWebClient(address);

    final Holder<Book> holder = new Holder<Book>();
    InvocationCallback<Book> callback = createCallback(holder);

    Future<Book> future = asyncInvoker ? wc.async().get(callback) : wc.get(callback);
    Book book = future.get();/* ww  w .  j a va 2s . c  o  m*/
    assertSame(book, holder.value);
    assertEquals(124L, book.getId());
    validateResponse(wc);
}