Develop à la J2EE by using .NET with the SnippetStore Project (Chapter III)
by Julien Brunet (j.brunet@cross-systems.com)

Chapter 3/3
Printed : environ 8 pages
Version 1.2-RAW

[WARNING] This is a fast rewrite of the rough Translation from the Google service. Please apologize the numerous mistakes, someone is actually reviewing this automated translation.

Converting Java PetStore to .NET

This chapter describes the conversion of Java PetStore to .NET using the SnippetStore framework .

- If you arrived here by error, you can start again in chapter 1.
- If you did not build the SnippetStore framework yet, you can go to chapter 2 and follow the labs 1 to 8 without reading the detailed explanations.

 

Before starting

Preparing the labs

Lab 9. Preparing the conversion of Java PetStore

- Open a Command Prompt in C:\SnippetStore
- Execute setenv.cmd in this Command Prompt
- Keep this Command Prompt opened during all the labs

 

Preparing the ASP.NET environment

Lab 10. Preparing the ASP.NET environnement for Java PetStore

C:\SnippetStore> nant jps-reset

This NAnt target :
- Copy the Web resources in the out directory of the ASP.NET application (C:\SnippetStore\samples\jps1.1.2\out)
- Create/replace the global.asax and global.asax.jsl files containing the initialization of the Web container
- Create/replace the Web.Config file of the out directory with preset values (MSDE connections, etc...)
- Copy the required assemblies of the SnippetStore framework in the out\bin directory of the ASP.NET application.
 

Compiling the SnippetStore toolkiy

We spoke much about the toolkit, but it is still not compiled!

Lab 11. Compiling the SnippetStore toolkit

C:\SnippetStore> nant SnippetStore.Design
C:\SnippetStore> nant SnippetStore.Design.NAntTasks
C:\SnippetStore> nant SnippetStore.Tracer

This NAnt target :
- Compilesthe EJB/Servlets/TagLib/JSP conversion toolkit into C #
- Compiles the NAnt extension layer
- Compiles the SnippetStore visual tracer
 

Converting the Components part of Java PetStore

The Components part of Java PetStore is located in the C:\SnippetStore\samples\jps1.1.2\src\components directory. It contains in particular 9 EJBs, and several classes of data access (DAO).
 

J# Compilation

Lab 12. J# compilation of the Components part of Java PetStore

C:\SnippetStore> nant jps-comp-1

This NAnt target compiles the Java sources by using the J# compiler (vjc.exe)

It produces the components.dll assembly in the out directory
 

Generating wrappers

Lab 13. Generating wrappers for the Components part of Java PetStore

C:\SnippetStore> nant jps-comp-2

This NAnt target :
- Dynamically loads the NAnt extension layer of the SnippetStore toolkit
- Invoke the EJB conversion task <ejbimp> by in particular indicating the XML manifest as input and the Web.config file as output. It produces the C# source code AL_ejb.cs in the src directory and enhances the Web.config file
 

Compiling wrappers

Lab 14. Compiling wrappers for the Components part of Java PetStore

C:\SnippetStore> nant jps-comp-3

This NAnt target compiles the generated sources using the C# compiler (csc.exe)

It produces the components.AL.dll assembly in the out directory

 

Converting the Web part of Java PetStore

The Web part of Java PetStore is located in the C:\SnippetStore\samples\jps1.1.2\src\petstore directory. It contains in particular 1 EJB, 2 Servlets, 1 TagLibs about 10 tags, several Java classes to manage the MVC model, and approximatively 75 JSP pages.

 

Manuals fixups of the Java PetStore code

Some manual fixups must be made to the Java PetStore code before compilation, mainly because of the J# gaps explained in chapter 2.

Problem Action Location(s)
1) The J# ClassLoader always returns null Use the LAME wrapper to simulate a ClassLoader com\sun\j2ee\blueprints\petstore\control\web\MainServlet.java
(line 78)

modelManager = (ModelManager) Beans.instantiate(new LAME.ClassLoader(), "com.sun.j2ee.blueprints.petstore.control.web.ModelManager");

com\sun\j2ee\blueprints\petstore\control\web\RequestProcessor.java
(line 105)


handler = (RequestHandler)new LAME.ClassLoader().loadClass(requestProcessorString).newInstance();

com\sun\j2ee\blueprints\petstore\control\web\ScreenFlowManager.java
(line 141)

handler = (FlowHandler)new LAME.ClassLoader().loadClass(flowHandlerString).newInstance();
 
2.a) Incorrect display of the cart.aspx page Replace the HTML input field name itemQuantity_ to itemQuantity(space) com\sun\j2ee\blueprints\petstore\control\web\handlers\CartHandler.java
(line 48)

if ((param != null) && param.startsWith("itemQuantity ")) {

(line 51)

String id = param.substring("itemQuantity ".length(),
 
3) A new operator has appeared in the J2SE 1.2 Use the LAME wrapper to change the vector to a collection com\sun\j2ee\blueprints\petstore\taglib\list\SearchListTag.java
(line 41)

Collection keywords = new LAME.Vector2Collection( JSPUtil.parseKeywords(searchString) );
Lab 15. Manuals fixups of the Web part of Java PetStore

- Apply the corrective measures mentioned above

 

J# Compilation

Lab 16. J# compilation of the Web part of Java PetStore

C:\SnippetStore> nant jps-estore-1

This NAnt target compiles the Java sources using the J# compiler (vjc.exe)

It produces the estore.dll assembly in the out directory

 

Generating wrappers

Lab 17. Generating wrappers for the Web part of Java PetStore

C:\SnippetStore> nant jps-estore-2

This NAnt target :
- Dynamically loads the NAnt extension layer of the SnippetStore toolkit
- Invoke the EJB,TagLib,WebApp conversion tasks <ejbimp>,<taglibimp>,<webappimp> by in particular indicating the XML manifest as input and the Web.config file as output. It produces the C# source code AL_ejb.cs, AL_TagLib.cs and AL_WebApp.cs in the src directory and enhances the Web.config file
 

Compiling wrappers

Lab 18. Compiling wrappers for the Web part of Java PetStore

C:\SnippetStore> nant jps-estore-3

This NAnt target compiles the generated sources using the C# compiler (csc.exe)

It produces the estore.AL.dll assembly in the out directory

 

JSP->ASP.NET conversion

Lab 19. JSP->ASP.NET conversion

C:\SnippetStore> nant jps-estore-4

This NAnt target transforms the JSP pages into ASP.NET pages. The original JSP files are not modified.

It produces the .aspx files in the out directory

 

Manual fixups for the JSP->ASP.NET conversion

Manual fixups must be applied to the converted code of Java PetStore before it run.

Paths are relative to the out directory (C:\SnippetStore\samples\jps1.1.2\out)
 

Problem Action Location(s)
4) Japanese version is not supported Remove the reference in the requestmappings.xml file WEB-INF\xml\requestmappings.xml
(line 5)
supprimer ou commenter la ligne "<screen-definition ... language="ja_JP"/>
5) Pages should be JSP fragments Rename to .jspf files mylist.jsp
renommer en mylist.jspf

sideindex.jsp
renommer en sideindex.jspf

splash.jsp
renommer en splash.jspf

changeaddressform.jsp
renommer en changeaddressform.jspf

changepreferencesform.jsp
renommer en changepreferencesform.jspf
 
6) Invalid page references Refer to the pages you just renamed annotated-index.html
(line 7)

<frame src="annotation/ann_overview_j2ee.aspx" name="annotation"> </frame>

index.aspx
(line 15)

<!-- #include file="sideindex.jspf" -->

(line 18)

<!-- #include file="splash.jspf" -->

cart.aspx
(line 28)


<!-- #include file="mylist.jspf" -->

enteruserdata.aspx
(line 183)


<!-- #include file="changeaddressform.jspf" -->

editaccount.aspx
(line 62)


<!-- #include file="changeaddressform.jspf" -->

(line 67)


<!-- #include file="changepreferencesform.jspf" -->
 
7) Server tags cannot contain <% ... %> instructions Give and ID to each invalid WebControl and declare a Page_Load() script productcategory.aspx
(line 14)


<script runat="server">
  void Page_Load(Object Sender, System.EventArgs e)
  {
    ((productList)FindControl("productList")).set_category(request.getParameter("category_id"));
  }
</script>


<j2ee:productList id="productList" runat="server" numItems="4" >

product.aspx
(line 28)

<script runat="server">
  void Page_Load(Object Sender, System.EventArgs e)
  {
    ((productItemList)FindControl("productItemList")).set_productId( request.getParameter("product_id"));
  }
</script>


<j2ee:productItemList id="productItemList" runat="server" numItems="3" emptyList="Unable to find requested Product">

search.aspx
(line 14)


<script runat="server">
  void Page_Load(Object Sender, System.EventArgs e)
  {
    ((searchList)FindControl("searchList")).set_searchText( request.getParameter("search_text"));
  }
</script>


<j2ee:searchList id="searchList" runat="server" numItems="4" emptyList="No products found">
 
8) block has inferior scope in ASP.NET Replace the blocks <% ...%> with <script runat=server>...</script> product.aspx
(line 23)

<script runat="server">
  Product product = catalog.getProduct(request.getParameter("product_id"), Locale.US);
</script>
 
9) Exceptions are not checked by ASP.NET CodeDom Replace the blocks <%=...%> by <script runat=server> try { ... } catch (Exception e) {} </script> populate_install.aspx
(line 30)

<% try { if (populateBean.getDatabaseProductName().equals("DBMS:cloudscape")) out.println("(Recommended Based on Configuration)"); } catch (Exception e) {} %>

(line 32)

<% try { if (populateBean.getDatabaseProductName().equals("Oracle")) out.println("(Recommended Based on Configuration)"); } catch (Exception e) {} %>

(line 34)

<% try { if (populateBean.getDatabaseProductName().equals("SQL Server")) out.println("(Recommended Based on Configuration)"); } catch (Exception e) {} %>

petfooter.aspx
(line 34)

<% try { out.print( profilemgr.getBanner(favCat) ); } catch (Exception e) {} %>
 
2.b) Incorrect display of the cart.aspx Replace the HTML input field name itemQuantity_ by itemQuantity(space) mylist.jspf
(line 14)

<j2ee:myList runat="server" numItems="3">

(line 26)

<j2ee:items runat="server">

(line 37)

<j2ee:prevForm runat="server" action="cart">

(line 42)

<j2ee:nextForm runat="server" action="cart">

(line 28-30)

<a href="product?product_id=<j2ee:productAttribute runat="server" attribute="id"/>" ><j2ee:productAttribute runat="server" />
</a> (<j2ee:productAttribute runat="server" attribute="id"/>)

cart.aspx
(line 62)

<input name="itemQuantity <j2ee:cartAttribute runat="server" attribute="itemid"/>"
 
Lab 20. Manuals fixups for the JSP->ASP.NET conversion

- Apply the manual fixups above

Creating the MSDE database

Happy initiative on behalf of Sun, the Java PetStore application does not require more than a empty base to begin. It starts by checking the existence of tables in the schema, and proposes to you to create them on the flight, directly from the Web interface.

Creating of the MSDE database

To create a base MSDE, not need for the SQL Server client tools such as Enterprise Manager. ASP.NET Web Matrix enables you to create very easily a new database.

Lab 21. Creation of the data base MSDE

- Ensure that your MSDE instance is configured and running
- In ASP.NET WebMatrix, "WorkSpace" window, "Database", click on the "New Connection" icon
- In the "Connect to Database" dialog box, click on the "Create has new database" link
- Enter ESTOREDB as the database name and validates

Remark: If your MSDE instance is different from (local), modify the connection string accordingly in the Web.config file (here in red)

<configuration>
   ...
  <SnippetStore.naming>
    <add key="jdbc/EstoreDataSource"    value="#datasource:Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=ESTOREDB;Data Source=(local);Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=MONTREAL;Use Encryption for Data=False;Tag with column collation when possible=False" />
    <add key="jdbc/SignOnDataSource"    value="#datasource:Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=ESTOREDB;Data Source=(local);Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=MONTREAL;Use Encryption for Data=False;Tag with column collation when possible=False" />
    <add key="jdbc/InventoryDataSource" value="#datasource:Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=ESTOREDB;Data Source=(local);Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=MONTREAL;Use Encryption for Data=False;Tag with column collation when possible=False" />
    ...

Altering the schema creation script for MSDE

Because Java PetStore supports Oracle, Sybase and Clouscape databases, three schema creation scripts are provided. We will focus on the Sybase script, because it is technically nearest to SQL Server. For as much, it should be modified so that it is understood completely by our JDBC/ADO.NET bridge

Lab 22. Altering the schema creation script for MSDE

- Edit the C:\SnippetStore\samples\jps1.1.2\out\WEB-INF\sql\Sybase.sql file
- Remove all DROP statements
- Save the file

 

Running Java PetStore on .NET

 

Starting the SnippetStore visual tracer

Applications based on the SnippetStore framework running under Cassini, how to show a trace log?

Lab 23. Starting the SnippetStore visual tracer

-
Run C:\SnippetStore\bin\SnippetStore.Tracer.exe

 

Running Cassini with the ASP.NET generated application

Cassini is a mini Webserver limited to local calls only (except if you it "unsling":-)), acting as ASP.NET container. Contrary to IIS (Internet Information Services), it forwards all the Web requests to the ASP.NET engine, while IIS makes the first filtering on the basis of files extensions. Problem, the files extensions do not exist in Java PetStore since it is controlled by servlets (/populate/... and/control/...)

To run a Cassini server, you have to provide the virtual root, HTTP port and physical path of the application

Lab 24. Running Cassini with the generated ASP.NET application
note: The %Cassini% environment variable shall reference the installation directory of Cassini

C:\SnippetStore> %Cassini%\WebServer" /port:8080 /path:C:\SnippetStore\samples\jps1.1.2\out /vpath:/estore

 

Starting the generated ASP.NET application

Don't be disappointed if the thing is not working on the first time, maybe you did not apply all the manual fixups described in this chapter. JSP fixups can be made along the execution of the application, since ASP.NET pages are dynamically reloaded like their J2EE counterparts.

Lab 25. Starting the generated ASP.NET application

Remarquk: ASP.NET WebMatrix must be configured to use the 1.1 Framework

- Create WebServer.exe.config file
- Add    <configuration>
           <startup>
             <requiredRuntime version="v1.1.4322"/>
             <supportedRuntime version="v1.1.4322"/>
           </startup>
         </configuration>

- Start Internet Explorer (or your favorite browser)
- Open the address http://localhost:8080/estore/index.html
- Click on the link "Enter the store". The SnippetStore visual tracer shall start to display lines

 

Conclusion

I've little time to conclude this study which impassioned me... I hope that you will have found there, whatever your "religion" is, an honest comparison of the two technical platforms. Personally, I assert only a .NET knowledge, but this experiment was a formidable springboard to deepen the J2EE platform.

The SnippetStore sources are freely downloadable on SourceForge.

 


Author : Julien Brunet

Biography: Hired by Cross Systems in 1998 for its consulting department, Julien Brunet helps customers in the design and realization of their software architectures.

Disclaimer: The remarks made above engage only its author and do not necessarily reflect the methodologies and others practise of the Cross Systems company.
 

Thanks to Charline, Gilles, Hans, Nicolas, Laurent, and Sami for their support !

Copyright © June 2003

Resources

SnippetStore official website : http://snippetstore.sourceforge.net/

Download the sources and binaries

SnippetStore (sources) : Snippetstore (zip File)

Lesser-GPL Licence

All files are provided under Lesser-GPL license.