1 19 20 package org.netbeans.modules.j2ee.refactoring; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import org.netbeans.jmi.javamodel.Feature; 25 import org.netbeans.jmi.javamodel.Resource; 26 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 27 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject; 28 import org.netbeans.modules.j2ee.refactoring.safedelete.EntitySafeDeleteRefactoring; 29 import org.netbeans.modules.j2ee.refactoring.safedelete.JaxWsXmlSafeDeleteRafactoring; 30 import org.netbeans.modules.j2ee.refactoring.safedelete.PersistenceXmlSafeDeleteRefactoring; 31 import org.netbeans.modules.j2ee.refactoring.safedelete.SunJaxWsXmlSafeDeleteRefactoring; 32 import org.netbeans.modules.j2ee.refactoring.safedelete.TldClassSafeDeleteRefactoring; 33 import org.netbeans.modules.j2ee.refactoring.safedelete.WebXmlSafeDeleteRefactoring; 34 import org.netbeans.modules.j2ee.refactoring.safedelete.WebservicesXmlSafeDeleteRefactoring; 35 import org.netbeans.modules.web.taglib.model.Taglib; 36 import org.netbeans.modules.j2ee.refactoring.safedelete.EjbMethodSafeDeleteRefactoring; 37 import org.netbeans.modules.j2ee.refactoring.safedelete.EjbClassSafeDeleteRefactoring; 38 import org.netbeans.modules.j2ee.refactoring.safedelete.EjbCmpFieldSafeDeleteRefactoring; 39 import org.netbeans.modules.j2ee.refactoring.safedelete.BaseRefactoring; 40 import org.netbeans.modules.j2ee.refactoring.safedelete.EjbQueryMethodSafeDeleteRefactoring; 41 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 42 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb; 43 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 44 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField; 45 import org.netbeans.modules.j2ee.dd.api.ejb.Query; 46 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 47 import org.netbeans.modules.refactoring.api.Problem; 48 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; 49 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 50 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 51 import org.netbeans.jmi.javamodel.Element; 52 import org.netbeans.jmi.javamodel.Method; 53 import org.netbeans.jmi.javamodel.JavaClass; 54 import org.netbeans.modules.web.api.webmodule.WebModule; 55 import org.netbeans.modules.web.taglib.TLDDataObject; 56 import org.openide.ErrorManager; 57 import org.openide.filesystems.FileObject; 58 59 import java.util.List ; 60 import java.util.LinkedList ; 61 import java.util.Iterator ; 62 import java.util.Collection ; 63 import org.netbeans.modules.web.taglib.TLDLoader; 64 import org.openide.loaders.DataObject; 65 import org.openide.loaders.DataObjectNotFoundException; 66 import org.openide.util.NbBundle; 67 68 69 72 public class J2EESafeDeleteRefactoringPlugin implements RefactoringPlugin { 73 74 77 private static ThreadLocal semafor = new ThreadLocal (); 78 79 80 private static ErrorManager err = ErrorManager.getDefault() 81 .getInstance("org.netbeans.modules.j2ee.refactoring.safedelete"); private List refactorings = new LinkedList (); 83 84 private SafeDeleteRefactoring safeDeleteRefactoring; 85 86 private Taglib taglib = null; 87 88 91 public J2EESafeDeleteRefactoringPlugin(AbstractRefactoring refactoring) { 92 safeDeleteRefactoring = (SafeDeleteRefactoring) refactoring; 93 } 94 95 100 public Problem preCheck() { 101 Problem problem = null; 102 if (semafor.get() == null) { 103 semafor.set(new Object ()); 104 try { 105 prepareRefactorings(); 106 for (Iterator it = refactorings.iterator(); it.hasNext();) { 107 J2EERefactoring refactoring = ((J2EERefactoring) it.next()); 108 problem = Utility.addProblemsToEnd(problem, refactoring.preCheck()); 109 } 110 if (safeDeleteRefactoring.getElementsToDelete().length > 0){ 111 Element elem = safeDeleteRefactoring.getElementsToDelete()[0]; 112 if (Utility.isEjb30(elem) && Utility.hasEjbJar(elem)){ 113 Problem ejbJarProblem = new Problem(false, NbBundle.getMessage(J2EESafeDeleteRefactoringPlugin.class, "TXT_EjbJarSafeDeleteWarning")); problem = Utility.addProblemsToEnd(problem, ejbJarProblem); 115 } 116 } 117 }finally { 118 semafor.set(null); 119 } 120 } 121 return problem; 122 } 123 124 public Problem fastCheckParameters() { 125 Problem problem = null; 126 if (semafor.get() == null) { 127 semafor.set(new Object ()); 128 try { 129 for (Iterator it = refactorings.iterator(); it.hasNext();) { 130 J2EERefactoring refactoring = ((J2EERefactoring) it.next()); 131 problem = Utility.addProblemsToEnd(problem, refactoring.fastCheckParameters()); 132 } 133 } finally { 134 semafor.set(null); 135 } 136 } 137 return problem; 138 } 139 140 145 public Problem checkParameters() { 146 Problem problem = null; 147 if (semafor.get() == null) { 148 semafor.set(new Object ()); 149 try { 150 for (Iterator it = refactorings.iterator(); it.hasNext();) { 151 J2EERefactoring refactoring = ((J2EERefactoring) it.next()); 152 problem = Utility.addProblemsToEnd(problem, refactoring.checkParameters()); 153 } 154 } finally { 155 semafor.set(null); 156 } 157 } 158 return problem; 159 } 160 161 169 public Problem prepare(RefactoringElementsBag refactoringElements) { 170 Problem problem = null; 171 if (semafor.get() == null) { 172 semafor.set(new Object ()); 173 try { 174 for (Iterator it = refactorings.iterator(); it.hasNext();) { 175 J2EERefactoring refactoring = ((J2EERefactoring) it.next()); 176 problem = Utility.addProblemsToEnd(problem, refactoring.prepare(refactoringElements)); 177 } 178 } finally { 179 semafor.set(null); 180 } 181 } 182 return problem; 183 } 184 185 public void cancelRequest() { 186 } 187 188 private void prepareRefactorings() { 189 refactorings.clear(); 190 Element[] elementsToDelete = safeDeleteRefactoring.getElementsToDelete(); 191 for (int i = 0; i < elementsToDelete.length; i++) { 192 Element element = elementsToDelete[i]; 193 preparePersistenceRefactoring(element); 194 prepareJaxWsRefactoring(element); 195 prepareSunJaxWsRefactoring(element); 196 prepareEntityRefactoring(element); 197 prepareWebXmlRefactoring(element); 198 prepareWebservicesRefactoring(element); 199 200 if (!Utility.isEjb30(element)){ 201 FileObject ejbJarFileObject = Utility.getEjbJarFileObject(element); 202 EjbJar ejbJar = Utility.getEjbJar(Utility.getApiEjbJar(element)); 203 if (ejbJar != null) { 204 final BaseRefactoring.EjbHelper helper = 206 new BaseRefactoring.EjbHelper(safeDeleteRefactoring, ejbJarFileObject, ejbJar); 207 prepareRefactoring(helper, element); 208 } 209 } 210 211 Collection webModules = Utility.getRelevantWebModules(element); 213 Iterator wmIter = webModules.iterator(); 214 if (wmIter != null) { 215 while (wmIter.hasNext()) { 216 WebModule wm = (WebModule)wmIter.next(); 217 if (wm != null) { FileObject webInf = wm.getWebInf(); 219 Enumeration e = null; 220 if (webInf != null) { 221 e = webInf.getChildren(true); 222 } 223 if (e != null) { 224 while (e.hasMoreElements()) { 225 FileObject tld = (FileObject)e.nextElement(); 226 if (isTld(tld)) { 227 DataObject tldData = null; 228 try { 229 tldData = DataObject.find(tld); 230 } catch (DataObjectNotFoundException dne) { 231 } 233 if ((tldData != null) && (tldData instanceof TLDDataObject)) { 234 try { 235 taglib = ((TLDDataObject)tldData).getTaglib(); 236 } catch (IOException ioe) {} 237 if (taglib != null) { 238 final BaseRefactoring.TldHelper helper = 239 new BaseRefactoring.TldHelper(safeDeleteRefactoring, tld, taglib); 240 prepareRefactoring(helper, element); 241 } 242 } 243 } 244 } 245 } 246 } 247 } 248 } 249 } 250 } 251 252 private boolean isTld(FileObject fo) { 253 boolean isTld = false; 254 if (fo != null) { 255 String ext = fo.getExt(); 256 if (TLDLoader.tldExt.equalsIgnoreCase(ext)) { 257 isTld = true; 258 } 259 } 260 return isTld; 261 } 262 263 private void prepareRefactoring(BaseRefactoring.EjbHelper helper, Element element) { 264 if (element instanceof Method) { 265 prepareMethodRefactoring(helper, (Method) element); 266 } else if (element instanceof JavaClass) { 267 JavaClass javaClass = ((JavaClass) element); 268 Ejb ejb = Utility.getEjb(helper.getEjbJar(), javaClass); 269 if (ejb != null) { 270 refactorings.add(new EjbClassSafeDeleteRefactoring(helper, ejb, javaClass)); 271 } 272 } else { 273 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Safe Delete - Unknown Element: " + element); 274 } 275 } 276 277 private void prepareRefactoring(BaseRefactoring.TldHelper helper, Element element) { 278 if (element instanceof JavaClass) { 279 JavaClass javaClass = ((JavaClass) element); 280 if (taglib != null) { 281 refactorings.add(new TldClassSafeDeleteRefactoring(helper, taglib, javaClass)); 282 } 283 } else if (element instanceof Resource) { 284 List classes = ((Resource)element).getClassifiers(); 285 if (classes != null) { 286 287 } 288 } else { 289 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Safe Delete - Unknown Element: " + element); 290 } 291 } 292 293 private void prepareMethodRefactoring(BaseRefactoring.EjbHelper helper, Method method) { 294 Ejb ejb = Utility.getEjb(helper.getEjbJar(), method.getDeclaringClass()); 295 if (ejb != null) { 296 if (ejb instanceof Entity) { 297 final Entity entity = (Entity) ejb; 298 if (checkCmpFields(helper, entity, method)) { 299 return; 300 } 301 if (checkQueries(helper, entity, method)) { 302 return; 303 } 304 } 305 refactorings.add(new EjbMethodSafeDeleteRefactoring(helper, method)); 306 } 307 } 308 309 private void preparePersistenceRefactoring(Element element){ 310 if (!(element instanceof JavaClass)){ 311 return; 312 } 313 PersistenceXmlSafeDeleteRefactoring refactoring = 314 new PersistenceXmlSafeDeleteRefactoring(element, safeDeleteRefactoring); 315 refactorings.add(refactoring); 316 } 317 318 private void prepareWebservicesRefactoring(Element element){ 319 if (!(element instanceof JavaClass)){ 320 return; 321 } 322 WebservicesXmlSafeDeleteRefactoring refactoring = 323 new WebservicesXmlSafeDeleteRefactoring(element, safeDeleteRefactoring); 324 refactorings.add(refactoring); 325 } 326 327 private void prepareJaxWsRefactoring(Element element){ 328 if (!(element instanceof JavaClass)){ 329 return; 330 } 331 JaxWsXmlSafeDeleteRafactoring refactoring = new JaxWsXmlSafeDeleteRafactoring((JavaClass) element); 332 refactorings.add(refactoring); 333 } 334 335 private void prepareSunJaxWsRefactoring(Element element){ 336 if (!(element instanceof JavaClass)){ 337 return; 338 } 339 SunJaxWsXmlSafeDeleteRefactoring refactoring = new SunJaxWsXmlSafeDeleteRefactoring((JavaClass) element); 340 refactorings.add(refactoring); 341 } 342 343 private void prepareEntityRefactoring(Element element){ 344 if (!(element instanceof Feature)){ 345 return; 346 } 347 Feature feature = (Feature) element; 348 JavaClass javaClass = (JavaClass) feature.getDeclaringClass(); 349 if (!PersistenceRefactoringUtil.isEntity(javaClass)){ 350 return; 351 } 352 EntitySafeDeleteRefactoring refactoring = 353 new EntitySafeDeleteRefactoring(javaClass, feature, safeDeleteRefactoring); 354 refactorings.add(refactoring); 355 } 356 357 private void prepareWebXmlRefactoring(Element element){ 358 if (!(element instanceof JavaClass)){ 359 return; 360 } 361 WebXmlSafeDeleteRefactoring refactoring = new WebXmlSafeDeleteRefactoring(safeDeleteRefactoring, (JavaClass) element); 362 refactorings.add(refactoring); 363 } 364 365 private boolean checkCmpFields(BaseRefactoring.EjbHelper helper, Entity entity, Method method) { 366 final String fieldName = Utility.getFieldName(method); 367 if (fieldName != null) { 368 final CmpField[] cmpField = entity.getCmpField(); 369 for (int i = 0; i < cmpField.length; i++) { 370 CmpField field = cmpField[i]; 371 if (fieldName.equals(field.getFieldName())) { 372 final Collection accessMethods = Utility.getAccessMethods(entity, fieldName); 373 if (accessMethods.remove(method)) { 374 final Method[] additionalMethods = new Method[accessMethods.size()]; 375 accessMethods.toArray(additionalMethods); 376 final BaseRefactoring refactoring = new EjbCmpFieldSafeDeleteRefactoring(helper, entity, field); 377 refactoring.addChildRefactoring(new SafeDeleteRefactoring(additionalMethods)); 378 refactorings.add(refactoring); 379 return true; 380 } 381 break; 382 } 383 } 384 } 385 return false; 386 } 387 388 private boolean checkQueries(BaseRefactoring.EjbHelper helper, Entity entity, Method method) { 389 final String methodName = method.getName(); 390 final Query[] queries = entity.getQuery(); 391 for (int i = 0; i < queries.length; i++) { 392 Query query = queries[i]; 393 if (methodName.equals(query.getQueryMethod().getMethodName())) { 394 final Collection queryMethods = Utility.getQueryMethods(entity, query); 395 if (queryMethods.remove(method)) { 396 final Method[] additionalMethods = new Method[queryMethods.size()]; 397 queryMethods.toArray(additionalMethods); 398 final BaseRefactoring refactoring = 399 new EjbQueryMethodSafeDeleteRefactoring(helper, entity, query); 400 refactoring.addChildRefactoring(new SafeDeleteRefactoring(additionalMethods)); 401 refactorings.add(refactoring); 402 return true; 403 } 404 break; 405 } 406 } 407 return false; 408 } 409 410 } | Popular Tags |