BrightSide Workbench Full Report + Source Code
ContextFactory.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.context;
19 
20 import java.io.File;
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;
29 import java.util.Map;
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;
46 
51 public class ContextFactory {
52 
54  private ContextFactory() {
55  }
56 
57  public static IContext getContext(IConstructor constructor) {
58  return getContext(constructor, null);
59  }
60 
61  public static IContext getContext(IConstructor constructor, String path) {
62  IContext impl = new DefaultContext(); //(IContext) getInstance("context", DefaultContext.class);
63  if(impl != null) {
64  impl.setConstructor(constructor, path);
65  }
66  return impl;
67  }
68 
69  public static IElement getContextElement(IContext context, Element node) {
70  return getContextElement(context, node.getAttributeValue("type"), node);
71  }
72 
73  public static IElement getContextElement(IContext context, String type, Element node) {
74  IElement impl = null;
75  Element element = null;
76  if(!"none".equals(type)) {
77  element = getConfNode("elements/element[@type='" + type + "']");
78  }
79  if(element != null) {
80  try {
81  String jClass = element.getAttributeValue("java-class");
82  Class cls = Class.forName(makeItTurro(jClass));
83  if(cls != null) {
84  Constructor cons = cls.getConstructor(new Class[] {});
85  if(cons != null) {
86  impl = (IElement)cons.newInstance(new Object[] {});
87  }
88  }
89  } catch(ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
90  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
91  }
92  }
93  if(impl instanceof AbstractElement) {
94  ((AbstractElement) impl).setConfiguration(node, element);
95  //impl.setContext(context);
96  }
97  return impl;
98  }
99 
100  public static IAction getAction(IConstructor constructor) {
101  IAction impl = null;
102  String type = constructor.getRequest().getServletPath().substring(9);
103  Element element = getConfNode("actions/action[@type='" + type + "']");
104  if(element != null) {
105  try {
106  String jClass = element.getAttributeValue("java-class");
107  Class cls = Class.forName(makeItTurro(jClass));
108  if(cls != null) {
109  Constructor cons = cls.getConstructor(new Class[] {});
110  if(cons != null) {
111  impl = (IAction)cons.newInstance(new Object[] {});
112  }
113  }
114  } catch(Exception ex) {
115  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
116  }
117  }
118  if(impl != null) {
119  impl.setConstructor(constructor);
120  }
121  return impl;
122  }
123 
124  public static Element getInclude(IConstructor constructor) {
125  String type = constructor.getRequest().getServletPath();
126  return getConfNode("includes/include[@type='" + type + "']");
127  }
128 
129  public static Element getRedirection(String path) {
130  return getConfNode("redirections/redirect[@request and starts-with('" + path + "',@request)]");
131  }
132 
133  public static Element getVirtual(String server) {
134  return getConfNode("virtuals/virtual[@server='" + server + "']");
135  }
136 
137  public static List getImplementationNamesByType(String type) {
138  List list = new ArrayList();
139  for(Element element : (List<Element>) getConfNodes("implementations")) {
140  if(element != null) {
141  try {
142  Iterator it = XPath.selectNodes(element, "impl[starts-with(@interface,'" + type + "_')]/@interface").iterator();
143  while(it.hasNext()) {
144  list.add(((Attribute)it.next()).getValue());
145  }
146  } catch(Exception ex) {
147  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
148  }
149  }
150  }
151  return list;
152  }
153 
154  public static Object getImplementation(IElement iel, String name) {
155  IConstructor xpcons = null;
156  Object impl = null;
157  if(iel != null) xpcons = iel.getConstructor();
158  if(xpcons != null) {
159  impl = getImplementation(xpcons, name);
160  if(impl != null) {
161  if(impl instanceof IImplementation) {
162  ((IImplementation)impl).setElement(iel);
163  }
164  }
165  }
166  return impl;
167  }
168 
169  public static Object getImplementation(IConstructor constructor, String name) {
170  Object impl = null;
171  Element element = getConfNode("implementations/impl[@interface='" + name + "']");
172  if(element != null) {
173  try {
174  String jClass = element.getAttributeValue("java-class");
175  Class cls = Class.forName(makeItTurro(jClass));
176  if(cls != null) {
177  Constructor cons = cls.getConstructor(new Class[] {});
178  if(cons != null) {
179  impl = cons.newInstance(new Object[] {});
180  }
181  }
182  } catch(Exception ex) {
183  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
184  }
185  }
186  if(impl != null) {
187  if(impl instanceof IImplementation) {
188  ((IImplementation)impl).setConstructor(constructor);
189  if(element != null) {
190  ((IImplementation)impl).setConfiguration((Element)element.clone());
191  }
192  }
193  }
194  return impl;
195  }
196 
197  public static Object getJavaClass(String jClass) {
198  Object impl = null;
199  try {
200  Class cls = Class.forName(makeItTurro(jClass));
201  if(cls != null) {
202  Constructor cons = cls.getConstructor(new Class[] {});
203  if(cons != null) {
204  impl = cons.newInstance(new Object[] {});
205  }
206  }
207  } catch(Exception ex) {
208  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
209  }
210  return impl;
211  }
212 
213  public static Object getInstance(String node, Class defaultClass) {
214  Constructor cons = impls.get(node);
215  if(cons == null) {
216  Class javaClass = null;
217  Element element = getConfNode(node);
218  if(element != null) {
219  try {
220  String jClass = element.getAttributeValue("java-class");
221  javaClass = Class.forName(makeItTurro(jClass));
222  } catch(Exception ex) {
223  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
224  }
225  }
226  if(javaClass == null) {
227  javaClass = defaultClass;
228  }
229  try {
230  impls.put(node, javaClass.getConstructor(new Class[] {}));
231  return Reflections.of(javaClass).instance();
232  } catch (Exception ex) {
233  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
234  }
235  } else {
236  try {
237  return cons.newInstance(new Object[] {} );
238  } catch (Exception ex) {
239  Logger.getLogger(ContextFactory.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
240  }
241  }
242  return null;
243  }
244 
245  public static void resetImplementationsStartingWith(String name) {
246  elements.entrySet().removeIf(e -> e.getKey().startsWith("implementations/impl[@interface='" + name));
247  }
248 
249  private static Map<String, Constructor> impls = new HashMap<String, Constructor>();
250  private static Map<String, Element> elements = new HashMap<String, Element>();
251 
252  private static String makeItTurro(String jClass) {
253  return jClass.replaceAll("org\\.ao\\.", "org.turro.");
254  }
255 
256  private static List getConfNodes(String node) {
257  File configs[] = new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf")).listFiles(new FilenameFilter() {
258  @Override
259  public boolean accept(File dir, String name) {
260  return name.startsWith("elephant") && name.endsWith(".xml");
261  }
262  });
263  List list = new ArrayList();
264  for(File conf : configs) {
265  Element el = getConfigurationNode(conf, node);
266  if(el != null) list.add(el);
267  }
268  return list;
269  }
270 
271  private static Element getConfNode(String node) {
272  Element el = elements.get(node);
273  if(el == null) {
274  File configs[] = new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf")).listFiles(new FilenameFilter() {
275  @Override
276  public boolean accept(File dir, String name) {
277  return name.startsWith("elephant") && name.endsWith(".xml");
278  }
279  });
280  for(File conf : configs) {
281  el = getConfigurationNode(conf, node);
282  if(el != null) {
283  elements.put(node, el);
284  break;
285  }
286  }
287  }
288  return el;
289  }
290 
291  private static Element getConfigurationNode(File confFile, String node) {
292  if(confFile.exists()) {
293  Element conf = null;
294  SAXBuilder builder = new SAXBuilder();
295  Document doc;
296  try {
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);
301  }
302  if(conf != null) {
303  try {
304  return (Element)XPath.selectSingleNode(conf, node);
305  } catch(Exception ex) {
306  return null;
307  }
308  }
309  }
310  return null;
311  }
312 
313 }
static IElement getContextElement(IContext context, String type, Element node)
static IContext getContext(IConstructor constructor, String path)
static Element getInclude(IConstructor constructor)
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 getInstance(String node, Class defaultClass)
static IContext getContext(IConstructor constructor)
void setConstructor(IConstructor constructor)
void setConstructor(IConstructor constructor, String path)
void setConfiguration(IConstructor constructor, ElContext context)