How do we obtain the Google App Engine safe-URL key in Java for a different appId and namespace? -
when need application without namespace can use following code:
final key mykey = keyfactory.createkey(kind, id); final string safeurlkey = keyfactory.keytostring(mykey);
unfortunately when need different appid or namespace don't find way in java.
in python example can use following code:
new_key = db.key.from_path(entity, id, _app=application_id, namespace=namespace) return str(new_key)
but in java doesn't seem available.
any idea on how can this?
the app engine sdk indeed try prohibit this, evidenced lack of public classes/methods can handle app ids , namespaces. in python discouraged underscore prefix on _app
keyword argument. because app engine apps meant well-contained within project.
it possible use reflection workaround these barriers, only on standard java 8 runtime (which in beta). standard java 7 runtime prohibits reflecting non-accessible methods. (if you're using app engine flex suspect you'll ok too, although haven't tested that.)
if using java 8 or willing switch, able create keys arbitrary app ids/namespaces following:
key createkey(string appid, string namespace, string kind, long id) { try { class<?> appnsclazz = class.forname("com.google.appengine.api.datastore.appidnamespace"); constructor<?> constructor = appnsclazz.getconstructor(string.class, string.class); constructor.setaccessible(true); constructor<key> keyfactory = key.class.getdeclaredconstructor(string.class, key.class, long.class, string.class, appnsclazz); keyfactory.setaccessible(true); object appns = constructor.newinstance(appid, namespace); return keyfactory.newinstance(kind, /* parent key */ null, id, /* name */ null, appns); } catch (classnotfoundexception | nosuchmethodexception | invocationtargetexception | instantiationexception | illegalaccessexception e) { throw new runtimeexception(e); } }
if running code cache constructor instances, , appns
instance if possible avoid performance overhead of reflection.
please note code not work on standard java 7 runtime.
Comments
Post a Comment