1/*
2 * Copyright (c) 1997, 2015, 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.oracle.webservices.internal.api.message;
27
28import java.io.IOException;
29import java.io.InputStream;
30
31import com.oracle.webservices.internal.api.EnvelopeStyle;
32import com.sun.xml.internal.ws.api.SOAPVersion; // TODO leaking RI APIs
33import com.sun.xml.internal.ws.util.ServiceFinder;
34
35import javax.xml.soap.MimeHeaders;
36import javax.xml.soap.SOAPMessage;
37import javax.xml.transform.Source;
38import javax.xml.ws.WebServiceFeature;
39
40public abstract class MessageContextFactory
41{
42    private static final MessageContextFactory DEFAULT = new com.sun.xml.internal.ws.api.message.MessageContextFactory(new WebServiceFeature[0]);
43
44    protected com.sun.xml.internal.ws.api.message.saaj.SAAJFactory saajFactory = null;
45
46    protected abstract MessageContextFactory newFactory(WebServiceFeature ... f);
47
48    public abstract MessageContext createContext();
49
50    public abstract MessageContext createContext(SOAPMessage m);
51
52    public abstract MessageContext createContext(Source m);
53
54    public abstract MessageContext createContext(Source m, EnvelopeStyle.Style envelopeStyle);
55
56    public abstract MessageContext createContext(InputStream in, String contentType) throws IOException;
57
58    /**
59     * @deprecated http://java.net/jira/browse/JAX_WS-1077
60     */
61    @Deprecated
62    public abstract MessageContext createContext(InputStream in, MimeHeaders headers) throws IOException;
63
64    static public MessageContextFactory createFactory(WebServiceFeature ... f) {
65        return createFactory(null, f);
66    }
67
68    static public MessageContextFactory createFactory(ClassLoader cl, WebServiceFeature ...f) {
69        for (MessageContextFactory factory : ServiceFinder.find(MessageContextFactory.class, cl)) {
70            MessageContextFactory newfac = factory.newFactory(f);
71            if (newfac != null) return newfac;
72        }
73        return new com.sun.xml.internal.ws.api.message.MessageContextFactory(f);
74    }
75
76    @Deprecated
77    public abstract MessageContext doCreate();
78
79    @Deprecated
80    public abstract MessageContext doCreate(SOAPMessage m);
81
82    //public abstract MessageContext doCreate(InputStream x);
83
84    @Deprecated
85    public abstract MessageContext doCreate(Source x, SOAPVersion soapVersion);
86
87    @Deprecated
88    public static MessageContext create(final ClassLoader... classLoader) {
89        return serviceFinder(classLoader,
90                             new Creator() {
91                                 public MessageContext create(final MessageContextFactory f) {
92                                     return f.doCreate();
93                                 }
94                             });
95    }
96
97    @Deprecated
98    public static MessageContext create(final SOAPMessage m, final ClassLoader... classLoader) {
99        return serviceFinder(classLoader,
100                             new Creator() {
101                                 public MessageContext create(final MessageContextFactory f) {
102                                     return f.doCreate(m);
103                                 }
104                             });
105    }
106
107    @Deprecated
108    public static MessageContext create(final Source m, final SOAPVersion v, final ClassLoader... classLoader) {
109        return serviceFinder(classLoader,
110                             new Creator() {
111                                 public MessageContext create(final MessageContextFactory f) {
112                                     return f.doCreate(m, v);
113                                 }
114                             });
115    }
116
117    @Deprecated
118    private static MessageContext serviceFinder(final ClassLoader[] classLoader, final Creator creator) {
119        final ClassLoader cl = classLoader.length == 0 ? null : classLoader[0];
120        for (MessageContextFactory factory : ServiceFinder.find(MessageContextFactory.class, cl)) {
121            final MessageContext messageContext = creator.create(factory);
122            if (messageContext != null)
123                return messageContext;
124        }
125        return creator.create(DEFAULT);
126    }
127
128    @Deprecated
129    private static interface Creator {
130        public MessageContext create(MessageContextFactory f);
131    }
132
133    public void setSAAJFactory(com.sun.xml.internal.ws.api.message.saaj.SAAJFactory saajFactory) {
134        this.saajFactory = saajFactory;
135    }
136}
137