Refactor to the code use wrapper classes

/to @ritch 


Add AccessContext, Principal, and AccessRequest
Add debug information