1 16 package org.jahia.pipelines.impl; 17 18 import java.util.ArrayList ; 19 20 import org.jahia.pipelines.Pipeline; 21 import org.jahia.pipelines.PipelineDescriptor; 22 import org.jahia.pipelines.PipelineException; 23 import org.jahia.pipelines.valves.Valve; 24 import org.jahia.pipelines.valves.ValveContext; 25 import org.jahia.pipelines.valves.ValveDescriptor; 26 27 34 public class GenericPipeline implements Pipeline, ValveContext { 35 36 37 protected String name; 38 39 private Valve[] valves = new Valve[0]; 40 41 49 protected ThreadLocal state = new ThreadLocal (); 50 51 54 protected PipelineDescriptor descriptor; 55 56 public GenericPipeline () { 57 58 } 59 60 63 public void setDescriptor (PipelineDescriptor descriptor) { 64 this.descriptor = descriptor; 65 } 66 67 70 public PipelineDescriptor getDescriptor () { 71 return descriptor; 72 } 73 74 77 public void initialize () 78 throws PipelineException { 79 setName(getDescriptor().getName()); 80 ArrayList valveDescriptors = (ArrayList ) getDescriptor(). 81 getValveDescriptors(); 82 83 for (int i = 0; i < valveDescriptors.size(); i++) { 84 ValveDescriptor vDescriptor = (ValveDescriptor) valveDescriptors. 85 get(i); 86 String className = vDescriptor.getClassName(); 87 88 Valve valve; 89 90 try { 91 valve = (Valve) Class.forName(className).newInstance(); 92 } catch (Exception e) { 93 throw new PipelineException("Failed to create valve: " + 94 className, e); 95 } 96 97 addValve(valve); 98 } 99 100 103 for (int i = 0; i < valves.length; i++) { 105 valves[i].initialize(); 107 } 108 } 109 110 115 public void setName (String name) { 116 this.name = name; 117 } 118 119 124 public String getName () { 125 return name; 126 } 127 128 public Valve[] getValves () { 129 synchronized (valves) { 130 Valve[] results = new Valve[valves.length]; 131 System.arraycopy(valves, 0, results, 0, valves.length); 132 return results; 133 } 134 } 135 136 public void addValve (Valve valve) { 137 synchronized (valves) { 139 Valve[] results = new Valve[valves.length + 1]; 140 System.arraycopy(valves, 0, results, 0, valves.length); 141 results[valves.length] = valve; 142 valves = results; 143 } 144 } 145 146 public void removeValve (Valve valve) { 147 synchronized (valves) { 148 int index = -1; 150 for (int i = 0; i < valves.length; i++) { 151 if (valve == valves[i]) { 152 index = i; 153 break; 154 } 155 } 156 if (index < 0) { 157 return; 158 } 159 160 Valve[] results = new Valve[valves.length - 1]; 162 int n = 0; 163 for (int i = 0; i < valves.length; i++) { 164 if (i == index) { 165 continue; 166 } 167 results[n++] = valves[i]; 168 } 169 valves = results; 170 } 171 } 172 173 public void invoke (Object context) 174 throws PipelineException { 175 state.set(new Integer (0)); 177 178 invokeNext(context); 180 } 181 182 public void invokeNext (Object context) 183 throws PipelineException { 184 Integer current = (Integer ) state.get(); 186 int valvePos = current.intValue(); 187 188 if (valvePos < valves.length) { 189 state.set(new Integer (valvePos + 1)); 192 valves[valvePos].invoke(context, this); 193 } 194 195 } 196 197 201 public boolean hasValveOfClass(Class c) { 202 return getFirstValveOfClass(c) != null; 203 } 204 205 208 public Valve getFirstValveOfClass(Class c) { 209 for (int i = 0; i < this.valves.length; i++) { 210 if (c.isInstance(valves[i])) { 211 return valves[i]; 212 } 213 } 214 return null; 215 } 216 218 219 } 220 | Popular Tags |