]> git.argeo.org Git - lgpl/argeo-commons.git/blob - rcp/org.argeo.cms.e4.rcp/src/org/argeo/cms/e4/rcp/CmsE4Application.java
Prepare release.
[lgpl/argeo-commons.git] / 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
5 import javax.security.auth.Subject;
6 import javax.security.auth.login.LoginContext;
7 import javax.security.auth.login.LoginException;
8
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;
23
24 public class CmsE4Application implements IApplication, CmsView {
25 private LoginContext loginContext;
26 private IApplication e4Application;
27 private UxContext uxContext;
28
29 @Override
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);
36 try {
37 // try pre-auth
38 loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, loginShell);
39 loginContext.login();
40 } catch (LoginException e) {
41 e.printStackTrace();
42 loginShell.createUi();
43 loginShell.open();
44
45 while (!loginShell.getShell().isDisposed()) {
46 if (!display.readAndDispatch())
47 display.sleep();
48 }
49 }
50 if (CurrentUser.getUsername(getSubject()) == null)
51 throw new CmsException("Cannot log in");
52
53 // try {
54 // CallbackHandler callbackHandler = new DefaultLoginDialog(
55 // display.getActiveShell());
56 // loginContext = new LoginContext(
57 // NodeConstants.LOGIN_CONTEXT_SINGLE_USER, subject,
58 // callbackHandler);
59 // } catch (LoginException e1) {
60 // throw new CmsException("Cannot initialize login context", e1);
61 // }
62 //
63 // // login
64 // try {
65 // loginContext.login();
66 // subject = loginContext.getSubject();
67 // } catch (LoginException e) {
68 // e.printStackTrace();
69 // display.dispose();
70 // try {
71 // Thread.sleep(2000);
72 // } catch (InterruptedException e1) {
73 // // silent
74 // }
75 // return null;
76 // }
77
78 uxContext = new SimpleUxContext();
79
80 e4Application = getApplication(null);
81 Object res = Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
82
83 @Override
84 public Object run() throws Exception {
85 return e4Application.start(context);
86 }
87
88 });
89 return res;
90 }
91
92 @Override
93 public void stop() {
94 if (e4Application != null)
95 e4Application.stop();
96 }
97
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");
101 try {
102 IConfigurationElement[] elements = extension.getConfigurationElements();
103 if (elements.length > 0) {
104 IConfigurationElement[] runs = elements[0].getChildren("run");
105 if (runs.length > 0) {
106 Object runnable;
107 runnable = runs[0].createExecutableExtension("class");
108 if (runnable instanceof IApplication)
109 return (IApplication) runnable;
110 }
111 }
112 } catch (Exception e) {
113 throw new IllegalStateException("Cannot find e4 application", e);
114 }
115 throw new IllegalStateException("Cannot find e4 application");
116 }
117
118 public static Display createDisplay() {
119 Display.setAppName("Argeo CMS RCP");
120
121 // create the display
122 Display newDisplay = Display.getCurrent();
123 if (newDisplay == null) {
124 newDisplay = new Display();
125 }
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));
129 return newDisplay;
130 }
131
132 //
133 // CMS VIEW
134 //
135
136 @Override
137 public UxContext getUxContext() {
138 return uxContext;
139 }
140
141 @Override
142 public void navigateTo(String state) {
143 // TODO Auto-generated method stub
144
145 }
146
147 @Override
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)
153 // try {
154 // this.loginContext.logout();
155 // } catch (LoginException e1) {
156 // System.err.println("Could not log out: " + e1);
157 // }
158 this.loginContext = loginContext;
159 }
160
161 @Override
162 public void logout() {
163 if (loginContext == null)
164 throw new CmsException("Login context should not bet null");
165 try {
166 CurrentUser.logoutCmsSession(loginContext.getSubject());
167 loginContext.logout();
168 } catch (LoginException e) {
169 throw new CmsException("Cannot log out", e);
170 }
171 }
172
173 @Override
174 public void exception(Throwable e) {
175 // TODO Auto-generated method stub
176
177 }
178
179 @Override
180 public CmsImageManager getImageManager() {
181 // TODO Auto-generated method stub
182 return null;
183 }
184
185 protected Subject getSubject() {
186 return loginContext.getSubject();
187 }
188
189 @Override
190 public boolean isAnonymous() {
191 return CurrentUser.isAnonymous(getSubject());
192 }
193
194 }