Wednesday, August 31, 2011

How to deploy a Web Application in Stratos Live using Carbon Studio


Prerequisites 
1) Download and install Carbon Studio  (I am using version 1.0.12 in this tutorial)
 1) Create a CApp
Go to File ->New -> Project


Select Carbon Application Project


Give a name to the project. Give TestProject as the name of the CApp.

 2) Create a Web Application Project 
Right click on TestProject -> New -> Web Application (WAR)



A wizard will be shown for the creation process of the WAR. Select Web Application from web project as shown. Since we don't have any web projects in the workspace, let's create one from scratch. So click on New Dynamic Web Project.


Give the 'WebApp' as the project name. Click on next.


The following page will be shown. Click on next.


In the next page, the information is given for you. Just click on Finish to finish creating the web project. (The contents for this project will be added later).


When you clicked on 'Finish' you will see the newly-created web application in your workspace and it will be selected in the drop down box. Click on Next.


We need the web application to be contained in the CApp, therefore select TestProject and click on Finish.


You will now have two separate projects in your workspace. WebApp is the dynamic web project and TestProject is the CApp project which contains a reference to the WebApp.


For this scenario, the web app will contain one JSP and servlet each. Let's create the JSP first. 
Right click on WebApp -> New -> JSP File


Give index.jsp as the name and click on Next.



New JSP File (html) will be the default selection as the JSP template. Click on Finish.


The new index.jsp file is now created inside the 'WebContent' folder.


Overwrite your index.jsp file with the following code and save.


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h2>Welcome to the tutorial on how to deploy a Web App in StratosLive!</h2>
<form method="get" action="welcome.jsp">
Enter your name here : 
<input type="text" name="name">
<input type="submit" value="Submit"></form>
</body>
</html>



Let's create the servlet for the web app now. 
Right click on WebApp -> New -> Servlet


You will now see a wizard. In the first page, Project will be already selected. Source folder is the location of the servlet that we are creating. Give wso2.sample as the package name and WelcomeServlet as the class name as shown below. Click on Next. 


In the next page, add a description. Since this app doesn't require any initialization parameters click on Next. (We'll be adding the URL Mappings in the web.xml later).


Select the method stubs that you want. Click on Finish.


Your servlet is now created inside WebApp/src. However there will be errors because we need to add a library containing the javax.servlet package to the build path of the WebApp project.


To add the required library right click on WebApp -> Build Path -> Configure Build Path.


Click on Add External Jars in the Libraries tab.


Provide the relevant jar file from your local file system. I used servlet-api-5.5.15.jar. Click on OK.


Replace your WelcomeServlet.java with the following code and save.

package wso2.sample;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WelcomeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private String userName;

	public void init() throws ServletException {
		userName = "default";
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		userName = (String) request.getParameter("name");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<h2>" + "<font color=\"blue\">" + "Welcome " + userName
				+ " :) !!" + "</font></h2>");
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}






Let's now add a URL pattern for the servlet mapping in the web.xml file. The web.xml file can be found in WebApp -> WebContent -> WEB-INF. Replace your web.xml file with the following code. Save the file. 



<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>WebApp</display-name>
	<servlet>
		<description>This servlet prints a welcome message to the user.</description>
		<display-name>WelcomeServlet</display-name>
		<servlet-name>WelcomeServlet</servlet-name>
		<servlet-class>wso2.sample.WelcomeServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>WelcomeServlet</servlet-name>
		<url-pattern>/welcome.jsp</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

We have now finished creating the web application. 


4) Deploy the Web Application via Carbon Studio


In order to add a web application to Stratos Live directly from Carbon Studio we need to first create a remote server instance within Carbon Studio. Go to Window -> Show View -> Servers. 


You will now see the Servers view/tab at the bottom. Right click on the Servers tab -> New -> Server.


You will now see a list of server types in this view. Scroll down to find the servers under WSO2.


Under WSO2 server types, select WSO2 Carbon remote server. Provide the server's host name and name (you can give names of your preference). Click Next.


To provide the remote Carbon server URL, you need to give the App Server URL for your tenant. The URL takes the format - https://appserver.stratoslive.wso2.com/t/(your tenant domain). In my case, it's https://appserver.stratoslive.wso2.com/t/testUser.com. Provide the username and password for your tenant. You can test the server connection by clicking the Test connection button and you can validate your credentials by clicking on the validate button as shown below. Click Next to proceed.


Now you will be able to add resources to your remote server. We need to add TestProject to the remote server, select TestProject and click on Add>. Click Finish.


Now you will see that TestProject has been added to the server. Click on the server to start the server and to deploy TestProject.



When the server has started, the server will indicate that it has started as shown below.



Let's see if our application has been properly deployed in the Application Server in Stratos Live. Open your browser and enter https://appserver.stratoslive.wso2.com/t/(your tenant domain).Sign in to the Application Server.




After signing in, go to Manage -> Applications -> List (on the left) and you will see that TestProject has been deployed in the server successfully. The Applications list shows the list of CAR files deployed in the server. 


Next go to Manage -> Web Applications -> List. You will now be able to view the web application 'WebApp' deployed in the server.





5) Deploy the Web Application manually
i)  Package the Web Application inside a CAR file
There are two ways that we can package and deploy the web application. We can simply right click on WebApp -> Export -> WAR file and export the project as a WAR file. However I'll be showing how to package the Web Application as a WAR file within a CAR file

Go to TestProject (the CApp we created). You will find that a link to the WebApp project is created  under artifacts -> carbon-ui -> webapps. Now let's export TestProject as a CAR file. Double click on root-artifact.xml. Select WebApp under Existing Artifacts and click on the Create Archive button on the top right. 



Create and save the CAR file. (Click here for more information on creating CAR files). 


Sign-in with your tenant id and password.



When you go to the Stratos Live manager page. Click on Application Server. The Application Server is where the web app will be hosted.


Go to Manage -> Applications -> Add.


Under Add Applications you will be able to browse for a CAR file. Choose TestProject-1.0.0.car and upload it as shown.






After the success message is displayed go to Manage -> Web Applications -> List as shown below.


You will see that WebApp is now deployed in the Application Server.


Go to Manage -> Web Applications -> List as shown below.  To run/view the application click on 'Go to URL'  for  WebApp as shown below.




You will be taken to the index.jsp page that we created. Enter your name and press the submit button.




Next you will be directed to the WelcomeServlet.java as shown below.



And that's it :). 
Our simple web application is now up and running on Stratos Live. Similarly, you can host any web application of your choice in Stratos Live.

Tuesday, August 30, 2011

WSO2Con 2011 - A Week for the Tech Savvy



It's going to be an amazing week for SOA folks and all techies in general. WSO2Con is a week of tutorials, tech talks by industry experts and networking events. Learn how global enterprises, SaaS providers and innovative startups are using WSO2 platforms to build distributed web apps, java services, bpel flows, Software-as-a-Service (SaaS) and more.


You'll find out what customers have to say about the WSO2 products and how they are used to build their enterprise software in financial services, government, telecom and other industries. You can learn the best practices for enterprise architecture initiatives, how to manage SOA projects and get comprehensive knowledge on a range of middleware technologies that WSO2 has to offer. Lot's to learn from 6 Keynote speakers around the world, a panel of 30 speakers and numerous tutorial sessions in 5 days.  Don't miss out! Check out our speaker panel  and agendaTo register and for more information go to http://wso2.com/events/wso2con-2011-colombo/


Related Blog Posts -

Are you attending WSO2Con 2011? by Sanjiva Weerawarana
WSO2Con is back (2011) by Sumedha Rubasinghe




Friday, August 19, 2011

How To Create And Deploy A CAR File in a Stand-Alone WSO2 Server and WSO2 Stratos

A CAR file, short for Carbon Archive, is just like a zip or jar file tailor-made for the Carbon Platform. It contains 1 or more artifacts and information about them. A CAR file is generated through WSO2 Carbon Studio. To learn more about a CAR file check out WSO2 Carbon Studio in a Nutshell.  

How to Create a CAR File 

For this demonstration, I am using the 1.0.12 version of Carbon Studio. First create a CApp project in Carbon Studio and add your artifacts. I have created a registry resource (AreaService.wsdl) to be deployed in the Governance Registry server, a data service (Dataservice1.dbs) to be deployed in the Data Services Server and a proxy service (proxyService1.xml) to be deployed in the ESB Server. TestApp is the CApp file which contains all of this. 

To create a CAR file, go to your CApp. You will find a file named root-artifact.xml, double click to open it. The 3 artifacts that I created are listed below. Select the artifacts you want to include in the CAR file that you are going to create. Click on the 'Create Archive' button on the top right corner. 
  


The following wizard will be shown to you next. Make any changes if you wish to. Click on Finish. 


After the CAR file is created it will be saved in your file system.


Now, we have a CAR file all ready to be deployed in one or more servers. 


How to Deploy a CAR file in a Stand-Alone WSO2 Server

Go inside one of your server folders (carbon_home folder). I want to deploy my proxy service in the ESB server. Go to the bin folder in carbon_home and start the server.




Log in to the management console. 


You will now see the dashboard. On the left you will see 'Applications' under the 'Manage' tab. Under 'Applications' click on 'Add'. 




You will then see the following page where you will have to browse for your CAR file.




After selecting the CAR file, it will be displayed as in the following image. Click on upload to finish. 




After a successful upload, you will see the following message. 




If you go back to 'Applications' under the 'Manage' tab and click on 'List' you will see the CAR file we just uploaded. 




I uploaded the CAR file to the ESB server. The CAR file contained 3 different types of artifacts that belong to 3 different types of servers. So did the ESB Server pick the right artifact, the proxy service? Go to the 'Manage' tab, under 'Services' go to 'List' and you will see proxyService1 being listed.




So how did the ESB server know what artifact to take? Well, if you go back to your CApp, and if you open your root-artifact.xml file in an xml editor you will see the following. 



As shown in the xml file above, each dependency element has 3 properties. The name, version and the server role. The server role indicates the server to which the artifact belongs. So when the CAR file is deployed in a server, the server looks for artifacts with the same server role as the server. 

How to Deploy a CAR in WSO2 Stratos 

You can take the same CAR file and deploy it in the ESB Server running in Stratos (or any other server).   Log in to your Stratos installation or Stratos Live. I'll be logging into Stratos Live at https://stratoslive.wso2.com/.


After logging in you will be directed to the Stratos home page. 



Click on 'Enterprise Service Bus' and you will see the WSO2 Enterprise Service Bus as a Service interface.  The following image is the Stratos version of the WSO2 ESB home page. You will see the same controls on the left. As done earlier, go to 'Manage' -> 'Applications' -> 'Add' to add the CAR file and follow the same steps. 



When the upload is successful, go to 'Applications' -> 'List' and you will see the CAR file listed.