| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.webmacro.engine; |
| 25 | |
|
| 26 | |
import java.lang.reflect.Constructor; |
| 27 | |
import java.lang.reflect.InvocationTargetException; |
| 28 | |
import java.util.Hashtable; |
| 29 | |
|
| 30 | |
import org.slf4j.Logger; |
| 31 | |
import org.slf4j.LoggerFactory; |
| 32 | |
|
| 33 | |
import org.webmacro.Broker; |
| 34 | |
import org.webmacro.InitException; |
| 35 | |
import org.webmacro.NotFoundException; |
| 36 | |
import org.webmacro.Provider; |
| 37 | |
import org.webmacro.util.Instantiator; |
| 38 | |
import org.webmacro.util.Settings; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 6 | public final class ParserProvider implements Provider |
| 44 | |
{ |
| 45 | 2 | static Logger _log = LoggerFactory.getLogger(Instantiator.class); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 6 | private final Hashtable _parsers = new Hashtable(); |
| 50 | |
|
| 51 | 6 | private Broker _broker = null; |
| 52 | 6 | private final Class[] _brokerParam = {Broker.class}; |
| 53 | 6 | private final Object[] _brokerArg = new Object[1]; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public final void register (String pClassName, String pType) |
| 61 | |
throws IntrospectionException, InitException |
| 62 | |
{ |
| 63 | |
Class pclass; |
| 64 | 6 | String pname = extractName(pClassName); |
| 65 | 6 | String name = (pType != null && !pType.equals("")) |
| 66 | |
? pType : pname; |
| 67 | |
try |
| 68 | |
{ |
| 69 | 6 | pclass = _broker.classForName(pClassName); |
| 70 | |
} |
| 71 | 0 | catch (Exception e) |
| 72 | |
{ |
| 73 | 0 | throw new IntrospectionException("No class " + pClassName); |
| 74 | 6 | } |
| 75 | |
try |
| 76 | |
{ |
| 77 | 6 | _log.info("Registering parser: " + name + " (" + pClassName + ")"); |
| 78 | 6 | Parser p = (Parser) _parsers.get(name); |
| 79 | 6 | if (p == null) |
| 80 | |
{ |
| 81 | 6 | Constructor ctor = pclass.getConstructor(_brokerParam); |
| 82 | 6 | p = (Parser) ctor.newInstance(_brokerArg); |
| 83 | 6 | _parsers.put(name, p); |
| 84 | 6 | } |
| 85 | 0 | else if (!pclass.equals(p.getClass())) |
| 86 | |
{ |
| 87 | 0 | throw new InitException( |
| 88 | |
"Attempt to register parser " + pClassName |
| 89 | |
+ " failed because " + p.getClass() |
| 90 | |
+ " is already registered for type " + name); |
| 91 | |
} |
| 92 | |
} |
| 93 | 0 | catch (InstantiationException ne) |
| 94 | |
{ |
| 95 | 0 | throw new IntrospectionException("Parsers could not be instantiated", |
| 96 | |
ne); |
| 97 | |
} |
| 98 | 0 | catch (IllegalAccessException ia) |
| 99 | |
{ |
| 100 | 0 | throw new IntrospectionException("Parser class must be public", ia); |
| 101 | |
} |
| 102 | 0 | catch (InvocationTargetException it) |
| 103 | |
{ |
| 104 | 0 | throw new InitException("Parser threw an exception", it); |
| 105 | |
} |
| 106 | 0 | catch (NoSuchMethodException nm) |
| 107 | |
{ |
| 108 | 0 | throw new IntrospectionException( |
| 109 | |
"Parser missing the required constructor", nm); |
| 110 | 6 | } |
| 111 | 6 | } |
| 112 | |
|
| 113 | |
public final Parser getParser (String pname) |
| 114 | |
throws NotFoundException |
| 115 | |
{ |
| 116 | 228 | Parser p = (Parser) _parsers.get(pname); |
| 117 | 228 | if (p == null) |
| 118 | |
{ |
| 119 | 0 | throw new NotFoundException("No parser registered for type " |
| 120 | |
+ pname); |
| 121 | |
} |
| 122 | 228 | return p; |
| 123 | |
} |
| 124 | |
|
| 125 | |
private static String extractName (String par) |
| 126 | |
throws IntrospectionException |
| 127 | |
{ |
| 128 | 6 | if (!par.endsWith("Parser")) |
| 129 | |
{ |
| 130 | 0 | throw new IntrospectionException( |
| 131 | |
"Malformed classname (" + par + "), must end with Parser"); |
| 132 | |
} |
| 133 | 6 | int end = par.length() - 6; |
| 134 | 6 | int start = par.lastIndexOf('.', end) + 1; |
| 135 | 6 | String parName = par.substring(start, end); |
| 136 | 6 | if (par.startsWith("org.webmacro.")) |
| 137 | |
{ |
| 138 | 6 | parName = parName.toLowerCase(); |
| 139 | |
} |
| 140 | 6 | return parName; |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
public String getType () |
| 145 | |
{ |
| 146 | 6 | return "parser"; |
| 147 | |
} |
| 148 | |
|
| 149 | 12 | private class SettingHandler extends Settings.ListSettingHandler |
| 150 | |
{ |
| 151 | |
|
| 152 | |
public void processSetting (String settingKey, String settingValue) |
| 153 | |
{ |
| 154 | |
try |
| 155 | |
{ |
| 156 | 6 | register(settingValue, settingKey); |
| 157 | |
} |
| 158 | 0 | catch (Exception ce) |
| 159 | |
{ |
| 160 | 0 | _log.error("Could not load parser: " + settingValue, ce); |
| 161 | 6 | } |
| 162 | 6 | } |
| 163 | |
} |
| 164 | |
|
| 165 | |
public void init (Broker broker, Settings p) throws InitException |
| 166 | |
{ |
| 167 | 6 | _brokerArg[0] = broker; |
| 168 | 6 | _broker = broker; |
| 169 | |
|
| 170 | |
try |
| 171 | |
{ |
| 172 | 6 | p.processListSetting("Parsers", new SettingHandler()); |
| 173 | |
} |
| 174 | 0 | catch (Exception e) |
| 175 | |
{ |
| 176 | 0 | throw new InitException("Could not init ParserProvider", e); |
| 177 | 6 | } |
| 178 | 6 | } |
| 179 | |
|
| 180 | |
public void destroy () |
| 181 | |
{ |
| 182 | 0 | _parsers.clear(); |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
public Object get (String name) throws NotFoundException |
| 186 | |
{ |
| 187 | |
try |
| 188 | |
{ |
| 189 | 228 | return getParser(name); |
| 190 | |
} |
| 191 | 0 | catch (Exception e) |
| 192 | |
{ |
| 193 | 0 | throw new NotFoundException("No such parser: " + name, e); |
| 194 | |
} |
| 195 | |
} |
| 196 | |
|
| 197 | |
public void flush () |
| 198 | |
{ |
| 199 | 0 | } |
| 200 | |
} |
| 201 | |
|
| 202 | |
|