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