Showing posts with label Virtualization. Show all posts
Showing posts with label Virtualization. Show all posts

Friday, July 29, 2011

Creating a Simple Application using Google App Engine

If hosting your application on the Cloud is your primary goal, then Google App Engine might be a good start. Here we are going to see a simple example of how to run an app on Google App Engine.

Install Google App IDE Plugin for your IDE. (For using Eclipse IDE, go to Eclipse.org download eclipse, and go to code.google.com and find how to get it installed for Eclipse)

Steps are more detailed in : http://code.google.com/appengine/docs/java/gettingstarted/installing.html

After Installing, Create a new Web Application Project

image

Name your Project, and deselect “Use Google Web Toolkit” unless needed.

image

This will be the file structure. Under /src/<package Name>/ you will find your Servlet Class source code. (.java file)

image

 

Next – lets modify the Servlet.

Open your servlet from src/((package_name))/((Project_name))Servlet.java & modify your servlet.java code to make it perform some work, when called as below which gives the date and current time.

   1:  package //your package name;
   2:  import java.io.IOException;
   3:  import javax.servlet.http.*;
   4:  import java.io.PrintWriter;
   5:  import java.text.SimpleDateFormat;
   6:  import java.util.Date;
   7:  import java.util.SimpleTimeZone;
   8:   
   9:  @SuppressWarnings("serial")
  10:  public class Conciliation1Servlet extends HttpServlet {
  11:      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  12:          SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
  13:          fmt.setTimeZone(new SimpleTimeZone(0,""));
  14:          resp.setContentType("text/html");
  15:          PrintWriter out = resp.getWriter();
  16:          out.println("<html><body><p> Current Time is: " + fmt.format(new Date())+ "</p></body></html>");
  17:      }
  18:  }



 


Open web.xml (usually in war/WEB-INF/lib/web.xml ), and modify as shown


   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3:  xmlns="http://java.sun.com/xml/ns/javaee"
   4:  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   5:  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   6:  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
   7:      <servlet>
   8:          <servlet-name> <!-- Give a Name for Servlet--> </servlet-name>
   9:          <servlet-class> <!-- Package Name --> </servlet-class>
  10:      </servlet>
  11:      <servlet-mapping>
  12:          <servlet-name> <!-- Same name as the Servlet provided above --> </servlet-name>
  13:          <url-pattern>/dothis </url-pattern><!-- Give the URL pattern, example /* (or) /dothis, always start the url pattern with / --> 
  14:      </servlet-mapping>
  15:      <welcome-file-list>
  16:          <welcome-file>index.html</welcome-file>
  17:      </welcome-file-list>
  18:  </web-app>



Now Modify index.html under war/WEB-INF/


   1:  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   2:  <!-- The HTML 4.01 Transitional DOCTYPE declaration-->
   3:  <!-- above set at the top of the file will set     -->
   4:  <!-- the browser's rendering engine into           -->
   5:  <!-- "Quirks Mode". Replacing this declaration     -->
   6:  <!-- with a "Standards Mode" doctype is supported, -->
   7:  <!-- but may lead to some differences in layout.   -->
   8:   
   9:  <html>
  10:    <head>
  11:      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  12:      <title>Hello App Engine</title>
  13:    </head>
  14:   
  15:    <body>
  16:      <h1>Hello App Engine!</h1>
  17:      
  18:      <table>
  19:        <tr>
  20:          <td colspan="2" style="font-weight:bold;">Available Servlets:</td>        
  21:        </tr>
  22:        <tr>
  23:          <td><a href="dothis">Current Date</a></td>
  24:        </tr>
  25:      </table>
  26:    </body>
  27:  </html>



Open and update version number (line 4) if needed in appengine-web.xml from war/WEB-INF/lib


If you have a google app engine setup for your google login id, then create a google application. You will be asked to choose an application_id. Use this application_id in line 3, before you upload it to google app engine for testing.


   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
   3:      <application><!—application id—></application>
   4:      <version>1</version> <!-- update version number if needed -->
   5:      
   6:      <!-- Configure java.util.logging -->
   7:      <system-properties>
   8:          <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
   9:      </system-properties>
  10:      
  11:  </appengine-web-app>



Execute the App in Debug Mode


image


image


Click on “Current Date” and you will see the current time.


image


Let us now try to customize it per user (in development server a default user test@example.com gets automatically created with app engine, but when you deploy it to google app engine, you will be asked to be authenticated by Google Sign In Process)


Modify the servlet.java as follows.


   1:  package //package name;
   2:  import java.io.IOException;
   3:  import javax.servlet.http.*;
   4:  import java.io.PrintWriter;
   5:  import java.text.SimpleDateFormat;
   6:  import java.util.Date;
   7:  import java.util.SimpleTimeZone;
   8:  import com.google.appengine.api.users.User;
   9:  import com.google.appengine.api.users.UserService;
  10:  import com.google.appengine.api.users.UserServiceFactory;
  11:   
  12:  @SuppressWarnings("serial")
  13:  public class Conciliation1Servlet extends HttpServlet {
  14:      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  15:          SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
  16:          fmt.setTimeZone(new SimpleTimeZone(0,""));
  17:          
  18:          UserService userService = UserServiceFactory.getUserService();
  19:          User user= userService.getCurrentUser();
  20:          
  21:          String navBar;
  22:          if (user != null)
  23:          {
  24:              navBar = "<p> Welcome, " + user.getNickname() + "! You can <a href=\""+ userService.createLogoutURL("/")+ "\"> Sign Out </a>.</p>";
  25:          } else {
  26:              navBar = "<p> Welcome! <a href=\""+ userService.createLoginURL("/") + "\"> Sign In or Register </a> to customize </p>";
  27:          }
  28:          
  29:          resp.setContentType("text/html");
  30:          PrintWriter out = resp.getWriter();
  31:          out.println("<html><body>");
  32:          out.println(navBar);
  33:          out.println("<p> Current Time is: " + fmt.format(new Date())+ "</p>");
  34:          out.println("</body></html>");
  35:      }
  36:  }



Execute the App in Debug Mode


Open the browser and view index.html


image


Click on Current Date


image


Now Click on “Sign In or Register”, in development it will take you to a simple console that will allow you to login as "test@example.com” but after you deploy in google app engine, you will be asked to login with your google id.


image


test@example.com is a default account that’s in development for debugging use, so choose “Log In”


It will take you back to first screen, because we gave a successful login as to take us to root of the site.


image


Now click on Current Date, lets see what happens now.


image


We should get this type of a window after it has authenticated you.


-- Thz it , its that’s simple–


Now you have a simple Web Application that is ready to be deployed to - Google App Engine


Steps to Upload your application to the cloud is more detailed on : http://code.google.com/appengine/docs/java/gettingstarted/uploading.html


NOTE: update your application_id in appengine-web.xml to the one in your google application admin console for your google account where you are planning to upload the app to. If this is not  the same you might have trouble uploading your application.

Sunday, May 24, 2009

Virtual Box – Sun Microsystems

Virtual box is open source – full virtualization of x86 hardware. Unlike Microsoft Virual PC (A free version of virtualization software for x86 architecture) which had some issue to bring up the imagedisplay setting properly for fedora Linux or open Solaris OS. I have a Dell Inspiron 1520/1521 who's WXGA+ diplay uses inbuilt display from intel chipset which shares 512 MB or RAM (max) when the installed RAM is 4GB. On parallel installation of Fedora Linux or Open Solaris OS, they fail to detect my WXGA+ display, or comply with it for even a lower resolution. It appeared the same when i tried to install these Operating Systems as Virtual instances on Microsoft Virtual PC. Microsoft Virtual PC, is expected to generate a virtualized display card with generic drivers, but what i expected was not a true possibility as it was built for what it imagewas. Other hardware virtualization options of Xen HyperV (Open Source) was ruled out, because the issue was not just installing Virtual OS over my laptop but, was to bypass the video card compatibility issue with Open Solaris OS or Fedora Linux. Surprisingly i came across the Virtual Box from Sun Microsystems. Open Solaris requires only 512 MB , but if you allocate 512 MB to install the OS then it might take few hours to complete the installation. It is better allocate 1GB+. After it installs Open Solaris runs without any issues on the machine. Network connection gets shared automatically with the underlying Network imageconnection. On the whole I appreciate this flaw less open source Virtual Box.