KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > core > CoefficientInterceptor


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.core;
21
22 import za.org.coefficient.interfaces.*;
23
24 /**
25  *
26  * @version $Revision: 1.4 $ $Date: 2004/09/28 15:17:03 $
27  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
28  */

29 public abstract class CoefficientInterceptor {
30     //~ Static fields/initializers =============================================
31

32     /** Continue processing the chain */
33     public static final int INVOKE_PROCESS_CHILD = 0x100;
34
35     /** Stop processing at this point, but not in error */
36     public static final int INVOKE_PROCESS_DONE = 0x101;
37
38     /** Don't process this interceptor any further */
39     public static final int INVOKE_ABORT_LOCAL = 0x200;
40
41     /** Stop processing at this point - an error occurred */
42     public static final int INVOKE_ABORT_GLOBAL = 0x201;
43
44     /** Skip your sibling but run its sibling */
45     public static final int INVOKE_SKIP_NEXT = 0x202;
46
47     /** The constant that represents the prefix for aborted interceptors */
48     public static final String JavaDoc ABORT_INTERCEPTOR_PREFIX =
49         "__abort_interceptor_";
50
51     //~ Instance fields ========================================================
52

53     /** The sibling of this interceptor */
54     private CoefficientInterceptor sibling;
55
56     //~ Methods ================================================================
57

58     public void setSibling(CoefficientInterceptor val) {
59         this.sibling = val;
60     }
61
62     //
63
// Accessor & mutator methods
64
//
65
public CoefficientInterceptor getSibling() {
66         return sibling;
67     }
68
69     public void append(CoefficientInterceptor val) {
70         last()
71             .setSibling(val);
72     }
73
74     public int invoke(CoefficientContext ctx) throws Exception JavaDoc {
75
76         // Do the work of the invoke
77
int result = abortNamedInterceptor(ctx);
78         if (result == INVOKE_PROCESS_CHILD) {
79             result = handleInvoke(ctx);
80         }
81         boolean doPostInvoke = true;
82         
83         // And do the next sibling, as necessary
84
switch (result) {
85         case INVOKE_ABORT_LOCAL:
86             doPostInvoke = false;
87
88         case INVOKE_PROCESS_CHILD:
89             if (sibling != null) {
90                 result = sibling.invoke(ctx);
91             } else {
92                 result = INVOKE_PROCESS_DONE;
93             }
94
95             break;
96
97         case INVOKE_ABORT_GLOBAL:
98
99         case INVOKE_PROCESS_DONE:
100             doPostInvoke = false;
101         }
102
103         if (doPostInvoke && (result != INVOKE_ABORT_GLOBAL)) {
104             result = handlePostInvoke(ctx);
105         }
106
107         return result;
108     }
109
110     public CoefficientInterceptor last() {
111         CoefficientInterceptor result = this;
112         if (sibling != null) {
113             do {
114                 result = result.sibling;
115             } while (result.sibling != null);
116         }
117
118         return result;
119     }
120
121     protected int abortNamedInterceptor(CoefficientContext ctx) {
122         return ctx.getParameterAsInt(this.getClass().getName(),
123             INVOKE_PROCESS_CHILD);
124     }
125
126     protected int handleInvoke(CoefficientContext ctx) throws Exception JavaDoc {
127         return INVOKE_PROCESS_CHILD;
128     }
129
130     protected int handlePostInvoke(CoefficientContext ctx) throws Exception JavaDoc {
131         return INVOKE_PROCESS_CHILD;
132     }
133 }
134
Popular Tags