Spring Fundamentals and Sakai Fundamentals

Download Report

Transcript Spring Fundamentals and Sakai Fundamentals

Spring Fundamentals
&
RSF Fundamentals
Antranig Basman
[email protected]
Sakai NWU Workshop, South Africa
Creative Commons AttributionNonCommercial-ShareAlike 2.5 License
Sakai Programmers’ Café
The Spring Framework
• The most significant development in
programming in 10 years
• Will only have a lightning tour in this session
• Crucial since
– Is used throughout Sakai as a service location
framework
– Is the basis for the RSF presentation framework
we will be using for tool development
• Is extensively used in the wider world as a
Java “enterprise” technology
• Purpose is to organise and orchestrate the
different parts of an application, which are
packaged as “beans”
2
What is so important about Spring?
• Somewhat hard to convey without seeing it!
• Spring is the first “negative technology”, that
as well as staying invisible in your code, also
works hard to hide dependencies on other
technologies as well
• Solidly separates code from configuration,
making it easy to work with extremely large
applications without getting lost/in a tangle
• Helps you think properly about smaller
applications too!
• Is a deeper idea than it appears, that you will
need a bit of time to settle into
3
Java Beans and Spring
• Beans have been with us since the beginning of Java
(1996 and beyond)
• Almost a non-concept – a bean is a simple Java object
with “getters and setters”
public class MyBean {
private String property;
public void setProperty(String property) {
this.property = property;
}
public String getProperty() {
return property;
}
}
• Spring concept of a bean is not very much more loaded –
however it is imagined that each bean “does some work”
• Setters in Spring are generally much more important
than getters
• In Spring, a Setter is used to deliver a dependency
4
A Simple Spring Bean
Setter method marks
this as a bean The dependency on
myService is
injected
The bean’s business
method (work for its
clients) is defined
here
public class WorkerBean {
private UsefulService myService;
public void setMyService(UsefulService myService) {
this.myService = myService; }
public int doMyWork(int argument) {
int result = myService.invoke(argument);
return result + 3;
}
}
• Notes:
– The whole point of Spring is not to see it
– Spring isn’t just about service location, but
it is one (common) way to use it
5
Spring Configuration for the bean
Injection here
delivers the bean
“myService” to the
setter on the client
<bean id=“usefulService” class=“mypackage.UsefulService”>
....
</bean>
<bean class=“mypackage.WorkerBean”>
<property name=“myService” ref=“usefulService”/>
</bean>
• Notes:
– The “id” attribute is optional, but typically supplied since you
usually want to refer to the bean again
– You can “keep on going”, building a deeper and deeper tree of
clients and dependencies
– The ultimate endpoint of a mature Spring design is to have the
entire application structure in Spring (still a controversial view!)
– The use of Spring in Sakai is typically much “thinner” – there is
ONE clear API/Impl boundary across the server
6
Typical Spring usages
• The Spring configuration is typically written in
an XML file, defining an Application Context
– Recent support for configuration through Java 5
Annotations, but this defeats the whole point!
• Special support for loading in a Servlet
environment, creating a
WebApplicationContext from a file by default
named applicationContext.xml
– Initialised on context startup using Servlet
Listeners defined in web.xml
• Can also use Spring completely “headless”
by creating the application context by hand
– Will see an example tomorrow morning
7
Sakai Services in Spring
• Sakai APIs are defined in terms of Java interfaces
• There is ONE implementation of each API interface in Sakai
• The name of the Spring bean in the Sakai Spring context (file
components.xml) is always the fully qualified interface name
• Classic example: the Sakai UserDirectoryService
<bean id="org.sakaiproject.user.api.UserDirectoryService“
class="org.sakaiproject.user.impl.DbUserService"
init-method="init" destroy-method="destroy"
singleton="true">
......
<property name="autoDdl“ value=“${auto.ddl}”/>
<property name="cacheMinutes“ value=“5”/>
</bean>
• Lots more stuff here! Will learn about this later, but concentrate
on the familiar elements <bean class=, id=> and <property>
• There is ONE global, shared Sakai application context holding
beans for all Sakai services
8
Sakai UserDirectoryService in code
package org.sakaiproject.user.api;
....
/** <p>
* UserDirectoryService manages the end-user modeling for Sakai.
* </p>
*/
public interface UserDirectoryService extends EntityProducer {
....
User getUser(String id) throws UserNotDefinedException;
...
}
• To use this API in your bean, you would write a setter
which accepted a UserDirectoryService object, and
then set up the injection in a Spring XML file
9
RSF
10
Why RSF?
• RSF was designed with several special
requirements of the Sakai community (and other
similar communities) in mind
– The first key point is to decouple the workflows of
developers and designers/UX experts, and allow
them to work independently
– The second key point is to enable universal
portability of apps to whatever environments may
arise, without requiring code changes (today, to
Servlets, Sakai, and JSR-168 Portlets, tomorrow to
JSR-286 or the final “containerless” liberation)
• RSF is closely involved with the FLUID “Flexible
UI” project now starting up at UToronto
11
RSF for coders and designers
• RSF is fun for coders since it is built out of
Spring components
– Can always break open the framework in an
emergency
– Once you get the pattern, it is really obvious how
to build very powerful components and apps that
require a lot more custom/stovepipe work in other
frameworks
• RSF is fun for designers since they can just
work with plain HTML and hand it over the
fence to coders who can start working with it
directly
12
Today’s talk
• A lightning tour of the basics, with
enough “bootstrap” info about RSF
fundamentals to let you understand and
work with basic apps, and tackle the
exercise after the break
• More thorough introduction and survey
tomorrow
13
Let’s get started!
• Here is some real HTML:
<html>
<head><title>RSF sample</title></head>
<body>
<h1>Hello <span rsf:id=“user-name”>User Name</span></h1>
Today is <span rsf:id=“current-date”>1/1/2006</span><br/>
<table>
<tr rsf:id=“item-row:”>
<td rsf:id=“item-value”>item value here</td>
</tr>
</table>
</body>
</html>
• It is also an RSF template!
– To make a template, you simply add the rsf:id
attribute to tags which might have their content
replaced when the app runs, or might need to be
copied or moved about
14
RSF Templates
• The template will load and render fine in any
browser or editor
– Properly, we will always give RSF templates a
proper XHTML doctype, and namespace for the
rsf:id attribute
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:rsf="http://ponder.org.uk/rsf">
– The rsf:id attribute is the ONLY addition to the
schema
– RSF can actually render with any kind of XML
template, not just XHTML
15
Using the Template
• Everything in RSF is a Spring-configured
bean of some kind
• A special kind of bean called a Producer (or
View Producer) is responsible for rendering a
page from a template
– The same Producer can render from many
different templates, as long as the rsf:ids agree
• The purpose of a Producer is to create a
Component Tree of RSF “Primitive
Components” which will get paired up with
the template by the RSF renderer (IKAT)
16
View Producer
public class ItemsProducer implements ViewComponentProducer, DefaultView {
...
private CrudPlusLogic logic;
public void setLogic(CrudPlusLogic logic) {
this.logic = logic;
}
public void fillComponents(UIContainer tofill, ViewParameters viewparams,
ComponentChecker checker) {
UIOutput.make(tofill, "user-name", logic.getUserName());
for (CrudPlusItem item: logic.getAllVisibleItems().iterator()) {
UIBranchContainer itemrow =
UIBranchContainer.make(listform, "item-row:", item.getId());
UIOutput.make(itemrow, “item-value”, item.getValue());
}
UIOutput.make(itemrow, “current-date", new Date().toString() );
}
}
• Java code (a Spring bean) which defines what appears in the
view
• UIOutput and UIBranchContainer are primitive RSF components
• The 2nd arguments to the make() calls match up with the rsf:ids
written in the template
17
RSF rendering basics
• UIOutput – will pair up with ANY tag in the markup.
The 3rd argument will replace the body of the tag.
– rsf:id must not have a colon
• UIBranchContainer – will also pair up with any tag.
Represents a “branch point” in the rendering where
a tag will be copied out again, missed out
completely, or rendered out of order.
– rsf:id must have a colon
• Full details on all the RSF primitive components
(like UILink, UIForm, UICommand) on the RSF wiki
at http://www2.caret.cam.ac.uk/rsfwiki/Wiki.jsp?page=PrimitiveComponents
18
Registering a Producer
• Spring beans typically live as long as the
entire application (application scope)
• RSF extends Spring with a fast request-scope
implementation, RSAC
• RSF Producers are typically declared as
Spring beans at request scope
• Request-scope beans go into
requestContext.xml rather than
applicationContext.xml, but the file format is
the same
• You can refer to any application-scope beans
(including Sakai services) directly as
dependencies of your request-scope beans
19
Registration of the ItemsView
• In WEB-INF/requestContext.xml:
<bean class="org.sakaiproject.crudplus.tool.producers.ItemsProducer">
<property name="logic"
ref="org.sakaiproject.crudplus.logic.CrudPlusLogic" />
</bean>
• Typically no need for an id since RSF detects
and loads up producers by itself
• The one dependency is a bean representing
a Sakai API (service implementation) – not
only at application scope but in the shared
area.
20
The Scene
• This is enough “bootstrap” info about
RSF basics to let you understand and
work with basic apps, and tackle the
exercise after the break
• More thorough introduction and survey
tomorrow
21
Questions?
• RSF wiki, forums and JIRA
– http://www2.caret.cam.ac.uk/rsfwiki
• Spring framework
– http://www.springframework.org/
22
The Scene
• Having set the scene with some general
theory and background, we are set to
continue after lunch with tackling some
practical Sakai development
23