|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2009 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.jst.javaee.ltk.core.change; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Properties; |
| 17 |
import java.util.concurrent.ExecutionException; |
| 18 |
|
| 19 |
import org.eclipse.core.resources.IFile; |
| 20 |
import org.eclipse.core.resources.IProject; |
| 21 |
import org.eclipse.core.runtime.CoreException; |
| 22 |
import org.eclipse.core.runtime.IPath; |
| 23 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 24 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 25 |
import org.eclipse.core.runtime.OperationCanceledException; |
| 26 |
import org.eclipse.core.runtime.Path; |
| 27 |
import org.eclipse.jem.util.emf.workbench.ProjectUtilities; |
| 28 |
import org.eclipse.jst.j2ee.application.internal.operations.UpdateManifestDataModelProperties; |
| 29 |
import org.eclipse.jst.j2ee.application.internal.operations.UpdateManifestDataModelProvider; |
| 30 |
import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest; |
| 31 |
import org.eclipse.jst.j2ee.internal.J2EEConstants; |
| 32 |
import org.eclipse.jst.j2ee.internal.common.CreationConstants; |
| 33 |
import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin; |
| 34 |
import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities; |
| 35 |
import org.eclipse.jst.j2ee.model.IModelProvider; |
| 36 |
import org.eclipse.jst.j2ee.model.ModelProviderManager; |
| 37 |
import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities; |
| 38 |
import org.eclipse.jst.javaee.ltk.core.nls.RefactoringResourceHandler; |
| 39 |
import org.eclipse.ltk.core.refactoring.Change; |
| 40 |
import org.eclipse.ltk.core.refactoring.ChangeDescriptor; |
| 41 |
import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
| 42 |
import org.eclipse.osgi.util.NLS; |
| 43 |
import org.eclipse.wst.common.componentcore.ComponentCore; |
| 44 |
import org.eclipse.wst.common.componentcore.internal.resources.VirtualComponent; |
| 45 |
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; |
| 46 |
import org.eclipse.wst.common.componentcore.resources.IVirtualFile; |
| 47 |
import org.eclipse.wst.common.componentcore.resources.IVirtualReference; |
| 48 |
import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory; |
| 49 |
import org.eclipse.wst.common.frameworks.datamodel.IDataModel; |
| 50 |
|
| 51 |
|
| 52 |
public class NonEARModuleReferenceRemoveChange extends Change { |
| 53 |
|
| 54 |
|
| 55 |
public NonEARModuleReferenceRemoveChange(IProject referencingEARProject, IProject projectToRemove) { |
| 56 |
super(); |
| 57 |
this.referencingModuleProject = referencingEARProject; |
| 58 |
this.projectToRemove = projectToRemove; |
| 59 |
this.referencingModuleProjectComp = (VirtualComponent)ComponentCore.createComponent(referencingEARProject); |
| 60 |
cachedRefs = referencingModuleProjectComp.getReferences(); |
| 61 |
this.projectToRemoveComp = ComponentCore.createComponent(projectToRemove); |
| 62 |
} |
| 63 |
|
| 64 |
IProject referencingModuleProject = null; |
| 65 |
VirtualComponent referencingModuleProjectComp = null; |
| 66 |
IProject projectToRemove = null; |
| 67 |
IVirtualComponent projectToRemoveComp = null; |
| 68 |
IVirtualReference[] cachedRefs = null; |
| 69 |
@Override |
| 70 |
public Object getModifiedElement() { |
| 71 |
return null; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public String getName() { |
| 76 |
|
| 77 |
String name = NLS.bind( |
| 78 |
RefactoringResourceHandler.Remove_JavaEE_References, |
| 79 |
new Object[] {projectToRemove.getName()}); |
| 80 |
|
| 81 |
name += referencingModuleProject.getName(); |
| 82 |
return name; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void initializeValidationData(IProgressMonitor pm) { |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, |
| 93 |
OperationCanceledException { |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public Change perform(IProgressMonitor pm) throws CoreException { |
| 99 |
|
| 100 |
try { |
| 101 |
removeModuleDependency(); |
| 102 |
if(isEJBClientDeletion()){ |
| 103 |
updateEJBDDWithEJBClientDeletion(); |
| 104 |
} |
| 105 |
} catch (ExecutionException e) { |
| 106 |
J2EEPlugin.logError(e); |
| 107 |
} |
| 108 |
return null; |
| 109 |
} |
| 110 |
|
| 111 |
public ChangeDescriptor getDescriptor() { |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
private boolean isEJBClientDeletion(){ |
| 116 |
|
| 117 |
if(!JavaEEProjectUtilities.isEJBProject(referencingModuleProject)) |
| 118 |
return false; |
| 119 |
Properties props = referencingModuleProjectComp.getMetaProperties(); |
| 120 |
String clientCompName = props.getProperty(CreationConstants.EJB_CLIENT_NAME); |
| 121 |
if(clientCompName == null || clientCompName.isEmpty()){ |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
if(clientCompName.equals(projectToRemove.getName())){ |
| 126 |
return true; |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/* |
| 133 |
* Remove the client JAR entry from the deployment descriptor |
| 134 |
* This method is to be used only to remove EJB Client jar entry from |
| 135 |
* EJB DD |
| 136 |
*/ |
| 137 |
private void updateEJBDDWithEJBClientDeletion() { |
| 138 |
IModelProvider ejbModel = ModelProviderManager.getModelProvider(referencingModuleProject); |
| 139 |
ejbModel.modify(new Runnable() { |
| 140 |
public void run() { |
| 141 |
IModelProvider writableEjbModel = ModelProviderManager.getModelProvider(referencingModuleProject); |
| 142 |
Object modelObject = writableEjbModel.getModelObject(); |
| 143 |
|
| 144 |
if (modelObject instanceof org.eclipse.jst.javaee.ejb.EJBJar) { |
| 145 |
org.eclipse.jst.javaee.ejb.EJBJar ejbres = (org.eclipse.jst.javaee.ejb.EJBJar) writableEjbModel.getModelObject(); |
| 146 |
if (ejbres != null) |
| 147 |
ejbres.setEjbClientJar(null); |
| 148 |
} |
| 149 |
else { |
| 150 |
org.eclipse.jst.j2ee.ejb.EJBJar ejbres = (org.eclipse.jst.j2ee.ejb.EJBJar) writableEjbModel.getModelObject(); |
| 151 |
ejbres.setEjbClientJar(null); |
| 152 |
} |
| 153 |
Properties props = referencingModuleProjectComp.getMetaProperties(); |
| 154 |
props.remove(CreationConstants.CLIENT_JAR_URI); |
| 155 |
props.remove(CreationConstants.EJB_CLIENT_NAME); |
| 156 |
referencingModuleProjectComp.clearMetaProperties(); |
| 157 |
referencingModuleProjectComp.setMetaProperties(props); |
| 158 |
|
| 159 |
} |
| 160 |
},null); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected void removeModuleDependency() throws ExecutionException { |
| 166 |
|
| 167 |
// create IVirtualComponents for the dependent and the refactored project |
| 168 |
final IVirtualComponent dependentComp = referencingModuleProjectComp; |
| 169 |
final IVirtualComponent refactoredComp = projectToRemoveComp; |
| 170 |
final IProgressMonitor monitor = new NullProgressMonitor(); |
| 171 |
// Does the dependent project have a .component reference on the refactored project? |
| 172 |
final IVirtualReference ref = hadReference(); |
| 173 |
final boolean webLibDep = hasWebLibDependency(ref); |
| 174 |
|
| 175 |
// remove the component reference on the deleted project |
| 176 |
if (refactoredComp != null) { |
| 177 |
removeReferencedComponents(monitor); |
| 178 |
} |
| 179 |
|
| 180 |
// update the manifest |
| 181 |
updateManifestDependency(true); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
protected void updateManifestDependency(final boolean remove) throws ExecutionException { |
| 186 |
final IVirtualComponent dependentComp = referencingModuleProjectComp; |
| 187 |
IProject project= dependentComp.getProject(); |
| 188 |
if(project.isAccessible()){ |
| 189 |
final String dependentProjName = referencingModuleProject.getName(); |
| 190 |
final String refactoredProjName = projectToRemove.getName(); |
| 191 |
final IVirtualFile vf = dependentComp.getRootFolder().getFile(new Path(J2EEConstants.MANIFEST_URI) ); |
| 192 |
final IFile manifestmf = vf.getUnderlyingFile(); |
| 193 |
// adding this check for https://bugs.eclipse.org/bugs/show_bug.cgi?id=170074 |
| 194 |
// (some adopters have non-jst.ear module projects that are missing manifests) |
| 195 |
if (!manifestmf.exists()) { |
| 196 |
return; |
| 197 |
} |
| 198 |
final IProgressMonitor monitor = new NullProgressMonitor(); |
| 199 |
final IDataModel updateManifestDataModel = DataModelFactory.createDataModel(new UpdateManifestDataModelProvider()); |
| 200 |
updateManifestDataModel.setProperty(UpdateManifestDataModelProperties.PROJECT_NAME, dependentProjName); |
| 201 |
updateManifestDataModel.setBooleanProperty(UpdateManifestDataModelProperties.MERGE, false); |
| 202 |
updateManifestDataModel.setProperty(UpdateManifestDataModelProperties.MANIFEST_FILE, manifestmf); |
| 203 |
final ArchiveManifest manifest = J2EEProjectUtilities.readManifest(manifestmf); |
| 204 |
String[] cp = manifest.getClassPathTokenized(); |
| 205 |
List cpList = new ArrayList(); |
| 206 |
String newCp = refactoredProjName + ".jar";//$NON-NLS-1$ |
| 207 |
for (int i = 0; i < cp.length; i++) { |
| 208 |
if (!cp[i].equals(newCp)) { |
| 209 |
cpList.add(cp[i]); |
| 210 |
} |
| 211 |
} |
| 212 |
if (!remove) { |
| 213 |
cpList.add(newCp); |
| 214 |
} |
| 215 |
updateManifestDataModel.setProperty(UpdateManifestDataModelProperties.JAR_LIST, cpList); |
| 216 |
try { |
| 217 |
updateManifestDataModel.getDefaultOperation().execute(monitor, null ); |
| 218 |
} catch (org.eclipse.core.commands.ExecutionException e) { |
| 219 |
J2EEPlugin.logError(e); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
protected void removeReferencedComponents(IProgressMonitor monitor) { |
| 225 |
|
| 226 |
if (referencingModuleProjectComp == null || !referencingModuleProjectComp.getProject().isAccessible() || referencingModuleProjectComp.isBinary()) return; |
| 227 |
|
| 228 |
IVirtualReference [] existingReferencesArray = cachedRefs; |
| 229 |
if(existingReferencesArray == null || existingReferencesArray.length == 0){ |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
List existingReferences = new ArrayList(); |
| 234 |
for(int i=0;i<existingReferencesArray.length; i++){ |
| 235 |
existingReferences.add(existingReferencesArray[i]); |
| 236 |
} |
| 237 |
|
| 238 |
List targetprojectList = new ArrayList(); |
| 239 |
if (projectToRemoveComp==null ) |
| 240 |
return; |
| 241 |
|
| 242 |
IVirtualReference ref = findMatchingReference(existingReferences, projectToRemoveComp, null); |
| 243 |
//if a ref was found matching the specified deployPath, then remove it |
| 244 |
if(ref != null){ |
| 245 |
removeRefereneceInComponent(referencingModuleProjectComp, ref); |
| 246 |
existingReferences.remove(ref); |
| 247 |
//after removing the ref, check to see if it was the last ref removed to that component |
| 248 |
//and if it was, then also remove the project reference |
| 249 |
ref = findMatchingReference(existingReferences, projectToRemoveComp); |
| 250 |
if(ref == null){ |
| 251 |
IProject targetProject = projectToRemoveComp.getProject(); |
| 252 |
targetprojectList.add(targetProject); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
try { |
| 258 |
ProjectUtilities.removeReferenceProjects(referencingModuleProjectComp.getProject(),targetprojectList); |
| 259 |
} catch (CoreException e) { |
| 260 |
J2EEPlugin.logError(e); |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
private IVirtualReference findMatchingReference(List existingReferences, IVirtualComponent comp) { |
| 266 |
return findMatchingReference(existingReferences, comp, null); |
| 267 |
} |
| 268 |
|
| 269 |
protected void removeRefereneceInComponent(IVirtualComponent component, IVirtualReference reference) { |
| 270 |
((VirtualComponent)component.getComponent()).removeReference(reference); |
| 271 |
} |
| 272 |
|
| 273 |
private IVirtualReference findMatchingReference(List existingReferences, IVirtualComponent comp, IPath path) { |
| 274 |
for(int i=0;i<existingReferences.size(); i++){ |
| 275 |
IVirtualReference ref = (IVirtualReference)existingReferences.get(i); |
| 276 |
IVirtualComponent c = ref.getReferencedComponent(); |
| 277 |
if(c != null && c.getName().equals(comp.getName())){ |
| 278 |
if(path == null){ |
| 279 |
return ref; |
| 280 |
} else if(path.equals(ref.getRuntimePath())){ |
| 281 |
return ref; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
return null; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
/** |
| 290 |
* Does the dependent project have a .component reference on the refactored project? |
| 291 |
* @return IVirtualReference or null if one didn't exist. |
| 292 |
*/ |
| 293 |
protected IVirtualReference hadReference() { |
| 294 |
final IVirtualComponent refactoredComp = projectToRemoveComp; |
| 295 |
if (refactoredComp == null) { |
| 296 |
return null; |
| 297 |
} |
| 298 |
final IVirtualReference[] refs = cachedRefs; |
| 299 |
IVirtualReference ref = null; |
| 300 |
for (int i = 0; i < refs.length; i++) { |
| 301 |
if (refs[i].getReferencedComponent().equals(refactoredComp)) { |
| 302 |
ref = refs[i]; |
| 303 |
break; |
| 304 |
} |
| 305 |
} |
| 306 |
return ref; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Does the dependent project have a .project reference on the refactored project? |
| 311 |
* (dynamic project refs don't count) |
| 312 |
* @return True if a project reference exists. |
| 313 |
*/ |
| 314 |
protected boolean hadProjectReference() { |
| 315 |
try { |
| 316 |
final IProject[] refs = referencingModuleProject.getDescription().getReferencedProjects(); |
| 317 |
final IProject refactoredProject= projectToRemove; |
| 318 |
for (int i = 0; i < refs.length; i++) { |
| 319 |
if (refs[i].equals(refactoredProject)) { |
| 320 |
return true; |
| 321 |
} |
| 322 |
} |
| 323 |
} catch (CoreException ce) { |
| 324 |
J2EEPlugin.logError(ce); |
| 325 |
} |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Returns true if the dependency was a web library dependency. |
| 331 |
* @param ref |
| 332 |
* @return |
| 333 |
*/ |
| 334 |
protected static boolean hasWebLibDependency(final IVirtualReference ref) { |
| 335 |
if (ref == null) { |
| 336 |
return false; |
| 337 |
} |
| 338 |
return ref.getRuntimePath().equals(new Path("/WEB-INF/lib")); //$NON-NLS-1$ |
| 339 |
} |
| 340 |
|
| 341 |
} |