Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64088a15b5012eb2f08e278052786192d7139cf8 (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
/*******************************************************************************
 * 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.project;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.maven.model.Model;

import org.eclipse.m2e.core.core.MavenLogger;


/**
 * @author Eugene Kuleshov
 */
public class MavenProjectInfo {

  /**
   * Project basedir must NOT be renamed on filesystem.
   */
  public static final int RENAME_NO = 0;

  /**
   * Project basedir MUST be ranamed to match workspace project name.  
   */
  public static final int RENAME_REQUIRED = 2;

  private final String label;

  private File pomFile;

  private Model model;

  private final MavenProjectInfo parent;

  /**
   * Map of MavenProjectInfo
   */
  private final Map<String, MavenProjectInfo> projects = new LinkedHashMap<String, MavenProjectInfo>();

  private final Set<String> profiles = new HashSet<String>();

  private int basedirRename = RENAME_NO;

  public MavenProjectInfo(String label, File pomFile, Model model, MavenProjectInfo parent) {
    this.label = label;
    this.pomFile = pomFile;
    this.model = model;
    this.parent = parent;
  }

  public void setPomFile(File pomFile) {
    File oldDir = this.pomFile.getParentFile();
    File newDir = pomFile.getParentFile();
    
    for(MavenProjectInfo projectInfo : projects.values()) {
      File childPom = projectInfo.getPomFile();
      if(isSubDir(oldDir, childPom.getParentFile())) {
        String oldPath = oldDir.getAbsolutePath();
        String path = childPom.getAbsolutePath().substring(oldPath.length());
        projectInfo.setPomFile(new File(newDir, path));
      }
    }
    
    this.pomFile = pomFile;
  }

  /** @deprecated use set/get BasedirRename */
  public void setNeedsRename(boolean needsRename) {
    setBasedirRename(needsRename? RENAME_REQUIRED: RENAME_NO);
  }
  
  /** @deprecated use set/get BasedirRenamePolicy */
  public boolean isNeedsRename() {
    return getBasedirRename() == RENAME_REQUIRED;
  }

  /**
   * See {@link #RENAME_NO}, {@link #RENAME_REQUIRED}
   */
  public void setBasedirRename(int basedirRename) {
    this.basedirRename = basedirRename;
  }

  /**
   * See {@link #RENAME_NO}, {@link #RENAME_REQUIRED}
   */
  public int getBasedirRename() {
    return basedirRename;
  }

  private boolean isSubDir(File parentDir, File subDir) {
    if(parentDir.equals(subDir)) {
      return true;
    }
    
    if(subDir.getParentFile()!=null) {
      return isSubDir(parentDir, subDir.getParentFile());
    }
    
    return false;
  }

  public void add(MavenProjectInfo info) {
    String key;
    try {
      if(info.getPomFile() == null) {
        // Is this possible?
        key = info.getLabel();
      } else {
        key = info.getPomFile().getCanonicalPath();
      }
    } catch(IOException ex) {
      throw new RuntimeException(ex);
    }
    MavenProjectInfo i = projects.get(key);
    if(i==null) {
      projects.put(key, info);
    } else {
      MavenLogger.log("Project info " + this + " already has a child project info with key '" + key + "'"); //$NON-NLS-3$
      for(Iterator<String> it = info.getProfiles().iterator(); it.hasNext();) {
        i.addProfile(it.next());
      }
    }
  }
  
  public void addProfile(String profileId) {
    if(profileId!=null) {
      this.profiles.add(profileId);
    }
  }
  
  public void addProfiles(Collection<String> profiles) {
    this.profiles.addAll(profiles);
  }
  
  public String getLabel() {
    return this.label;
  }
  
  public File getPomFile() {
    return this.pomFile;
  }
  
  public Model getModel() {
    return this.model;
  }
  
  public void setModel(Model model) {
    this.model = model;
  }
  
  public Collection<MavenProjectInfo> getProjects() {
    return this.projects.values();
  }
 
  public MavenProjectInfo getParent() {
    return this.parent;
  }
  
  public Set<String> getProfiles() {
    return this.profiles;
  }
  
  public boolean equals(Object obj) {
    if(obj instanceof MavenProjectInfo) {
      MavenProjectInfo info = (MavenProjectInfo) obj;
      if(pomFile == null) {
        return info.getPomFile() == null;
      }
      return pomFile.equals(info.getPomFile());
    }
    return false;
  }
  
  public int hashCode() {
    return pomFile==null ? 0 : pomFile.hashCode();
  }

  public String toString() {
    return "'" + label + "'" + (pomFile == null ? "" : " " + pomFile.getAbsolutePath()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  }
}

Back to the top