Lines 46-51
Link Here
|
46 |
import java.net.URL; |
46 |
import java.net.URL; |
47 |
import java.net.URLConnection; |
47 |
import java.net.URLConnection; |
48 |
import java.text.MessageFormat; |
48 |
import java.text.MessageFormat; |
|
|
49 |
import java.util.Collections; |
50 |
import java.util.HashMap; |
51 |
import java.util.LinkedList; |
52 |
import java.util.List; |
53 |
import java.util.Map; |
54 |
import java.util.Map.Entry; |
49 |
import java.util.Properties; |
55 |
import java.util.Properties; |
50 |
import java.util.logging.Level; |
56 |
import java.util.logging.Level; |
51 |
import java.util.logging.Logger; |
57 |
import java.util.logging.Logger; |
Lines 56-62
Link Here
|
56 |
import org.apache.tools.ant.module.api.support.AntScriptUtils; |
62 |
import org.apache.tools.ant.module.api.support.AntScriptUtils; |
57 |
import org.netbeans.api.java.classpath.ClassPath; |
63 |
import org.netbeans.api.java.classpath.ClassPath; |
58 |
import org.netbeans.api.java.platform.JavaPlatform; |
64 |
import org.netbeans.api.java.platform.JavaPlatform; |
59 |
import org.netbeans.api.java.platform.JavaPlatformManager; |
|
|
60 |
import org.netbeans.api.project.FileOwnerQuery; |
65 |
import org.netbeans.api.project.FileOwnerQuery; |
61 |
import org.netbeans.api.project.Project; |
66 |
import org.netbeans.api.project.Project; |
62 |
import org.netbeans.api.project.ProjectUtils; |
67 |
import org.netbeans.api.project.ProjectUtils; |
Lines 65-70
Link Here
|
65 |
import org.openide.filesystems.FileUtil; |
70 |
import org.openide.filesystems.FileUtil; |
66 |
import org.openide.util.ChangeSupport; |
71 |
import org.openide.util.ChangeSupport; |
67 |
import org.openide.util.Exceptions; |
72 |
import org.openide.util.Exceptions; |
|
|
73 |
import org.openide.util.Parameters; |
68 |
import org.openide.util.WeakListeners; |
74 |
import org.openide.util.WeakListeners; |
69 |
import org.w3c.dom.Attr; |
75 |
import org.w3c.dom.Attr; |
70 |
import org.w3c.dom.DOMException; |
76 |
import org.w3c.dom.DOMException; |
Lines 84-141
Link Here
|
84 |
|
90 |
|
85 |
private static final Logger LOG = Logger.getLogger(ProjectRunnerImpl.class.getName()); |
91 |
private static final Logger LOG = Logger.getLogger(ProjectRunnerImpl.class.getName()); |
86 |
|
92 |
|
87 |
public boolean isSupported(String command, FileObject file) { |
93 |
public boolean isSupported(String command, Map<String, ?> properties) { |
88 |
return locateScript(command) != null && checkRunSupported(file); |
94 |
return locateScript(command) != null; |
89 |
} |
95 |
} |
90 |
|
96 |
|
91 |
public void execute(String command, Properties props, FileObject toRun) throws IOException { |
97 |
public void execute(String command, Map<String, ?> properties) throws IOException { |
92 |
Project project = FileOwnerQuery.getOwner(toRun); |
98 |
String[] projectName = new String[1]; |
93 |
ClassPath exec = ClassPath.getClassPath(toRun, ClassPath.EXECUTE); |
99 |
Properties antProps = computeProperties(properties, projectName); |
94 |
ClassPath boot = ClassPath.getClassPath(toRun, ClassPath.BOOT); |
|
|
95 |
ClassPath source = ClassPath.getClassPath(toRun, ClassPath.SOURCE); |
96 |
JavaPlatform foundPlatform = JavaPlatformManager.getDefault().getDefaultPlatform(); |
97 |
|
98 |
for (JavaPlatform p : JavaPlatformManager.getDefault().getInstalledPlatforms()) { |
99 |
if (boot.entries().equals(p.getBootstrapLibraries().entries())) { |
100 |
LOG.log(Level.FINE, "found platform={0}", p.getDisplayName()); |
101 |
foundPlatform = p; |
102 |
break; |
103 |
} |
104 |
} |
105 |
|
106 |
LOG.log(Level.FINE, "using platform={0}", foundPlatform.getDisplayName()); |
107 |
LOG.log(Level.FINE, "execute classpath={0}", exec); |
108 |
|
109 |
String cp = exec.toString(ClassPath.PathConversionMode.FAIL); |
110 |
|
111 |
Properties antProps = (Properties) props.clone(); |
112 |
|
113 |
antProps.setProperty("classpath", cp); |
114 |
antProps.setProperty("classname", source.getResourceName(toRun, '.', false)); |
115 |
if (antProps.get("work.dir") == null && project != null) { //NOI18N |
116 |
FileObject projDirectory = project.getProjectDirectory(); |
117 |
assert projDirectory != null; |
118 |
File file = FileUtil.toFile(projDirectory); |
119 |
if (file != null) { |
120 |
antProps.setProperty("work.dir", file.getAbsolutePath()); //NOI18N |
121 |
} |
122 |
} |
123 |
antProps.setProperty("platform.java", FileUtil.toFile(foundPlatform.findTool("java")).getAbsolutePath()); |
124 |
|
100 |
|
125 |
FileObject script = buildScript(command); |
101 |
FileObject script = buildScript(command); |
126 |
String projectName = project != null ? ProjectUtils.getInformation(project).getDisplayName() : ""; |
102 |
AntProjectCookie apc = new FakeAntProjectCookie(AntScriptUtils.antProjectCookieFor(script), projectName[0]); |
127 |
AntProjectCookie apc = new FakeAntProjectCookie(AntScriptUtils.antProjectCookieFor(script), projectName); |
|
|
128 |
AntTargetExecutor.Env execenv = new AntTargetExecutor.Env(); |
103 |
AntTargetExecutor.Env execenv = new AntTargetExecutor.Env(); |
129 |
Properties p = execenv.getProperties(); |
104 |
Properties props = execenv.getProperties(); |
130 |
p.putAll(antProps); |
105 |
props.putAll(antProps); |
131 |
execenv.setProperties(p); |
106 |
execenv.setProperties(props); |
132 |
|
107 |
|
133 |
AntTargetExecutor.createTargetExecutor(execenv).execute(apc, null); |
108 |
AntTargetExecutor.createTargetExecutor(execenv).execute(apc, null); |
134 |
} |
109 |
} |
135 |
|
110 |
|
136 |
private static boolean checkRunSupported(FileObject file) { |
111 |
static Properties computeProperties(Map<String, ?> properties, String[] projectNameOut) { |
137 |
//XXX: finish |
112 |
properties = new HashMap<String, Object>(properties); |
138 |
return true; |
113 |
FileObject toRun = getValue(properties, "execute.file", FileObject.class); |
|
|
114 |
String workDir = getValue(properties, "work.dir", String.class); |
115 |
String className = getValue(properties, "classname", String.class); |
116 |
ClassPath exec = getValue(properties, "execute.classpath", ClassPath.class); |
117 |
String javaTool = getValue(properties, "platform.java", String.class); |
118 |
String projectName = getValue(properties, "project.name", String.class); |
119 |
Iterable<String> runJVMArgs = getMultiValue(properties, "run.jvmargs", String.class); |
120 |
Iterable<String> args = getMultiValue(properties, "application.args", String.class); |
121 |
if (workDir == null) { |
122 |
Parameters.notNull("toRun", toRun); |
123 |
Project project = FileOwnerQuery.getOwner(toRun); |
124 |
if (project != null) { |
125 |
//NOI18N |
126 |
FileObject projDirectory = project.getProjectDirectory(); |
127 |
assert projDirectory != null; |
128 |
File file = FileUtil.toFile(projDirectory); |
129 |
if (file != null) { |
130 |
workDir = file.getAbsolutePath(); //NOI18N |
131 |
} |
132 |
} |
133 |
} |
134 |
if (className == null) { |
135 |
Parameters.notNull("toRun", toRun); |
136 |
ClassPath source = ClassPath.getClassPath(toRun, ClassPath.SOURCE); |
137 |
className = source.getResourceName(toRun, '.', false); |
138 |
} |
139 |
if (exec == null) { |
140 |
Parameters.notNull("toRun", toRun); |
141 |
exec = ClassPath.getClassPath(toRun, ClassPath.EXECUTE); |
142 |
} |
143 |
if (javaTool == null) { |
144 |
JavaPlatform p = getValue(properties, "platform", JavaPlatform.class); |
145 |
|
146 |
if (p == null) { |
147 |
p = JavaPlatform.getDefault(); |
148 |
} |
149 |
|
150 |
javaTool = FileUtil.toFile(p.findTool("java")).getAbsolutePath(); |
151 |
} |
152 |
if (projectName == null) { |
153 |
Project project = getValue(properties, "project", Project.class); |
154 |
if (project != null) { |
155 |
projectName = ProjectUtils.getInformation(project).getDisplayName(); |
156 |
} |
157 |
if (projectName == null && toRun != null) { |
158 |
project = FileOwnerQuery.getOwner(toRun); |
159 |
if (project != null) { |
160 |
//NOI18N |
161 |
projectName = ProjectUtils.getInformation(project).getDisplayName(); |
162 |
} |
163 |
} |
164 |
if (projectName == null) { |
165 |
projectName = ""; |
166 |
} |
167 |
} |
168 |
|
169 |
LOG.log(Level.FINE, "execute classpath={0}", exec); |
170 |
String cp = exec.toString(ClassPath.PathConversionMode.FAIL); |
171 |
Properties antProps = new Properties(); |
172 |
antProps.setProperty("classpath", cp); |
173 |
antProps.setProperty("classname", className); |
174 |
antProps.setProperty("platform.java", javaTool); |
175 |
antProps.setProperty("work.dir", workDir); |
176 |
antProps.setProperty("run.jvmargs", toOneLine(runJVMArgs)); |
177 |
antProps.setProperty("application.args", toOneLine(args)); |
178 |
|
179 |
for (Entry<String, ?> e : properties.entrySet()) { |
180 |
if (e.getValue() instanceof String) { |
181 |
antProps.setProperty(e.getKey(), (String) e.getValue()); |
182 |
} |
183 |
} |
184 |
|
185 |
projectNameOut[0] = projectName; |
186 |
|
187 |
return antProps; |
188 |
} |
189 |
|
190 |
private static <T> T getValue(Map<String, ?> properties, String name, Class<T> type) { |
191 |
Object v = properties.remove(name); |
192 |
|
193 |
if (v instanceof FileObject && type == String.class) { |
194 |
FileObject f = (FileObject) v; |
195 |
File file = FileUtil.toFile(f); |
196 |
|
197 |
v = file.getAbsolutePath(); |
198 |
} |
199 |
|
200 |
return type.cast(v); |
201 |
} |
202 |
|
203 |
private static <T> Iterable<T> getMultiValue(Map<String, ?> properties, String name, Class<T> type) { |
204 |
Iterable v = (Iterable) properties.remove(name); |
205 |
List<T> result = new LinkedList<T>(); |
206 |
|
207 |
if (v == null) { |
208 |
return Collections.emptyList(); |
209 |
} |
210 |
|
211 |
for (Object o : v) { |
212 |
result.add(type.cast(o)); |
213 |
} |
214 |
|
215 |
return result; |
216 |
} |
217 |
|
218 |
private static String toOneLine(Iterable<String> it) { |
219 |
StringBuilder result = new StringBuilder(); |
220 |
boolean first = true; |
221 |
|
222 |
for (String s : it) { |
223 |
if (!first) { |
224 |
result.append(' '); |
225 |
} |
226 |
first = false; |
227 |
result.append(s); |
228 |
} |
229 |
|
230 |
return result.toString(); |
139 |
} |
231 |
} |
140 |
|
232 |
|
141 |
private static URL locateScript(String actionName) { |
233 |
private static URL locateScript(String actionName) { |