root / tags / v2_0_0_Build_2049 / extensions / org.gvsig.mkmvnproject / src / main / java / org / gvsig / mkmvnproject / MakeMavenProjectExtension.java @ 38505
History | View | Annotate | Download (9.11 KB)
1 | 32708 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | 32378 | jbadia | package org.gvsig.mkmvnproject; |
23 | |||
24 | 38429 | jldominguez | import java.awt.Component; |
25 | 35207 | jjdelcerro | import java.awt.GridBagConstraints; |
26 | 32378 | jbadia | import java.io.File; |
27 | 34125 | cordinyana | import java.io.IOException; |
28 | 32378 | jbadia | import java.net.URL; |
29 | |||
30 | 38429 | jldominguez | import javax.swing.JOptionPane; |
31 | |||
32 | import org.apache.tools.ant.BuildListener; |
||
33 | 32378 | jbadia | import org.apache.tools.ant.DefaultLogger; |
34 | import org.apache.tools.ant.Project; |
||
35 | import org.apache.tools.ant.ProjectHelper; |
||
36 | 32708 | cordinyana | import org.slf4j.Logger; |
37 | import org.slf4j.LoggerFactory; |
||
38 | 32378 | jbadia | |
39 | 34125 | cordinyana | import org.gvsig.andami.PluginServices; |
40 | import org.gvsig.andami.messages.NotificationManager; |
||
41 | import org.gvsig.andami.plugins.Extension; |
||
42 | 38429 | jldominguez | import org.gvsig.andami.ui.mdiManager.IWindow; |
43 | import org.gvsig.i18n.Messages; |
||
44 | 34125 | cordinyana | |
45 | 32861 | cordinyana | /**
|
46 | * Extension to launch the project creation from templates.
|
||
47 | *
|
||
48 | * @author gvSIG team
|
||
49 | * @version $Id$
|
||
50 | */
|
||
51 | 32708 | cordinyana | public class MakeMavenProjectExtension extends Extension { |
52 | 32378 | jbadia | |
53 | 38429 | jldominguez | private static final String ANT_BUILD_FILE = "mkmvnproject.xml"; |
54 | private static final String ANT_CHECK_JDK = "checkjdk.xml"; |
||
55 | 32708 | cordinyana | |
56 | private static final String ANT_TARGET = "mkproject"; |
||
57 | 32861 | cordinyana | |
58 | 32708 | cordinyana | private static final Logger LOG = LoggerFactory |
59 | .getLogger(MakeMavenProjectExtension.class); |
||
60 | |||
61 | 34125 | cordinyana | private boolean enabled = true; |
62 | |||
63 | private final Object lock = new Object(); |
||
64 | |||
65 | 32378 | jbadia | public void execute(String actionCommand) { |
66 | 38429 | jldominguez | |
67 | 38433 | jldominguez | if (!doJdkCheck()) {
|
68 | return;
|
||
69 | 38429 | jldominguez | } |
70 | |||
71 | 38433 | jldominguez | // ==========================================================
|
72 | 34125 | cordinyana | // TODO: add support to parallel executions in Andami??
|
73 | 32378 | jbadia | |
74 | 35207 | jjdelcerro | // Create messages console
|
75 | CreatePluginConsoleWindow console = null;
|
||
76 | 32708 | cordinyana | |
77 | 35207 | jjdelcerro | if (console == null) { |
78 | try {
|
||
79 | console = new CreatePluginConsoleWindow();
|
||
80 | } catch (IOException e) { |
||
81 | NotificationManager.addWarning("Error creating the console", e);
|
||
82 | } |
||
83 | } |
||
84 | if (console != null) { |
||
85 | PluginServices.getMDIManager().addWindow(console, GridBagConstraints.LAST_LINE_START);
|
||
86 | } |
||
87 | |||
88 | 34125 | cordinyana | try {
|
89 | // Disable extension so it can't be executed again.
|
||
90 | synchronized (lock) {
|
||
91 | enabled = false;
|
||
92 | } |
||
93 | new CreatePluginThread(console).start();
|
||
94 | } finally {
|
||
95 | synchronized (lock) {
|
||
96 | // create plugin process finish. Enable extension.
|
||
97 | enabled = true;
|
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | 32378 | jbadia | |
102 | 34125 | cordinyana | /**
|
103 | 38433 | jldominguez | *
|
104 | */
|
||
105 | private boolean doJdkCheck() { |
||
106 | |||
107 | boolean this_is_jdk = false; |
||
108 | try {
|
||
109 | this_is_jdk = jdkAvailable(); |
||
110 | } catch (Exception ex) { |
||
111 | LOG.info("While searching for JDK: " + ex.getMessage());
|
||
112 | this_is_jdk = false;
|
||
113 | } |
||
114 | |||
115 | if (this_is_jdk) {
|
||
116 | return true; |
||
117 | } |
||
118 | |||
119 | String env_j_h = System.getenv("JAVA_HOME"); |
||
120 | |||
121 | String jdk_str = null; |
||
122 | if (this_is_jdk) {
|
||
123 | jdk_str = Messages.getText("_gvSIG_is_running_on_JDK");
|
||
124 | } else {
|
||
125 | jdk_str = Messages.getText("_gvSIG_is_not_running_on_JDK");
|
||
126 | } |
||
127 | |||
128 | String jh_str = null; |
||
129 | if (env_j_h == null) { |
||
130 | jh_str = Messages.getText("_JAVA_HOME_is_not_set");
|
||
131 | } else {
|
||
132 | jh_str = Messages.getText("_JAVA_HOME_is_set_to")
|
||
133 | + " '" + env_j_h + "'"; |
||
134 | } |
||
135 | |||
136 | String recomm = ""; |
||
137 | 38437 | jldominguez | if (true) { // !this_is_jdk && env_j_h == null) { |
138 | recomm = "\n\n"
|
||
139 | + Messages.getText("_Requirements_for_this_plugin") + ":\n\n" |
||
140 | + "- " + Messages.getText("_On_Linux_make_JAVA_HOME_point_to_JDK_1_5_higher"); |
||
141 | recomm = recomm + ". \n- "
|
||
142 | + Messages.getText("_On_Windows_run_JDK_use_devel_exe") + "."; |
||
143 | 38433 | jldominguez | } |
144 | |||
145 | String tot_msg = Messages.getText("_Detected_settings") + ":" |
||
146 | + "\n\n"
|
||
147 | + "- " + jdk_str + "\n" |
||
148 | + "- " + jh_str + " " + recomm + "\n\n" |
||
149 | + Messages.getText("_Continue_question");
|
||
150 | |||
151 | int opt = JOptionPane.showConfirmDialog( |
||
152 | null, // getActiveWindowComponent(), |
||
153 | tot_msg, |
||
154 | 38437 | jldominguez | Messages.getText("_Make_maven_project_plugin") + " - " + |
155 | 38433 | jldominguez | Messages.getText("_Searching_JDK"),
|
156 | JOptionPane.YES_NO_OPTION,
|
||
157 | (recomm.length() == 0) ?
|
||
158 | JOptionPane.INFORMATION_MESSAGE : JOptionPane.WARNING_MESSAGE |
||
159 | ); |
||
160 | |||
161 | return (opt == JOptionPane.YES_OPTION); |
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | 38429 | jldominguez | * @return
|
166 | */
|
||
167 | private Component getActiveWindowComponent() { |
||
168 | |||
169 | IWindow iw = PluginServices.getMDIManager().getActiveWindow(); |
||
170 | if (iw instanceof Component) { |
||
171 | return (Component) iw; |
||
172 | } else {
|
||
173 | return null; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | 38433 | jldominguez | |
178 | 38429 | jldominguez | private boolean jdkAvailable() throws Exception { |
179 | |||
180 | ClassLoader loader = this.getClass().getClassLoader(); |
||
181 | URL class_file_url = loader.getResource("compilationcheck/DummyClass.class"); |
||
182 | File class_file = null; |
||
183 | |||
184 | // deleting compiled test file if existed
|
||
185 | if (class_file_url != null) { |
||
186 | class_file = new File(class_file_url.getFile()); |
||
187 | if (class_file.exists()) {
|
||
188 | class_file.delete(); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | URL build = loader.getResource("scripts/" + ANT_CHECK_JDK); |
||
193 | 38433 | jldominguez | |
194 | String execut = null; |
||
195 | /*
|
||
196 | 38429 | jldominguez | // first attempt: javac from JAVA_HOME
|
197 | 38433 | jldominguez | execut =
|
198 | 38429 | jldominguez | System.getenv().get("JAVA_HOME")
|
199 | + File.separator + "bin" + File.separator + "javac";
|
||
200 | |||
201 | if (execut != null) {
|
||
202 | this.runScript(build, "default", null, "executable", execut);
|
||
203 | class_file_url = loader.getResource("compilationcheck/DummyClass.class");
|
||
204 | if (class_file_url != null) {
|
||
205 | // OK, test file is not compiled
|
||
206 | return true;
|
||
207 | }
|
||
208 | }
|
||
209 | 38433 | jldominguez | */
|
210 | 38429 | jldominguez | |
211 | // second attempt: java.home (jre)
|
||
212 | execut = System.getProperty("java.home"); |
||
213 | int ind = execut.lastIndexOf(File.separator); |
||
214 | // one up, then bin/javac
|
||
215 | execut = execut.substring(0, ind)
|
||
216 | + File.separator
|
||
217 | + "bin" + File.separator + "javac"; |
||
218 | |||
219 | this.runScript(build, "default", null, "executable", execut); |
||
220 | class_file_url = loader.getResource("compilationcheck/DummyClass.class");
|
||
221 | // if test file not compiled, then false
|
||
222 | return (class_file_url != null); |
||
223 | } |
||
224 | |||
225 | /**
|
||
226 | 34125 | cordinyana | * Thread to launch the ant base plugin creation project.
|
227 | *
|
||
228 | * @author gvSIG Team
|
||
229 | * @version $Id$
|
||
230 | */
|
||
231 | 38429 | jldominguez | private final class CreatePluginThread extends Thread { |
232 | 34125 | cordinyana | |
233 | private final CreatePluginConsoleWindow console; |
||
234 | |||
235 | /**
|
||
236 | * Constructor.
|
||
237 | *
|
||
238 | * @param console
|
||
239 | */
|
||
240 | public CreatePluginThread(CreatePluginConsoleWindow console) {
|
||
241 | this.console = console;
|
||
242 | setName("Create plugin execution thread");
|
||
243 | setDaemon(true);
|
||
244 | } |
||
245 | |||
246 | @Override
|
||
247 | public void run() { |
||
248 | DefaultLogger log = new DefaultLogger();
|
||
249 | log.setMessageOutputLevel(Project.MSG_INFO); |
||
250 | |||
251 | if (console != null) { |
||
252 | log.setErrorPrintStream(console.getErrorPrintStream()); |
||
253 | log.setOutputPrintStream(console.getPrintStream()); |
||
254 | } else {
|
||
255 | LOG.warn("Console window not available, will use the default console for Ant");
|
||
256 | log.setErrorPrintStream(System.err);
|
||
257 | log.setOutputPrintStream(System.out);
|
||
258 | } |
||
259 | |||
260 | 38429 | jldominguez | |
261 | |||
262 | ClassLoader loader = this.getClass().getClassLoader(); |
||
263 | URL build = loader.getResource("scripts/" + ANT_BUILD_FILE); |
||
264 | runScript(build, ANT_TARGET, log); |
||
265 | } |
||
266 | } |
||
267 | 34125 | cordinyana | |
268 | |||
269 | 38429 | jldominguez | private void runScript( |
270 | URL build,
|
||
271 | String target,
|
||
272 | BuildListener blistener) { |
||
273 | |||
274 | runScript(build, target, blistener, null, null); |
||
275 | 32378 | jbadia | } |
276 | |||
277 | 38429 | jldominguez | private void runScript( |
278 | URL build,
|
||
279 | String target,
|
||
280 | BuildListener blistener, |
||
281 | String prop, String val) { |
||
282 | |||
283 | File file = new File(build.getFile()); |
||
284 | |||
285 | final Project ant = new Project(); |
||
286 | if (blistener != null) { |
||
287 | ant.addBuildListener(blistener); |
||
288 | } |
||
289 | ant.setUserProperty("ant.file", file.getAbsolutePath());
|
||
290 | |||
291 | if (prop != null && val != null) { |
||
292 | ant.setUserProperty(prop, val); |
||
293 | } |
||
294 | |||
295 | ant.init(); |
||
296 | ProjectHelper.getProjectHelper().parse(ant, file); |
||
297 | |||
298 | LOG.info("Starting ant task with the file {} and the target {}",
|
||
299 | file, target); |
||
300 | ant.executeTarget(target); |
||
301 | } |
||
302 | |||
303 | 32378 | jbadia | public void initialize() { |
304 | 32708 | cordinyana | // Nothing to do
|
305 | 32378 | jbadia | } |
306 | |||
307 | public boolean isEnabled() { |
||
308 | 34125 | cordinyana | return enabled;
|
309 | 32378 | jbadia | } |
310 | |||
311 | public boolean isVisible() { |
||
312 | return true; |
||
313 | } |
||
314 | 38429 | jldominguez | |
315 | 32378 | jbadia | |
316 | 32708 | cordinyana | } |