1 19 20 25 package org.netbeans.modules.j2ee.dd.impl.common; 26 27 import java.lang.reflect.*; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 31 import org.openide.util.NbBundle; 32 import org.netbeans.modules.schema2beans.BaseBean; 33 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean; 34 35 40 41 public class CommonDDAccess { 42 43 public static final String DOT = "."; 45 public static final String COMMON_API = "org.netbeans.modules.j2ee.dd.api.common."; 47 public static final String SERVLET_2_3 = "2_3"; public static final String SERVLET_2_4 = "2_4"; public static final String WEB_API = "org.netbeans.modules.j2ee.dd.api.web."; public static final String WEB_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.web.model_"; 52 public static final String APP_1_3 = "1_3"; public static final String APP_1_4 = "1_4"; public static final String APP_API = "org.netbeans.modules.j2ee.dd.api.application."; public static final String APP_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.application.model_"; 57 public static final String EJB_2_0 = "2_0"; public static final String EJB_2_1 = "2_1"; public static final String EJB_1_1 = "1_1"; public static final String EJB_API = "org.netbeans.modules.j2ee.dd.api.ejb."; public static final String EJB_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.ejb.model_"; 63 private static Set COMMON_BEANS = new HashSet (); 64 static { 65 COMMON_BEANS.add("Icon"); COMMON_BEANS.add("InitParam"); COMMON_BEANS.add("EnvEntry"); COMMON_BEANS.add("EjbRef"); COMMON_BEANS.add("EjbLocalRef"); COMMON_BEANS.add("ResourceRef"); COMMON_BEANS.add("ResourceEnvRef"); COMMON_BEANS.add("ServiceRef"); COMMON_BEANS.add("Handler"); COMMON_BEANS.add("PortComponentRef"); COMMON_BEANS.add("MessageDestination"); COMMON_BEANS.add("MessageDestinationRef"); COMMON_BEANS.add("SecurityRole"); COMMON_BEANS.add("SecurityRoleRef"); } 80 81 89 90 public static BaseBean newBean(CommonDDBean parent, String beanName, String pkgName) throws ClassNotFoundException { 91 beanName = getImplementationBeanName(parent, beanName, pkgName); 92 try { 93 Class beanClass = Class.forName( 94 pkgName 95 + DOT 96 + beanName); 97 return (BaseBean) beanClass.newInstance(); 98 99 } catch (Exception e) { 100 if (e instanceof ClassNotFoundException ) 101 throw (ClassNotFoundException )e; 102 else { 103 e.printStackTrace(); 105 throw new RuntimeException ( 106 NbBundle.getMessage(CommonDDAccess.class, 107 "MSG_COMMONDDACCESS_ERROR", "newBean", 108 ", package = " + pkgName + ", beanName = " + beanName, e+ ": " +e.getMessage())); 109 } 110 } 111 } 112 113 114 public static void addBean(CommonDDBean parent, CommonDDBean child, String beanName, String pkgName) { 115 beanName = getImplementationBeanName(parent, beanName, pkgName); 116 String apiPrefix = getAPIPrefix(beanName, pkgName); 117 try { 118 Class p = parent.getClass(); 119 Class ch = Class.forName(apiPrefix + beanName); Method setter=null; 121 try { 122 setter = p.getMethod("set" + beanName, new Class []{ch}); setter.invoke(parent, new Object []{child}); 124 } catch (NoSuchMethodException ex) { 125 } 126 if (setter==null) { 127 setter = p.getMethod("add" + getNameForMethod(parent, beanName), new Class []{ch}); setter.invoke(parent, new Object []{child}); 129 } 130 } catch (Exception e) { 131 e.printStackTrace(); 133 throw new RuntimeException ( 134 NbBundle.getMessage(CommonDDAccess.class, 135 "MSG_COMMONDDACCESS_ERROR", "addBean", 136 ", package = " + pkgName + ", beanName = " + beanName, e+ ": " +e.getMessage())); 137 } 138 } 139 140 141 144 private static String getImplementationBeanName (CommonDDBean parent, String beanName, String pkgName) { 145 if (pkgName.equals(WEB_PACKAGE_PREFIX + SERVLET_2_3)) { 146 if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; else return beanName; 148 } else if (beanName.equals("Session") || beanName.equals("Entity") || beanName.equals("MessageDriven")) { return beanName + "Bean"; } else 151 return beanName; 152 } 153 154 private static String getAPIPrefix(String beanName, String pkgName){ 155 if (COMMON_BEANS.contains(beanName)) 156 return COMMON_API; 157 if (pkgName.startsWith(EJB_PACKAGE_PREFIX)) 158 return EJB_API; 159 else if (pkgName.startsWith(WEB_PACKAGE_PREFIX)) 160 return WEB_API; 161 else if (pkgName.startsWith(APP_PACKAGE_PREFIX)) 162 return APP_API; 163 assert false : "Invalid package prefix:" + pkgName; 164 return ""; } 166 167 175 public static BaseBean findBeanByName(BaseBean parent, String beanProperty, String nameProperty, String value) { 176 Class c = parent.getClass(); 177 Method getter; 178 Object result; 179 try { 180 getter = c.getMethod("get" + getNameForMethod((CommonDDBean)parent,beanProperty), null); result = getter.invoke(parent, null); 182 if (result == null) { 183 return null; 184 } else if (result instanceof BaseBean) { 185 return null; 186 } else { 187 BaseBean[] beans = (BaseBean[]) result; 188 for (int i=0;i<beans.length;i++) { 189 Class c1 = beans[i].getClass(); 190 Method getter1; 191 Object result1; 192 getter1 = c1.getMethod("get" + nameProperty, null); result1 = getter1.invoke(beans[i], null); 194 if (result1 instanceof String ) { 195 if (value.equals((String )result1)) { 196 return beans[i]; 197 } 198 } 199 } 200 return null; 201 } 202 } catch (Exception e) { 203 e.printStackTrace(); 205 throw new RuntimeException ( 206 NbBundle.getMessage(CommonDDAccess.class, 207 "MSG_COMMONDDACCESS_ERROR", "getBeanByName", 208 "parent = " + parent + ", beanProperty = " + beanProperty 209 + ", nameProperty = " + nameProperty 210 + ", value = " + value, 211 e+ ": " +e.getMessage())); 212 } 213 } 214 215 218 private static String getNameForMethod (CommonDDBean parent, String beanName) { 219 220 if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; else if ("ServiceRefHandler".equals(beanName)) return "Handler"; else { 223 return beanName; 224 } 225 } 226 } 227 | Popular Tags |