mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-02 20:02:29 +01:00
Making unit tests work
This commit is contained in:
parent
4fbecf15ec
commit
ee39ec82d3
@ -25,7 +25,6 @@ import org.apache.log4j.Logger;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.ha.HaWorkVO;
|
||||
import com.cloud.ha.HighAvailabilityManager;
|
||||
import com.cloud.ha.HighAvailabilityManager.Step;
|
||||
import com.cloud.ha.HighAvailabilityManager.WorkType;
|
||||
import com.cloud.utils.db.Filter;
|
||||
@ -40,7 +39,7 @@ import com.cloud.utils.exception.CloudRuntimeException;
|
||||
@Local(value={HighAvailabilityDao.class})
|
||||
public class HighAvailabilityDaoImpl extends GenericDaoBase<HaWorkVO, Long> implements HighAvailabilityDao {
|
||||
private static final Logger s_logger = Logger.getLogger(HighAvailabilityDaoImpl.class);
|
||||
|
||||
|
||||
private final SearchBuilder<HaWorkVO> TBASearch;
|
||||
private final SearchBuilder<HaWorkVO> PreviousInstanceSearch;
|
||||
private final SearchBuilder<HaWorkVO> UntakenMigrationSearch;
|
||||
@ -53,54 +52,54 @@ public class HighAvailabilityDaoImpl extends GenericDaoBase<HaWorkVO, Long> impl
|
||||
|
||||
protected HighAvailabilityDaoImpl() {
|
||||
super();
|
||||
|
||||
|
||||
CleanupSearch = createSearchBuilder();
|
||||
CleanupSearch.and("time", CleanupSearch.entity().getTimeToTry(), Op.LTEQ);
|
||||
CleanupSearch.and("step", CleanupSearch.entity().getStep(), Op.IN);
|
||||
CleanupSearch.done();
|
||||
|
||||
|
||||
TBASearch = createSearchBuilder();
|
||||
TBASearch.and("server", TBASearch.entity().getServerId(), Op.NULL);
|
||||
TBASearch.and("taken", TBASearch.entity().getDateTaken(), Op.NULL);
|
||||
TBASearch.and("time", TBASearch.entity().getTimeToTry(), Op.LTEQ);
|
||||
TBASearch.done();
|
||||
|
||||
|
||||
PreviousInstanceSearch = createSearchBuilder();
|
||||
PreviousInstanceSearch.and("instance", PreviousInstanceSearch.entity().getInstanceId(), Op.EQ);
|
||||
PreviousInstanceSearch.done();
|
||||
|
||||
|
||||
UntakenMigrationSearch = createSearchBuilder();
|
||||
UntakenMigrationSearch.and("host", UntakenMigrationSearch.entity().getHostId(), Op.EQ);
|
||||
UntakenMigrationSearch.and("type", UntakenMigrationSearch.entity().getWorkType(), Op.EQ);
|
||||
UntakenMigrationSearch.and("server", UntakenMigrationSearch.entity().getServerId(), Op.NULL);
|
||||
UntakenMigrationSearch.and("taken", UntakenMigrationSearch.entity().getDateTaken(), Op.NULL);
|
||||
UntakenMigrationSearch.done();
|
||||
|
||||
|
||||
TakenWorkSearch = createSearchBuilder();
|
||||
TakenWorkSearch.and("type", TakenWorkSearch.entity().getWorkType(), Op.EQ);
|
||||
TakenWorkSearch.and("server", TakenWorkSearch.entity().getServerId(), Op.NNULL);
|
||||
TakenWorkSearch.and("taken", TakenWorkSearch.entity().getDateTaken(), Op.NNULL);
|
||||
TakenWorkSearch.and("step", TakenWorkSearch.entity().getStep(), Op.NIN);
|
||||
TakenWorkSearch.done();
|
||||
|
||||
|
||||
PreviousWorkSearch = createSearchBuilder();
|
||||
PreviousWorkSearch.and("instance", PreviousWorkSearch.entity().getInstanceId(), Op.EQ);
|
||||
PreviousWorkSearch.and("type", PreviousWorkSearch.entity().getWorkType(), Op.EQ);
|
||||
PreviousWorkSearch.and("taken", PreviousWorkSearch.entity().getDateTaken(), Op.NULL);
|
||||
PreviousWorkSearch.done();
|
||||
|
||||
|
||||
ReleaseSearch = createSearchBuilder();
|
||||
ReleaseSearch.and("server", ReleaseSearch.entity().getServerId(), Op.EQ);
|
||||
ReleaseSearch.and("step", ReleaseSearch.entity().getStep(), Op.NIN);
|
||||
ReleaseSearch.and("taken", ReleaseSearch.entity().getDateTaken(), Op.NNULL);
|
||||
ReleaseSearch.done();
|
||||
|
||||
|
||||
FutureHaWorkSearch = createSearchBuilder();
|
||||
FutureHaWorkSearch.and("instance", FutureHaWorkSearch.entity().getInstanceId(), Op.EQ);
|
||||
FutureHaWorkSearch.and("type", FutureHaWorkSearch.entity().getType(), Op.EQ);
|
||||
FutureHaWorkSearch.and("id", FutureHaWorkSearch.entity().getId(), Op.GT);
|
||||
FutureHaWorkSearch.done();
|
||||
|
||||
|
||||
RunningHaWorkSearch = createSearchBuilder();
|
||||
RunningHaWorkSearch.and("instance", RunningHaWorkSearch.entity().getInstanceId(), Op.EQ);
|
||||
RunningHaWorkSearch.and("type", RunningHaWorkSearch.entity().getType(), Op.EQ);
|
||||
@ -108,24 +107,24 @@ public class HighAvailabilityDaoImpl extends GenericDaoBase<HaWorkVO, Long> impl
|
||||
RunningHaWorkSearch.and("step", RunningHaWorkSearch.entity().getStep(), Op.NIN);
|
||||
RunningHaWorkSearch.done();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<HaWorkVO> listRunningHaWorkForVm(long vmId) {
|
||||
SearchCriteria<HaWorkVO> sc = RunningHaWorkSearch.create();
|
||||
sc.setParameters("instance", vmId);
|
||||
sc.setParameters("type", WorkType.HA);
|
||||
sc.setParameters("step", Step.Done, Step.Error, Step.Cancelled);
|
||||
|
||||
|
||||
return search(sc, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<HaWorkVO> listFutureHaWorkForVm(long vmId, long workId) {
|
||||
SearchCriteria<HaWorkVO> sc = FutureHaWorkSearch.create();
|
||||
sc.setParameters("instance", vmId);
|
||||
sc.setParameters("type", HighAvailabilityManager.WorkType.HA);
|
||||
sc.setParameters("type", WorkType.HA);
|
||||
sc.setParameters("id", workId);
|
||||
|
||||
|
||||
return search(sc, null);
|
||||
}
|
||||
|
||||
@ -171,7 +170,7 @@ public class HighAvailabilityDaoImpl extends GenericDaoBase<HaWorkVO, Long> impl
|
||||
public void cleanup(final long time) {
|
||||
final SearchCriteria<HaWorkVO> sc = CleanupSearch.create();
|
||||
sc.setParameters("time", time);
|
||||
sc.setParameters("step", HighAvailabilityManager.Step.Done, HighAvailabilityManager.Step.Cancelled);
|
||||
sc.setParameters("step", Step.Done, Step.Cancelled);
|
||||
expunge(sc);
|
||||
}
|
||||
|
||||
@ -185,47 +184,47 @@ public class HighAvailabilityDaoImpl extends GenericDaoBase<HaWorkVO, Long> impl
|
||||
Date date = new Date();
|
||||
work.setDateTaken(date);
|
||||
work.setServerId(serverId);
|
||||
work.setStep(HighAvailabilityManager.Step.Cancelled);
|
||||
|
||||
work.setStep(Step.Cancelled);
|
||||
|
||||
update(work, sc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HaWorkVO> findTakenWorkItems(WorkType type) {
|
||||
SearchCriteria<HaWorkVO> sc = TakenWorkSearch.create();
|
||||
sc.setParameters("type", type);
|
||||
sc.setParameters("step", Step.Done, Step.Cancelled, Step.Error);
|
||||
|
||||
return listBy(sc);
|
||||
SearchCriteria<HaWorkVO> sc = TakenWorkSearch.create();
|
||||
sc.setParameters("type", type);
|
||||
sc.setParameters("step", Step.Done, Step.Cancelled, Step.Error);
|
||||
|
||||
return listBy(sc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean delete(long instanceId, WorkType type) {
|
||||
SearchCriteria<HaWorkVO> sc = PreviousWorkSearch.create();
|
||||
sc.setParameters("instance", instanceId);
|
||||
sc.setParameters("type", type);
|
||||
return expunge(sc) > 0;
|
||||
SearchCriteria<HaWorkVO> sc = PreviousWorkSearch.create();
|
||||
sc.setParameters("instance", instanceId);
|
||||
sc.setParameters("type", type);
|
||||
return expunge(sc) > 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasBeenScheduled(long instanceId, WorkType type) {
|
||||
SearchCriteria<HaWorkVO> sc = PreviousWorkSearch.create();
|
||||
sc.setParameters("instance", instanceId);
|
||||
sc.setParameters("type", type);
|
||||
return listBy(sc, null).size() > 0;
|
||||
SearchCriteria<HaWorkVO> sc = PreviousWorkSearch.create();
|
||||
sc.setParameters("instance", instanceId);
|
||||
sc.setParameters("type", type);
|
||||
return listBy(sc, null).size() > 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int releaseWorkItems(long nodeId) {
|
||||
SearchCriteria<HaWorkVO> sc = ReleaseSearch.create();
|
||||
sc.setParameters("server", nodeId);
|
||||
sc.setParameters("step", Step.Done, Step.Cancelled, Step.Error);
|
||||
|
||||
|
||||
HaWorkVO vo = createForUpdate();
|
||||
vo.setDateTaken(null);
|
||||
vo.setServerId(null);
|
||||
|
||||
|
||||
return update(vo, sc);
|
||||
}
|
||||
}
|
||||
@ -37,10 +37,14 @@ import javax.ejb.Local;
|
||||
import javax.inject.Inject;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.*;
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.AuthorizeSecurityGroupEgressCmd;
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.AuthorizeSecurityGroupIngressCmd;
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.CreateSecurityGroupCmd;
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.DeleteSecurityGroupCmd;
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.RevokeSecurityGroupEgressCmd;
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.RevokeSecurityGroupIngressCmd;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.agent.AgentManager;
|
||||
import com.cloud.agent.api.NetworkRulesSystemVmCommand;
|
||||
@ -49,8 +53,7 @@ import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto;
|
||||
import com.cloud.agent.manager.Commands;
|
||||
import com.cloud.api.query.dao.SecurityGroupJoinDao;
|
||||
import com.cloud.api.query.vo.SecurityGroupJoinVO;
|
||||
|
||||
import org.apache.cloudstack.api.command.user.securitygroup.RevokeSecurityGroupEgressCmd;
|
||||
import com.cloud.cluster.ManagementServerNode;
|
||||
import com.cloud.configuration.Config;
|
||||
import com.cloud.configuration.dao.ConfigurationDao;
|
||||
import com.cloud.domain.dao.DomainDao;
|
||||
@ -75,9 +78,7 @@ import com.cloud.network.security.dao.SecurityGroupRulesDao;
|
||||
import com.cloud.network.security.dao.SecurityGroupVMMapDao;
|
||||
import com.cloud.network.security.dao.SecurityGroupWorkDao;
|
||||
import com.cloud.network.security.dao.VmRulesetLogDao;
|
||||
import com.cloud.projects.Project.ListProjectResourcesCriteria;
|
||||
import com.cloud.projects.ProjectManager;
|
||||
import com.cloud.server.ManagementServer;
|
||||
import com.cloud.tags.dao.ResourceTagDao;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountManager;
|
||||
@ -87,17 +88,12 @@ import com.cloud.user.dao.AccountDao;
|
||||
import com.cloud.uservm.UserVm;
|
||||
import com.cloud.utils.NumbersUtil;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.Ternary;
|
||||
|
||||
import com.cloud.utils.component.Manager;
|
||||
import com.cloud.utils.concurrency.NamedThreadFactory;
|
||||
import com.cloud.utils.db.DB;
|
||||
import com.cloud.utils.db.Filter;
|
||||
import com.cloud.utils.db.GlobalLock;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.db.SearchCriteria.Func;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.utils.fsm.StateListener;
|
||||
import com.cloud.utils.net.NetUtils;
|
||||
@ -163,10 +159,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
UsageEventDao _usageEventDao;
|
||||
@Inject
|
||||
ResourceTagDao _resourceTagDao;
|
||||
|
||||
@Inject
|
||||
ManagementServer _msServer;
|
||||
|
||||
|
||||
ScheduledExecutorService _executorPool;
|
||||
ScheduledExecutorService _cleanupExecutor;
|
||||
|
||||
@ -463,7 +456,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
List<SecurityGroupVMMapVO> groupsForVm = _securityGroupVMMapDao.listByInstanceId(vm.getId());
|
||||
// For each group, find the security rules that allow the group
|
||||
for (SecurityGroupVMMapVO mapVO : groupsForVm) {// FIXME: use custom sql in the dao
|
||||
//Add usage events for security group assign
|
||||
//Add usage events for security group assign
|
||||
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_SECURITY_GROUP_ASSIGN, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), mapVO.getSecurityGroupId());
|
||||
_usageEventDao.persist(usageEvent);
|
||||
|
||||
@ -479,7 +472,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
List<SecurityGroupVMMapVO> groupsForVm = _securityGroupVMMapDao.listByInstanceId(vm.getId());
|
||||
// For each group, find the security rules rules that allow the group
|
||||
for (SecurityGroupVMMapVO mapVO : groupsForVm) {// FIXME: use custom sql in the dao
|
||||
//Add usage events for security group remove
|
||||
//Add usage events for security group remove
|
||||
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_SECURITY_GROUP_REMOVE, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), mapVO.getSecurityGroupId());
|
||||
_usageEventDao.persist(usageEvent);
|
||||
|
||||
@ -583,11 +576,11 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
Map groupList = cmd.getUserSecurityGroupList();
|
||||
return authorizeSecurityGroupRule(securityGroupId,protocol,startPort,endPort,icmpType,icmpCode,cidrList,groupList,SecurityRuleType.IngressRule);
|
||||
}
|
||||
|
||||
|
||||
private List<SecurityGroupRuleVO> authorizeSecurityGroupRule(Long securityGroupId,String protocol,Integer startPort,Integer endPort,Integer icmpType,Integer icmpCode,List<String> cidrList,Map groupList,SecurityRuleType ruleType) {
|
||||
Integer startPortOrType = null;
|
||||
Integer endPortOrCode = null;
|
||||
|
||||
|
||||
// Validate parameters
|
||||
SecurityGroup securityGroup = _securityGroupDao.findById(securityGroupId);
|
||||
if (securityGroup == null) {
|
||||
@ -755,7 +748,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@DB
|
||||
@ActionEvent(eventType = EventTypes.EVENT_SECURITY_GROUP_REVOKE_EGRESS, eventDescription = "Revoking Egress Rule ", async = true)
|
||||
@ -763,7 +756,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
Long id = cmd.getId();
|
||||
return revokeSecurityGroupRule(id, SecurityRuleType.EgressRule);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@DB
|
||||
@ActionEvent(eventType = EventTypes.EVENT_SECURITY_GROUP_REVOKE_INGRESS, eventDescription = "Revoking Ingress Rule ", async = true)
|
||||
@ -772,11 +765,11 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
Long id = cmd.getId();
|
||||
return revokeSecurityGroupRule(id, SecurityRuleType.IngressRule);
|
||||
}
|
||||
|
||||
|
||||
private boolean revokeSecurityGroupRule(Long id, SecurityRuleType type) {
|
||||
// input validation
|
||||
Account caller = UserContext.current().getCaller();
|
||||
|
||||
|
||||
SecurityGroupRuleVO rule = _securityGroupRuleDao.findById(id);
|
||||
if (rule == null) {
|
||||
s_logger.debug("Unable to find security rule with id " + id);
|
||||
@ -788,7 +781,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
s_logger.debug("Mismatch in rule type for security rule with id " + id );
|
||||
throw new InvalidParameterValueException("Mismatch in rule type for security rule with id " + id);
|
||||
}
|
||||
|
||||
|
||||
// Check permissions
|
||||
SecurityGroup securityGroup = _securityGroupDao.findById(rule.getSecurityGroupId());
|
||||
_accountMgr.checkAccess(caller, null, true, securityGroup);
|
||||
@ -866,15 +859,15 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
_answerListener = new SecurityGroupListener(this, _agentMgr, _workDao);
|
||||
_agentMgr.registerForHostEvents(_answerListener, true, true, true);
|
||||
|
||||
_serverId = _msServer.getId();
|
||||
_serverId = ManagementServerNode.getManagementServerId();
|
||||
|
||||
s_logger.info("SecurityGroupManager: num worker threads=" + _numWorkerThreads +
|
||||
", time between cleanups=" + _timeBetweenCleanups + " global lock timeout=" + _globalWorkLockTimeout);
|
||||
", time between cleanups=" + _timeBetweenCleanups + " global lock timeout=" + _globalWorkLockTimeout);
|
||||
createThreadPools();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected void createThreadPools() {
|
||||
_executorPool = Executors.newScheduledThreadPool(_numWorkerThreads, new NamedThreadFactory("NWGRP"));
|
||||
_cleanupExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("NWGRP-Cleanup"));
|
||||
@ -971,7 +964,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
s_logger.debug("Unable to send ingress rules updates for vm: " + userVmId + "(agentid=" + agentId + ")");
|
||||
_workDao.updateStep(work.getInstanceId(), seqnum, Step.Done);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@ -1034,10 +1027,10 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
@Override
|
||||
@DB
|
||||
public void removeInstanceFromGroups(long userVmId) {
|
||||
if (_securityGroupVMMapDao.countSGForVm(userVmId) < 1) {
|
||||
s_logger.trace("No security groups found for vm id=" + userVmId + ", returning");
|
||||
return;
|
||||
}
|
||||
if (_securityGroupVMMapDao.countSGForVm(userVmId) < 1) {
|
||||
s_logger.trace("No security groups found for vm id=" + userVmId + ", returning");
|
||||
return;
|
||||
}
|
||||
final Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
UserVm userVm = _userVMDao.acquireInLockTable(userVmId); // ensures that duplicate entries are not created in
|
||||
@ -1104,7 +1097,7 @@ public class SecurityGroupManagerImpl implements SecurityGroupManager, SecurityG
|
||||
if (count.intValue() == 0) {
|
||||
// handle empty result cases
|
||||
return new Pair<List<SecurityGroupJoinVO>, Integer>(new ArrayList<SecurityGroupJoinVO>(), count);
|
||||
}
|
||||
}
|
||||
List<SecurityGroupVMMapVO> sgVmMappings = sgVmMappingPair.first();
|
||||
Long[] sgIds = new Long[sgVmMappings.size()];
|
||||
int i = 0;
|
||||
|
||||
@ -21,6 +21,8 @@ import java.util.Map;
|
||||
import javax.ejb.Local;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.Command;
|
||||
import com.cloud.agent.api.StartupCommand;
|
||||
@ -34,6 +36,7 @@ import com.cloud.host.Status.Event;
|
||||
import com.cloud.hypervisor.Hypervisor.HypervisorType;
|
||||
import com.cloud.resource.ServerResource;
|
||||
|
||||
@Component
|
||||
@Local(value = { AgentManager.class })
|
||||
public class MockAgentManagerImpl implements AgentManager {
|
||||
|
||||
|
||||
@ -28,6 +28,7 @@ import org.apache.cloudstack.api.command.admin.usage.ListTrafficTypeImplementors
|
||||
import org.apache.cloudstack.api.command.user.network.CreateNetworkCmd;
|
||||
import org.apache.cloudstack.api.command.user.network.ListNetworksCmd;
|
||||
import org.apache.cloudstack.api.command.user.network.RestartNetworkCmd;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.dc.DataCenter;
|
||||
import com.cloud.dc.Vlan.VlanType;
|
||||
@ -68,6 +69,7 @@ import com.cloud.vm.VirtualMachine;
|
||||
import com.cloud.vm.VirtualMachineProfile;
|
||||
import com.cloud.vm.VirtualMachineProfileImpl;
|
||||
|
||||
@Component
|
||||
@Local(value = { NetworkManager.class, NetworkService.class })
|
||||
public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkService {
|
||||
|
||||
@ -167,7 +169,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public List<NetworkVO> setupNetwork(Account owner, NetworkOffering offering, DeploymentPlan plan, String name, String displayText, boolean isDefault)
|
||||
@ -183,7 +185,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void allocate(VirtualMachineProfile<? extends VMInstanceVO> vm, List<Pair<NetworkVO, NicProfile>> networks) throws InsufficientCapacityException, ConcurrentOperationException {
|
||||
@ -216,17 +218,17 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public List<NicProfile> getNicProfiles(VirtualMachine vm) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Pair<NetworkGuru, NetworkVO> implementNetwork(long networkId, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException,
|
||||
InsufficientCapacityException {
|
||||
@ -261,9 +263,9 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean applyIpAssociations(Network network, boolean continueOnError) throws ResourceUnavailableException {
|
||||
@ -271,7 +273,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean startNetwork(long networkId, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
|
||||
@ -279,7 +281,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public IPAddressVO markIpAsUnavailable(long addrId) {
|
||||
// TODO Auto-generated method stub
|
||||
@ -292,22 +294,22 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean applyStaticNats(List<? extends StaticNat> staticNats, boolean continueOnError) throws ResourceUnavailableException {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Map<Service, Set<Provider>> getNetworkOfferingServiceProvidersMap(long networkOfferingId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public PhysicalNetwork createPhysicalNetwork(Long zoneId, String vnetRange, String networkSpeed, List<String> isolationMethods, String broadcastDomainRange, Long domainId, List<String> tags, String name) {
|
||||
@ -412,16 +414,16 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Network getExclusiveGuestNetwork(long zoneId) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public UserDataServiceProvider getPasswordResetProvider(Network network) {
|
||||
@ -429,7 +431,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public PhysicalNetworkServiceProvider updateNetworkServiceProvider(Long id, String state, List<String> enabledServices) {
|
||||
// TODO Auto-generated method stub
|
||||
@ -442,7 +444,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.cloud.network.NetworkManager#applyRules(java.util.List, com.cloud.network.rules.FirewallRule.Purpose, com.cloud.network.NetworkRuleApplier, boolean)
|
||||
*/
|
||||
@ -499,7 +501,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
NetworkVO network, NetworkOfferingVO findById) throws ConcurrentOperationException,
|
||||
InsufficientAddressCapacityException, ResourceUnavailableException, InsufficientCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@ -555,7 +557,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
*/
|
||||
@Override
|
||||
public boolean restartNetwork(RestartNetworkCmd cmd, boolean cleanup) throws ConcurrentOperationException,
|
||||
ResourceUnavailableException, InsufficientCapacityException {
|
||||
ResourceUnavailableException, InsufficientCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
@ -593,7 +595,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
*/
|
||||
@Override
|
||||
public IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException,
|
||||
ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
|
||||
ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@ -604,7 +606,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
@Override
|
||||
public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, String vlan,
|
||||
String startIp, String endIP, String gateway, String netmask, long networkOwnerId, Long vpcId)
|
||||
throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
|
||||
throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@ -665,7 +667,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
Network network, String requestedIp) throws InsufficientVirtualNetworkCapcityException,
|
||||
InsufficientAddressCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@ -684,8 +686,8 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
@Override
|
||||
public Pair<NicProfile, Integer> allocateNic(NicProfile requested, Network network, Boolean isDefaultNic,
|
||||
int deviceId, VirtualMachineProfile<? extends VMInstanceVO> vm)
|
||||
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
|
||||
ConcurrentOperationException {
|
||||
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
|
||||
ConcurrentOperationException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@ -696,8 +698,8 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
@Override
|
||||
public NicProfile prepareNic(VirtualMachineProfile<? extends VMInstanceVO> vmProfile, DeployDestination dest,
|
||||
ReservationContext context, long nicId, NetworkVO network)
|
||||
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
|
||||
ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
|
||||
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
|
||||
ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@ -708,7 +710,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
@Override
|
||||
public void removeNic(VirtualMachineProfile<? extends VMInstanceVO> vm, Nic nic) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@ -727,7 +729,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
public void releaseNic(VirtualMachineProfile<? extends VMInstanceVO> vmProfile, Nic nic)
|
||||
throws ConcurrentOperationException, ResourceUnavailableException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@ -736,8 +738,8 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
@Override
|
||||
public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext context,
|
||||
VirtualMachineProfileImpl<VMInstanceVO> vmProfile, boolean prepare)
|
||||
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
|
||||
ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
|
||||
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
|
||||
ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@ -758,7 +760,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
@Override
|
||||
public void markPublicIpAsAllocated(IPAddressVO addr) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@ -816,7 +818,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
|
||||
*/
|
||||
@Override
|
||||
public IpAddress allocateIP(Account ipOwner, boolean isSystem, long zoneId) throws ResourceAllocationException,
|
||||
InsufficientAddressCapacityException, ConcurrentOperationException {
|
||||
InsufficientAddressCapacityException, ConcurrentOperationException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -29,9 +29,6 @@ import junit.framework.Assert;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.cloud.dc.VlanVO;
|
||||
import com.cloud.dc.dao.VlanDao;
|
||||
@ -43,8 +40,6 @@ import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
import com.cloud.utils.net.Ip;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations="classpath:/testContext.xml")
|
||||
public class NetworkModelTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
@ -25,22 +25,18 @@ import javax.naming.ConfigurationException;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.cloud.utils.Profiler;
|
||||
import com.cloud.vm.dao.UserVmDaoImpl;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "classpath:/testContext.xml")
|
||||
@ContextConfiguration(locations = "classpath:/SecurityGroupManagerTestContext.xml")
|
||||
public class SecurityGroupManagerImpl2Test extends TestCase {
|
||||
@Inject
|
||||
SecurityGroupManagerImpl2 _sgMgr = null;
|
||||
@Inject
|
||||
UserVmDaoImpl _vmDao = null;
|
||||
|
||||
@Override
|
||||
@After
|
||||
@ -62,7 +58,7 @@ public class SecurityGroupManagerImpl2Test extends TestCase {
|
||||
+ " ms");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testSchedule() throws ConfigurationException {
|
||||
_schedule(1000);
|
||||
}
|
||||
@ -71,6 +67,5 @@ public class SecurityGroupManagerImpl2Test extends TestCase {
|
||||
public void testWork() throws ConfigurationException {
|
||||
_schedule(1000);
|
||||
_sgMgr.work();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,60 +17,142 @@
|
||||
|
||||
package com.cloud.network.security;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.core.type.filter.TypeFilter;
|
||||
|
||||
import com.cloud.agent.AgentManager;
|
||||
import com.cloud.agent.MockAgentManagerImpl;
|
||||
import com.cloud.api.query.dao.SecurityGroupJoinDaoImpl;
|
||||
import com.cloud.cluster.agentlb.dao.HostTransferMapDaoImpl;
|
||||
import com.cloud.configuration.dao.ConfigurationDaoImpl;
|
||||
import com.cloud.dc.dao.ClusterDaoImpl;
|
||||
import com.cloud.dc.dao.DataCenterDaoImpl;
|
||||
import com.cloud.dc.dao.DataCenterIpAddressDaoImpl;
|
||||
import com.cloud.dc.dao.DataCenterLinkLocalIpAddressDaoImpl;
|
||||
import com.cloud.dc.dao.DataCenterVnetDaoImpl;
|
||||
import com.cloud.dc.dao.DcDetailsDaoImpl;
|
||||
import com.cloud.dc.dao.HostPodDaoImpl;
|
||||
import com.cloud.dc.dao.PodVlanDaoImpl;
|
||||
import com.cloud.domain.dao.DomainDaoImpl;
|
||||
import com.cloud.event.dao.UsageEventDaoImpl;
|
||||
import com.cloud.host.dao.HostDaoImpl;
|
||||
import com.cloud.host.dao.HostDetailsDaoImpl;
|
||||
import com.cloud.host.dao.HostTagsDaoImpl;
|
||||
import com.cloud.network.NetworkManager;
|
||||
import com.cloud.projects.MockProjectManagerImpl;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.security.SecurityGroupManagerTestConfiguration.Library;
|
||||
import com.cloud.network.security.dao.SecurityGroupDaoImpl;
|
||||
import com.cloud.network.security.dao.SecurityGroupRuleDaoImpl;
|
||||
import com.cloud.network.security.dao.SecurityGroupRulesDaoImpl;
|
||||
import com.cloud.network.security.dao.SecurityGroupVMMapDaoImpl;
|
||||
import com.cloud.network.security.dao.SecurityGroupWorkDaoImpl;
|
||||
import com.cloud.network.security.dao.VmRulesetLogDaoImpl;
|
||||
import com.cloud.projects.ProjectManager;
|
||||
import com.cloud.tags.dao.ResourceTagsDaoImpl;
|
||||
import com.cloud.user.AccountManager;
|
||||
import com.cloud.user.DomainManager;
|
||||
import com.cloud.user.MockAccountManagerImpl;
|
||||
import com.cloud.user.MockDomainManagerImpl;
|
||||
import com.cloud.utils.component.ComponentContext;
|
||||
import com.cloud.vm.MockUserVmManagerImpl;
|
||||
import com.cloud.vm.MockVirtualMachineManagerImpl;
|
||||
import com.cloud.user.dao.AccountDaoImpl;
|
||||
import com.cloud.utils.component.SpringComponentScanUtils;
|
||||
import com.cloud.vm.UserVmManager;
|
||||
import com.cloud.vm.VirtualMachineManager;
|
||||
import com.cloud.vpc.MockNetworkManagerImpl;
|
||||
import com.cloud.vm.dao.NicDaoImpl;
|
||||
import com.cloud.vm.dao.UserVmDaoImpl;
|
||||
import com.cloud.vm.dao.UserVmDetailsDaoImpl;
|
||||
import com.cloud.vm.dao.VMInstanceDaoImpl;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackageClasses={
|
||||
SecurityGroupRulesDaoImpl.class,
|
||||
UserVmDaoImpl.class,
|
||||
AccountDaoImpl.class,
|
||||
ConfigurationDaoImpl.class,
|
||||
SecurityGroupWorkDaoImpl.class,
|
||||
VmRulesetLogDaoImpl.class,
|
||||
VMInstanceDaoImpl.class,
|
||||
DomainDaoImpl.class,
|
||||
UsageEventDaoImpl.class,
|
||||
ResourceTagsDaoImpl.class,
|
||||
HostDaoImpl.class,
|
||||
HostDetailsDaoImpl.class,
|
||||
HostTagsDaoImpl.class,
|
||||
ClusterDaoImpl.class,
|
||||
HostPodDaoImpl.class,
|
||||
DataCenterDaoImpl.class,
|
||||
DataCenterIpAddressDaoImpl.class,
|
||||
HostTransferMapDaoImpl.class,
|
||||
SecurityGroupManagerImpl2.class,
|
||||
SecurityGroupDaoImpl.class,
|
||||
SecurityGroupVMMapDaoImpl.class,
|
||||
UserVmDetailsDaoImpl.class,
|
||||
DataCenterIpAddressDaoImpl.class,
|
||||
DataCenterLinkLocalIpAddressDaoImpl.class,
|
||||
DataCenterVnetDaoImpl.class,
|
||||
PodVlanDaoImpl.class,
|
||||
DcDetailsDaoImpl.class,
|
||||
SecurityGroupRuleDaoImpl.class,
|
||||
NicDaoImpl.class,
|
||||
SecurityGroupJoinDaoImpl.class},
|
||||
includeFilters={@Filter(value=Library.class, type=FilterType.CUSTOM)},
|
||||
useDefaultFilters=false
|
||||
)
|
||||
public class SecurityGroupManagerTestConfiguration {
|
||||
|
||||
@Bean
|
||||
public NetworkModel networkModel() {
|
||||
return Mockito.mock(NetworkModel.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AgentManager agentManager() {
|
||||
return ComponentContext.inject(MockAgentManagerImpl.class);
|
||||
return Mockito.mock(AgentManager.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VirtualMachineManager virtualMachineManager(){
|
||||
return ComponentContext.inject(MockVirtualMachineManagerImpl.class);
|
||||
return Mockito.mock(VirtualMachineManager.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UserVmManager userVmManager() {
|
||||
return ComponentContext.inject(MockUserVmManagerImpl.class);
|
||||
return Mockito.mock(UserVmManager.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NetworkManager networkManager(){
|
||||
return ComponentContext.inject(MockNetworkManagerImpl.class);
|
||||
return Mockito.mock(NetworkManager.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AccountManager accountManager() {
|
||||
return ComponentContext.inject(MockAccountManagerImpl.class);
|
||||
return Mockito.mock(AccountManager.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DomainManager domainManager() {
|
||||
return ComponentContext.inject(MockDomainManagerImpl.class);
|
||||
return Mockito.mock(DomainManager.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ProjectManager projectManager() {
|
||||
return ComponentContext.inject(MockProjectManagerImpl.class);
|
||||
return Mockito.mock(ProjectManager.class);
|
||||
}
|
||||
|
||||
public static class Library implements TypeFilter {
|
||||
|
||||
@Override
|
||||
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
|
||||
mdr.getClassMetadata().getClassName();
|
||||
ComponentScan cs = SecurityGroupManagerTestConfiguration.class.getAnnotation(ComponentScan.class);
|
||||
return SpringComponentScanUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,13 +25,13 @@ import javax.naming.ConfigurationException;
|
||||
import org.apache.cloudstack.acl.ControlledEntity;
|
||||
import org.apache.cloudstack.acl.RoleType;
|
||||
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
|
||||
import com.cloud.api.query.vo.ControlledViewEntity;
|
||||
|
||||
import org.apache.cloudstack.api.command.admin.account.UpdateAccountCmd;
|
||||
import org.apache.cloudstack.api.command.admin.user.DeleteUserCmd;
|
||||
import org.apache.cloudstack.api.command.admin.user.RegisterCmd;
|
||||
import org.apache.cloudstack.api.command.admin.account.UpdateAccountCmd;
|
||||
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.api.query.vo.ControlledViewEntity;
|
||||
import com.cloud.domain.Domain;
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
@ -44,6 +44,7 @@ import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
|
||||
|
||||
@Component
|
||||
@Local(value = { AccountManager.class, AccountService.class })
|
||||
public class MockAccountManagerImpl implements Manager, AccountManager, AccountService {
|
||||
|
||||
@ -283,27 +284,27 @@ public class MockAccountManagerImpl implements Manager, AccountManager, AccountS
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public UserAccount createUserAccount(String userName, String password,
|
||||
String firstName, String lastName, String email, String timezone,
|
||||
String accountName, short accountType, Long domainId,
|
||||
String networkDomain, Map<String, String> details) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public UserAccount createUserAccount(String userName, String password,
|
||||
String firstName, String lastName, String email, String timezone,
|
||||
String accountName, short accountType, Long domainId,
|
||||
String networkDomain, Map<String, String> details) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account createAccount(String accountName, short accountType,
|
||||
Long domainId, String networkDomain, Map details) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Account createAccount(String accountName, short accountType,
|
||||
Long domainId, String networkDomain, Map details) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enableAccount(long accountId) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean enableAccount(long accountId) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildACLSearchBuilder(SearchBuilder<? extends ControlledEntity> sb, Long domainId, boolean isRecursive, List<Long> permittedAccounts,
|
||||
|
||||
@ -25,12 +25,15 @@ import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.cloudstack.api.command.admin.domain.ListDomainChildrenCmd;
|
||||
import org.apache.cloudstack.api.command.admin.domain.ListDomainsCmd;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.domain.Domain;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.utils.component.Manager;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.Manager;
|
||||
|
||||
@Component
|
||||
@Local(value = { DomainManager.class, DomainService.class })
|
||||
public class MockDomainManagerImpl implements Manager, DomainManager, DomainService {
|
||||
|
||||
|
||||
@ -23,29 +23,29 @@ import java.util.Map;
|
||||
import javax.ejb.Local;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import com.cloud.agent.api.StopAnswer;
|
||||
import com.cloud.agent.api.VmStatsEntry;
|
||||
import com.cloud.agent.api.to.NicTO;
|
||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||
import com.cloud.agent.manager.Commands;
|
||||
import com.cloud.api.query.vo.UserVmJoinVO;
|
||||
|
||||
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
|
||||
import org.apache.cloudstack.api.command.admin.vm.RecoverVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.*;
|
||||
import org.apache.cloudstack.api.command.user.vmgroup.DeleteVMGroupCmd;
|
||||
import org.apache.cloudstack.api.command.user.volume.AttachVolumeCmd;
|
||||
import org.apache.cloudstack.api.command.user.volume.DetachVolumeCmd;
|
||||
import org.apache.cloudstack.api.command.user.template.CreateTemplateCmd;
|
||||
import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.DeployVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.DestroyVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.ListVMsCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.RebootVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.ResetVMPasswordCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.StartVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.UpgradeVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd;
|
||||
import org.apache.cloudstack.api.command.user.vmgroup.DeleteVMGroupCmd;
|
||||
import org.apache.cloudstack.api.command.user.volume.AttachVolumeCmd;
|
||||
import org.apache.cloudstack.api.command.user.volume.DetachVolumeCmd;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.agent.api.StopAnswer;
|
||||
import com.cloud.agent.api.VmStatsEntry;
|
||||
import com.cloud.agent.api.to.NicTO;
|
||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||
import com.cloud.agent.manager.Commands;
|
||||
import com.cloud.api.query.vo.UserVmJoinVO;
|
||||
import com.cloud.dc.DataCenter;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
@ -70,6 +70,7 @@ import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.Manager;
|
||||
import com.cloud.utils.exception.ExecutionException;
|
||||
|
||||
@Component
|
||||
@Local(value = { UserVmManager.class, UserVmService.class })
|
||||
public class MockUserVmManagerImpl implements UserVmManager, UserVmService, Manager {
|
||||
|
||||
@ -377,30 +378,30 @@ public class MockUserVmManagerImpl implements UserVmManager, UserVmService, Mana
|
||||
|
||||
@Override
|
||||
public VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException,
|
||||
VirtualMachineMigrationException {
|
||||
VirtualMachineMigrationException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserVm moveVMToUser(AssignVMCmd moveUserVMCmd)
|
||||
throws ResourceAllocationException, ConcurrentOperationException,
|
||||
ResourceUnavailableException, InsufficientCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public UserVm moveVMToUser(AssignVMCmd moveUserVMCmd)
|
||||
throws ResourceAllocationException, ConcurrentOperationException,
|
||||
ResourceUnavailableException, InsufficientCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserVm restoreVM(RestoreVMCmd cmd) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public UserVm restoreVM(RestoreVMCmd cmd) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -410,18 +411,18 @@ public class MockUserVmManagerImpl implements UserVmManager, UserVmService, Mana
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareStop(VirtualMachineProfile<UserVmVO> profile) {
|
||||
// TODO Auto-generated method stub
|
||||
@Override
|
||||
public void prepareStop(VirtualMachineProfile<UserVmVO> profile) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.cloud.vm.VirtualMachineGuru#plugNic(com.cloud.network.Network, com.cloud.agent.api.to.NicTO, com.cloud.agent.api.to.VirtualMachineTO, com.cloud.vm.ReservationContext, com.cloud.deploy.DeployDestination)
|
||||
*/
|
||||
@Override
|
||||
public boolean plugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException,
|
||||
InsufficientCapacityException {
|
||||
InsufficientCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -23,6 +23,8 @@ import java.util.Map;
|
||||
import javax.ejb.Local;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.agent.api.to.NicTO;
|
||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
@ -51,6 +53,7 @@ import com.cloud.vm.VirtualMachine.Event;
|
||||
import com.cloud.vm.VirtualMachine.Type;
|
||||
import com.cloud.vm.VirtualMachineProfile.Param;
|
||||
|
||||
@Component
|
||||
@Local(value = VirtualMachineManager.class)
|
||||
public class MockVirtualMachineManagerImpl implements VirtualMachineManager {
|
||||
|
||||
@ -212,18 +215,18 @@ public class MockVirtualMachineManagerImpl implements VirtualMachineManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends VMInstanceVO> T storageMigration(T vm,
|
||||
StoragePool storagePoolId) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public <T extends VMInstanceVO> T storageMigration(T vm,
|
||||
StoragePool storagePoolId) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VMInstanceVO findById(long vmId) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public VMInstanceVO findById(long vmId) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.cloud.vm.VirtualMachineManager#checkIfCanUpgrade(com.cloud.vm.VirtualMachine, long)
|
||||
@ -231,7 +234,7 @@ public class MockVirtualMachineManagerImpl implements VirtualMachineManager {
|
||||
@Override
|
||||
public void checkIfCanUpgrade(VirtualMachine vmInstance, long newServiceOfferingId) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
42
server/test/resources/SecurityGroupManagerTestContext.xml
Normal file
42
server/test/resources/SecurityGroupManagerTestContext.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
|
||||
license agreements. See the NOTICE file distributed with this work for additional
|
||||
information regarding copyright ownership. The ASF licenses this file to
|
||||
you under the Apache License, Version 2.0 (the "License"); you may not use
|
||||
this file except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
|
||||
by applicable law or agreed to in writing, software distributed under the
|
||||
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
||||
OF ANY KIND, either express or implied. See the License for the specific
|
||||
language governing permissions and limitations under the License. -->
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
|
||||
http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<context:annotation-config />
|
||||
|
||||
<!-- @DB support -->
|
||||
<aop:config proxy-target-class="true">
|
||||
<aop:aspect id="dbContextBuilder" ref="transactionContextBuilder">
|
||||
<aop:pointcut id="captureAnyMethod" expression="execution(* *(..))" />
|
||||
|
||||
<aop:around pointcut-ref="captureAnyMethod" method="AroundAnyMethod" />
|
||||
</aop:aspect>
|
||||
|
||||
</aop:config>
|
||||
|
||||
<bean id="transactionContextBuilder" class="com.cloud.utils.db.TransactionContextBuilder" />
|
||||
<bean id="componentContext" class="com.cloud.utils.component.ComponentContext"/>
|
||||
<bean id="TestConfiguration"
|
||||
class="com.cloud.network.security.SecurityGroupManagerTestConfiguration" />
|
||||
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
|
||||
<property name="requiredParameterValue" value="false" />
|
||||
</bean>
|
||||
</beans>
|
||||
@ -17,13 +17,19 @@
|
||||
|
||||
package com.cloud.utils.component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.framework.ProxyFactoryBean;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
@ -32,7 +38,7 @@ import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.utils.component.ComponentContext;
|
||||
import com.cloud.utils.db.GenericDao;
|
||||
import com.cloud.utils.db.TransactionContextBuilder;
|
||||
|
||||
/**
|
||||
@ -45,124 +51,209 @@ import com.cloud.utils.db.TransactionContextBuilder;
|
||||
public class ComponentContext implements ApplicationContextAware {
|
||||
private static final Logger s_logger = Logger.getLogger(ComponentContext.class);
|
||||
|
||||
private static ApplicationContext s_appContext;
|
||||
private static ApplicationContext s_appContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
s_appContext = applicationContext;
|
||||
}
|
||||
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return s_appContext;
|
||||
}
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void initComponentsLifeCycle() {
|
||||
@SuppressWarnings("rawtype")
|
||||
Collection<GenericDao> daos = ComponentContext.getApplicationContext().getBeansOfType(GenericDao.class).values();
|
||||
Collection<Manager> mgrs = ComponentContext.getApplicationContext().getBeansOfType(Manager.class).values();
|
||||
Collection<Adapter> adapters = ComponentContext.getApplicationContext().getBeansOfType(Adapter.class).values();
|
||||
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
for (GenericDao dao : daos) {
|
||||
try {
|
||||
s_logger.info("Configuring DAO: " + ComponentContext.getTargetClass(dao).getName());
|
||||
dao.configure(dao.getClass().getSimpleName(), params);
|
||||
} catch (ConfigurationException e) {
|
||||
s_logger.error("Unable to configure DAO: " + dao.getClass().getSimpleName(), e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
List<String> avoidMap = new ArrayList<String>();
|
||||
for (Manager manager : mgrs) {
|
||||
if (avoidMap.contains(manager.getName())) {
|
||||
s_logger.info("Skip manager: " + ComponentContext.getTargetClass(manager).getName() + " as it is already started");
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
s_logger.info("Configuring manager: " + ComponentContext.getTargetClass(manager).getName() + "...");
|
||||
manager.configure(manager.getClass().getSimpleName(), params);
|
||||
avoidMap.add(manager.getName());
|
||||
} catch (Exception e) {
|
||||
s_logger.error("Problems to start manager:" + ComponentContext.getTargetClass(manager).getName(), e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (Adapter adapter : adapters) {
|
||||
if (avoidMap.contains(adapter.getName())) {
|
||||
s_logger.info("Skip adapter: " + ComponentContext.getTargetClass(adapter).getName() + " as it is already started");
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
s_logger.info("Configuring adapter: " + ComponentContext.getTargetClass(adapter).getName() + "...");
|
||||
adapter.configure(adapter.getName(), params);
|
||||
avoidMap.add(adapter.getName());
|
||||
} catch (Exception e) {
|
||||
s_logger.error("Problems to start adapter:" + ComponentContext.getTargetClass(adapter).getName(), e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
avoidMap.clear();
|
||||
|
||||
for (Manager manager : mgrs) {
|
||||
if (avoidMap.contains(manager.getName())) {
|
||||
s_logger.info("Skip start on manager: " + ComponentContext.getTargetClass(manager).getName() + " as it is already started");
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
s_logger.info("Starting manager: " + ComponentContext.getTargetClass(manager).getName() + "...");
|
||||
manager.start();
|
||||
avoidMap.add(manager.getName());
|
||||
} catch (Exception e) {
|
||||
s_logger.error("Problems to start manager:" + ComponentContext.getTargetClass(manager).getName(), e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (Adapter adapter : adapters) {
|
||||
if (avoidMap.contains(adapter.getName())) {
|
||||
s_logger.info("Skip start on adapter: " + ComponentContext.getTargetClass(adapter).getName() + " as it is already started");
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
s_logger.info("Startinging adapter: " + ComponentContext.getTargetClass(adapter).getName() + "...");
|
||||
adapter.start();
|
||||
avoidMap.add(adapter.getName());
|
||||
} catch (Exception e) {
|
||||
s_logger.error("Problems to start adapter:" + ComponentContext.getTargetClass(adapter).getName(), e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T getComponent(String name) {
|
||||
assert(s_appContext != null);
|
||||
return (T)s_appContext.getBean(name);
|
||||
assert(s_appContext != null);
|
||||
return (T)s_appContext.getBean(name);
|
||||
}
|
||||
|
||||
|
||||
public static <T> T getComponent(Class<T> beanType) {
|
||||
assert(s_appContext != null);
|
||||
try {
|
||||
return (T)s_appContext.getBean(beanType);
|
||||
} catch(NoSuchBeanDefinitionException e) {
|
||||
Map<String, T> matchedTypes = getComponentsOfType(beanType);
|
||||
if(matchedTypes.size() > 0) {
|
||||
for(Map.Entry<String, T> entry : matchedTypes.entrySet()) {
|
||||
Primary primary = getTargetClass(entry.getValue()).getAnnotation(Primary.class);
|
||||
if(primary != null)
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
if(matchedTypes.size() > 1) {
|
||||
s_logger.warn("Unable to uniquely locate bean type " + beanType.getName());
|
||||
for(Map.Entry<String, T> entry : matchedTypes.entrySet()) {
|
||||
s_logger.warn("Candidate " + getTargetClass(entry.getValue()).getName());
|
||||
}
|
||||
}
|
||||
|
||||
return (T)matchedTypes.values().toArray()[0];
|
||||
}
|
||||
}
|
||||
throw new NoSuchBeanDefinitionException(beanType.getName());
|
||||
assert(s_appContext != null);
|
||||
try {
|
||||
return s_appContext.getBean(beanType);
|
||||
} catch(NoSuchBeanDefinitionException e) {
|
||||
Map<String, T> matchedTypes = getComponentsOfType(beanType);
|
||||
if(matchedTypes.size() > 0) {
|
||||
for(Map.Entry<String, T> entry : matchedTypes.entrySet()) {
|
||||
Primary primary = getTargetClass(entry.getValue()).getAnnotation(Primary.class);
|
||||
if(primary != null)
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
if(matchedTypes.size() > 1) {
|
||||
s_logger.warn("Unable to uniquely locate bean type " + beanType.getName());
|
||||
for(Map.Entry<String, T> entry : matchedTypes.entrySet()) {
|
||||
s_logger.warn("Candidate " + getTargetClass(entry.getValue()).getName());
|
||||
}
|
||||
}
|
||||
|
||||
return (T)matchedTypes.values().toArray()[0];
|
||||
}
|
||||
}
|
||||
throw new NoSuchBeanDefinitionException(beanType.getName());
|
||||
}
|
||||
|
||||
|
||||
public static <T> Map<String, T> getComponentsOfType(Class<T> beanType) {
|
||||
return s_appContext.getBeansOfType(beanType);
|
||||
return s_appContext.getBeansOfType(beanType);
|
||||
}
|
||||
|
||||
|
||||
public static <T> boolean isPrimary(Object instance, Class<T> beanType) {
|
||||
// we assume single line of interface inheritance of beanType
|
||||
Class<?> componentType = beanType;
|
||||
Class<?> targetClass = getTargetClass(instance);
|
||||
|
||||
Class<?> interfaces[] = targetClass.getInterfaces();
|
||||
for(Class<?> intf : interfaces) {
|
||||
if(beanType.isAssignableFrom(intf) && intf != beanType) {
|
||||
componentType = intf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, T> matchedTypes = (Map<String, T>)ComponentContext.getComponentsOfType(componentType);
|
||||
if(matchedTypes.size() > 1) {
|
||||
Primary primary = targetClass.getAnnotation(Primary.class);
|
||||
if(primary != null) {
|
||||
s_logger.info(targetClass.getName() + " is the primary component of " + componentType.getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
s_logger.warn(targetClass.getName() + " is not the primary component of " + componentType.getName() + ", there are other candidates");
|
||||
for(T candidate : matchedTypes.values()) {
|
||||
s_logger.warn("Candidate " + getTargetClass(candidate).getName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
// we assume single line of interface inheritance of beanType
|
||||
Class<?> componentType = beanType;
|
||||
Class<?> targetClass = getTargetClass(instance);
|
||||
|
||||
Class<?> interfaces[] = targetClass.getInterfaces();
|
||||
for(Class<?> intf : interfaces) {
|
||||
if(beanType.isAssignableFrom(intf) && intf != beanType) {
|
||||
componentType = intf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, T> matchedTypes = (Map<String, T>)ComponentContext.getComponentsOfType(componentType);
|
||||
if(matchedTypes.size() > 1) {
|
||||
Primary primary = targetClass.getAnnotation(Primary.class);
|
||||
if(primary != null) {
|
||||
s_logger.info(targetClass.getName() + " is the primary component of " + componentType.getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
s_logger.warn(targetClass.getName() + " is not the primary component of " + componentType.getName() + ", there are other candidates");
|
||||
for(T candidate : matchedTypes.values()) {
|
||||
s_logger.warn("Candidate " + getTargetClass(candidate).getName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static Class<?> getTargetClass(Object instance) {
|
||||
while(instance instanceof Advised) {
|
||||
try {
|
||||
instance = ((Advised)instance).getTargetSource().getTarget();
|
||||
} catch(Exception e) {
|
||||
return instance.getClass();
|
||||
}
|
||||
}
|
||||
return instance.getClass();
|
||||
while(instance instanceof Advised) {
|
||||
try {
|
||||
instance = ((Advised)instance).getTargetSource().getTarget();
|
||||
} catch(Exception e) {
|
||||
return instance.getClass();
|
||||
}
|
||||
}
|
||||
return instance.getClass();
|
||||
}
|
||||
|
||||
|
||||
public static <T> T getTargetObject(Object instance) {
|
||||
while(instance instanceof Advised) {
|
||||
try {
|
||||
instance = ((Advised)instance).getTargetSource().getTarget();
|
||||
} catch (Exception e) {
|
||||
return (T)instance;
|
||||
}
|
||||
try {
|
||||
instance = ((Advised)instance).getTargetSource().getTarget();
|
||||
} catch (Exception e) {
|
||||
return (T)instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (T)instance;
|
||||
}
|
||||
|
||||
|
||||
public static <T> T inject(Class<T> clz) {
|
||||
T instance = s_appContext.getAutowireCapableBeanFactory().createBean(clz);
|
||||
return inject(instance);
|
||||
T instance = s_appContext.getAutowireCapableBeanFactory().createBean(clz);
|
||||
return inject(instance);
|
||||
}
|
||||
|
||||
|
||||
public static <T> T inject(Object instance) {
|
||||
// autowire dynamically loaded object
|
||||
AutowireCapableBeanFactory beanFactory = s_appContext.getAutowireCapableBeanFactory();
|
||||
// autowire dynamically loaded object
|
||||
AutowireCapableBeanFactory beanFactory = s_appContext.getAutowireCapableBeanFactory();
|
||||
beanFactory.autowireBean(instance);
|
||||
|
||||
beanFactory.autowireBean(instance);
|
||||
beanFactory.initializeBean(instance, null);
|
||||
Advisor advisor = new DefaultPointcutAdvisor(new MatchAnyMethodPointcut(),
|
||||
new TransactionContextBuilder());
|
||||
|
||||
Advisor advisor = new DefaultPointcutAdvisor(new MatchAnyMethodPointcut(),
|
||||
new TransactionContextBuilder());
|
||||
|
||||
ProxyFactory pf = new ProxyFactory();
|
||||
ProxyFactory pf = new ProxyFactory();
|
||||
pf.setTarget(instance);
|
||||
pf.addAdvisor(advisor);
|
||||
|
||||
|
||||
return (T)pf.getProxy();
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package com.cloud.utils.component;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
|
||||
public class SpringComponentScanUtils {
|
||||
|
||||
public static boolean includedInBasePackageClasses(String clazzName, ComponentScan cs) {
|
||||
Class<?> clazzToCheck;
|
||||
try {
|
||||
clazzToCheck = Class.forName(clazzName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new CloudRuntimeException("Unable to find " + clazzName);
|
||||
}
|
||||
Class<?>[] clazzes = cs.basePackageClasses();
|
||||
for (Class<?> clazz : clazzes) {
|
||||
if (clazzToCheck.isAssignableFrom(clazz)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user