var api = require('./api');
var pap = require('../pap');

// TODO: Check how to share app with PAP
function init(settings) {
    return new Promise(function(resolve, reject) {
        pap.init(settings.pap).then(function() {
            api.init(settings.pdp, pap).then(function(fresh) {
                resolve(fresh);
            }, function(e) {
                reject(e);
            });
        }, function(e) {
            reject(e);
        });
    });
}

function finish() {
    return new Promise(function(resolve, reject) {
        api.finish().then(function() {
            pap.finish().then(function(r) {
                resolve(r);
            }, function(e) {
                reject(e);
            });
        }, function(e) {
            reject(e);
        });
    });
}

module.exports = {
    init: init,
    finish: finish,
    checkRead : api.checkRead,
    checkWrite : api.checkWrite,
    pap: pap
}

@@ -55,48 +55,69 @@
 );%0A%0A
-// TODO: Check how to share app with PAP
+var Promise = require(%22bluebird%22);%0A%0Avar initialized = false;%0A
 %0Afun
@@ -135,24 +135,30 @@
 settings
+, _pap
 ) %7B%0A    
 return n
@@ -153,336 +153,542 @@
     
-return new Promise(function(resolve, reject) %7B%0A        pap.init(settings.pap).then(function() %7B%0A            api.init(settings.pdp, pap).then(function(fresh) %7B%0A                resolve(fresh);%0A            %7D, function(e) %7B%0A                reject(e);%0A            %7D);%0A        %7D, function(e) %7B%0A            reject(e);%0A        %7D
+if(initialized) %7B%0A        // console.log(process.pid + %22: PDP has already been initialized. Skip%22);%0A        return Promise.resolve(this);%0A    %7D else %7B%0A        // console.log(process.pid + %22: Init PDP.%22);%0A        initialized = true;%0A    %7D%0A%0A    if(_pap === undefined) %7B%0A        // console.log(%22PDP init WO PAP%22);%0A        return pap.init(settings).%0A            then(api.init.bind(null, settings, pap));%0A    %7D else %7B%0A        // console.log(%22PDP init WITH PAP%22);%0A        api.init(settings, _pap)%0A        return Promise.resolve(this
 );%0A    %7D
 );%0A%7D
@@ -679,26 +679,24 @@
 this);%0A    %7D
-);
 %0A%7D%0A%0Afunction
@@ -1080,17 +1080,16 @@
 heckRead
- 
 : api.ch
@@ -1111,17 +1111,16 @@
 eckWrite
- 
 : api.ch
@@ -1129,16 +1129,50 @@
 kWrite,%0A
+    checkAccess: api.checkAccess,%0A
     pap:

Allow PDP to be initialized with existing PAP and export new API