Revision 38429 branches/v2_0_0_prep/extensions/org.gvsig.mkmvnproject/src/main/java/org/gvsig/mkmvnproject/MakeMavenProjectExtension.java

View differences:

MakeMavenProjectExtension.java
21 21
 */
22 22
package org.gvsig.mkmvnproject;
23 23

  
24
import java.awt.Component;
24 25
import java.awt.GridBagConstraints;
25 26
import java.io.File;
26 27
import java.io.IOException;
27 28
import java.net.URL;
28 29

  
30
import javax.swing.JOptionPane;
31

  
32
import org.apache.tools.ant.BuildListener;
29 33
import org.apache.tools.ant.DefaultLogger;
30 34
import org.apache.tools.ant.Project;
31 35
import org.apache.tools.ant.ProjectHelper;
......
35 39
import org.gvsig.andami.PluginServices;
36 40
import org.gvsig.andami.messages.NotificationManager;
37 41
import org.gvsig.andami.plugins.Extension;
42
import org.gvsig.andami.ui.mdiManager.IWindow;
43
import org.gvsig.i18n.Messages;
38 44

  
39 45
/**
40 46
 * Extension to launch the project creation from templates.
......
44 50
 */
45 51
public class MakeMavenProjectExtension extends Extension {
46 52

  
47
	private static final String ANT_BUILD_FILE = "mkmvnproject.xml";
53
    private static final String ANT_BUILD_FILE = "mkmvnproject.xml";
54
    private static final String ANT_CHECK_JDK = "checkjdk.xml";
48 55

  
49 56
	private static final String ANT_TARGET = "mkproject";
50 57

  
......
56 63
	private final Object lock = new Object();
57 64

  
58 65
	public void execute(String actionCommand) {
66
	    
67
	    boolean found_jdk = false;
68
	    try {
69
	        found_jdk = jdkAvailable(); 
70
	    } catch (Exception ex) {
71
	        LOG.info("While searching for JDK: " + ex.getMessage());
72
	        found_jdk = false;
73
	    }
74
	    
75
	    if (!found_jdk) {
76
	        
77
	        int opt = JOptionPane.showConfirmDialog(
78
	            getActiveWindowComponent(),
79
	            Messages.getText("_Java_compiler_not_found_near_current_JRE_nor_in_JAVA_HOME")
80
	            + "\n\n"
81
	            + Messages.getText("_Continue_question"),
82
	            Messages.getText("_Searching_JDK"),
83
	            JOptionPane.YES_NO_OPTION,
84
	            JOptionPane.WARNING_MESSAGE);
85
	        
86
	        if (opt == JOptionPane.NO_OPTION) {
87
	            return;
88
	        }
89
	    }
90
	    
91
	    
59 92
		// TODO: add support to parallel executions in Andami??
60 93

  
61 94
        // Create messages console
......
87 120
	}
88 121

  
89 122
	/**
123
     * @return
124
     */
125
    private Component getActiveWindowComponent() {
126
        
127
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
128
        if (iw instanceof Component) {
129
            return (Component) iw;
130
        } else {
131
            return null;
132
        }
133
    }
134

  
135
    /**
136
     * @return
137
     */
138
    private boolean jdkAvailable() throws Exception {
139
        
140
        ClassLoader loader = this.getClass().getClassLoader();
141
        URL class_file_url = loader.getResource("compilationcheck/DummyClass.class");
142
        File class_file = null;
143
        
144
        // deleting compiled test file if existed
145
        if (class_file_url != null) {
146
            class_file = new File(class_file_url.getFile());
147
            if (class_file.exists()) {
148
                class_file.delete();
149
            }
150
        }
151
        
152
        URL build = loader.getResource("scripts/" + ANT_CHECK_JDK);
153
        
154
        // first attempt: javac from JAVA_HOME
155
        String execut =
156
            System.getenv().get("JAVA_HOME")
157
            + File.separator + "bin" + File.separator + "javac";
158
        
159
        if (execut != null) {
160
            this.runScript(build, "default", null, "executable", execut);
161
            class_file_url = loader.getResource("compilationcheck/DummyClass.class");
162
            if (class_file_url != null) {
163
                // OK, test file is not compiled
164
                return true;
165
            }
166
        }
167
        
168
        
169
        // second attempt: java.home (jre)
170
        execut = System.getProperty("java.home");
171
        int ind = execut.lastIndexOf(File.separator);
172
        // one up, then bin/javac
173
        execut = execut.substring(0, ind)
174
            + File.separator
175
            + "bin" + File.separator + "javac";
176
        
177
        this.runScript(build, "default", null, "executable", execut);
178
        class_file_url = loader.getResource("compilationcheck/DummyClass.class");
179
        // if test file not compiled, then false
180
        return (class_file_url != null);
181
    }
182

  
183
    /**
90 184
	 * Thread to launch the ant base plugin creation project.
91 185
	 * 
92 186
	 * @author gvSIG Team
93 187
	 * @version $Id$
94 188
	 */
95
	private static final class CreatePluginThread extends Thread {
189
	private final class CreatePluginThread extends Thread {
96 190

  
97 191
		private final CreatePluginConsoleWindow console;
98 192

  
......
121 215
				log.setOutputPrintStream(System.out);
122 216
			}
123 217

  
124
			ClassLoader loader = this.getClass().getClassLoader();
125
			URL build = loader.getResource("scripts/" + ANT_BUILD_FILE);
126
			File file = new File(build.getFile());
218
			
219
			
220
            ClassLoader loader = this.getClass().getClassLoader();
221
            URL build = loader.getResource("scripts/" + ANT_BUILD_FILE);
222
            runScript(build, ANT_TARGET, log);
223
		}
224
	}
127 225

  
128
			final Project ant = new Project();
129
			ant.addBuildListener(log);
130
			ant.setUserProperty("ant.file", file.getAbsolutePath());
131
			ant.init();
132
			ProjectHelper.getProjectHelper().parse(ant, file);
133 226

  
134
			LOG.info("Starting ant task with the file {} and the target {}",
135
					file, ANT_TARGET);
136
			ant.executeTarget(ANT_TARGET);
137
		}
227
	private void runScript(
228
	        URL build,
229
	        String target,
230
	        BuildListener blistener) {
231
	    
232
	    runScript(build, target, blistener, null, null);
138 233
	}
139 234

  
235
	private void runScript(
236
	    URL build,
237
	    String target,
238
	    BuildListener blistener,
239
	    String prop, String val) {
240

  
241
        File file = new File(build.getFile());
242

  
243
        final Project ant = new Project();
244
        if (blistener != null) {
245
            ant.addBuildListener(blistener);
246
        }
247
        ant.setUserProperty("ant.file", file.getAbsolutePath());
248
        
249
        if (prop != null && val != null) {
250
            ant.setUserProperty(prop, val);
251
        }
252
        
253
        ant.init();
254
        ProjectHelper.getProjectHelper().parse(ant, file);
255

  
256
        LOG.info("Starting ant task with the file {} and the target {}",
257
                file, target);
258
        ant.executeTarget(target);
259
	}
260

  
140 261
	public void initialize() {
141 262
		// Nothing to do
142 263
	}
......
148 269
	public boolean isVisible() {
149 270
		return true;
150 271
	}
272
	
151 273

  
152 274
}

Also available in: Unified diff