summaryrefslogtreecommitdiffstats
path: root/sandbox/dougsleite/implementation-guardian/src/main/java/org/apache/tuscany/sca/implementation/guardian/impl/GuardianGroupImpl.java
blob: 9094fc248d587730b7935367757a2a468de350e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
 * 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.
 */
package org.apache.tuscany.sca.implementation.guardian.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.tuscany.sca.contribution.service.ContributionReadException;
import org.apache.tuscany.sca.implementation.guardian.common.InvalidRegularExpressionException;
import org.apache.tuscany.sca.implementation.guardian.GuardianGroup;
import org.apache.tuscany.sca.implementation.guardian.GuardianMember;
import org.apache.tuscany.sca.implementation.guardian.common.ConcurrentExceptionOcurrenceException;
import org.apache.tuscany.sca.implementation.guardian.common.Constants;
import org.apache.tuscany.sca.implementation.guardian.common.Context;
import org.apache.tuscany.sca.implementation.guardian.common.GlobalException;
import org.apache.tuscany.sca.implementation.guardian.common.GlobalExceptionInterface;
import org.apache.tuscany.sca.implementation.guardian.common.InvalidNodeException;
import org.apache.tuscany.sca.implementation.guardian.common.ResolutionTreeUtils;
import org.apache.tuscany.sca.implementation.guardian.common.SuspendException;
import org.apache.tuscany.sca.implementation.guardian.xml.RecoveryRulesProcessor;
import org.apache.tuscany.sca.implementation.guardian.xml.ResolutionTreesProcessor;

public class GuardianGroupImpl implements GuardianGroup {

    private List<GuardianMember> guardianList;
    private InnerGuardianGroupThread innerThread;
    private List<GlobalExceptionInterface> concurrentExList;
    private Map<String, OMElement> resolutionTreeElements;
    private ResolutionTreeUtils resolutionTreeUtils;
    private Map<String, OMElement> ruleElements;

    public GuardianGroupImpl(String recoveryRules, String resolutionTrees) {
        guardianList = new LinkedList<GuardianMember>();
        concurrentExList = new LinkedList<GlobalExceptionInterface>();
        innerThread = new InnerGuardianGroupThread();
        resolutionTreeUtils = new ResolutionTreeUtils();

        setRecoveryRules(recoveryRules);
        setResolutionTree(resolutionTrees);
    }

    private void setRecoveryRules(String recoveryRules) {
        try {
            FileInputStream fileInputStream = new FileInputStream(recoveryRules);
            XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream);

            RecoveryRulesProcessor processor = new RecoveryRulesProcessor();
            ruleElements = processor.read(xmlReader).getRuleElements();

        } catch (ContributionReadException ex) {
            Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (XMLStreamException ex) {
            Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setResolutionTree(String resolutionTree) {

        try {
            FileInputStream fileInputStream = new FileInputStream(resolutionTree);
            XMLStreamReader resolutionTreeReader = XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream);

            ResolutionTreesProcessor processor = new ResolutionTreesProcessor();
            resolutionTreeElements = processor.read(resolutionTreeReader).getResolutionTreeElements();

        } catch (ContributionReadException ex) {
            Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (XMLStreamException ex) {
            Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addGuardianMember(GuardianMember guardianMember) {
        guardianList.add(guardianMember);
        //guardianMember.setUniqueParticipantID(guardianList.size() - 1);
    }

    public boolean removeGuardianMember(GuardianMember guardianMember) {
        return guardianList.remove(guardianMember);
    }

    public void enableContext(Context context) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Context removeContext() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public synchronized void gthrow(GlobalExceptionInterface ex, List<String> participantList) {

        concurrentExList.add(ex);

        //Sends a message representing the exception to the other guardian members
        SuspendException suspendEx = new SuspendException();
        suspendEx.putSignalingParticipant(ex.getSignalingParticipants().get(0));

        if (participantList == null) {
            for (GuardianMember g : guardianList) {
                g.gthrow(suspendEx, null);
            }
        } else {
            for (GuardianMember g : guardianList) {
                if (participantList.contains(g.getParticipantIdentifier()) ||
                        g.getParticipantIdentifier().equals(ex.getSignalingParticipants().get(0))) {
                    g.gthrow(suspendEx, null);
                }
            }
        }

        if (!innerThread.isRunning()) {
            innerThread.setGlobalException(ex);
            new Thread(innerThread).start();
        }
       
    }

    public boolean propagate(GlobalExceptionInterface ex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void checkExceptionStatus() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private class InnerGuardianGroupThread implements Runnable {

        private boolean isRunning;
        private GlobalExceptionInterface ex = null;

        public InnerGuardianGroupThread() {
            isRunning = false;
        }

        public void setGlobalException(GlobalExceptionInterface ex) {
            this.ex = ex;
        }

        public GlobalExceptionInterface getGlobalException() {
            return ex;
        }

        public void run() {
            isRunning = true;

            if (ex != null) {

                //Time window of 10 seconds just for tests
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ex1) {
                    Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex1);
                }

                try {
                    applyRecoveryRules(ex);
                } catch (ConcurrentExceptionOcurrenceException ce) {
                    applyConcurrentRecoveryRules();
                }

                //Clear the concurrent exception list
                concurrentExList.clear();

                //Set up the participant state to NORMAL
                for (GuardianMember gm : guardianList) {
                    if (gm.getParticipantState() == SUSPENDED_PARTICIPANT_STATE) {
                        gm.setParticipantState(NORMAL_PARTICIPANT_STATE);
                    }
                }
            }
            isRunning = false;
        }

        public boolean isRunning() {
            return isRunning;
        }

        private void applyRecoveryRules(GlobalExceptionInterface ex) throws ConcurrentExceptionOcurrenceException {
            ruleTag(ex, ruleElements.values().iterator());
        }

        private void ruleTag(GlobalExceptionInterface ex, Iterator<OMElement> ruleElements) throws ConcurrentExceptionOcurrenceException {
            String signaledException;
            String exceptionName;

            OMElement rule;
            while (ruleElements.hasNext()) {

                rule = ruleElements.next();
                signaledException = getAttributeValue(rule, Constants.SIGNALED_EXCEPTION);
                exceptionName = ex.getClass().getName();

                if (signaledException.equals(exceptionName)) {
                    participantExceptionTag(ex, rule.getChildElements());
                    break;
                }
            }
        }

        private void participantExceptionTag(GlobalExceptionInterface ex, Iterator<OMElement> participantElements) throws ConcurrentExceptionOcurrenceException {
            String matchParticipant;
            List<GuardianMember> gmList;

            OMElement participant;
            while (participantElements.hasNext()) {
                participant = participantElements.next();

                matchParticipant = getAttributeValue(participant, Constants.MATCH);
                gmList = getMatchingParticipants(matchParticipant, ex);

                if (!gmList.isEmpty()) {
                    throwExceptionTag(gmList, ex, participant.getChildElements());
                }
            }
        }

        private void throwExceptionTag(List<GuardianMember> gmList, GlobalExceptionInterface ex, Iterator<OMElement> throwExceptionElements) throws ConcurrentExceptionOcurrenceException {
            String className;
            String targetContextName;
            Integer minParticipantJoined;
            Integer maxParticipantJoined;

            OMElement throwException;
            while (throwExceptionElements.hasNext()) {

                throwException = throwExceptionElements.next();

                className = getAttributeValue(throwException, Constants.CLASS);
                targetContextName = getAttributeValue(throwException, Constants.TARGET_CONTEXT);

                try {
                    minParticipantJoined = Integer.parseInt(getAttributeValue(throwException, Constants.MIN_PARTICIPANT_JOINED));
                } catch (NumberFormatException nex) {
                    minParticipantJoined = null;
                }

                try {
                    maxParticipantJoined = Integer.parseInt(getAttributeValue(throwException, Constants.MAX_PARTICIPANT_JOINED));
                } catch (NumberFormatException nexc) {
                    maxParticipantJoined = null;
                }

                //Test the min and max joined participants condition
                if (minParticipantJoined != null && maxParticipantJoined != null) {
                    if (!(guardianList.size() >= minParticipantJoined && guardianList.size() < maxParticipantJoined)) {
                        break;
                    }
                } else if (minParticipantJoined != null) {
                    if (!(guardianList.size() >= minParticipantJoined)) {
                        break;
                    }
                } else if (minParticipantJoined != null) {
                    if (!(guardianList.size() >= minParticipantJoined)) {
                        break;
                    }
                }

                //<affected_participants>
                String affectedParticipants = affectedParticipantsTag(throwException.getChildElements());
                int index = -1;

                //Verify if the parameter is an index
                try {
                    index = Integer.parseInt(affectedParticipants);
                } catch (NumberFormatException nexc) {
                    index = -1;
                }

                //Create the new exception instance
                Class exceptionClass;
                try {
                    exceptionClass = Class.forName(className);

                    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.putSignalingParticipant(ex.getSignalingParticipants().toString());

                    //Check concurrent exception existence
                    if (concurrentExList.size() > 1) {
                        throw new ConcurrentExceptionOcurrenceException(concurrentExList.toString());
                    }

                    //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(Constants.FIRST)) {
                            gmList.get(0).addException(newException);
                        } else if (affectedParticipants.toUpperCase().equals(Constants.LAST)) {
                            gmList.get(gmList.size() - 1).addException(newException);
                        } else if (affectedParticipants.toUpperCase().equals(Constants.ALL)) {
                            for (GuardianMember gm : gmList) {
                                gm.addException(newException);
                            }
                        }
                    } else {
                        for (GuardianMember gm : gmList) {
                            gm.addException(newException);
                        }
                    }

                } 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(Iterator<OMElement> affectedParticipantElements) {

            String affectedParticipantValue = null;

            OMElement affectedParticipant;
            while (affectedParticipantElements.hasNext()) {

                affectedParticipant = affectedParticipantElements.next();
                affectedParticipantValue = affectedParticipant.getText();
            }

            if (affectedParticipantValue != null && affectedParticipantValue.length() == 0) {
                affectedParticipantValue = null;
            }

            return affectedParticipantValue;
        }

        private String getAttributeValue(OMElement element, String attributeName) {
            OMAttribute at;
            Iterator it = element.getAllAttributes();

            while (it.hasNext()) {
                at = (OMAttribute) it.next();
                if (at.getLocalName().equals(attributeName)) {
                    return at.getAttributeValue();
                }
            }

            return null;
        }

        private void applyConcurrentRecoveryRules() {

            boolean concurrentExOcurrence = false;
            List<GlobalExceptionInterface> copyConcurrentExList;
            GlobalExceptionInterface resolvedEx;

            do {
                System.out.println("Concurrent exceptions: " + concurrentExList);

                copyConcurrentExList = new LinkedList(concurrentExList);

                resolvedEx = checkExceptionResolutionTrees(copyConcurrentExList, resolutionTreeElements.values().iterator());

                concurrentExList.clear();

                System.out.println("Resolved Exception: " + resolvedEx);
                if (resolvedEx != null) {
                    System.out.println("List of participants: " + resolvedEx.getSignalingParticipants());
                }

                try {
                    //Process the exception list sequentially
                    if (resolvedEx == null) {
                        for (GlobalExceptionInterface ex : copyConcurrentExList) {
                            applyRecoveryRules(ex);
                        }
                    } else {
                        applyRecoveryRules(resolvedEx);
                    }

                } catch (ConcurrentExceptionOcurrenceException exc) {
                    concurrentExOcurrence = true;
                    break;
                }
            } while (concurrentExOcurrence);
        }

        //FIXME: Need to check the exception level
        private GlobalExceptionInterface checkExceptionResolutionTrees(List<GlobalExceptionInterface> exceptionList, Iterator resolutionTreesElements) {

            OMElement tree;
            OMElement root;
            String exceptionLevel = null;
            GlobalExceptionInterface resolvedEx = null;

            while (resolutionTreesElements.hasNext()) {
                tree = (OMElement) resolutionTreesElements.next();
                exceptionLevel = tree.getAttributeValue(Constants.EXCEPTION_LEVEL_QNAME);

                root = (OMElement) tree.getChildElements().next();
                resolvedEx = checkExceptionResolutionTree(exceptionList, root);

                if (resolvedEx != null) {
                    break;
                }
            }

            return resolvedEx;
        }

        //Search for the root of the smallest subtree that contains all the concurrently signaled exceptions. If not found, return null.
        private GlobalExceptionInterface checkExceptionResolutionTree(List<GlobalExceptionInterface> exceptionList, OMElement rootTree) {

            resolutionTreeUtils.setRoot(rootTree);
            String ex1, ex2;
            GlobalExceptionInterface resolvedEx = null;

            ex1 = exceptionList.get(0).getClass().getName();
            for (int i = 1; i < exceptionList.size(); i++) {
                ex2 = exceptionList.get(i).getClass().getName();

                try {
                    ex1 = resolutionTreeUtils.getLowestCommonAncestor(ex1, ex2);
                } catch (InvalidNodeException invalidNodeException) {
                    ex1 = null;
                    break;
                }
            }

            if (ex1 != null) {
                Class exceptionClass;

                try {
                    exceptionClass = Class.forName(ex1);
                    resolvedEx = (GlobalException) exceptionClass.newInstance();

                    for (GlobalExceptionInterface ex : exceptionList) {
                        resolvedEx.putSignalingParticipant(ex.getSignalingParticipants().get(0));
                    }

                } catch (InstantiationException ex) {
                    Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(GuardianGroupImpl.class.getName()).log(Level.SEVERE, null, ex);
                }

                return resolvedEx;

            } else {
                return null;
            }
        }

        private List<GuardianMember> getMatchingParticipants(String regularExpression, GlobalExceptionInterface signaledException) {
            List<GuardianMember> matchingParticipants = new LinkedList();
            String[] splitByComma = regularExpression.split(",");

            //Invalid regular expression
            if (splitByComma.length > 2) {
                throw new InvalidRegularExpressionException("The comma ',' operator can only be applied for two expressions");
            }

            //There is no comma on the regular expression
            if (splitByComma.length == 1) {

                if (regularExpression.toUpperCase().equals("SIGNALER")) {
                    for (GuardianMember gm : guardianList) {
                        if (signaledException.getSignalingParticipants().contains(gm.getParticipantIdentifier())) {
                            matchingParticipants.add(gm);
                        }
                    }
                } else if (regularExpression.toUpperCase().equals("!SIGNALER")) {
                    for (GuardianMember gm : guardianList) {
                        if (!signaledException.getSignalingParticipants().contains(gm.getParticipantIdentifier())) {
                            matchingParticipants.add(gm);
                        }
                    }

                } else {
                    //Create an java regular expression
                    String re = createJavaRegularExpression(regularExpression);

                    for (GuardianMember gm : guardianList) {
                        if (gm.getParticipantIdentifier().matches(re)) {
                            matchingParticipants.add(gm);
                        }
                    }
                }

            } //There is comma on the regular expression
            else {

                String element;
                int index = -1;
                for (int i = 0; i < splitByComma.length; i++) {
                    element = splitByComma[i].toUpperCase();
                    if (element.equals("SIGNALER") || element.equals("!SIGNALER")) {
                        if (index == -1) {
                            index = i;
                        } else {
                            index = -1;
                        }
                    }
                }

                //Invalid expression
                if (index == -1) {
                    throw new InvalidRegularExpressionException("The comma ',' requires a SIGNALER or !SIGNALER element in one side of the expression");
                }

                String re = createJavaRegularExpression(splitByComma[1 - index]);

                for (GuardianMember gm : guardianList) {

                    //Test if the participant matches with the regular expression
                    if (gm.getParticipantIdentifier().matches(re)) {

                        //Test if the participant is Signaler
                        if (splitByComma[index].toUpperCase().equals("SIGNALER")) {

                            if (signaledException.getSignalingParticipants().contains(gm.getParticipantIdentifier())) {
                                matchingParticipants.add(gm);
                            }
                        } //Test if the participant is not Signaler
                        else {
                            if (!signaledException.getSignalingParticipants().contains(gm.getParticipantIdentifier())) {
                                matchingParticipants.add(gm);
                            }
                        }
                    }
                }

            }

            return matchingParticipants;
        }

        /* Valid expressions: *, <Context>.*, <Context>, *.<Context>, *.<Context>.*,
         *                    *.<Context>.*.<Context>.*, <REG_EXP> || <REG_EXP>
         *
         * Invalid expressions: *.*, **,
         *
         * Not supported yet: !<Context>, !<Context> || <Context>, !(<Context> || <Context>)
         */
        private String createJavaRegularExpression(String regularExpression) throws InvalidRegularExpressionException {
            StringBuffer re = new StringBuffer();

            String[] splitByBar = regularExpression.split("\\|\\|");
            String[] splitByPeriod;

            for (int i = 0; i < splitByBar.length; i++) {

                splitByPeriod = splitByBar[i].split("\\.");

                if (i > 0) {
                    re.append("|");
                }

                re.append("^");
                for (int j = 0; j < splitByPeriod.length; j++) {

                    //*
                    if (splitByPeriod[j].equals("*")) {

                        //Validate the regular expression
                        if (j + 1 != splitByPeriod.length && splitByPeriod[j + 1].equals("*")) {
                            throw new InvalidRegularExpressionException();
                        }

                        //*
                        if (splitByPeriod.length == 1) {
                            re.append("(\\w+)");
                        } //*.<CONTEXT>

                        if (j == 0 && splitByPeriod.length != 1) {
                            re.append("(\\w+\\");
                            re.append(".)*");
                        } //<CONTEXT>.*
                        else {
                            re.append("(\\");
                            re.append(".\\w+)*");
                        }
                    } //<CONTEXT>
                    else {

//                    //Validate the regular expression
//                    if (splitedByPeriod[j].matches("^(\\*)*$")) {
//                        throw new RuntimeException("Invalid name for a context");
//                    }

                        //<CONTEXT> || <CONTEXT>.<CONTEXT>.<CONTEXT> || *.<CONTEXT>
                        if (splitByPeriod.length == 1) {
                            re.append("(\\w+\\");
                            re.append(".)*");
                        }

                        if (j == 0 || j - 1 == 0) {
                            re.append("(" + splitByPeriod[j] + ")");
                        } else {
                            re.append("(\\." + splitByPeriod[j] + ")");
                        }
                    }
                }
                re.append("$");
            }
            return re.toString();
        }
    }
}