geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From de...@apache.org
Subject svn commit: r816451 - in /geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui: ./ src/main/java/org/apache/geronimo/st/ui/refactoring/
Date Fri, 18 Sep 2009 03:18:24 GMT
Author: delos
Date: Fri Sep 18 03:18:23 2009
New Revision: 816451

URL: http://svn.apache.org/viewvc?rev=816451&view=rev
Log:
GERONIMODEVTOOLS-592 Extend "rename" for ejb/connector/ear project,too, meanwhile rename artifactId
in these deployment plans.Thanks Rodger for this patch!

Added:
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanEditHelper.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanHandler.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanTextNode.java
Removed:
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/GeronimoWebProjectRenameParticipant.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/WebDeploymentPlanEditHelper.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/WebDeploymentPlanHandler.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/WebTextNode.java
Modified:
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/plugin.xml
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/GeronimoProjectRenameParticipant.java

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/plugin.xml
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/plugin.xml?rev=816451&r1=816450&r2=816451&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/plugin.xml (original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/plugin.xml Fri
Sep 18 03:18:23 2009
@@ -217,11 +217,11 @@
                     URL="http://geronimo.apache.org/" />
     </extension>
     
- 	<extension point="org.eclipse.ltk.core.refactoring.renameParticipants">    
+ 	<extension point="org.eclipse.ltk.core.refactoring.renameParticipants">		    
 		<renameParticipant    
 			id="org.apache.geronimo.st.ui.refactoring.rename"    
-			name="Rename a project with rename additional info"     
-			class="org.apache.geronimo.st.ui.refactoring.GeronimoWebProjectRenameParticipant">
    
+			name="Rename a project with renaming additional info in deployment plan"     
+			class="org.apache.geronimo.st.ui.refactoring.GeronimoProjectRenameParticipant">   
 
 			<enablement>
 				<with variable="element">
 					<instanceof value="org.eclipse.core.resources.IProject">

Added: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanEditHelper.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanEditHelper.java?rev=816451&view=auto
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanEditHelper.java
(added)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanEditHelper.java
Fri Sep 18 03:18:23 2009
@@ -0,0 +1,62 @@
+package org.apache.geronimo.st.ui.refactoring;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+
+/**
+ * 
+ */
+public class DeploymentPlanEditHelper {
+	private String fileName;
+	private DeploymentPlanHandler handler;
+	
+	public DeploymentPlanEditHelper(String fileName){
+		this.fileName=fileName;
+		this.handler=new DeploymentPlanHandler(fileName);
+		init();
+	}
+	
+	public DeploymentPlanEditHelper(DeploymentPlanHandler deploymentPlanHandler){
+		this.fileName=deploymentPlanHandler.file;
+		this.handler=deploymentPlanHandler;
+		//parse the deployment plan, generate info
+		init();
+	} 
+	
+	private void init(){
+		SAXParserFactory factory=SAXParserFactory.newInstance();
+		try {
+			factory.setFeature("http://xml.org/sax/features/namespaces", true);
+			SAXParser p = factory.newSAXParser();
+			File file =new File(fileName);
+			p.parse(file, handler);
+		} catch (SAXNotRecognizedException e) {
+			e.printStackTrace();
+		} catch (SAXNotSupportedException e) {
+			e.printStackTrace();
+		} catch (ParserConfigurationException e) {
+			e.printStackTrace();
+		} catch (SAXException e) {
+			e.printStackTrace();
+		}catch (IOException e) {
+			e.printStackTrace();
+		}	
+	}
+	
+	public int getNodeOffset(String nodeName){
+		return handler.getNodeOffset(nodeName);
+	}
+	
+	public String getNodeValue(String nodeName){
+		return handler.getNodeValue(nodeName);
+	}
+	
+}

Added: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanHandler.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanHandler.java?rev=816451&view=auto
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanHandler.java
(added)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanHandler.java
Fri Sep 18 03:18:23 2009
@@ -0,0 +1,177 @@
+package org.apache.geronimo.st.ui.refactoring;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * A SAX Handler for geronimo deployment plan
+ */
+public class DeploymentPlanHandler extends DefaultHandler {
+	protected String file;
+	protected Locator locator;
+	protected List<DeploymentPlanTextNode> nodeList = new ArrayList<DeploymentPlanTextNode>();
+
+	protected static final int START = 0, IN_ROOT_ELEMENT = 1,
+			IN_CONTEXT_ROOT = 2, IN_ENVIRONMENT = 3, IN_MODULEID = 4,
+			IN_ARTIFACTID = 5;
+
+	protected int state = START;
+
+	public DeploymentPlanHandler(String file) {
+		this.file = file;
+	}
+
+	public List<DeploymentPlanTextNode> getNodeList() {
+		return nodeList;
+	}
+
+	public int getNodeOffset(String nodeName) {
+		for (DeploymentPlanTextNode n : nodeList) {
+			if (n.getName().equals(nodeName))
+				return n.getOffset();
+		}
+		return -1;
+	}
+
+	public String getNodeValue(String nodeName) {
+		for (DeploymentPlanTextNode n : nodeList) {
+			if (n.getName().equals(nodeName))
+				return n.getValue();
+		}
+		return null;
+	}
+
+	public void setDocumentLocator(Locator locator) {
+		this.locator = locator;
+	}
+
+	public void characters(char[] ch, int start, int length)
+			throws SAXException {
+		String value = null;
+		DeploymentPlanTextNode wtn = null;
+		switch (state) {
+		case IN_CONTEXT_ROOT:
+			value = new String(ch, start, length);
+			wtn = new DeploymentPlanTextNode();
+			try {
+				int offset = getOffset(locator.getLineNumber(), locator
+						.getColumnNumber());
+				wtn.setName(DeploymentPlanTextNode.CONTEXT_ROOT);
+				wtn.setValue(value);
+				wtn.setOffset(offset - length);
+				nodeList.add(wtn);
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+			break;
+		case IN_ARTIFACTID:
+			value = new String(ch, start, length);
+			wtn = new DeploymentPlanTextNode();
+			try {
+				int offset = getOffset(locator.getLineNumber(), locator
+						.getColumnNumber());
+				wtn.setName(DeploymentPlanTextNode.ARTIFACT_ID);
+				wtn.setValue(value);
+				wtn.setOffset(offset - length);
+				nodeList.add(wtn);
+
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+			break;
+		default:
+			break;
+		}
+	}
+
+	public void startElement(String uri, String localName, String qName,
+			Attributes attributes) throws SAXException {
+		switch (state) {
+		case START:
+			if (localName.equals("web-app") || localName.equals("openejb-jar")
+					|| localName.equals("connector")
+					|| localName.equals("application"))
+				state = IN_ROOT_ELEMENT;
+			break;
+		case IN_ROOT_ELEMENT:
+			if (localName.equals("environment"))
+				state = IN_ENVIRONMENT;
+			else if (localName.equals("context-root"))
+				state = IN_CONTEXT_ROOT;
+			break;
+		case IN_ENVIRONMENT:
+			if (localName.equals("moduleId"))
+				state = IN_MODULEID;
+			break;
+		case IN_MODULEID:
+			if (localName.equals("artifactId"))
+				state = IN_ARTIFACTID;
+			break;
+		default:
+			break;
+		}
+	}
+
+	public void endElement(String uri, String localName, String qName)
+			throws SAXException {
+		switch (state) {
+		case IN_ROOT_ELEMENT:
+			if (localName.equals("web-app") || localName.equals("openejb-jar")
+					|| localName.equals("connector")
+					|| localName.equals("application"))
+				state = START;
+			break;
+		case IN_ENVIRONMENT:
+			if (localName.equals("environment"))
+				state = IN_ROOT_ELEMENT;
+			break;
+		case IN_MODULEID:
+			if (localName.equals("moduleId"))
+				state = IN_ENVIRONMENT;
+			break;
+		case IN_ARTIFACTID:
+			if (localName.equals("artifactId"))
+				state = IN_MODULEID;
+			break;
+		case IN_CONTEXT_ROOT:
+			if (localName.equals("context-root"))
+				state = IN_ROOT_ELEMENT;
+			break;
+		default:
+			break;
+		}
+	}
+
+	// return the offset of the DeploymentPlanTextNode's end
+	protected int getOffset(int lineNumber, int columnNumber)
+			throws IOException {
+		BufferedReader br = new BufferedReader(new FileReader(file));
+
+		if (lineNumber < 1 || columnNumber < 1)
+			return -1;
+
+		int current;
+		int offset = 0;
+
+		for (int i = 1; i < lineNumber;) {
+			do {
+				current = br.read();
+				offset++;
+			} while (current != '\n');
+			i++;
+		}
+		offset += (columnNumber - 1);
+
+		br.close();
+
+		return offset;
+	}
+}

Added: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanTextNode.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanTextNode.java?rev=816451&view=auto
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanTextNode.java
(added)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/DeploymentPlanTextNode.java
Fri Sep 18 03:18:23 2009
@@ -0,0 +1,44 @@
+package org.apache.geronimo.st.ui.refactoring;
+
+public class DeploymentPlanTextNode {
+	private String name;
+	private String value;
+	private int offset;	
+	
+	public static final String CONTEXT_ROOT="context-root",ARTIFACT_ID="artifactId";
+	
+	public DeploymentPlanTextNode() {
+		this(null,null,-1);
+	}
+
+	public DeploymentPlanTextNode(String name, String value, int offset) {
+		this.name = name;
+		this.value = value;
+		this.offset = offset;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getValue() {
+		return value;
+	}
+
+	public void setValue(String value) {
+		this.value = value;
+	}
+
+	public int getOffset() {
+		return offset;
+	}
+
+	public void setOffset(int offset) {
+		this.offset = offset;
+	}
+
+}

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/GeronimoProjectRenameParticipant.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/GeronimoProjectRenameParticipant.java?rev=816451&r1=816450&r2=816451&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/GeronimoProjectRenameParticipant.java
(original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.ui/src/main/java/org/apache/geronimo/st/ui/refactoring/GeronimoProjectRenameParticipant.java
Fri Sep 18 03:18:23 2009
@@ -1,16 +1,154 @@
 package org.apache.geronimo.st.ui.refactoring;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.CompositeChange;
 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
 import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
+import org.eclipse.text.edits.MultiTextEdit;
+import org.eclipse.text.edits.ReplaceEdit;
+import org.eclipse.wst.common.componentcore.ComponentCore;
+import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
 
-public abstract class GeronimoProjectRenameParticipant extends RenameParticipant{
+public class GeronimoProjectRenameParticipant extends RenameParticipant {
 	protected IProject project;
-	
-	public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context)
throws OperationCanceledException {
+	protected IFile deploymentPlanFile;
+	protected String deploymentPlanName;
+
+	public RefactoringStatus checkConditions(IProgressMonitor pm,
+			CheckConditionsContext context) throws OperationCanceledException {
 		return new RefactoringStatus();
 	}
+
+	protected boolean initialize(Object element) {
+		// if not a project, then stop this refactoring
+		if (!(element instanceof IProject)) {
+			return false;
+		}
+
+		project = (IProject) element;
+
+		// if not a javaee object, then stop this refactoring
+		if (JavaEEProjectUtilities.isDynamicWebProject(project)) {
+			deploymentPlanName = "WEB-INF/geronimo-web.xml";
+		} else if (JavaEEProjectUtilities.isEJBProject(project)) {
+			deploymentPlanName = "META-INF/openejb-jar.xml";
+		} else if (JavaEEProjectUtilities.isJCAProject(project)) {
+			deploymentPlanName = "META-INF/geronimo-ra.xml";
+		} else if (JavaEEProjectUtilities.isEARProject(project)) {
+			deploymentPlanName = "META-INF/geronimo-application.xml";
+		} else {
+			return false;
+		}
+
+		// sure that project is DynamicWebProject
+		deploymentPlanFile = getDeploymentPlanFile(project);
+
+		// if no deployment plan file, then stop this refactoring
+		if (deploymentPlanFile == null) {
+			return false;
+		}
+
+		return true;
+	}
+
+	public Change createChange(IProgressMonitor pm) throws CoreException,
+			OperationCanceledException {
+		CompositeChange result = new CompositeChange(getName());
+		try {
+			pm
+					.beginTask(
+							"beging create change for context-root and artifactId",
+							100);
+			String oldName = project.getName();
+			String underProjectFilePath = getDeploymentPlanFileUnderProjectPath(project);
+			String projectRelativeFilePath = deploymentPlanFile.getFullPath()
+					.toString();
+			String absoluteWorkspacePath = project.getParent().getLocation()
+					.toString();
+			String absoluteFilePath = absoluteWorkspacePath
+					+ projectRelativeFilePath;
+
+			DeploymentPlanEditHelper editHelper = new DeploymentPlanEditHelper(
+					new DeploymentPlanHandler(absoluteFilePath));
+
+			// Before this change being applied, the project has been
+			// renamed. So, use a MovedTextFileChange instead of TextFileChange
+			String newName = this.getArguments().getNewName();
+			IProject newProject = ((IWorkspaceRoot) deploymentPlanFile
+					.getProject().getParent()).getProject(newName);
+			IFile newFile = newProject.getFile(underProjectFilePath);
+
+			// if a web project, should create a context-root change if any
+			if (JavaEEProjectUtilities.isDynamicWebProject(project)) {
+				// create change for context-root
+				String oldName1 = editHelper
+						.getNodeValue(DeploymentPlanTextNode.CONTEXT_ROOT);
+				int offset1 = editHelper
+						.getNodeOffset(DeploymentPlanTextNode.CONTEXT_ROOT);
+				if (offset1 != -1 && oldName1 != null
+						&& oldName1.substring(1).equals(oldName)) {
+					MovedTextFileChange change1 = new MovedTextFileChange(
+							"Rename artifactId's value", newFile,
+							deploymentPlanFile);
+					MultiTextEdit rootEdit1 = new MultiTextEdit();
+					ReplaceEdit edit1 = new ReplaceEdit(offset1, oldName1
+							.length(), "/" + newName);
+					rootEdit1.addChild(edit1);
+					change1.setEdit(rootEdit1);
+					result.add(change1);
+				}
+			}
+
+			// create change for artifactId
+			String oldName2 = editHelper
+					.getNodeValue(DeploymentPlanTextNode.ARTIFACT_ID);
+			int offset2 = editHelper
+					.getNodeOffset(DeploymentPlanTextNode.ARTIFACT_ID);
+			if (offset2 != -1 && oldName2 != null && oldName2.equals(oldName)) {
+				MovedTextFileChange change2 = new MovedTextFileChange(
+						"Rename context-root's value", newFile,
+						deploymentPlanFile);
+				MultiTextEdit rootEdit2 = new MultiTextEdit();
+				ReplaceEdit edit2 = new ReplaceEdit(offset2, oldName2.length(),
+						newName);
+				rootEdit2.addChild(edit2);
+				change2.setEdit(rootEdit2);
+				result.add(change2);
+			}
+
+		} finally {
+			pm.done();
+		}
+		if (result.getChildren().length == 0)
+			return null;
+
+		return result;
+	}
+
+	public String getName() {
+		return "Rename project name related info in geronimo deployment plan";
+	}
+
+	protected IFile getDeploymentPlanFile(IProject project) {
+		IVirtualComponent comp = ComponentCore.createComponent(project);
+		IPath deployPlanPath = comp.getRootFolder().getUnderlyingFolder()
+				.getProjectRelativePath().append(deploymentPlanName);
+		return project.getFile(deployPlanPath);
+	}
+
+	protected String getDeploymentPlanFileUnderProjectPath(IProject project) {
+		IVirtualComponent comp = ComponentCore.createComponent(project);
+		IPath deployPlanPath = comp.getRootFolder().getUnderlyingFolder()
+				.getProjectRelativePath().append(deploymentPlanName);
+		return deployPlanPath.toString();
+	}
 }



Mime
View raw message