Statistics
| Revision:

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

History | View | Annotate | Download (8.94 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

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

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

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

    
58
        private static final Logger LOG = LoggerFactory
59
                        .getLogger(MakeMavenProjectExtension.class);
60

    
61
        private boolean enabled = true;
62

    
63
        private final Object lock = new Object();
64

    
65
        public void execute(String actionCommand) {
66
            
67
            if (!doJdkCheck()) {
68
                return;
69
            }
70
            
71
        // ==========================================================
72
                // TODO: add support to parallel executions in Andami??
73

    
74
        // Create messages console
75
            CreatePluginConsoleWindow console = null;
76

    
77
        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
                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

    
102
        /**
103
     * 
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
        if (!this_is_jdk && env_j_h == null) {
138
            recomm = "\n\n" + Messages.getText("_You_might_need_to_run_gvSIG_on_JDK_or_set_JAVA_HOME_properly");
139
            recomm = recomm + ".    \n"
140
            + Messages.getText("_Visit_gvSIG_website_for_details") + ".";
141
        }
142
        
143
        String tot_msg = Messages.getText("_Detected_settings") + ":"
144
            + "\n\n"
145
            + "- " + jdk_str + "\n"
146
            + "- " + jh_str + "     " + recomm + "\n\n"
147
                + Messages.getText("_Continue_question");
148
            
149
        int opt = JOptionPane.showConfirmDialog(
150
            null, // getActiveWindowComponent(),
151
            tot_msg,
152
            Messages.getText("_Searching_JDK"),
153
            JOptionPane.YES_NO_OPTION,
154
            (recomm.length() == 0) ?
155
                JOptionPane.INFORMATION_MESSAGE : JOptionPane.WARNING_MESSAGE
156
            );
157
        
158
        return (opt == JOptionPane.YES_OPTION);
159
    }
160

    
161
    /**
162
     * @return
163
     */
164
    private Component getActiveWindowComponent() {
165
        
166
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
167
        if (iw instanceof Component) {
168
            return (Component) iw;
169
        } else {
170
            return null;
171
        }
172
    }
173

    
174

    
175
    private boolean jdkAvailable() throws Exception {
176
        
177
        ClassLoader loader = this.getClass().getClassLoader();
178
        URL class_file_url = loader.getResource("compilationcheck/DummyClass.class");
179
        File class_file = null;
180
        
181
        // deleting compiled test file if existed
182
        if (class_file_url != null) {
183
            class_file = new File(class_file_url.getFile());
184
            if (class_file.exists()) {
185
                class_file.delete();
186
            }
187
        }
188
        
189
        URL build = loader.getResource("scripts/" + ANT_CHECK_JDK);
190

    
191
        String execut = null;
192
        /*
193
        // first attempt: javac from JAVA_HOME
194
        execut =
195
            System.getenv().get("JAVA_HOME")
196
            + File.separator + "bin" + File.separator + "javac";
197
        
198
        if (execut != null) {
199
            this.runScript(build, "default", null, "executable", execut);
200
            class_file_url = loader.getResource("compilationcheck/DummyClass.class");
201
            if (class_file_url != null) {
202
                // OK, test file is not compiled
203
                return true;
204
            }
205
        }
206
        */
207
        
208
        // second attempt: java.home (jre)
209
        execut = System.getProperty("java.home");
210
        int ind = execut.lastIndexOf(File.separator);
211
        // one up, then bin/javac
212
        execut = execut.substring(0, ind)
213
            + File.separator
214
            + "bin" + File.separator + "javac";
215
        
216
        this.runScript(build, "default", null, "executable", execut);
217
        class_file_url = loader.getResource("compilationcheck/DummyClass.class");
218
        // if test file not compiled, then false
219
        return (class_file_url != null);
220
    }
221

    
222
    /**
223
         * Thread to launch the ant base plugin creation project.
224
         * 
225
         * @author gvSIG Team
226
         * @version $Id$
227
         */
228
        private final class CreatePluginThread extends Thread {
229

    
230
                private final CreatePluginConsoleWindow console;
231

    
232
                /**
233
                 * Constructor.
234
                 * 
235
                 * @param console
236
                 */
237
                public CreatePluginThread(CreatePluginConsoleWindow console) {
238
                        this.console = console;
239
                        setName("Create plugin execution thread");
240
                        setDaemon(true);
241
                }
242

    
243
                @Override
244
                public void run() {
245
                        DefaultLogger log = new DefaultLogger();
246
                        log.setMessageOutputLevel(Project.MSG_INFO);
247

    
248
                        if (console != null) {
249
                                log.setErrorPrintStream(console.getErrorPrintStream());
250
                                log.setOutputPrintStream(console.getPrintStream());
251
                        } else {
252
                                LOG.warn("Console window not available, will use the default console for Ant");
253
                                log.setErrorPrintStream(System.err);
254
                                log.setOutputPrintStream(System.out);
255
                        }
256

    
257
                        
258
                        
259
            ClassLoader loader = this.getClass().getClassLoader();
260
            URL build = loader.getResource("scripts/" + ANT_BUILD_FILE);
261
            runScript(build, ANT_TARGET, log);
262
                }
263
        }
264

    
265

    
266
        private void runScript(
267
                URL build,
268
                String target,
269
                BuildListener blistener) {
270
            
271
            runScript(build, target, blistener, null, null);
272
        }
273

    
274
        private void runScript(
275
            URL build,
276
            String target,
277
            BuildListener blistener,
278
            String prop, String val) {
279

    
280
        File file = new File(build.getFile());
281

    
282
        final Project ant = new Project();
283
        if (blistener != null) {
284
            ant.addBuildListener(blistener);
285
        }
286
        ant.setUserProperty("ant.file", file.getAbsolutePath());
287
        
288
        if (prop != null && val != null) {
289
            ant.setUserProperty(prop, val);
290
        }
291
        
292
        ant.init();
293
        ProjectHelper.getProjectHelper().parse(ant, file);
294

    
295
        LOG.info("Starting ant task with the file {} and the target {}",
296
                file, target);
297
        ant.executeTarget(target);
298
        }
299

    
300
        public void initialize() {
301
                // Nothing to do
302
        }
303

    
304
        public boolean isEnabled() {
305
                return enabled;
306
        }
307

    
308
        public boolean isVisible() {
309
                return true;
310
        }
311
        
312

    
313
}