1 package org
.argeo
.cms
.e4
.rcp
;
3 import java
.security
.PrivilegedExceptionAction
;
6 import javax
.security
.auth
.Subject
;
7 import javax
.security
.auth
.login
.LoginContext
;
8 import javax
.security
.auth
.login
.LoginException
;
10 import org
.argeo
.api
.cms
.CmsAuth
;
11 import org
.argeo
.api
.cms
.ux
.CmsImageManager
;
12 import org
.argeo
.api
.cms
.ux
.CmsView
;
13 import org
.argeo
.api
.cms
.ux
.UxContext
;
14 import org
.argeo
.cms
.auth
.CurrentUser
;
15 import org
.argeo
.cms
.swt
.CmsException
;
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
;
26 public class CmsE4Application
implements IApplication
, CmsView
{
27 private LoginContext loginContext
;
28 private IApplication e4Application
;
29 private UxContext uxContext
;
33 public Object
start(IApplicationContext context
) throws Exception
{
34 // TODO wait for CMS to be ready
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
);
45 loginContext
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_SINGLE_USER
, subject
, loginShell
);
47 } catch (LoginException e
) {
49 loginShell
.createUi();
52 while (!loginShell
.getShell().isDisposed()) {
53 if (!display
.readAndDispatch())
57 if (CurrentUser
.getUsername(getSubject()) == null)
58 throw new IllegalStateException("Cannot log in");
61 // CallbackHandler callbackHandler = new DefaultLoginDialog(
62 // display.getActiveShell());
63 // loginContext = new LoginContext(
64 // NodeConstants.LOGIN_CONTEXT_SINGLE_USER, subject,
66 // } catch (LoginException e1) {
67 // throw new CmsException("Cannot initialize login context", e1);
72 // loginContext.login();
73 // subject = loginContext.getSubject();
74 // } catch (LoginException e) {
75 // e.printStackTrace();
78 // Thread.sleep(2000);
79 // } catch (InterruptedException e1) {
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
>() {
92 public Object
run() throws Exception
{
93 return e4Application
.start(context
);
102 if (e4Application
!= null)
103 e4Application
.stop();
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");
110 IConfigurationElement
[] elements
= extension
.getConfigurationElements();
111 if (elements
.length
> 0) {
112 IConfigurationElement
[] runs
= elements
[0].getChildren("run");
113 if (runs
.length
> 0) {
115 runnable
= runs
[0].createExecutableExtension("class");
116 if (runnable
instanceof IApplication
)
117 return (IApplication
) runnable
;
120 } catch (Exception e
) {
121 throw new IllegalStateException("Cannot find e4 application", e
);
123 throw new IllegalStateException("Cannot find e4 application");
126 public static Display
createDisplay() {
127 Display
.setAppName("Argeo CMS RCP");
129 // create the display
130 Display newDisplay
= Display
.getCurrent();
131 if (newDisplay
== null) {
132 newDisplay
= new Display();
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));
145 public UxContext
getUxContext() {
150 public void navigateTo(String state
) {
151 // TODO Auto-generated method stub
156 public void authChange(LoginContext loginContext
) {
157 if (loginContext
== null)
158 throw new CmsException("Login context cannot be null");
159 // logout previous login context
160 // if (this.loginContext != null)
162 // this.loginContext.logout();
163 // } catch (LoginException e1) {
164 // System.err.println("Could not log out: " + e1);
166 this.loginContext
= loginContext
;
170 public void logout() {
171 if (loginContext
== null)
172 throw new CmsException("Login context should not bet null");
174 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
175 loginContext
.logout();
176 } catch (LoginException e
) {
177 throw new CmsException("Cannot log out", e
);
182 public void exception(Throwable e
) {
183 // TODO Auto-generated method stub
188 public CmsImageManager
getImageManager() {
189 // TODO Auto-generated method stub
193 protected Subject
getSubject() {
194 return loginContext
.getSubject();
198 public boolean isAnonymous() {
199 return CurrentUser
.isAnonymous(getSubject());
203 public String
getUid() {