/** * Copyright � 2002 Sun Microsystems, Inc. All rights reserved. */ package com.sun.s1peqe.ejb.stateless.converter.client; import java.util.Properties; import java.math.BigDecimal; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import com.sun.s1peqe.ejb.stateless.converter.ejb.*; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import java.io.File; import com.sun.ejte.ccl.reporter.SimpleReporterAdapter; import org.apache.xpath.*; /** * A simple java client. This uses the services provided by the <code>ConverterBean</code> and * converts 100 US dollars to Yen and 100 Yen to Euro. * <p>In this regard, it does the following in order * <ul> * <li>Locates the home interface of the enterprise bean * <li>Gets a reference to the remote interface * <li>Invokes business methods * </ul> * <br> * <b>Locating the home interface:</b> * <blockquote><pre> * Context initial = new InitialContext(); * Context myEnv = (Context)initial.lookup("java:comp/env"); * Object objref = myEnv.lookup("ejb/SimpleConverter"); * ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objref, ConverterHome.class); * </pre></blockquote> * <br> * <b>Creating the remote interface:</b> * <blockquote><pre> * Converter currencyConverter = home.create(); * </pre></blockquote> * <br> * <b>Invoking business methods:</b> * <blockquote><pre> * BigDecimal param = new BigDecimal ("100.00"); * amount = currencyConverter.dollarToYen(param); * amount = currencyConverter.yenToEuro(param); * </pre></blockquote> * <br> * <b>Output:</b> * <pre> * 12160.00 * 0.77 * </pre> * * */ public class ConverterClient { private SimpleReporterAdapter stat = new SimpleReporterAdapter("appserv-tests"); ConverterClient() { } /** * The main method of the client. This invokes the <code>ConverterBean</code> to use * its services. It then asks the bean to convert 100 dollars to yen and * 100 yen to euro. The results are printed at the terminal where the client is run. * See <code>appclient</code> documentation in SunONE app server to run the clinet. * */ public static void main(String[] args) { ConverterClient client = new ConverterClient(); client.run(args); } private void run(String[] args) { String testId = null; String xmlfile = args[0]; try { stat.addDescription("Verify converter sample with avk"); testId = "AVK- Converter::Sample AppClient"; if ( validArchive( xmlfile)) { System.out.println("Static Check returned 0 - good!"); stat.addStatus(testId, stat.PASS); } else { stat.addStatus(testId, stat.FAIL); System.out.println("Static Check returned non zero - not good!"); } } catch (Exception ex) { stat.addStatus(testId, stat.FAIL); System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } finally { stat.printSummary(testId); } } private boolean validReport(String verifierhome) throws Exception{ String search = "app-verification/ejb-percentage"; if (verifierhome == null ) return false; // check the xml results file generated by running AVK StaticCheck System.out.println("verifierhome is " + verifierhome); // javke.home/reporttool/resultS String xmlfile = verifierhome + File.separator + "reporttool" + File.separator + "results" + File.separator + "result.xml"; System.out.println("file path is " + xmlfile); File xmlFile = new File( xmlfile ); boolean passed = false; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { System.out.println(" get db"); DocumentBuilder db = dbf.newDocumentBuilder(); System.out.println(" parse "); Document doc = db.parse(xmlFile); System.out.println(" get root "); Element root = doc.getDocumentElement(); System.out.println( "The failed count is: " + findValue( root, search)); if ( findValue( root, search).equals("100")) passed = true; } catch (org.xml.sax.SAXException se) { throw new Exception(se); } catch (javax.xml.parsers.ParserConfigurationException pce) { throw new Exception(pce.toString()); } catch (Exception e) { throw new Exception(e.toString()); } return passed; } private boolean validArchive( String xmlfile) throws Exception{ // check the xml results file generated by running AVK StaticCheck System.out.println("xmlfile is " + xmlfile ); // results of test is in current working directory. // String xmlfile = "ejb-stateless-converterApp.ear.xml"; boolean passed = false; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); String good = "<failure-number>0</failure-number>"; try { DocumentBuilder db = dbf.newDocumentBuilder(); // The next method requires /sun/appserver8/lib/dtds/static-verification_1_4.dtd // static-verification_1_4.dtd needs to be moved to that location for method to succeed. // // [exec] java.lang.Exception: java.io.FileNotFoundException: // /sun/appserver8/lib/dtds/static-verification_1_4.dtd // (No such file or directory) Document doc = db.parse(xmlfile); Element root = doc.getDocumentElement(); System.out.println( "The failed count is: " + findValue( root, "/static-verification/failure-count/failure-number") ); if ( findValue( root, "/static-verification/failure-count/failure-number").equals("0") ) passed = true; } catch (org.xml.sax.SAXException se) { throw new Exception(se); } catch (javax.xml.parsers.ParserConfigurationException pce) { throw new Exception(pce.toString()); } catch (Exception e) { throw new Exception(e.toString()); } return passed; } /** * Returns the contents of all immediate child text nodes, can strip whitespace * <p> * Takes a node as input and merges all its immediate text nodes into a * string. If the strip whitespace flag is set, whitespace at the beggining * and end of each merged text node will be removed * * @param node node to extract text contents of * @param b_strip_whitespace flag to set whitespace removal * @return string containing text contents of the node **/ public static String getTextContents ( Node node ) { NodeList childNodes; StringBuffer contents = new StringBuffer(); childNodes = node.getChildNodes(); for(int i=0; i < childNodes.getLength(); i++ ) { if( childNodes.item(i).getNodeType() == Node.TEXT_NODE ) { contents.append(childNodes.item(i).getNodeValue()); } } return contents.toString(); } /** * Returns the text contents of the first node mathcing an XPath expression * <p> * Takes a context node and an xpath expression and finds a matching * node. The text contents of this node are returned as a string * * @param node context node at which to eval xpath * @param xql XPath expression * @return Text contents of matching node **/ public static String findValue(Node node, String xql) throws Exception { System.out.println(" looking for this xpath expression : " + xql); if( (xql == null) || (xql.length() == 0) ) { throw new Exception("findValue called with empty xql statement"); } if(node == null) { throw new Exception("findValue called with null node"); } return getTextContents( XPathAPI.selectSingleNode(node,xql) ); } }