Class NamespaceManager


  • public class NamespaceManager
    extends Object
    A simple thread local namespace manager, similar to legacy GAE's NamespaceManager.
    • Constructor Detail

      • NamespaceManager

        public NamespaceManager()
    • Method Detail

      • set

        public static Closeable set​(String namespaceName)

        Sets the default namespace for this thread. Similar to legacy GAE's NamespaceManager. While a namespace is set, all keys which are not created with an explicit namespace and all queries without an explicit namespace will inherit this value.

        To exit the namespace, call close() on the return value. This should be performed in a finally block, or even better with the a try-with-resources idiom:

        try (Closeable ignored = NamespaceManager.set("blah")) { ... }

        Note that this namespace affects key creation, but once a key has been created, it has an inherent namespace.

             final Key<Foo> key = Key.create(Foo.class, 123);
        
             try (final Closeable ignored = NamespaceManager.set("blah")) {
                 ofy().load().key(key);  // The key already has the default namespace
                 ofy().load().type(Foo.class).id(123);  // Uses the 'blah' namespace
                 ofy().load().key(Key.create(Foo.class, 123));  // Uses the 'blah' namespace
             }
         

        You can call set(null) to clear the namespace; this is identical to calling close() on the return value.

      • get

        public static String get()
        Returns:
        the currently set default namespace, or null if one is not set