Skip to content

Commit 3be274c

Browse files
committed
fix no bean error
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 9ec24e3 commit 3be274c

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

api/src/main/java/org/apache/cloudstack/context/CallContext.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@
2323

2424
import org.apache.cloudstack.api.ApiCommandResourceType;
2525
import org.apache.cloudstack.managed.threadlocal.ManagedThreadLocal;
26-
import org.apache.logging.log4j.Logger;
2726
import org.apache.logging.log4j.LogManager;
27+
import org.apache.logging.log4j.Logger;
28+
import org.apache.logging.log4j.ThreadContext;
29+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2830

2931
import com.cloud.exception.CloudAuthenticationException;
3032
import com.cloud.projects.Project;
3133
import com.cloud.user.Account;
3234
import com.cloud.user.AccountService;
3335
import com.cloud.user.User;
3436
import com.cloud.utils.UuidUtils;
37+
import com.cloud.utils.component.ComponentContext;
3538
import com.cloud.utils.db.EntityManager;
3639
import com.cloud.utils.exception.CloudRuntimeException;
37-
import org.apache.logging.log4j.ThreadContext;
3840

3941
/**
4042
* CallContext records information about the environment the call is made. This
@@ -69,11 +71,9 @@ protected Stack<CallContext> initialValue() {
6971
private String apiName;
7072

7173
static EntityManager s_entityMgr;
72-
static AccountService accountService;
7374

74-
public static void init(EntityManager entityMgr, AccountService accountService) {
75+
public static void init(EntityManager entityMgr) {
7576
s_entityMgr = entityMgr;
76-
CallContext.accountService = accountService;
7777
}
7878

7979
protected CallContext() {
@@ -140,7 +140,15 @@ public Account getCallingAccount() {
140140

141141
public boolean isCallingAccountRootAdmin() {
142142
if (isAccountRootAdmin == null) {
143-
accountService.isRootAdmin(getCallingAccount());
143+
AccountService accountService;
144+
try {
145+
accountService = ComponentContext.getDelegateComponentOfType(AccountService.class);
146+
} catch (NoSuchBeanDefinitionException e) {
147+
LOGGER.warn("Falling back to account type check for isRootAdmin for account ID: {} as no AccountService bean found: {}", accountId, e.getMessage());
148+
Account caller = getCallingAccount();
149+
return caller != null && caller.getType() == Account.Type.ADMIN;
150+
}
151+
isAccountRootAdmin = accountService.isRootAdmin(getCallingAccount());
144152
}
145153
return Boolean.TRUE.equals(isAccountRootAdmin);
146154
}

api/src/main/java/org/apache/cloudstack/context/CallContextListener.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@
2323

2424
import org.apache.cloudstack.managed.context.ManagedContextListener;
2525

26-
import com.cloud.user.AccountService;
2726
import com.cloud.utils.db.EntityManager;
2827

2928
public class CallContextListener implements ManagedContextListener<Object> {
3029

3130
@Inject
3231
EntityManager entityMgr;
3332

34-
@Inject
35-
AccountService accountService;
36-
3733
@Override
3834
public Object onEnterContext(boolean reentry) {
3935
if (!reentry && CallContext.current() == null) {
@@ -51,6 +47,6 @@ public void onLeaveContext(Object unused, boolean reentry) {
5147

5248
@PostConstruct
5349
public void init() {
54-
CallContext.init(entityMgr, accountService);
50+
CallContext.init(entityMgr);
5551
}
5652
}

api/src/test/java/org/apache/cloudstack/context/CallContextTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.mockito.junit.MockitoJUnitRunner;
3232

3333
import com.cloud.user.Account;
34-
import com.cloud.user.AccountService;
3534
import com.cloud.user.User;
3635
import com.cloud.utils.db.EntityManager;
3736

@@ -41,12 +40,9 @@ public class CallContextTest {
4140
@Mock
4241
EntityManager entityMgr;
4342

44-
@Mock
45-
AccountService accountService;
46-
4743
@Before
4844
public void setUp() {
49-
CallContext.init(entityMgr, accountService);
45+
CallContext.init(entityMgr);
5046
CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
5147
}
5248

0 commit comments

Comments
 (0)