1 package org
.argeo
.security
.ui
.admin
.internal
;
3 import java
.security
.AccessController
;
4 import java
.security
.Principal
;
6 import javax
.security
.auth
.Subject
;
7 import javax
.security
.auth
.x500
.X500Principal
;
8 import javax
.transaction
.Status
;
9 import javax
.transaction
.UserTransaction
;
11 import org
.argeo
.ArgeoException
;
12 import org
.eclipse
.ui
.IWorkbenchWindow
;
13 import org
.eclipse
.ui
.PlatformUI
;
14 import org
.eclipse
.ui
.services
.ISourceProviderService
;
15 import org
.osgi
.service
.useradmin
.Role
;
16 import org
.osgi
.service
.useradmin
.User
;
18 /** First effort to centralize back end methods used by the user admin UI */
19 public class UiAdminUtils
{
20 public final static String
getUsername() {
21 Subject subject
= Subject
.getSubject(AccessController
.getContext());
22 Principal principal
= subject
.getPrincipals(X500Principal
.class)
24 return principal
.getName();
28 public final static String
getUsername(User user
) {
29 String cn
= getProperty(user
, UserAdminConstants
.KEY_CN
);
31 cn
= getProperty(user
, UserAdminConstants
.KEY_UID
);
35 public final static String
getProperty(Role role
, String key
) {
36 Object obj
= role
.getProperties().get(key
);
43 public final static String
getDefaultCn(String firstName
, String lastName
) {
44 return (firstName
.trim() + " " + lastName
.trim() + " ").trim();
48 * INTERNAL METHODS: Below methods are meant to stay here and are not part
49 * of a potential generic backend to manage the useradmin
51 public final static boolean notNull(String string
) {
55 return !"".equals(string
.trim());
58 public final static boolean isEmpty(String string
) {
62 return "".equals(string
.trim());
65 /** Must be called from the UI Thread. */
66 public final static void beginTransactionIfNeeded(
67 UserTransaction userTransaction
) {
69 if (userTransaction
.getStatus() == Status
.STATUS_NO_TRANSACTION
) {
70 userTransaction
.begin();
71 notifyTransactionStateChange(userTransaction
);
73 } catch (Exception e
) {
74 throw new ArgeoException("Unable to begin transaction", e
);
78 /** Easily notify the ActiveWindow that the transaction had a state change */
79 public final static void notifyTransactionStateChange(
80 UserTransaction userTransaction
) {
82 IWorkbenchWindow aww
= PlatformUI
.getWorkbench()
83 .getActiveWorkbenchWindow();
84 ISourceProviderService sourceProviderService
= (ISourceProviderService
) aww
85 .getService(ISourceProviderService
.class);
86 UserTransactionProvider esp
= (UserTransactionProvider
) sourceProviderService
87 .getSourceProvider(UserTransactionProvider
.TRANSACTION_STATE
);
88 esp
.setUserTransaction(userTransaction
);
89 esp
.fireTransactionStateChange();
90 } catch (Exception e
) {
91 throw new ArgeoException("Unable to begin transaction", e
);