Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.mkmvnproject / src / main / java / org / gvsig / mkmvnproject / MakeMavenProjectExtension.java @ 38429

History | View | Annotate | Download (7.67 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
            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
            
92
                // TODO: add support to parallel executions in Andami??
93

    
94
        // Create messages console
95
            CreatePluginConsoleWindow console = null;
96

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

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

    
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
    /**
184
         * Thread to launch the ant base plugin creation project.
185
         * 
186
         * @author gvSIG Team
187
         * @version $Id$
188
         */
189
        private final class CreatePluginThread extends Thread {
190

    
191
                private final CreatePluginConsoleWindow console;
192

    
193
                /**
194
                 * Constructor.
195
                 * 
196
                 * @param console
197
                 */
198
                public CreatePluginThread(CreatePluginConsoleWindow console) {
199
                        this.console = console;
200
                        setName("Create plugin execution thread");
201
                        setDaemon(true);
202
                }
203

    
204
                @Override
205
                public void run() {
206
                        DefaultLogger log = new DefaultLogger();
207
                        log.setMessageOutputLevel(Project.MSG_INFO);
208

    
209
                        if (console != null) {
210
                                log.setErrorPrintStream(console.getErrorPrintStream());
211
                                log.setOutputPrintStream(console.getPrintStream());
212
                        } else {
213
                                LOG.warn("Console window not available, will use the default console for Ant");
214
                                log.setErrorPrintStream(System.err);
215
                                log.setOutputPrintStream(System.out);
216
                        }
217

    
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
        }
225

    
226

    
227
        private void runScript(
228
                URL build,
229
                String target,
230
                BuildListener blistener) {
231
            
232
            runScript(build, target, blistener, null, null);
233
        }
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

    
261
        public void initialize() {
262
                // Nothing to do
263
        }
264

    
265
        public boolean isEnabled() {
266
                return enabled;
267
        }
268

    
269
        public boolean isVisible() {
270
                return true;
271
        }
272
        
273

    
274
}