Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.mkmvnproject.app / org.gvsig.mkmvnproject.app.mainplugin / src / main / java / org / gvsig / mkmvnproject / MakeMavenProjectExtension.java @ 41074

History | View | Annotate | Download (11.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.mkmvnproject;
25

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

    
32
import javax.swing.JOptionPane;
33

    
34
import org.apache.tools.ant.BuildListener;
35
import org.apache.tools.ant.DefaultLogger;
36
import org.apache.tools.ant.Project;
37
import org.apache.tools.ant.ProjectHelper;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.andami.messages.NotificationManager;
43
import org.gvsig.andami.plugins.Extension;
44
import org.gvsig.andami.ui.mdiManager.IWindow;
45
import org.gvsig.i18n.Messages;
46
import org.gvsig.mkmvnproject.gui.CreatePluginConsoleWindow;
47
import org.gvsig.mkmvnproject.gui.settings.PlatformPropertiesWindow;
48

    
49
/**
50
 * Extension to launch the project creation from templates.
51
 * 
52
 * @author gvSIG team
53
 * @version $Id$
54
 */
55
public class MakeMavenProjectExtension extends Extension {
56

    
57
    private static final String ANT_BUILD_FILE = "mkmvnproject.xml";
58
    private static final String ANT_CHECK_JDK = "checkjdk.xml";
59

    
60
        private static final String ANT_TARGET = "mkproject";
61
        
62
        public static final String GVSIG_PLATFORM_PROPERTIES_FILE_NAME =
63
            ".gvsig.platform.properties";
64

    
65
        private static final Logger LOG = LoggerFactory
66
                        .getLogger(MakeMavenProjectExtension.class);
67

    
68
        private boolean enabled = true;
69

    
70
        private final Object lock = new Object();
71

    
72
        public void execute(String actionCommand) {
73
            
74
            /*
75
             * Problem is that for some reason  dummy file cannot be
76
             * compiled, but this plugin works (?)
77
             * 
78
            if (!checkJDK()) {
79
                return;
80
            }
81
            */
82

    
83
//            if (!checkPlatformProperties()) {
84
//                return;
85
//            }
86
            
87
        // ==========================================================
88
                // TODO: add support to parallel executions in Andami??
89

    
90
        // Create messages console
91
            CreatePluginConsoleWindow console = null;
92

    
93
        if (console == null) {
94
            try {
95
                console = new CreatePluginConsoleWindow();
96
            } catch (IOException e) {
97
                NotificationManager.addWarning("Error creating the console", e);
98
            }
99
        }
100
        if (console != null) {
101
            PluginServices.getMDIManager().addWindow(console, GridBagConstraints.LAST_LINE_START);
102
        }
103

    
104
                try {
105
                        // Disable extension so it can't be executed again.
106
                        synchronized (lock) {
107
                                enabled = false;
108
                        }
109
                        new CreatePluginThread(console).start();
110
                } finally {
111
                        synchronized (lock) {
112
                                // create plugin process finish. Enable extension.
113
                                enabled = true;
114
                        }
115
                }
116
        }
117

    
118
        /**
119
         * Checks existence of file:
120
         * 
121
         * USER_HOME/.gvsig.platform.properties
122
         * 
123
         * If not present, show text area with suggested content.
124
         * 
125
     * @return whether the mentioned file is present or the
126
     * user has created it here and accepts to continue.
127
     *  
128
     */
129
    private boolean checkPlatformProperties() {
130
        
131
        String user_home = System.getProperty("user.home");
132
        File the_file = new File(
133
            user_home +
134
            File.separator +
135
            GVSIG_PLATFORM_PROPERTIES_FILE_NAME);
136

    
137
        Component parent_window = getActiveWindowComponent();
138

    
139
        try {
140
            if (the_file.exists()) {
141
                // ok file exists
142
                LOG.info("Found platform properties file: "
143
                    + the_file.getAbsolutePath());
144
                return true;
145
            }
146
            
147
            boolean is_windows = isThisWindows();
148
            PlatformPropertiesWindow ppw = new PlatformPropertiesWindow(
149
                the_file, is_windows);
150
            PluginServices.getMDIManager().addCentredWindow(ppw);
151
            // it's a modal dialog
152
            return ppw.validSettings();
153
            
154
        } catch (Exception ex) {
155
            
156
            String _msg =
157
                Messages.getText("_Error_while_checking_platform_properties")
158
                + "\n\n[ " + ex.getMessage() + " ]";
159
            String _tit = Messages.getText(
160
                "_Checking_platform_properties");
161
            JOptionPane.showMessageDialog(
162
                parent_window,
163
                _msg, _tit,
164
                JOptionPane.ERROR_MESSAGE);
165
        }
166
        return false;
167
        
168
    }
169

    
170

    
171
    /**
172
     * @return whether we are running on a windows system
173
     */
174
    private boolean isThisWindows() {
175
        String os = System.getProperty("os.name").toLowerCase();
176
        return (os.indexOf("win") >= 0);
177
    }
178

    
179
    /**
180
     * Check and possibly show JDK and JAVA_HOME settings and
181
     * prompt user with suggestion.
182
     * 
183
     * @return whether JDK is detected or user accepts to
184
     * continue anyway
185
     */
186
    private boolean checkJDK() {
187
        
188
        boolean this_is_jdk = false;
189
        try {
190
            this_is_jdk = jdkAvailable();
191
        } catch (Exception ex) {
192
            LOG.info("While searching for JDK : " + ex.getMessage(), ex);
193
            this_is_jdk = false;
194
        }
195
        
196
        if (this_is_jdk) {
197
            return true;
198
        }
199
        
200
        String env_j_h = System.getenv("JAVA_HOME");
201
        
202
        String jdk_str = null;
203
        if (this_is_jdk) {
204
            jdk_str = Messages.getText("_gvSIG_is_running_on_JDK");
205
        } else {
206
            jdk_str = Messages.getText("_gvSIG_is_not_running_on_JDK");
207
        }
208
        
209
        String jh_str = null;
210
        if (env_j_h == null) {
211
            jh_str = Messages.getText("_JAVA_HOME_is_not_set");
212
        } else {
213
            jh_str = Messages.getText("_JAVA_HOME_is_set_to")
214
                + " '" + env_j_h + "'";
215
        }
216
        
217
        String recomm = "";
218
        if (true) { // !this_is_jdk && env_j_h == null) {
219
            recomm = "\n\n"
220
                + Messages.getText("_Requirements_for_this_plugin") + ":\n\n"
221
                + "- " + Messages.getText("_On_Linux_make_JAVA_HOME_point_to_JDK_1_5_higher");
222
            recomm = recomm + ".    \n- "
223
            + Messages.getText("_On_Windows_run_JDK_use_devel_exe") + ".";
224
        }
225
        
226
        String tot_msg = Messages.getText("_Detected_settings") + ":"
227
            + "\n\n"
228
            + "- " + jdk_str + "\n"
229
            + "- " + jh_str + "     " + recomm + "\n\n"
230
                + Messages.getText("_Continue_question");
231
            
232
        int opt = JOptionPane.showConfirmDialog(
233
            null, // getActiveWindowComponent(),
234
            tot_msg,
235
            Messages.getText("_Make_maven_project_plugin") + " - " + 
236
            Messages.getText("_Searching_JDK"),
237
            JOptionPane.YES_NO_OPTION,
238
            (recomm.length() == 0) ?
239
                JOptionPane.INFORMATION_MESSAGE : JOptionPane.WARNING_MESSAGE
240
            );
241
        
242
        return (opt == JOptionPane.YES_OPTION);
243
    }
244

    
245
    /**
246
     * @return
247
     */
248
    private Component getActiveWindowComponent() {
249
        
250
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
251
        if (iw instanceof Component) {
252
            return (Component) iw;
253
        } else {
254
            return null;
255
        }
256
    }
257

    
258

    
259
    private boolean jdkAvailable() throws Exception {
260
        
261
        ClassLoader loader = this.getClass().getClassLoader();
262
        URL class_file_url = loader.getResource("compilationcheck/DummyClass.class");
263
        File class_file = null;
264
        
265
        // deleting compiled test file if existed
266
        if (class_file_url != null) {
267
            class_file = new File(class_file_url.getFile());
268
            if (class_file.exists()) {
269
                class_file.delete();
270
            }
271
        }
272
        
273
        URL build = loader.getResource("scripts/" + ANT_CHECK_JDK);
274

    
275
        String execut = null;
276
        /*
277
        // first attempt: javac from JAVA_HOME
278
        execut =
279
            System.getenv().get("JAVA_HOME")
280
            + File.separator + "bin" + File.separator + "javac";
281
        
282
        if (execut != null) {
283
            this.runScript(build, "default", null, "executable", execut);
284
            class_file_url = loader.getResource("compilationcheck/DummyClass.class");
285
            if (class_file_url != null) {
286
                // OK, test file is not compiled
287
                return true;
288
            }
289
        }
290
        */
291
        
292
        // second attempt: java.home (jre)
293
        execut = System.getProperty("java.home");
294
        int ind = execut.lastIndexOf(File.separator);
295
        // one up, then bin/javac
296
        execut = execut.substring(0, ind)
297
            + File.separator
298
            + "bin" + File.separator + "javac";
299
        
300
        this.runScript(build, "default", null, "executable", execut);
301
        class_file_url = loader.getResource("compilationcheck/DummyClass.class");
302
        // if test file not compiled, then false
303
        return (class_file_url != null);
304
    }
305

    
306
    /**
307
         * Thread to launch the ant base plugin creation project.
308
         * 
309
         * @author gvSIG Team
310
         * @version $Id$
311
         */
312
        private final class CreatePluginThread extends Thread {
313

    
314
                private final CreatePluginConsoleWindow console;
315

    
316
                /**
317
                 * Constructor.
318
                 * 
319
                 * @param console
320
                 */
321
                public CreatePluginThread(CreatePluginConsoleWindow console) {
322
                        this.console = console;
323
                        setName("Create plugin execution thread");
324
                        setDaemon(true);
325
                }
326

    
327
                @Override
328
                public void run() {
329
                        DefaultLogger log = new DefaultLogger();
330
                        log.setMessageOutputLevel(Project.MSG_INFO);
331

    
332
                        if (console != null) {
333
                                log.setErrorPrintStream(console.getErrorPrintStream());
334
                                log.setOutputPrintStream(console.getPrintStream());
335
                        } else {
336
                                LOG.warn("Console window not available, will use the default console for Ant");
337
                                log.setErrorPrintStream(System.err);
338
                                log.setOutputPrintStream(System.out);
339
                        }
340

    
341
                        
342
                        
343
            ClassLoader loader = this.getClass().getClassLoader();
344
            URL build = loader.getResource("scripts/" + ANT_BUILD_FILE);
345
            runScript(build, ANT_TARGET, log);
346
                }
347
        }
348

    
349

    
350
        private void runScript(
351
                URL build,
352
                String target,
353
                BuildListener blistener) {
354
            
355
        runScript(build, target, blistener, null, null);
356
            
357
        }
358

    
359
        private void runScript(
360
            URL build,
361
            String target,
362
            BuildListener blistener,
363
            String prop, String val) {
364
            
365
            LOG.info("Starting runScript:\n"
366
                + "    build = " + build.toString() + "\n"
367
                + "    property name = " + prop + "\n"
368
            + "    property value = " + val);
369
                
370

    
371
        File file = new File(build.getFile());
372

    
373
        final Project ant = new Project();
374
        if (blistener != null) {
375
            ant.addBuildListener(blistener);
376
        }
377
        ant.setUserProperty("ant.file", file.getAbsolutePath());
378
        
379
        if (prop != null && val != null) {
380
            ant.setUserProperty(prop, val);
381
        }
382
        
383
        ant.init();
384
        ProjectHelper.getProjectHelper().parse(ant, file);
385

    
386
        LOG.info("Starting ant task with the file {} and the target {}",
387
                file, target);
388
        ant.executeTarget(target);
389
        }
390

    
391
        public void initialize() {
392
                // Nothing to do
393
        }
394

    
395
        public boolean isEnabled() {
396
                return enabled;
397
        }
398

    
399
        public boolean isVisible() {
400
                return true;
401
        }
402
        
403

    
404
}