]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.rcp/src/org/argeo/security/ui/rcp/AbstractSecureApplication.java
75184cbab6110c7848a040285dd78870bc00c0cf
[lgpl/argeo-commons.git] / org.argeo.security.ui.rcp / src / org / argeo / security / ui / rcp / AbstractSecureApplication.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.security.ui.rcp;
17
18 import java.security.PrivilegedAction;
19
20 import javax.security.auth.Subject;
21 import javax.security.auth.login.LoginException;
22
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;
33
34 /**
35 * RCP workbench initialization
36 */
37 public abstract class AbstractSecureApplication implements IApplication {
38 final static String NODE_REPO_URI = "argeo.node.repo.uri";
39
40 private static final Log log = LogFactory
41 .getLog(AbstractSecureApplication.class);
42
43 protected WorkbenchAdvisor createWorkbenchAdvisor(String username) {
44 return new SecureWorkbenchAdvisor(username);
45 }
46
47 public Object start(IApplicationContext context) throws Exception {
48 // wait for the system to be initialized
49 // try {
50 // Thread.sleep(3000);
51 // } catch (Exception e2) {
52 // // silent
53 // }
54
55 boolean remote = System.getProperty(NODE_REPO_URI) != null;
56
57 // choose login context
58 final ILoginContext loginContext;
59 if (remote) {
60 loginContext = SecureApplicationActivator
61 .createLoginContext(SecureApplicationActivator.CONTEXT_REMOTE);
62 } else {
63 if (OperatingSystem.os == OperatingSystem.WINDOWS)
64 loginContext = SecureApplicationActivator
65 .createLoginContext(SecureApplicationActivator.CONTEXT_WINDOWS);
66 else
67 loginContext = SecureApplicationActivator
68 .createLoginContext(SecureApplicationActivator.CONTEXT_NIX);
69 }
70
71 final Display display = PlatformUI.createDisplay();
72
73 // login
74 Subject subject = null;
75 try {
76 loginContext.login();
77 subject = loginContext.getSubject();
78 } catch (LoginException e) {
79 log.error("Error when logging in.", e);
80 display.dispose();
81 try {
82 Thread.sleep(2000);
83 } catch (InterruptedException e1) {
84 // silent
85 }
86 return null;
87 }
88
89 // identify after successful login
90 if (log.isDebugEnabled())
91 log.debug("subject=" + subject);
92 final String username = subject.getPrincipals().iterator().next()
93 .getName();
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);
100 // }
101 // });
102
103 try {
104 PrivilegedAction<?> privilegedAction = new PrivilegedAction<Object>() {
105 public Object run() {
106 int result = PlatformUI.createAndRunWorkbench(display,
107 createWorkbenchAdvisor(username));
108 return new Integer(result);
109 }
110 };
111
112 Integer returnCode = (Integer) Subject.doAs(subject,
113 privilegedAction);
114 logout(loginContext, username);
115 return processReturnCode(returnCode);
116 } catch (Exception e) {
117 if (subject != null)
118 logout(loginContext, username);
119 log.error("Unexpected error", e);
120 } finally {
121 display.dispose();
122 }
123 return null;
124 }
125
126 protected Integer processReturnCode(Integer returnCode) {
127 if (returnCode == PlatformUI.RETURN_RESTART)
128 return IApplication.EXIT_RESTART;
129 else
130 return IApplication.EXIT_OK;
131 }
132
133 static void logout(ILoginContext secureContext, String username) {
134 try {
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);
140 }
141 }
142
143 public void stop() {
144 final IWorkbench workbench;
145 try {
146 workbench = PlatformUI.getWorkbench();
147 } catch (Exception e) {
148 return;
149 }
150 if (workbench == null)
151 return;
152 final Display display = workbench.getDisplay();
153 if (display != null && !display.isDisposed())
154 display.syncExec(new Runnable() {
155
156 public void run() {
157 if (!display.isDisposed())
158 workbench.close();
159 }
160 });
161
162 if (log.isDebugEnabled())
163 log.debug("workbench stopped");
164 }
165
166 }