Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ffec17e845aa165b1b91787cfc045e0d12edce6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.refactoring;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.resource.Resource;

import org.apache.maven.project.MavenProject;

import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.model.edit.pom.Model;
import org.eclipse.m2e.model.edit.pom.PropertyElement;


/**
 * This class manages all refactoring-related resources for a particular maven project
 * 
 * @author Anton Kraev
 */
public class RefactoringModelResources {
  private static final Logger log = LoggerFactory.getLogger(RefactoringModelResources.class);

  private static final String TMP_PROJECT_NAME = ".m2eclipse_refactoring"; //$NON-NLS-1$

  protected IFile pomFile;

  protected IFile tmpFile;

  protected ITextFileBuffer pomBuffer;

  protected ITextFileBuffer tmpBuffer;

  protected Model tmpModel;

  protected org.apache.maven.model.Model effective;

  protected ITextFileBufferManager textFileBufferManager;

  protected Map<String, PropertyInfo> properties;

  protected MavenProject project;

  protected CompoundCommand command;

  protected static IProject tmpProject;

  protected IProject getTmpProject() {
    if(tmpProject == null) {
      tmpProject = ResourcesPlugin.getWorkspace().getRoot().getProject(TMP_PROJECT_NAME);
    }
    if(!tmpProject.exists()) {
      try {
        tmpProject.create(null);
        tmpProject.open(null);
      } catch(CoreException ex) {
        log.error(ex.getMessage(), ex);
      }
    }
    return tmpProject;
  }

  public RefactoringModelResources(IMavenProjectFacade projectFacade) throws CoreException, IOException {
    textFileBufferManager = FileBuffers.getTextFileBufferManager();
    project = projectFacade.getMavenProject(null);
    effective = project.getModel();
    pomFile = projectFacade.getPom();
    pomBuffer = getBuffer(pomFile);

    //create temp file
    IProject project = getTmpProject();
    File f = File.createTempFile("pom", ".xml", project.getLocation().toFile()); //$NON-NLS-1$ //$NON-NLS-2$
    f.delete();
    tmpFile = project.getFile(f.getName());
    pomFile.copy(tmpFile.getFullPath(), true, null);

    Resource resource = AbstractPomRefactoring.loadResource(tmpFile);
    tmpModel = (Model) resource.getContents().get(0);
    tmpBuffer = getBuffer(tmpFile);
  }

  public CompoundCommand getCommand() {
    return command;
  }

  public void setCommand(CompoundCommand command) {
    this.command = command;
  }

  public IFile getPomFile() {
    return pomFile;
  }

  public IFile getTmpFile() {
    return tmpFile;
  }

  public ITextFileBuffer getPomBuffer() {
    return pomBuffer;
  }

  public ITextFileBuffer getTmpBuffer() {
    return tmpBuffer;
  }

  public Model getTmpModel() {
    return tmpModel;
  }

  public org.apache.maven.model.Model getEffective() {
    return effective;
  }

  public MavenProject getProject() {
    return project;
  }

  public Map<String, PropertyInfo> getProperties() {
    return properties;
  }

  public void setProperties(Map<String, PropertyInfo> properties) {
    this.properties = properties;
  }

  public void releaseAllResources() throws CoreException {
    releaseBuffer(pomBuffer, pomFile);
    if(tmpFile != null && tmpFile.exists()) {
      releaseBuffer(tmpBuffer, tmpFile);
    }
    if(tmpModel != null) {
      tmpModel.eResource().unload();
    }
  }

  public static void cleanupTmpProject() throws CoreException {
    if(tmpProject.exists()) {
      tmpProject.delete(true, true, null);
    }
  }

  protected ITextFileBuffer getBuffer(IFile file) throws CoreException {
    textFileBufferManager.connect(file.getLocation(), LocationKind.NORMALIZE, null);
    return textFileBufferManager.getTextFileBuffer(file.getLocation(), LocationKind.NORMALIZE);
  }

  protected void releaseBuffer(ITextFileBuffer buffer, IFile file) throws CoreException {
    buffer.revert(null);
    textFileBufferManager.disconnect(file.getLocation(), LocationKind.NORMALIZE, null);
  }

  public String getName() {
    return pomFile.getProject().getName();
  }

  public static class PropertyInfo {
    protected PropertyElement pair;

    protected RefactoringModelResources resource;

    protected Command newValue;

    public Command getNewValue() {
      return newValue;
    }

    public void setNewValue(Command newValue) {
      this.newValue = newValue;
    }

    public PropertyElement getPair() {
      return pair;
    }

    public void setPair(PropertyElement pair) {
      this.pair = pair;
    }

    public RefactoringModelResources getResource() {
      return resource;
    }

    public void setResource(RefactoringModelResources resource) {
      this.resource = resource;
    }
  }

}

Back to the top