]> git.argeo.org Git - gpl/argeo-slc.git/blob - swt/rcp/org.argeo.cms.e4.rcp/src/org/argeo/cms/e4/rcp/CmsE4Application.java
Merge tag 'v2.3.14' into testing
[gpl/argeo-slc.git] / swt / rcp / org.argeo.cms.e4.rcp / src / org / argeo / cms / e4 / rcp / CmsE4Application.java
1 package org.argeo.cms.e4.rcp;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.security.PrivilegedExceptionAction;
6 import java.util.TimerTask;
7 import java.util.UUID;
8 import java.util.concurrent.Callable;
9
10 import javax.security.auth.Subject;
11 import javax.security.auth.login.LoginContext;
12 import javax.security.auth.login.LoginException;
13
14 import org.argeo.api.cms.CmsAuth;
15 import org.argeo.api.cms.ux.CmsImageManager;
16 import org.argeo.api.cms.ux.CmsView;
17 import org.argeo.api.cms.ux.UxContext;
18 import org.argeo.cms.CurrentUser;
19 import org.argeo.cms.swt.CmsSwtUtils;
20 import org.argeo.cms.swt.SimpleSwtUxContext;
21 import org.argeo.cms.swt.auth.CmsLoginShell;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtension;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.equinox.app.IApplication;
26 import org.eclipse.equinox.app.IApplicationContext;
27 import org.eclipse.swt.widgets.Display;
28
29 public class CmsE4Application implements IApplication, CmsView {
30 private LoginContext loginContext;
31 private IApplication e4Application;
32 private UxContext uxContext;
33 private String uid;
34
35 private String httpServerBase;
36
37 @Override
38 public Object start(IApplicationContext context) throws Exception {
39 // TODO wait for CMS to be ready
40 Thread.sleep(5000);
41
42 uid = UUID.randomUUID().toString();
43 Subject subject = new Subject();
44 Display display = createDisplay();
45 CmsLoginShell loginShell = new CmsLoginShell(this, null);
46 // TODO customize CmsLoginShell to be smaller and centered
47 loginShell.setSubject(subject);
48 try {
49 // try pre-auth
50 loginContext = new LoginContext(CmsAuth.LOGIN_CONTEXT_SINGLE_USER, subject, loginShell);
51 loginContext.login();
52 } catch (LoginException e) {
53 e.printStackTrace();
54 loginShell.createUi();
55 loginShell.open();
56
57 while (!loginShell.getShell().isDisposed()) {
58 if (!display.readAndDispatch())
59 display.sleep();
60 }
61 }
62 if (CurrentUser.getUsername(getSubject()) == null)
63 throw new IllegalStateException("Cannot log in");
64
65 // try {
66 // CallbackHandler callbackHandler = new DefaultLoginDialog(
67 // display.getActiveShell());
68 // loginContext = new LoginContext(
69 // NodeConstants.LOGIN_CONTEXT_SINGLE_USER, subject,
70 // callbackHandler);
71 // } catch (LoginException e1) {
72 // throw new CmsException("Cannot initialize login context", e1);
73 // }
74 //
75 // // login
76 // try {
77 // loginContext.login();
78 // subject = loginContext.getSubject();
79 // } catch (LoginException e) {
80 // e.printStackTrace();
81 // display.dispose();
82 // try {
83 // Thread.sleep(2000);
84 // } catch (InterruptedException e1) {
85 // // silent
86 // }
87 // return null;
88 // }
89
90 uxContext = new SimpleSwtUxContext();
91 // UiContext.setData(CmsView.KEY, this);
92 CmsSwtUtils.registerCmsView(loginShell.getShell(), this);
93 e4Application = getApplication(null);
94 Object res = Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
95
96 @Override
97 public Object run() throws Exception {
98 return e4Application.start(context);
99 }
100
101 });
102 return res;
103 }
104
105 @Override
106 public void stop() {
107 if (e4Application != null)
108 e4Application.stop();
109 }
110
111 static IApplication getApplication(String[] args) {
112 IExtension extension = Platform.getExtensionRegistry().getExtension(Platform.PI_RUNTIME,
113 Platform.PT_APPLICATIONS, "org.eclipse.e4.ui.workbench.swt.E4Application");
114 try {
115 IConfigurationElement[] elements = extension.getConfigurationElements();
116 if (elements.length > 0) {
117 IConfigurationElement[] runs = elements[0].getChildren("run");
118 if (runs.length > 0) {
119 Object runnable;
120 runnable = runs[0].createExecutableExtension("class");
121 if (runnable instanceof IApplication)
122 return (IApplication) runnable;
123 }
124 }
125 } catch (Exception e) {
126 throw new IllegalStateException("Cannot find e4 application", e);
127 }
128 throw new IllegalStateException("Cannot find e4 application");
129 }
130
131 public static Display createDisplay() {
132 Display.setAppName("Argeo CMS RCP");
133
134 // create the display
135 Display newDisplay = Display.getCurrent();
136 if (newDisplay == null) {
137 newDisplay = new Display();
138 }
139 // Set the priority higher than normal so as to be higher
140 // than the JobManager.
141 Thread.currentThread().setPriority(Math.min(Thread.MAX_PRIORITY, Thread.NORM_PRIORITY + 1));
142 return newDisplay;
143 }
144
145 //
146 // CMS VIEW
147 //
148
149 @Override
150 public UxContext getUxContext() {
151 return uxContext;
152 }
153
154 @Override
155 public void navigateTo(String state) {
156 // TODO Auto-generated method stub
157
158 }
159
160 @Override
161 public void authChange(LoginContext loginContext) {
162 if (loginContext == null)
163 throw new IllegalStateException("Login context cannot be null");
164 // logout previous login context
165 // if (this.loginContext != null)
166 // try {
167 // this.loginContext.logout();
168 // } catch (LoginException e1) {
169 // System.err.println("Could not log out: " + e1);
170 // }
171 this.loginContext = loginContext;
172 }
173
174 @Override
175 public void logout() {
176 if (loginContext == null)
177 throw new IllegalStateException("Login context should not bet null");
178 try {
179 CurrentUser.logoutCmsSession(loginContext.getSubject());
180 loginContext.logout();
181 } catch (LoginException e) {
182 throw new IllegalStateException("Cannot log out", e);
183 }
184 }
185
186 @Override
187 public void exception(Throwable e) {
188 // TODO Auto-generated method stub
189
190 }
191
192 @Override
193 public CmsImageManager getImageManager() {
194 // TODO Auto-generated method stub
195 return null;
196 }
197
198 protected Subject getSubject() {
199 return loginContext.getSubject();
200 }
201
202 @Override
203 public boolean isAnonymous() {
204 return CurrentUser.isAnonymous(getSubject());
205 }
206
207 @Override
208 public String getUid() {
209 return uid;
210 }
211
212 @Override
213 public <T> T doAs(Callable<T> action) {
214 throw new UnsupportedOperationException();
215 }
216
217 @Override
218 public TimerTask schedule(Runnable task, long delay) {
219 throw new UnsupportedOperationException();
220 }
221
222 @Override
223 public TimerTask schedule(Runnable task, long delay, long period) {
224 throw new UnsupportedOperationException();
225 }
226
227 @Override
228 public URI toBackendUri(String url) {
229 try {
230 URI u = new URI(url);
231 if (u.getHost() == null) {
232 // TODO make it more robust
233 u = new URI(httpServerBase + url);
234 }
235 return u;
236 } catch (URISyntaxException e) {
237 throw new IllegalArgumentException("Cannot convert " + url, e);
238 }
239 }
240
241 public void setHttpServerBase(String httpServerBase) {
242 this.httpServerBase = httpServerBase;
243 }
244
245 }