From: <ke...@us...> - 2002-12-07 06:15:55
|
Update of /cvsroot/webmacro/webmacro/src/org/webmacro In directory sc8-pr-cvs1:/tmp/cvs-serv24763 Modified Files: Context.java Log Message: Reworked addTool method to avoid throwing exceptions and generating spurious log messages when instantiating tools. Index: Context.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/Context.java,v retrieving revision 1.56 retrieving revision 1.57 diff -C2 -d -r1.56 -r1.57 *** Context.java 10 Nov 2002 20:51:12 -0000 1.56 --- Context.java 7 Dec 2002 06:15:52 -0000 1.57 *************** *** 567,571 **** } - /** * Attempts to instantiate the tool using three different constructors --- 567,570 ---- *************** *** 589,594 **** try { c = _broker.classForName(className); ! } ! catch (ClassNotFoundException e) { _log.warning("Context: Could not locate class for context tool " + className); --- 588,592 ---- try { c = _broker.classForName(className); ! } catch (ClassNotFoundException e) { _log.warning("Context: Could not locate class for context tool " + className); *************** *** 609,656 **** } Object instance = null; - StringBuffer log = null; - try { - Constructor ctor = c.getConstructor(_ctorArgs1); - Object[] args = new Object[2]; - args[0] = key; - args[1] = new SubSettings(_broker.getSettings(), key); - instance = ctor.newInstance(args); - } - catch (Exception e) { - log = new StringBuffer(); - log.append("Error loading component key="); - log.append(key); - log.append(" class="); - log.append(c.toString()); - log.append("\n"); - log.append("Trying 2-argument constructor: "); - log.append(e.toString()); - log.append("\n"); - } ! if (instance == null) { ! try { ! Constructor ctor = c.getConstructor(_ctorArgs2); ! Object[] args = new Object[1]; ! args[0] = key; ! instance = ctor.newInstance(args); } ! catch (Exception e) { ! log.append("Trying 1-argument constructor: "); ! log.append(e.toString()); ! log.append("\n"); } } - if (instance == null) { try { instance = c.newInstance(); ! } ! catch (Exception e) { ! log.append("Trying 0-argument constructor: "); ! log.append(e.toString()); ! log.append("\n"); ! _log.warning(log.toString()); return; } --- 607,660 ---- } + Constructor ctor = null; + Constructor[] ctors = c.getConstructors(); + Class[] parmTypes = null; Object instance = null; ! // check for 2 arg constructor ! for (int i=0; i<ctors.length; i++){ ! parmTypes = ctors[i].getParameterTypes(); ! if (parmTypes.length == 2 ! && parmTypes[0].equals(_ctorArgs1[0]) ! && parmTypes[1].equals(_ctorArgs1[1])){ ! ctor = ctors[i]; ! Object[] args = new Object[2]; ! args[0] = key; ! args[1] = new SubSettings(_broker.getSettings(), key); ! try { ! instance = ctor.newInstance(args); ! } ! catch (Exception e){ ! _log.error("Failed to instantiate tool " ! + key + " of class " + className + " using constructor " ! + ctor.toString(), e); ! } } ! } ! if (instance == null){ ! // check for 1 arg constructor ! for (int i=0; i<ctors.length; i++){ ! parmTypes = ctors[i].getParameterTypes(); ! if (parmTypes.length == 1 && parmTypes[0].equals(_ctorArgs1[0])){ ! ctor = ctors[i]; ! Object[] args = new Object[1]; ! args[0] = key; ! try { ! instance = ctor.newInstance(args); ! } ! catch (Exception e){ ! _log.error("Failed to instantiate tool " ! + key + " of class " + className + " using constructor " ! + ctor.toString(), e); ! } ! } } } if (instance == null) { + // try no-arg constructor try { instance = c.newInstance(); ! } catch (Exception e) { ! _log.error("Unable to construct tool " + key + " of class " + className, e); return; } *************** *** 659,665 **** _log.info("Registered ContextTool " + key); } ! ! ! ////////////////////////////////////////////////////////////// --- 663,667 ---- _log.info("Registered ContextTool " + key); } ! ////////////////////////////////////////////////////////////// |