2 * Copyright (C) 2007-2012 Argeo GmbH
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
.admin
.internal
.commands
;
18 import javax
.transaction
.Status
;
19 import javax
.transaction
.UserTransaction
;
21 import org
.argeo
.ArgeoException
;
22 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
23 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
24 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminWrapper
;
25 import org
.eclipse
.core
.commands
.AbstractHandler
;
26 import org
.eclipse
.core
.commands
.ExecutionEvent
;
27 import org
.eclipse
.core
.commands
.ExecutionException
;
28 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
30 /** Manage the transaction that is bound to the current perspective */
31 public class UserTransactionHandler
extends AbstractHandler
{
32 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
33 + ".userTransactionHandler";
35 public final static String PARAM_COMMAND_ID
= "param.commandId";
37 public final static String TRANSACTION_BEGIN
= "transaction.begin";
38 public final static String TRANSACTION_COMMIT
= "transaction.commit";
39 public final static String TRANSACTION_ROLLBACK
= "transaction.rollback";
41 /* DEPENDENCY INJECTION */
42 private UserAdminWrapper userAdminWrapper
;
44 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
45 String commandId
= event
.getParameter(PARAM_COMMAND_ID
);
46 UserTransaction userTransaction
= userAdminWrapper
.getUserTransaction();
48 if (TRANSACTION_BEGIN
.equals(commandId
)) {
49 if (userTransaction
.getStatus() != Status
.STATUS_NO_TRANSACTION
)
50 throw new ArgeoException("A transaction already exists");
52 userTransaction
.begin();
53 } else if (TRANSACTION_COMMIT
.equals(commandId
)) {
54 if (userTransaction
.getStatus() == Status
.STATUS_NO_TRANSACTION
)
55 throw new ArgeoException("No transaction.");
57 userTransaction
.commit();
58 } else if (TRANSACTION_ROLLBACK
.equals(commandId
)) {
59 if (userTransaction
.getStatus() == Status
.STATUS_NO_TRANSACTION
)
60 throw new ArgeoException("No transaction to rollback.");
62 userTransaction
.rollback();
63 userAdminWrapper
.notifyListeners(new UserAdminEvent(null,
64 UserAdminEvent
.ROLE_CHANGED
, null));
67 UiAdminUtils
.notifyTransactionStateChange(userTransaction
);
68 } catch (ArgeoException e
) {
70 } catch (Exception e
) {
71 throw new ArgeoException("Unable to call " + commandId
+ " on "
72 + userTransaction
, e
);
77 /* DEPENDENCY INJECTION */
78 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
79 this.userAdminWrapper
= userAdminWrapper
;