Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1b5cc1ec8eb84a617d4c1860ea71e8a10bcf0709 (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
/*******************************************************************************
 * 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.internal.launch;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.osgi.util.NLS;

import org.eclipse.m2e.core.embedder.IMavenLauncherConfiguration;
import org.eclipse.m2e.core.project.IMavenProjectFacade;


/**
 * MavenLauncherConfigurationHandler
 * 
 * @author Igor Fedorenko
 */
public class MavenLauncherConfigurationHandler implements IMavenLauncherConfiguration {

  private String mainType;

  private String mainRealm;

  private LinkedHashMap<String, List<String>> realms = new LinkedHashMap<String, List<String>>();

  private List<String> forcedEntries = new ArrayList<String>();

  private List<String> curEntries = forcedEntries;

  public void addArchiveEntry(String entry) {
    curEntries.add(entry);
  }

  public void addProjectEntry(IMavenProjectFacade facade) {
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IFolder output = root.getFolder(facade.getOutputLocation());
    if(output.isAccessible()) {
      addArchiveEntry(output.getLocation().toFile().getAbsolutePath());
    }
    // add custom classpath entries
    final IJavaProject javaProject = JavaCore.create(facade.getProject());
    try {
      for(IClasspathEntry cpe : javaProject.getRawClasspath()) {
        if(cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
          IResource resource = root.findMember(cpe.getPath());
          if(resource != null) {
            // workspace resource
            addArchiveEntry(resource.getLocation().toOSString());
          } else {
            // external 
            File file = cpe.getPath().toFile();
            if(file.exists()) {
              addArchiveEntry(file.getAbsolutePath());
            }
          }
        }
      }
    } catch(JavaModelException ex) {
      // XXX to do what to do about this
    }
  }

  public void addRealm(String realm) {
    if(!realms.containsKey(realm)) {
      curEntries = new ArrayList<String>();
      realms.put(realm, curEntries);
    }
  }

  public void setMainType(String type, String realm) {
    this.mainType = type;
    this.mainRealm = realm;
  }

  public void save(OutputStream os) throws IOException {
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); //$NON-NLS-1$
    out.write(NLS.bind("main is {0} from {1}\n", mainType, mainRealm));
    for(Map.Entry<String, List<String>> realm : realms.entrySet()) {
      if(LAUNCHER_REALM.equals(realm.getKey())) {
        continue;
      }
      out.write(NLS.bind("[{0}]\n", realm.getKey()));
      if(mainRealm.equals(realm.getKey())) {
        for(String entry : forcedEntries) {
          out.write(NLS.bind("load {0}\n", entry));
        }
      }
      for(String entry : realm.getValue()) {
        out.write(NLS.bind("load {0}\n", entry));
      }
    }
    out.flush();
  }

  public String getMainReal() {
    return mainRealm;
  }

  public List<String> getRealmEntries(String realm) {
    return realms.get(realm);
  }
}

Back to the top