RE: Re: SpringBeanRouter enhancement
Sorry, I got no time to write a full fledged unit test class, I simply submitted a ticket in your backlog :-) http://restlet.tigris.org/issues/show_bug.cgi?id=735 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1184490
Re: Managing a JPA/Hibernate session
Hi Peter, I recently encountered this problem myself. I ended up creating a Representation wrapper that would close my EM after the call to write. The code [1] is part of restlet-jpa [2]. Regards, Michael. [1] - http://trac.sarugo.org/restlet-jpa/browser/trunk/src/main/java/org/sarugo/restlet/jpa/TransactionFilter.java [2] - http://trac.sarugo.org/restlet-jpa/ On Wed, Feb 18, 2009 at 10:22 AM, Peter Becker pbec...@itee.uq.edu.au wrote: Hello, I'm using Restlet above a JPA persistence layer with Hibernate as provider. That works reasonably well, but unfortunately I haven't found a way to handle the persistence sessions properly. What I would like to see is that a new session is opened whenever needed and is closed after all request processing is done. This is my current attempt: public class EntityManagerFilter extends Filter implements EntityManagerSource { private final EntityManagerFactory emf; private final ThreadLocalEntityManager entityManagerTL = new ThreadLocalEntityManager(); public EntityManagerFilter(Context context, Restlet next, EntityManagerFactory emf) { super(context, next); this.emf = emf; } public EntityManagerFilter(Context context, EntityManagerFactory emf) { super(context); this.emf = emf; } public EntityManagerFilter(EntityManagerFactory emf) { this.emf = emf; } public EntityManager getEntityManager() { // we lazily initialize in case the entity manager is not actually needed // by a request EntityManager entityManager = entityManagerTL.get(); if(entityManager == null) { entityManager = emf.createEntityManager(); entityManagerTL.set(entityManager); } return entityManager; } @Override protected void afterHandle(Request request, Response response) { EntityManager entityManager = entityManagerTL.get(); if(entityManager!=null) { entityManagerTL.remove(); assert entityManager.isOpen(): Entity manager should only be closed here but must have been closed elsewhere; entityManager.close(); } super.afterHandle(request, response); } } This filter is attached as the root of the application, forwarding to the router that does the main dispatch. The approach works to some extent, but with a huge but: most of the time I produce TemplateRepresentations to render data with Freemarker. The processing of those templates happens after the afterHandle(..) method of the filter, which means that the session is closed and if the template uses anything that is not eagerly fetched and the Java code hasn't used it will get an exception from the persistence layer. That is actually a pretty common case since a lot of the details of objects is needed only in the template rendering. Currently I work around that problem by explicitly loading objects in Java code. I'd rather solve that problem properly, but I can't find a hook that gets called after the write(..) method of the representations. Long intro, short question: Is there such thing? Thanks, Peter -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1182479 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1185212
RE: // performance
Hello, We need to do further tests to see how we can tune this... I've entered a report: Improve performance of WAR client http://restlet.tigris.org/issues/show_bug.cgi?id=736 Could you add comments if necessary and, ideally, attach a small reproducible example to the issue. A patch would be even better :) Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : blackh...@collab.net [mailto:blackh...@collab.net] Envoye : jeudi 12 fevrier 2009 20:45 A : discuss@restlet.tigris.org Objet : RE: // performance Hello, Could you try to turn off content negotiation (via the negotiateContent property) on your Directory and see if it improves performance? Hi Jerome, I disabled content negotation (directory.setNegotiateContent(false)), but this didn't seem to have an appreciable impact on execution time. Cheers -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1146975 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186178
RE: The request was rejected because no multipart boundary was found
Hi Satyendra, This is because client-side Restlet doesn't support multi-part yet. This is planned for Restlet 1.2 though: Support composite representations http://restlet.tigris.org/issues/show_bug.cgi?id=71 For now, I would suggest to use Apache HTTP client directly: http://hc.apache.org/httpclient-3.x/methods/multipartpost.html Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Satyendra Singh [mailto:singh_satyen...@yahoo.com] Envoyé : vendredi 13 février 2009 07:28 À : discuss@restlet.tigris.org Objet : The request was rejected because no multipart boundary was found I can upload the file successfully from the browser (html) but getting the exception on server side while uploading a same file using the restlet client. org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found The Client code is: Request request = new Request(); ChallengeResponse auth = new ChallengeResponse(ChallengeScheme.HTTP_BASIC,16512002000,); request.setChallengeResponse(auth); request.setReferrerRef(http://127.0.0.1/;); Reference reference = new Reference(http://localhost:8080/cas/subscriber/phone/1.651.2001000/familysubmailbox/2/upload/greeting/1/en-us/;); request.setResourceRef(reference); // Action: POST request.setMethod(Method.POST); FileRepresentation oFileRepresentation = new FileRepresentation(C:\\AA.pdf, MediaType.MULTIPART_ALL, 10); request.setEntity(oFileRepresentation); Client client = new Client(Protocol.HTTP); Response response = client.handle(request); System.out.println(response.getStatus()); Kindly help me out from this issue. Thanks, Satyendra -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1149253 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186223
RE: SpringBeanRouter enhancement
Hi Rhett, It looks like a good/simple enhancement to me as well. I would suggest to directly update SVN trunk in this case? If you copy paste the code snippet, we need Daniel to send a signed JCA... As it is quite straightforward, I would suggest coding the logic yourself again from scratch. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Rhett Sutphin [mailto:rh...@detailedbalance.net] Envoye : lundi 16 fevrier 2009 16:37 A : discuss@restlet.tigris.org Objet : Re: SpringBeanRouter enhancement Hi Daniel, This is a reasonable fix. Can you open a ticket in the issue tracker? If you'd care to submit a patch with unit tests, that would be even better. Thanks, Rhett On Feb 16, 2009, at 12:50 AM, Daniel Woo wrote: Hi guys, The SpringBeanRouter.resolveUri() method in 1.1.1 has a potential problem, if you specify restlet in Spring configuration file without an id like this way: bean name=/product-folders scope=prototype class=... /bean Your BeanFinder will never be attached the URI /product-folders. The reason is that the resolveUri() method tries to get name and other aliases from id, see the code below protected String resolveUri(String resourceName, ConfigurableListableBeanFactory factory) { for (final String alias : factory.getAliases(resourceName)) { if (alias.startsWith(/)) { return alias; } } return null; } if you don't specify and ID in the Spring configuration file factory.getAliases(resourceName) will return null when the passed in resourceName is actually the bean name. To workaround this, you have to specify an ID for each restlet, eg, bean id=_product-folders name=/product-folders scope=prototype class=... /bean Note, the id cannot be the same as the name because XML id cannot contain characters in URI like / or {id}. Also, it's troublesome to specify id for each bean. So, how can I just simply specify name without IDs? I made it by changing resolveUri as below protected String resolveUri(String resourceName, ApplicationContext factory) { String aliasName = null; if (resourceName.startsWith(/)) { aliasName = resourceName; } else { for (final String alias : factory.getAliases(resourceName)) { if (alias.startsWith(/)) { aliasName = alias; break; } } } logger.debug(resolveUri: alias= + aliasName); return aliasName; } I first check resourceName, if you don't specify an ID, bean name will be passed in as resourceName, in this case, just return the beanName if it starts with a slash. If you specify an ID, bean id will be passed in, then getAliases() will return the beanName of that id. it's done. Tested with 1.1.1 What do you guys think? Regards, Daniel -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1169062 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1171440 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186265
RE: [Restlet] ?media= only available for registered types
Hi Paul, Good suggestion. The enhancement is in SVN trunk! Let me know if it works as expected. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org/ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com http://www.noelios.com _ De : Paul Austin [mailto:mail-li...@revolsys.com] Envoye : mardi 17 fevrier 2009 18:26 A : discuss@restlet.tigris.org Objet : [Restlet] ?media= only available for registered types I've been looking at the TunnerlFilter and have noticed that the ?media=mediaType query string extension only works if the media type is registered as a MetadataExtension with the the MetadataService. Is this by design?Or would it be possible if a metadata entry was not found to set the mediaType based on the value passed in? Cheers, Paul -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186343
RE: Re: SpringBeanRouter enhancement
Hi again, I missed that email... Forget about my previous email! Thanks Daniel for taking the time to create the RFE. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Daniel Woo [mailto:daniel.y@gmail.com] Envoye : mercredi 18 fevrier 2009 09:56 A : discuss@restlet.tigris.org Objet : RE: Re: SpringBeanRouter enhancement Sorry, I got no time to write a full fledged unit test class, I simply submitted a ticket in your backlog :-) http://restlet.tigris.org/issues/show_bug.cgi?id=735 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1184490 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186354
RE: Managing a JPA/Hibernate session
Hi guys, This is such a hook available. See the ConnectorService#afterSend method: http://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend(org.restlet.resource.Represent ation) Hope this helps! Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Michael Terrington [mailto:mich...@terrington.id.au] Envoye : mercredi 18 fevrier 2009 12:23 A : discuss@restlet.tigris.org Objet : Re: Managing a JPA/Hibernate session Hi Peter, I recently encountered this problem myself. I ended up creating a Representation wrapper that would close my EM after the call to write. The code [1] is part of restlet-jpa [2]. Regards, Michael. [1] - http://trac.sarugo.org/restlet-jpa/browser/trunk/src/main/java/org/sarugo/restlet/jpa/TransactionFilter.java [2] - http://trac.sarugo.org/restlet-jpa/ On Wed, Feb 18, 2009 at 10:22 AM, Peter Becker pbec...@itee.uq.edu.au wrote: Hello, I'm using Restlet above a JPA persistence layer with Hibernate as provider. That works reasonably well, but unfortunately I haven't found a way to handle the persistence sessions properly. What I would like to see is that a new session is opened whenever needed and is closed after all request processing is done. This is my current attempt: public class EntityManagerFilter extends Filter implements EntityManagerSource { private final EntityManagerFactory emf; private final ThreadLocalEntityManager entityManagerTL = new ThreadLocalEntityManager(); public EntityManagerFilter(Context context, Restlet next, EntityManagerFactory emf) { super(context, next); this.emf = emf; } public EntityManagerFilter(Context context, EntityManagerFactory emf) { super(context); this.emf = emf; } public EntityManagerFilter(EntityManagerFactory emf) { this.emf = emf; } public EntityManager getEntityManager() { // we lazily initialize in case the entity manager is not actually needed // by a request EntityManager entityManager = entityManagerTL.get(); if(entityManager == null) { entityManager = emf.createEntityManager(); entityManagerTL.set(entityManager); } return entityManager; } @Override protected void afterHandle(Request request, Response response) { EntityManager entityManager = entityManagerTL.get(); if(entityManager!=null) { entityManagerTL.remove(); assert entityManager.isOpen(): Entity manager should only be closed here but must have been closed elsewhere; entityManager.close(); } super.afterHandle(request, response); } } This filter is attached as the root of the application, forwarding to the router that does the main dispatch. The approach works to some extent, but with a huge but: most of the time I produce TemplateRepresentations to render data with Freemarker. The processing of those templates happens after the afterHandle(..) method of the filter, which means that the session is closed and if the template uses anything that is not eagerly fetched and the Java code hasn't used it will get an exception from the persistence layer. That is actually a pretty common case since a lot of the details of objects is needed only in the template rendering. Currently I work around that problem by explicitly loading objects in Java code. I'd rather solve that problem properly, but I can't find a hook that gets called after the write(..) method of the representations. Long intro, short question: Is there such thing? Thanks, Peter -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1182479 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1185212 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186386
Re: Managing a JPA/Hibernate session
Below is my solution which is based on a similar ServletFilter from the SpringFramework. You will need to set the entityManagerFactory before using this. I normall have that configured in spring and get a reference to this filter bean from the context. package com.revolsys.restlet; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceException; import org.apache.log4j.Logger; import org.restlet.Filter; import org.restlet.data.Request; import org.restlet.data.Response; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.orm.jpa.EntityManagerFactoryUtils; import org.springframework.orm.jpa.EntityManagerHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; public class OpenEntityManagerFilter extends Filter { private static final Logger logger = Logger.getLogger(OpenEntityManagerFilter.class); private EntityManagerFactory entityManagerFactory; protected int beforeHandle(Request request, Response response) { if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) { request.getAttributes().put(this.toString(), Boolean.TRUE); } else { logger.debug(Opening JPA EntityManager in OpenEntityManagerFilter); try { EntityManager em = entityManagerFactory.createEntityManager(); TransactionSynchronizationManager.bindResource(entityManagerFactory, new EntityManagerHolder(em)); } catch (PersistenceException ex) { throw new DataAccessResourceFailureException( Could not create JPA EntityManager, ex); } } return super.beforeHandle(request, response); } protected void afterHandle(Request request, Response response) { MapString, Object attributes = request.getAttributes(); if (attributes.get(this.toString()) != Boolean.TRUE) { EntityManagerHolder emHolder = (EntityManagerHolder)TransactionSynchronizationManager.unbindResource(entityManagerFactory); logger.debug(Closing JPA EntityManager in OpenEntityManagerFilter); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } } public EntityManagerFactory getEntityManagerFactory() { return entityManagerFactory; } public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { this.entityManagerFactory = entityManagerFactory; } } On Wed, Feb 18, 2009 at 6:45 AM, Jerome Louvel jerome.lou...@noelios.comwrote: Hi guys, This is such a hook available. See the ConnectorService#afterSend method: http://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend(org.restlet.resource.Representhttp://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend%28org.restlet.resource.Represent ation) Hope this helps! Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Michael Terrington [mailto:mich...@terrington.id.au] Envoye : mercredi 18 fevrier 2009 12:23 A : discuss@restlet.tigris.org Objet : Re: Managing a JPA/Hibernate session Hi Peter, I recently encountered this problem myself. I ended up creating a Representation wrapper that would close my EM after the call to write. The code [1] is part of restlet-jpa [2]. Regards, Michael. [1] - http://trac.sarugo.org/restlet-jpa/browser/trunk/src/main/java/org/sarugo/restlet/jpa/TransactionFilter.java [2] - http://trac.sarugo.org/restlet-jpa/ On Wed, Feb 18, 2009 at 10:22 AM, Peter Becker pbec...@itee.uq.edu.au wrote: Hello, I'm using Restlet above a JPA persistence layer with Hibernate as provider. That works reasonably well, but unfortunately I haven't found a way to handle the persistence sessions properly. What I would like to see is that a new session is opened whenever needed and is closed after all request processing is done. This is my current attempt: public class EntityManagerFilter extends Filter implements EntityManagerSource { private final EntityManagerFactory emf; private final ThreadLocalEntityManager entityManagerTL = new ThreadLocalEntityManager(); public EntityManagerFilter(Context context, Restlet next, EntityManagerFactory emf) { super(context, next); this.emf = emf; } public EntityManagerFilter(Context context, EntityManagerFactory emf) { super(context); this.emf = emf; } public EntityManagerFilter(EntityManagerFactory emf) { this.emf = emf; } public EntityManager getEntityManager() { // we lazily initialize in case the entity manager is not actually needed // by a request EntityManager entityManager = entityManagerTL.get(); if(entityManager ==
New resource API design
On Wed, Feb 18, 2009 at 12:09 PM, Jerome Louvel jerome.lou...@noelios.comwrote: Did you have a chance to check the updated resource API design? http://wiki.restlet.org/developers/172-restlet/226-restlet.html It looks good to me. My only concern is that in the attempt to be consistent with JAX-RS, you're giving up an opportunity for more compile-time checking. If the annotation parameter type for @Get, @Post, @Put was something like Class? extends MediaType instead of String[] it would cut down on the bugs related to typos in those parameters. You could provide common variants as subclasses of MediaType and let users provide their own. As a fallback to the other style, you could have a special subclass that uses an optional String[] parameter to convey the media type(s). Using those ideas, one could rewrite the example on the refactoring page as: @Get(AtomType.class) public Feed toAtom() { // ... return null; } @Get(XmlApplicationType.class) public Representation toXml() { // ... return null; } @Post(XmlType.class) public Representation accept(Document entity) { // ... return null; } @Put(StringArrayType.class, type=atom) // fallback to String[] style public void storeAtom(Feed feed) { // ... } @Put(XmlApplicationType.class) public void storeXml(InputStream stream) { // ... } @Delete public void removeAll() { // ... } I bet most people would want to know at compile-time that they'd written @Put(XmlAplicationType.class) instead of learning at run-time (if ever) that they'd written @Put(application/custom_xml) (See the underscore instead of the plus sign? I would probably miss it.) I'm not sure why JAX-RS didn't do this in the first place. Any idea why they went with easily mistyped string values as arguments to the annotations? --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1187454
Re: Managing a JPA/Hibernate session
Thanks Jerome! That will be a much neater solution. On Thu, Feb 19, 2009 at 1:15 AM, Jerome Louvel jerome.lou...@noelios.com wrote: Hi guys, This is such a hook available. See the ConnectorService#afterSend method: http://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend(org.restlet.resource.Represent ation) Hope this helps! Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Michael Terrington [mailto:mich...@terrington.id.au] Envoye : mercredi 18 fevrier 2009 12:23 A : discuss@restlet.tigris.org Objet : Re: Managing a JPA/Hibernate session Hi Peter, I recently encountered this problem myself. I ended up creating a Representation wrapper that would close my EM after the call to write. The code [1] is part of restlet-jpa [2]. Regards, Michael. [1] - http://trac.sarugo.org/restlet-jpa/browser/trunk/src/main/java/org/sarugo/restlet/jpa/TransactionFilter.java [2] - http://trac.sarugo.org/restlet-jpa/ On Wed, Feb 18, 2009 at 10:22 AM, Peter Becker pbec...@itee.uq.edu.au wrote: Hello, I'm using Restlet above a JPA persistence layer with Hibernate as provider. That works reasonably well, but unfortunately I haven't found a way to handle the persistence sessions properly. What I would like to see is that a new session is opened whenever needed and is closed after all request processing is done. This is my current attempt: public class EntityManagerFilter extends Filter implements EntityManagerSource { private final EntityManagerFactory emf; private final ThreadLocalEntityManager entityManagerTL = new ThreadLocalEntityManager(); public EntityManagerFilter(Context context, Restlet next, EntityManagerFactory emf) { super(context, next); this.emf = emf; } public EntityManagerFilter(Context context, EntityManagerFactory emf) { super(context); this.emf = emf; } public EntityManagerFilter(EntityManagerFactory emf) { this.emf = emf; } public EntityManager getEntityManager() { // we lazily initialize in case the entity manager is not actually needed // by a request EntityManager entityManager = entityManagerTL.get(); if(entityManager == null) { entityManager = emf.createEntityManager(); entityManagerTL.set(entityManager); } return entityManager; } @Override protected void afterHandle(Request request, Response response) { EntityManager entityManager = entityManagerTL.get(); if(entityManager!=null) { entityManagerTL.remove(); assert entityManager.isOpen(): Entity manager should only be closed here but must have been closed elsewhere; entityManager.close(); } super.afterHandle(request, response); } } This filter is attached as the root of the application, forwarding to the router that does the main dispatch. The approach works to some extent, but with a huge but: most of the time I produce TemplateRepresentations to render data with Freemarker. The processing of those templates happens after the afterHandle(..) method of the filter, which means that the session is closed and if the template uses anything that is not eagerly fetched and the Java code hasn't used it will get an exception from the persistence layer. That is actually a pretty common case since a lot of the details of objects is needed only in the template rendering. Currently I work around that problem by explicitly loading objects in Java code. I'd rather solve that problem properly, but I can't find a hook that gets called after the write(..) method of the representations. Long intro, short question: Is there such thing? Thanks, Peter -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1182479 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1185212 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186386 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1188600
Form parsing doesn't work
I have a bit of code that processes a multipart/form-data form. I'm sending a test request using curl, e.g.: curl -v -F from=paul%40lucasmail.org http://localhost:8182/foo; I have code that parses the form that starts off with: final MediaType mediaType = request.getEntity().getMediaType(); final Form form = MediaType.APPLICATION_WWW_FORM.equals( mediaType, true ) || MediaType.MULTIPART_FORM_DATA.equals( mediaType, true ) ? request.getEntityAsForm() : request.getResourceRef().getQueryAsForm(); The form data isn't parsed correctly: the one and only name is the boundary string. The FormReader class doesn't seem to handle multipart/form-data. I'm using an older Restlet version (1.0.9), but I tried upgrading to 1.1.2 and it makes no difference. How to I parse multipart/form-data forms correctly? (Preferably using 1.0.9.) - Paul -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1188665
Re: Managing a JPA/Hibernate session
Paul, correct me if I am wrong, but I believe your solution suffers from the same problem as mine: if there is still data to fetch during the processing of a representation object (as it happens on the write(..) method of the TemplateRepresentation of the Freemarker extension), then you will get exceptions from the persistence layer. By using the ConnectorService the JPA sessions lives long enough to include the call to the write(..) method. Peter Paul Austin wrote: Below is my solution which is based on a similar ServletFilter from the SpringFramework. You will need to set the entityManagerFactory before using this. I normall have that configured in spring and get a reference to this filter bean from the context. package com.revolsys.restlet; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceException; import org.apache.log4j.Logger; import org.restlet.Filter; import org.restlet.data.Request; import org.restlet.data.Response; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.orm.jpa.EntityManagerFactoryUtils; import org.springframework.orm.jpa.EntityManagerHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; public class OpenEntityManagerFilter extends Filter { private static final Logger logger = Logger.getLogger(OpenEntityManagerFilter.class); private EntityManagerFactory entityManagerFactory; protected int beforeHandle(Request request, Response response) { if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) { request.getAttributes().put(this.toString(), Boolean.TRUE); } else { logger.debug(Opening JPA EntityManager in OpenEntityManagerFilter); try { EntityManager em = entityManagerFactory.createEntityManager(); TransactionSynchronizationManager.bindResource(entityManagerFactory, new EntityManagerHolder(em)); } catch (PersistenceException ex) { throw new DataAccessResourceFailureException( Could not create JPA EntityManager, ex); } } return super.beforeHandle(request, response); } protected void afterHandle(Request request, Response response) { MapString, Object attributes = request.getAttributes(); if (attributes.get(this.toString()) != Boolean.TRUE) { EntityManagerHolder emHolder = (EntityManagerHolder)TransactionSynchronizationManager.unbindResource(entityManagerFactory); logger.debug(Closing JPA EntityManager in OpenEntityManagerFilter); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } } public EntityManagerFactory getEntityManagerFactory() { return entityManagerFactory; } public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { this.entityManagerFactory = entityManagerFactory; } } On Wed, Feb 18, 2009 at 6:45 AM, Jerome Louvel jerome.lou...@noelios.comwrote: Hi guys, This is such a hook available. See the ConnectorService#afterSend method: http://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend(org.restlet.resource.Representhttp://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend%28org.restlet.resource.Represent ation) Hope this helps! Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Michael Terrington [mailto:mich...@terrington.id.au] Envoye : mercredi 18 fevrier 2009 12:23 A : discuss@restlet.tigris.org Objet : Re: Managing a JPA/Hibernate session Hi Peter, I recently encountered this problem myself. I ended up creating a Representation wrapper that would close my EM after the call to write. The code [1] is part of restlet-jpa [2]. Regards, Michael. [1] - http://trac.sarugo.org/restlet- jpa/browser/trunk/src/main/java/org/sarugo/restlet/jpa/TransactionFilter.java [2] - http://trac.sarugo.org/restlet-jpa/ On Wed, Feb 18, 2009 at 10:22 AM, Peter Becker pbec...@itee.uq.edu.au wrote: Hello, I'm using Restlet above a JPA persistence layer with Hibernate as provider. That works reasonably well, but unfortunately I haven't found a way to handle the persistence sessions properly. What I would like to see is that a new session is opened whenever needed and is closed after all request processing is done. This is my current attempt: public class EntityManagerFilter extends Filter implements EntityManagerSource { private final EntityManagerFactory emf; private final ThreadLocalEntityManager entityManagerTL = new ThreadLocalEntityManager(); public EntityManagerFilter(Context context, Restlet
RE: Managing a JPA/Hibernate session
Thanks Jerome, that was what I was looking for. I just replaced the type and the afterHandle with afterSend and it seems all works as I wanted. For the record: it gets attached on the Application instance via setConnectorService(..) -- didn't take too long to find, but it might be a good addition for the JavaDoc of that class ;-) Cheers, Peter Jerome Louvel wrote: Hi guys, This is such a hook available. See the ConnectorService#afterSend method: http://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend(org.restlet.resource.Represent ation) Hope this helps! Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Michael Terrington [mailto:mich...@terrington.id.au] Envoye : mercredi 18 fevrier 2009 12:23 A : discuss@restlet.tigris.org Objet : Re: Managing a JPA/Hibernate session Hi Peter, I recently encountered this problem myself. I ended up creating a Representation wrapper that would close my EM after the call to write. The code [1] is part of restlet-jpa [2]. Regards, Michael. [1] - [http://trac.sarugo.org/restlet- jpa/browser/trunk/src/main/java/org/sarugo/restlet/jpa/TransactionFilter.java [2] - http://trac.sarugo.org/restlet-jpa/ On Wed, Feb 18, 2009 at 10:22 AM, Peter Becker pbec...@itee.uq.edu.au wrote: Hello, I'm using Restlet above a JPA persistence layer with Hibernate as provider. That works reasonably well, but unfortunately I haven't found a way to handle the persistence sessions properly. What I would like to see is that a new session is opened whenever needed and is closed after all request processing is done. This is my current attempt: public class EntityManagerFilter extends Filter implements EntityManagerSource { private final EntityManagerFactory emf; private final ThreadLocalEntityManager entityManagerTL = new ThreadLocalEntityManager(); public EntityManagerFilter(Context context, Restlet next, EntityManagerFactory emf) { super(context, next); this.emf = emf; } public EntityManagerFilter(Context context, EntityManagerFactory emf) { super(context); this.emf = emf; } public EntityManagerFilter(EntityManagerFactory emf) { this.emf = emf; } public EntityManager getEntityManager() { // we lazily initialize in case the entity manager is not actually needed // by a request EntityManager entityManager = entityManagerTL.get(); if(entityManager == null) { entityManager = emf.createEntityManager(); entityManagerTL.set(entityManager); } return entityManager; } @Override protected void afterHandle(Request request, Response response) { EntityManager entityManager = entityManagerTL.get(); if(entityManager!=null) { entityManagerTL.remove(); assert entityManager.isOpen(): Entity manager should only be closed here but must have been closed elsewhere; entityManager.close(); } super.afterHandle(request, response); } } This filter is attached as the root of the application, forwarding to the router that does the main dispatch. The approach works to some extent, but with a huge but: most of the time I produce TemplateRepresentations to render data with Freemarker. The processing of those templates happens after the afterHandle(..) method of the filter, which means that the session is closed and if the template uses anything that is not eagerly fetched and the Java code hasn't used it will get an exception from the persistence layer. That is actually a pretty common case since a lot of the details of objects is needed only in the template rendering. Currently I work around that problem by explicitly loading objects in Java code. I'd rather solve that problem properly, but I can't find a hook that gets called after the write(..) method of the representations. Long intro, short question: Is there such thing? Thanks, Peter -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1182479 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1185212 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186386 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1189311
RE: Re: problem with restlet-gwt
I am having the same problem. Does any one know of the solution? Hi all, I hope to find time this week. best regards, Thierry Boileau I very, very seldom use Windows, but Jerome and Thierry do, so I'm sure he's used the sample there -- I think he updated it most recently. I think I see what you are asking -- you need to use both Restlet and RPC in the same application. I don't have an example of that right at hand; I use Restlet so I don't have to use RPC at all ... but I do understand and I know that it can be set up. If you are running under Eclipse, it is possible for the web.xml to be overwritten and Eclipse doesn't know about it without you doing an explicit Refresh on the file -- but you probably tried this already. Is your web.xml exactly as it came from the example, or have you modified it in any way? When we run the gwtshell under eclipse, I saw that it starts a tomcat instance. It also deploys the restlet resources there in order they to be found by the gwt engine? My question is does the gwt shell behave as a web container and deploy the resources (in the server folder) within it as we had created a web project and put the restlet based application classes within it? Yes, that is how the hosted mode works. If I start the restlet TestServer component as a standaolne application and then run the shell again it complains of an already used address and so the server side should work well even if I can not see the TestServer's sytem.out statements in the console. If the standalone uses the same port as hosted mode, you can't run them both at once. I think hosted mode defaults to . I read several books on gwt (gwt in practice and Pro web 2.0 with gwt ... and so on) and I have seen different solutions but noone clearly refers to this particular integration with rest. It is very new -- not likely to be in the books yet! Can You please help me? Trying :-) Thierry here may also have some ideas. - Rob -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1188476
RE: Managing a JPA/Hibernate session
Hi Peter, I've updated the Javadocs as suggested! Cheers, Jerome -Message d'origine- De : news [mailto:n...@ger.gmane.org] De la part de Peter Becker Envoye : jeudi 19 fevrier 2009 02:47 A : discuss@restlet.tigris.org Objet : RE: Managing a JPA/Hibernate session Thanks Jerome, that was what I was looking for. I just replaced the type and the afterHandle with afterSend and it seems all works as I wanted. For the record: it gets attached on the Application instance via setConnectorService(..) -- didn't take too long to find, but it might be a good addition for the JavaDoc of that class ;-) Cheers, Peter Jerome Louvel wrote: Hi guys, This is such a hook available. See the ConnectorService#afterSend method: http://www.restlet.org/documentation/snapshot/api/org/restlet/service/ConnectorService.html#afterSend(org.restlet.resource.Represent ation) Hope this helps! Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Michael Terrington [mailto:mich...@terrington.id.au] Envoye : mercredi 18 fevrier 2009 12:23 A : discuss@restlet.tigris.org Objet : Re: Managing a JPA/Hibernate session Hi Peter, I recently encountered this problem myself. I ended up creating a Representation wrapper that would close my EM after the call to write. The code [1] is part of restlet-jpa [2]. Regards, Michael. [1] - [http://trac.sarugo.org/restlet- jpa/browser/trunk/src/main/java/org/sarugo/restlet/jpa/TransactionFilter.java [2] - http://trac.sarugo.org/restlet-jpa/ On Wed, Feb 18, 2009 at 10:22 AM, Peter Becker pbec...@itee.uq.edu.au wrote: Hello, I'm using Restlet above a JPA persistence layer with Hibernate as provider. That works reasonably well, but unfortunately I haven't found a way to handle the persistence sessions properly. What I would like to see is that a new session is opened whenever needed and is closed after all request processing is done. This is my current attempt: public class EntityManagerFilter extends Filter implements EntityManagerSource { private final EntityManagerFactory emf; private final ThreadLocalEntityManager entityManagerTL = new ThreadLocalEntityManager(); public EntityManagerFilter(Context context, Restlet next, EntityManagerFactory emf) { super(context, next); this.emf = emf; } public EntityManagerFilter(Context context, EntityManagerFactory emf) { super(context); this.emf = emf; } public EntityManagerFilter(EntityManagerFactory emf) { this.emf = emf; } public EntityManager getEntityManager() { // we lazily initialize in case the entity manager is not actually needed // by a request EntityManager entityManager = entityManagerTL.get(); if(entityManager == null) { entityManager = emf.createEntityManager(); entityManagerTL.set(entityManager); } return entityManager; } @Override protected void afterHandle(Request request, Response response) { EntityManager entityManager = entityManagerTL.get(); if(entityManager!=null) { entityManagerTL.remove(); assert entityManager.isOpen(): Entity manager should only be closed here but must have been closed elsewhere; entityManager.close(); } super.afterHandle(request, response); } } This filter is attached as the root of the application, forwarding to the router that does the main dispatch. The approach works to some extent, but with a huge but: most of the time I produce TemplateRepresentations to render data with Freemarker. The processing of those templates happens after the afterHandle(..) method of the filter, which means that the session is closed and if the template uses anything that is not eagerly fetched and the Java code hasn't used it will get an exception from the persistence layer. That is actually a pretty common case since a lot of the details of objects is needed only in the template rendering. Currently I work around that problem by explicitly loading objects in Java code. I'd rather solve that problem properly, but I can't find a hook that gets called after the write(..) method of the representations. Long intro, short question: Is there such thing? Thanks, Peter -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1182479 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1185212 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1186386 --