1 package org
.argeo
.cms
.e4
.rcp
;
3 import java
.security
.PrivilegedExceptionAction
;
5 import java
.util
.concurrent
.Callable
;
7 import javax
.security
.auth
.Subject
;
8 import javax
.security
.auth
.login
.LoginContext
;
9 import javax
.security
.auth
.login
.LoginException
;
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
.auth
.CurrentUser
;
16 import org
.argeo
.cms
.swt
.CmsException
;
17 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
18 import org
.argeo
.cms
.swt
.SimpleSwtUxContext
;
19 import org
.argeo
.cms
.swt
.auth
.CmsLoginShell
;
20 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
21 import org
.eclipse
.core
.runtime
.IExtension
;
22 import org
.eclipse
.core
.runtime
.Platform
;
23 import org
.eclipse
.equinox
.app
.IApplication
;
24 import org
.eclipse
.equinox
.app
.IApplicationContext
;
25 import org
.eclipse
.swt
.widgets
.Display
;
27 public class CmsE4Application
implements IApplication
, CmsView
{
28 private LoginContext loginContext
;
29 private IApplication e4Application
;
30 private UxContext uxContext
;
34 public Object
start(IApplicationContext context
) throws Exception
{
35 // TODO wait for CMS to be ready
38 uid
= UUID
.randomUUID().toString();
39 Subject subject
= new Subject();
40 Display display
= createDisplay();
41 CmsLoginShell loginShell
= new CmsLoginShell(this, null);
42 // TODO customize CmsLoginShell to be smaller and centered
43 loginShell
.setSubject(subject
);
46 loginContext
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_SINGLE_USER
, subject
, loginShell
);
48 } catch (LoginException e
) {
50 loginShell
.createUi();
53 while (!loginShell
.getShell().isDisposed()) {
54 if (!display
.readAndDispatch())
58 if (CurrentUser
.getUsername(getSubject()) == null)
59 throw new IllegalStateException("Cannot log in");
62 // CallbackHandler callbackHandler = new DefaultLoginDialog(
63 // display.getActiveShell());
64 // loginContext = new LoginContext(
65 // NodeConstants.LOGIN_CONTEXT_SINGLE_USER, subject,
67 // } catch (LoginException e1) {
68 // throw new CmsException("Cannot initialize login context", e1);
73 // loginContext.login();
74 // subject = loginContext.getSubject();
75 // } catch (LoginException e) {
76 // e.printStackTrace();
79 // Thread.sleep(2000);
80 // } catch (InterruptedException e1) {
86 uxContext
= new SimpleSwtUxContext();
87 // UiContext.setData(CmsView.KEY, this);
88 CmsSwtUtils
.registerCmsView(loginShell
.getShell(), this);
89 e4Application
= getApplication(null);
90 Object res
= Subject
.doAs(subject
, new PrivilegedExceptionAction
<Object
>() {
93 public Object
run() throws Exception
{
94 return e4Application
.start(context
);
103 if (e4Application
!= null)
104 e4Application
.stop();
107 static IApplication
getApplication(String
[] args
) {
108 IExtension extension
= Platform
.getExtensionRegistry().getExtension(Platform
.PI_RUNTIME
,
109 Platform
.PT_APPLICATIONS
, "org.eclipse.e4.ui.workbench.swt.E4Application");
111 IConfigurationElement
[] elements
= extension
.getConfigurationElements();
112 if (elements
.length
> 0) {
113 IConfigurationElement
[] runs
= elements
[0].getChildren("run");
114 if (runs
.length
> 0) {
116 runnable
= runs
[0].createExecutableExtension("class");
117 if (runnable
instanceof IApplication
)
118 return (IApplication
) runnable
;
121 } catch (Exception e
) {
122 throw new IllegalStateException("Cannot find e4 application", e
);
124 throw new IllegalStateException("Cannot find e4 application");
127 public static Display
createDisplay() {
128 Display
.setAppName("Argeo CMS RCP");
130 // create the display
131 Display newDisplay
= Display
.getCurrent();
132 if (newDisplay
== null) {
133 newDisplay
= new Display();
135 // Set the priority higher than normal so as to be higher
136 // than the JobManager.
137 Thread
.currentThread().setPriority(Math
.min(Thread
.MAX_PRIORITY
, Thread
.NORM_PRIORITY
+ 1));
146 public UxContext
getUxContext() {
151 public void navigateTo(String state
) {
152 // TODO Auto-generated method stub
157 public void authChange(LoginContext loginContext
) {
158 if (loginContext
== null)
159 throw new CmsException("Login context cannot be null");
160 // logout previous login context
161 // if (this.loginContext != null)
163 // this.loginContext.logout();
164 // } catch (LoginException e1) {
165 // System.err.println("Could not log out: " + e1);
167 this.loginContext
= loginContext
;
171 public void logout() {
172 if (loginContext
== null)
173 throw new CmsException("Login context should not bet null");
175 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
176 loginContext
.logout();
177 } catch (LoginException e
) {
178 throw new CmsException("Cannot log out", e
);
183 public void exception(Throwable e
) {
184 // TODO Auto-generated method stub
189 public CmsImageManager
getImageManager() {
190 // TODO Auto-generated method stub
194 protected Subject
getSubject() {
195 return loginContext
.getSubject();
199 public boolean isAnonymous() {
200 return CurrentUser
.isAnonymous(getSubject());
204 public String
getUid() {
209 public <T
> T
doAs(Callable
<T
> action
) {
210 throw new UnsupportedOperationException();