Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JGrapes Event Driven Framework
* Copyright (C) 2017-2018 Michael N. Lipp
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see .
*/
package org.jgrapes.webconlet.sysinfo;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.time.Duration;
import java.util.HashSet;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import org.jgrapes.core.Channel;
import org.jgrapes.core.Event;
import org.jgrapes.core.Manager;
import org.jgrapes.core.annotation.Handler;
import org.jgrapes.webconsole.base.Conlet.RenderMode;
import org.jgrapes.webconsole.base.ConsoleConnection;
import org.jgrapes.webconsole.base.WebConsoleUtils;
import org.jgrapes.webconsole.base.events.AddConletType;
import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
import org.jgrapes.webconsole.base.events.ConsoleReady;
import org.jgrapes.webconsole.base.events.NotifyConletModel;
import org.jgrapes.webconsole.base.events.NotifyConletView;
import org.jgrapes.webconsole.base.events.RenderConlet;
import org.jgrapes.webconsole.base.events.RenderConletRequestBase;
import org.jgrapes.webconsole.base.freemarker.FreeMarkerConlet;
/**
*
*/
public class SysInfoConlet
extends FreeMarkerConlet {
private static final Set MODES = RenderMode.asSet(
RenderMode.Preview, RenderMode.View);
/**
* The periodically generated update event.
*/
public static class Update extends Event {
}
/**
* Creates a new component with its channel set to the given channel.
*
* @param componentChannel the channel that the component's handlers listen
* on by default and that {@link Manager#fire(Event, Channel...)}
* sends the event to
*/
public SysInfoConlet(Channel componentChannel) {
super(componentChannel);
setPeriodicRefresh(Duration.ofSeconds(1), () -> new Update());
}
/**
* On {@link ConsoleReady}, fire the {@link AddConletType}.
*
* @param event the event
* @param connection the console connection
* @throws TemplateNotFoundException the template not found exception
* @throws MalformedTemplateNameException the malformed template name
* exception
* @throws ParseException the parse exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@Handler
public void onConsoleReady(ConsoleReady event, ConsoleConnection connection)
throws TemplateNotFoundException, MalformedTemplateNameException,
ParseException, IOException {
// Add SysInfoConlet resources to page
connection.respond(new AddConletType(type())
.addRenderMode(RenderMode.Preview).setDisplayNames(
localizations(connection.supportedLocales(), "conletName"))
.addScript(new ScriptResource()
.setRequires("jquery")
.setScriptType("module")
.setScriptUri(event.renderSupport().conletResource(
type(), "SysInfo-functions.ftl.js")))
.addCss(event.renderSupport(), WebConsoleUtils.uriFromPath(
"SysInfo-style.css")));
}
@Override
protected Optional createStateRepresentation(Event> event,
ConsoleConnection session, String conletId) throws Exception {
return Optional.of(new SysInfoModel());
}
@Override
protected Set doRenderConlet(RenderConletRequestBase> event,
ConsoleConnection connection, String conletId,
SysInfoModel conletState) throws Exception {
Set renderedAs = new HashSet<>();
if (event.renderAs().contains(RenderMode.Preview)) {
Template tpl
= freemarkerConfig().getTemplate("SysInfo-preview.ftl.html");
connection.respond(new RenderConlet(type(), conletId,
processTemplate(event, tpl,
fmModel(event, connection, conletId, conletState)))
.setRenderAs(
RenderMode.Preview.addModifiers(event.renderAs()))
.setSupportedModes(MODES));
renderedAs.add(RenderMode.Preview);
}
if (event.renderAs().contains(RenderMode.View)) {
Template tpl
= freemarkerConfig().getTemplate("SysInfo-view.ftl.html");
connection.respond(new RenderConlet(type(), conletId,
processTemplate(event, tpl,
fmModel(event, connection, conletId, conletState)))
.setRenderAs(
RenderMode.View.addModifiers(event.renderAs()))
.setSupportedModes(MODES));
renderedAs.add(RenderMode.View);
}
if (!renderedAs.isEmpty()) {
updateView(connection, conletId);
}
return renderedAs;
}
private void updateView(ConsoleConnection connection, String conletId) {
if (!connection.isConnected()) {
return;
}
Runtime runtime = Runtime.getRuntime();
connection.respond(new NotifyConletView(type(),
conletId, "updateMemorySizes",
System.currentTimeMillis(), runtime.maxMemory(),
runtime.totalMemory(),
runtime.totalMemory() - runtime.freeMemory()));
}
/**
* Handle the periodic update event by sending {@link NotifyConletView}
* events.
*
* @param event the event
* @param connection the console connection
*/
@Handler
public void onUpdate(Update event, ConsoleConnection connection) {
for (String conletId : conletIds(connection)) {
updateView(connection, conletId);
}
}
@Override
@SuppressWarnings("PMD.DoNotCallGarbageCollectionExplicitly")
protected void doUpdateConletState(NotifyConletModel event,
ConsoleConnection connection, SysInfoModel conletState)
throws Exception {
event.stop();
System.gc();
for (String conletId : conletIds(connection)) {
updateView(connection, conletId);
}
}
/**
* The conlet's model.
*/
@SuppressWarnings("serial")
public static class SysInfoModel implements Serializable {
/**
* Return the system properties.
*
* @return the properties
*/
public Properties systemProperties() {
return System.getProperties();
}
/**
* Return the {@link Runtime}.
*
* @return the runtime
*/
public Runtime runtime() {
return Runtime.getRuntime();
}
}
}