/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.gradle.plugins.node.internal.util;
import java.io.File;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskContainer;
/**
* @author Andrea Di Giorgi
*/
public class GradleUtil extends com.liferay.gradle.util.GradleUtil {
public static <T extends Task> T fetchTask(
Project project, String taskName, Class<T> clazz) {
TaskContainer taskContainer = project.getTasks();
Task task = taskContainer.findByName(taskName);
if (clazz.isInstance(task)) {
return (T)task;
}
return null;
}
/**
* Copied from <code>com.liferay.portal.kernel.util.ThreadUtil</code>.
*/
public static Thread[] getThreads() {
Thread currentThread = Thread.currentThread();
ThreadGroup threadGroup = currentThread.getThreadGroup();
while (threadGroup.getParent() != null) {
threadGroup = threadGroup.getParent();
}
int threadCountGuess = threadGroup.activeCount();
Thread[] threads = new Thread[threadCountGuess];
int threadCountActual = threadGroup.enumerate(threads);
while (threadCountActual == threadCountGuess) {
threadCountGuess *= 2;
threads = new Thread[threadCountGuess];
threadCountActual = threadGroup.enumerate(threads);
}
return threads;
}
public static boolean isRunningInsideDaemon() {
for (Thread thread : getThreads()) {
if (thread == null) {
continue;
}
String name = thread.getName();
if (name.startsWith("Daemon worker")) {
return true;
}
}
return false;
}
public static boolean toBoolean(Object object) {
object = toObject(object);
if (object instanceof Boolean) {
return (Boolean)object;
}
if (object instanceof String) {
return Boolean.parseBoolean((String)object);
}
return false;
}
public static File toFile(Project project, Object object) {
object = toObject(object);
if (object == null) {
return null;
}
return project.file(object);
}
}