]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/AbstractSwtCmsView.java
Start improving single-user login
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / AbstractSwtCmsView.java
1 package org.argeo.cms.swt;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.concurrent.Callable;
6 import java.util.concurrent.CompletableFuture;
7 import java.util.concurrent.CompletionException;
8 import java.util.concurrent.ExecutionException;
9
10 import javax.security.auth.Subject;
11 import javax.security.auth.login.LoginContext;
12
13 import org.argeo.api.cms.CmsApp;
14 import org.argeo.api.cms.CmsEventBus;
15 import org.argeo.api.cms.CmsLog;
16 import org.argeo.api.cms.ux.CmsImageManager;
17 import org.argeo.api.cms.ux.CmsUi;
18 import org.argeo.api.cms.ux.CmsView;
19 import org.argeo.api.cms.ux.UxContext;
20 import org.argeo.cms.CurrentUser;
21 import org.argeo.cms.util.CurrentSubject;
22 import org.eclipse.swt.widgets.Display;
23
24 public abstract class AbstractSwtCmsView implements CmsView {
25 private final static CmsLog log = CmsLog.getLog(AbstractSwtCmsView.class);
26
27 protected final String uiName;
28
29 protected LoginContext loginContext;
30 protected String state;
31 // protected Throwable exception;
32 protected UxContext uxContext;
33 protected CmsImageManager imageManager;
34
35 protected Display display;
36 protected CmsUi ui;
37
38 protected String uid;
39
40 public AbstractSwtCmsView(String uiName) {
41 this.uiName = uiName;
42 }
43
44 public abstract CmsEventBus getCmsEventBus();
45
46 public abstract CmsApp getCmsApp();
47
48 @Override
49 public void sendEvent(String topic, Map<String, Object> properties) {
50 if (properties == null)
51 properties = new HashMap<>();
52 if (properties.containsKey(CMS_VIEW_UID_PROPERTY) && !properties.get(CMS_VIEW_UID_PROPERTY).equals(uid))
53 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY + " is set to another CMS view uid ("
54 + properties.get(CMS_VIEW_UID_PROPERTY) + ") then " + uid);
55 properties.put(CMS_VIEW_UID_PROPERTY, uid);
56
57 log.trace(() -> uid + ": send event to " + topic);
58
59 getCmsEventBus().sendEvent(topic, properties);
60 // getCmsApp().onEvent(topic, properties);
61 }
62
63 // public void runAs(Runnable runnable) {
64 // display.asyncExec(() -> doAs(Executors.callable(runnable)));
65 // }
66
67 public <T> T doAs(Callable<T> action) {
68 try {
69 CompletableFuture<T> result = new CompletableFuture<>();
70 Runnable toDo = () -> {
71 log.trace(() -> uid + ": process doAs");
72 Subject subject = CurrentSubject.current();
73 T res;
74 if (subject != null) {
75 assert subject == getSubject();
76 try {
77 res = action.call();
78 } catch (Exception e) {
79 throw new CompletionException("Failed to execute action for " + subject, e);
80 }
81 } else {
82 res = CurrentSubject.callAs(getSubject(), action);
83 }
84 result.complete(res);
85 };
86 if (Thread.currentThread() == display.getThread())
87 toDo.run();
88 else {
89 display.asyncExec(toDo);
90 display.wake();
91 }
92 // throw new IllegalStateException("Must be called from UI thread");
93 return result.get();
94 } catch (InterruptedException | ExecutionException e) {
95 throw new IllegalStateException("Cannot execute action ins CMS view " + uid, e);
96 }
97 }
98
99 @Override
100 public UxContext getUxContext() {
101 return uxContext;
102 }
103
104 @Override
105 public String getUid() {
106 return uid;
107 }
108
109 @Override
110 public CmsImageManager<?, ?> getImageManager() {
111 return imageManager;
112 }
113
114 @Override
115 public boolean isAnonymous() {
116 return CurrentUser.isAnonymous(getSubject());
117 }
118
119 protected Subject getSubject() {
120 return loginContext.getSubject();
121 }
122
123 @Override
124 public Object getData(String key) {
125 if (ui != null) {
126 return ui.getData(key);
127 } else {
128 throw new IllegalStateException("UI is not initialized");
129 }
130 }
131
132 @Override
133 public void setData(String key, Object value) {
134 if (ui != null) {
135 ui.setData(key, value);
136 } else {
137 throw new IllegalStateException("UI is not initialized");
138 }
139 }
140
141 }