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:
Rohit Yadav 2013-01-09 17:19:49 -08:00
parent 657fb6ac0b
commit d13cc7e7e4
3 changed files with 14 additions and 46 deletions

View File

@ -16,14 +16,10 @@
// under the License.
package org.apache.cloudstack.discovery;
import com.cloud.utils.component.Adapter;
import com.cloud.utils.component.PluggableService;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.api.response.ListResponse;
import java.util.Map;
public interface ApiDiscoveryService extends Adapter, PluggableService {
public interface ApiDiscoveryService extends PluggableService {
ListResponse<? extends BaseResponse> listApis();
Map<String, Class<?>> getApiNameCmdClassMapping();
}

View File

@ -56,9 +56,6 @@ under the License.
<adapters key="org.apache.cloudstack.acl.APIAccessChecker">
<adapter name="StaticRoleBasedAPIAccessChecker" class="org.apache.cloudstack.acl.StaticRoleBasedAPIAccessChecker"/>
</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">
<adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.FirstFitRoutingAllocator"/>
<!--adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.RecreateHostAllocator"/-->

View File

@ -17,7 +17,6 @@
package org.apache.cloudstack.discovery;
import com.cloud.utils.ReflectUtil;
import com.cloud.utils.component.AdapterBase;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.BaseAsyncCmd;
@ -30,7 +29,6 @@ import org.apache.cloudstack.api.response.ListResponse;
import org.apache.log4j.Logger;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
@ -39,36 +37,28 @@ import java.util.Map;
import java.util.Set;
@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 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() {
super();
generateApiNameCmdClassMap();
cacheListApiResponse();
}
private void generateApiNameCmdClassMapping() {
_apiNameCmdClassMap = new HashMap<String, Class<?>>();
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class, new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
private void generateApiNameCmdClassMap() {
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class,
new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
for(Class<?> cmdClass: cmdClasses) {
String apiName = cmdClass.getAnnotation(APICommand.class).name();
if (_apiNameCmdClassMap.containsKey(apiName)) {
s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
continue;
}
_apiNameCmdClassMap.put(apiName, cmdClass);
}
for(Class<?> cmdClass: cmdClasses)
_apiNameCmdClassMap.put(cmdClass.getAnnotation(APICommand.class).name(), cmdClass);
}
private void precacheListApiResponse() {
if(_apiNameCmdClassMap == null)
return;
_discoveryResponse = new ListResponse<ApiDiscoveryResponse>();
private void cacheListApiResponse() {
List<ApiDiscoveryResponse> apiDiscoveryResponses = new ArrayList<ApiDiscoveryResponse>();
@ -117,21 +107,6 @@ public class ApiDiscoveryServiceImpl extends AdapterBase implements ApiDiscovery
_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
public ListResponse<? extends BaseResponse> listApis() {
return _discoveryResponse;