1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.oracle.jms.transformers; |
12 | |
|
13 | |
import org.mule.config.i18n.MessageFactory; |
14 | |
import org.mule.providers.jms.JmsConnector; |
15 | |
import org.mule.providers.oracle.jms.OracleJmsConnector; |
16 | |
import org.mule.transformers.AbstractEventAwareTransformer; |
17 | |
import org.mule.umo.UMOEventContext; |
18 | |
import org.mule.umo.transformer.TransformerException; |
19 | |
import org.mule.util.StringMessageUtils; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.Writer; |
23 | |
import java.sql.SQLException; |
24 | |
|
25 | |
import javax.jms.JMSException; |
26 | |
import javax.jms.Session; |
27 | |
|
28 | |
import oracle.jms.AQjmsSession; |
29 | |
import oracle.jms.AdtMessage; |
30 | |
import oracle.sql.CLOB; |
31 | |
import oracle.xdb.XMLType; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class StringToXMLMessage extends AbstractEventAwareTransformer |
42 | |
{ |
43 | |
public StringToXMLMessage() |
44 | |
{ |
45 | 0 | super(); |
46 | 0 | registerSourceType(String.class); |
47 | 0 | registerSourceType(byte[].class); |
48 | 0 | setReturnClass(AdtMessage.class); |
49 | 0 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException |
56 | |
{ |
57 | |
Session session; |
58 | |
AdtMessage message; |
59 | |
XMLType xmltype; |
60 | |
|
61 | |
try |
62 | |
{ |
63 | |
|
64 | 0 | if (endpoint.getTransactionConfig().isTransacted() == false) |
65 | |
{ |
66 | |
|
67 | |
|
68 | |
|
69 | 0 | throw new TransformerException(MessageFactory.createStaticMessage("This transformer may only be used with a transacted endpoint. Refer to http://mule.codehaus.org/display/MULE/Transaction+Management for more information."), this); |
70 | |
} |
71 | 0 | session = ((JmsConnector) endpoint.getConnector()).getSessionFromTransaction(); |
72 | 0 | if (session == null) { |
73 | 0 | throw new TransformerException(MessageFactory.createStaticMessage("No JMS session associated with this endpoint."), this); |
74 | |
} |
75 | 0 | if ((session instanceof AQjmsSession) == false) { |
76 | 0 | throw new TransformerException(MessageFactory.createStaticMessage("Endpoint must be an OracleAQ session."), this); |
77 | |
} |
78 | |
|
79 | |
|
80 | |
String xml; |
81 | 0 | if (src instanceof byte[]) { |
82 | 0 | xml = new String((byte[]) src, encoding); |
83 | |
} |
84 | 0 | else if (src instanceof String) { |
85 | 0 | xml = (String) src; |
86 | |
} |
87 | 0 | else throw new TransformerException(MessageFactory.createStaticMessage("Object to transform is not one of the supported types for this transformer."), this); |
88 | |
|
89 | 0 | logger.debug("Creating an Oracle XMLType based on the following XML:\n" + StringMessageUtils.truncate(xml, 200, false)); |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | 0 | CLOB clob = CLOB.createTemporary(((AQjmsSession) session).getDBConnection(), true, CLOB.DURATION_SESSION); |
96 | |
try { |
97 | 0 | Writer clobStream = clob.getCharacterOutputStream(); |
98 | |
try { |
99 | 0 | clobStream.write(xml); |
100 | |
} finally { |
101 | 0 | clobStream.close(); |
102 | 0 | } |
103 | 0 | xmltype = XMLType.createXML(((AQjmsSession) session).getDBConnection(), clob); |
104 | |
|
105 | |
|
106 | 0 | message = ((AQjmsSession) session).createAdtMessage(); |
107 | 0 | message.setAdtPayload(xmltype); |
108 | 0 | return message; |
109 | 0 | } finally { |
110 | |
|
111 | |
|
112 | |
} |
113 | |
} |
114 | 0 | catch (JMSException e) { throw new TransformerException(this, e); } |
115 | 0 | catch (SQLException e) { throw new TransformerException(this, e); } |
116 | 0 | catch (IOException e) { throw new TransformerException(this, e); } |
117 | |
} |
118 | |
} |