ApiDiscovery: Use UserContext getCallerUser, do a lazy eval to get account service

Impl. and use UserContext to get User.
CloudStack's @Inject is horrible, it may sometimes fail to inject account service
during startup. Do a lazy injection using ComponentLocator when needed.

Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
This commit is contained in:
Rohit Yadav 2013-01-15 16:06:21 -08:00
parent 4f889bd426
commit f6a8b45de5
3 changed files with 25 additions and 28 deletions

View File

@ -50,6 +50,10 @@ public class UserContext {
return userId; return userId;
} }
public User getCallerUser() {
return _accountMgr.getActiveUser(userId);
}
public void setCallerUserId(long userId) { public void setCallerUserId(long userId) {
this.userId = userId; this.userId = userId;
} }

View File

@ -16,11 +16,8 @@
// under the License. // under the License.
package org.apache.cloudstack.api.command.user.discovery; package org.apache.cloudstack.api.command.user.discovery;
import com.cloud.user.AccountService;
import com.cloud.user.User; import com.cloud.user.User;
import com.cloud.user.UserContext; import com.cloud.user.UserContext;
import com.cloud.utils.component.Inject;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.BaseCmd;
@ -42,16 +39,13 @@ public class ListApisCmd extends BaseCmd {
@PlugService @PlugService
ApiDiscoveryService _apiDiscoveryService; ApiDiscoveryService _apiDiscoveryService;
@Inject
private AccountService _accountService;
@Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="API name") @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="API name")
private String name; private String name;
@Override @Override
public void execute() throws ServerApiException { public void execute() throws ServerApiException {
if (_apiDiscoveryService != null) { if (_apiDiscoveryService != null) {
User user = _accountService.getActiveUser(UserContext.current().getCallerUserId()); User user = UserContext.current().getCallerUser();
ListResponse<ApiDiscoveryResponse> response = (ListResponse<ApiDiscoveryResponse>) _apiDiscoveryService.listApis(user, name); ListResponse<ApiDiscoveryResponse> response = (ListResponse<ApiDiscoveryResponse>) _apiDiscoveryService.listApis(user, name);
if (response == null) { if (response == null) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Api Discovery plugin was unable to find an api by that name or process any apis"); throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Api Discovery plugin was unable to find an api by that name or process any apis");

View File

@ -17,17 +17,13 @@
package org.apache.cloudstack.discovery; package org.apache.cloudstack.discovery;
import com.cloud.serializer.Param; import com.cloud.serializer.Param;
import com.cloud.server.ManagementServer;
import com.cloud.user.User; import com.cloud.user.User;
import com.cloud.utils.ReflectUtil; import com.cloud.utils.ReflectUtil;
import com.cloud.utils.StringUtils; import com.cloud.utils.StringUtils;
import com.cloud.utils.component.Adapters; import com.cloud.utils.component.Adapters;
import com.cloud.utils.component.ComponentLocator; import com.cloud.utils.component.ComponentLocator;
import com.cloud.utils.component.Inject;
import com.cloud.utils.component.PluggableService;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import org.apache.cloudstack.acl.APIChecker; import org.apache.cloudstack.acl.APIChecker;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.BaseAsyncCmd;
@ -54,16 +50,14 @@ import java.util.Set;
public class ApiDiscoveryServiceImpl implements ApiDiscoveryService { public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
private static final Logger s_logger = Logger.getLogger(ApiDiscoveryServiceImpl.class); private static final Logger s_logger = Logger.getLogger(ApiDiscoveryServiceImpl.class);
@Inject(adapter = APIChecker.class) protected static Adapters<APIChecker> s_apiAccessCheckers = null;
protected Adapters<APIChecker> _apiAccessCheckers; private static Map<String, ApiDiscoveryResponse> s_apiNameDiscoveryResponseMap = null;
private static Map<String, ApiDiscoveryResponse> _apiNameDiscoveryResponseMap = null;
protected ApiDiscoveryServiceImpl() { protected ApiDiscoveryServiceImpl() {
super(); super();
if (_apiNameDiscoveryResponseMap == null) { if (s_apiNameDiscoveryResponseMap == null) {
long startTime = System.nanoTime(); long startTime = System.nanoTime();
_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>(); s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
cacheResponseMap(); cacheResponseMap();
long endTime = System.nanoTime(); long endTime = System.nanoTime();
s_logger.info("Api Discovery Service: Annotation, docstrings, api relation graph processed in " + (endTime - startTime) / 1000000.0 + " ms"); s_logger.info("Api Discovery Service: Annotation, docstrings, api relation graph processed in " + (endTime - startTime) / 1000000.0 + " ms");
@ -142,11 +136,11 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
} }
} }
response.setObjectName("api"); response.setObjectName("api");
_apiNameDiscoveryResponseMap.put(apiName, response); s_apiNameDiscoveryResponseMap.put(apiName, response);
} }
for (String apiName : _apiNameDiscoveryResponseMap.keySet()) { for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
ApiDiscoveryResponse response = _apiNameDiscoveryResponseMap.get(apiName); ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
Set<ApiParameterResponse> processedParams = new HashSet<ApiParameterResponse>(); Set<ApiParameterResponse> processedParams = new HashSet<ApiParameterResponse>();
for (ApiParameterResponse param : response.getParams()) { for (ApiParameterResponse param : response.getParams()) {
if (responseApiNameListMap.containsKey(param.getRelated())) { if (responseApiNameListMap.containsKey(param.getRelated())) {
@ -166,7 +160,7 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
} else { } else {
response.setRelated(null); response.setRelated(null);
} }
_apiNameDiscoveryResponseMap.put(apiName, response); s_apiNameDiscoveryResponseMap.put(apiName, response);
} }
} }
@ -175,34 +169,39 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
ListResponse<ApiDiscoveryResponse> response = new ListResponse<ApiDiscoveryResponse>(); ListResponse<ApiDiscoveryResponse> response = new ListResponse<ApiDiscoveryResponse>();
List<ApiDiscoveryResponse> responseList = new ArrayList<ApiDiscoveryResponse>(); List<ApiDiscoveryResponse> responseList = new ArrayList<ApiDiscoveryResponse>();
if (s_apiAccessCheckers == null) {
ComponentLocator locator = ComponentLocator.getCurrentLocator();
s_apiAccessCheckers = locator.getAdapters(APIChecker.class);
}
if (user == null) if (user == null)
return null; return null;
if (name != null) { if (name != null) {
if (!_apiNameDiscoveryResponseMap.containsKey(name)) if (!s_apiNameDiscoveryResponseMap.containsKey(name))
return null; return null;
for (APIChecker apiChecker : _apiAccessCheckers) { for (APIChecker apiChecker : s_apiAccessCheckers) {
try { try {
apiChecker.checkAccess(user, name); apiChecker.checkAccess(user, name);
} catch (Exception ex) { } catch (Exception ex) {
return null; return null;
} }
} }
responseList.add(_apiNameDiscoveryResponseMap.get(name)); responseList.add(s_apiNameDiscoveryResponseMap.get(name));
} else { } else {
for (String apiName : _apiNameDiscoveryResponseMap.keySet()) { for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
boolean isAllowed = true; boolean isAllowed = true;
for (APIChecker apiChecker : _apiAccessCheckers) { for (APIChecker apiChecker : s_apiAccessCheckers) {
try { try {
apiChecker.checkAccess(user, name); apiChecker.checkAccess(user, apiName);
} catch (Exception ex) { } catch (Exception ex) {
isAllowed = false; isAllowed = false;
} }
} }
if (isAllowed) if (isAllowed)
responseList.add(_apiNameDiscoveryResponseMap.get(apiName)); responseList.add(s_apiNameDiscoveryResponseMap.get(apiName));
} }
} }
response.setResponses(responseList); response.setResponses(responseList);