1 package org_scala_tools_maven_executions;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import java.lang.reflect.Method;
11 import java.lang.reflect.Modifier;
12 import java.net.URL;
13 import java.net.URLClassLoader;
14 import java.net.URLDecoder;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.codehaus.plexus.util.DirectoryScanner;
23 import org.codehaus.plexus.util.StringUtils;
24
25
26
27
28
29 public class MainHelper {
30
31 public static String toMultiPath(List<String> paths) {
32 return StringUtils.join(paths.iterator(), File.pathSeparator);
33 }
34
35 public static String toMultiPath(String[] paths) {
36 return StringUtils.join(paths, File.pathSeparator);
37 }
38
39 public static String[] findFiles(File dir, String pattern) {
40 return findFiles(dir, new String[] { pattern }, new String[0]);
41 }
42
43 public static String[] findFiles(File dir, String[] includes, String[] excludes) {
44 DirectoryScanner scanner = new DirectoryScanner();
45 scanner.setBasedir(dir);
46 scanner.setIncludes(includes);
47 scanner.setExcludes(excludes);
48 scanner.addDefaultExcludes();
49 scanner.scan();
50 return scanner.getIncludedFiles();
51 }
52
53 public static String toClasspathString(ClassLoader cl) throws Exception {
54 StringBuilder back = new StringBuilder();
55 List<String> cps = new LinkedList<String>();
56 appendUrltoClasspathCollection(cl, cps);
57 for(String cp : cps) {
58 if (back.length() != 0) {
59 back.append(File.pathSeparatorChar);
60 }
61 back.append(cp);
62 }
63 return back.toString();
64 }
65
66 public static void appendUrltoClasspathCollection(ClassLoader cl, Collection<String> classpath) throws Exception {
67 if (cl == null) {
68 cl = Thread.currentThread().getContextClassLoader();
69 }
70 while (cl != null) {
71 if (cl instanceof URLClassLoader) {
72 URLClassLoader ucl = (URLClassLoader) cl;
73 URL[] urls = ucl.getURLs();
74 for (URL url : urls) {
75 classpath.add(url.getFile());
76 }
77 }
78 cl = cl.getParent();
79 }
80 }
81
82
83
84
85
86
87 private static String escapeArgumentForScalacArgumentFile(String arg) {
88 if(arg.matches(".*\\s.*")) {
89 return '"' + arg + '"';
90 }
91 return arg;
92 }
93
94
95
96
97
98
99 private static String unescapeArgumentForScalacArgumentFile(String arg) {
100 if(arg.charAt(0) == '"' && arg.charAt(arg.length() -1) == '"') {
101 return arg.substring(1, arg.length() -1);
102 }
103 return arg;
104 }
105
106
107
108
109
110
111
112 public static File createArgFile(List<String> args) throws IOException {
113 final File argFile = File.createTempFile("scala-maven-", ".args");
114
115 final PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(argFile)));
116 try {
117 for(String arg : args) {
118 out.println(escapeArgumentForScalacArgumentFile(arg));
119 }
120 } finally {
121 out.close();
122 }
123 return argFile;
124 }
125
126
127
128
129
130
131
132 public static List<String> readArgFile(File argFile) throws IOException {
133 ArrayList<String> back = new ArrayList<String>();
134 final BufferedReader in = new BufferedReader(new FileReader(argFile));
135 try {
136 String line = null;
137 while ((line = in.readLine()) != null) {
138 back.add(unescapeArgumentForScalacArgumentFile(line));
139 }
140 } finally {
141 in.close();
142 }
143 return back;
144 }
145
146
147 public static void runMain(String mainClassName, List<String> args, ClassLoader cl) throws Exception {
148 if(cl == null) {
149 cl = Thread.currentThread().getContextClassLoader();
150 }
151 Class<?> mainClass = cl.loadClass(mainClassName);
152 Method mainMethod = mainClass.getMethod("main", String[].class);
153 int mods = mainMethod.getModifiers();
154 if(mainMethod.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
155 throw new NoSuchMethodException("main");
156 }
157 String[] argArray = args.toArray(new String[args.size()]);
158
159
160
161 mainMethod.invoke(null, new Object[] {argArray});
162 }
163
164 public static String locateJar(Class<?> c) throws Exception {
165 final URL location;
166 final String classLocation = c.getName().replace('.', '/') + ".class";
167 final ClassLoader loader = c.getClassLoader();
168 if( loader == null ) {
169 location = ClassLoader.getSystemResource(classLocation);
170 } else {
171 location = loader.getResource(classLocation);
172 }
173 if( location != null ){
174 Pattern p = Pattern.compile( "^.*file:(.*)!.*$" ) ;
175 Matcher m = p.matcher( location.toString() ) ;
176 if( m.find() ) {
177 return URLDecoder.decode(m.group( 1 ), "UTF-8") ;
178 }
179 throw new ClassNotFoundException( "Cannot parse location of '" + location + "'. Probably not loaded from a Jar" ) ;
180 }
181 throw new ClassNotFoundException( "Cannot find class '" + c.getName() + " using the classloader" ) ;
182 }
183 }