Source Code Cross Referenced for ServiceExecutor.java in  » ESB » open-esb » com » sun » jbi » engine » xslt » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » open esb » com.sun.jbi.engine.xslt 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * @(#)ServiceExecutor.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.Command;
032:        import com.sun.jbi.engine.xslt.util.ConfigData;
033:        import com.sun.jbi.engine.xslt.util.Service;
034:        import com.sun.jbi.engine.xslt.util.ServiceManager;
035:        import com.sun.jbi.engine.xslt.util.StringTranslator;
036:        import com.sun.jbi.engine.xslt.TransformationEngineContext;
037:
038:        import java.io.BufferedReader;
039:        import java.io.BufferedWriter;
040:        import java.io.File;
041:        import java.io.FileInputStream;
042:        import java.io.FileOutputStream;
043:        import java.io.FileReader;
044:        import java.io.FileWriter;
045:        import java.io.InputStreamReader;
046:        import java.io.PrintWriter;
047:
048:        import java.net.URI;
049:
050:        import java.util.Random;
051:        import java.util.StringTokenizer;
052:        import java.util.Vector;
053:        import java.util.logging.Logger;
054:
055:        import javax.jbi.messaging.DeliveryChannel;
056:        import javax.jbi.messaging.InOut;
057:        import javax.jbi.messaging.MessageExchange;
058:        import javax.jbi.messaging.MessagingException;
059:        import javax.jbi.messaging.NormalizedMessage;
060:
061:        import javax.xml.transform.Source;
062:        import javax.xml.transform.Transformer;
063:        import javax.xml.transform.TransformerFactory;
064:        import javax.xml.transform.stream.StreamResult;
065:        import javax.xml.transform.stream.StreamSource;
066:
067:        import javax.wsdl.Definition;
068:        import com.sun.jbi.wsdl11wrapper.WrapperParser;
069:        import com.sun.jbi.wsdl11wrapper.HelperFactory;
070:        import com.sun.jbi.wsdl11wrapper.util.WrapperUtil;
071:
072:        import com.sun.jbi.wsdl11wrapper.WrapperBuilder;
073:        import com.sun.jbi.wsdl11wrapper.Wsdl11WrapperHelper;
074:        import com.sun.jbi.wsdl11wrapper.Wsdl11WrapperHelperException;
075:        import org.w3c.dom.Node;
076:        import org.w3c.dom.Document;
077:        import org.w3c.dom.Element;
078:        import org.w3c.dom.NodeList;
079:        import org.w3c.dom.NamedNodeMap;
080:        import javax.xml.transform.dom.DOMResult;
081:        import javax.xml.transform.dom.DOMSource;
082:        import javax.jbi.servicedesc.ServiceEndpoint;
083:        import java.io.StringWriter;
084:        import javax.wsdl.Port;
085:        import javax.wsdl.Part;
086:        import javax.wsdl.Message;
087:        import javax.wsdl.Operation;
088:        import javax.wsdl.extensions.*;
089:        import javax.wsdl.xml.*;
090:
091:        import javax.xml.namespace.QName;
092:
093:        import java.util.List;
094:        import java.util.Iterator;
095:        import java.util.Map;
096:
097:        import javax.xml.parsers.DocumentBuilderFactory;
098:        import javax.xml.parsers.DocumentBuilder;
099:
100:        /**
101:         * This command contains the request response implementation.
102:         *
103:         * @author Sun Microsystems Inc.
104:         */
105:        public class ServiceExecutor implements  Command, TEResources {
106:            /**
107:             * In Out MEP.
108:             */
109:            public static final String IN_OUT = "http://www.w3.org/2004/08/wsdl/in-out";
110:
111:            /**
112:             * Out In MEP.
113:             */
114:            public static final String OUT_IN = "http://www.w3.org/2004/08/wsdl/out-in";
115:
116:            /**
117:             *    
118:             */
119:            private static Random sRand = new Random();
120:
121:            /**
122:             * Delivery Channel
123:             */
124:            private DeliveryChannel mChannel = null;
125:
126:            /**
127:             * This is logger instance used for logging information.
128:             */
129:            private Logger mLogger = null;
130:
131:            /**
132:             * MessageExchange containg the input request
133:             */
134:            private MessageExchange mExchange = null;
135:
136:            /**
137:             *    
138:             */
139:            private ServiceManager mServiceManager;
140:
141:            /**
142:             *    
143:             */
144:            private StringBuffer mOutSuffix = null;
145:
146:            /**
147:             *    
148:             */
149:            private StringTranslator mTranslator = null;
150:
151:            /**
152:             * Construtor for creating RequestResponseCommand Object.
153:             *
154:             * @param channel for replying back to sender
155:             * @param exchange received by the sender.
156:             */
157:            public ServiceExecutor(DeliveryChannel channel,
158:                    MessageExchange exchange) {
159:                mTranslator = new StringTranslator("com.sun.jbi.engine.xslt",
160:                        this .getClass().getClassLoader());
161:
162:                // Do any command specific init.
163:                mLogger = TransformationEngineContext.getInstance().getLogger(
164:                        "");
165:                mChannel = channel;
166:                mExchange = exchange;
167:                mServiceManager = ServiceManager.getInstance();
168:                mOutSuffix = new StringBuffer();
169:                mOutSuffix.append("</outputxml>");
170:            }
171:
172:            /**
173:             * Converts the non-xml data to xml.
174:             *
175:             * @param service Service
176:             * @param outputfile Output file
177:             * @param inputFile Input file
178:             *
179:             * @return boolean
180:             */
181:            public boolean convertToXml(Service service, String outputfile,
182:                    String inputFile) {
183:                boolean bSUCCESS = true;
184:                boolean bFAILURE = false;
185:
186:                try {
187:                    String tokens = service.getColumnSeparator();
188:                    File file = new File(outputfile);
189:                    FileOutputStream fos = new FileOutputStream(file);
190:                    PrintWriter pw = new PrintWriter(fos);
191:                    String tag1 = "<"
192:                            + mTranslator.getString(TEResources.INPUTXML) + ">";
193:                    String tag2 = "<"
194:                            + mTranslator.getString(TEResources.RECORD) + ">";
195:                    String tag3 = "<" + "/"
196:                            + mTranslator.getString(TEResources.RECORD) + ">";
197:
198:                    // This has to change after WSDL support, hard coded INPUTXML for now
199:                    String tag4 = "<" + "/"
200:                            + mTranslator.getString(TEResources.INPUTXML) + ">";
201:                    BufferedReader reader = new BufferedReader(
202:                            new InputStreamReader(
203:                                    new FileInputStream(inputFile)));
204:                    String line = null;
205:                    Vector colnames = new Vector();
206:                    boolean usefirstrow = false;
207:                    boolean colrow = false;
208:                    int index = -1;
209:                    int lastIndex = -1;
210:                    int i = 0;
211:                    boolean starttagfound = false;
212:
213:                    while ((line = reader.readLine()) != null) {
214:                        i++;
215:                        index = -1;
216:                        lastIndex = -1;
217:
218:                        if (line.equals("")) {
219:                            continue;
220:                        }
221:
222:                        if (!starttagfound) {
223:                            index = line.indexOf(tag1);
224:
225:                            if (index >= 0) {
226:                                index = index + tag3.length();
227:                                starttagfound = true;
228:                                i = 0;
229:                                mLogger.finer("Start tag found");
230:                                pw.println(tag1);
231:                            } else {
232:                                mLogger
233:                                        .finer("looping till first tag is found");
234:
235:                                continue;
236:                            }
237:                        }
238:
239:                        lastIndex = line.indexOf(tag4);
240:
241:                        if (lastIndex >= 0) {
242:                            mLogger.finer("Endtag is found" + lastIndex);
243:                        }
244:
245:                        if ((lastIndex >= 0) && (index >= 0)) {
246:                            mLogger.finer("Start and End tag found");
247:                            line = line.substring(index, lastIndex);
248:                        } else if (index >= 0) {
249:                            line = line.substring(index, line.length());
250:                        } else if (lastIndex >= 0) {
251:                            line = line.substring(0, lastIndex);
252:                        } else {
253:                            mLogger.finer(" Read line has no start or end tag");
254:                        }
255:
256:                        StringTokenizer st = new StringTokenizer(line, tokens,
257:                                false);
258:                        int j = 0;
259:
260:                        if ((i == 0) && service.getFirstRowColHeaders()) {
261:                            mLogger.finer("Parsing first row for column names");
262:
263:                            while (st.hasMoreTokens()) {
264:                                colnames.add(st.nextToken());
265:                            }
266:
267:                            usefirstrow = true;
268:                            i++;
269:
270:                            continue;
271:                        } else if ((i == 0)
272:                                && (service.getColumnNames() != null)) {
273:                            mLogger
274:                                    .finer("Getting column names read from service.xml");
275:                            colnames = service.getColumnNames();
276:                            colrow = true;
277:                            i++;
278:                        } else {
279:                            ;
280:                        }
281:
282:                        pw.println("  ");
283:                        pw.print(tag2);
284:
285:                        if (usefirstrow || colrow) {
286:                            while (st.hasMoreTokens()) {
287:                                try {
288:                                    String colname = (String) colnames
289:                                            .elementAt(j);
290:
291:                                    if (colname != null) {
292:                                        pw.println("    ");
293:                                        pw.print("<");
294:                                        pw.print(colname);
295:                                        pw.print(">");
296:                                        pw.print(st.nextToken());
297:                                        pw.print("</");
298:                                        pw.print(colname);
299:                                        pw.print(">");
300:                                    }
301:                                } catch (ArrayIndexOutOfBoundsException ex) {
302:                                    mLogger
303:                                            .severe(mTranslator
304:                                                    .getString(TEResources.INVALID_INPUT));
305:
306:                                    break;
307:                                }
308:
309:                                j++;
310:                            }
311:                        } else {
312:                            while (st.hasMoreTokens()) {
313:                                j++;
314:
315:                                String columnTag = "<" + "column_" + j + ">";
316:                                String columnTagEnd = "</" + "column_" + j
317:                                        + ">";
318:                                pw.println("    ");
319:                                pw.print(columnTag);
320:                                pw.print(st.nextToken());
321:                                pw.print(columnTagEnd);
322:                            }
323:                        }
324:
325:                        pw.println("  ");
326:                        pw.print(tag3);
327:                    }
328:
329:                    if (lastIndex >= 0) {
330:                        pw.println("");
331:                        pw.print(tag4);
332:                    }
333:
334:                    pw.flush();
335:                    pw.close();
336:                    reader.close();
337:
338:                    return bSUCCESS;
339:                } catch (Exception ex) {
340:                    ex.printStackTrace();
341:
342:                    return bFAILURE;
343:                }
344:            }
345:
346:            /**
347:             * This is called by workmanager in a free thread. It extracts the symbol
348:             * from the normalized message, processes it and generates response or
349:             * fault depending upon the input message.
350:             */
351:            public void execute() {
352:                mLogger
353:                        .finer("##### SERVICEEXECUTERCOMMAND:EXECUTE() INVOKED BY WORKMANAGER* ########");
354:
355:                URI pattern = mExchange.getPattern();
356:                mLogger.finer(mTranslator
357:                        .getString(TEResources.PATTERN_EXCG_ID)
358:                        + mExchange.getExchangeId()
359:                        + mTranslator.getString(TEResources.IS) + pattern);
360:
361:                String temp = (pattern.toString()).trim();
362:
363:                if (mExchange instanceof  InOut) {
364:                    if ((mExchange.getStatus() != null)
365:                            && ((mExchange.getStatus()).toString()).trim()
366:                                    .equalsIgnoreCase(ConfigData.ACTIVE)) {
367:                        mLogger.finer(mTranslator
368:                                .getString(TEResources.RCVD_IN_OUT)
369:                                + mExchange.getExchangeId());
370:                        mLogger.finer(mTranslator
371:                                .getString(TEResources.RCVD_IN_OUT_MSG_EX)
372:                                + mExchange.getStatus());
373:                        processInOut((InOut) mExchange);
374:                    } else {
375:                        // Received response
376:                        mLogger
377:                                .finer(mTranslator
378:                                        .getString(TEResources.RCVD_IN_OUT)
379:                                        + mExchange.getExchangeId()
380:                                        + mTranslator
381:                                                .getString(TEResources.STATUS_IS)
382:                                        + mExchange.getStatus()
383:                                        + mTranslator
384:                                                .getString(TEResources.IGNORING_STATUS));
385:                    }
386:                }
387:
388:                else {
389:                    mLogger.severe(mTranslator
390:                            .getString(TEResources.RECEIVED_INVALID_PATTERN)
391:                            + mExchange.getExchangeId());
392:
393:                    return;
394:                }
395:
396:                mLogger.info(mTranslator
397:                        .getString(TEResources.SERVICEEXECUTOR_EXECUTE_END));
398:            }
399:
400:            /**
401:             * DOCUMENT ME!
402:             *
403:             * @param inout NOT YET DOCUMENTED
404:             */
405:            public void processInOut(InOut inout) {
406:                // This must be a request
407:                try {
408:                    String serviceName = inout.getEndpoint().getServiceName()
409:                            .toString();
410:                    serviceName = serviceName.trim();
411:                    mLogger.finer(mTranslator
412:                            .getString(TEResources.GETTING_SVC_BEAN)
413:                            + serviceName);
414:
415:                    if (serviceName == null) {
416:                        mLogger.severe(mTranslator
417:                                .getString(TEResources.SERVICENAME_NULL));
418:                        mLogger.finer(mTranslator
419:                                .getString(TEResources.NO_PROCESSING_DONE));
420:                        inout
421:                                .setError(new Exception(
422:                                        mTranslator
423:                                                .getString(TEResources.SERVICE_PROCESSING_NOT_AVAILABLE)));
424:
425:                        try {
426:                            mLogger.info("DEBUG : Service name "
427:                                    + inout.getEndpoint().getServiceName()
428:                                    + " Endpint "
429:                                    + inout.getEndpoint().getEndpointName()
430:                                    + " Service endpoint "
431:                                    + inout.getEndpoint().getServiceName()
432:                                    + " Operation " + inout.getOperation());
433:                            mChannel.send(inout);
434:                        } catch (MessagingException ex) {
435:                            ex.printStackTrace();
436:                        }
437:
438:                        return;
439:                    }
440:
441:                    // Get the Service from ServicerRegistry 
442:                    Service service = mServiceManager.getService(serviceName);
443:
444:                    if (service == null) {
445:                        // Error case should happen
446:                        mLogger.severe(mTranslator
447:                                .getString(TEResources.INVALID_REGISTRY_STATE));
448:                        mLogger.finer(mTranslator
449:                                .getString(TEResources.NO_PROCESSING_DONE));
450:                        inout
451:                                .setError(new Exception(
452:                                        mTranslator
453:                                                .getString(TEResources.SERVICE_NULL_NOT_PROCESSED)));
454:
455:                        try {
456:                            mLogger.info("DEBUG : Service name "
457:                                    + inout.getEndpoint().getServiceName()
458:                                    + " Endpint "
459:                                    + inout.getEndpoint().getEndpointName()
460:                                    + " Service endpoint "
461:                                    + inout.getEndpoint().getServiceName()
462:                                    + " Operation " + inout.getOperation());
463:                            mChannel.send(inout);
464:                        } catch (MessagingException ex) {
465:                            ex.printStackTrace();
466:                        }
467:
468:                        return;
469:                    }
470:
471:                    if (service.getStatus().equalsIgnoreCase(ConfigData.START)) {
472:                        mLogger.finer(mTranslator
473:                                .getString(TEResources.GOT_SERVICE)
474:                                + service);
475:
476:                        try {
477:                            //Should check return value of process (if false, set error
478:                            // on MessageExchange
479:                            process(serviceName, inout);
480:                        } catch (Exception ex) {
481:                            inout.setError(new Exception(ex.getMessage()));
482:                        }
483:                    } else {
484:                        mLogger.severe(mTranslator
485:                                .getString(TEResources.SERVICE_NOT_STARTED));
486:                        inout.setError(new Exception(mTranslator
487:                                .getString(TEResources.SERVICE_NOT_RUNNING)));
488:                    }
489:                } catch (Exception me) {
490:                    mLogger.severe(mTranslator
491:                            .getString(TEResources.COULD_NOT_SEND_MESSAGE)
492:                            + inout.getExchangeId()
493:                            + mTranslator.getString(TEResources.BECAUSE)
494:                            + me.getMessage());
495:                    inout.setError(new Exception(me.getMessage()));
496:                } finally {
497:                    mLogger
498:                            .finer(" ******** processInOut Finally called ********");
499:                    mLogger.finer(mTranslator
500:                            .getString(TEResources.SENDING_MESSAGE_EXCHANGE));
501:
502:                    try {
503:                        mLogger.finer("DEBUG : Service name "
504:                                + inout.getEndpoint().getServiceName()
505:                                + " Endpint "
506:                                + inout.getEndpoint().getEndpointName()
507:                                + " Service endpoint "
508:                                + inout.getEndpoint().getServiceName()
509:                                + " Operation " + inout.getOperation());
510:                        mChannel.send(inout);
511:                    } catch (MessagingException ex) {
512:                        mLogger
513:                                .severe(mTranslator
514:                                        .getString(TEResources.PROBLEM_SENDING_MESSAGE));
515:                        ex.printStackTrace();
516:                    }
517:                }
518:            }
519:
520:            /**
521:             * This method processes the Message Exchange
522:             *
523:             * @param serviceName Service name for which the transformation request has come
524:             * @param inout Message Exchange object
525:             *
526:             * @return boolean true/false
527:             *
528:             * @throws javax.jbi.messaging.MessagingException
529:             * @throws Exception exception
530:             */
531:            private boolean process(String serviceName, InOut inout)
532:                    throws javax.jbi.messaging.MessagingException, Exception {
533:                mLogger.info(mTranslator
534:                        .getString(TEResources.SERVICEEXECUTOR_START));
535:
536:                boolean status = false;
537:                String mparentdir = "";
538:                String mfilename = "";
539:                String minputfile = "";
540:                String mOtherFile = "";
541:
542:                Source source = null;
543:
544:                Service service = mServiceManager.getService(serviceName);
545:                NormalizedMessage inputMsg = inout.getInMessage();
546:                NormalizedMessage outputMsg = inout.createMessage();
547:
548:                // determine if the message corresponds to the service deployment type
549:                // If the service was deployed using WSDL 1.1, reject messages sent
550:                // in other form and vice versa.
551:                // In case the service was deployed using XML, TE should be able to handle
552:                // message in either format (WSDL 1.1 or 2.0)
553:                if (service.getDeploymentType().equalsIgnoreCase("wsdl_11")) {
554:                    // do WSDL 1.1 message exchange stuff
555:                    try {
556:                        source = handleWSDL11Message(inout, service);
557:                    } catch (Wsdl11WrapperHelperException wEx) {
558:                        throw new Exception(wEx.getMessage()
559:                                + " Incorrect incoming Message.");
560:                    }
561:                } else {
562:                    // this is either XML deployment or WSDL 2.0
563:                    source = inputMsg.getContent();
564:                }
565:
566:                if (source != null) {
567:                    mLogger.info("[BEGIN_SOURCE_DUMP]");
568:                    //dump(source);
569:                    mLogger.info("[END_SOURCE_DUMP]");
570:                    mLogger.fine("Recevied source of type: "
571:                            + source.getClass().getName());
572:                } else {
573:                    mLogger
574:                            .severe("No Source found for Transformation. ERROR.");
575:                    return false;
576:                }
577:
578:                String xsltfile = service.getStyleSheet();
579:                if (xsltfile == null) {
580:                    mLogger.severe(mTranslator
581:                            .getString(TEResources.STYLESHEET_NOT_FOUND));
582:                    inout.setError(new Exception(mTranslator
583:                            .getString(TEResources.STYLESHEET_NOT_FOUND)));
584:
585:                    return false;
586:                } else {
587:                    mLogger.finer(mTranslator
588:                            .getString(TEResources.STYLESHEET_FOUND)
589:                            + xsltfile);
590:                }
591:
592:                //tmp code
593:                long mnum = 0;
594:
595:                synchronized (this ) {
596:                    mnum = Math.abs(sRand.nextLong());
597:                }
598:
599:                try {
600:                    mparentdir = new File(xsltfile).getParent();
601:                } catch (Exception ex) {
602:                    mLogger.severe(mTranslator
603:                            .getString(TEResources.PARENT_DIR_FAILED));
604:                    // In case of problem, try using INSTALL_ROOT for the component
605:                    mparentdir = "";
606:                }
607:
608:                mLogger.finer(mTranslator.getString(TEResources.PARENT_DIR)
609:                        + mparentdir + "*");
610:
611:                // Writting orignal request to file 
612:                mfilename = mparentdir + File.separator
613:                        + new Long(mnum).toString();
614:                minputfile = mfilename
615:                        + mTranslator.getString(TEResources.INPUT_FILE);
616:
617:                // Code for converting non-xml data to xml
618:                String type = mTranslator.getString(TEResources.NON_XML);
619:
620:                if ((type != null)
621:                        && service.getType().equalsIgnoreCase(type.trim())) {
622:                    //Covert non-xml to xml
623:                    //Read from StreamSource and store into minputfile
624:                    storeSourceToFile((StreamSource) source, minputfile);
625:                    mOtherFile = mfilename
626:                            + mTranslator.getString(TEResources.OTHER);
627:                    convertToXml(service, mOtherFile, minputfile);
628:                    minputfile = mOtherFile;
629:                } else {
630:                    // Service deployment is XML
631:                    try {
632:                        TransformationImpl cprocessor = (TransformationImpl) TransformationProcessorFactory
633:                                .getImpl();
634:                        TransformerFactory tFactory = cprocessor
635:                                .getTransformerFactory();
636:
637:                        //Identical Copy Transformation. No Transformation Done actually. 
638:                        //Transformer writes
639:                        //the input XML as it is to the Result (file here)
640:                        Transformer copier = tFactory.newTransformer();
641:                        copier.transform(source, new StreamResult(minputfile));
642:                        cprocessor.putTransformerFactory(tFactory);
643:                    } catch (Exception ex) {
644:                        mLogger
645:                                .severe(mTranslator
646:                                        .getString(TEResources.WRITTING_INPUT_REQUEST_FAILED));
647:                        inout
648:                                .setError(new Exception(
649:                                        mTranslator
650:                                                .getString(TEResources.WRITTING_INPUT_REQUEST_FAILED)));
651:                        ex.printStackTrace();
652:
653:                        return false;
654:                    }
655:                }
656:
657:                source = new StreamSource(new FileInputStream(minputfile));
658:
659:                if (source instanceof  StreamSource) {
660:                    mLogger.finer(mTranslator
661:                            .getString(TEResources.REQ_STREAMSOURCE));
662:
663:                    String filename = "";
664:                    String inputfile = "";
665:                    String outfile = "";
666:
667:                    long num = 0;
668:
669:                    synchronized (this ) {
670:                        num = Math.abs(sRand.nextLong());
671:                    }
672:
673:                    // Writting orignal request to file 
674:                    filename = mparentdir + File.separator
675:                            + new Long(num).toString();
676:                    outfile = filename
677:                            + mTranslator.getString(TEResources.OUTPUT_FILE);
678:                    mLogger.finer(mTranslator
679:                            .getString(TEResources.GEN_OUT_FILE)
680:                            + outfile);
681:
682:                    StreamResult result = new StreamResult(new File(outfile));
683:                    try {
684:                        TransformationImpl processor = (TransformationImpl) TransformationProcessorFactory
685:                                .getImpl();
686:                        processor.setTemplateName(serviceName);
687:                        status = processor
688:                                .doTransform(source, xsltfile, result);
689:
690:                        // If the message was received in WSDL 1.1 format, wrap the
691:                        // transformed output in JBI Wrapper format.
692:                        if (service.getDeploymentType().equalsIgnoreCase(
693:                                "wsdl_11")) {
694:                            Document msgDoc = prepareWSDL11Response(inout,
695:                                    service, outfile);
696:                            outputMsg.setContent(new DOMSource(msgDoc
697:                                    .getDocumentElement()));
698:                        } else {
699:                            // this is either XML deployment or WSDL 2.0
700:                            outputMsg.setContent(new StreamSource(outfile));
701:                        }
702:
703:                        mLogger.info(mTranslator
704:                                .getString(TEResources.TRANSFORMATION_SUCCESS));
705:                    } catch (Exception ex) {
706:                        mLogger.severe(mTranslator
707:                                .getString(TEResources.TRANSFORMATION_FAILED));
708:                        ex.printStackTrace();
709:                        inout.setError(new Exception(ex.getMessage()));
710:
711:                        return false;
712:                    }
713:                } else {
714:
715:                    long num = 0;
716:
717:                    synchronized (this ) {
718:                        num = Math.abs(sRand.nextLong());
719:                    }
720:
721:                    // Writting orignal request to file 
722:                    String filename = mparentdir + File.separator
723:                            + new Long(num).toString();
724:                    String outfile = filename
725:                            + mTranslator.getString(TEResources.OUTPUT_FILE);
726:                    String outfile2 = filename
727:                            + mTranslator.getString(TEResources.OUT_FILE_2);
728:
729:                    StreamResult result = new StreamResult(outfile);
730:
731:                    try {
732:                        TransformationImpl processor = (TransformationImpl) TransformationProcessorFactory
733:                                .getImpl();
734:                        processor.setTemplateName(serviceName);
735:                        status = processor
736:                                .doTransform(source, xsltfile, result);
737:
738:                        BufferedReader reader = new BufferedReader(
739:                                new FileReader(outfile));
740:                        PrintWriter pw = new PrintWriter(new BufferedWriter(
741:                                new FileWriter(outfile2)));
742:                        String readobj = null;
743:
744:                        int itr = 1;
745:
746:                        while ((readobj = reader.readLine()) != null) {
747:                            if (itr == 1) {
748:                                String prolog = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
749:                                int length = prolog.length();
750:                                int index = readobj.indexOf(prolog);
751:
752:                                if (index >= 0) {
753:                                    String str = readobj.substring(index
754:                                            + length, readobj.length());
755:                                    pw.println(str);
756:                                } else {
757:                                    pw.println(readobj);
758:                                }
759:                            } else {
760:                                pw.println(readobj);
761:                            }
762:
763:                            itr++;
764:                        }
765:
766:                        pw.flush();
767:                        pw.close();
768:
769:                        reader.close();
770:                        outputMsg.setContent(new StreamSource(outfile2));
771:                        mLogger
772:                                .info(mTranslator
773:                                        .getString(TEResources.TRANSFORMATION_SUCCESS_OTHER));
774:                    } catch (Exception ex) {
775:                        mLogger.severe(mTranslator
776:                                .getString(TEResources.TRANSFORMATION_FAILED));
777:                        ex.printStackTrace();
778:                        inout.setError(new Exception(ex.getMessage()));
779:
780:                        return false;
781:                    }
782:                }
783:
784:                // Set the Out Message on Message Exchange
785:                inout.setOutMessage(outputMsg);
786:                mLogger.info(mTranslator
787:                        .getString(TEResources.SERVICEEXECUTOR_END));
788:
789:                return status;
790:            }
791:
792:            private void storeSourceToFile(StreamSource src, String fileName) {
793:                try {
794:                    if (src.getSystemId() != null) {
795:                        mLogger.finer("System ID = " + src.getSystemId());
796:                        URI fileUri = new URI(src.getSystemId());
797:                        BufferedReader re = new BufferedReader(new FileReader(
798:                                new File(fileUri)));
799:                        String line;
800:                        BufferedWriter bw = new BufferedWriter(new FileWriter(
801:                                new File(fileName)));
802:                        while ((line = re.readLine()) != null) {
803:                            mLogger.finer(line);
804:                            bw.write(line);
805:                            bw.newLine();
806:
807:                        }
808:                        bw.flush();
809:                        bw.close();
810:                    }
811:                    if (src.getInputStream() != null) {
812:                        mLogger.finer("Inputstream is not null.");
813:                    }
814:
815:                    if (src.getReader() != null) {
816:                        mLogger.finer("Reader is not null.");
817:                        BufferedReader reader = new BufferedReader(src
818:                                .getReader());
819:                        String line;
820:                        while ((line = reader.readLine()) != null) {
821:                            mLogger.finer(line);
822:                        }
823:                    }
824:                } catch (Exception e) {
825:                    e.printStackTrace();
826:                }
827:            }
828:
829:            /**
830:             * This method prepares the Wrapped response. This is done only if the 
831:             * deployment(service) is of type "wsdl_11".
832:             *
833:             * @param source Source object to print
834:             */
835:            private void dump(Source source) throws Exception {
836:                TransformerFactory tf = TransformerFactory.newInstance();
837:                Transformer t = tf.newTransformer();
838:                StringWriter sw = new StringWriter();
839:                StreamResult stdOut = new StreamResult(System.out);
840:
841:                t.transform(source, new StreamResult(sw));
842:                mLogger.info(sw.toString());
843:            }
844:
845:            /**
846:             * This method handles the Wrapped response. This is done only if the 
847:             * deployment(service) is of type "wsdl_11".  Using the WSDL 11 Wrapper Helper
848:             * object, the incoming message is "unwrapped" to get the input data.
849:             *
850:             * @param inout MessageExchange Object
851:             * @param service Service object 
852:             *
853:             * @return Source 
854:             */
855:            private Source handleWSDL11Message(InOut inout, Service service)
856:                    throws Wsdl11WrapperHelperException {
857:                DOMSource src = null;
858:
859:                //Creatre the Helper class that will do wrapping/unwrapping
860:                Wsdl11WrapperHelper mWrapHelper = createWrapperHelper(service);
861:
862:                NormalizedMessage inMsg = inout.getInMessage();
863:                ServiceEndpoint epref = service.getReference();
864:                Document unwrappedDoc = null;
865:
866:                unwrappedDoc = mWrapHelper.unwrapMessage(inMsg.getContent(),
867:                        epref.getServiceName(), epref.getEndpointName(), inout
868:                                .getOperation().getLocalPart(), true);
869:
870:                if (unwrappedDoc != null) {
871:                    src = new DOMSource(unwrappedDoc);
872:                }
873:
874:                return src;
875:            }
876:
877:            /**
878:             * This method creates WSDL11 wrapper helper object for given Service.
879:             *
880:             * @param service Service object 
881:             *
882:             * @return Wsdl11WrapperHelper
883:             */
884:            private Wsdl11WrapperHelper createWrapperHelper(Service serv) {
885:                // Get the WSDL Definition for this service/endpoint
886:                ServiceEndpoint epref = serv.getReference();
887:                Definition servDef = TransformationEngineContext.getInstance()
888:                        .getEndpointDef(epref);
889:                return new Wsdl11WrapperHelper(servDef);
890:
891:            }
892:
893:            /**
894:             * This method prepares the Wrapped response. This is done only if the 
895:             * deployment(service) is of type "wsdl_11".
896:             *
897:             * @param inout MessageExchange Object
898:             * @param service Service object 
899:             * @param outfile Complete path to the transformed output
900:             *
901:             * @return
902:             */
903:            private Document prepareWSDL11Response(InOut inout,
904:                    Service service, String outfile) {
905:                Document wrappedDoc = null;
906:
907:                try {
908:                    DocumentBuilderFactory docFac = DocumentBuilderFactory
909:                            .newInstance();
910:                    docFac.setNamespaceAware(true);
911:                    DocumentBuilder docBuilder = docFac.newDocumentBuilder();
912:                    Document transDoc = docBuilder.parse(new File(outfile));
913:
914:                    Wsdl11WrapperHelper mWrapHelper = createWrapperHelper(service);
915:                    QName servName = service.getReference().getServiceName();
916:                    String epName = service.getReference().getEndpointName();
917:                    String operation = inout.getOperation().getLocalPart();
918:
919:                    wrappedDoc = mWrapHelper.wrapMessage(transDoc, servName,
920:                            epName, operation, false);
921:
922:                    mLogger.info("[BEGIN_WRAPPED_MESSAGE]");
923:                    if (wrappedDoc != null)
924:                        dump(new DOMSource(wrappedDoc.getDocumentElement()));
925:                    mLogger.info("[END_WRAPPED_MESSAGE]");
926:
927:                } catch (Wsdl11WrapperHelperException wEx) {
928:                    wEx.printStackTrace();
929:                    inout.setError(wEx);
930:                    return null;
931:                } catch (Exception ex) {
932:                    ex.printStackTrace();
933:                    inout.setError(ex);
934:                    return null;
935:                }
936:                return wrappedDoc;
937:            }
938:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.