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