1 package org.weda.workflow.impl;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Stack;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8 import java.util.regex.PatternSyntaxException;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.weda.workflow.Entry;
12 import org.weda.workflow.EntryContainer;
13 import org.weda.workflow.EntryException;
14 import org.weda.workflow.Workflow;
15
16 /**
17 *
18 * @author tim
19 */
20 public abstract class AbstractEntry implements Entry {
21 protected final static Log entryLog = LogFactory.getLog(AbstractEntry.class);
22
23 private String name;
24 private Pattern pattern;
25 private boolean exactMatch;
26 private Workflow workflow;
27 private boolean defaultEntry = false;
28 private EntryContainer container;
29 protected String logShift;
30 private ThreadLocal<List<String>> groupValuesStore =
31 new ThreadLocal<List<String>>(){
32 protected synchronized List<String> initialValue(){
33 return new ArrayList<String>();
34 }
35 };
36
37 public void init() throws EntryException {
38 StringBuffer buf=new StringBuffer();
39 EntryContainer con=getContainer();
40 while (con!=null){
41 buf.append("--");
42 con=con.getContainer();
43 }
44 logShift = buf.toString();
45 if (!isDefaultEntry()){
46 try{
47 if ( name.length()>=2
48 && name.startsWith("/")
49 && name.endsWith("/"))
50 {
51
52 String regexp = name.substring(1, name.length()-1);
53 pattern = Pattern.compile(regexp);
54 exactMatch=false;
55 }else
56 exactMatch=true;
57 }catch(PatternSyntaxException e){
58 throw new EntryException(
59 String.format(
60 "Can't initialize workflow entry (type: %s, name %s)"
61 , this.getClass().getName(), name)
62 , e);
63 }
64 }else{
65 exactMatch=true;
66 }
67 }
68
69 public boolean matches(String expression) {
70 List<String> groupValues=groupValuesStore.get();
71 groupValues.clear();
72 if (defaultEntry){
73 groupValues.add(expression);
74 return true;
75 }else if (exactMatch){
76 if (name.equals(expression)){
77 groupValues.add(expression);
78 return true;
79 }
80 return false;
81 }else{
82 Matcher matcher = pattern.matcher(expression);
83 boolean res = matcher.matches();
84 if (res){
85
86 for (int i=0; i<matcher.groupCount()+1; ++i)
87 groupValues.add(matcher.group(i));
88 }
89 return res;
90 }
91 }
92
93 public List<String> getGroupValues() throws EntryException {
94 return groupValuesStore.get();
95 }
96
97 public boolean isExactMatch() {
98 return exactMatch;
99 }
100
101 public String getName() {
102 return name;
103 }
104
105 public void setName(String name) {
106 if (name==null)
107 throw new IllegalArgumentException("Parameter name can't be null");
108 if (!isDefaultEntry())
109 this.name = name;
110 }
111
112 public Workflow getWorkflow(){
113 return workflow;
114 }
115
116 public void setWorkflow(Workflow workflow) {
117 this.workflow = workflow;
118 }
119
120 public boolean isDefaultEntry() {
121 return defaultEntry;
122 }
123
124 public EntryContainer getContainer() {
125 return container;
126 }
127
128 public void setContainer(EntryContainer container) {
129 this.container = container;
130 }
131
132 public void setDefaultEntry(boolean defaultEntry) {
133 if (defaultEntry)
134 setName(DEFAULT_ENTRY_NAME);
135 this.defaultEntry = defaultEntry;
136 }
137
138
139 public boolean operate(Stack<String> expressions) throws EntryException {
140 logOperate(expressions.size()>0?expressions.peek():null);
141 return true;
142 }
143
144 protected void logOperate(String expression){
145 if (entryLog.isDebugEnabled())
146 entryLog.debug(
147 logShift +
148 String.format(
149 "Found final entry (class: %s, name: %s) "
150 , this.getClass().getName(), getName())
151 );
152 }
153 }