Transcription
Advanced JCR Persistence in theGateIn Portal FrameworkJulien VietJUDCon 8th of October 2010Copyright 2010. All rights Reserved, eXo Platform SAS
Agenda GateIn introductionJCR basics with CRaSHChromattic JCR persistence mapperLogomattic a JCR demoCopyright 2010. All rights Reserved, eXo Platform SAS
GateIn and above GateIn 3.0 released in March 2010GateIn 3.1 released in June 2010eXo Social 1.0eXo WCM 2.0eXo KS 2.0eXo Platform 1.0Copyright 2010. All rights Reserved, eXo Platform SAS
Standards, features and technologiesStandards & FeaturesRich UIPortal ialGadgetsJSR 286PortletGateIn PortalWebUIIdentityintegrationWSRPModelObject forPortalJBossPortletBridge reXoJBoss WSeXo JCR /WebDAVChromattic(JCR /Object)Copyright 2010. All rights Reserved, eXo Platform SASRest /JAX-RS
Major features Aggregation via Portlet and GadgetRich User InterfaceJava Content RepositoryInternationalization / RTLManagementIdentity 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 / ShindigCopyright 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 PortletCopyright 2010. All rights Reserved, eXo Platform SAS
Java Content Repository intro A tree of typed items– Organized as a set of Workspace in aRepository– 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 nodeCopyright 2010. All rights Reserved, eXo Platform SAS
Java Content RepositoryCopyright 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 prettymuch like JPA Query support (content is indexed) State change notification support Node versioningCopyright 2010. All rights Reserved, eXo Platform SAS
ChromatticCopyright 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.2Copyright 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 stem.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 calledwith the node type ” typeName);}}Copyright 2010. All rights Reserved, eXo Platform SAS
JCR by nature is not type safepublic void print(Node node) throws RepositoryException {if (node.isNodeType(“nt:file”)) {Node content node.getNode(“jcr:content”);String mimeType “”;if (content.hasProperty(“mimeType”)mimeType stem.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 calledwith the node type ” typeName);}}Copyright 2010. All rights Reserved, eXo Platform SAS
OO programming provides type safetypublic void print(File file) {if (file instanceof Document) {String mimeType (“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 fromdomain modelCopyright 2010. All rights Reserved, eXo Platform SAS
Chromattic classes generation based on APTFile.javaAPTFile Chromattic.javaCompilerFile Chromattic.classFile.classClassloadingClass File extends Class File Chromattic Copyright 2010. All rights Reserved, eXo Platform SAS
Chromattic sNodetypedefsCopyright 2010. All rights Reserved, eXo Platform SASJCR
Entity life cycle ChromatticSession provides support forinteracting with objects life st()destroy()PersistentRemovedCopyright 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 typeCopyright 2010. All rights Reserved, eXo Platform SAS
Multivalued property mapping A JCR property can be multivalued and aremapped either to native array type andjava.util.List java type@Propertyint[] getIntArray();@PropertyInteger[] getIntegerArray();@PropertyList 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@OneToManypublic abstract Collection File getFiles();}Copyright 2010. All rights Reserved, eXo Platform SAS
Node hierarchy inheritance mappingpublic abstract class Path {@ManyToOnepublic 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 {@OneToManypublic abstract Collection Path getChildren();@OneToManypublic abstract Collection File getFiles();@OneToManypublic abstract Collection Folder getFolders();}Copyright 2010. All rights Reserved, eXo Platform SAS
Collection mapping Unordered collection– Collection Path getChildren(); Ordered collection based on JCR nodeorder– 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 isinvolved in a hierarchical relationship with aparentFile 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 itterminates its relationship with its parentFolder 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 willeither move the entity, rename the entityFolder 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 orderedFolder 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 inheritancepublic abstract class Container T {@OneToManypublic abstract Collection T getChildren();}@PrimaryType(name “folder”)public abstract class Folder extends Container Path {}public abstract class IdentityContainer I extendsIdentityObject extends Container I {}Copyright 2010. All rights Reserved, eXo Platform SAS
Relationship types Four kinds of relationship– Hierarchic: the default one– Reference– Path– EmbeddedCopyright 2010. All rights Reserved, eXo Platform SAS
Reference relationships JCR provides references between nodesproviding support for relationships– Reminder: a reference is actually a node propertyof type Reference to another nodepublic 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 relationshipwith the differences– No integrity applies (weak reference) A node can point with an invalid path The identity of the related node is not garanteedCopyright 2010. All rights Reserved, eXo Platform SAS
Embedded relationships Provides a one-to-one relationship to aJava object that models a part of the nodetype– A mixin type– A super type The relationship occurs between two javaobjects exposing a different type of thesame nodeCopyright 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 mixinpublic abstract void setVoteable(Voteable voteable);}Copyright 2010. All rights Reserved, eXo Platform SAS
Embedding a super type The use case is to replace inheritance bydelegation Similar to the mixin type mapping except itexposes one the super type of the primarynode type Provides support for multiple node typeinheritance Main difference is that it is not possible toact 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– QueryCopyright 2010. All rights Reserved, eXo Platform SAS
Node attributes annotationspublic class Foo {// Returns the node id@Idpublic abstract String getId();// Returns the node name@Namepublic abstract String getName();// Returns the path@Pathpublic abstract String getPath();// Returns the workspace name@WorkspaceNamepublic 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 integrationpublic 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 providerCopyright 2010. All rights Reserved, eXo Platform SAS
Roadmap Detached/attached entitiesType safe query builderBean validation integrationObject versioningCopyright 2010. All rights Reserved, eXo Platform SAS
GateIn Frameworks Chromattic and CRaSH are powerful toolsfor building applications on top of GateIn http://www.gatein.org http://chromattic .googlecode.com http://crsh.googlecode.comCopyright 2010. All rights Reserved, eXo Platform SAS
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 persistent folder.getFiles().put("file", file); // That would make it also persistent file.setParent(folder);