2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.security
.ui
.rcp
;
18 import java
.security
.PrivilegedAction
;
20 import javax
.security
.auth
.Subject
;
21 import javax
.security
.auth
.login
.LoginException
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.OperatingSystem
;
26 import org
.eclipse
.equinox
.app
.IApplication
;
27 import org
.eclipse
.equinox
.app
.IApplicationContext
;
28 import org
.eclipse
.equinox
.security
.auth
.ILoginContext
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.ui
.IWorkbench
;
31 import org
.eclipse
.ui
.PlatformUI
;
32 import org
.eclipse
.ui
.application
.WorkbenchAdvisor
;
35 * RCP workbench initialization
37 public abstract class AbstractSecureApplication
implements IApplication
{
38 final static String NODE_REPO_URI
= "argeo.node.repo.uri";
40 private static final Log log
= LogFactory
41 .getLog(AbstractSecureApplication
.class);
43 protected WorkbenchAdvisor
createWorkbenchAdvisor(String username
) {
44 return new SecureWorkbenchAdvisor(username
);
47 public Object
start(IApplicationContext context
) throws Exception
{
48 // wait for the system to be initialized
50 // Thread.sleep(3000);
51 // } catch (Exception e2) {
55 boolean remote
= System
.getProperty(NODE_REPO_URI
) != null;
57 // choose login context
58 final ILoginContext loginContext
;
60 loginContext
= SecureApplicationActivator
61 .createLoginContext(SecureApplicationActivator
.CONTEXT_REMOTE
);
63 if (OperatingSystem
.os
== OperatingSystem
.WINDOWS
)
64 loginContext
= SecureApplicationActivator
65 .createLoginContext(SecureApplicationActivator
.CONTEXT_WINDOWS
);
67 loginContext
= SecureApplicationActivator
68 .createLoginContext(SecureApplicationActivator
.CONTEXT_NIX
);
71 final Display display
= PlatformUI
.createDisplay();
74 Subject subject
= null;
77 subject
= loginContext
.getSubject();
78 } catch (LoginException e
) {
79 log
.error("Error when logging in.", e
);
83 } catch (InterruptedException e1
) {
89 // identify after successful login
90 if (log
.isDebugEnabled())
91 log
.debug("subject=" + subject
);
92 final String username
= subject
.getPrincipals().iterator().next()
94 if (log
.isDebugEnabled())
95 log
.debug(username
+ " logged in");
96 // display.disposeExec(new Runnable() {
97 // public void run() {
98 // log.debug("Display disposed");
99 // logout(loginContext, username);
104 PrivilegedAction
<?
> privilegedAction
= new PrivilegedAction
<Object
>() {
105 public Object
run() {
106 int result
= PlatformUI
.createAndRunWorkbench(display
,
107 createWorkbenchAdvisor(username
));
108 return new Integer(result
);
112 Integer returnCode
= (Integer
) Subject
.doAs(subject
,
114 logout(loginContext
, username
);
115 return processReturnCode(returnCode
);
116 } catch (Exception e
) {
118 logout(loginContext
, username
);
119 log
.error("Unexpected error", e
);
126 protected Integer
processReturnCode(Integer returnCode
) {
127 if (returnCode
== PlatformUI
.RETURN_RESTART
)
128 return IApplication
.EXIT_RESTART
;
130 return IApplication
.EXIT_OK
;
133 static void logout(ILoginContext secureContext
, String username
) {
135 secureContext
.logout();
136 log
.info("Logged out " + (username
!= null ? username
: "")
137 + " (THREAD=" + Thread
.currentThread().getId() + ")");
138 } catch (LoginException e
) {
139 log
.error("Erorr when logging out", e
);
144 final IWorkbench workbench
;
146 workbench
= PlatformUI
.getWorkbench();
147 } catch (Exception e
) {
150 if (workbench
== null)
152 final Display display
= workbench
.getDisplay();
153 if (display
!= null && !display
.isDisposed())
154 display
.syncExec(new Runnable() {
157 if (!display
.isDisposed())
162 if (log
.isDebugEnabled())
163 log
.debug("workbench stopped");