notebookAuthorization as singleton

This commit is contained in:
Khalid Huseynov 2016-09-19 14:46:06 +09:00
parent 9427e6260a
commit 09e6723482
5 changed files with 28 additions and 13 deletions

View file

@ -92,7 +92,7 @@ public class ZeppelinServer extends Application {
notebookWsServer, heliumApplicationFactory, depResolver);
this.notebookRepo = new NotebookRepoSync(conf);
this.notebookIndex = new LuceneSearch();
this.notebookAuthorization = new NotebookAuthorization(conf);
this.notebookAuthorization = NotebookAuthorization.init(conf);
this.credentials = new Credentials(conf.credentialsPersist(), conf.getCredentialsPath());
notebook = new Notebook(conf,
notebookRepo, schedulerFactory, replFactory, notebookWsServer,

View file

@ -31,17 +31,22 @@ import java.util.*;
*/
public class NotebookAuthorization {
private static final Logger LOG = LoggerFactory.getLogger(NotebookAuthorization.class);
private static NotebookAuthorization instance = null;
/*
* { "note1": { "owners": ["u1"], "readers": ["u1", "u2"], "writers": ["u1"] }, "note2": ... } }
*/
private Map<String, Map<String, Set<String>>> authInfo = new HashMap<>();
private ZeppelinConfiguration conf;
private Gson gson;
private String filePath;
private static Map<String, Map<String, Set<String>>> authInfo = new HashMap<>();
private static ZeppelinConfiguration conf;
private static Gson gson;
private static String filePath;
public NotebookAuthorization(ZeppelinConfiguration conf) {
this.conf = conf;
private NotebookAuthorization() {}
public static NotebookAuthorization init(ZeppelinConfiguration config) {
if (instance == null) {
instance = new NotebookAuthorization();
}
conf = config;
filePath = conf.getNotebookAuthorizationPath();
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
@ -51,9 +56,19 @@ public class NotebookAuthorization {
} catch (IOException e) {
LOG.error("Error loading NotebookAuthorization", e);
}
return instance;
}
private void loadFromFile() throws IOException {
public static NotebookAuthorization getInstance() {
if (instance == null) {
LOG.warn("Notebook authorization module was called without initialization,"
+ " initializing with default configuration");
init(ZeppelinConfiguration.create());
}
return instance;
}
private static void loadFromFile() throws IOException {
File settingFile = new File(filePath);
LOG.info(settingFile.getAbsolutePath());
if (!settingFile.exists()) {
@ -74,7 +89,7 @@ public class NotebookAuthorization {
String json = sb.toString();
NotebookAuthorizationInfoSaving info = gson.fromJson(json,
NotebookAuthorizationInfoSaving.class);
this.authInfo = info.authInfo;
authInfo = info.authInfo;
}
private void saveToFile() {

View file

@ -89,7 +89,7 @@ public class HeliumApplicationFactoryTest implements JobListenerFactory {
SearchService search = mock(SearchService.class);
notebookRepo = new VFSNotebookRepo(conf);
NotebookAuthorization notebookAuthorization = new NotebookAuthorization(conf);
NotebookAuthorization notebookAuthorization = NotebookAuthorization.init(conf);
notebook = new Notebook(
conf,
notebookRepo,

View file

@ -91,7 +91,7 @@ public class NotebookTest implements JobListenerFactory{
SearchService search = mock(SearchService.class);
notebookRepo = new VFSNotebookRepo(conf);
notebookAuthorization = new NotebookAuthorization(conf);
notebookAuthorization = NotebookAuthorization.init(conf);
credentials = new Credentials(conf.credentialsPersist(), conf.getCredentialsPath());
notebook = new Notebook(conf, notebookRepo, schedulerFactory, factory, this, search,

View file

@ -99,7 +99,7 @@ public class NotebookRepoSyncTest implements JobListenerFactory {
search = mock(SearchService.class);
notebookRepoSync = new NotebookRepoSync(conf);
notebookAuthorization = new NotebookAuthorization(conf);
notebookAuthorization = NotebookAuthorization.init(conf);
credentials = new Credentials(conf.credentialsPersist(), conf.getCredentialsPath());
notebookSync = new Notebook(conf, notebookRepoSync, schedulerFactory, factory, this, search,
notebookAuthorization, credentials);