001: /**
002: * $Id: ConsumerObjectFactory.java,v 1.2 2005/04/18 12:24:04 pa157442 Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wsrp.consumer.producermanager;
014:
015: import java.util.List;
016: import java.util.ArrayList;
017: import java.util.Iterator;
018: import java.util.Map;
019: import java.util.HashMap;
020:
021: import java.io.StringReader;
022: import java.io.StringWriter;
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.InputStream;
026: import java.io.UnsupportedEncodingException;
027:
028: import javax.xml.bind.JAXBContext;
029: import javax.xml.bind.JAXBException;
030: import javax.xml.bind.Marshaller;
031: import javax.xml.bind.Unmarshaller;
032: import javax.xml.transform.stream.StreamSource;
033:
034: import com.sun.portal.wsrp.common.jaxb.consumer.ObjectFactory;
035: import com.sun.portal.wsrp.common.jaxb.consumer.MultiValueMap;
036: import com.sun.portal.wsrp.common.jaxb.consumer.MultiValuePairType;
037:
038: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
039:
040: /**
041: * utility class for marshalling and unmarshalling jaxb objects.
042: */
043: public class ConsumerObjectFactory {
044:
045: //
046: // static instance
047: //
048: protected static ConsumerObjectFactory factory = null;
049: //
050: // jaxb context
051: //
052: private JAXBContext jc = null;
053: private ObjectFactory jfactory = null;
054:
055: public static ConsumerObjectFactory getInstance()
056: throws WSRPConsumerException {
057:
058: //
059: // perform double-checked locking
060: //
061: if (factory == null) {
062: synchronized (ConsumerObjectFactory.class) {
063: if (factory == null) {
064: factory = new ConsumerObjectFactory();
065: }
066: }
067: }
068: return factory;
069: }
070:
071: private ConsumerObjectFactory() throws WSRPConsumerException {
072: init();
073: }
074:
075: public void init() throws WSRPConsumerException {
076: //
077: // initialize jaxb
078: //
079: try {
080: jc = JAXBContext.newInstance(
081: "com.sun.portal.wsrp.common.jaxb.consumer", this
082: .getClass().getClassLoader());
083: jfactory = new ObjectFactory();
084: } catch (JAXBException jbe) {
085: throw new WSRPConsumerException(
086: "ConsumerObjectFactory.init(): failed to initialize JAXB components.",
087: jbe);
088: }
089: }
090:
091: public ObjectFactory getObjectFactory() {
092: return jfactory;
093: }
094:
095: public Map getMap(String mvXML) throws WSRPConsumerException {
096: MultiValueMap mvMap = getMultiValueMap(mvXML);
097: return getMap(mvMap);
098: }
099:
100: public String getMultiValueMapXML(Map map)
101: throws WSRPConsumerException {
102:
103: return getMultiValueMapXML(map, false);
104: }
105:
106: public String getMultiValueMapXML(Map map, boolean isFormatted)
107: throws WSRPConsumerException {
108:
109: MultiValueMap mvMap = getMultiValueMap(map);
110: String xml = null;
111: if (mvMap != null) {
112: StringWriter writer = new StringWriter();
113: try {
114: Marshaller marshaller = jc.createMarshaller();
115: if (isFormatted) {
116: marshaller.setProperty(
117: Marshaller.JAXB_FORMATTED_OUTPUT,
118: Boolean.TRUE);
119: }
120: marshaller.marshal(mvMap, writer);
121: } catch (JAXBException jbe) {
122: throw new WSRPConsumerException(
123: "ConsumerObjectFactory.getMultiValueMapXML(): failed to marshall MultiValueMap mvMap="
124: + mvMap, jbe);
125: }
126: xml = writer.toString();
127: }
128:
129: return xml;
130: }
131:
132: public MultiValueMap getMultiValueMap(String mvXML)
133: throws WSRPConsumerException {
134:
135: MultiValueMap mvMap = null;
136: if (mvXML != null && mvXML.length() > 0) {
137: try {
138: Unmarshaller unmarshaller = jc.createUnmarshaller();
139: mvMap = (MultiValueMap) unmarshaller
140: .unmarshal(new StreamSource(new StringReader(
141: mvXML)));
142: } catch (JAXBException jbe) {
143: throw new WSRPConsumerException(
144: "ConsumerObjectFactory.getMultiValueMap(): failed to unmarshall multiValueMap data. xml="
145: + mvXML, jbe);
146: }
147: }
148:
149: return mvMap;
150: }
151:
152: public Map getMap(MultiValueMap mvMap) throws WSRPConsumerException {
153:
154: Map map = null;
155: if (mvMap != null) {
156: List values = mvMap.getValues();
157: if (values != null && values.size() > 0) {
158: map = new HashMap();
159: for (Iterator i = values.iterator(); i.hasNext();) {
160: MultiValuePairType pair = (MultiValuePairType) i
161: .next();
162: map.put(pair.getKey(), pair.getValues());
163: }
164: }
165: }
166:
167: return map;
168: }
169:
170: public MultiValueMap getMultiValueMap(Map map)
171: throws WSRPConsumerException {
172:
173: MultiValueMap mvMap = null;
174: if (map != null && map.size() > 0) {
175: try {
176: mvMap = jfactory.createMultiValueMap();
177: } catch (JAXBException jbe) {
178: throw new WSRPConsumerException(
179: "ConsumerObjectFactory.getMultiValueMap(): failed to create JAXB object.",
180: jbe);
181: }
182:
183: List values = new ArrayList();
184: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
185: String key = (String) i.next();
186: MultiValuePairType pair = null;
187: try {
188: pair = jfactory.createMultiValuePairType();
189: } catch (JAXBException jbe) {
190: throw new WSRPConsumerException(
191: "ConsumerObjectFactory.getMultiValueMap(): failed to create JAXB object.",
192: jbe);
193: }
194: pair.setKey(key);
195: pair.getValues().addAll((List) map.get(key));
196: values.add(pair);
197: }
198: mvMap.getValues().addAll(values);
199: }
200:
201: return mvMap;
202: }
203: }
|