Commit dac060dc authored by Peter Harrison's avatar Peter Harrison

Adding Findbugs to POM

Adding  try/catch to  ensure OCM connections are closed on error.
Addressing some findbugs bugs.
parent 549d8817
......@@ -439,4 +439,14 @@
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
</plugin>
</plugins>
</reporting>
</project>
......@@ -100,17 +100,11 @@ public class AutomationEngine {
try {
card = cardController.getCard(cardHolder.getBoardId(),
null, cardHolder.getCardId(), "full");
board = boardsCache.getItem(cardHolder.getBoardId());
} catch (ResourceNotFoundException e) {
LOG.info("Resource no longer exists: " + cardHolder.toString());
return;
}
if (board == null) {
LOG.warn("Board Not Found: " + cardHolder.getBoardId());
return;
}
Map<String,Rule> rules = getRulesFromBoard(cardHolder.getBoardId());
LOG.info("Automation Examining :" + card.getPath());
......@@ -121,7 +115,7 @@ public class AutomationEngine {
Collection<CardEvent> alerts = cardController.getAlerts(card.getBoard(), card.getPhase(), card.getId().toString());
for( CardEvent alert : alerts){
if(alert==null){
LOG.equals("Alert NULL inside Loop");
LOG.error("Alert NULL inside Loop");
}
if("alert".equals(alert.getLevel())){
......@@ -698,10 +692,6 @@ public class AutomationEngine {
// Get Rule.
Rule rule = ruleCache.getItem(boardId, ruleId);
if(rule==null){
LOG.warn("Execute Actions: Rule Not Found: "+ boardId + "/" + ruleId);
return;
}
List<Action> sortedActionList = getSortedActions(rule.getActions());
......@@ -771,37 +761,42 @@ public class AutomationEngine {
private void raiseAlertOnError(Throwable e, Rule rule, Action action, Card card) throws Exception{
String causeMessage = "";
StringBuilder message = new StringBuilder();
message.append(rule.getName());
message.append(" ");
message.append(action.getName());
message.append(" calling: ");
message.append(action.getType());
message.append(".");
message.append(action.getResource());
message.append(".");
message.append(action.getMethod());
message.append(" caused ");
Throwable cause = e;
while(cause!=null){
if( cause.getMessage()!=null){
causeMessage = causeMessage + "(" + cause.getMessage() + ") ";
message.append("(");
message.append(cause.getMessage());
message.append(") ");
}
cause = cause.getCause();
}
String message = rule.getName()
+ " " + action.getName()
+ " calling: " + action.getType()
+ "." + action.getResource()
+ "." + action.getMethod()
+ " caused " + causeMessage;
if(card!=null){
if( cardController!=null){
cardController.saveAlert(
card.getBoard(),
card.getPhase(),
card.getId().toString(),
message,
message.toString(),
"alert");
}
LOG.warn( "Automation Exception - Card " + card.getId() + " " + message, e);
LOG.warn( "Automation Exception - Card " + card.getId() + " " + message.toString(), e);
} else {
LOG.warn( "Automation Exception " + message, e);
LOG.warn( "Automation Exception " + message.toString(), e);
}
}
private void outputContext( Map<String,Object> context){
......
......@@ -115,7 +115,7 @@ public class ClusterManager {
*/
@Scheduled( fixedDelay=30000l )
public void pollLeader() throws Exception{
if(!this.leader){
if(!isLeader()){
this.executeChallenge();
}
}
......@@ -137,28 +137,28 @@ public class ClusterManager {
Message coupMessage = coupSync.receiveMessage(10000l);
boolean newLeader = (coupMessage==null);
if( this.leader!=newLeader){
if( isLeader()!=newLeader){
if(newLeader){
LOG.warn("FAILOVER: Node promoted to MASTER.");
this.leader = true;
setLeader(true);
this.jcrObserver.start();
this.timerManager.startup();
} else {
LOG.warn("FAILOVER: Node demoted to worker.");
this.leader = false;
setLeader(false);
this.jcrObserver.stop();
this.timerManager.stopAll();
}
}
return this.leader;
return isLeader();
}
public void setLeader(boolean leader) {
public synchronized void setLeader(boolean leader) {
this.leader = leader;
}
public boolean isLeader() {
public synchronized boolean isLeader() {
return leader;
}
......
......@@ -105,10 +105,6 @@ public class TimerManager {
Map<String, String> rules;
try {
rules = ruleCache.list(boardId,"");
if( rules==null){
LOG.warn("Board Rules Not Found when Loading Timer: " + boardId);
return;
}
} catch (javax.jcr.PathNotFoundException e ){
LOG.info("No Rule for Board: "+ boardId);
return;
......
......@@ -62,6 +62,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.amazonaws.util.StringUtils;
/**
*
*/
......@@ -407,11 +409,11 @@ public class BoardController {
detailFilter = "(jcr:contains(@detail,'${detail}'))".replaceAll("\\$\\{detail\\}", detail);
}
if(categoryFilter!="" && detailFilter!=""){
if( !StringUtils.isNullOrEmpty(categoryFilter) && !StringUtils.isNullOrEmpty(detailFilter)){
qmFilter.addJCRExpression( categoryFilter + " or " + detailFilter);
} else if (categoryFilter!="" && detailFilter=="") {
} else if ( !StringUtils.isNullOrEmpty(categoryFilter) && StringUtils.isNullOrEmpty(detailFilter)) {
qmFilter.addJCRExpression( categoryFilter );
} else if (categoryFilter=="" && detailFilter!="") {
} else if (StringUtils.isNullOrEmpty(categoryFilter) && !StringUtils.isNullOrEmpty(detailFilter)) {
qmFilter.addJCRExpression( detailFilter );
}
......
......@@ -913,7 +913,6 @@ public class CardController {
Rule rule = ruleCache.getItem(boardId,taskId);
if(rule==null){
ocm.logout();
logger.warn("Rule Not Found: " + boardId + "." + taskId);
throw new ResourceNotFoundException();
}
......@@ -1030,15 +1029,12 @@ public class CardController {
String.format(URI.TASKS_URI, boardId, phaseId, cardId,
taskId));
if (node == null) {
ocm.logout();
throw new ResourceNotFoundException();
}
node.remove();
ocm.save();
} finally {
if (null != ocm) {
ocm.logout();
}
ocm.logout();
}
}
......
......@@ -77,13 +77,16 @@ public class FilterController {
}
ObjectContentManager ocm = ocmFactory.getOcm();
listTools.ensurePresence(String.format(URI.BOARD_URI, boardId), "filters", ocm.getSession());
String newId = IdentifierTools.getIdFromNamedModelClass(filter);
filter.setPath(String.format(URI.FILTER_URI, boardId, newId.toString()));
ocm.insert(filter);
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
try {
listTools.ensurePresence(String.format(URI.BOARD_URI, boardId), "filters", ocm.getSession());
String newId = IdentifierTools.getIdFromNamedModelClass(filter);
filter.setPath(String.format(URI.FILTER_URI, boardId, newId.toString()));
ocm.insert(filter);
ocm.save();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
} finally {
ocm.logout();
}
return filter;
}
......@@ -98,10 +101,14 @@ public class FilterController {
}
ObjectContentManager ocm = ocmFactory.getOcm();
ocm.update(filter);
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
try {
ocm.update(filter);
ocm.save();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
} finally {
ocm.logout();
}
return filter;
}
......@@ -110,12 +117,16 @@ public class FilterController {
public @ResponseBody Filter getFilter(@PathVariable String boardId,
@PathVariable String filterId) throws Exception {
ObjectContentManager ocm = ocmFactory.getOcm();
Filter filter = (Filter) ocm.getObject(Filter.class,String.format(URI.FILTER_URI, boardId, filterId));
ocm.logout();
if(filter==null){
throw new ResourceNotFoundException();
}
ObjectContentManager ocm = ocmFactory.getOcm();
Filter filter = null;
try {
filter = (Filter) ocm.getObject(Filter.class,String.format(URI.FILTER_URI, boardId, filterId));
if(filter==null){
throw new ResourceNotFoundException();
}
} finally {
ocm.logout();
}
return filter;
}
......@@ -125,13 +136,16 @@ public class FilterController {
@PathVariable String filterId,
@RequestBody Condition filterField) throws Exception {
ObjectContentManager ocm = ocmFactory.getOcm();
listTools.ensurePresence(String.format(URI.FILTER_URI, boardId, filterId), "conditions", ocm.getSession());
filterField.setPath(String.format(URI.FILTER_CONDITION_URI, boardId, filterId, filterField.getFieldName()));
ocm.insert(filterField);
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
ObjectContentManager ocm = ocmFactory.getOcm();
try {
listTools.ensurePresence(String.format(URI.FILTER_URI, boardId, filterId), "conditions", ocm.getSession());
filterField.setPath(String.format(URI.FILTER_CONDITION_URI, boardId, filterId, filterField.getFieldName()));
ocm.insert(filterField);
ocm.save();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
} finally {
ocm.logout();
}
return filterField;
}
......@@ -141,12 +155,15 @@ public class FilterController {
@PathVariable String filterId,
@PathVariable String fieldId) throws Exception {
ObjectContentManager ocm = ocmFactory.getOcm();
String path = String.format(URI.FILTER_CONDITION_URI, boardId, filterId, fieldId);
ocm.getSession().removeItem(path);
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
ObjectContentManager ocm = ocmFactory.getOcm();
try {
String path = String.format(URI.FILTER_CONDITION_URI, boardId, filterId, fieldId);
ocm.getSession().removeItem(path);
ocm.save();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
} finally {
ocm.logout();
}
}
@PreAuthorize("hasPermission(#boardId, 'BOARD', 'READ,WRITE,ADMIN')")
......@@ -154,8 +171,12 @@ public class FilterController {
public @ResponseBody Map<String,String> listFilters(@PathVariable String boardId) throws Exception {
Session session = ocmFactory.getOcm().getSession();
Map<String,String> result = listTools.list(String.format(URI.FILTER_URI, boardId,""), "name", session);
session.logout();
Map<String,String> result = null;
try {
result = listTools.list(String.format(URI.FILTER_URI, boardId,""), "name", session);
} finally {
session.logout();
}
return result;
}
......@@ -166,9 +187,13 @@ public class FilterController {
throws Exception {
ObjectContentManager ocm = ocmFactory.getOcm();
Collection<Card> cards = null;
Collection<Card> cards = listTools.query(boardId, null, filterId, ocm);
ocm.logout();
try {
cards = listTools.query(boardId, null, filterId, ocm);
} finally {
ocm.logout();
}
return cards;
}
......@@ -182,16 +207,18 @@ public class FilterController {
}
ObjectContentManager ocm = ocmFactory.getOcm();
Node node = ocm.getSession().getNode(String.format(URI.FILTER_URI, boardId, filterId));
try {
Node node = ocm.getSession().getNode(String.format(URI.FILTER_URI, boardId, filterId));
if(node==null){
if(node==null){
throw new ResourceNotFoundException();
}
node.remove();
ocm.save();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
} finally {
ocm.logout();
throw new ResourceNotFoundException();
}
node.remove();
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(BoardController.BOARD, boardId);
}
}
......@@ -76,14 +76,17 @@ public class ListController {
@RequestBody ListResource list ) throws Exception {
ObjectContentManager ocm = ocmFactory.getOcm();
try {
listTools.ensurePresence(String.format( URI.BOARD_URI, boardId), "lists", ocm.getSession());
String newId = IdentifierTools.getIdFromNamedModelClass(list);
list.setPath(String.format(URI.LIST_URI, boardId, newId.toString()));
ocm.insert(list);
ocm.save();
ocm.logout();
listTools.ensurePresence(String.format( URI.BOARD_URI, boardId), "lists", ocm.getSession());
String newId = IdentifierTools.getIdFromNamedModelClass(list);
list.setPath(String.format(URI.LIST_URI, boardId, newId.toString()));
ocm.insert(list);
ocm.save();
} finally {
ocm.logout();
}
return list;
}
......@@ -164,17 +167,18 @@ public class ListController {
}
ObjectContentManager ocm = ocmFactory.getOcm();
Node node = ocm.getSession().getNode(String.format(URI.LIST_URI, boardId, listId));
try {
Node node = ocm.getSession().getNode(String.format(URI.LIST_URI, boardId, listId));
if(node==null){
throw new ResourceNotFoundException();
}
if(node==null){
node.remove();
ocm.save();
this.cacheInvalidationManager.invalidate(LIST, listCache.getCacheId(boardId,listId));
} finally {
ocm.logout();
throw new ResourceNotFoundException();
}
node.remove();
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(LIST, listCache.getCacheId(boardId,listId));
}
}
......@@ -96,10 +96,8 @@ public class NotificationController {
if (null != notification) {
final String type = notification.getName();
if (StringUtils.isAlphanumeric(type)) {
ObjectContentManager ocm = null;
ObjectContentManager ocm = ocmFactory.getOcm();
try {
ocm = ocmFactory.getOcm();
if (!doesNotificationTypeExist(type, ocm)) {
notification.setPath(String.format(URI.NOTIFICATIONS_TYPE_URI, type));
ocm.insert(notification);
......@@ -109,14 +107,8 @@ public class NotificationController {
} else {
LOG.info("Failed to create new Notification Type:'" + type + "', type already exists.");
}
} catch (Exception e) {
LOG.error("Failed to store Notification Type:'" + notification + "' to JCR Repository.", e);
throw e;
} finally {
if (null != ocm) {
ocm.save();
ocm.logout();
}
ocm.logout();
}
} else {
LOG.error("Failed to store invalid Notification Type:'" + notification + "' to JCR Repository. Notification Type was not alphanumeric");
......@@ -128,29 +120,21 @@ public class NotificationController {
public @ResponseBody void createNotificationTypeMapping(@PathVariable String notificationType, @RequestBody NotificationTypeMapping notificationTypeMapping) throws Exception{
LOG.debug("Attempting to create notification type mapping :'" + notificationTypeMapping);
ObjectContentManager ocm = null;
try {
ocm = ocmFactory.getOcm();
ObjectContentManager ocm = ocmFactory.getOcm();
try {
if (!doesNotificationTypeExist(notificationType,ocm)) {
throw new IllegalArgumentException("notification type doesn't exists into jcr.");
}
final Session session = ocm.getSession();
listTools.ensurePresence(URI.NOTIFICATIONS_URI,notificationType, session);
final String path = String.format(URI.NOTIFICATIONS_TYPE_MAPPING_URI, notificationType);
notificationTypeMapping.setPath(path);
ocm.insert(notificationTypeMapping);
ocm.save();
}catch (Exception e) {
LOG.error("Failed to store NotificationType Mapping for Type:'"+notificationType+"' with mapping :' " + notificationTypeMapping.toString() +"to JCR Repository.", e);
throw e;
} finally {
if (null != ocm) {
ocm.logout();
if (!doesNotificationTypeExist(notificationType,ocm)) {
throw new IllegalArgumentException("notification type doesn't exists into jcr.");
}
final Session session = ocm.getSession();
listTools.ensurePresence(URI.NOTIFICATIONS_URI,notificationType, session);
final String path = String.format(URI.NOTIFICATIONS_TYPE_MAPPING_URI, notificationType);
notificationTypeMapping.setPath(path);
ocm.insert(notificationTypeMapping);
ocm.save();
} finally {
ocm.logout();
}
}
@RequestMapping(value = "/{notificationType}", method=RequestMethod.GET)
......@@ -160,9 +144,7 @@ public class NotificationController {
try{
notificationTypeMapping = (NotificationTypeMapping) ocm.getObject(NotificationTypeMapping.class,String.format(URI.NOTIFICATIONS_TYPE_MAPPING_URI, notificationType));
} finally {
if (null != ocm) {
ocm.logout();
}
ocm.logout();
}
return notificationTypeMapping;
}
......@@ -191,50 +173,40 @@ public class NotificationController {
public @ResponseBody void createNotification(@PathVariable String type,
@RequestBody Map<String,Object> context) throws Exception {
LOG.debug("Attempting to create new notification for type:'"+type+"' with context :'" + context.toString() );
ObjectContentManager ocm = null;
try {
ocm = ocmFactory.getOcm();
ObjectContentManager ocm = ocmFactory.getOcm();
try {
if (!doesNotificationTypeExist(type,ocm)) {
throw new IllegalArgumentException("notification type doesn't exists into jcr.");
}
final Session session = ocm.getSession();
final String path = String.format(URI.NOTIFICATIONS_TYPE_URI, type);
listTools.ensurePresence(URI.NOTIFICATIONS_URI, type, session);
/*
* Create cardNotification from Context and saves it to JCR.
*/
CardNotification cardNotification = new CardNotification();
Long notificationId = IdentifierTools.getIdFromRepository(session, path, "notifcationTypeUID");
cardNotification.setOccuredTime(new Date());
cardNotification.setPath(String.format(URI.NOTIFICATIONS_TYPE_ID_URI, type, notificationId.toString()));
cardNotification.setUser(listTools.getCurrentUser());
ocm.insert(cardNotification);
ocm.save();
if (!doesNotificationTypeExist(type,ocm)) {
throw new IllegalArgumentException("notification type doesn't exists into jcr.");
}
final Session session = ocm.getSession();
final String path = String.format(URI.NOTIFICATIONS_TYPE_URI, type);
/*
* Create Notification and Send onto to JMS Queue.
*/
Notification notification = new Notification();
notification.setName(type);
notification.setContext(context);
this.jmsTemplate.convertAndSend(notification);
LOG.debug("Successfully created new notification for type:'"+type+"' with cardNotification:'"+cardNotification+"'.");
listTools.ensurePresence(URI.NOTIFICATIONS_URI, type, session);
/*
* Create cardNotification from Context and saves it to JCR.
*/
CardNotification cardNotification = new CardNotification();
Long notificationId = IdentifierTools.getIdFromRepository(session, path, "notifcationTypeUID");
cardNotification.setOccuredTime(new Date());
cardNotification.setPath(String.format(URI.NOTIFICATIONS_TYPE_ID_URI, type, notificationId.toString()));
cardNotification.setUser(listTools.getCurrentUser());
ocm.insert(cardNotification);
ocm.save();
/*
* Create Notification and Send onto to JMS Queue.