Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / org.gvsig.mkmvnproject / src / main / java / org / gvsig / mkmvnproject / MakeMavenProjectExtension.java @ 39330

History | View | Annotate | Download (11.7 KB)

1
/* 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
package org.gvsig.mkmvnproject;
23

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

    
30
import javax.swing.JOptionPane;
31

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

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

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

    
55
    private static final String ANT_BUILD_FILE = "mkmvnproject.xml";
56
    private static final String ANT_CHECK_JDK = "checkjdk.xml";
57

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

    
63
        private static final Logger LOG = LoggerFactory
64
                        .getLogger(MakeMavenProjectExtension.class);
65

    
66
        private boolean enabled = true;
67

    
68
        private final Object lock = new Object();
69

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

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

    
88
        // Create messages console
89
            CreatePluginConsoleWindow console = null;
90

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

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

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

    
135
        Component parent_window = getActiveWindowComponent();
136

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

    
168

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

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

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

    
256

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

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

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

    
312
                private final CreatePluginConsoleWindow console;
313

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

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

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

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

    
347

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

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

    
369
        File file = new File(build.getFile());
370

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

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

    
389
        public void initialize() {
390
                // Nothing to do
391
        }
392

    
393
        public boolean isEnabled() {
394
                return enabled;
395
        }
396

    
397
        public boolean isVisible() {
398
                return true;
399
        }
400
        
401

    
402
}