PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/google/appengine/datanucleus/PojoDatastoreBridge.java

http://datanucleus-appengine.googlecode.com/
Java | 103 lines | 66 code | 14 blank | 23 comment | 1 complexity | 9cdb25b0cf1ea5b892ded002ca553177 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright (C) 2010 Google Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.appengine.datanucleus;
  17. import com.google.appengine.api.datastore.Cursor;
  18. import com.google.appengine.api.datastore.Entity;
  19. import org.datanucleus.ClassLoaderResolver;
  20. import org.datanucleus.ExecutionContext;
  21. import org.datanucleus.store.StoreManager;
  22. import org.datanucleus.store.connection.ManagedConnection;
  23. import org.datanucleus.store.connection.ManagedConnectionResourceListener;
  24. import org.datanucleus.metadata.AbstractClassMetaData;
  25. import com.google.appengine.datanucleus.query.DatastoreQuery;
  26. import org.datanucleus.store.query.AbstractJavaQuery;
  27. import org.datanucleus.store.query.AbstractQueryResult;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * Utilities for converting between the low-level datastore api and pojos.
  32. *
  33. * @author Max Ross <max.ross@gmail.com>
  34. */
  35. class PojoDatastoreBridge {
  36. <T> List<T> toPojoResult(final ExecutionContext ec, Class<T> cls, Iterable<Entity> queryResultIterable, Cursor endCursor) {
  37. final ClassLoaderResolver clr = ec.getClassLoaderResolver();
  38. final AbstractClassMetaData acmd = ec.getMetaDataManager().getMetaDataForClass(cls, clr);
  39. Utils.Function<Entity, Object> func = new Utils.Function<Entity, Object>() {
  40. public Object apply(Entity from) {
  41. return EntityUtils.entityToPojo(from, acmd, clr, ec, true, ec.getFetchPlan().getCopy());
  42. }
  43. };
  44. AbstractJavaQuery query = new DummyQuery(ec.getStoreManager(), ec);
  45. final ManagedConnection mconn = ec.getStoreManager().getConnection(ec);
  46. try {
  47. List<T> results = (List<T>) DatastoreQuery.newStreamingQueryResultForEntities(
  48. queryResultIterable, func, endCursor, query);
  49. if (results instanceof AbstractQueryResult) {
  50. // Lazy loading results : add listener to the connection so we can get a callback when the connection is flushed.
  51. final AbstractQueryResult qr = (AbstractQueryResult)results;
  52. ManagedConnectionResourceListener listener = new ManagedConnectionResourceListener() {
  53. public void managedConnectionPreClose() {
  54. // Disconnect the query from this ManagedConnection (read in unread rows etc)
  55. qr.disconnect();
  56. }
  57. public void managedConnectionPostClose() {}
  58. public void resourcePostClose() {
  59. mconn.removeListener(this);
  60. }
  61. public void transactionFlushed() {}
  62. public void transactionPreClose() {
  63. // Disconnect the query from this ManagedConnection (read in unread rows etc)
  64. qr.disconnect();
  65. }
  66. };
  67. mconn.addListener(listener);
  68. qr.addConnectionListener(listener);
  69. }
  70. return results;
  71. } finally {
  72. mconn.release();
  73. }
  74. }
  75. private static final class DummyQuery extends AbstractJavaQuery {
  76. private DummyQuery(StoreManager storeMgr, ExecutionContext ec) {
  77. super(storeMgr, ec);
  78. }
  79. public String getSingleStringQuery() {
  80. throw new UnsupportedOperationException();
  81. }
  82. protected void compileInternal(Map parameterValues) {
  83. throw new UnsupportedOperationException();
  84. }
  85. protected Object performExecute(Map parameters) {
  86. throw new UnsupportedOperationException();
  87. }
  88. }
  89. }