Revision 34125

View differences:

branches/v2_0_0_prep/extensions/org.gvsig.mkmvnproject/src/main/java/org/gvsig/mkmvnproject/CreatePluginConsolePanel.java
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.BorderLayout;
25
import java.awt.Color;
26
import java.awt.Dimension;
27
import java.io.BufferedReader;
28
import java.io.IOException;
29
import java.io.InputStreamReader;
30
import java.io.PipedInputStream;
31
import java.io.PipedOutputStream;
32
import java.io.PrintStream;
33

  
34
import javax.swing.JPanel;
35
import javax.swing.JScrollPane;
36
import javax.swing.JTextArea;
37
import javax.swing.SwingUtilities;
38

  
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

  
42
/**
43
 * A panel which shows the execution log messages of the create plugin process.
44
 * 
45
 * @author gvSIG Team
46
 * @version $Id$
47
 */
48
public class CreatePluginConsolePanel extends JPanel {
49

  
50
	private static final long serialVersionUID = -7213048219847956909L;
51

  
52
	private static final Logger LOG = LoggerFactory
53
			.getLogger(CreatePluginConsolePanel.class);
54

  
55
	private JTextArea console;
56

  
57
	private PrintStream printStream;
58
	private PipedOutputStream pipedos;
59
	private PipedInputStream pipedis;
60
	private Thread consoleThread;
61

  
62
	/**
63
	 * Constructor.
64
	 * 
65
	 * @throws IOException
66
	 *             if there is an error creating the console streams
67
	 */
68
	public CreatePluginConsolePanel() throws IOException {
69
		super();
70
		initialize();
71
	}
72

  
73
	/**
74
	 * Constructor.
75
	 * 
76
	 * @param isDoubleBuffered
77
	 *            if the panel must be double buffered.
78
	 * @throws IOException
79
	 *             if there is an error creating the console streams
80
	 */
81
	public CreatePluginConsolePanel(boolean isDoubleBuffered)
82
			throws IOException {
83
		super(isDoubleBuffered);
84
		initialize();
85
	}
86

  
87
	/**
88
	 * Initializes the panel GUI.
89
	 * 
90
	 * @throws IOException
91
	 */
92
	private void initialize() throws IOException {
93
		Dimension dimension = new Dimension(600, 200);
94
		setSize(dimension);
95
		setLayout(new BorderLayout());
96

  
97
		console = new JTextArea();
98
		console.setEditable(false);
99
		// console.setColumns(20);
100
		// console.setRows(5);
101
		console.setBackground(Color.WHITE);
102
		console.setLineWrap(true);
103
		console.setWrapStyleWord(true);
104

  
105
		JScrollPane consoleScrollPane = new JScrollPane(console);
106
		add(consoleScrollPane, BorderLayout.CENTER);
107

  
108
		pipedis = new PipedInputStream();
109

  
110
		pipedos = new PipedOutputStream(pipedis);
111

  
112
		printStream = new PrintStream(pipedos);
113
		consoleThread = new ConsoleThread(pipedis, console);
114
		consoleThread.start();
115
	}
116

  
117
	/**
118
	 * Returns a {@link PrintStream} which allows to write messages to the
119
	 * console.
120
	 * 
121
	 * @return a {@link PrintStream} which allows to write messages to the
122
	 *         console
123
	 */
124
	public PrintStream getPrintStream() {
125
		return printStream;
126
	}
127

  
128
	/**
129
	 * Returns a {@link PrintStream} which allows to write error messages to the
130
	 * console.
131
	 * 
132
	 * @return a {@link PrintStream} which allows to write error messages to the
133
	 *         console
134
	 */
135
	public PrintStream getErrorPrintStream() {
136
		return getPrintStream();
137
	}
138

  
139
	/**
140
	 * Closes the console. Once this method is called, any more calls to the
141
	 * console {@link PrintStream} will throw an exception.
142
	 */
143
	public void closeConsole() {
144
		printStream.flush();
145
		printStream.close();
146
		try {
147
			pipedos.close();
148
			pipedis.close();
149
			// Wait for the thread to finish
150
			consoleThread.join(500);
151
		} catch (IOException e) {
152
			LOG.warn("Error closing the internal piped streams", e);
153
		} catch (InterruptedException e) {
154
			// Nothing special to do
155
			LOG.debug("Console thread interrupted while waiting to finish", e);
156
		}
157
	}
158

  
159
	private static final class ConsoleThread extends Thread {
160

  
161
		private final PipedInputStream pipedis;
162
		private final JTextArea console;
163

  
164
		/**
165
		 * Constructor.
166
		 */
167
		public ConsoleThread(PipedInputStream pipedis, JTextArea console) {
168
			super("Create plugin console update thread");
169
			setDaemon(true);
170
			this.pipedis = pipedis;
171
			this.console = console;
172
		}
173

  
174
		@Override
175
		public void run() {
176
			try {
177
				BufferedReader reader = new BufferedReader(
178
						new InputStreamReader(pipedis));
179
				String line;
180
				while ((line = reader.readLine()) != null) {
181
					SwingUtilities.invokeLater(new ConsoleUpdateRunnable(
182
							console, line));
183
				}
184
				LOG.debug("Console input stream end, finish reading");
185
				reader.close();
186
			} catch (IOException e) {
187
				LOG.error("Error reading from the console string", e);
188
			}
189
		}
190

  
191
	}
192

  
193
	/**
194
	 * Runnable used to update the console text field.
195
	 * 
196
	 * @author gvSIG Team
197
	 * @version $Id$
198
	 */
199
	private static final class ConsoleUpdateRunnable implements Runnable {
200
		private final String newLine;
201
		private final JTextArea console;
202

  
203
		/**
204
		 * Constructor.
205
		 */
206
		public ConsoleUpdateRunnable(JTextArea console, String newLine) {
207
			this.console = console;
208
			this.newLine = newLine;
209
		}
210

  
211
		public void run() {
212
			console.append(newLine);
213
			console.append("\n");
214
		}
215
	}
216
}
branches/v2_0_0_prep/extensions/org.gvsig.mkmvnproject/src/main/java/org/gvsig/mkmvnproject/MakeMavenProjectExtension.java
22 22
package org.gvsig.mkmvnproject;
23 23

  
24 24
import java.io.File;
25
import java.io.IOException;
25 26
import java.net.URL;
26 27

  
27 28
import org.apache.tools.ant.DefaultLogger;
28 29
import org.apache.tools.ant.Project;
29 30
import org.apache.tools.ant.ProjectHelper;
30
import org.gvsig.andami.plugins.Extension;
31 31
import org.slf4j.Logger;
32 32
import org.slf4j.LoggerFactory;
33 33

  
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.andami.plugins.Extension;
37

  
34 38
/**
35 39
 * Extension to launch the project creation from templates.
36 40
 * 
......
46 50
	private static final Logger LOG = LoggerFactory
47 51
			.getLogger(MakeMavenProjectExtension.class);
48 52

  
53
	private boolean enabled = true;
54

  
55
	private final Object lock = new Object();
56

  
49 57
	public void execute(String actionCommand) {
50
		ClassLoader loader = this.getClass().getClassLoader();
51
		URL build = loader.getResource("scripts/" + ANT_BUILD_FILE);
52
		File file = new File(build.getFile());
58
		// TODO: add support to parallel executions in Andami??
53 59

  
54
		DefaultLogger log = new DefaultLogger();
55
		log.setErrorPrintStream(System.err);
56
		log.setOutputPrintStream(System.out);
57
		log.setMessageOutputLevel(Project.MSG_INFO);
60
		// Create messages console
61
		CreatePluginConsoleWindow console = null;
62
		try {
63
			console = new CreatePluginConsoleWindow();
64
			PluginServices.getMDIManager().addCentredWindow(console);
65
		} catch (IOException e) {
66
			NotificationManager.addWarning("Error creating the console", e);
67
		}
58 68

  
59
		Project ant = new Project();
60
		ant.addBuildListener(log);
61
		ant.setUserProperty("ant.file", file.getAbsolutePath());
62
		ant.init();
63
		ProjectHelper.getProjectHelper().parse(ant, file);
69
		try {
70
			// Disable extension so it can't be executed again.
71
			synchronized (lock) {
72
				enabled = false;
73
			}
74
			new CreatePluginThread(console).start();
75
		} finally {
76
			synchronized (lock) {
77
				// create plugin process finish. Enable extension.
78
				enabled = true;
79
			}
80
		}
81
	}
64 82

  
65
		LOG.info("Starting ant task with the file {} and the target {}", file,
66
				ANT_TARGET);
67
		ant.executeTarget(ANT_TARGET);
83
	/**
84
	 * Thread to launch the ant base plugin creation project.
85
	 * 
86
	 * @author gvSIG Team
87
	 * @version $Id$
88
	 */
89
	private static final class CreatePluginThread extends Thread {
90

  
91
		private final CreatePluginConsoleWindow console;
92

  
93
		/**
94
		 * Constructor.
95
		 * 
96
		 * @param console
97
		 */
98
		public CreatePluginThread(CreatePluginConsoleWindow console) {
99
			this.console = console;
100
			setName("Create plugin execution thread");
101
			setDaemon(true);
102
		}
103

  
104
		@Override
105
		public void run() {
106
			DefaultLogger log = new DefaultLogger();
107
			log.setMessageOutputLevel(Project.MSG_INFO);
108

  
109
			if (console != null) {
110
				log.setErrorPrintStream(console.getErrorPrintStream());
111
				log.setOutputPrintStream(console.getPrintStream());
112
			} else {
113
				LOG.warn("Console window not available, will use the default console for Ant");
114
				log.setErrorPrintStream(System.err);
115
				log.setOutputPrintStream(System.out);
116
			}
117

  
118
			ClassLoader loader = this.getClass().getClassLoader();
119
			URL build = loader.getResource("scripts/" + ANT_BUILD_FILE);
120
			File file = new File(build.getFile());
121

  
122
			final Project ant = new Project();
123
			ant.addBuildListener(log);
124
			ant.setUserProperty("ant.file", file.getAbsolutePath());
125
			ant.init();
126
			ProjectHelper.getProjectHelper().parse(ant, file);
127

  
128
			LOG.info("Starting ant task with the file {} and the target {}",
129
					file, ANT_TARGET);
130
			ant.executeTarget(ANT_TARGET);
131
		}
68 132
	}
69 133

  
70 134
	public void initialize() {
......
72 136
	}
73 137

  
74 138
	public boolean isEnabled() {
75
		return true;
139
		return enabled;
76 140
	}
77 141

  
78 142
	public boolean isVisible() {
branches/v2_0_0_prep/extensions/org.gvsig.mkmvnproject/src/main/java/org/gvsig/mkmvnproject/CreatePluginConsoleWindow.java
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.Dimension;
25
import java.io.IOException;
26

  
27
import org.gvsig.andami.ui.mdiManager.IWindow;
28
import org.gvsig.andami.ui.mdiManager.IWindowListener;
29
import org.gvsig.andami.ui.mdiManager.WindowInfo;
30

  
31
/**
32
 * A gvSIG window which shows the execution log messages of the create plugin
33
 * process.
34
 * 
35
 * @author gvSIG Team
36
 * @version $Id$
37
 */
38
public class CreatePluginConsoleWindow extends CreatePluginConsolePanel
39
		implements IWindow, IWindowListener {
40

  
41
	private static final long serialVersionUID = -5589080107545290284L;
42

  
43
	private WindowInfo info;
44

  
45
	private Object profile = WindowInfo.EDITOR_PROFILE;
46

  
47
	/**
48
	 * Constructor.
49
	 * 
50
	 * @throws IOException
51
	 *             if there is an error creating the console streams
52
	 */
53
	public CreatePluginConsoleWindow() throws IOException {
54
		super();
55
		initializeWindow();
56
	}
57

  
58
	/**
59
	 * Constructor.
60
	 * 
61
	 * @param isDoubleBuffered
62
	 *            if the panel must be double buffered.
63
	 * @throws IOException
64
	 *             if there is an error creating the console streams
65
	 */
66
	public CreatePluginConsoleWindow(boolean isDoubleBuffered) throws IOException {
67
		super(isDoubleBuffered);
68
		initializeWindow();
69
	}
70

  
71
	/**
72
	 * Initializes the window.
73
	 */
74
	private void initializeWindow() {
75
		Dimension dimension = new Dimension(600, 300);
76
		setSize(dimension);
77
		int code = WindowInfo.ICONIFIABLE | WindowInfo.MAXIMIZABLE
78
				| WindowInfo.RESIZABLE | WindowInfo.MODELESSDIALOG;
79
		profile = WindowInfo.EDITOR_PROFILE;
80
		info = new WindowInfo(code);
81
		info.setTitle("Console");
82
		info.setMinimumSize(dimension);
83
	}
84

  
85
	public WindowInfo getWindowInfo() {
86
		return info;
87
	}
88

  
89
	public Object getWindowProfile() {
90
		return profile;
91
	}
92

  
93
	public void windowActivated() {
94
		// Nothing to do
95
	}
96

  
97
	public void windowClosed() {
98
		super.closeConsole();
99
	}
100

  
101
}

Also available in: Unified diff