diff options
Diffstat (limited to 'sandbox/dougsleite/guardian-model/src/main/java/org/apache/tuscany/sca/guardian/GuardianGroupImpl.java')
-rw-r--r-- | sandbox/dougsleite/guardian-model/src/main/java/org/apache/tuscany/sca/guardian/GuardianGroupImpl.java | 221 |
1 files changed, 212 insertions, 9 deletions
diff --git a/sandbox/dougsleite/guardian-model/src/main/java/org/apache/tuscany/sca/guardian/GuardianGroupImpl.java b/sandbox/dougsleite/guardian-model/src/main/java/org/apache/tuscany/sca/guardian/GuardianGroupImpl.java index a54ffe71b7..e21016b3f5 100644 --- a/sandbox/dougsleite/guardian-model/src/main/java/org/apache/tuscany/sca/guardian/GuardianGroupImpl.java +++ b/sandbox/dougsleite/guardian-model/src/main/java/org/apache/tuscany/sca/guardian/GuardianGroupImpl.java @@ -47,8 +47,7 @@ public class GuardianGroupImpl implements GuardianGroup { @Property(name = "recovery_rules", required = true) public void setRecoveryRules(String recoveryRules) { try { - //FileInputStream fileInputStream = new FileInputStream("src/main/resources/recoveryrules.xml"); - FileInputStream fileInputStream = new FileInputStream("src/main/resources/recoveryrules.xml"); + FileInputStream fileInputStream = new FileInputStream(recoveryRules); XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream); reader = new ResettableReader(xmlReader); } catch (XMLStreamException ex) { @@ -64,14 +63,15 @@ public class GuardianGroupImpl implements GuardianGroup { } public void enableContext(Context context) { - throw new UnsupportedOperationException("Not supported yet."); + System.out.println("Enable Context.. nothing to do!"); + //throw new UnsupportedOperationException("Not supported yet."); } public void removeContext() { throw new UnsupportedOperationException("Not supported yet."); } - public void gthrow(GlobalException ex, List<String> participantList) { + public void gthrow(GlobalExceptionInterface ex, List<String> participantList) { //1)Ivoked by a GuardianMember instance through the gthrow method //2)Notify all participants about the exception (FIFO atomic broadcast model) - it will cause the suspension of all participants @@ -190,7 +190,177 @@ public class GuardianGroupImpl implements GuardianGroup { } - private void applyRecoveryRules(GlobalException ex) { + private void applyRecoveryRules(GlobalExceptionInterface ex) { + reader.reset(); + ruleTag(ex); + } + + private void ruleTag(GlobalExceptionInterface ex) { + try { + while (reader.hasNext()) { + reader.next(); + //<rule name="" signaled_exception=""> + if (reader.isStartElement() && reader.getLocalName().equals("rule")) { + for (int i = 0; i < reader.getAttributeCount(); i++) { + //ex == signaled_exception + if (reader.getAttributeLocalName(i).equals("signaled_exception") && ex.getClass().getName().equals(reader.getAttributeValue(i))) { + participantExceptionTag(ex); + break; + } + } + } + } + } catch (XMLStreamException exc) { + Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private void participantExceptionTag(GlobalExceptionInterface ex) { + List<GuardianMember> gmList; + try { + while (reader.hasNext() && !(reader.isEndElement() && reader.getLocalName().equals("rule"))) { + reader.next(); + //<participant match="<REG_EXP> | SIGNALER"> + if (reader.isStartElement() && reader.getLocalName().equals("participant")) { + String participantMatch = reader.getAttributeValue(0).trim(); + + gmList = getMatchingParticipants(participantMatch, ex); + + if (!gmList.isEmpty()) { + throwExceptionTag(gmList, ex); + } + } + } + } catch (XMLStreamException exc) { + Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, exc); + } + } + + private void throwExceptionTag(List<GuardianMember> gmList, GlobalExceptionInterface ex) { + + String exceptionClassName; + String targetContextName; + Integer min_participant_joined; + Integer max_participant_joined; + + try { + + while (reader.hasNext() && !(reader.isEndElement() && reader.getLocalName().equals("participant"))) { + reader.next(); + + //<throw_exception class="<Exception>" target_context="<Context>"/> + if (reader.isStartElement() && reader.getLocalName().equals("throw_exception")) { + + exceptionClassName = null; + targetContextName = null; + min_participant_joined = null; + max_participant_joined = null; + for (int j = 0; j < reader.getAttributeCount(); j++) { + if (reader.getAttributeLocalName(j).equals("class")) { + //class value + exceptionClassName = reader.getAttributeValue(j); + } else if (reader.getAttributeLocalName(j).equals("target_context")) { + //target_context value + targetContextName = reader.getAttributeValue(j); + } else if (reader.getAttributeLocalName(j).equals("min_participant_joined")) { + //min_participant_joined value + min_participant_joined = Integer.parseInt(reader.getAttributeValue(j)); + } else { + //max_participant_joined value + max_participant_joined = Integer.parseInt(reader.getAttributeValue(j)); + } + } + + //Test the min and max joined participants condition + if (min_participant_joined != null && max_participant_joined != null) { + if (!(guardianList.size() >= min_participant_joined && guardianList.size() < max_participant_joined)) { + break; + } + } else if (min_participant_joined != null) { + if (!(guardianList.size() >= min_participant_joined)) { + break; + } + } else if (max_participant_joined != null) { + if (!(guardianList.size() >= min_participant_joined)) { + break; + } + } + + //<affected_participants> + String affectedParticipants = affectedParticipantsTag(); + int index = -1; + + //Verify if the parameter is an index + try { + index = Integer.parseInt(affectedParticipants); + } catch (NumberFormatException nexc) { + index = -1; + } + + Class exceptionClass = Class.forName(exceptionClassName); + Context targetContext; + if (targetContextName.toUpperCase().equals(Context.CURRENT_CONTEXT.getName().toUpperCase())) { + targetContext = Context.CURRENT_CONTEXT; + } else if (targetContextName.toUpperCase().equals(Context.INIT_CONTEXT.getName().toUpperCase())) { + targetContext = Context.INIT_CONTEXT; + } else { + targetContext = new Context(targetContextName); + } + GlobalException newException = (GlobalException) exceptionClass.newInstance(); + + newException.setTargetContext(targetContext); + newException.setSignalingContext(ex.getSignalingContext()); + newException.setSignalingParticipant(ex.getSignalingParticipant()); + + //Add the exception to the participants matched + if (index != -1) { + gmList.get(index).addException(newException); + } else if (affectedParticipants != null && affectedParticipants.length() != 0) { + if (affectedParticipants.toUpperCase().equals("FIRST")) { + gmList.get(0).addException(newException); + } else if (affectedParticipants.toUpperCase().equals("LAST")) { + gmList.get(gmList.size() - 1).addException(newException); + } else if (affectedParticipants.toUpperCase().equals("ALL")) { + for (GuardianMember gm : gmList) { + gm.addException(newException); + } + } + } else { + for (GuardianMember gm : gmList) { + gm.addException(newException); + } + } + } + } + } catch (XMLStreamException ex1) { + Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex1); + } catch (InstantiationException ex1) { + Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex1); + } catch (IllegalAccessException ex1) { + Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex1); + } catch (ClassNotFoundException ex1) { + Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex1); + } + } + + private String affectedParticipantsTag() { + String affectedParticipants = null; + try { + while (reader.hasNext() && !(reader.isEndElement() && reader.getLocalName().equals("throw_exception"))) { + reader.next(); + //<affected_participants> + if (reader.isStartElement() && reader.getLocalName().equals("affected_participants")) { + affectedParticipants = reader.getElementText(); + } + } + } catch (XMLStreamException ex) { + Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex); + } + + return affectedParticipants; + } + + private void applyRecoveryRulesOriginal(GlobalExceptionInterface ex) { String exceptionClassName; String targetContextName; @@ -265,6 +435,24 @@ public class GuardianGroupImpl implements GuardianGroup { //match value gmList = getMatchingParticipants(participant_match, ex); + String affectedParticipants = null; + int index = -1; + while (reader.hasNext() && !(reader.isEndElement() && reader.getLocalName().equals("throw_exception"))) { + reader.next(); + + //<affected_participants> + if (reader.isStartElement() && reader.getLocalName().equals("affected_participants")) { + affectedParticipants = reader.getElementText(); + + //Verify if the parameter is an index + try { + index = Integer.parseInt(affectedParticipants); + } catch (NumberFormatException nexc) { + index = -1; + } + } + } + Class exceptionClass = Class.forName(exceptionClassName); Context targetContext; @@ -280,8 +468,23 @@ public class GuardianGroupImpl implements GuardianGroup { newException.setTargetContext(targetContext); //Add the exception to the participants matched - for (GuardianMember gm : gmList) { - gm.addException(newException); + if (index != -1) { + gmList.get(index).addException(newException); + } else if (affectedParticipants != null && affectedParticipants.length() != 0) { + if (affectedParticipants.toUpperCase().equals("FIRST")) { + gmList.get(0).addException(newException); + + } else if (affectedParticipants.toUpperCase().equals("LAST")) { + gmList.get(gmList.size() - 1).addException(newException); + } else if (affectedParticipants.toUpperCase().equals("ALL")) { + for (GuardianMember gm : gmList) { + gm.addException(newException); + } + } + } else { + for (GuardianMember gm : gmList) { + gm.addException(newException); + } } } } @@ -299,7 +502,7 @@ public class GuardianGroupImpl implements GuardianGroup { } } - private List<GuardianMember> getMatchingParticipants(String regularExpression, GlobalException signaledException) { + private List<GuardianMember> getMatchingParticipants(String regularExpression, GlobalExceptionInterface signaledException) { List<GuardianMember> matchingParticipants = new LinkedList(); if (regularExpression.toUpperCase().equals("SIGNALER")) { @@ -401,7 +604,7 @@ public class GuardianGroupImpl implements GuardianGroup { return re.toString(); } - public boolean propagate(GlobalException ex) { + public boolean propagate(GlobalExceptionInterface ex) { throw new UnsupportedOperationException("Not supported yet."); } |