use of net.sf.json.JSONObject in project opennms by OpenNMS.
the class JsonCollectorSolarisZonesIT method testXpath.
/**
* Test to verify XPath content.
*
* @throws Exception the exception
*/
@Test
@SuppressWarnings("unchecked")
public void testXpath() throws Exception {
JSONObject json = MockDocumentBuilder.getJSONDocument();
JXPathContext context = JXPathContext.newContext(json);
Iterator<Pointer> itr = context.iteratePointers("/zones/zone");
while (itr.hasNext()) {
Pointer resPtr = itr.next();
JXPathContext relativeContext = context.getRelativeContext(resPtr);
String resourceName = (String) relativeContext.getValue("@name");
Assert.assertNotNull(resourceName);
String value = (String) relativeContext.getValue("parameter[@key='nproc']/@value");
Assert.assertNotNull(Integer.valueOf(value));
}
}
use of net.sf.json.JSONObject in project opennms by OpenNMS.
the class MockDocumentBuilder method getJSONDocument.
/**
* Gets the JSON document.
*
* @return the JSON document
*/
public static JSONObject getJSONDocument() {
if (m_jsonFileName == null)
return null;
JSONObject json = null;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(m_jsonFileName);
String everything = IOUtils.toString(inputStream);
json = AbstractJsonCollectionHandler.wrapArray(JSONSerializer.toJSON(everything));
} catch (Exception e) {
} finally {
if (inputStream != null)
IOUtils.closeQuietly(inputStream);
}
return json;
}
use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.
the class RunBundleWatches method startBundleWatches.
@Initializer(after = InitMilestone.JOB_LOADED)
public static void startBundleWatches() {
if (!isEnabled || Boolean.getBoolean("blueocean.features.BUNDLE_WATCH_SKIP")) {
return;
}
System.out.println("Running in development mode, watching bundles...");
int buildNumber = 0;
List<PluginWrapper> plugins = Jenkins.getInstance().pluginManager.getPlugins();
;
for (final PluginWrapper p : plugins) {
try {
final File projectDir = findPluginWorkDir(new File(p.baseResourceURL.getPath()));
if (projectDir != null) {
final String path = projectDir.getCanonicalPath();
final File packageFile = new File(projectDir, "package.json");
final BundleBuild build = new BundleBuild();
build.name = p.getShortName();
System.out.println("---- Watching " + build.name + " in: " + path);
JSONObject packageJson = JSONObject.fromObject(FileUtils.readFileToString(packageFile));
final String[] npmCommand = { "mvnbuild", null };
if (packageJson.has("scripts")) {
JSONObject scripts = packageJson.getJSONObject("scripts");
if (scripts.has("mvnbuild:fast")) {
npmCommand[0] = "mvnbuild:fast";
}
if (scripts.has("watch")) {
npmCommand[1] = "watch";
}
if (scripts.has("bundle:watch")) {
npmCommand[1] = "bundle:watch";
}
}
// Leaving this code here because we may want to enable "watch" behavior
if ("watch".equals(npmCommand[1]) && Boolean.getBoolean("blueocean.features.NATIVE_NPM_WATCH")) {
build.thread = new Thread() {
public void run() {
long backOff = DEFAULT_BACK_OFF;
while (true) {
try {
Map<String, String> env = new HashMap<>(System.getenv());
String[] command;
if (SystemUtils.IS_OS_WINDOWS) {
command = new String[] { "cmd", "/C", "npm", "run", npmCommand[1] };
} else {
command = new String[] { "bash", "-c", "${0} ${1+\"$@\"}", "npm", "run", npmCommand[1] };
}
ProcessBuilder pb = new ProcessBuilder(Arrays.asList(command)).redirectErrorStream(true).directory(projectDir);
if (SystemUtils.IS_OS_WINDOWS) {
pb.environment().put("Path", new File(projectDir, "node").getCanonicalPath() + ";" + env.get("Path"));
} else {
pb.environment().put("PATH", new File(projectDir, "node").getCanonicalPath() + ":" + env.get("PATH"));
}
final Process process = pb.start();
build.destructor = new Destructor() {
@Override
public void destroy() {
if (process.isAlive()) {
try {
process.destroy();
process.destroyForcibly();
} catch (Exception e) {
// ignore
}
}
build.destructor = null;
}
};
InputStream in = process.getInputStream();
try (BufferedReader rdr = new BufferedReader(new InputStreamReader(in, "utf-8"))) {
String line;
long startMillis = 0;
while ((line = rdr.readLine()) != null) {
if (line.contains("Starting 'log-env'")) {
startMillis = System.currentTimeMillis();
build.buildCount.incrementAndGet();
build.logLines.clear();
System.out.println("---- Rebuilding: " + path);
}
// add here because we clear when starting a new build
build.logLines.add(new LogLine(System.currentTimeMillis(), line));
if (line.contains("missing script: bundle:watch")) {
System.out.println("---- Unable to find script 'bundle:watch' in: " + packageFile.getCanonicalPath());
// don't retry this case
build.thread = null;
return;
}
// if (line.contains("Finished 'bundle:watch'") || line.contains("Finished 'bundle'")) {
if (line.contains("Finished 'bundle'")) {
if (build.hasError) {
for (LogLine l : build.logLines) {
System.out.println(l.text);
}
build.hasError = false;
}
long time = System.currentTimeMillis() - startMillis;
build.lastBuildTimeMillis = time;
build.buildCount.decrementAndGet();
System.out.println("---- Rebuilt " + build.name + " in: " + time + "ms");
}
if (line.contains("Failed at the")) {
System.out.println("---- Failed to build: " + build.name);
build.buildCount.decrementAndGet();
for (LogLine l : build.logLines) {
System.out.println(l.text);
}
}
if (line.contains("error") || line.contains("Error") || line.contains("ERROR")) {
build.hasError = true;
}
}
}
} catch (RuntimeException e) {
// Thanks findbugs
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
build.destructor = null;
try {
Thread.sleep(backOff);
backOff = (long) Math.floor(backOff * 2);
} catch (InterruptedException e) {
// nothing to see here
backOff = DEFAULT_BACK_OFF;
}
}
}
};
build.thread.setDaemon(true);
build.thread.setName("Watching " + build.name + " for changes in: " + path);
build.thread.start();
} else {
// Might be better to be "/src/main"
// but JDL and core js currently have a different structure
// limiting the scope is handled with the PROJECT_PATH_FILTER and EXTENSIONS_TO_CAUSE_REBUILD
final File watchDir = projectDir;
final Path watchPath = watchDir.toPath();
// java nio watch, run `mvnbuild` instead
Thread fileWatchThread = new Thread() {
volatile Process buildProcess;
@Override
public void run() {
build.destructor = new Destructor() {
@Override
public void destroy() {
if (buildProcess != null && buildProcess.isAlive()) {
try {
buildProcess.destroy();
if (buildProcess != null && buildProcess.isAlive()) {
buildProcess.destroyForcibly();
}
} catch (Exception e) {
// ignore
e.printStackTrace();
}
}
buildProcess = null;
build.logLines.add(new LogLine("Process restarted."));
}
};
RecursivePathWatcher watcher = new RecursivePathWatcher(watchPath, PROJECT_PATH_FILTER);
watcher.start(new RecursivePathWatcher.PathEventHandler() {
@Override
public void accept(final RecursivePathWatcher.Event event, final Path modified) {
// only rebuild for certain types of files
if (!EXTENSIONS_TO_CAUSE_REBUILD.matcher(modified.toString()).matches()) {
return;
}
// kill any currently running builds
build.destructor.destroy();
// run in a separate thread so we can pick up changes and kill any existing
// running processes
build.thread = new Thread() {
@Override
public void run() {
build.buildCount.incrementAndGet();
long startMillis = System.currentTimeMillis();
try {
Map<String, String> env = new HashMap<>(System.getenv());
String[] command;
if (SystemUtils.IS_OS_WINDOWS) {
command = new String[] { "cmd", "/C", "npm", "run", npmCommand[0] };
} else {
command = new String[] { "bash", "-c", "${0} ${1+\"$@\"}", "npm", "run", npmCommand[0] };
}
System.out.println("---- Rebuilding: " + build.name + " due to change in: " + watchPath.relativize(modified));
ProcessBuilder pb = new ProcessBuilder(Arrays.asList(command)).redirectErrorStream(true).directory(projectDir);
if (SystemUtils.IS_OS_WINDOWS) {
pb.environment().put("Path", new File(projectDir, "node").getCanonicalPath() + ";" + env.get("Path"));
} else {
pb.environment().put("PATH", new File(projectDir, "node").getCanonicalPath() + ":" + env.get("PATH"));
}
buildProcess = pb.start();
InputStream in = buildProcess.getInputStream();
try (BufferedReader rdr = new BufferedReader(new InputStreamReader(in, "utf-8"))) {
String line;
while ((line = rdr.readLine()) != null) {
build.logLines.add(new LogLine(System.currentTimeMillis(), line));
if (line.contains("missing script: mvnbuild")) {
build.hasError = true;
}
if (line.contains("Failed at the")) {
build.hasError = true;
}
if (line.contains("error") || line.contains("Error") || line.contains("ERROR")) {
build.hasError = true;
}
}
}
// If process wasn't killed or error, output the rebuild info
if (buildProcess != null && buildProcess.waitFor() == 0) {
build.lastBuildTimeMillis = System.currentTimeMillis() - startMillis;
System.out.println("---- Rebuilt " + build.name + " in: " + build.lastBuildTimeMillis + "ms");
}
} catch (RuntimeException e) {
// Thanks findbugs
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
buildProcess = null;
build.buildCount.decrementAndGet();
if (build.hasError) {
for (LogLine l : build.logLines) {
System.out.println(l.text);
}
build.hasError = false;
}
// Don't use excessive memory:
build.logLines.clear();
}
}
};
build.thread.setDaemon(true);
build.thread.setName("Building: " + path);
build.thread.start();
}
});
}
};
fileWatchThread.setDaemon(true);
fileWatchThread.setName("Watching " + build.name + " for changes in: " + path);
fileWatchThread.start();
}
builds.add(build);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of net.sf.json.JSONObject in project jaffa-framework by jaffa-projects.
the class ExcelExportService method createJsonConfig.
/**
* Creates a JsonConfig with the rootClass set to the input. Adds
* custom-support for handling FlexCriteriaBean.
*/
private static JsonConfig createJsonConfig(Class rootClass) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(rootClass);
jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {
public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
if (target == FlexCriteriaBean.class) {
try {
// Determine the name of the associated dynaClass and
// use that to instantiate the FlexCriteriaBean
JSONObject dynaClassObject = source.getJSONObject("dynaClass");
String dynaClassName = dynaClassObject.getString("name");
FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
// Add the criteria elements
source.remove("dynaClass");
for (Iterator i = source.keys(); i.hasNext(); ) {
String key = (String) i.next();
Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
propType = findCriteriaFieldClass(propType);
Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
bean.set(key, propValue);
}
source.clear();
return bean;
} catch (Exception e) {
String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
log.error(s, e);
throw new InvocationTargetException(e, s);
}
}
return target.newInstance();
}
});
return jsonConfig;
}
use of net.sf.json.JSONObject in project jaffa-framework by jaffa-projects.
the class ExcelExportService method createJsonConfig.
/**
* Creates a JsonConfig with the rootClass set to the input. Adds
* custom-support for handling FlexCriteriaBean.
*/
private static JsonConfig createJsonConfig(Class rootClass) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(rootClass);
jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {
public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
if (target == FlexCriteriaBean.class) {
try {
// Determine the name of the associated dynaClass and
// use that to instantiate the FlexCriteriaBean
JSONObject dynaClassObject = source.getJSONObject("dynaClass");
String dynaClassName = dynaClassObject.getString("name");
FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
// Add the criteria elements
source.remove("dynaClass");
for (Iterator i = source.keys(); i.hasNext(); ) {
String key = (String) i.next();
Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
propType = findCriteriaFieldClass(propType);
Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
bean.set(key, propValue);
}
source.clear();
return bean;
} catch (Exception e) {
String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
log.error(s, e);
throw new InvocationTargetException(e, s);
}
}
return target.newInstance();
}
});
return jsonConfig;
}
Aggregations