mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
server: ApiServer inits with processing cmd classes to fill apiname:class mapping
- Add maven dependency reflections (wtfl license, apl compliant) - Use reflections to recurse and get apiname:class mapping Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
This commit is contained in:
parent
072c6f1988
commit
580bf857ff
@ -79,6 +79,11 @@
|
|||||||
<classifier>tests</classifier>
|
<classifier>tests</classifier>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.reflections</groupId>
|
||||||
|
<artifactId>reflections</artifactId>
|
||||||
|
<version>0.9.8</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<defaultGoal>install</defaultGoal>
|
<defaultGoal>install</defaultGoal>
|
||||||
|
|||||||
@ -119,10 +119,8 @@ import com.cloud.user.User;
|
|||||||
import com.cloud.user.UserAccount;
|
import com.cloud.user.UserAccount;
|
||||||
import com.cloud.user.UserContext;
|
import com.cloud.user.UserContext;
|
||||||
import com.cloud.user.UserVO;
|
import com.cloud.user.UserVO;
|
||||||
import com.cloud.utils.IdentityProxy;
|
|
||||||
import com.cloud.utils.Pair;
|
import com.cloud.utils.Pair;
|
||||||
import com.cloud.utils.component.Adapters;
|
import com.cloud.utils.component.Adapters;
|
||||||
import com.cloud.utils.PropertiesUtil;
|
|
||||||
import com.cloud.utils.StringUtils;
|
import com.cloud.utils.StringUtils;
|
||||||
import com.cloud.utils.component.ComponentLocator;
|
import com.cloud.utils.component.ComponentLocator;
|
||||||
import com.cloud.utils.component.Inject;
|
import com.cloud.utils.component.Inject;
|
||||||
@ -133,6 +131,8 @@ import com.cloud.utils.exception.CSExceptionErrorCode;
|
|||||||
import com.cloud.uuididentity.dao.IdentityDao;
|
import com.cloud.uuididentity.dao.IdentityDao;
|
||||||
import com.cloud.acl.APIAccessChecker;
|
import com.cloud.acl.APIAccessChecker;
|
||||||
|
|
||||||
|
import org.reflections.Reflections;
|
||||||
|
|
||||||
public class ApiServer implements HttpRequestHandler {
|
public class ApiServer implements HttpRequestHandler {
|
||||||
private static final Logger s_logger = Logger.getLogger(ApiServer.class.getName());
|
private static final Logger s_logger = Logger.getLogger(ApiServer.class.getName());
|
||||||
private static final Logger s_accessLogger = Logger.getLogger("apiserver." + ApiServer.class.getName());
|
private static final Logger s_accessLogger = Logger.getLogger("apiserver." + ApiServer.class.getName());
|
||||||
@ -152,6 +152,7 @@ public class ApiServer implements HttpRequestHandler {
|
|||||||
private static int _workerCount = 0;
|
private static int _workerCount = 0;
|
||||||
private static ApiServer s_instance = null;
|
private static ApiServer s_instance = null;
|
||||||
private static final DateFormat _dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
private static final DateFormat _dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
||||||
|
private Map<String, Class<?>> _apiNameCmdClassMap = new HashMap<String, Class<?>>();
|
||||||
|
|
||||||
private static ExecutorService _executor = new ThreadPoolExecutor(10, 150, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("ApiServer"));
|
private static ExecutorService _executor = new ThreadPoolExecutor(10, 150, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("ApiServer"));
|
||||||
|
|
||||||
@ -199,6 +200,19 @@ public class ApiServer implements HttpRequestHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Populate api name and cmd class mappings
|
||||||
|
Reflections reflections = new Reflections("org.apache.cloudstack");
|
||||||
|
Set<Class<?>> cmdClasses = reflections.getTypesAnnotatedWith(APICommand.class);
|
||||||
|
reflections = new Reflections("com.cloud.api");
|
||||||
|
cmdClasses.addAll(reflections.getTypesAnnotatedWith(APICommand.class));
|
||||||
|
for(Class<?> cmdClass: cmdClasses) {
|
||||||
|
String apiName = cmdClass.getAnnotation(APICommand.class).name();
|
||||||
|
if (_apiNameCmdClassMap.containsKey(apiName)) {
|
||||||
|
s_logger.error("Cmd class " + cmdClass.getName() + " conflicts on apiname" + apiName);
|
||||||
|
}
|
||||||
|
_apiNameCmdClassMap.put(apiName, cmdClass);
|
||||||
|
}
|
||||||
|
|
||||||
encodeApiResponse = Boolean.valueOf(configDao.getValue(Config.EncodeApiResponse.key()));
|
encodeApiResponse = Boolean.valueOf(configDao.getValue(Config.EncodeApiResponse.key()));
|
||||||
String jsonType = configDao.getValue(Config.JavaScriptDefaultContentType.key());
|
String jsonType = configDao.getValue(Config.JavaScriptDefaultContentType.key());
|
||||||
if (jsonType != null) {
|
if (jsonType != null) {
|
||||||
@ -322,9 +336,8 @@ public class ApiServer implements HttpRequestHandler {
|
|||||||
paramMap.put(key, decodedValue);
|
paramMap.put(key, decodedValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
String cmdClassName = getCmdClassName(command[0]);
|
Class<?> cmdClass = getCmdClass(command[0]);
|
||||||
if (cmdClassName != null) {
|
if (cmdClass != null) {
|
||||||
Class<?> cmdClass = Class.forName(cmdClassName);
|
|
||||||
BaseCmd cmdObj = (BaseCmd) cmdClass.newInstance();
|
BaseCmd cmdObj = (BaseCmd) cmdClass.newInstance();
|
||||||
cmdObj.setFullUrlParams(paramMap);
|
cmdObj.setFullUrlParams(paramMap);
|
||||||
cmdObj.setResponseType(responseType);
|
cmdObj.setResponseType(responseType);
|
||||||
@ -795,15 +808,8 @@ public class ApiServer implements HttpRequestHandler {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getCmdClassName(String cmdName) {
|
private Class<?> getCmdClass(String cmdName) {
|
||||||
String cmdClassName = null;
|
return _apiNameCmdClassMap.get(cmdName);
|
||||||
for (APIAccessChecker apiChecker : _apiAccessCheckers){
|
|
||||||
cmdClassName = apiChecker.getApiCommands().getProperty(cmdName);
|
|
||||||
// Break on the first non-null value
|
|
||||||
if (cmdClassName != null)
|
|
||||||
return cmdClassName;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: rather than isError, we might was to pass in the status code to give more flexibility
|
// FIXME: rather than isError, we might was to pass in the status code to give more flexibility
|
||||||
@ -939,7 +945,7 @@ public class ApiServer implements HttpRequestHandler {
|
|||||||
|
|
||||||
public String getSerializedApiError(int errorCode, String errorText, Map<String, Object[]> apiCommandParams, String responseType, Exception ex) {
|
public String getSerializedApiError(int errorCode, String errorText, Map<String, Object[]> apiCommandParams, String responseType, Exception ex) {
|
||||||
String responseName = null;
|
String responseName = null;
|
||||||
String cmdClassName = null;
|
Class<?> cmdClass = null;
|
||||||
String responseText = null;
|
String responseText = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -950,10 +956,9 @@ public class ApiServer implements HttpRequestHandler {
|
|||||||
// cmd name can be null when "command" parameter is missing in the request
|
// cmd name can be null when "command" parameter is missing in the request
|
||||||
if (cmdObj != null) {
|
if (cmdObj != null) {
|
||||||
String cmdName = ((String[]) cmdObj)[0];
|
String cmdName = ((String[]) cmdObj)[0];
|
||||||
cmdClassName = getCmdClassName(cmdName);
|
cmdClass = getCmdClass(cmdName);
|
||||||
if (cmdClassName != null) {
|
if (cmdClass != null) {
|
||||||
Class<?> claz = Class.forName(cmdClassName);
|
responseName = ((BaseCmd) cmdClass.newInstance()).getCommandName();
|
||||||
responseName = ((BaseCmd) claz.newInstance()).getCommandName();
|
|
||||||
} else {
|
} else {
|
||||||
responseName = "errorresponse";
|
responseName = "errorresponse";
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user