1 19 20 25 package org.netbeans.modules.web.dd.impl.common; 26 27 import java.lang.reflect.*; 28 import org.openide.util.NbBundle; 29 import org.netbeans.modules.schema2beans.BaseBean; 30 import org.netbeans.api.web.dd.WebApp; 31 import org.netbeans.api.web.dd.ServiceRef; 32 import org.netbeans.api.web.dd.common.CommonDDBean; 33 34 39 40 public class CommonDDAccess { 41 42 public static final String SERVLET_2_3 = "2_3"; public static final String SERVLET_2_4 = "2_4"; 45 public static final String PACKAGE_PREFIX = "org.netbeans.modules.web.dd.impl.model_"; public static final String DOT = "."; 48 56 57 public static BaseBean newBean(CommonDDBean parent, String beanName, String version) throws ClassNotFoundException { 58 beanName = getImplementationBeanName(parent, beanName, version); 59 try { 60 Class beanClass = Class.forName( 61 PACKAGE_PREFIX 62 + version + DOT 63 + beanName); 64 return (BaseBean) beanClass.newInstance(); 65 66 } catch (Exception e) { 67 if (e instanceof ClassNotFoundException ) 68 throw (ClassNotFoundException )e; 69 else { 70 e.printStackTrace(); 72 throw new RuntimeException ( 73 NbBundle.getMessage(CommonDDAccess.class, 74 "MSG_COMMONDDACCESS_ERROR", "newBean", 75 ", version = " + version + ", beanName = " + beanName, e+ ": " +e.getMessage())); 76 } 77 } 78 } 79 80 public static void addBean(CommonDDBean parent, CommonDDBean child, String beanName, String version) { 81 beanName = getImplementationBeanName(parent, beanName, version); 82 try { 83 Class p = parent.getClass(); 84 Class ch = Class.forName("org.netbeans.api.web.dd."+beanName); Method setter=null; 86 try { 87 setter = p.getMethod("set" + beanName, new Class []{ch}); setter.invoke(parent, new Object []{child}); 89 } catch (NoSuchMethodException ex) { 90 } 91 if (setter==null) { 92 setter = p.getMethod("add" + getNameForMethod(parent, beanName), new Class []{ch}); setter.invoke(parent, new Object []{child}); 94 } 95 } catch (Exception e) { 96 e.printStackTrace(); 98 throw new RuntimeException ( 99 NbBundle.getMessage(CommonDDAccess.class, 100 "MSG_COMMONDDACCESS_ERROR", "addBean", 101 ", version = " + version + ", beanName = " + beanName, e+ ": " +e.getMessage())); 102 } 103 } 104 105 113 public static BaseBean findBeanByName(BaseBean parent, String beanProperty, String nameProperty, String value) { 114 Class c = parent.getClass(); 115 Method getter; 116 Object result; 117 try { 118 getter = c.getMethod("get" + getNameForMethod((CommonDDBean)parent,beanProperty), null); result = getter.invoke(parent, null); 120 if (result == null) { 121 return null; 122 } else if (result instanceof BaseBean) { 123 return null; 124 } else { 125 BaseBean[] beans = (BaseBean[]) result; 126 for (int i=0;i<beans.length;i++) { 127 Class c1 = beans[i].getClass(); 128 Method getter1; 129 Object result1; 130 getter1 = c1.getMethod("get" + nameProperty, null); result1 = getter1.invoke(beans[i], null); 132 if (result1 instanceof String ) { 133 if (value.equals((String )result1)) { 134 return beans[i]; 135 } 136 } 137 } 138 return null; 139 } 140 } catch (Exception e) { 141 e.printStackTrace(); 143 throw new RuntimeException ( 144 NbBundle.getMessage(CommonDDAccess.class, 145 "MSG_COMMONDDACCESS_ERROR", "getBeanByName", 146 "parent = " + parent + ", beanProperty = " + beanProperty 147 + ", nameProperty = " + nameProperty 148 + ", value = " + value, 149 e+ ": " +e.getMessage())); 150 } 151 } 152 153 156 private static String getImplementationBeanName (CommonDDBean parent, String beanName, String version) { 157 158 if (version.equals(SERVLET_2_3)) { 159 if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; else if ("Handler".equals(beanName) && parent instanceof ServiceRef) return "ServiceRefHandler"; else return beanName; 162 } else { 163 return beanName; 164 } 165 } 166 167 170 private static String getNameForMethod (CommonDDBean parent, String beanName) { 171 172 if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; else if ("ServiceRefHandler".equals(beanName)) return "Handler"; else { 175 return beanName; 176 } 177 } 178 } 179 | Popular Tags |