GridSphere Programming Oliver Wehrens Albert Einstein Institut (MPG)

advertisement
GridSphere
Programming
Oliver Wehrens
Louisiana State University - CCT/AEI MPG
Jason Novotny, Michael Russell, Oliver Wehrens
Albert Einstein Institut (MPG)
Overview
JSR 168 Portlets
Software Requirements for GridSphere
Creating a JSR Portlet project in GridSphere
“Hello World”
GridSphere Visual Beans
UI Design
GridSphere services
Code Development
GridPortlets Programming
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
JSR Portlets
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
What is a JSR Portlet?
A portlet is a Java technology-based web
component. It is managed by the portlet
container and processes requests and generates
dynamic content as response. Portlets are used
by portals as pluggable user interface
components.
The content generated by a portlet is called a
fragment. A fragment is a piece of markup (e.g.
HTML, XHTML, WML) adhering to certain rules
and can be aggregated with other fragments into
a complete document. The content of a portlet is
normally aggregated with the content of other
portlets into the portal page.
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet JSR Lifecycle
The portlet container instanciates portlets,
manages their lifecycle and invoking them to
process requests. The lifecycle consists of:
initializing the portlet using using the init method
request processsing
taking the portlet out of service using the destroy method
Request processing is divided into two types
action requests handled through the processAction
method, to perform actions targeted to the portlet
render requests handled through the render method, to
perform the render operation
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet JSR Lifecycle (cont.)
defined in interface javax.portlet.Portlet
public void init(PortletConfig config) throws
PortletException;
public void processAction(ActionRequest request,
ActionResponse response) throws PortletException,
java.io.IOException;
public void render(RenderRequest request,
RenderResponse response) throws PortletException,
java.io.IOException;
public void destroy();
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Generic Portlet
JSR specification provides an abstract
javax.portlet.GenericPortlet which implements
Portlet
JSR portal containers extend from GenericPortlet
to provide own implementation
Provides additional functionality such as:
doView(), doEdit() and do Help() methods for displaying
different PortletModes
handling InitParameters, PortletContext, ResourceBundles
More documentation in the javadoc of
javax.portlet.*
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GridSphere
Development
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GridSphere Requirements
Software
minimum
suggested
Java SDK
1.4
> 1.4.2
Tomcat/Jetty
4.1.18/4.2.x
5.0.x/4.2.x
Jakarta Ant
1.6
1.6
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Getting the Sourcecode
Download the tar ball from the website
Get the newest source from CVS (latest code
from CVS always compiles, if not complain on the
mailinglist)
setenv CVSROOT :pserver:anonymous@cvs.gridsphere.org:
/home/repository
cvs login
cvs co gridsphere
cd gridsphere
cvs -dP
mkdir projects
cd projects
cvs co gridportlets
cvs gridportlets
cvs -dP
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Create a new project
GridSphere provides an ant build script to
generate skeleton for new portal projects
“ant new-project” will ask for a project title (e.g.
Example Portlets), a webapp name (e.g.
exampleportlets) and will create the directory and
file structure in projects/<webapp-name>
Decide between JSR 168 and GridSphere/
WebSphere style portlets, would recommend JSR
168 project
File like web.xml, PortletServices.xml and others
need to be edited to match your services and
portlets!
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Creating Portlets
3 Steps
Create the portlet source code / JSP
Edit the web application configuration files
portlet.xml, layout.xml and group.xml in webapps/WEBINF directory of the project
install portlets to GridSphere using “ant install”
Following code is part of the cvs module
“jsrturorial” available from cvs.gridsphere.org
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
A Simple Hello World
Each Portlet needs to implement 'processAction'
(will be called at any action) and 'doView' (will be
called when needs to be displayed)
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
JSP
JSP is a standard JSP file, note the usage of
variable url it contains the right url to return to
the right portlet
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Configuration files
portlet.xml (JSR)
<portlet>
<description xml:lang="en">The classic Hello World example</description>
<portlet-name>ActionHelloPortlet</portlet-name>
<display-name xml:lang="en">Hello World</display-name>
<portlet-class>org.gridlab.gridsphere.jsrtutorial.portlets.helloworld.ActionHelloWorld</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>
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Configuration files (cont.)
layout.xml (GS) to define the placement of the
portlets in GridSphere, for starters just replace
one of the portlet classes with the one just
created
<portlet-frame>
<portlet-class>jsrtutorial#ActionHelloPortlet</portlet-class>
</portlet-frame>
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Configuration files (cont.)
Group.xml (GS) defines a group of portlets a user
can subscribe to in the subscription portlet
Access can be restricted to different roles like
ADMIN and USER
<portlet-group>
<group-name>JSRTutorial</group-name>
<group-description>JSR Tutorial portlets</group-description>
<group-visibility>PUBLIC</group-visibility>
<portlet-role-info>
<portlet-class>jsrtutorial#ActionHelloPortlet</portlet-class>
<required-role>USER</required-role>
</portlet-role-info>
</portlet-group>
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Output
The HelloWorld Portlet
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GridSphere UI Model
Method 'processAction' method will grow with the
size of the portlet and will have a lot of nasty 'ifthen-else' constructs
GridSphere provides an easy and unified way to
handle HTML forms and elements
This model provides an added-value tag library to
make presentations more like component based
design like Swing
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Visual Beans
Instead of using HTML in Portlet java code, visual
beans provide wrappers around CSS/HTML that
allows portlet developers to modify or create
presentation elements
Beans are identified by tags called “beanId”
which act as a label
Beans are created and updated after each
request and made available to portlet code
Every visual bean has a corresponding UI tag for
JSP pages to produce presentation output
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Visual Beans (cont)
to create/query a TextField in Java
to display it in the JSP:
Standard HTML elements are wrapped as beans:
CheckBoxBeam, CheckBoxBean, TableBean,
HiddenFieldBean, TextFieldBean etc.
A java developer does not have to know about
HTML or CSS, he just needs to handle the beans
and tags
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GS UI JSR Portlet
Need to extend ActionPortlet
Each Portlets needs to have an 'init' method
Other methods will be directly called from an
action of the corresponding Button in the JSP
('showName')
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GS UI Model JSP & Output
By defining the actionsubmit button 'showName'
we define the action to be called in the portlet
code
All code is available in the cvs module 'jsrtutorial'
available at cvs.gridsphere.org
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
UI Design
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
UI Design
U.I. design is communication, not engineering
Often underestimated esp. by programmers
The most important thing to know regarding your
user is that he is not interested in using your
product. He is interested in doing his work, and
your product must help him do it more easily.
Paul Heckel, "The Elements of Friendly Software
Design"
Typical UI Development Cycle
Prototype
Revise
Rewrite
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
UI Design Rules
Keep It Simple
Know who your user is
Make a good first impression
Give the user control
Provide feedback on actions
Don't let the user focus on mechanics
Build a model in the user's mind
Must test on users
Iterate, Iterate, Iterate
http://www.sylvantech.com/~talin/projects/
ui_design.html
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
UI Design Experiences
Clean interfaces let’s a user work more efficient
If he can’t do his job faster with the portal or if
he does not get added value, he will not work
with a portal
Esp. true in the GridWorld, if we can’t deliver
better grid experience the user won’t use it, since
we deal with the power users (using machines
directly) who love the shell and perl scripts
Since the grid is slow it has to be added value
A good UI is a first step to provide a better
experience
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Unified Look & Feel
Users want to have the same look and feel in all
portlets
JSR 168 has about 50 different CSS definition
(PLT.C)
portlet-section-subheader
portlet-section-body
portlet-msg-alert
...
If you write pure HTML output or use your own
taglib make sure to use these CSS
Problems are placement of buttons and messages
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Visual Beans and CSS
GridSphere Visual Beans are using the defined
CSS
They are defined in webapps/gridsphere/themes/
<themename>/css/portlet.css
Setting Style ALERT on a TextBean will use the
portlet-msg-alert alert style from the CSS
This provides an easy way to have a consistent
look and feel throughout the whole portal
Other advantages (of using CSS):
Change one style, all the appearances will change
GridSphere themes can be applied very fast
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet UI Design
Use the GridSphere UI beans
Use tags/html with the Portlet CSS
Place the buttons always at the same place
Give feedback about actions
Example: extend your all your portlet from a BasePortlet
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet UI Design
In Portlet use:
In the JSP use:
You get a consistent
look for showing
feedback to the user
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GridSphere Services
Programming
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
PortletServices
Purpose is to encapsulate business or logic
operations that can be reused across portlets
Minimize logic placed in portlet and make it
available as a reusable library
Services are created and destroyed by a
PortletServiceFactory
PortletServices provide init and destroy lifecycle
methods
“User services” provide method level access
control based on a User’s access control
information
Service tests can be developed and executed
using Junit and Cactus frameworks
Services are defined by a portlet services
descriptor
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet Service Example
Create HelloWorld service that keeps track of
supplied user names.
Create src/services/examples/HelloService.java
with two service methods:
public interface HelloService extends PortletService {
public void addName(String name);
}
public List getNames();
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet Service Example
Create HelloWorld service implementation in src/
services/examples/impl/HelloServiceImpl.java:
public class HelloServiceImpl implements PortletServiceProvider,
HelloService {
private List list = new ArrayList();
public void init(PortletServiceConfig config) { }
public void destroy() { } // for now don’t use
public void addName(String name) {
}
}
public List getNames() {
return list;
}
list.add(name);
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet Service Descriptor
Add service descriptor entry to webapps/WEBINF/PortletServices.xml:
<service>
<name>HelloService</name>
<description>an example service</description>
<interface>services.examples.HelloService</interface>
<implementation>services.examples.impl.HelloServiceImpl
</implementation>
</service>
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Use Portlet Service
The HelloService may now be used from a portlet
A service instance is obtained via PortletContext
public class HelloWorld
extends AbstractPortlet {
public void init(PortletConfig config) throws UnavailableException {
hello = (HelloService)config.getContext().getService(HelloService.class);
}
public void processAction(ActionRequest req, ActionResponse res) {
String name = req.getParameter(“name”);
hello.addName(name);
}
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Available GS Services
UserManagement Service
access additional user information
ChartService
create charts and graphs
uses JFreeChart
PersistenceManagerService
Store data in XML and SQL databases
uses Castor and Hibernate
Most of the functionality shown in the core
portlets is exposed as a reusable Service
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Persistence RDBM Service
PersistenceManager interface
Current implementation is based on Hibernate
Requires to write a hibernate mapping file(s)
Usually it is used by other services and not by
portlets directly
Instantiated by PersistenceManagerFactory
PersistenceManagerFactory.createPersistenceManagerRdb
ms("WEBAPPNAME");
Provides methods from create, restore, updated
and delete of objects in the persistence storage
e.g. pm.delete(object) will delete your object from
persistent storage
If you need more advanced features, need to use
Hibernate directly
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Portlet Code
Development
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Code Development
Use an IDE such as NetBeans, Eclipse or
JetBrains IDEA
refactoring
importing packages mostly works automatically
shows errors while writing code
easier navigation in sourcecode
code completion
...
Use a debugger!
It is possible to debug a tomcat process step by step
Hot code replacement works with JDK > 1.4, edit code
and deploy it at runtime through the debugger (some
limitations apply)
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Code Development (cont.)
Take advantage of the possibility to restart
webapps with GridSphere/Tomcat instead
restarting the whole container
Write documentation!
Javadoc all your interfaces as you write (at least
if you expect other users to use it)
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GridPortlets
Programming
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GridPortlets
GridPortlets is based on the older IBM Websphere
API Model but can be used from JSR Portlets and
Services
Some limitations exists such as the UserObject is
not present in the JSR specification
Need to create some helper method to get
around that
Planned for 2005 to convert GridPortlets to JSR
(and is in beta right now!)
Following part will show how to use the
GridPortlets API and it is taken from
'CactusCodePortlets' to support the Cactus
Usercommunity
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Typical Tasks
Need credential
Locate executable
Add parameters and parameterfiles
Choose machine to run on
Transfer executable & parameterfiles to the
selected resource
Execute job
Get Status on job
Transfer StdOut/StdErr and other outputfiles
Everything was seems easy on local machines
maybe not on the Grid (e.g. mkdir -p) !
All codesamples are contained within GridPortlets
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
GridPortlet Services
Everything is wrapped up in services
CredentialManagerService
JobSubmissionService
UserManagementService
...
To use these any portlet or other service has to
instantiate the needed services
public void init(PortletConfig config) throws PortletException {
super.init(config);
try {
ums = (UserManagerService)createPortletService(UserManagerService.class);
cms = (CredentialManagerService)createPortletService(CredentialManagerService.class);
crs = (CredentialRetrievalService)createPortletService(CredentialRetrievalService.class);
} catch (PortletServiceException e) {
log.error("Unable to initalize Credential/UsermanagerService."+e);
}
}
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Credentials
CredentialManagment has function to
create
activate
deactivate
delete
...
To check if a user has a credential
public boolean userHasCredentials(String username) {
User user = ums.getUserByUserName(username);
boolean answer = true;
if (cms.getActiveCredentials(user).size() == 0) {
answer = false;
}
return answer;
}
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Listing Files
To get a filelisting use the FileBrowserService
1
2
3
4
FileBrowser fileBrowser = fileBrowserService.createFileBrowser(user,
fileHostName);
FileListing fileListing = fileBrowser.list(filePath);
fileListing.waitFor();
List fileLocations = fileListing.getFileLocations();
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Directory operations
Get a user's homedirectory
1 FileBrowser fileBrowser =
fileBrowserService.createFileBrowser(user,
fileHostName);
2 String homeDir = fileBrowser.getHomeDirectory();
Create a directory
1 FileBrowser fileBrowser =
fileBrowserService.createFileBrowser(user,
fileHostName);
2 FileMakeDir makeDir = fileBrowser.makeDirectory(dstPath);
3 makeDir.waitFor();
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Copy files
FileBrowserServices provides needed methods
1 FileBrowser srcBrowser =
fileBrowserService.createFileBrowser(user,
srcHostName);
2 FileLocation srcLocation =
srcBrowser.createFileLocation(srcPath);
3 FileBrowser dstBrowser =
fileBrowserService.createFileBrowser(user,
dstHostName);
4 FileBrowser dstLocation =
fileBrowser.createFileLocation(dstPath);
5 FileCopy copy = srcFileBrowser.copy(srcLocation,
dstLocation);
7 copy.waitFor();
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Move Files
Very similiar to FileCopy
1 FileBrowser srcBrowser =
fileBrowserService.createFileBrowser(user,
srcHostName);
2 List srcLocations = // ... create or obtain some file
location list (such as one of list tasks above)
3 FileLocationSet srcFileSet =
srcBrowser.createFileLocationSet(srcLocations);
4 FileBrowser dstBrowser =
fileBrowserService.createFileBrowser(user,
dstHostName);
5 FileBrowser dstLocation =
fileBrowser.createFileLocation(dstPath);
6 FileMove move = srcFileBrowser.move(srcFileSet,
dstLocation);
7 move.waitFor();
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Submit a Job I
Defining a method to submit a Job to a remote
machine
public Job submitSimulation(User user,
String hostName,
int numCpus,
String directory,
FileLocation executable,
FileLocation parameterFile) throws
PortletException {
Parameter: userinformation, hostname, number
of cpu's, directory, executable and parameterfile
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Submit a Job II
JobSpec holds a specification for a Job
Job job = null;
try {
// Create the job spec
log.debug("Creating job spec for " + user.getUserName());
JobSpec jobSpec =
jobSubmissionService.createJobSpec(JobType.INSTANCE);
jobSpec.setUser(user);
// Set the directory
jobSpec.setDirectory(directory);
// Set the executable location
// The executable could be on the local host
// on the same host as job, or some other host
jobSpec.setExecutableLocation(executable);
Can also set the location of stdOut/Err
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Adding paramters
Very easy to add paramters and parameterfiles
If parameterfile needs to be copied to the
machine just add it as 'staged' parameter, will be
staged to the same directory where the job will
be run
set the Host and # of cpu's used
// Add the parameter file name as an argument
jobSpec.addArgument(parameterFile.getFileName());
// Add the parameter file as a file stage parameter
jobSpec.addFileStageParameter(parameterFile);
// Set host name
jobSpec.setHostName(hostName);
// Assume we want mpi job if num cpus > 1
if (numCpus > 1) {
jobSpec.setExecutionMethod(ExecutionMethod.MPI);
jobSpec.setCpuCount(new Integer(numCpus));
}
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Submitting the Job
Use the JobSubmissionsService to submit the job
// Submit the job
job = jobSubmissionService.submitJob(jobSpec);
} catch (JobException e) {
log.error("Unable to submit job", e);
throw new PortletException(e.getMessage());
}
return job;
}
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
JobSubmission - after submit
JobObject provides addtl. methods to get
information about the job after it is submitted
JobId
JobStatus (submitted, waiting, running, finished)
JobHostname
...
If StdErr/Out was transferred to the portal it can
be shown (during and after job execution)
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Grid Functionality
Credential Retrieval & Management
Support for retrieval & storing with MyProxy with Java CoG 1.1
Resource Registry & Management
Resources can be described in XML and online
Resource Information Gathering
Partial resource discovery and update with MDS2
Full resource discovery and update with iGrid 3.1b (from
GridLab)
Job Submission & Migration
GRAM job submission with Java CoG 1.1
Job submission and migration with Grid Resource Management
System (GRMS) 1.9
Physical File Browsing
Support for GridFTP with Java CoG 1.1
Logical File Management
Support for GridLab Logical File Management System
(GLFMS) 1.0
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Example: CactusCodePortlets
We use the API to support our Cactus Community
The projects helps the users to deploy the code
on remote machines (getting it from CVS) and
create executables
Manage Parameter and Parameterfiles
Run Jobs with these executables
Manage the output on the machines
First prototype took two weeks to write
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Join our Mailinglists!
All mailinglists are available on https://
www.gridlab.org/mailman/listinfo
gridsphere-users@gridlab.org
GridSphere Users Mailinglist
gridsphere-dev@gridlab.org
Developer Mailinglist for GridSphere
gridportlets-users@gridlab.org
Users Mailinglist for GridPortlets
gridportlets-dev@gridlab.org
Developer Mailinglist for
GridPortlets
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Q&A
Questions ?
Oliver Wehrens (oliver.wehrens@aei.mpg.de); 2002-2005 GridSphere Team
Download