1/*
2 * Copyright (c) 1997, 2013, 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 */
25
26package com.sun.xml.internal.ws.developer;
27
28import com.sun.xml.internal.ws.api.FeatureConstructor;
29import com.sun.xml.internal.ws.api.model.SEIModel;
30import com.sun.xml.internal.bind.api.JAXBRIContext;
31import com.sun.xml.internal.bind.api.TypeReference;
32import com.sun.istack.internal.NotNull;
33import com.sun.istack.internal.Nullable;
34
35import javax.xml.ws.WebServiceFeature;
36import javax.xml.bind.JAXBContext;
37import javax.xml.bind.JAXBException;
38import java.lang.reflect.InvocationTargetException;
39import java.util.List;
40
41import com.sun.org.glassfish.gmbal.ManagedAttribute;
42import com.sun.org.glassfish.gmbal.ManagedData;
43
44/**
45 * A {@link WebServiceFeature} that instructs the JAX-WS runtime to use a specific {@link JAXBContextFactory}
46 * instance of creating {@link JAXBContext}.
47 *
48 * @see UsesJAXBContext
49 * @since 2.1.5
50 * @author Kohsuke Kawaguchi
51 */
52@ManagedData
53public class UsesJAXBContextFeature extends WebServiceFeature {
54    /**
55     * Constant value identifying the {@link UsesJAXBContext} feature.
56     */
57    public static final String ID = "http://jax-ws.dev.java.net/features/uses-jaxb-context";
58
59    private final JAXBContextFactory factory;
60
61    /**
62     * Creates {@link UsesJAXBContextFeature}.
63     *
64     * @param factoryClass
65     *      This class has to have a public no-arg constructor, which will be invoked to create
66     *      a new instance. {@link JAXBContextFactory#createJAXBContext(SEIModel, List, List)} will
67     *      be then called to create {@link JAXBContext}.
68     */
69    @FeatureConstructor("value")
70    public UsesJAXBContextFeature(@NotNull Class<? extends JAXBContextFactory> factoryClass) {
71        try {
72            factory = factoryClass.getConstructor().newInstance();
73        } catch (InstantiationException e) {
74            Error x = new InstantiationError(e.getMessage());
75            x.initCause(e);
76            throw x;
77        } catch (IllegalAccessException e) {
78            Error x = new IllegalAccessError(e.getMessage());
79            x.initCause(e);
80            throw x;
81        } catch (InvocationTargetException e) {
82            Error x = new InstantiationError(e.getMessage());
83            x.initCause(e);
84            throw x;
85        } catch (NoSuchMethodException e) {
86            Error x = new NoSuchMethodError(e.getMessage());
87            x.initCause(e);
88            throw x;
89        }
90    }
91
92    /**
93     * Creates {@link UsesJAXBContextFeature}.
94     * This version allows {@link JAXBContextFactory} to carry application specific state.
95     *
96     * @param factory
97     *      Uses a specific instance of {@link JAXBContextFactory} to create {@link JAXBContext}.
98     */
99    public UsesJAXBContextFeature(@Nullable JAXBContextFactory factory) {
100        this.factory = factory;
101    }
102
103    /**
104     * Creates {@link UsesJAXBContextFeature}.
105     * This version allows you to create {@link JAXBRIContext} upfront and uses it.
106     */
107    public UsesJAXBContextFeature(@Nullable final JAXBRIContext context) {
108        this.factory = new JAXBContextFactory() {
109            @NotNull
110            public JAXBRIContext createJAXBContext(@NotNull SEIModel sei, @NotNull List<Class> classesToBind, @NotNull List<TypeReference> typeReferences) throws JAXBException {
111                return context;
112            }
113        };
114    }
115
116    /**
117     * Gets the {@link JAXBContextFactory} instance to be used for creating {@link JAXBContext} for SEI.
118     *
119     * @return
120     *      null if the default {@link JAXBContext} shall be used.
121     */
122    @ManagedAttribute
123    public @Nullable JAXBContextFactory getFactory() {
124        return factory;
125    }
126
127    @ManagedAttribute
128    public String getID() {
129        return ID;
130    }
131}
132