You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by bh...@apache.org on 2013/01/16 01:13:10 UTC

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

Updated Branches:
  refs/heads/master 4f889bd42 -> f6a8b45de


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 <bh...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/f6a8b45d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/f6a8b45d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/f6a8b45d

Branch: refs/heads/master
Commit: f6a8b45de52ad57646adb71dc55ddf3ee9dbd194
Parents: 4f889bd
Author: Rohit Yadav <bh...@apache.org>
Authored: Tue Jan 15 16:06:21 2013 -0800
Committer: Rohit Yadav <bh...@apache.org>
Committed: Tue Jan 15 16:11:51 2013 -0800

----------------------------------------------------------------------
 api/src/com/cloud/user/UserContext.java            |    4 ++
 .../api/command/user/discovery/ListApisCmd.java    |    8 +---
 .../discovery/ApiDiscoveryServiceImpl.java         |   41 +++++++--------
 3 files changed, 25 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/f6a8b45d/api/src/com/cloud/user/UserContext.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/user/UserContext.java b/api/src/com/cloud/user/UserContext.java
index b140728..539e118 100644
--- a/api/src/com/cloud/user/UserContext.java
+++ b/api/src/com/cloud/user/UserContext.java
@@ -50,6 +50,10 @@ public class UserContext {
         return userId;
     }
 
+    public User getCallerUser() {
+        return _accountMgr.getActiveUser(userId);
+    }
+
     public void setCallerUserId(long userId) {
         this.userId = userId;
     }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/f6a8b45d/plugins/api/discovery/src/org/apache/cloudstack/api/command/user/discovery/ListApisCmd.java
----------------------------------------------------------------------
diff --git a/plugins/api/discovery/src/org/apache/cloudstack/api/command/user/discovery/ListApisCmd.java b/plugins/api/discovery/src/org/apache/cloudstack/api/command/user/discovery/ListApisCmd.java
index 132416b..bad7ca7 100644
--- a/plugins/api/discovery/src/org/apache/cloudstack/api/command/user/discovery/ListApisCmd.java
+++ b/plugins/api/discovery/src/org/apache/cloudstack/api/command/user/discovery/ListApisCmd.java
@@ -16,11 +16,8 @@
 // under the License.
 package org.apache.cloudstack.api.command.user.discovery;
 
-import com.cloud.user.AccountService;
 import com.cloud.user.User;
 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.ApiConstants;
 import org.apache.cloudstack.api.BaseCmd;
@@ -42,16 +39,13 @@ public class ListApisCmd extends BaseCmd {
     @PlugService
     ApiDiscoveryService _apiDiscoveryService;
 
-    @Inject
-    private AccountService _accountService;
-
     @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="API name")
     private String name;
 
     @Override
     public void execute() throws ServerApiException {
         if (_apiDiscoveryService != null) {
-            User user = _accountService.getActiveUser(UserContext.current().getCallerUserId());
+            User user = UserContext.current().getCallerUser();
             ListResponse<ApiDiscoveryResponse> response = (ListResponse<ApiDiscoveryResponse>) _apiDiscoveryService.listApis(user, name);
             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");

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/f6a8b45d/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java
----------------------------------------------------------------------
diff --git a/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java b/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java
index 7e2ed17..f06e200 100644
--- a/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java
+++ b/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java
@@ -17,17 +17,13 @@
 package org.apache.cloudstack.discovery;
 
 import com.cloud.serializer.Param;
-import com.cloud.server.ManagementServer;
 import com.cloud.user.User;
 import com.cloud.utils.ReflectUtil;
 import com.cloud.utils.StringUtils;
 import com.cloud.utils.component.Adapters;
 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 org.apache.cloudstack.acl.APIChecker;
-import org.apache.cloudstack.acl.RoleType;
 import org.apache.cloudstack.api.APICommand;
 import org.apache.cloudstack.api.BaseCmd;
 import org.apache.cloudstack.api.BaseAsyncCmd;
@@ -54,16 +50,14 @@ import java.util.Set;
 public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
     private static final Logger s_logger = Logger.getLogger(ApiDiscoveryServiceImpl.class);
 
-    @Inject(adapter = APIChecker.class)
-    protected Adapters<APIChecker> _apiAccessCheckers;
-
-    private static Map<String, ApiDiscoveryResponse> _apiNameDiscoveryResponseMap = null;
+    protected static Adapters<APIChecker> s_apiAccessCheckers = null;
+    private static Map<String, ApiDiscoveryResponse> s_apiNameDiscoveryResponseMap = null;
 
     protected ApiDiscoveryServiceImpl() {
         super();
-        if (_apiNameDiscoveryResponseMap == null) {
+        if (s_apiNameDiscoveryResponseMap == null) {
             long startTime = System.nanoTime();
-            _apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
+            s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
             cacheResponseMap();
             long endTime = System.nanoTime();
             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");
-            _apiNameDiscoveryResponseMap.put(apiName, response);
+            s_apiNameDiscoveryResponseMap.put(apiName, response);
         }
 
-        for (String apiName : _apiNameDiscoveryResponseMap.keySet()) {
-            ApiDiscoveryResponse response = _apiNameDiscoveryResponseMap.get(apiName);
+        for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
+            ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
             Set<ApiParameterResponse> processedParams = new HashSet<ApiParameterResponse>();
             for (ApiParameterResponse param : response.getParams()) {
                 if (responseApiNameListMap.containsKey(param.getRelated())) {
@@ -166,7 +160,7 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
             } else {
                 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>();
         List<ApiDiscoveryResponse> responseList = new ArrayList<ApiDiscoveryResponse>();
 
+        if (s_apiAccessCheckers == null) {
+            ComponentLocator locator = ComponentLocator.getCurrentLocator();
+            s_apiAccessCheckers = locator.getAdapters(APIChecker.class);
+        }
+
         if (user == null)
             return null;
 
         if (name != null) {
-            if (!_apiNameDiscoveryResponseMap.containsKey(name))
+            if (!s_apiNameDiscoveryResponseMap.containsKey(name))
                 return null;
 
-            for (APIChecker apiChecker : _apiAccessCheckers) {
+            for (APIChecker apiChecker : s_apiAccessCheckers) {
                 try {
                     apiChecker.checkAccess(user, name);
                 } catch (Exception ex) {
                     return null;
                 }
             }
-            responseList.add(_apiNameDiscoveryResponseMap.get(name));
+            responseList.add(s_apiNameDiscoveryResponseMap.get(name));
 
         } else {
-            for (String apiName : _apiNameDiscoveryResponseMap.keySet()) {
+            for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
                 boolean isAllowed = true;
-                for (APIChecker apiChecker : _apiAccessCheckers) {
+                for (APIChecker apiChecker : s_apiAccessCheckers) {
                     try {
-                        apiChecker.checkAccess(user, name);
+                        apiChecker.checkAccess(user, apiName);
                     } catch (Exception ex) {
                         isAllowed = false;
                     }
                 }
                 if (isAllowed)
-                    responseList.add(_apiNameDiscoveryResponseMap.get(apiName));
+                    responseList.add(s_apiNameDiscoveryResponseMap.get(apiName));
             }
         }
         response.setResponses(responseList);