Spring Jcr Extension

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    Spring Jcr Extension - Presentation Transcript

    1. Everything is content Spring JCR (JSR170) Salvatore Incandela Spring Framework Meeting 31 Ottobre 2009
    2. Agenda • Introduzione ai Content Repository. • Java Content Repository API. • Repository Model. • Esempio pratico. • Vantaggi Spring JCR. • Links.
    3. Introduzine ai content repository Caratteristiche di File system e database ●Versioning ●Controllo accessi ●Classifcazione contenuti ●Monitoraggio eventi ●Ricerche testuali
    4. Java Content Repository API Con API proprietarie
    5. Java Content Repository API Con JCR
    6. Java Content Repository API Why JCR 170? Swappability API semplici Più di 800 vendor sul Standard API mercato Riduzione costi
    7. Java Content Repository API •Level 1 • Traversing tree • Getting value of properties • Namespace remapping (transient) • Export to XML • Query by Xpath
    8. Java Content Repository API • Level 2 • Adding, removing items • Writing to properties • Persistent namespace changes • Import from XML • Assigning node types to nodes
    9. Java Content Repository API •Transactions (JTA) •Versioning •Observation (events) •Locking •SQL syntax for queries
    10. Java Content Repository API
    11. Repository model Il content model è costituito da più workspace, ognuno di essi contiene un albero composto da più item (nodi o property). Nodo : one parent more child Property: one parent no child
    12. Repository model Navigazione dell'albero Percorso xpath Absolute : /a/b/c Relative: ../../b/c UUID
    13. Repository model Nodo ●Ogni nodo ha un primary type ● jcr:primaryType ●Più mixing types, opzionali contenuti nella proprietà jcr:mixingTypes ● mix:versionable ● mix:lockable ● mix:referencable ●Possono avere fratelli uguali ● /nodo/nodofglio[2]
    14. Repository model Tipi di nodo Un content repository deve supportare il tipo nt:base Può inoltre supportare dei tipi opzionali –nt:unstructured –nt:hierarchyNode (sottotipi: nt:fle,nt:folder) –nt:resource –nt:version, nt:versionHistory –nt:query
    15. Repository model Tipi di nodo Un content repository deve supportare il tipo nt:base Può inoltre supportare dei tipi opzionali –nt:unstructured –nt:hierarchyNode (sottotipi: nt:fle,nt:folder) –nt:resource –nt:version, nt:versionHistory –nt:query
    16. Repository model NodeTypeName mix:lockable Supertypes [] Tipi di nodo IsMixin true HasOrderableChildNodes false NodeTypeName PrimaryItemName nt:file null Supertypes PropertyDefinition nt:hierarchyNode Name jcr:lockOwner IsMixin RequiredType STRING false ValueConstraints [] HasOrderableChildNodes DefaultValues null false AutoCreated false PrimaryItemName Mandatory false OnParentVersion IGNORE jcr:content Protected true ChildNodeDefinition Multiple false Name jcr:content PropertyDefinition RequiredPrimaryTypes [nt:base] Name jcr:lockIsDeep DefaultPrimaryType null RequiredType BOOLEAN AutoCreatefalse ValueConstraints [] Mandatory true DefaultValues null AutoCreatefalse OnParentVersion COPY Mandatory false Protected false OnParentVersion IGNORE SameNameSiblings false Protectedtrue Multiple false
    17. Repository model Proprietà – LONG, DATE, STRING, DOUBLE, BOOLEAN – PATH: percorso nel workspace – REFERENCE: contiene l'UUID del nodo (mix:referenceable) – NAME: nome del tipo di nodo Impostare un valore null ad una property equivale ad eliminarla
    18. Repository model Namespace Il nome di una property come in xml può essere preceduto da un prefsso (namespace): ●jcr (eg. jcr:primaryNode) ●nt – per i tipi di nodi (es. nt:version) ●mix – per i mixing types (es. mix:versionable) ●xml – per la compatibilità con xml
    19. Esempi Eseguire una query Workspace workspace = session.getWorkspace(); Node node = session.getRootNode(); workspace.getNamespaceRegistry().registerNamespace("pnx", "http://pronetics.jcrexample/1.0"); QueryManager queryManager = workspace.getQueryManager(); Query query = queryManager.createQuery("//pnx:rubrica/pnx:contact[@pnx:name='christian')]", Query.XPATH); QueryResult queryResult = query.execute(); NodeIterator nodeIterator = queryResult.getNodes(); Esportare File file = new File("backup.xml"); FileOutputStream fileOutputStream = new FileOutputStream(file); session.exportSystemView("/pnx:rubrica", fileOutputStream, false, false); Importare File file = new File("backup.xml"); FileInputStream fileInputStream = new FileInputStream(file); session.importXML("/", fileInputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
    20. Esempio pratico Considerato l'albero in fgura
    21. Esempio pratico Autenticazione al repository //Get repository Repository repository = (Repository) ctx.lookup("myrepo"); //Get Credentials Credentials credentials = new SimpleCredentials("MyName", "MyPassword".toCharArray()); //Get Session Session mySession = repository.login(credentials, "MyWorkSpace"); Traversal access di una property Node root = mySession.getRootNode(); Node myNode = root.getNode("a/e"); //mySession.getNodeByUUID("123456789") Property property = myNode.getProperty("k"); Value value = property.getValue(); double myDouble = value.getDouble();
    22. Esempio pratico Proviamo a creare un nodo Session mySession; Node root = mySession.getRootNode(); Node myNode = root.getNode("a/e"); myNode.addNode("Y"); session.save(); Per eliminare una property Session mySession; Node root = mySession.getRootNode(); Node myNode = root.getNode("a/e"); myNode.setProperty("p", (Value) null); session.save();
    23. Esempio pratico Inserimento e stampa property Repository r = new TransientRepository(); Session session = r.login(new SimpleCredentials("pippo", "pluto".toCharArray())); try { Workspace workspace = session.getWorkspace(); Node node = session.getRootNode(); workspace.getNamespaceRegistry().registerNamespace("pnx", "http://pronetics.jcrexample/1.0"); Node rubrica = node.addNode("pnx:rubrica"); Node contact = rubrica.addNode("pnx:contact"); contact.setProperty("pnx:name", "christian"); contact.setProperty("pnx:tel", "000000"); contact.setProperty("pnx:category", new String[] { "professional", "metal" }); // IMPORTANTE session.save(); rubrica = node.getNode("pnx:rubrica"); node.getProperty("pnxs:rubrica/pnx:contact[1]/pnx:name"); NodeIterator nodeIterator = node.getNodes(); while (nodeIterator.hasNext()) { Node cnt = nodeIterator.nextNode(); PropertyIterator propertyIterator = cnt.getProperties(); while (propertyIterator.hasNext()) { Property property = propertyIterator.nextProperty(); LOG.info("Property name: {} property value: {}", property.getName(), property.getValue()); } } } finally { session.logout(); }
    24. Session Factory L'interfaccia SessionFactory descrive come ottenere una session, agisce come wrapper del javax.jcr.Repository. Per ottenere una sessionFactory abbiamo bisogno del repository bean e delle credenziali: <bean id="jcrSessionFactory" class="org.springframework.extensions.jcr.JcrSessionFactory"> <property name="repository" ref="repository"/> <property name="credentials"> <bean class="javax.jcr.SimpleCredentials"> <constructor-arg index="0" value="bogus"/> <constructor-arg index="1" value="pass"/> </bean> </property> </bean> JcrSessionFactory permette inoltre la registrazione di namespaces, agiungere listener per ulteriori informazioni fare riferimento ai javadoc.
    25. Namespace registration Per effettuare la registrazione di namespaces custom basta semplicemente passarli come property, la key rappresenta il prefsso e il valore rappresenta il namespace: <bean id="sessionFactory" class="org.springframework.extensions.jcr.JcrSessionFactory"> ... <property name="namespaces"> <props> <prop key="foo">http://bar.com/jcr</prop> <prop key="hocus">http://pocus.com/jcr</prop> </props> </property> </bean> Modalità di registrazione: ●ForceNamespacesRegistration ●KeepNewNamespaces ●skipExistingNamespaces
    26. Event listeners La registrazione dei listener permette di identifcare il path del nodo ascoltato, o una espressione regolare <bean id="sessionFactory" class="org.springframework.extensions.jcr.JcrSessionFactory"> ... <property name="eventListeners"> <list> <bean class="org.springframework.extensions.jcr.EventListenerDefinition"> <property name="listener"> <bean class="org.springframework.extensions.examples.jcr.DummyEventListener"/> </property> <property name="absPath" value="/rootNode/someFolder/someLeaf"/> </bean> </list> </property> </property>
    27. NodeTypeDefinition La registrazione dei tipi di nodo è tipica del CR che stiamo usando, in questo caso JackRabbit: <bean id="jackrabbitSessionFactory" class="org.springframework.extensions.jcr.jackrabbit.JackrabbitSessionFactory"> ... <property name="nodeDefinitions"> <list> <value>classpath:/nodeTypes/wikiTypes.cnd</value> <value>classpath:/nodeTypes/clientATypes.cnd</value> </list> </property> </bean>
    28. Esempio SpringJcr Inserimento e stampa property //Repository r = new TransientRepository(); //Session session = r.login(new SimpleCredentials("pippo", "pluto".toCharArray())); //try { Workspace workspace = session.getWorkspace(); Node node = session.getRootNode(); //workspace.getNamespaceRegistry().registerNamespace("pnx", "http://pronetics.jcrexample/1.0"); Node rubrica = node.addNode("pnx:rubrica"); Node contact = rubrica.addNode("pnx:contact"); contact.setProperty("pnx:name", "christian"); contact.setProperty("pnx:tel", "000000"); contact.setProperty("pnx:category", new String[] { "professional", "metal" }); // IMPORTANTE session.save(); rubrica = node.getNode("pnx:rubrica"); node.getProperty("pnxs:rubrica/pnx:contact[1]/pnx:name"); NodeIterator nodeIterator = node.getNodes(); while (nodeIterator.hasNext()) { Node cnt = nodeIterator.nextNode(); PropertyIterator propertyIterator = cnt.getProperties(); while (propertyIterator.hasNext()) { Property property = propertyIterator.nextProperty(); LOG.info("Property name: {} property value: {}", property.getName(), property.getValue()); } } //} finally { // session.logout(); //}
    29. JcrTemplate e JcrCallback La maggior parte del lavoro con JCR viene svolta dal Jcrtemplate stesso. Il template richiede una sessionFactory e può essere confgurato in modo da creare nuove sessioni on demand o riusarle. <bean id="jcrTemplate" class="org.springframework.extensions.jcr.JcrTemplate"> <property name="sessionFactory" ref="sessionFactory"/> <property name="allowCreate" value="true"/> </bean> JcrTemplate contiene molte delle funzioni di javax.jcr.Session e javax.jcr.query.Query. Nel caso in cui queste non siano suffcienti interviene JcrCallback che opera direttamente con la sessione. JcrCallback è thread-safe, apre e chiude sessioni. public void saveSmth() { template.execute(new JcrCallback() { public Object doInJcr(Session session) throws RepositoryException { Node root = session.getRootNode(); log.info("starting from root node " + root); Node sample = root.addNode("sample node"); sample.setProperty("sample property", "bla bla"); log.info("saved property " + sample); session.save(); return null; } }); }
    30. DAO senza callback E' possibile utilizzare JcrDaoSupport analogamente a JdbcDaoSupport. public class ProductDaoImpl extends JcrDaoSupport { public void saveSmth() throws DataAccessException, MyException { Session session = getSession(); try { Node root = session.getRootNode(); log.info("starting from root node " + root); Node sample = root.addNode("sample node"); sample.setProperty("sample property", "bla bla"); log.info("saved property " + sample); session.save(); return null; } catch (RepositoryException ex) { throw convertJcrAccessException(ex); } } }
    31. Finish Q/A
    32. Link utili JSR-170 http://www.jcp.org/en/jsr/detail?id=170 Jackrabbit http://jackrabbit.apache.org/ Versione html della specifca http://www.day.com/specs/jcr/1.0/ Spring JCR http://se-jcr.sourceforge.net/ Author www.twitter.com/sincandela salvatoreincandela.blogspot.com www.linkedin.com/in/salvatoreincandela

    + Salvatore IncandelaSalvatore Incandela, 3 months ago

    custom

    762 views, 2 favs, 3 embeds more stats

    Everything si content. La produzione frenetica di c more

    More info about this presentation

    © All Rights Reserved

    • Total Views 762
      • 642 on SlideShare
      • 120 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 12
    Most viewed embeds
    • 84 views on http://salvatoreincandela.blogspot.com
    • 35 views on http://pronetics.wordpress.com
    • 1 views on http://translate.googleusercontent.com

    more

    All embeds
    • 84 views on http://salvatoreincandela.blogspot.com
    • 35 views on http://pronetics.wordpress.com
    • 1 views on http://translate.googleusercontent.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories