/** * Create a Ref based on the key */ public LiveRef(Key<T> key) { this(key, ObjectifyService.ofy()); }
/** */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try (Closeable closeable = ObjectifyService.begin()) { chain.doFilter(request, response); } } }
/** * <p>An alternative to run() which is somewhat easier to use with testing (ie, @Before and @After) frameworks. * You must close the return value at the end of the request in a finally block. It's better/safer to use run().</p> * * <p>This method is not typically necessary - in a normal request, the ObjectifyFilter takes care of this housekeeping * for you. However, in unit tests or remote API calls it can be useful.</p> */ public static Closeable begin() { return factory().open(); } }
@Before public void setUp() throws Exception { // Don't call super.setUp because we initialize slightly differently // here... countdownLatch = new LocalTaskQueueTestConfig.TaskCountDownLatch(1); // See // http://www.ensor.cc/2010/11/unit-testing-named-queues-spring.html // NOTE: THE QUEUE XML PATH RELATIVE TO WEB APP ROOT, More info // below // http://stackoverflow.com/questions/11197058/testing-non-default-app-engine-task-queues final LocalTaskQueueTestConfig localTaskQueueConfig = new LocalTaskQueueTestConfig() .setDisableAutoTaskExecution(false).setQueueXmlPath("src/test/resources/queue.xml") .setTaskExecutionLatch(countdownLatch).setCallbackClass(DeleteShardedCounterDeferredCallback.class); // Use a different queue.xml for testing purposes helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig().setApplyAllHighRepJobPolicy(), new LocalMemcacheServiceTestConfig(), new LocalCapabilitiesServiceTestConfig(), localTaskQueueConfig); helper.setUp(); memcache = MemcacheServiceFactory.getMemcacheService(); capabilitiesService = CapabilitiesServiceFactory.getCapabilitiesService(); // New Objectify 5.1 Way. See https://groups.google.com/forum/#!topic/objectify-appengine/O4FHC_i7EGk this.session = ObjectifyService.begin(); // Enable Joda Translators JodaTimeTranslators.add(ObjectifyService.factory()); ObjectifyService.factory().register(CounterData.class); ObjectifyService.factory().register(CounterShardData.class); ObjectifyService.factory().register(CounterShardOperationData.class); shardedCounterServiceImpl = new ShardedCounterServiceImpl(); this.shardedCounterService = shardedCounterServiceImpl; }
public static <T> T run(Work<T> work) { return ObjectifyService.run(work); } }
@Test public void accept() { String token = requestTransfer(); ObjectifyService.run(new VoidWork() { @Override public void vrun() { Response response = approvalResource.accept(uri, token); Response.Status status = Response.Status.fromStatusCode(response.getStatus()); // Make sure we're redirected if (status != Response.Status.SEE_OTHER) { throw new AssertionError("Could not accept and execute database transfer: { " + status + " : " + response.getEntity() + " }"); } } }); }
@Override public void start(UpdatableInjectionContext injectionContext) { InterceptorRegistry interceptorRegistry = injectionContext.get(InterceptorRegistry.class); interceptorRegistry.registerInterceptor(JobProcessor.class, new JobProcessorInterceptor()); ObjectifyService.register(JobStatus.class); } }
protected Objectify ofy() { return ObjectifyService.ofy(); }
/** * <p>Runs one unit of work, making the root Objectify context available. This does not start a transaction, * but it makes the static ofy() method return an appropriate object.</p> * * <p>Normally you do not need to use this method. When servicing a normal request, the ObjectifyFilter * will run this for you. This method is useful for using Objectify outside of a normal request - * using the remote api, for example.</p> * * <p>Alternatively, you can use the begin() method and close the session manually.</p> * * @return the result of the work. */ public static <R> R run(final Work<R> work) { try (Closeable closeable = begin()) { return work.run(); } }
/** * You can extend the servlet and override this method if you are doing something unusual with the factory. */ protected EntityMemcacheStats getMemcacheStats() { return ObjectifyService.factory().getMemcacheStats(); }
@Test public void startTransfer() { ObjectifyService.run(new VoidWork() { @Override public void vrun() { RequestTransfer request = buildRequest(); Response response = resource.requestTransfer(alex, request); Response.Status status = Response.Status.fromStatusCode(response.getStatus()); if (status != Response.Status.OK) { throw new AssertionError("Could not generate request: { " + status + " }"); } Database database = em.find(Database.class, databaseId); String token = database.getTransferToken(); TransferAuthorized transfer = new TransferAuthorized(alex.getId(), bavon.getId(), databaseId, token); response = resource.startTransfer(uri, authTokenProvider, transfer); status = Response.Status.fromStatusCode(response.getStatus()); // Make sure we redirect if (status != Response.Status.SEE_OTHER) { throw new AssertionError("Could not start transfer: { " + status + ": " + response.getEntity() + " }"); } database = em.find(Database.class, databaseId); // Make sure database has transferred to new owner and token is nulled assertTrue(database.getOwner().getId() == bavon.getUserId()); assertTrue(database.getTransferToken() == null); assertTrue(database.getTransferUser() == null); assertTrue(database.getTransferRequestDate() == null); } }); }
/** * Get the current objectify instance associated with this ref */ private Objectify ofy() { // If we have an expired transaction context, we need a new context if (ofy == null || (ofy.getTransaction() != null && !ofy.getTransaction().isActive())) ofy = ObjectifyService.ofy(); return ofy; }
@BeforeClass public void setupObjectify() { OfyHelper.registerEntityClasses(); closeable = ObjectifyService.begin(); }
/** * A shortcut for {@code factory().register()} * * @see ObjectifyFactory#register(Class) */ public static void register(Class<?> clazz) { factory().register(clazz); }
public static Objectify ofy() { return ObjectifyService.ofy(); }
protected void doOperationRemotely() throws IOException { String appUrl = ClientProperties.TARGET_URL.replaceAll("^https?://", ""); String appDomain = appUrl.split(":")[0]; int appPort = appUrl.contains(":") ? Integer.parseInt(appUrl.split(":")[1]) : 443; System.out.println("--- Starting remote operation ---"); System.out.println("Going to connect to:" + appDomain + ":" + appPort); RemoteApiOptions options = new RemoteApiOptions().server(appDomain, appPort); if (ClientProperties.isTargetUrlDevServer()) { // Dev Server doesn't require credential. options.useDevelopmentServerCredential(); } else { // Your Google Cloud SDK needs to be authenticated for Application Default Credentials // in order to run any script in production server. // Refer to https://developers.google.com/identity/protocols/application-default-credentials. options.useApplicationDefaultCredential(); } RemoteApiInstaller installer = new RemoteApiInstaller(); installer.install(options); OfyHelper.registerEntityClasses(); Closeable objectifySession = ObjectifyService.begin(); try { doOperation(); } finally { objectifySession.close(); installer.uninstall(); } System.out.println("--- Remote operation completed ---"); }
/** * The method to call at any time to get the current Objectify, which may change depending on txn context */ public static Objectify ofy() { return factory().ofy(); }
ObjectifyService.run(new VoidWork() { @Override public void vrun() {