001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TransformationImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.xslt;
030:
031: import com.sun.jbi.engine.xslt.framework.WorkManager;
032: import com.sun.jbi.engine.xslt.util.StringTranslator;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037:
038: import java.util.EmptyStackException;
039: import java.util.Properties;
040: import java.util.Stack;
041: import java.util.logging.Logger;
042:
043: import javax.xml.transform.OutputKeys;
044: import javax.xml.transform.Result;
045: import javax.xml.transform.Source;
046: import javax.xml.transform.Templates;
047: import javax.xml.transform.Transformer;
048: import javax.xml.transform.TransformerFactory;
049: import javax.xml.transform.stream.StreamSource;
050:
051: /**
052: * This class provides interfaces to process the messages received by the
053: * QuoteService implementations. It also contains interfaces to create the
054: * outbound response and fault message.
055: *
056: * @author Sun Microsystems, Inc.
057: */
058: public final class TransformationImpl implements
059: TransformationProcessor, TEResources {
060: /**
061: *
062: */
063: private static StringTranslator sTranslator = new StringTranslator(
064: "com.sun.jbi.engine.xslt", null);
065:
066: /**
067: *
068: */
069: private static Stack sPool = new Stack();
070:
071: /**
072: *
073: */
074: private static TemplateRegistry sTemplateRegistry = TemplateRegistry
075: .getRegistry();
076:
077: /**
078: *
079: */
080: private static Logger sLogger = TransformationEngineContext
081: .getInstance().getLogger("");
082:
083: /**
084: *
085: */
086: private Properties mCache = null;
087:
088: /**
089: *
090: */
091: private String mTemplateKey = null;
092:
093: /**
094: *
095: */
096: private WorkManager mWorkManager = null;
097:
098: /**
099: *
100: */
101: private int mCount = 0;
102:
103: /**
104: * Creates a new instance of TransformationImpl.
105: *
106: * @param count number of Factory instances
107: */
108: public TransformationImpl(int count) {
109: init(count);
110: }
111:
112: /**
113: * DOCUMENT ME!
114: *
115: * @return NOT YET DOCUMENTED
116: */
117: public static synchronized TransformerFactory getTransformerFactory() {
118: TransformerFactory tFactory = null;
119:
120: try {
121: tFactory = (TransformerFactory) sPool.pop();
122: } catch (EmptyStackException ex) {
123: sLogger.warning(sTranslator
124: .getString(TEResources.NO_TRANSFORMATIONIMPL));
125: }
126:
127: return tFactory;
128: }
129:
130: /**
131: * DOCUMENT ME!
132: *
133: * @param tfactory NOT YET DOCUMENTED
134: */
135: public static synchronized void putTransformerFactory(
136: TransformerFactory tfactory) {
137: Object obj = sPool.push(tfactory);
138:
139: if (obj == null) {
140: sLogger
141: .warning(sTranslator
142: .getString(TEResources.PUSHING_TRANSFORMATIONIMPL_FAILED));
143: }
144: }
145:
146: /**
147: * DOCUMENT ME!
148: *
149: * @param template NOT YET DOCUMENTED
150: */
151: public void setTemplateName(String template) {
152: mTemplateKey = template;
153: }
154:
155: /**
156: * DOCUMENT ME!
157: *
158: * @return NOT YET DOCUMENTED
159: */
160: public String getTemplateName() {
161: return mTemplateKey;
162: }
163:
164: /**
165: * Generates a fault message.
166: *
167: * @param document - a DOM object which will contain the fault message.
168: * @param input - the stock symbol name.
169: * @param message - error message.
170: */
171: public void createFaultMessage(Document document, Node input,
172: String message) {
173: sLogger.info(sTranslator
174: .getString(TEResources.CREATEFAULTMESSAGE_START));
175:
176: Element transformElement = document.createElement(sTranslator
177: .getString(TEResources.TRANSFORM));
178: Element faultElement = document.createElement(sTranslator
179: .getString(TEResources.FAULT));
180: Element messageElement = document.createElement(sTranslator
181: .getString(TEResources.MESSAGE));
182: messageElement.appendChild(document.createTextNode(message));
183: faultElement.appendChild(messageElement);
184: transformElement.appendChild(faultElement);
185: document.appendChild(transformElement);
186: }
187:
188: /**
189: * Generates a response message.
190: *
191: * @param document - a DOM object which will contain the response message.
192: * @param response - the stock symbol name.
193: *
194: * @throws Exception exception
195: */
196: public void createResponseMesage(Document document, Node response)
197: throws Exception {
198: Element transformElement = document.createElement(sTranslator
199: .getString(TEResources.TRANSFORM));
200: Element responseElement = document.createElement(sTranslator
201: .getString(TEResources.RESPONSE));
202: responseElement.appendChild(response);
203: transformElement.appendChild(responseElement);
204: document.appendChild(transformElement);
205: }
206:
207: /**
208: * Does transformation on the input DOM object using the stylesheet.
209: *
210: * @param input - a DOM object containing the message request.
211: * @param xsltfile - XSLT Tranformation stylesheet
212: * @param result - Result of the XSLT transformation
213: *
214: * @return the symbol if the message conforms to the format; otherwise
215: * null.
216: *
217: * @throws Exception exception
218: */
219: public boolean doTransform(Source input, String xsltfile,
220: Result result) throws Exception {
221: boolean status = false;
222: TransformerFactory tFactory = null;
223: Transformer transformer = null;
224:
225: //Check template registry for the xsltfile
226: Templates tpl = (Templates) sTemplateRegistry.get(mTemplateKey);
227: int mSomeNumber = 19;
228:
229: try {
230: if (tpl != null) {
231: transformer = tpl.newTransformer();
232: } else {
233: try {
234: int attempt = 0;
235:
236: while ((tFactory == null)
237: && (attempt < mSomeNumber)) {
238: tFactory = getTransformerFactory();
239:
240: if (tFactory == null) {
241: sLogger
242: .severe(sTranslator
243: .getString(TEResources.CACHED_TRANSFORMERFACTORY_NOT_PRESENT));
244: attempt++;
245:
246: // wait for some time
247: }
248: }
249:
250: // Temproary fix
251: if (tFactory == null) {
252: tFactory = TransformerFactory.newInstance();
253: }
254:
255: if (xsltfile != null) {
256: StreamSource stylesheet = new StreamSource(
257: xsltfile);
258: transformer = tFactory
259: .newTransformer(stylesheet);
260: transformer.setOutputProperty(
261: OutputKeys.OMIT_XML_DECLARATION, "yes");
262: }
263: } finally {
264: //Returning the tFactory to pool
265: if (sPool.size() < mCount) {
266: putTransformerFactory(tFactory);
267: }
268: }
269:
270: TemplateCommand command = new TemplateCommand(
271: mTemplateKey, new StreamSource(xsltfile));
272:
273: while (!mWorkManager.processCommand(command)) {
274: //block till template is done
275: }
276: }
277:
278: transformer.transform(input, result);
279: status = true;
280: } catch (Exception ex) {
281: sLogger.severe(sTranslator
282: .getString(TEResources.TRANSFORMATION_FAILED));
283: throw ex;
284: }
285:
286: return status;
287: }
288:
289: /**
290: * Does transformation on the input DOM object using the stylesheet.
291: *
292: * @param input - a DOM object containing the message request.
293: * @param result - Result of Transformation
294: *
295: * @return the symbol if the message conforms to the format; otherwise
296: * null.
297: *
298: * @throws Exception exception
299: */
300: public boolean transform(Source input, Result result)
301: throws Exception {
302: return doTransform(input, "/temp.xsl", result);
303: }
304:
305: /**
306: * Initializes the instance.
307: *
308: * @param num - number of Factory instances
309: */
310: private void init(int num) {
311: mWorkManager = WorkManager.getWorkManager(sTranslator
312: .getString(TEResources.XSLT));
313: mCount = num;
314:
315: /* Commenting out the following as JAXP will be able to detect
316: the provider for the underlying platform
317:
318: String key = "javax.xml.transform.TransformerFactory";
319:
320: //String value = "org.apache.xalan.xsltc.trax.SmartTransformerFactoryImpl";
321: //String value = "org.apache.xalan.processor.TransformerFactoryImpl";
322: String value = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
323: mCache = System.getProperties();
324: mCache.put(key, value);
325: System.setProperties(mCache); */
326:
327: for (int i = 0; i < mCount; i++) {
328: TransformerFactory tFactory = TransformerFactory
329: .newInstance();
330: putTransformerFactory(tFactory);
331: }
332: }
333: }
|