summaryrefslogtreecommitdiffstats
path: root/env/src/main/java/org/onap/aaf/inno/env/jaxb/JAXBmar.java
blob: d07c2463a4f19c0b0c25efe95b25677f1adf31d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
/**
 * JAXBumar.java
 *
 * Created on: Apr 10, 2009
 * Created by: 
 *
 * Revamped to do away with ThreadLocal 5/27/2011,
 *
 * (c) 2009 SBC Knowledge Ventures, L.P. All rights reserved.
 ******************************************************************* 
 * RESTRICTED - PROPRIETARY INFORMATION The Information contained 
 * herein is for use only by authorized employees of AT&T Services, 
 * Inc., and authorized Affiliates of AT&T Services, Inc., and is 
 * not for general distribution within or outside the respective 
 * companies. 
 *******************************************************************
 */
package org.onap.aaf.inno.env.jaxb;

import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

import org.onap.aaf.inno.env.APIException;
import org.onap.aaf.inno.env.LogTarget;
import org.onap.aaf.inno.env.util.Pool;
import org.onap.aaf.inno.env.util.Pool.Pooled;

/**
 * JAXBmar classes are inexpensive for going in and out of scope
 * and have been made thread safe via Pooling

 *
 */
public class JAXBmar {
	// Need to store off possible JAXBContexts based on Class, which will be stored in Creator
	private static Map<Class<?>[],Pool<PMarshaller>> pools = new HashMap<Class<?>[], Pool<PMarshaller>>();

	// Handle Marshaller class setting of properties only when needed
	private class PMarshaller {
		private Marshaller m;
		private boolean p;
		private boolean f;
		
		public PMarshaller(Marshaller marshaller) throws JAXBException {
			m = marshaller;
    		m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = false);
    		m.setProperty(Marshaller.JAXB_FRAGMENT, f = false);
		}
		
		public Marshaller get(boolean pretty, boolean fragment) throws JAXBException {
			if(pretty != p) {
	    		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = pretty);
			}
			if(fragment != f) {
	    		m.setProperty(Marshaller.JAXB_FRAGMENT, f = fragment);
			}
			return m;
		}
	}
	
	private class Creator implements Pool.Creator<PMarshaller> {
		private JAXBContext jc;
		private String name;
		public Creator(Class<?>[] classes) throws JAXBException {
			jc = JAXBContext.newInstance(classes);
			name = "JAXBmar: " + classes[0].getName();
		}
		
		// @Override
		public PMarshaller create() throws APIException {
			try {
				return new PMarshaller(jc.createMarshaller());
			} catch (JAXBException e) {
				throw new APIException(e);
			}
		}

		public String toString() {
			return name;
		}

		// @Override
		public void reuse(PMarshaller pm) {
			// Nothing to do
		}
		
		// @Override
		public void destroy(PMarshaller pm) {
			// Nothing to do
		}

		// @Override
		public boolean isValid(PMarshaller t) {
			return true; 
		}
	}

	//TODO isn't UTF-8 a standard string somewhere for encoding?
	private boolean fragment= false;
	private boolean pretty=false;
	private QName qname;
	
	private Pool<PMarshaller> mpool; // specific Pool associated with constructed Classes
	private Class<?> cls;
	
	private Pool<PMarshaller> getPool(Class<?> ... classes) throws JAXBException {
		Pool<PMarshaller> mp;
		synchronized(pools) {
			mp = pools.get(classes);
			if(mp==null) {
				pools.put(classes,mp = new Pool<PMarshaller>(new Creator(classes)));
			}
		}		
		return mp;
	}
	
	public JAXBmar(Class<?>... classes) throws JAXBException {
		cls = classes[0];
		mpool = getPool(classes);
		qname = null;
	}

	public JAXBmar(QName theQname, Class<?>... classes) throws JAXBException {
		cls = classes[0];
		mpool = getPool(classes);
		qname = theQname;
	}

	@SuppressWarnings("unchecked")
	public<O> O marshal(LogTarget lt,O o, Writer writer, boolean ... options) throws JAXBException, APIException {
		boolean pretty, fragment;
		pretty = options.length>0?options[0]:this.pretty;
		fragment = options.length>1?options[1]:this.fragment;
		Pooled<PMarshaller> m = mpool.get(lt);
		try {
			if(qname==null) {
				m.content.get(pretty,fragment).marshal(o, writer);
			} else {
				m.content.get(pretty,fragment).marshal(
					new JAXBElement<O>(qname, (Class<O>)cls, o ),
					writer);
			}
			return o;
		} finally {
			m.done();
		}
	}

	@SuppressWarnings("unchecked")
	public<O> O marshal(LogTarget lt, O o, OutputStream os, boolean ... options) throws JAXBException, APIException {
		boolean pretty, fragment;
		pretty = options.length>0?options[0]:this.pretty;
		fragment = options.length>1?options[1]:this.fragment;
		Pooled<PMarshaller> m = mpool.get(lt);
		try {
			if(qname==null) {
				m.content.get(pretty,fragment).marshal(o, os);
			} else {
				m.content.get(pretty,fragment).marshal(
					new JAXBElement<O>(qname, (Class<O>)cls, o ),os);
			}
			return o;
		} finally {
			m.done();
		}
	}
	
	public<O> O marshal(LogTarget lt, O o, Writer writer, Class<O> clss) throws JAXBException, APIException {
		Pooled<PMarshaller> m = mpool.get(lt);
		try {
			if(qname==null) {
				m.content.get(pretty,fragment).marshal(o, writer);
			} else {
				m.content.get(pretty,fragment).marshal(
					new JAXBElement<O>(qname, clss, o),writer);
			}
			return o;
		} finally {
			m.done();
		}
			
	}

	public<O> O marshal(LogTarget lt, O o, OutputStream os, Class<O> clss) throws JAXBException, APIException {
		Pooled<PMarshaller> m = mpool.get(lt);
		try {
			if(qname==null) { 
				m.content.get(pretty,fragment).marshal(o, os);
			} else {
				m.content.get(pretty,fragment).marshal(
					new JAXBElement<O>(qname, clss, o ),os);
			}
			return o;
		} finally {
			m.done();
		}
	}

	/**
	 * @return
	 */
	public Class<?> getMarshalClass() {
		return cls;
	}

	public<O> String stringify(LogTarget lt, O o) throws JAXBException, APIException {
		StringWriter sw = new StringWriter();
		marshal(lt,o,sw);
		return sw.toString();
	}

	public JAXBmar pretty(boolean pretty) {
		this.pretty = pretty;
		return this;
	}
	
	public JAXBmar asFragment(boolean fragment) {
		this.fragment = fragment;
		return this;
	}
}