Skip to content

Commit 820ad3e

Browse files
committed
Improved watch goal (still having problems with generated repo)
1 parent 583a91e commit 820ad3e

File tree

6 files changed

+135
-151
lines changed

6 files changed

+135
-151
lines changed

src/license/THIRD-PARTY.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# Please fill the missing licenses for dependencies :
1414
#
1515
#
16-
#Wed Nov 29 15:23:13 CET 2017
16+
#Wed Nov 29 22:13:09 CET 2017
1717
classworlds--classworlds--1.1-alpha-2=
1818
commons-collections--commons-collections--3.1=
1919
dom4j--dom4j--1.6.1=

src/main/java/org/seedstack/maven/SeedStackUtils.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.seedstack.maven;
1010

11+
import java.lang.reflect.InvocationTargetException;
1112
import java.lang.reflect.Method;
1213
import org.apache.maven.plugin.MojoExecutionException;
1314

@@ -34,7 +35,7 @@ public static Object getSeedLauncher() throws Exception {
3435
}
3536
return getLauncherMethod.invoke(null);
3637
} catch (Exception e) {
37-
throw new MojoExecutionException("Cannot launch SeedStack application", e);
38+
throw new MojoExecutionException("Cannot launch SeedStack application", unwrapException(e));
3839
}
3940
}
4041

@@ -50,18 +51,27 @@ public static Object getToolLauncher(String tool) throws MojoExecutionException
5051
}
5152
return getLauncherMethod.invoke(null, tool);
5253
} catch (Exception e) {
53-
throw new MojoExecutionException("Cannot launch SeedStack tool", e);
54+
throw new MojoExecutionException("Cannot launch SeedStack tool", unwrapException(e));
5455
}
5556
}
5657

5758
public static void launch(Object seedLauncher, String[] args) throws Exception {
58-
Method launchMethod = seedLauncher.getClass().getMethod("launch", String[].class);
59-
launchMethod.invoke(seedLauncher, new Object[]{args});
59+
try {
60+
Method launchMethod = seedLauncher.getClass().getMethod("launch", String[].class);
61+
launchMethod.invoke(seedLauncher, new Object[]{args});
62+
} catch (Exception e) {
63+
throw new MojoExecutionException("Cannot launch SeedStack application", unwrapException(e));
64+
65+
}
6066
}
6167

6268
public static void shutdown(Object seedLauncher) throws Exception {
63-
Method shutdownMethod = seedLauncher.getClass().getMethod("shutdown");
64-
shutdownMethod.invoke(seedLauncher);
69+
try {
70+
Method shutdownMethod = seedLauncher.getClass().getMethod("shutdown");
71+
shutdownMethod.invoke(seedLauncher);
72+
} catch (Exception e) {
73+
throw new MojoExecutionException("Cannot shutdown SeedStack application", unwrapException(e));
74+
}
6575
}
6676

6777
public static void refresh(Object seedLauncher) throws Exception {
@@ -70,8 +80,21 @@ public static void refresh(Object seedLauncher) throws Exception {
7080
refreshMethod = seedLauncher.getClass().getMethod("refresh");
7181
} catch (NoSuchMethodException e) {
7282
throw new MojoExecutionException("Application refresh is not supported before Seed 3.4.0", e);
83+
}
84+
try {
85+
refreshMethod.invoke(seedLauncher);
86+
} catch (Exception e) {
87+
throw new MojoExecutionException("Unable to refresh SeedStack application", unwrapException(e));
88+
}
89+
}
7390

91+
private static Throwable unwrapException(Exception e) {
92+
Throwable t;
93+
if (e instanceof InvocationTargetException) {
94+
t = ((InvocationTargetException) e).getTargetException();
95+
} else {
96+
t = e;
7497
}
75-
refreshMethod.invoke(seedLauncher);
98+
return t;
7699
}
77100
}

src/main/java/org/seedstack/maven/WatchMojo.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void run() {
128128
URLClassLoader getClassLoader(final URL[] classPathUrls) {
129129
reloadingClassLoader = AccessController.doPrivileged(new PrivilegedAction<ReloadingClassLoader>() {
130130
public ReloadingClassLoader run() {
131-
return new ReloadingClassLoader(getLog(), classPathUrls);
131+
return new ReloadingClassLoader(getLog(), classPathUrls, compileSourceRoots);
132132
}
133133
});
134134
return reloadingClassLoader;
@@ -189,6 +189,9 @@ private void refresh(Set<FileEvent> fileEvents) {
189189
reloadingClassLoader.invalidateAllClasses();
190190
}
191191

192+
// Invalidate generated classes
193+
reloadingClassLoader.invalidateClassesFromPackage("org.seedstack.business.__generated");
194+
192195
// Recompile the sources
193196
recompile();
194197

@@ -276,7 +279,7 @@ private Set<String> collectClassNames(File classFile) throws MojoExecutionExcept
276279
try (FileInputStream is = new FileInputStream(classFile)) {
277280
ClassReader classReader = new ClassReader(is);
278281
classReader.accept(new ClassNameCollector(classNames), 0);
279-
} catch (IOException e) {
282+
} catch (Exception e) {
280283
throw new MojoExecutionException("Unable to analyze class file " + classFile.getAbsolutePath());
281284
}
282285
return classNames;

src/main/java/org/seedstack/maven/classloader/ApplicationConfig.java

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.maven.classloader;
9+
10+
import java.io.IOException;
11+
import java.nio.ByteBuffer;
12+
import java.security.CodeSigner;
13+
import java.security.CodeSource;
14+
import java.security.SecureClassLoader;
15+
import sun.misc.Resource;
16+
17+
class DisposableClassLoader extends SecureClassLoader {
18+
private final ReloadingClassLoader reloadingClassLoader;
19+
private final Resource res;
20+
private final String name;
21+
22+
DisposableClassLoader(ReloadingClassLoader reloadingClassLoader, String name, Resource res) {
23+
this.reloadingClassLoader = reloadingClassLoader;
24+
this.name = name;
25+
this.res = res;
26+
}
27+
28+
@Override
29+
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
30+
if (this.name.equals(name)) {
31+
synchronized (getClassLoadingLock(name)) {
32+
// First, check if the class has already been loaded
33+
Class<?> c = findLoadedClass(name);
34+
if (c == null) {
35+
try {
36+
ByteBuffer byteBuffer = res.getByteBuffer();
37+
if (byteBuffer != null) {
38+
// Use (direct) ByteBuffer:
39+
CodeSigner[] signers = res.getCodeSigners();
40+
CodeSource cs = new CodeSource(res.getURL(), signers);
41+
c = defineClass(name, byteBuffer, cs);
42+
} else {
43+
byte[] b = res.getBytes();
44+
// must read certificates AFTER reading bytes.
45+
CodeSigner[] signers = res.getCodeSigners();
46+
CodeSource cs = new CodeSource(res.getURL(), signers);
47+
c = defineClass(name, b, 0, b.length, cs);
48+
}
49+
} catch (IOException e) {
50+
throw new ClassNotFoundException(name, e);
51+
}
52+
if (resolve) {
53+
resolveClass(c);
54+
}
55+
}
56+
return c;
57+
}
58+
} else {
59+
return reloadingClassLoader.loadClass(name, resolve);
60+
}
61+
}
62+
63+
64+
}

0 commit comments

Comments
 (0)