advanced jcr persistence in the gatein portal framework · advanced jcr persistence in the gatein...
Post on 13-May-2018
248 Views
Preview:
TRANSCRIPT
Copyright © 2010. All rights Reserved, eXo Platform SAS
Advanced JCR Persistence in the GateIn Portal Framework
Julien Viet JUDCon 8th of October 2010
Copyright © 2010. All rights Reserved, eXo Platform SAS
Agenda
• GateIn introduction • JCR basics with CRaSH • Chromattic JCR persistence mapper • Logomattic a JCR demo
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
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
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)
Copyright © 2010. All rights Reserved, eXo Platform SAS
Content aggregation
• Based on standards and specifications – Portlet 2.0/ GateIn Portlet Container – OpenSocial Gadgets / Shindig
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
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
Copyright © 2010. All rights Reserved, 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
Copyright © 2010. All rights Reserved, eXo Platform SAS
Chromattic
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
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); }}
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); }}
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); } }
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
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>>
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
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
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
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();
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();}
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 { …}
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();}
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();
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);
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”);
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);
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);
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> {
}
Copyright © 2010. All rights Reserved, eXo Platform SAS
Relationship types
• Four kinds of relationship – Hierarchic: the default one – Reference – Path – Embedded
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();}
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
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
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); }}
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);}
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)
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!}
Copyright © 2010. All rights Reserved, eXo Platform SAS
Interacting with Chromattic session
• Find node by – Id – Path – Query
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();}
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); } }
…}
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
Copyright © 2010. All rights Reserved, eXo Platform SAS
Roadmap
• Detached/attached entities • Type safe query builder • Bean validation integration • Object versioning
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
top related