use of org.gradle.api.GradleException in project gradle by gradle.
the class DefaultJvmVersionDetector method parseJavaVersionCommandOutput.
private JavaVersion parseJavaVersionCommandOutput(String javaExecutable, BufferedReader reader) {
try {
String versionStr = reader.readLine();
while (versionStr != null) {
Matcher matcher = Pattern.compile("(?:java|openjdk) version \"(.+?)\"( \\d{4}-\\d{2}-\\d{2}( LTS)?)?").matcher(versionStr);
if (matcher.matches()) {
return JavaVersion.toVersion(matcher.group(1));
}
versionStr = reader.readLine();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
throw new GradleException(String.format("Could not determine Java version using executable %s.", javaExecutable));
}
use of org.gradle.api.GradleException in project gradle by gradle.
the class SimpleStateCache method serialize.
private void serialize(T newValue) {
try {
if (!cacheFile.isFile()) {
cacheFile.createNewFile();
}
// read-write-execute for user only
chmod.chmod(cacheFile.getParentFile(), 0700);
// read-write for user only
chmod.chmod(cacheFile, 0600);
OutputStreamBackedEncoder encoder = new OutputStreamBackedEncoder(new BufferedOutputStream(new FileOutputStream(cacheFile)));
try {
serializer.write(encoder, newValue);
} finally {
encoder.close();
}
} catch (Exception e) {
throw new GradleException(String.format("Could not write cache value to '%s'.", cacheFile), e);
}
}
use of org.gradle.api.GradleException in project tomee by apache.
the class TomEEEmbeddedTask method doRun.
private void doRun() {
final Properties originalSystProp = new Properties();
originalSystProp.putAll(System.getProperties());
final Thread thread = Thread.currentThread();
final ClassLoader loader = thread.getContextClassLoader();
if (inlinedServerXml != null && !inlinedServerXml.trim().isEmpty()) {
if (serverXml != null && serverXml.exists()) {
throw new GradleException("you can't define a server.xml and an inlinedServerXml");
}
try {
GFileUtils.mkdirs(workDir);
serverXml = new File(workDir, "server.xml_dump");
GFileUtils.writeFile(inlinedServerXml, serverXml);
} catch (final Exception e) {
throw new GradleException(e.getMessage(), e);
}
}
final AtomicBoolean running = new AtomicBoolean();
AutoCloseable container;
Thread hook;
try {
final Class<?> containerClass = loader.loadClass("org.apache.tomee.embedded.Container");
final Class<?> configClass = loader.loadClass("org.apache.tomee.embedded.Configuration");
final Class<?> parentLoaderFinderClass = loader.loadClass("org.apache.openejb.core.ParentClassLoaderFinder");
final Class<?> loaderFinderClass = loader.loadClass("org.apache.openejb.core.ProvidedClassLoaderFinder");
final Class<?> systemInstanceClass = loader.loadClass("org.apache.openejb.loader.SystemInstance");
container = AutoCloseable.class.cast(containerClass.newInstance());
final Object config = getConfig(configClass);
containerClass.getMethod("setup", configClass).invoke(container, config);
if (inlinedTomEEXml != null && inlinedTomEEXml.trim().isEmpty()) {
try {
final File conf = new File(dir, "conf");
GFileUtils.mkdirs(conf);
GFileUtils.writeFile(inlinedTomEEXml, new File(conf, "tomee.xml"));
} catch (final Exception e) {
throw new GradleException(e.getMessage(), e);
}
}
final AutoCloseable finalContainer = container;
hook = new Thread() {
@Override
public void run() {
if (running.compareAndSet(true, false)) {
final Thread thread = Thread.currentThread();
final ClassLoader old = thread.getContextClassLoader();
thread.setContextClassLoader(loader);
try {
finalContainer.close();
} catch (final NoClassDefFoundError noClassDefFoundError) {
// debug cause it is too late to shutdown properly so don't pollute logs
getLogger().debug("can't stop TomEE", noClassDefFoundError);
} catch (final Exception e) {
getLogger().error("can't stop TomEE", e);
} finally {
thread.setContextClassLoader(old);
}
}
}
};
hook.setName("TomEE-Embedded-ShutdownHook");
// yes should be done after but we can't help much if we don't do it there for auto shutdown
running.set(true);
containerClass.getMethod("start").invoke(container);
// SystemInstance.get().setComponent(ParentClassLoaderFinder.class, new ProvidedClassLoaderFinder(loader));
final Object providedLoaderFinder = loaderFinderClass.getConstructor(ClassLoader.class).newInstance(loader);
final Object systemInstance = systemInstanceClass.getMethod("get").invoke(null);
systemInstanceClass.getMethod("setComponent", Class.class, Object.class).invoke(systemInstance, parentLoaderFinderClass, providedLoaderFinder);
Runtime.getRuntime().addShutdownHook(hook);
containerClass.getMethod("deployClasspathAsWebApp", String.class, File.class, boolean.class).invoke(container, context, docBase, singleClassloader);
getLogger().info("TomEE embedded started on " + configClass.getMethod("getHost").invoke(config) + ":" + configClass.getMethod("getHttpPort").invoke(config));
} catch (final Exception e) {
throw new GradleException(e.getMessage(), e);
}
try {
String line;
final Scanner scanner = new Scanner(System.in);
while ((line = scanner.nextLine()) != null) {
final String cmd = line.trim().toLowerCase(Locale.ENGLISH);
switch(cmd) {
case "exit":
case "quit":
running.set(false);
Runtime.getRuntime().removeShutdownHook(hook);
container.close();
return;
default:
getLogger().warn("Unknown: '" + cmd + "', use 'exit' or 'quit'");
}
}
} catch (final Exception e) {
Thread.interrupted();
} finally {
thread.setContextClassLoader(loader);
System.setProperties(originalSystProp);
}
}
use of org.gradle.api.GradleException in project gradle by gradle.
the class GroovySystemLoaderFactory method forClassLoader.
public GroovySystemLoader forClassLoader(ClassLoader classLoader) {
try {
Class<?> groovySystem;
try {
groovySystem = classLoader.loadClass("groovy.lang.GroovySystem");
} catch (ClassNotFoundException e) {
// Not a Groovy implementation, or not an implementation that we need to deal with
return NOT_BROKEN;
}
if (groovySystem.getClassLoader() != classLoader) {
// Groovy implementation visible from somewhere else
return NOT_BROKEN;
}
String versionString;
try {
Method getVersion = groovySystem.getDeclaredMethod("getVersion");
versionString = (String) getVersion.invoke(null);
} catch (NoSuchMethodException ex) {
return NOT_BROKEN;
}
VersionNumber groovyVersion = VersionNumber.parse(versionString);
boolean isFaultyGroovy = groovyVersion.getMajor() == 2 && groovyVersion.getMinor() == 4;
return isFaultyGroovy ? new LeakyOnJava7GroovySystemLoader(classLoader) : NOT_BROKEN;
} catch (Exception e) {
throw new GradleException("Could not inspect the Groovy system for ClassLoader " + classLoader, e);
}
}
use of org.gradle.api.GradleException in project gradle by gradle.
the class LeakyOnJava7GroovySystemLoader method shutdown.
@Override
public void shutdown() {
if (leakingLoader == getClass().getClassLoader()) {
throw new IllegalStateException("Cannot shut down the main Groovy loader.");
}
// Remove cached value for every class seen by this ClassLoader
try {
Iterator<?> it = globalClassSetIterator();
while (it.hasNext()) {
Object classInfo = it.next();
if (classInfo != null) {
Class clazz = (Class) clazzField.get(classInfo);
removeFromGlobalClassValue.invoke(globalClassValue, clazz);
if (LOG.isDebugEnabled()) {
LOG.debug("Removed ClassInfo from {} loaded by {}", clazz.getName(), clazz.getClassLoader());
}
}
}
} catch (Exception e) {
throw new GradleException("Could not shut down the Groovy system for " + leakingLoader, e);
}
}
Aggregations