advanced jcr persistence in the gatein portal framework · advanced jcr persistence in the gatein...

44
Copyright © 2010. All rights Reserved, eXo Platform SAS Advanced JCR Persistence in the GateIn Portal Framework Julien Viet JUDCon 8 th of October 2010

Upload: nguyenthien

Post on 13-May-2018

248 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Advanced JCR Persistence in the GateIn Portal Framework

Julien Viet JUDCon 8th of October 2010

Page 2: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Agenda

•  GateIn introduction •  JCR basics with CRaSH •  Chromattic JCR persistence mapper •  Logomattic a JCR demo

Page 3: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

GateIn and above

•  GateIn 3.0 released in March 2010 •  GateIn 3.1 released in June 2010 •  eXo Social 1.0 •  eXo WCM 2.0 •  eXo KS 2.0 •  eXo Platform 1.0

Page 4: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Standards, features and technologies

Standards & Features

GateIn Portal

JBoss eXo

Identity integration WSRP

Model Object for

Portal

Gadget Server

Portlet Container

JBoss Identity

eXo JCR / WebDAV

Chromattic (JCR / Object)

JBoss WS

WebUI

JSR 286 Portlet

Java Content

Repository

Remote Portlets

Rich UI Portal / Portlet

Opensocial Gadgets

Portlet Bridge /

Richfaces

Rest / JAX-RS

Page 5: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Major features

•  Aggregation via Portlet and Gadget •  Rich User Interface •  Java Content Repository •  Internationalization / RTL •  Management •  Identity integration (Database, LDAP) •  SSO integration (SSO, CAS, OpenSSO)

Page 6: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Content aggregation

•  Based on standards and specifications – Portlet 2.0/ GateIn Portlet Container – OpenSocial Gadgets / Shindig

Page 7: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Portal development

•  Portlet bridges – JSF (JSR 301) – Struts 1 & Struts 2 – Apache Tapestry – Apache Wicket

•  Portlet frameworks – Spring MVC Portlet – Grails Portlet

Page 8: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Java Content Repository intro

•  A tree of typed items – Organized as a set of Workspace in a

Repository – Item have a path – Node contains items – Property contain data

•  Simple type: String, Long, Double, Calendar, Boolean)

•  Binary streams •  Reference: a reference (UUID) to another node •  Name: a reference (path) to another node

Page 9: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Java Content Repository

Page 10: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Java Content Repository quickly

•  Two kinds of node types – Primary node type – Mixin node type

•  Session based programming model pretty much like JPA

•  Query support (content is indexed) •  State change notification support •  Node versioning

Page 11: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Chromattic

Page 12: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Chromattic project

•  Developed for GateIn persistence •  Hosted on Google Code under LGPL •  Status:

– 1.0 used in GateIn 3.0 and 3.1 – 1.1 scheduled for GateIn 3.2

Page 13: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

What’s wrong with? public void print(Node node) throws RepositoryException {if (node.isNodeType(“nt:file”)) { Node content = node.getNode(“jcr:content”); String mimeType = “”; if (content.hasProperty(“mimeType”) mimeType = content.getProperty(“mimeType”).getString(); System.out.println(“File” + node.getName() + “:” + mimeType); } else if (node.isNodeType(“nt:folder)) { System.out.println(“Folder “ + node.getName()); Iterator i = node.getNodes(); while (i.hasNext()) { Node child = (Node)i.next(); print(node); } else { throw new IllegalArgumentException(“print should not be called

with the node type ” + typeName); }}

Page 14: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

JCR by nature is not type safe public void print(Node node) throws RepositoryException {if (node.isNodeType(“nt:file”)) { Node content = node.getNode(“jcr:content”); String mimeType = “”; if (content.hasProperty(“mimeType”) mimeType = content.getProperty(“mimeType”).getString(); System.out.println(“File” + node.getName() + “:” + mimeType); } else if (node.isNodeType(“nt:folder)) { System.out.println(“Folder “ + node.getName()); Iterator<?> i = node.getNodes(); while (i.hasNext()) { Node child = (Node)i.next(); print(node); } else { throw new IllegalArgumentException(“print should not be called

with the node type ” + typeName); }}

Page 15: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

OO programming provides type safety public void print(File file) { if (file instanceof Document) { String mimeType = file.getContent().getMimeType

(); System.out.println(“File “ + file.getName() + “

“ + mimeType; else { Folder folder = (Folder)file; System.out.println(“Folder “ + folder.getName(); for (File child : folder.getChildren()) print(child); } }

Page 16: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Chromattic

•  Provides a type safe object model for JCR –  Data + code –  Refactoring friendly

•  Enable rapid development of rich models •  Support JCR specific features

– multiple inheritance – mixins

•  Generation of node type definitions from domain model

Page 17: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Class  loading  

Compiler  

Chromattic classes generation based on APT

File.java File_Chromattic.java

File.class File_Chromattic.class

Class<File> Class<File_Chromattic>

APT

<<extends>>

Page 18: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Chromattic concepts

Chroma'c  Session  

build() Chroma'c  Builder  

Chroma'c  

Chroma'c  

classes  

Meta  Model  

Objects  

open()

JCR  

Node  type  defs  

Java  classes  

Page 19: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Entity life cycle

Transient   Persistent   Removed  

Chroma'c  Session  

create()

destroy()

persist()

insert()

•  ChromatticSession provides support for interacting with objects life cycle

Page 20: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Property mapping

•  Java types are mapped to JCR property types – String as JCR String – Integer/int/Long/long as JCR Long – Boolean/boolean as JCR Boolean – Float/float/Double/double as JCR Double – java.util.Date as JCR Calendar – InputStream as JCR Binary

•  Java enum type mapped to JCR String type

Page 21: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Multivalued property mapping

•  A JCR property can be multivalued and are mapped either to native array type and java.util.List java type

@Property int[] getIntArray();

@Property Integer[] getIntegerArray();

@Property List<Integer> getIntegerList();

Page 22: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Node hierarchy mapping @PrimaryType(name=“folder”)public abstract class Folder {

// Returns the files in the folder @OneToMany public abstract Collection<File> getFiles();}

Page 23: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Node hierarchy inheritance mapping

public abstract class Path { @ManyToOne public abstract Folder getFolder(); }

@PrimaryType(name=“file”)public abstract class File extends Path { …}

Page 24: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Node hierarchy inheritance mapping @PrimaryType(name=“folder”)public abstract class Folder extends Path {

@OneToMany public abstract Collection<Path> getChildren();

@OneToMany public abstract Collection<File> getFiles();

@OneToMany public abstract Collection<Folder> getFolders();}

Page 25: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Collection mapping

•  Unordered collection – Collection<Path> getChildren();

•  Ordered collection based on JCR node order – List<Path> getChildren();

•  Associative collection – Map<String, Path> getChildren();

Page 26: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Relationship and life cycle

•  Transient entity becomes persistent when it is involved in a hierarchical relationship with a parent

File file = session.create(File.class);Folder folder = session.findByPath(Folder.class,

“folder);

// Becomes persistentfolder.getFiles().put(“file”, file);

// That would make it also persistentfile.setParent(folder);

Page 27: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Relationship and life cycle

•  A persistent entity becomes removed when it terminates its relationship with its parent

Folder folder = session.findByPath(Folder.class, “folder);

File file = folder.getFiles().get(“file);

// The file becomes removedfile.setParent(null)

// That would also remove itfolder.getFiles().remove(“file”);

Page 28: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Relationship and life cycle

•  Adding a persistent entity to a collection will either move the entity, rename the entity

Folder folder1 = …Folder folder2 = …File file = folder1.get(“file”);

// Renames the filefolder1.put(“foo”, file)

// Moves the file to a new parentfolder2.put(“foo”, file);

Page 29: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Collection ordering with a list

•  A JCR node can have its children ordered

Folder folder = session.findByPath(Folder.class, “folder”);

List<File> files = folder.getFiles();

// Moves the first file to the last positionFile f = files.get(0);files.add(files.size()-1, f);

Page 30: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Parameter type and generics inheritance public abstract class Container<T> { @OneToMany public abstract Collection<T> getChildren();}

@PrimaryType(name=“folder”)public abstract class Folder extends Container<Path> {}

public abstract class IdentityContainer<I extends IdentityObject> extends Container<I> {

}

Page 31: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Relationship types

•  Four kinds of relationship – Hierarchic: the default one – Reference – Path – Embedded

Page 32: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Reference relationships

•  JCR provides references between nodes providing support for relationships – Reminder: a reference is actually a node property

of type Reference to another node

public abstract class File extends Path { @MappedBy(“target”) @OneToMany(type=RelationshipType.REFERENCE) public abstract Collection<Link> getLinks();}public abstract class Link extends Path { @MappedBy(“target”); @ManyToOne(type=RelationshipType.REFERENCE) public abstract File getTarget();}

Page 33: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Path relationship

•  Similar to reference based relationship with the differences – No integrity applies (weak reference)

•  A node can point with an invalid path •  The identity of the related node is not garanteed

Page 34: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Embedded relationships

•  Provides a one-to-one relationship to a Java object that models a part of the node type – A mixin type – A super type

•  The relationship occurs between two java objects exposing a different type of the same node

Page 35: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Mixin types // Voterable is a java type that maps a java class to a// JCR mixin type

@MixinType(name=“voteable”)public abstract class Voteable {

public abstract int getVoteCount(); public abstract void setVoteCount(int value);

public void vote() { setVoteCount(getVoteCount() + 1); }}

Page 36: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Embedding a mixin type @PrimaryType(name=“page”)public abstract class Page {

// Returns null if the mixin is not present @OneToOne(type=RelationshipType.EMBEDDED) public abstract Voteable getVoteable();

// Controls the life cycle of the mixin public abstract void setVoteable(Voteable voteable);}

Page 37: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Embedding a super type

•  The use case is to replace inheritance by delegation

•  Similar to the mixin type mapping except it exposes one the super type of the primary node type

•  Provides support for multiple node type inheritance

•  Main difference is that it is not possible to act on the life cycle (for obvious reasons)

Page 38: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Embedding a super type @PrimaryType(name=“describable)public abstract class Describable { @Property(name=“description”); public abstract String getDescription(); public abstract void setDescription(String desc);}

@PrimaryType(name=“page”)public abstract class Page extends Describable { @Owner @OneToOne(type=RelationshipType.EMBEDDED); Describable getDescribable(); // There is not setter!}

Page 39: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Interacting with Chromattic session

•  Find node by – Id – Path – Query

Page 40: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Node attributes annotations public class Foo { // Returns the node id @Id public abstract String getId();

// Returns the node name @Name public abstract String getName();

// Returns the path @Path public abstract String getPath();

// Returns the workspace name @WorkspaceName public abstract String getWorkspaceName();}

Page 41: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Eventing

•  The session provides eventing for state changes –  Entity life cycle changes –  State changes (properties)

•  Enables dependency injection integration

public class InjectingListener implements LifeCycleListener {

public created(Object o) { if (o instanceof FrameworkObject) { ((FrameworkObject)o).setFramework(framework); } }

…}

Page 42: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Chromattic 1.1

•  Groovy integration •  @Inject integration •  Improved property type support

– byte[], Calendar – pluggable property type provider

Page 43: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

Roadmap

•  Detached/attached entities •  Type safe query builder •  Bean validation integration •  Object versioning

Page 44: Advanced JCR Persistence in the GateIn Portal Framework · Advanced JCR Persistence in the GateIn Portal Framework ... – Grails Portlet . ... eXo Platform SAS Java Content Repository

Copyright © 2010. All rights Reserved, eXo Platform SAS

GateIn Frameworks

•  Chromattic and CRaSH are powerful tools for building applications on top of GateIn

•  http://www.gatein.org •  http://chromattic .googlecode.com •  http://crsh.googlecode.com