1 19 20 25 package org.netbeans.modules.j2ee.dd.impl.webservices; 26 27 import java.lang.reflect.*; 28 import org.openide.util.NbBundle; 29 import org.netbeans.modules.schema2beans.BaseBean; 30 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean; 31 32 37 38 public class CommonDDAccess { 39 40 public static final String WEBSERVICES_1_1 = "1_1"; 42 public static final String PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.webservices.model_"; public static final String DOT = "."; 45 53 54 public static BaseBean newBean(CommonDDBean parent, String beanName, String version) throws ClassNotFoundException { 55 beanName = getImplementationBeanName(parent, beanName, version); 56 try { 57 Class beanClass = Class.forName( 58 PACKAGE_PREFIX 59 + version + DOT 60 + beanName); 61 return (BaseBean) beanClass.newInstance(); 62 63 } catch (Exception e) { 64 if (e instanceof ClassNotFoundException ) 65 throw (ClassNotFoundException )e; 66 else { 67 e.printStackTrace(); 69 throw new RuntimeException ( 70 NbBundle.getMessage(CommonDDAccess.class, 71 "MSG_COMMONDDACCESS_ERROR", "newBean", 72 ", version = " + version + ", beanName = " + beanName, e+ ": " +e.getMessage())); 73 } 74 } 75 } 76 77 public static void addBean(CommonDDBean parent, CommonDDBean child, String beanName, String version) { 78 beanName = getImplementationBeanName(parent, beanName, version); 79 try { 80 Class p = parent.getClass(); 81 Class ch = Class.forName("org.netbeans.modules.j2ee.dd.api.webservices."+beanName); Method setter=null; 83 try { 84 setter = p.getMethod("set" + beanName, new Class []{ch}); setter.invoke(parent, new Object []{child}); 86 } catch (NoSuchMethodException ex) { 87 } 88 if (setter==null) { 89 setter = p.getMethod("add" + getNameForMethod(parent, beanName), new Class []{ch}); setter.invoke(parent, new Object []{child}); 91 } 92 } catch (Exception e) { 93 e.printStackTrace(); 95 throw new RuntimeException ( 96 NbBundle.getMessage(CommonDDAccess.class, 97 "MSG_COMMONDDACCESS_ERROR", "addBean", 98 ", version = " + version + ", beanName = " + beanName, e+ ": " +e.getMessage())); 99 } 100 } 101 102 110 public static BaseBean findBeanByName(BaseBean parent, String beanProperty, String nameProperty, String value) { 111 Class c = parent.getClass(); 112 Method getter; 113 Object result; 114 try { 115 getter = c.getMethod("get" + getNameForMethod((CommonDDBean)parent,beanProperty), null); result = getter.invoke(parent, null); 117 if (result == null) { 118 return null; 119 } else if (result instanceof BaseBean) { 120 return null; 121 } else { 122 BaseBean[] beans = (BaseBean[]) result; 123 for (int i=0;i<beans.length;i++) { 124 Class c1 = beans[i].getClass(); 125 Method getter1; 126 Object result1; 127 getter1 = c1.getMethod("get" + nameProperty, null); result1 = getter1.invoke(beans[i], null); 129 if (result1 instanceof String ) { 130 if (value.equals((String )result1)) { 131 return beans[i]; 132 } 133 } 134 } 135 return null; 136 } 137 } catch (Exception e) { 138 e.printStackTrace(); 140 throw new RuntimeException ( 141 NbBundle.getMessage(CommonDDAccess.class, 142 "MSG_COMMONDDACCESS_ERROR", "getBeanByName", 143 "parent = " + parent + ", beanProperty = " + beanProperty 144 + ", nameProperty = " + nameProperty 145 + ", value = " + value, 146 e+ ": " +e.getMessage())); 147 } 148 } 149 150 153 private static String getImplementationBeanName (CommonDDBean parent, String beanName, String version) { 154 if ("Webservices".equals(beanName)) { 155 return beanName; 156 } else { 157 return beanName + "Type"; } 159 } 160 161 164 private static String getNameForMethod (CommonDDBean parent, String beanName) { 165 if ("Webservices".equals(beanName) || 166 "WebserviceDescription".equals(beanName) || 167 "PortComponent".equals(beanName)) { 168 return beanName; 169 } else { 170 return beanName + "Type"; } 172 } 173 } 174 | Popular Tags |