Build the Java EE Project
The next step is to compile the project into Java bytecode. Before the build starts, Tomcat should be running because Grasshopper will deploy all the necessary files for the Blog Starter Kit to the Tomcat web application folder. You can start Tomcat using this shortcut: Start menu --> All Programs --> Mainsoft for Java EE --> Start Tomcat.
Right-click the Java EE project, and select Build. Grasshopper compiles the .NET code into Java bytecode using a Java version of the Mono Framework assemblies.
Switch the startup project in Visual Studio to the Java EE project, make sure that the MySQL virtual machine is running, and press F5 to start the application in Debug_Java mode. As a result you'll see the Blog Starter Kit home page running in Tomcat.
If you want to set a breakpoint and step through some of the code, Grasshopper makes this process transparent by mapping the C# source code to the bytecode currently executing in the Java EE runtime. You can still use Visual Studio to set breakpoints and view variable data, and you can also debug the Blog Starter Kit once it's deployed to the Tomcat server by attaching the Visual Studio debugger to the Tomcat virtual machine.
Do you remember previously setting up the administrative user and role using Visual Studio's built-in Configuration Manager? Grasshopper covers these settings by implementing its own default Membership provider, and it creates a Derby database that does the same job. Apache Derby is an embedded, 100% Pure Java database server, which is now included in the new Java EE project.
Select the Java EE project in Visual Studio, and click the Configuration Manager icon to go to Grasshopper's Web Site Administration Tool (WSAT). Add an administrative role and a user for that role, which enables you to log in to the admin section of the Blog Starter Kit at http://localhost:8090/BlogWebApplication/admin/default.aspx, and you can add posts and categories. Keep in mind that the administration tool is a Grasshopper application coded entirely in Java, but it can run under Linux, should you need to access it again (see Figure 4).
Figure 4. Add the User: The WSAT is a Grasshopper application coded entirely in Java, but it runs under Linux if you need to access it again.
Your Java EE application needs to know which MembershipProvider to use; add this tag to the system.web section of your project's web.config:
You also should enable remote access to the Grasshopper WSAT to be able to manage your users from a browser running remotely. An application setting key, which by default is disabled for security reasons, has been defined to enable remote access for the WSAT. Add this code into your web.config file to enable it:
<add key="allowRemoteConfiguration" value="true" />
Another change that is required for the WSAT is in global.asax. The Blog Starter Kit redirects all requests to the post.aspx page. Since the Grasshopper WSAT application is packaged within the application itself, and not as a separate application, you'll need to ensure that requests to the WSAT won't be redirected to the post.aspx page. Therefore, add another condition in global.asax before proceeding to the redirect, just to be sure that the request is not to the WSAT application:
Deploy the Application in Linux
The next step is to create the Java deployment package. Stop the running application by clicking the Stop button, or by closing the browser. Switch the project to Release_Java
, and then go to the Java EE project's properties page. Select Java Build from the left-hand menu, and change the Deployment method to Full deployment package (WAR)
. Then build the project once this property has been set.
Now for the fun part: running the starter kit on a Linux server to get you to the finish line. Follow the same steps outlined for the MySQL virtual appliance to download the Tomcat appliance, and run it on the VMware Server.
Start the Tomcat virtual appliance, and go to the Tomcat administration site as specified in the Linux command prompt. Log in to the Tomcat administration section as adminand enter admin as the password (if needed, check the Virtual Appliances download page for the up-to-date login details)and scroll to the bottom of the page. In the Import section, browse to the WAR file that Grasshopper generated in the project's bin directory and select it (see Figure 5).
Tomcat will install the Blog Starter Kit and provide a link to the site to set up the Blog Starter Kit running on the Tomcat virtual appliance (see Figure 6).
Figure 5. Deploy the WAR File: Browse to and select the WAR file that Grasshopper generated in the project's bin directory.
Figure 6. Running It: The ported Blog Starter Kit displays this home page when running in Linux.
There you have it: you've ported completely an ASP.NET 2.0 application that began in .NET running on Windows and SQL Express and ended up being served up by a Linux web server and MySQL. Hopefully, this procedure demonstrated a porting strategy that will better enable you to enter the world of cross-platform .NET development.