| 1 | |
package org.webmacro.broker; |
| 2 | |
|
| 3 | |
import java.lang.reflect.Constructor; |
| 4 | |
import java.util.Map; |
| 5 | |
import java.util.concurrent.ConcurrentHashMap; |
| 6 | |
|
| 7 | |
import org.slf4j.Logger; |
| 8 | |
import org.slf4j.LoggerFactory; |
| 9 | |
import org.webmacro.Broker; |
| 10 | |
import org.webmacro.Context; |
| 11 | |
import org.webmacro.PropertyException; |
| 12 | |
import org.webmacro.util.Settings; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | 84 | public class DefaultContextAutoLoader implements ContextAutoLoader { |
| 21 | |
|
| 22 | 2 | static Logger _log = LoggerFactory.getLogger(DefaultContextAutoLoader.class); |
| 23 | |
private Broker _broker; |
| 24 | 12 | private Map _factories = new ConcurrentHashMap(); |
| 25 | |
|
| 26 | |
public void init(Broker b, String name) { |
| 27 | 12 | _broker = b; |
| 28 | 12 | loadFactories(name); |
| 29 | 12 | } |
| 30 | |
|
| 31 | |
public Object get(String name, Context context) throws PropertyException { |
| 32 | 1160 | Object o = _factories.get(name); |
| 33 | 1160 | if (o == null) |
| 34 | 0 | return null; |
| 35 | 1160 | ContextObjectFactory f = (ContextObjectFactory) o; |
| 36 | 1160 | return f.get(context); |
| 37 | |
} |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
private void loadFactories (String keyName) |
| 45 | |
{ |
| 46 | 12 | _broker.getSettings().processListSetting(keyName, new AutoContextSettingsHandler()); |
| 47 | 12 | } |
| 48 | |
|
| 49 | 12 | private class AutoContextSettingsHandler extends Settings.ListSettingHandler |
| 50 | |
{ |
| 51 | |
public void processSetting (String settingKey, String settingValue) |
| 52 | |
{ |
| 53 | |
try |
| 54 | |
{ |
| 55 | 72 | addAutoVariable(settingKey, settingValue); |
| 56 | |
} |
| 57 | 0 | catch (Exception e) |
| 58 | |
{ |
| 59 | 0 | _log.error("Automatic variable (" + settingValue + ") failed to load", e); |
| 60 | 72 | } |
| 61 | 72 | } |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
private void addAutoVariable(String toolName, String className) { |
| 81 | |
Class c; |
| 82 | |
try |
| 83 | |
{ |
| 84 | 72 | c = _broker.classForName(className); |
| 85 | |
} |
| 86 | 0 | catch (ClassNotFoundException e) |
| 87 | |
{ |
| 88 | 0 | _log.warn("Context: Could not locate class for context tool " + className); |
| 89 | 0 | return; |
| 90 | 72 | } |
| 91 | 72 | if (toolName == null || toolName.equals("")) |
| 92 | |
{ |
| 93 | 0 | toolName = className; |
| 94 | 0 | int start = 0; |
| 95 | 0 | int end = toolName.length(); |
| 96 | 0 | int lastDot = toolName.lastIndexOf('.'); |
| 97 | 0 | if (lastDot != -1) |
| 98 | 0 | start = lastDot + 1; |
| 99 | 0 | toolName = toolName.substring(start, end); |
| 100 | |
} |
| 101 | |
|
| 102 | 72 | Constructor ctor = null; |
| 103 | 72 | Constructor[] ctors = c.getConstructors(); |
| 104 | 72 | Class[] parmTypes = null; |
| 105 | 72 | Object instance = null; |
| 106 | |
|
| 107 | |
|
| 108 | 162 | for (int i = 0; i < ctors.length; i++) |
| 109 | |
{ |
| 110 | 90 | parmTypes = ctors[i].getParameterTypes(); |
| 111 | 90 | if (parmTypes.length == 1 && parmTypes[0].equals(String.class)) |
| 112 | |
{ |
| 113 | 0 | ctor = ctors[i]; |
| 114 | 0 | Object[] args = {toolName}; |
| 115 | |
try |
| 116 | |
{ |
| 117 | 0 | instance = ctor.newInstance(args); |
| 118 | |
} |
| 119 | 0 | catch (Exception e) |
| 120 | |
{ |
| 121 | 0 | _log.error("Failed to instantiate tool " |
| 122 | |
+ toolName + " of class " + className + " using constructor " |
| 123 | |
+ ctor.toString(), e); |
| 124 | 0 | } |
| 125 | |
} |
| 126 | |
} |
| 127 | 72 | if (instance == null) |
| 128 | |
{ |
| 129 | |
|
| 130 | |
try |
| 131 | |
{ |
| 132 | 72 | instance = c.newInstance(); |
| 133 | |
} |
| 134 | 0 | catch (Exception e) |
| 135 | |
{ |
| 136 | 0 | _log.error("Unable to construct tool " + toolName + " of class " + className, e); |
| 137 | 0 | return; |
| 138 | 72 | } |
| 139 | |
} |
| 140 | 72 | if (instance instanceof ContextObjectFactory) { |
| 141 | 72 | _factories.put(toolName, instance); |
| 142 | 72 | _broker.registerAutoContextVariable(toolName, this); |
| 143 | 72 | _log.info("Registered automatic variable factory " + toolName); |
| 144 | |
} |
| 145 | |
else { |
| 146 | 0 | _log.warn("Context object " + toolName + " is not of type ContextObjectFactory"); |
| 147 | |
} |
| 148 | 72 | } |
| 149 | |
} |