]> git.argeo.org Git - lgpl/argeo-commons.git/blob - rcp/org.argeo.cms.e4.rcp/src/org/argeo/cms/e4/rcp/CmsE4Application.java
Improve BND usage in Makefile
[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 import java.util.UUID;
5
6 import javax.security.auth.Subject;
7 import javax.security.auth.login.LoginContext;
8 import javax.security.auth.login.LoginException;
9
10 import org.argeo.api.cms.CmsAuth;
11 import org.argeo.api.cms.CmsImageManager;
12 import org.argeo.api.cms.CmsView;
13 import org.argeo.api.cms.UxContext;
14 import org.argeo.cms.CmsException;
15 import org.argeo.cms.auth.CurrentUser;
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;
25
26 public class CmsE4Application implements IApplication, CmsView {
27 private LoginContext loginContext;
28 private IApplication e4Application;
29 private UxContext uxContext;
30 private String uid;
31
32 @Override
33 public Object start(IApplicationContext context) throws Exception {
34 uid = UUID.randomUUID().toString();
35 Subject subject = new Subject();
36 Display display = createDisplay();
37 CmsLoginShell loginShell = new CmsLoginShell(this);
38 // TODO customize CmsLoginShell to be smaller and centered
39 loginShell.setSubject(subject);
40 try {
41 // try pre-auth
42 loginContext = new LoginContext(CmsAuth.LOGIN_CONTEXT_USER, subject, loginShell);
43 loginContext.login();
44 } catch (LoginException e) {
45 e.printStackTrace();
46 loginShell.createUi();
47 loginShell.open();
48
49 while (!loginShell.getShell().isDisposed()) {
50 if (!display.readAndDispatch())
51 display.sleep();
52 }
53 }
54 if (CurrentUser.getUsername(getSubject()) == null)
55 throw new CmsException("Cannot log in");
56
57 // try {
58 // CallbackHandler callbackHandler = new DefaultLoginDialog(
59 // display.getActiveShell());
60 // loginContext = new LoginContext(
61 // NodeConstants.LOGIN_CONTEXT_SINGLE_USER, subject,
62 // callbackHandler);
63 // } catch (LoginException e1) {
64 // throw new CmsException("Cannot initialize login context", e1);
65 // }
66 //
67 // // login
68 // try {
69 // loginContext.login();
70 // subject = loginContext.getSubject();
71 // } catch (LoginException e) {
72 // e.printStackTrace();
73 // display.dispose();
74 // try {
75 // Thread.sleep(2000);
76 // } catch (InterruptedException e1) {
77 // // silent
78 // }
79 // return null;
80 // }
81
82 uxContext = new SimpleSwtUxContext();
83 //UiContext.setData(CmsView.KEY, this);
84 CmsSwtUtils.registerCmsView(loginShell.getShell(), this);
85 e4Application = getApplication(null);
86 Object res = Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
87
88 @Override
89 public Object run() throws Exception {
90 return e4Application.start(context);
91 }
92
93 });
94 return res;
95 }
96
97 @Override
98 public void stop() {
99 if (e4Application != null)
100 e4Application.stop();
101 }
102
103 static IApplication getApplication(String[] args) {
104 IExtension extension = Platform.getExtensionRegistry().getExtension(Platform.PI_RUNTIME,
105 Platform.PT_APPLICATIONS, "org.eclipse.e4.ui.workbench.swt.E4Application");
106 try {
107 IConfigurationElement[] elements = extension.getConfigurationElements();
108 if (elements.length > 0) {
109 IConfigurationElement[] runs = elements[0].getChildren("run");
110 if (runs.length > 0) {
111 Object runnable;
112 runnable = runs[0].createExecutableExtension("class");
113 if (runnable instanceof IApplication)
114 return (IApplication) runnable;
115 }
116 }
117 } catch (Exception e) {
118 throw new IllegalStateException("Cannot find e4 application", e);
119 }
120 throw new IllegalStateException("Cannot find e4 application");
121 }
122
123 public static Display createDisplay() {
124 Display.setAppName("Argeo CMS RCP");
125
126 // create the display
127 Display newDisplay = Display.getCurrent();
128 if (newDisplay == null) {
129 newDisplay = new Display();
130 }
131 // Set the priority higher than normal so as to be higher
132 // than the JobManager.
133 Thread.currentThread().setPriority(Math.min(Thread.MAX_PRIORITY, Thread.NORM_PRIORITY + 1));
134 return newDisplay;
135 }
136
137 //
138 // CMS VIEW
139 //
140
141 @Override
142 public UxContext getUxContext() {
143 return uxContext;
144 }
145
146 @Override
147 public void navigateTo(String state) {
148 // TODO Auto-generated method stub
149
150 }
151
152 @Override
153 public void authChange(LoginContext loginContext) {
154 if (loginContext == null)
155 throw new CmsException("Login context cannot be null");
156 // logout previous login context
157 // if (this.loginContext != null)
158 // try {
159 // this.loginContext.logout();
160 // } catch (LoginException e1) {
161 // System.err.println("Could not log out: " + e1);
162 // }
163 this.loginContext = loginContext;
164 }
165
166 @Override
167 public void logout() {
168 if (loginContext == null)
169 throw new CmsException("Login context should not bet null");
170 try {
171 CurrentUser.logoutCmsSession(loginContext.getSubject());
172 loginContext.logout();
173 } catch (LoginException e) {
174 throw new CmsException("Cannot log out", e);
175 }
176 }
177
178 @Override
179 public void exception(Throwable e) {
180 // TODO Auto-generated method stub
181
182 }
183
184 @Override
185 public CmsImageManager getImageManager() {
186 // TODO Auto-generated method stub
187 return null;
188 }
189
190 protected Subject getSubject() {
191 return loginContext.getSubject();
192 }
193
194 @Override
195 public boolean isAnonymous() {
196 return CurrentUser.isAnonymous(getSubject());
197 }
198
199 @Override
200 public String getUid() {
201 return uid;
202 }
203
204
205 }