Skip to content
This repository
tree: 0f753820ae
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 61 lines (51 sloc) 2.585 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
(ns appengine-magic.local-env-helpers
  (:use appengine-magic.utils)
  (:import java.io.File
           [com.google.apphosting.api ApiProxy ApiProxy$Environment]
           [com.google.appengine.tools.development ApiProxyLocalFactory ApiProxyLocalImpl
            LocalServerEnvironment]
           com.google.appengine.api.taskqueue.dev.LocalTaskQueue))


(defonce ^{:dynamic true} *current-app-id* (atom nil))
(defonce ^{:dynamic true} *current-app-version* (atom nil))

(defonce ^{:dynamic true} *current-server-port* (atom nil))


(defn make-thread-environment-proxy [& {:keys [user-email user-admin?]}]
  (proxy [ApiProxy$Environment] []
    (isLoggedIn [] (or (boolean user-email) false))
    (getAuthDomain [] "")
    (getRequestNamespace [] "")
    (getDefaultNamespace [] "")
    (getAttributes [] (java.util.HashMap.))
    (getEmail [] (or user-email ""))
    (isAdmin [] (or (Boolean/parseBoolean user-admin?) false))
    (getAppId [] @*current-app-id*)
    (getVersionId [] @*current-app-version*)))


(defn appengine-init [#^File dir, port]
  (let [appengine-web-file (File. dir "WEB-INF/appengine-web.xml")
        application-id (if (.exists appengine-web-file)
                           (first (xpath-value appengine-web-file "//application"))
                           "appengine-magic-app")
        application-version (if (.exists appengine-web-file)
                                (first (xpath-value appengine-web-file "//version"))
                                "")
        proxy-factory (ApiProxyLocalFactory.)
        environment (proxy [LocalServerEnvironment] []
                      (enforceApiDeadlines [] true)
                      (simulateProductionLatencies [] true)
                      (getAppDir [] dir)
                      (getHostName [] "localhost")
                      (getAddress [] "localhost")
                      (getPort [] port)
                      (waitForServerToStart [] nil))
        api-proxy (.create proxy-factory environment)]
    (reset! *current-app-id* application-id)
    (reset! *current-app-version* application-version)
    (reset! *current-server-port* port)
    (ApiProxy/setDelegate api-proxy)
    ;; This installs a thread environment onto the REPL thread and allows App
    ;; Engine API calls to work in the REPL.
    (ApiProxy/setEnvironmentForCurrentThread (make-thread-environment-proxy))))


(defn appengine-clear []
  (reset! *current-app-id* nil)
  (ApiProxy/clearEnvironmentForCurrentThread)
  (.stop (.getService (ApiProxy/getDelegate) LocalTaskQueue/PACKAGE))
  (.stop (ApiProxy/getDelegate)))
Something went wrong with that request. Please try again.