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