Execution time reported by TestNG reflect the time it took to run the query inside of a browser
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.mojo;
20 import org.apache.maven.plugin.AbstractMojo;
23 import java.io.IOException;
24 import java.net.MalformedURLException;
25 import java.net.URISyntaxException;
27 import java.net.URLClassLoader;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import org.apache.maven.artifact.Artifact;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugins.annotations.LifecyclePhase;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.apache.maven.project.MavenProject;
39 import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
41 /** Executes given HTML page in a browser. */
42 @Mojo(name="brwsr", defaultPhase=LifecyclePhase.DEPLOY)
43 public class BrswrMojo extends AbstractMojo {
46 /** Resource to show as initial page */
48 private String startpage;
50 @Parameter(defaultValue="${project}")
51 private MavenProject prj;
53 /** Root of the class files */
54 @Parameter(defaultValue="${project.build.directory}/classes")
58 public void execute() throws MojoExecutionException {
59 if (startpage == null) {
60 throw new MojoExecutionException("You have to provide a start page");
64 URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
66 Bck2BrwsrLauncher httpServer = new Bck2BrwsrLauncher();
67 httpServer.addClassLoader(url);
69 httpServer.showURL(startpage);
70 } catch (Exception ex) {
71 throw new MojoExecutionException("Can't open " + startpage, ex);
75 } catch (IOException ex) {
76 throw new MojoExecutionException("Can't show the browser", ex);
80 private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
81 if (!dir.isDirectory()) {
82 throw new MojoExecutionException("Not a directory " + dir);
84 File[] arr = dir.listFiles();
85 if (arr.length == 1 && arr[0].isDirectory()) {
86 return findNonEmptyFolder(arr[0]);
91 private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
92 File[] files = toCheck.listFiles();
95 for (File f : files) {
96 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
97 if (newest < lastModified) {
98 newest = lastModified;
102 } else if (toCheck.getName().endsWith(".class")) {
103 arr.add(prefix.substring(0, prefix.length() - 7));
104 return toCheck.lastModified();
110 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
111 List<URL> arr = new ArrayList<URL>();
112 arr.add(root.toURI().toURL());
113 for (Artifact a : deps) {
114 arr.add(a.getFile().toURI().toURL());
116 return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());