1 package org
.argeo
.cms
.e4
.rcp
;
4 import java
.net
.URISyntaxException
;
5 import java
.security
.PrivilegedExceptionAction
;
6 import java
.util
.TimerTask
;
8 import java
.util
.concurrent
.Callable
;
10 import javax
.security
.auth
.Subject
;
11 import javax
.security
.auth
.login
.LoginContext
;
12 import javax
.security
.auth
.login
.LoginException
;
14 import org
.argeo
.api
.cms
.CmsAuth
;
15 import org
.argeo
.api
.cms
.ux
.CmsImageManager
;
16 import org
.argeo
.api
.cms
.ux
.CmsView
;
17 import org
.argeo
.api
.cms
.ux
.UxContext
;
18 import org
.argeo
.cms
.CurrentUser
;
19 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
20 import org
.argeo
.cms
.swt
.SimpleSwtUxContext
;
21 import org
.argeo
.cms
.swt
.auth
.CmsLoginShell
;
22 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
23 import org
.eclipse
.core
.runtime
.IExtension
;
24 import org
.eclipse
.core
.runtime
.Platform
;
25 import org
.eclipse
.equinox
.app
.IApplication
;
26 import org
.eclipse
.equinox
.app
.IApplicationContext
;
27 import org
.eclipse
.swt
.widgets
.Display
;
29 public class CmsE4Application
implements IApplication
, CmsView
{
30 private LoginContext loginContext
;
31 private IApplication e4Application
;
32 private UxContext uxContext
;
35 private String httpServerBase
;
38 public Object
start(IApplicationContext context
) throws Exception
{
39 // TODO wait for CMS to be ready
42 uid
= UUID
.randomUUID().toString();
43 Subject subject
= new Subject();
44 Display display
= createDisplay();
45 CmsLoginShell loginShell
= new CmsLoginShell(this, null);
46 // TODO customize CmsLoginShell to be smaller and centered
47 loginShell
.setSubject(subject
);
50 loginContext
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_SINGLE_USER
, subject
, loginShell
);
52 } catch (LoginException e
) {
54 loginShell
.createUi();
57 while (!loginShell
.getShell().isDisposed()) {
58 if (!display
.readAndDispatch())
62 if (CurrentUser
.getUsername(getSubject()) == null)
63 throw new IllegalStateException("Cannot log in");
66 // CallbackHandler callbackHandler = new DefaultLoginDialog(
67 // display.getActiveShell());
68 // loginContext = new LoginContext(
69 // NodeConstants.LOGIN_CONTEXT_SINGLE_USER, subject,
71 // } catch (LoginException e1) {
72 // throw new CmsException("Cannot initialize login context", e1);
77 // loginContext.login();
78 // subject = loginContext.getSubject();
79 // } catch (LoginException e) {
80 // e.printStackTrace();
83 // Thread.sleep(2000);
84 // } catch (InterruptedException e1) {
90 uxContext
= new SimpleSwtUxContext();
91 // UiContext.setData(CmsView.KEY, this);
92 CmsSwtUtils
.registerCmsView(loginShell
.getShell(), this);
93 e4Application
= getApplication(null);
94 Object res
= Subject
.doAs(subject
, new PrivilegedExceptionAction
<Object
>() {
97 public Object
run() throws Exception
{
98 return e4Application
.start(context
);
107 if (e4Application
!= null)
108 e4Application
.stop();
111 static IApplication
getApplication(String
[] args
) {
112 IExtension extension
= Platform
.getExtensionRegistry().getExtension(Platform
.PI_RUNTIME
,
113 Platform
.PT_APPLICATIONS
, "org.eclipse.e4.ui.workbench.swt.E4Application");
115 IConfigurationElement
[] elements
= extension
.getConfigurationElements();
116 if (elements
.length
> 0) {
117 IConfigurationElement
[] runs
= elements
[0].getChildren("run");
118 if (runs
.length
> 0) {
120 runnable
= runs
[0].createExecutableExtension("class");
121 if (runnable
instanceof IApplication
)
122 return (IApplication
) runnable
;
125 } catch (Exception e
) {
126 throw new IllegalStateException("Cannot find e4 application", e
);
128 throw new IllegalStateException("Cannot find e4 application");
131 public static Display
createDisplay() {
132 Display
.setAppName("Argeo CMS RCP");
134 // create the display
135 Display newDisplay
= Display
.getCurrent();
136 if (newDisplay
== null) {
137 newDisplay
= new Display();
139 // Set the priority higher than normal so as to be higher
140 // than the JobManager.
141 Thread
.currentThread().setPriority(Math
.min(Thread
.MAX_PRIORITY
, Thread
.NORM_PRIORITY
+ 1));
150 public UxContext
getUxContext() {
155 public void navigateTo(String state
) {
156 // TODO Auto-generated method stub
161 public void authChange(LoginContext loginContext
) {
162 if (loginContext
== null)
163 throw new IllegalStateException("Login context cannot be null");
164 // logout previous login context
165 // if (this.loginContext != null)
167 // this.loginContext.logout();
168 // } catch (LoginException e1) {
169 // System.err.println("Could not log out: " + e1);
171 this.loginContext
= loginContext
;
175 public void logout() {
176 if (loginContext
== null)
177 throw new IllegalStateException("Login context should not bet null");
179 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
180 loginContext
.logout();
181 } catch (LoginException e
) {
182 throw new IllegalStateException("Cannot log out", e
);
187 public void exception(Throwable e
) {
188 // TODO Auto-generated method stub
193 public CmsImageManager
getImageManager() {
194 // TODO Auto-generated method stub
198 protected Subject
getSubject() {
199 return loginContext
.getSubject();
203 public boolean isAnonymous() {
204 return CurrentUser
.isAnonymous(getSubject());
208 public String
getUid() {
213 public <T
> T
doAs(Callable
<T
> action
) {
214 throw new UnsupportedOperationException();
218 public TimerTask
schedule(Runnable task
, long delay
) {
219 throw new UnsupportedOperationException();
223 public TimerTask
schedule(Runnable task
, long delay
, long period
) {
224 throw new UnsupportedOperationException();
228 public URI
toBackendUri(String url
) {
230 URI u
= new URI(url
);
231 if (u
.getHost() == null) {
232 // TODO make it more robust
233 u
= new URI(httpServerBase
+ url
);
236 } catch (URISyntaxException e
) {
237 throw new IllegalArgumentException("Cannot convert " + url
, e
);
241 public void setHttpServerBase(String httpServerBase
) {
242 this.httpServerBase
= httpServerBase
;