Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, 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 */
26 package com.sun.xml.internal.ws.db.glassfish;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.Map;
32 import java.lang.reflect.Type;
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.Marshaller;
36 import com.sun.xml.internal.bind.api.TypeReference;
37 import com.sun.xml.internal.bind.api.JAXBRIContext;
38 import com.sun.xml.internal.bind.api.CompositeStructure;
39 import com.sun.xml.internal.bind.v2.ContextFactory;
40 import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader;
41 import com.sun.xml.internal.bind.v2.runtime.MarshallerImpl;
42 import com.sun.xml.internal.ws.developer.JAXBContextFactory;
43 import com.sun.xml.internal.ws.spi.db.BindingContext;
44 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
45 import com.sun.xml.internal.ws.spi.db.BindingInfo;
46 import com.sun.xml.internal.ws.spi.db.DatabindingException;
47 import com.sun.xml.internal.ws.spi.db.TypeInfo;
48 import com.sun.xml.internal.ws.spi.db.WrapperComposite;
49 import java.util.Arrays;
51 /**
52 * JAXBRIContextFactory
53 *
54 * @author shih-chang.chen@oracle.com
55 */
56 public class JAXBRIContextFactory extends BindingContextFactory {
58 @Override
59 public BindingContext newContext(JAXBContext context) {
60 return new JAXBRIContextWrapper((JAXBRIContext) context, null);
61 }
63 @Override
64 public BindingContext newContext(BindingInfo bi) {
65 Class[] classes = bi.contentClasses().toArray(new Class[bi.contentClasses().size()]);
66 for (int i = 0; i < classes.length; i++) {
67 if (WrapperComposite.class.equals(classes[i])) {
68 classes[i] = CompositeStructure.class;
69 }
70 }
71 Map<TypeInfo, TypeReference> typeInfoMappings = typeInfoMappings(bi.typeInfos());
72 Map<Class, Class> subclassReplacements = bi.subclassReplacements();
73 String defaultNamespaceRemap = bi.getDefaultNamespace();
74 Boolean c14nSupport = (Boolean) bi.properties().get("c14nSupport");
75 RuntimeAnnotationReader ar = (RuntimeAnnotationReader) bi.properties().get("com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader");
76 JAXBContextFactory jaxbContextFactory = (JAXBContextFactory) bi.properties().get(JAXBContextFactory.class.getName());
77 try {
78 JAXBRIContext context = (jaxbContextFactory != null)
79 ? jaxbContextFactory.createJAXBContext(
80 bi.getSEIModel(),
81 toList(classes),
82 toList(typeInfoMappings.values()))
83 : ContextFactory.createContext(
84 classes, typeInfoMappings.values(),
85 subclassReplacements, defaultNamespaceRemap,
86 (c14nSupport != null) ? c14nSupport : false,
87 ar, false, false, false);
88 return new JAXBRIContextWrapper(context, typeInfoMappings);
89 } catch (Exception e) {
90 throw new DatabindingException(e);
91 }
92 }
94 private <T> List<T> toList(T[] a) {
95 List<T> l = new ArrayList<T>();
96 l.addAll(Arrays.asList(a));
97 return l;
98 }
100 private <T> List<T> toList(Collection<T> col) {
101 if (col instanceof List) {
102 return (List<T>) col;
103 }
104 List<T> l = new ArrayList<T>();
105 l.addAll(col);
106 return l;
107 }
109 private Map<TypeInfo, TypeReference> typeInfoMappings(Collection<TypeInfo> typeInfos) {
110 Map<TypeInfo, TypeReference> map = new java.util.HashMap<TypeInfo, TypeReference>();
111 for (TypeInfo ti : typeInfos) {
112 Type type = WrapperComposite.class.equals(ti.type) ? CompositeStructure.class : ti.type;
113 TypeReference tr = new TypeReference(ti.tagName, type, ti.annotations);
114 map.put(ti, tr);
115 }
116 return map;
117 }
119 @Override
120 protected BindingContext getContext(Marshaller m) {
121 return newContext(((MarshallerImpl) m).getContext());
122 }
124 @Override
125 protected boolean isFor(String str) {
126 return (str.equals("glassfish.jaxb")
127 || str.equals(this.getClass().getName())
128 || str.equals("com.sun.xml.internal.bind.v2.runtime"));
129 }
130 }