NBCR_GS_TRAINING

advertisement
GridSphere:Hands-on
Installation and Development
Jason Novotny
Jnovotny@ncmir.ucsd.edu
NBCR Summer Institute 2006
Requirements
You will need the following software packages on you local
machine
Java > 1.4.x (java.sun.com)
Apache Ant > 1.6 (ant.apache.org)
Apache Tomcat > 5.0.x (tomcat.apache.org)
Knowledge in Java programming
Knowledge in Servlet programming
It helps to have a look at the JSR 168 specification
(http://jcp.org/en/jsr/detail?id=168) or read articles on
javaworld.com (http://www.javaworld.com/javaworld/jw-082003/jw-0801-portlet.html) or sun.com
(http://developers.sun.com/prodtech/portalserver/reference/t
echart/jsr168/)
Requirements (II)
For programming part a good Java IDE (please
don’t use vi for real programming)
NetBeans (www.netbeans.org)
Eclipse (www.eclipse.org)
IntelliJ Idea (www.jetbrains.org) commercial
Getting the required software
Tomcat:
http://www.gridsphere.org/gridsphere/NBCR/tomcat.
tar.gz
Ant:
http://www.gridsphere.org/gridsphere/NBCR/ant.tar.
gz
Getting GridSphere
GridSphere is available from
http://www.gridsphere.org/gridsphere/gridsphere?ci
d=download
Download GridSphere 2.2 (we will refer to that
directory from now on as GRIDSPHERE_HOME)
Unzip/Untar it
$CATALINA_HOME needs to be set to the root
directory of tomcat
ANT_HOME needs to be set to the root directory of
ant
export ANT_HOME=
Installing
cd gridsphere
$ANT_HOME/bin/ant install
To enable deploying portlets in a
running GridSphere please modify the
$CATALINA_HOME/conf/tomcatusers.xml file
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="gridsphere" password="gridsphere" roles="manager,admin"/>
</tomcat-users>
$CATALINA_HOME/bin/startup.sh
http://localhost:8080/gridsphere/gridsph
ere
Setup GridSphere
Fill out the GridSphere setup screen
Running GridSphere
Programming
Stop tomcat
($CATALINA_HOME/bin/shutdown.sh)
We will create a ‘Hello World’ Example
Steps involved:
Create the templates
Create the jsp, the portlet code
Modify the descriptor files
Deploy to GridSphere
Change the Code
Redeploy to GridSphere
We going to write a classic HelloWorld
example
All files can be found at
http://www.gridsphere.org/NBCR/workshop/<fi
lename>
Creating template files
GridSphere provides a mechanism to create template
files for your project
It is not required to use but it provides some help
In GRIDSPHERE_HOME run ‘ant new-project’
You will be asked for a project title, enter Hello World
You will be asked for a project name, this will be used for
the webapplication name, enter gsexamples (this will be
used as directory name for your project)
You will be asked whether you want this to be a GS or
JSR portletwebapp, enter jsr
All templatefiles are now created in
GRIDSPHERE_HOME/project/gsexamples
If you made a mistake simply erase the
GRIDSPHERE_HOME/project/gsexamples directory
and start over again
Simple Hello World
create a file in the subdirectory
src/org/gridsphere/gsexamples/portlets
named HelloWorld.java
package org.gridsphere.gsexamples;
import javax.portlet.GenericPortlet;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.PrintWriter;
import java.io.IOException;
public class HelloWorld extends GenericPortlet {
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello World</h1>");
}
}
Deployment descriptors
All files are located in webapp/WEB-INF
We need to edit a couple of deployment descriptor
files
portlet.xml - JSR 168 standard, describing the portlet
layout.xml - GridSphere file, describing the layout of the
portlet within a page
group.xml - GridSphere file, Describing a collection of
portlets
More files are needed but are autogenerated and
need not to be modified for this example
web.xml - Standard web.xml descriptor
gridsphere-portlet.xml - GridSphere specific
PortletServices.xml - For use with GridSphere Portlet
Services
portlet.xml (JSR)
Specifiy the portlet in webapp/WEBINF/portlet.xml
<portlet>
<description xml:lang="en">
The classic Hello World example
</description>
<portlet-name>HelloPortlet</portlet-name>
<display-name xml:lang="en">Hello World</display-name>
<portlet-class>
org.gridsphere.gsexamples.portlets.HelloWorld
</portlet-class>
<expiration-cache>60</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>edit</portlet-mode>
<portlet-mode>help</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title>Hello World</title>
<short-title>Hello World</short-title>
<keywords>hello</keywords>
</portlet-info>
</portlet>
layout.xml (GS)
Specifiy the layout in webapp/WEBINF/layout.xml
<portlet-tabbed-pane>
<portlet-tab label="GridSphere Examples">
<title lang="en">GridSphere Examples</title>
<portlet-tabbed-pane style="sub-menu">
<portlet-tab label="helloworld">
<title lang="en">Hello</title>
<table-layout>
<row-layout>
<column-layout width="100%">
<portlet-frame label="helloportlet">
<portlet-class>
gsexamples#HelloPortlet
</portlet-class>
</portlet-frame>
</column-layout>
</row-layout>
</table-layout>
</portlet-tab>
</portlet-tabbed-pane>
</portlet-tab>
</portlet-tabbed-pane>
group.xml (GS)
Specify the layout in webapp/WEBINF/group.xml to define which portlet belong
together
<?xml version="1.0" encoding="UTF-8"?>
<portlet-group>
<group-name>Hello</group-name>
<group-description>An example group</group-description>
<group-visibility>PUBLIC</group-visibility>
<portlet-role-info>
<portlet-class>gsexamples#HelloPortlet</portlet-class>
<required-role>USER</required-role>
</portlet-role-info>
</portlet-group>
Deployment
Use ‘ant install’ to deploy this portlet to GridSphere
Start GridSphere
Subscribe to the Hello World group in the Profile
Manager
Hello World
Using UI Beans ...
Let’s enhance the example a bit
Make use of the GridSphere UI Visual Beans
Enter a name, say hello or goodbye
Creating the JSP
JSP’s are stored in webapp/jsp subdirectory of
your just created helloworld project
Create a jsp file uihelloworld.jsp there
<%@ taglib uri="/portletUI" prefix="ui" %>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects/>
<ui:form>
<ui:text beanId="hellobye"/> <ui:text beanId="greeting"/> !
<ui:textfield size="20" beanId="name"/>
<ui:actionsubmit action="sayHello" value="Say Hello!"/>
<ui:actionsubmit action="sayGoodBye" value="Say GoodBye!"/>
</ui:form>
Creating the portlet
Create a directory
src/org/gridsphere/gsexamples
/portlets/
Create the following portletcode in
UiHelloWorld.java:
package org.gridsphere.gsexamples.portlets;
import org.gridlab.gridsphere.provider.portlet.jsr.ActionPortlet;
import org.gridlab.gridsphere.provider.event.jsr.RenderFormEvent;
import org.gridlab.gridsphere.provider.event.jsr.ActionFormEvent;
import org.gridlab.gridsphere.provider.event.jsr.FormEvent;
import org.gridlab.gridsphere.provider.portletui.beans.TextBean;
import
org.gridlab.gridsphere.provider.portletui.beans.TextFieldBean;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
UiHelloWorld.java
public class UiHelloWorld extends ActionPortlet {
private static final String DISPLAY_PAGE = "uihelloworld.jsp";
public void init(PortletConfig config)
throws PortletException {
super.init(config);
DEFAULT_VIEW_PAGE = "prepare";
}
private void greet(FormEvent event) {
TextBean hellobye = event.getTextBean("hellobye");
hellobye.setValue("Hello");
TextFieldBean name = event.getTextFieldBean("name");
TextBean greeting = event.getTextBean("greeting");
if (name.getValue()==null) name.setValue("stranger");
greeting.setValue(name.getValue());
}
public void prepare(RenderFormEvent event)
throws PortletException {
greet(event);
setNextState(event.getRenderRequest(), DISPLAY_PAGE);
}
public void sayHello(ActionFormEvent event)
throws PortletException {
greet(event);
setNextState(event.getActionRequest(), DISPLAY_PAGE);
}
public void sayGoodBye(ActionFormEvent event)
throws PortletException {
greet(event);
TextBean hellobye = event.getTextBean("hellobye");
hellobye.setValue("Good Bye");
setNextState(event.getActionRequest(), DISPLAY_PAGE);
}
}
portlet.xml
Edit the <portlet/> section of the portlet.xml
Download as webapp/WEB-INF/portlet-2.xml
...
<portlet>
<description xml:lang="en">
An Ui Hello World example
</description>
<portlet-name>UiHelloWorld</portlet-name>
<display-name xml:lang="en">Ui Hello World</display-name>
<portlet-class>
org.gridsphere.gsexamples.portlets.UiHelloWorld
</portlet-class>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title>Ui Hello World</title>
<short-title>Ui Hello World</short-title>
<keywords>hello, ui, world</keywords>
</portlet-info>
</portlet>
...
layout.xml
Download as webapp/WEB-INF/layout-2.xml
<portlet-tabbed-pane>
<portlet-tab label="GridSphere Examples">
<title lang="en">GridSphere Examples</title>
<portlet-tabbed-pane style="sub-menu">
<portlet-tab label="helloworld">
<title lang="en">Hello</title>
<table-layout>
<row-layout>
<column-layout width="100%">
<portlet-frame label="uihelloportlet">
<portlet-class>gsexamples#UiHelloWorld</portlet-class>
</portlet-frame>
</column-layout>
</row-layout>
</table-layout>
</portlet-tab>
</portlet-tabbed-pane>
</portlet-tab>
</portlet-tabbed-pane>
group.xml
Download as webapp/WEB-INF/group-2.xml
<portlet-group>
<group-name>HelloWorld</group-name>
<group-description>UI Hello World Example</group-description>
<group-visibility>PUBLIC</group-visibility>
<portlet-role-info>
<portlet-class>gsexamples#UiHelloWorld</portlet-class>
<required-role>USER</required-role>
</portlet-role-info>
</portlet-group>
Redeploy Portlet
If you change code you can easily redploy the
portlet
ant install
In GridSphere reload the Hello World portlet app
Deployed portlet!
Click on the ‘Hello’ tab
Enter your name!
Questions ?
Download