1 package org
.argeo
.cms
.e4
.rcp
;
3 import java
.security
.PrivilegedExceptionAction
;
5 import javax
.security
.auth
.Subject
;
6 import javax
.security
.auth
.login
.LoginContext
;
7 import javax
.security
.auth
.login
.LoginException
;
9 import org
.argeo
.cms
.CmsException
;
10 import org
.argeo
.cms
.auth
.CurrentUser
;
11 import org
.argeo
.cms
.ui
.CmsImageManager
;
12 import org
.argeo
.cms
.ui
.CmsView
;
13 import org
.argeo
.cms
.ui
.UxContext
;
14 import org
.argeo
.cms
.util
.SimpleUxContext
;
15 import org
.argeo
.cms
.widgets
.auth
.CmsLoginShell
;
16 import org
.argeo
.node
.NodeConstants
;
17 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
18 import org
.eclipse
.core
.runtime
.IExtension
;
19 import org
.eclipse
.core
.runtime
.Platform
;
20 import org
.eclipse
.equinox
.app
.IApplication
;
21 import org
.eclipse
.equinox
.app
.IApplicationContext
;
22 import org
.eclipse
.swt
.widgets
.Display
;
24 public class CmsE4Application
implements IApplication
, CmsView
{
25 private LoginContext loginContext
;
26 private IApplication e4Application
;
27 private UxContext uxContext
;
30 public Object
start(IApplicationContext context
) throws Exception
{
31 Subject subject
= new Subject();
32 Display display
= createDisplay();
33 CmsLoginShell loginShell
= new CmsLoginShell(this);
34 // TODO customize CmsLoginShell to be smaller and centered
35 loginShell
.setSubject(subject
);
38 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, subject
, loginShell
);
40 } catch (LoginException e
) {
42 loginShell
.createUi();
45 while (!loginShell
.getShell().isDisposed()) {
46 if (!display
.readAndDispatch())
50 if (CurrentUser
.getUsername(getSubject()) == null)
51 throw new CmsException("Cannot log in");
54 // CallbackHandler callbackHandler = new DefaultLoginDialog(
55 // display.getActiveShell());
56 // loginContext = new LoginContext(
57 // NodeConstants.LOGIN_CONTEXT_SINGLE_USER, subject,
59 // } catch (LoginException e1) {
60 // throw new CmsException("Cannot initialize login context", e1);
65 // loginContext.login();
66 // subject = loginContext.getSubject();
67 // } catch (LoginException e) {
68 // e.printStackTrace();
71 // Thread.sleep(2000);
72 // } catch (InterruptedException e1) {
78 uxContext
= new SimpleUxContext();
80 e4Application
= getApplication(null);
81 Object res
= Subject
.doAs(subject
, new PrivilegedExceptionAction
<Object
>() {
84 public Object
run() throws Exception
{
85 return e4Application
.start(context
);
94 if (e4Application
!= null)
98 static IApplication
getApplication(String
[] args
) {
99 IExtension extension
= Platform
.getExtensionRegistry().getExtension(Platform
.PI_RUNTIME
,
100 Platform
.PT_APPLICATIONS
, "org.eclipse.e4.ui.workbench.swt.E4Application");
102 IConfigurationElement
[] elements
= extension
.getConfigurationElements();
103 if (elements
.length
> 0) {
104 IConfigurationElement
[] runs
= elements
[0].getChildren("run");
105 if (runs
.length
> 0) {
107 runnable
= runs
[0].createExecutableExtension("class");
108 if (runnable
instanceof IApplication
)
109 return (IApplication
) runnable
;
112 } catch (Exception e
) {
113 throw new IllegalStateException("Cannot find e4 application", e
);
115 throw new IllegalStateException("Cannot find e4 application");
118 public static Display
createDisplay() {
119 Display
.setAppName("Argeo CMS RCP");
121 // create the display
122 Display newDisplay
= Display
.getCurrent();
123 if (newDisplay
== null) {
124 newDisplay
= new Display();
126 // Set the priority higher than normal so as to be higher
127 // than the JobManager.
128 Thread
.currentThread().setPriority(Math
.min(Thread
.MAX_PRIORITY
, Thread
.NORM_PRIORITY
+ 1));
137 public UxContext
getUxContext() {
142 public void navigateTo(String state
) {
143 // TODO Auto-generated method stub
148 public void authChange(LoginContext loginContext
) {
149 if (loginContext
== null)
150 throw new CmsException("Login context cannot be null");
151 // logout previous login context
152 // if (this.loginContext != null)
154 // this.loginContext.logout();
155 // } catch (LoginException e1) {
156 // System.err.println("Could not log out: " + e1);
158 this.loginContext
= loginContext
;
162 public void logout() {
163 if (loginContext
== null)
164 throw new CmsException("Login context should not bet null");
166 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
167 loginContext
.logout();
168 } catch (LoginException e
) {
169 throw new CmsException("Cannot log out", e
);
174 public void exception(Throwable e
) {
175 // TODO Auto-generated method stub
180 public CmsImageManager
getImageManager() {
181 // TODO Auto-generated method stub
185 protected Subject
getSubject() {
186 return loginContext
.getSubject();
190 public boolean isAnonymous() {
191 return CurrentUser
.isAnonymous(getSubject());