1 16 17 package org.apache.velocity.tools.view; 18 19 import org.apache.velocity.tools.view.tools.ViewTool; 20 import org.apache.velocity.app.Velocity; 21 22 32 public class ViewToolInfo implements ToolInfo 33 { 34 35 private String key; 36 private Class clazz; 37 private boolean initializable = false; 38 39 40 public ViewToolInfo() {} 41 42 43 55 protected Class getApplicationClass(String name) throws ClassNotFoundException 56 { 57 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 58 if (loader == null) 59 { 60 loader = ViewToolInfo.class.getClassLoader(); 61 } 62 return loader.loadClass(name); 63 } 64 65 66 67 68 public void setKey(String key) 69 { 70 this.key = key; 71 } 72 73 74 80 public void setClassname(String classname) throws Exception 81 { 82 this.clazz = getApplicationClass(classname); 83 84 if (clazz.newInstance() instanceof ViewTool) 85 { 86 this.initializable = true; 87 } 88 } 89 90 91 92 93 public String getKey() 94 { 95 return key; 96 } 97 98 99 public String getClassname() 100 { 101 return clazz.getName(); 102 } 103 104 105 110 public Object getInstance(Object initData) 111 { 112 Object tool = null; 113 try 114 { 115 tool = clazz.newInstance(); 116 } 117 121 catch (IllegalAccessException e) 122 { 123 Velocity.error("Exception while instantiating instance of \"" + 124 getClassname() + "\": " + e); 125 } 126 catch (InstantiationException e) 127 { 128 Velocity.error("Exception while instantiating instance of \"" + 129 getClassname() + "\": " + e); 130 } 131 132 if (initializable) { 133 ((ViewTool)tool).init(initData); 134 } 135 return tool; 136 } 137 138 } 139 | Popular Tags |