Skip to content

Commit 5c73294

Browse files
committed
injection code smell removed
1 parent be01643 commit 5c73294

File tree

8 files changed

+59
-32
lines changed

8 files changed

+59
-32
lines changed

engine/components-api/src/main/java/com/cloud/network/NetworkStateListener.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,25 @@
3232
import com.cloud.network.Network.State;
3333
import com.cloud.utils.fsm.StateListener;
3434
import com.cloud.utils.fsm.StateMachine2;
35+
import org.springframework.beans.factory.annotation.Autowired;
36+
import org.springframework.beans.factory.annotation.Qualifier;
3537

3638
public class NetworkStateListener implements StateListener<State, Event, Network> {
3739

3840
@Inject
3941
private ConfigurationDao _configDao;
40-
@Inject
42+
@Autowired
43+
@Qualifier("eventDistributor")
4144
private EventDistributor eventDistributor;
4245

4346
public NetworkStateListener(ConfigurationDao configDao) {
4447
_configDao = configDao;
4548
}
4649

50+
public void setEventDistributor(EventDistributor eventDistributor) {
51+
this.eventDistributor = eventDistributor;
52+
}
53+
4754
@Override
4855
public boolean preStateTransitionEvent(State oldState, Event event, State newState, Network vo, boolean status, Object opaque) {
4956
pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState);
@@ -70,7 +77,7 @@ private void pubishOnEventBus(String event, String status, Network vo, State old
7077
String resourceName = getEntityFromClassName(Network.class.getName());
7178
org.apache.cloudstack.framework.events.Event eventMsg =
7279
new org.apache.cloudstack.framework.events.Event("management-server", EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid());
73-
Map<String, String> eventDescription = new HashMap<String, String>();
80+
Map<String, String> eventDescription = new HashMap<>();
7481
eventDescription.put("resource", resourceName);
7582
eventDescription.put("id", vo.getUuid());
7683
eventDescription.put("old-state", oldState.name());

framework/events/src/main/java/org/apache/cloudstack/framework/events/EventDistributorImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public void init() {
4747
public List<EventBusException> publish(Event event) {
4848
LOGGER.info(String.format("publishing %s to %d event busses", (event == null ? "<none>" : event.getDescription()), eventBusses.size()));
4949
List<EventBusException> exceptions = new ArrayList<>();
50+
if (event == null) {
51+
return exceptions;
52+
}
5053
for (EventBus bus : eventBusses) {
5154
try {
5255
bus.publish(event);

plugins/event-bus/kafka/src/main/java/org/apache/cloudstack/mom/kafka/KafkaEventBus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public void publish(Event event) throws EventBusException {
110110
if (s_logger.isTraceEnabled()) {
111111
s_logger.trace(String.format("publish \'%s\'", event.getDescription()));
112112
}
113-
ProducerRecord<String, String> record = new ProducerRecord<>(_topic, event.getResourceUUID(), event.getDescription());
114-
_producer.send(record);
113+
ProducerRecord<String, String> newRecord = new ProducerRecord<>(_topic, event.getResourceUUID(), event.getDescription());
114+
_producer.send(newRecord);
115115
}
116116

117117
@Override

plugins/network-elements/juniper-contrail/src/main/java/org/apache/cloudstack/network/contrail/management/EventUtils.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,29 @@
4343
import com.cloud.utils.component.ComponentContext;
4444
import com.cloud.utils.component.ComponentMethodInterceptor;
4545

46-
import javax.inject.Inject;
4746

4847
@Component
4948
public class EventUtils {
5049
private static final Logger s_logger = Logger.getLogger(EventUtils.class);
5150

52-
@Inject
53-
private EventDistributor eventDistributor;
51+
private static EventDistributor eventDistributor;
5452
protected static EventBus s_eventBus = null;
5553

5654
public EventUtils() {
5755
}
5856

57+
public static void setEventDistributor(EventDistributor eventDistributorImpl) {
58+
eventDistributor = eventDistributorImpl;
59+
}
60+
5961
private static void publishOnMessageBus(String eventCategory, String eventType, String details, Event.State state) {
6062

6163
if (state != com.cloud.event.Event.State.Completed) {
6264
return;
6365
}
6466

6567
try {
68+
setEventDistributor(ComponentContext.getComponent(EventDistributor.class));
6669
s_eventBus = ComponentContext.getComponent(EventBus.class);
6770
} catch (NoSuchBeanDefinitionException nbe) {
6871
return; // no provider is configured to provide events bus, so just return
@@ -71,16 +74,15 @@ private static void publishOnMessageBus(String eventCategory, String eventType,
7174
org.apache.cloudstack.framework.events.Event event =
7275
new org.apache.cloudstack.framework.events.Event(ManagementService.Name, eventCategory, eventType, EventTypes.getEntityForEvent(eventType), null);
7376

74-
Map<String, String> eventDescription = new HashMap<String, String>();
77+
Map<String, String> eventDescription = new HashMap<>();
7578
eventDescription.put("event", eventType);
7679
eventDescription.put("status", state.toString());
7780
eventDescription.put("details", details);
7881
event.setDescription(eventDescription);
79-
try {
80-
s_eventBus.publish(event);
81-
} catch (EventBusException evx) {
82-
String errMsg = "Failed to publish contrail event.";
83-
s_logger.warn(errMsg, evx);
82+
List<EventBusException> exceptions = eventDistributor.publish(event);
83+
for (EventBusException ex : exceptions) {
84+
String errMsg = "Failed to publish event.";
85+
s_logger.warn(errMsg, ex);
8486
}
8587

8688
}
@@ -123,7 +125,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
123125
}
124126

125127
protected List<ActionEvent> getActionEvents(Method m) {
126-
List<ActionEvent> result = new ArrayList<ActionEvent>();
128+
List<ActionEvent> result = new ArrayList<>();
127129

128130
ActionEvents events = m.getAnnotation(ActionEvents.class);
129131

server/src/main/java/com/cloud/storage/listener/SnapshotStateListener.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import javax.annotation.PostConstruct;
2626
import javax.inject.Inject;
2727

28+
import org.springframework.stereotype.Component;
29+
2830
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
2931
import org.apache.cloudstack.framework.events.EventDistributor;
30-
import org.apache.log4j.Logger;
31-
import org.springframework.stereotype.Component;
3232

3333
import com.cloud.configuration.Config;
3434
import com.cloud.event.EventCategory;
@@ -37,6 +37,7 @@
3737
import com.cloud.storage.Snapshot.Event;
3838
import com.cloud.storage.Snapshot.State;
3939
import com.cloud.storage.SnapshotVO;
40+
import com.cloud.utils.component.ComponentContext;
4041
import com.cloud.utils.fsm.StateListener;
4142
import com.cloud.utils.fsm.StateMachine2;
4243

@@ -47,10 +48,8 @@ public class SnapshotStateListener implements StateListener<State, Event, Snapsh
4748

4849
@Inject
4950
private ConfigurationDao configDao;
50-
@Inject
51-
private EventDistributor eventDistributor;
5251

53-
private static final Logger s_logger = Logger.getLogger(SnapshotStateListener.class);
52+
private EventDistributor eventDistributor = null;
5453

5554
public SnapshotStateListener() {
5655

@@ -61,6 +60,10 @@ void init() {
6160
s_configDao = configDao;
6261
}
6362

63+
public void setEventDistributor(EventDistributor eventDistributor) {
64+
this.eventDistributor = eventDistributor;
65+
}
66+
6467
@Override
6568
public boolean preStateTransitionEvent(State oldState, Event event, State newState, SnapshotVO vo, boolean status, Object opaque) {
6669
pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState);
@@ -81,12 +84,15 @@ private void pubishOnEventBus(String event, String status, Snapshot vo, State ol
8184
if(!configValue) {
8285
return;
8386
}
87+
if (eventDistributor == null) {
88+
setEventDistributor(ComponentContext.getComponent(EventDistributor.class));
89+
}
8490

8591
String resourceName = getEntityFromClassName(Snapshot.class.getName());
8692
org.apache.cloudstack.framework.events.Event eventMsg =
8793
new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName,
8894
vo.getUuid());
89-
Map<String, String> eventDescription = new HashMap<String, String>();
95+
Map<String, String> eventDescription = new HashMap<>();
9096
eventDescription.put("resource", resourceName);
9197
eventDescription.put("id", vo.getUuid());
9298
eventDescription.put("old-state", oldState.name());

server/src/main/java/com/cloud/storage/listener/VolumeStateListener.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25-
import javax.inject.Inject;
26-
2725
import com.cloud.configuration.Config;
2826
import com.cloud.event.EventCategory;
2927
import com.cloud.event.EventTypes;
@@ -32,6 +30,7 @@
3230
import com.cloud.storage.Volume;
3331
import com.cloud.storage.Volume.Event;
3432
import com.cloud.storage.Volume.State;
33+
import com.cloud.utils.component.ComponentContext;
3534
import com.cloud.utils.fsm.StateListener;
3635
import com.cloud.utils.fsm.StateMachine2;
3736
import com.cloud.vm.VMInstanceVO;
@@ -41,23 +40,22 @@
4140
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
4241
import org.apache.cloudstack.framework.events.EventDistributor;
4342

44-
import org.apache.log4j.Logger;
45-
4643
public class VolumeStateListener implements StateListener<State, Event, Volume> {
4744

4845
protected ConfigurationDao _configDao;
4946
protected VMInstanceDao _vmInstanceDao;
5047

51-
@Inject
5248
private EventDistributor eventDistributor;
5349

54-
private static final Logger s_logger = Logger.getLogger(VolumeStateListener.class);
55-
5650
public VolumeStateListener(ConfigurationDao configDao, VMInstanceDao vmInstanceDao) {
5751
this._configDao = configDao;
5852
this._vmInstanceDao = vmInstanceDao;
5953
}
6054

55+
public void setEventDistributor(EventDistributor eventDistributor) {
56+
this.eventDistributor = eventDistributor;
57+
}
58+
6159
@Override
6260
public boolean preStateTransitionEvent(State oldState, Event event, State newState, Volume vo, boolean status, Object opaque) {
6361
pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState);
@@ -93,18 +91,21 @@ public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> t
9391
return true;
9492
}
9593

96-
private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
94+
private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
9795

9896
String configKey = Config.PublishResourceStateEvent.key();
9997
String value = _configDao.getValue(configKey);
10098
boolean configValue = Boolean.parseBoolean(value);
10199
if(!configValue)
102100
return;
101+
if (eventDistributor == null) {
102+
setEventDistributor(ComponentContext.getComponent(EventDistributor.class));
103+
}
103104

104105
String resourceName = getEntityFromClassName(Volume.class.getName());
105106
org.apache.cloudstack.framework.events.Event eventMsg =
106107
new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName,
107-
vo.getUuid());
108+
vo.getUuid());
108109
Map<String, String> eventDescription = new HashMap<String, String>();
109110
eventDescription.put("resource", resourceName);
110111
eventDescription.put("id", vo.getUuid());

server/src/main/java/com/cloud/vm/UserVmStateListener.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.cloud.network.dao.NetworkVO;
3434
import com.cloud.service.dao.ServiceOfferingDao;
3535
import com.cloud.server.ManagementService;
36+
import com.cloud.utils.component.ComponentContext;
3637
import com.cloud.utils.fsm.StateListener;
3738
import com.cloud.utils.fsm.StateMachine2;
3839
import com.cloud.vm.VirtualMachine.Event;
@@ -52,7 +53,7 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
5253
@Inject protected UserVmDao _userVmDao;
5354
@Inject protected UserVmManager _userVmMgr;
5455
@Inject protected ConfigurationDao _configDao;
55-
@Inject private EventDistributor eventDistributor;
56+
private EventDistributor eventDistributor;
5657

5758
public UserVmStateListener(UsageEventDao usageEventDao, NetworkDao networkDao, NicDao nicDao, ServiceOfferingDao offeringDao, UserVmDao userVmDao, UserVmManager userVmMgr,
5859
ConfigurationDao configDao) {
@@ -65,6 +66,10 @@ public UserVmStateListener(UsageEventDao usageEventDao, NetworkDao networkDao, N
6566
this._configDao = configDao;
6667
}
6768

69+
public void setEventDistributor(EventDistributor eventDistributor) {
70+
this.eventDistributor = eventDistributor;
71+
}
72+
6873
@Override
6974
public boolean preStateTransitionEvent(State oldState, Event event, State newState, VirtualMachine vo, boolean status, Object opaque) {
7075
pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState);
@@ -122,12 +127,15 @@ private void pubishOnEventBus(String event, String status, VirtualMachine vo, Vi
122127
boolean configValue = Boolean.parseBoolean(value);
123128
if(!configValue)
124129
return;
130+
if (eventDistributor == null) {
131+
setEventDistributor(ComponentContext.getComponent(EventDistributor.class));
132+
}
125133

126134
String resourceName = getEntityFromClassName(VirtualMachine.class.getName());
127135
org.apache.cloudstack.framework.events.Event eventMsg =
128136
new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName,
129137
vo.getUuid());
130-
Map<String, String> eventDescription = new HashMap<String, String>();
138+
Map<String, String> eventDescription = new HashMap<>();
131139
eventDescription.put("resource", resourceName);
132140
eventDescription.put("id", vo.getUuid());
133141
eventDescription.put("old-state", oldState.name());

utils/src/main/java/com/cloud/utils/component/ComponentContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static void initComponentsLifeCycle() {
100100
s_logger.info("Running SystemIntegrityChecker " + entry.getKey());
101101
try {
102102
entry.getValue().check();
103-
} catch (Throwable e) {
103+
} catch (RuntimeException e) {
104104
s_logger.error("System integrity check failed. Refuse to startup", e);
105105
System.exit(1);
106106
}

0 commit comments

Comments
 (0)