18 package org.turro.elephant.impl.context;
21 import java.io.FilenameFilter;
22 import java.io.IOException;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import org.jdom.Attribute;
33 import org.jdom.Document;
34 import org.jdom.Element;
35 import org.jdom.JDOMException;
36 import org.jdom.input.SAXBuilder;
37 import org.jdom.xpath.XPath;
38 import org.turro.elephant.context.ElephantContext;
39 import org.turro.elephant.context.IAction;
40 import org.turro.elephant.context.IConstructor;
41 import org.turro.elephant.context.IContext;
42 import org.turro.elephant.context.IElement;
43 import org.turro.elephant.context.IImplementation;
44 import org.turro.elephant.impl.abstracts.AbstractElement;
45 import org.turro.reflection.Reflections;
75 Element element =
null;
76 if(!
"none".equals(type)) {
77 element = getConfNode(
"elements/element[@type='" + type +
"']");
81 String jClass = element.getAttributeValue(
"java-class");
82 Class cls = Class.forName(makeItTurro(jClass));
84 Constructor cons = cls.getConstructor(
new Class[] {});
86 impl = (
IElement)cons.newInstance(
new Object[] {});
89 }
catch(ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
102 String type = constructor.
getRequest().getServletPath().substring(9);
103 Element element = getConfNode(
"actions/action[@type='" + type +
"']");
104 if(element !=
null) {
106 String jClass = element.getAttributeValue(
"java-class");
107 Class cls = Class.forName(makeItTurro(jClass));
109 Constructor cons = cls.getConstructor(
new Class[] {});
111 impl = (
IAction)cons.newInstance(
new Object[] {});
114 }
catch(Exception ex) {
125 String type = constructor.
getRequest().getServletPath();
126 return getConfNode(
"includes/include[@type='" + type +
"']");
130 return getConfNode(
"redirections/redirect[@request and starts-with('" + path +
"',@request)]");
134 return getConfNode(
"virtuals/virtual[@server='" + server +
"']");
138 List list =
new ArrayList();
139 for(Element element : (List<Element>) getConfNodes(
"implementations")) {
140 if(element !=
null) {
142 Iterator it = XPath.selectNodes(element,
"impl[starts-with(@interface,'" + type +
"_')]/@interface").iterator();
143 while(it.hasNext()) {
144 list.add(((Attribute)it.next()).getValue());
146 }
catch(Exception ex) {
159 impl = getImplementation(xpcons, name);
171 Element element = getConfNode(
"implementations/impl[@interface='" + name +
"']");
172 if(element !=
null) {
174 String jClass = element.getAttributeValue(
"java-class");
175 Class cls = Class.forName(makeItTurro(jClass));
177 Constructor cons = cls.getConstructor(
new Class[] {});
179 impl = cons.newInstance(
new Object[] {});
182 }
catch(Exception ex) {
189 if(element !=
null) {
200 Class cls = Class.forName(makeItTurro(jClass));
202 Constructor cons = cls.getConstructor(
new Class[] {});
204 impl = cons.newInstance(
new Object[] {});
207 }
catch(Exception ex) {
213 public static Object
getInstance(String node, Class defaultClass) {
214 Constructor cons = impls.get(node);
216 Class javaClass =
null;
217 Element element = getConfNode(node);
218 if(element !=
null) {
220 String jClass = element.getAttributeValue(
"java-class");
221 javaClass = Class.forName(makeItTurro(jClass));
222 }
catch(Exception ex) {
226 if(javaClass ==
null) {
227 javaClass = defaultClass;
230 impls.put(node, javaClass.getConstructor(
new Class[] {}));
231 return Reflections.of(javaClass).instance();
232 }
catch (Exception ex) {
237 return cons.newInstance(
new Object[] {} );
238 }
catch (Exception ex) {
246 elements.entrySet().removeIf(e -> e.getKey().startsWith(
"implementations/impl[@interface='" + name));
249 private static Map<String, Constructor> impls =
new HashMap<String, Constructor>();
250 private static Map<String, Element> elements =
new HashMap<String, Element>();
252 private static String makeItTurro(String jClass) {
253 return jClass.replaceAll(
"org\\.ao\\.",
"org.turro.");
256 private static List getConfNodes(String node) {
259 public boolean accept(File dir, String name) {
260 return name.startsWith(
"elephant") && name.endsWith(
".xml");
263 List list =
new ArrayList();
264 for(File conf : configs) {
265 Element el = getConfigurationNode(conf, node);
266 if(el !=
null) list.add(el);
271 private static Element getConfNode(String node) {
272 Element el = elements.get(node);
274 File configs[] =
new File(ElephantContext.getRealPath(
"/WEB-INF/elephant/conf")).listFiles(
new FilenameFilter() {
276 public boolean accept(File dir, String name) {
277 return name.startsWith(
"elephant") && name.endsWith(
".xml");
280 for(File conf : configs) {
281 el = getConfigurationNode(conf, node);
283 elements.put(node, el);
291 private static Element getConfigurationNode(File confFile, String node) {
292 if(confFile.exists()) {
294 SAXBuilder builder =
new SAXBuilder();
297 doc = builder.build(confFile);
298 conf = doc.getRootElement();
299 }
catch (IOException | JDOMException ex) {
300 Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
304 return (Element)XPath.selectSingleNode(conf, node);
305 }
catch(Exception ex) {
static String getRealPath(String path)
static String logMsg(String msg)
static IElement getContextElement(IContext context, String type, Element node)
static IContext getContext(IConstructor constructor, String path)
static Element getVirtual(String server)
static Element getInclude(IConstructor constructor)
static Element getRedirection(String path)
static IAction getAction(IConstructor constructor)
static Object getImplementation(IConstructor constructor, String name)
static IElement getContextElement(IContext context, Element node)
static Object getImplementation(IElement iel, String name)
static void resetImplementationsStartingWith(String name)
static Object getJavaClass(String jClass)
static Object getInstance(String node, Class defaultClass)
static List getImplementationNamesByType(String type)
static IContext getContext(IConstructor constructor)
void setConstructor(IConstructor constructor)
HttpServletRequest getRequest()
void setConstructor(IConstructor constructor, String path)
IConstructor getConstructor()
void setConfiguration(IConstructor constructor, ElContext context)