Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.mkmvnproject / src / main / java / org / gvsig / mkmvnproject / MakeMavenProjectExtension.java @ 38543

History | View | Annotate | Download (11.4 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
            if (!checkJDK()) {
73
                return;
74
            }
75

    
76
            if (!checkPlatformProperties()) {
77
                return;
78
            }
79
            
80
        // ==========================================================
81
                // TODO: add support to parallel executions in Andami??
82

    
83
        // Create messages console
84
            CreatePluginConsoleWindow console = null;
85

    
86
        if (console == null) {
87
            try {
88
                console = new CreatePluginConsoleWindow();
89
            } catch (IOException e) {
90
                NotificationManager.addWarning("Error creating the console", e);
91
            }
92
        }
93
        if (console != null) {
94
            PluginServices.getMDIManager().addWindow(console, GridBagConstraints.LAST_LINE_START);
95
        }
96

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

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

    
130
        Component parent_window = getActiveWindowComponent();
131

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

    
163

    
164
    /**
165
     * @return whether we are running on a windows system
166
     */
167
    private boolean isThisWindows() {
168
        String os = System.getProperty("os.name").toLowerCase();
169
        return (os.indexOf("win") >= 0);
170
    }
171

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

    
238
    /**
239
     * @return
240
     */
241
    private Component getActiveWindowComponent() {
242
        
243
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
244
        if (iw instanceof Component) {
245
            return (Component) iw;
246
        } else {
247
            return null;
248
        }
249
    }
250

    
251

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

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

    
299
    /**
300
         * Thread to launch the ant base plugin creation project.
301
         * 
302
         * @author gvSIG Team
303
         * @version $Id$
304
         */
305
        private final class CreatePluginThread extends Thread {
306

    
307
                private final CreatePluginConsoleWindow console;
308

    
309
                /**
310
                 * Constructor.
311
                 * 
312
                 * @param console
313
                 */
314
                public CreatePluginThread(CreatePluginConsoleWindow console) {
315
                        this.console = console;
316
                        setName("Create plugin execution thread");
317
                        setDaemon(true);
318
                }
319

    
320
                @Override
321
                public void run() {
322
                        DefaultLogger log = new DefaultLogger();
323
                        log.setMessageOutputLevel(Project.MSG_INFO);
324

    
325
                        if (console != null) {
326
                                log.setErrorPrintStream(console.getErrorPrintStream());
327
                                log.setOutputPrintStream(console.getPrintStream());
328
                        } else {
329
                                LOG.warn("Console window not available, will use the default console for Ant");
330
                                log.setErrorPrintStream(System.err);
331
                                log.setOutputPrintStream(System.out);
332
                        }
333

    
334
                        
335
                        
336
            ClassLoader loader = this.getClass().getClassLoader();
337
            URL build = loader.getResource("scripts/" + ANT_BUILD_FILE);
338
            runScript(build, ANT_TARGET, log);
339
                }
340
        }
341

    
342

    
343
        private void runScript(
344
                URL build,
345
                String target,
346
                BuildListener blistener) {
347
            
348
            runScript(build, target, blistener, null, null);
349
        }
350

    
351
        private void runScript(
352
            URL build,
353
            String target,
354
            BuildListener blistener,
355
            String prop, String val) {
356

    
357
        File file = new File(build.getFile());
358

    
359
        final Project ant = new Project();
360
        if (blistener != null) {
361
            ant.addBuildListener(blistener);
362
        }
363
        ant.setUserProperty("ant.file", file.getAbsolutePath());
364
        
365
        if (prop != null && val != null) {
366
            ant.setUserProperty(prop, val);
367
        }
368
        
369
        ant.init();
370
        ProjectHelper.getProjectHelper().parse(ant, file);
371

    
372
        LOG.info("Starting ant task with the file {} and the target {}",
373
                file, target);
374
        ant.executeTarget(target);
375
        }
376

    
377
        public void initialize() {
378
                // Nothing to do
379
        }
380

    
381
        public boolean isEnabled() {
382
                return enabled;
383
        }
384

    
385
        public boolean isVisible() {
386
                return true;
387
        }
388
        
389

    
390
}