Here is the sample ATP servlet described in The scenario.
/** * @(#) ATPServlet.java * * Copyright (c) 1997-2000 CrossWorlds Software, Inc. * All rights reserved. * * This software is the confidential and proprietary information of * IBM. You shall not disclose such Confidential information and shall * use it only in accordance with the terms of the license agreement you * entered into with IBM Software. */ import javax.servlet.http.*; import javax.servlet.*; import java.io.*; import java.util.*; import java.text.*; import IdlAccessInterfaces.*; import CxCommon.BusinessObject; /** * Available To Promise Servlet example */ public class ATPServlet extends HttpServlet { // Defines for some statics public static String DEFAULT_SERVER = "CrossWorlds"; public static String DEFAULT_IOR = "CrossWorlds.ior"; public static String DEFAULT_USER = "admin"; public static String DEFAULT_PASSWD = "null"; // User name to login into the IC Server private String userName = DEFAULT_USER; // Password private String passWord = DEFAULT_PASSWD; // ServerName private String serverName = DEFAULT_SERVER; // IOR File private String iorFile = DEFAULT_IOR; // AccessSession private IInterchangeAccessSession accessSession = null; // AccessEngine private IAccessEngine accessEngine = null; // Servlet Context for getting config information private ServletContext ctx; // A formatter to print the price with precision. private static DecimalFormat formatter; // MIME type private String mimeType = "text/html"; /** * The init method. This method is used by the web server * when the Servlet is loaded for the first time. * @param ServletConfig Configuration information * associated with the servlet. * @exception ServletException is thrown when the * servlet cannot be initialized */ public void init(ServletConfig aConfig)throws ServletException { super.init(aConfig); // Formatter for printing prices in the correct format formatter = new DecimalFormat(); formatter.setDecimalSeparatorAlwaysShown(true); // Read up the initial parameters so we can connect to // the right ICS server String configuredServer = null; String configurediorFile = null; String configuredUser = null; String configuredpassWord = null; configuredServer = aConfig.getInitParameter("ICSNAME"); if ( configuredServer != null) { this.serverName = configuredServer; } else { this.log( "No Interchange Server configured, using default of CROSSWORLDS"); } configurediorFile = aConfig.getInitParameter("IORFILE"); if (iorFile != null) { this.iorFile = configurediorFile; } else { this.log( "IOR file not defined, will use CrossWorlds.ior from home directory"); } try { initAccessSession(); } catch(Exception e) { this.log("Encountered Initialization error", e); throw new ServletException(e.toString()); } } /** * Cleanup method called when servlet is unloaded from the Web Server */ public void destroy() { // Release our session if ( ( accessEngine != null) && (accessSession != null)) { accessEngine.IcloseSession(accessSession); accessEngine = null; accessSession = null; } } /* ** Utility method which creates an access session with InterChange Server. ** If one has already been established then return that. ** @exception Exception when an error occurs while establishing ** the connection to InterChange Server. */ private synchronized void initAccessSession() throws Exception { try { /* ** If the access session has already been established then ** see if the session is still valid (i.e. InterChange ** Server could have been rebooted since the last time ** we used the session). ** If it's not still valid, then open up a new one. */ if (accessSession != null) { try { accessSession.IcreateBusinessObject(""); } catch (ICxAccessError e) { /* ** Cached session is still valid. We expect to get this exception */ return; } // Catch Corba SystemException catch (org.omg.CORBA.SystemException se) { /* ** The session is invalid. ** Open a new one below */ this.log("Re-establishing sessions to ICS"); } } /** * Add the relevant IBM ORB properties to initialize the * IBM ORB. */ Properties orbProperties = new java.util.Properties(); orbProperties.setProperty("org.omg.CORBA.ORBClass", "com.ibm.CORBA.iiop.ORB"); orbProperties.setProperty("org.omg.CORBA.ORBSingletonClass", "com.ibm.rmi.corba.ORBSingleton"); org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init((String[])null, orbProperties); /* ** Use the file that contains the Internet Inter-Orb Reference. ** This object reference will be a serialized CORBA object ** reference to the running Interchange Server that ** we wish to talk to. */ LineNumberReader input = new LineNumberReader(new FileReader(iorFile)); /* ** Create a memory resident CORBA object reference from the IOR ** in the file */ org.omg.CORBA.Object object = orb.string_to_object (input.readLine()); /* ** Now create a real session with the running object */ private IAccessEngine accessEngine accessEngine = IAccessEngineHelper.narrow(object); Properties props = new Properties(); props.put("username","admin"); props.put("password","admin"); IInterchangeAccessSession accessSession = SecureLoginUtility.login(accessEngine,props); if (accessEngine == null) throw new Exception("Unable to communicate with server " + serverName + " using IOR from " + iorFile); /* ** Now that we have an object reference to a running ** server, we must authenticate ourselves before we ** can get a session that is useful. */ accessSession = accessEngine.IgetInterchangeAccessSession( userName, passWord); SCM Properties props = new Properties(); props.put("username","admin"); /* ** User must provide username here. ** In this example "admin" is used as the username. */ props.put("password","admin"); /* ** User must provide the corresponding password here. ** In this example "admin" is used as the password. */ accessSession = SecureLoginUtility.login(accessEngine , props); /* ** You must ensure that "Crossworlds.jar" is in the ** ClassPath. ** Login Utility class named "SecureLoginUtility" ** is provided along with "Crossworlds.jar". */ if (accessSession == null) throw new Exception("Invalid user name and password"); } catch (Exception e) { this.log("Encountered orb Initialization error" , e); if (e instanceof org.omg.CORBA.SystemException) throw new Exception(e.toString()); else throw e; } } /** * Get method called by the WebServer whenever a GET action * is requested by an HTML page. * @param HttpServletRequest handle to the http request * object@param HttpServletResponse handle to the http response * object @exception ServletException is thrown when the servlet * encounters an error @exception is thrown when the * Webserver cannot communicate to the calling * html page */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // String serializedHTMLQuote = null; // A BusinessObject to hold our incoming BO from the // requesting HTML page IBusinessObject aBO = null; // A BusinessObject to hold our resultant BO from the // result of the Collaboration execution IBusinessObject returnedQuoteBusObj = null; /* ** Make sure we have a valid access session with the interchange ** server first */ try { initAccessSession(); } catch(Exception e) { throw new ServletException ("InitAccessSession Failed " + e.toString()); } // Create a BO from the data provided by the HTML page try { aBO = accessSession.IcreateBusinessObjectFrom (req.getQueryString(), mimeType); } catch (ICxAccessError e) { throw new ServletException (" Creating Business Object Failed : " + e.IerrorMessage); } if (aBO == null) { throw new ServletException("Attempting to use Null Bo "); } /* ** Execute the collaboration. We'll get back a ** CrossWorlds business object that contains an ATP ** date for each item. */ try { returnedQuoteBusObj = accessSession.IexecuteCollaboration( "ATPExample","From", aBO); } catch(IExecuteCollaborationError ae) { String error = "Collaboration Error : " + ae.IerrorMessage + ae.status; this.log("Collaboration Error", ae); throw new ServletException(error); } /* ** Now create a table to send back that has: ** ItemNumber Quantity Price */ res.setContentType(mimeType); PrintWriter out = res.getWriter(); out.println("<body>"); out.println("<TABLE BORDER=\"1\">"); out.println("<caption align=\"center\" > " + "<font face=e=\"Haettenschweiler\" size=\"7\">" + "Sales Quote Response</caption>"); out.println("<TR> <TH>Item ID" + "<TH> Item Description" + "<TH> Quantity " + "<TH> Item Price" + "<TH> Available Date " + "<TH> Total Price " + "</TH> </TR>"); IBusinessObjectArray itemContainer = null; try { itemContainer = returnedQuoteBusObj. IgetBusinessObjectArrayAttribute ("OrderItems"); } catch (IInvalidAttributeTypeException e) { throw new ServletException(e.IerrorMessage); } catch (IInvalidAttributeNameException e) { throw new ServletException(e.IerrorMessage); } catch (IAttributeBlankException e) { throw new ServletException(e.IerrorMessage); } catch (IAttributeNotSetException e) { throw new ServletException(e.IerrorMessage); } // A subobject to hold each individual Item IBusinessObject item = null; int size = itemContainer.IgetSize(); // Loop thru the array and print each item // separately String attr = null; int itemQuantity = 0; double itemPrice = 0; //Loop thru the array of returned items for (int i = 0;i < size; i++) { try { // Get the item BusinessObject at the current indexitem = itemContainer.IgetBusinessObjectAtIndex(i); if (item != null) { // Build a html table row beginning with ITemID // attribute try { attr = item.IgetStringAttribute("ItemID"); out.print("<TR> <TD> " + attr + "</TD>" + "<TD>"); // We have printed the value, // set it to null again attr = null; } catch (IAttributeNotSetException e) { attr = "N/A"; out.print("<TR> <TD> "); out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeNameException e) { attr = "N/A"; out.print("<TR> <TD> "); out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeTypeException e) { attr = "N/A"; out.print("<TR> <TD> "); out.print(attr + "</TD>" + "<TD>"); } // Get the ItemType attribute try { attr = item.IgetStringAttribute ("itemType"); out.print(attr + "</TD>" + "<TD>"); // We have printed the value, // set it to null again attr = null; } catch (IAttributeNotSetException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeNameException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeTypeException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } // Get the orderQty Attribute try { attr = item.IgetStringAttribute ("orderQty"); try { itemQuantity = Integer.parseInt(attr); } catch (NumberFormatException e) { itemQuantity = -1; } out.print(attr + "</TD>" + "<TD>"); // We have printed the value, // set it to null again attr = null; } catch (IAttributeNotSetException e) { attr = "N/A"; itemQuantity = -1; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeNameException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeTypeException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } // Get the ItemPrice attribute try { attr = item.IgetStringAttribute("itemPrice"); int indexOfDollar = attr.indexOf("$"); String priceToParse = null; // Locate if we have "$" in the value if (indexOfDollar == -1) priceToParse = attr; else priceToParse = attr.substring (indexOfDollar + 1); // Format the price so it looks like $NNNN.NN try { itemPrice = Double.parseDouble
(priceToParse); } catch (NumberFormatException e) { itemPrice = -1; } out.print(attr + "</TD>" + "<TD>"); // We have printed the value, set it to null again attr = null; } catch (IAttributeNotSetException e) { attr = "N/A"; itemPrice = -1; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeNameException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeTypeException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } // Get the ATPDate and print it try { attr = item.IgetStringAttribute("ATPDate"); out.print(attr + "</TD>" + "<TD>"); } catch (IAttributeNotSetException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeNameException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } catch (IInvalidAttributeTypeException e) { attr = "N/A"; out.print(attr + "</TD>" + "<TD>"); } /* ** Now print the total price for the item. ** If we don't have sufficient information then ** print N/A */ if ((itemPrice == -1) || (itemQuantity == -1)) { out.println(attr + "</TD>" + "<TD>"); // We have printed the value, // set it to null again attr = null; } else { double totalPrice = itemQuantity * itemPrice; out.println("$" + formatter.format (totalPrice).trim() + "</TD>" + "<TD>"); } } // end if (Item != null) } // End try catch (IAttributeBlankException e2) { continue; } catch (IInvalidIndexException e) { throw new ServletException(e.getMessage()); } finally { if (item != null) accessSession.IreleaseBusinessObject(item); } }// End for loop // Releasing business objects, ideally should be done in a // finally block so that // in case of exceptions the business objects are released. if (itemContainer != null) accessSession.IreleaseBusinessObjectArray(itemContainer); if (returnedQuoteBusObj != null) accessSession.IreleaseBusinessObject(returnedQuoteBusObj); if (aBO != null) accessSession.IreleaseBusinessObject(aBO); // end release code // Close the HTML table out.println("</TABLE>"); // Finish the page body out.println("</body></html>"); } // end do get }