mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
ApiDiscoveryService: Use only as pluggable service
Remove usage and impl as adapter. We have duplicate code that generates apiname:cmd class maps which is unavoidable right now as: - Plugin should not depend on ApiServer or any other component - cloud-utils cannot depend on cloud-api for the APICommand annotation - Use java reflect to create a static method in cloud-utils that does the job would be unsafe. Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
This commit is contained in:
parent
657fb6ac0b
commit
d13cc7e7e4
@ -16,14 +16,10 @@
|
|||||||
// under the License.
|
// under the License.
|
||||||
package org.apache.cloudstack.discovery;
|
package org.apache.cloudstack.discovery;
|
||||||
|
|
||||||
import com.cloud.utils.component.Adapter;
|
|
||||||
import com.cloud.utils.component.PluggableService;
|
import com.cloud.utils.component.PluggableService;
|
||||||
import org.apache.cloudstack.api.BaseResponse;
|
import org.apache.cloudstack.api.BaseResponse;
|
||||||
import org.apache.cloudstack.api.response.ListResponse;
|
import org.apache.cloudstack.api.response.ListResponse;
|
||||||
|
|
||||||
import java.util.Map;
|
public interface ApiDiscoveryService extends PluggableService {
|
||||||
|
|
||||||
public interface ApiDiscoveryService extends Adapter, PluggableService {
|
|
||||||
ListResponse<? extends BaseResponse> listApis();
|
ListResponse<? extends BaseResponse> listApis();
|
||||||
Map<String, Class<?>> getApiNameCmdClassMapping();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,9 +56,6 @@ under the License.
|
|||||||
<adapters key="org.apache.cloudstack.acl.APIAccessChecker">
|
<adapters key="org.apache.cloudstack.acl.APIAccessChecker">
|
||||||
<adapter name="StaticRoleBasedAPIAccessChecker" class="org.apache.cloudstack.acl.StaticRoleBasedAPIAccessChecker"/>
|
<adapter name="StaticRoleBasedAPIAccessChecker" class="org.apache.cloudstack.acl.StaticRoleBasedAPIAccessChecker"/>
|
||||||
</adapters>
|
</adapters>
|
||||||
<adapters key="org.apache.cloudstack.discovery.ApiDiscoveryService">
|
|
||||||
<adapter name="ApiDiscoveryService" class="org.apache.cloudstack.discovery.ApiDiscoveryServiceImpl"/>
|
|
||||||
</adapters>
|
|
||||||
<adapters key="com.cloud.agent.manager.allocator.HostAllocator">
|
<adapters key="com.cloud.agent.manager.allocator.HostAllocator">
|
||||||
<adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.FirstFitRoutingAllocator"/>
|
<adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.FirstFitRoutingAllocator"/>
|
||||||
<!--adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.RecreateHostAllocator"/-->
|
<!--adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.RecreateHostAllocator"/-->
|
||||||
|
|||||||
@ -17,7 +17,6 @@
|
|||||||
package org.apache.cloudstack.discovery;
|
package org.apache.cloudstack.discovery;
|
||||||
|
|
||||||
import com.cloud.utils.ReflectUtil;
|
import com.cloud.utils.ReflectUtil;
|
||||||
import com.cloud.utils.component.AdapterBase;
|
|
||||||
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;
|
||||||
@ -30,7 +29,6 @@ import org.apache.cloudstack.api.response.ListResponse;
|
|||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import javax.ejb.Local;
|
import javax.ejb.Local;
|
||||||
import javax.naming.ConfigurationException;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -39,36 +37,28 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@Local(value = ApiDiscoveryService.class)
|
@Local(value = ApiDiscoveryService.class)
|
||||||
public class ApiDiscoveryServiceImpl extends AdapterBase 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);
|
||||||
private Map<String, Class<?>> _apiNameCmdClassMap;
|
|
||||||
private ListResponse<ApiDiscoveryResponse> _discoveryResponse;
|
private ListResponse<ApiDiscoveryResponse> _discoveryResponse = new ListResponse<ApiDiscoveryResponse>();
|
||||||
|
|
||||||
|
private Map<String, Class<?>> _apiNameCmdClassMap = new HashMap<String, Class<?>>();
|
||||||
|
|
||||||
protected ApiDiscoveryServiceImpl() {
|
protected ApiDiscoveryServiceImpl() {
|
||||||
super();
|
super();
|
||||||
|
generateApiNameCmdClassMap();
|
||||||
|
cacheListApiResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateApiNameCmdClassMapping() {
|
private void generateApiNameCmdClassMap() {
|
||||||
_apiNameCmdClassMap = new HashMap<String, Class<?>>();
|
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class,
|
||||||
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class, new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
|
new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
|
||||||
|
|
||||||
for(Class<?> cmdClass: cmdClasses) {
|
for(Class<?> cmdClass: cmdClasses)
|
||||||
String apiName = cmdClass.getAnnotation(APICommand.class).name();
|
_apiNameCmdClassMap.put(cmdClass.getAnnotation(APICommand.class).name(), cmdClass);
|
||||||
if (_apiNameCmdClassMap.containsKey(apiName)) {
|
|
||||||
s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_apiNameCmdClassMap.put(apiName, cmdClass);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void precacheListApiResponse() {
|
private void cacheListApiResponse() {
|
||||||
|
|
||||||
if(_apiNameCmdClassMap == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_discoveryResponse = new ListResponse<ApiDiscoveryResponse>();
|
|
||||||
|
|
||||||
List<ApiDiscoveryResponse> apiDiscoveryResponses = new ArrayList<ApiDiscoveryResponse>();
|
List<ApiDiscoveryResponse> apiDiscoveryResponses = new ArrayList<ApiDiscoveryResponse>();
|
||||||
|
|
||||||
@ -117,21 +107,6 @@ public class ApiDiscoveryServiceImpl extends AdapterBase implements ApiDiscovery
|
|||||||
_discoveryResponse.setResponses(apiDiscoveryResponses);
|
_discoveryResponse.setResponses(apiDiscoveryResponses);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean configure(String name, Map<String, Object> params)
|
|
||||||
throws ConfigurationException {
|
|
||||||
super.configure(name, params);
|
|
||||||
|
|
||||||
generateApiNameCmdClassMapping();
|
|
||||||
precacheListApiResponse();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Class<?>> getApiNameCmdClassMapping() {
|
|
||||||
return _apiNameCmdClassMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ListResponse<? extends BaseResponse> listApis() {
|
public ListResponse<? extends BaseResponse> listApis() {
|
||||||
return _discoveryResponse;
|
return _discoveryResponse;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user