Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gskorupa committed Sep 18, 2016
1 parent 3713297 commit c51019c
Show file tree
Hide file tree
Showing 29 changed files with 680 additions and 293 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ hs_err_pid*
/data/cricket.log
/www/kitty.jpg
/data/echo.xml
/data/scheduler.xml
/data/scheduler.xml
/www/test/test.txt
/data/cricket.log.lck
48 changes: 27 additions & 21 deletions src/java/cricket.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@
"silent-mode": "false"
}
},
"ScriptingService": {
"name": "ScriptingService",
"interfaceName": "HttpAdapterIface",
"classFullName": "org.cricketmsf.in.http.ScriptingAdapter",
"properties": {
"context": "/script",
"response-type": "JSON",
"silent-mode": "false"
}
},
"TestScheduler": {
"name": "TestScheduler",
"interfaceName": "EchoHttpAdapterIface",
Expand All @@ -42,22 +32,15 @@
"context": "/scheduler"
}
},
"ScriptingEngine": {
"name": "ScriptingEngine",
"interfaceName": "ScriptingAdapterIface",
"classFullName": "org.cricketmsf.out.script.NashornScriptingAdapter",
"properties": {
"script-file": "./script.js"
}
},
"LoggerAdapterIface": {
"name": "LoggerAdapterIface",
"interfaceName": "LoggerAdapterIface",
"classFullName": "org.cricketmsf.out.log.StandardLogger",
"properties": {
"name": "EchoService",
"level": "FINEST",
"log-file-name": "./data/cricket.log"
"log-file-name": "./data/cricket.log",
"console": "true"
}
},
"KeyValueCacheAdapterIface": {
Expand All @@ -69,7 +52,7 @@
"envVariable": "ECHO_DB_PATH",
"file": "echo.xml",
"max-records": "10",
"persistent": "true"
"persistent": "false"
}
},
"SchedulerIface": {
Expand All @@ -87,7 +70,8 @@
"interfaceName": "HtmlGenAdapterIface",
"classFullName": "org.cricketmsf.in.http.HtmlGenAdapter",
"properties": {
"context": "/"
"context": "/",
"use-cache": "false"
}
},
"FileReaderAdapterIface": {
Expand All @@ -97,6 +81,17 @@
"properties": {
"root": "./www/"
}
},
"EnvironmentAdapter": {
"name": "EnvironmentAdapter",
"interfaceName": "EnvironmentMonitorIface",
"classFullName": "org.cricketmsf.in.monitor.EnvironmentMonitor",
"properties": {
"sampling-interval": "1000",
"disk-path": "/",
"disk-limit": "10M",
"memory-limit": "100M"
}
}
}
},
Expand Down Expand Up @@ -176,6 +171,17 @@
"properties": {
"root": "./www/"
}
},
"EnvironmentAdapter": {
"name": "EnvironmentAdapter",
"interfaceName": "EnvironmentMonitorIface",
"classFullName": "org.cricketmsf.in.monitor.EnvironmentMonitor",
"properties": {
"sampling-interval": "1000",
"disk-path": "/",
"disk-limit": "10M",
"memory-limit": "100M"
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/java/org/cricketmsf/ArgumentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ArgumentParser {
* Default constructor
*/
public ArgumentParser(){
arguments=new HashMap<String,String>();
arguments=new HashMap<>();
}

/**
Expand Down Expand Up @@ -61,15 +61,15 @@ public boolean isProblem(){
* @return map of argument values related to argument names
*/
public static Map<String, String> getArguments(String[] args) {
HashMap<String, String> map = new HashMap<String,String>();
String name="";
HashMap<String, String> map = new HashMap<>();
String name;
String option="";
boolean confToRead = false;
if(args==null){
return map;
}
for (int i = 0; i < args.length; i++) {
name = args[i];
for (String arg : args) {
name = arg;
if (confToRead) {
if(name.startsWith("-")){
map.put("error", "option "+option+" must be followed by value");
Expand Down
2 changes: 0 additions & 2 deletions src/java/org/cricketmsf/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public Event() {
* @param origin the name of the source of this event
* @param category event category
* @param type event type (subcategory)
* @param rootEventId the ID of event which starts processing (not created
* by other event)
* @param timePoint defines when this event should happen.
* @param payload holds additional data
*/
Expand Down
1 change: 0 additions & 1 deletion src/java/org/cricketmsf/Httpd.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
public class Httpd {

//private Kernel service;
public HttpServer server = null;

public Httpd(Kernel service) {
Expand Down
34 changes: 34 additions & 0 deletions src/java/org/cricketmsf/InboundAdapterHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2016 Grzegorz Skorupa <g.skorupa at gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cricketmsf;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*
* @author Grzegorz Skorupa <g.skorupa at gmail.com>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InboundAdapterHook {
public String adapterName();
public String inputMethod();
}
88 changes: 67 additions & 21 deletions src/java/org/cricketmsf/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @author Grzegorz Skorupa
*/
public abstract class Kernel {

private static final String VERSION = "1.0.0";

// emergency LOGGER
Expand All @@ -58,14 +58,15 @@ public abstract class Kernel {

// user defined properties
public HashMap<String, String> properties = new HashMap<>();

public SimpleDateFormat dateFormat = null;

// http server
private String host = null;
private int port = 0;
private Httpd httpd;
private boolean httpHandlerLoaded = false;
private boolean inboundAdaptersLoaded = false;

private static long eventSeed = System.currentTimeMillis();

Expand All @@ -75,8 +76,6 @@ public abstract class Kernel {
private ArrayList corsHeaders;

private long startedAt = 0;



public Kernel() {
}
Expand Down Expand Up @@ -176,17 +175,17 @@ public static Object getInstanceWithProperties(Class c, Configuration config) {
((Kernel) instance).setId(config.getId());
((Kernel) instance).setProperties(config.getProperties());
((Kernel) instance).configureTimeFormat();
((Kernel) instance).loadAdapters(config);
((Kernel) instance).loadAdapters(config);
} catch (Exception e) {
instance = null;
LOGGER.log(Level.SEVERE, "{0}:{1}", new Object[]{e.getStackTrace()[0].toString(), e.getStackTrace()[1].toString()});
e.printStackTrace();
}
return instance;
}
private void configureTimeFormat(){
dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

private void configureTimeFormat() {
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone(getProperties().getOrDefault("time-zone", "GMT")));
}

Expand All @@ -208,6 +207,7 @@ private void printHeader(String version) {
private synchronized void loadAdapters(Configuration config) throws Exception {
printHeader(VERSION);
setHttpHandlerLoaded(false);
setInboundAdaptersLoaded(false);
System.out.println("LOADING SERVICE PROPERTIES FOR " + config.getService());
System.out.println("UUID: " + getUuid().toString());
setHost(config.getHost());
Expand All @@ -221,6 +221,7 @@ private synchronized void loadAdapters(Configuration config) throws Exception {
} catch (Exception e) {
}
System.out.println("http-port=" + getPort());
System.out.println("Extended properties: "+getProperties().toString());
System.out.println("LOADING ADAPTERS");
String adapterName = null;
AdapterConfiguration ac = null;
Expand All @@ -235,13 +236,15 @@ private synchronized void loadAdapters(Configuration config) throws Exception {
adaptersMap.put(adapterName, c.newInstance());
if (adaptersMap.get(adapterName) instanceof org.cricketmsf.in.http.HttpAdapter) {
setHttpHandlerLoaded(true);
} else if (adaptersMap.get(adapterName) instanceof org.cricketmsf.in.InboundAdapter) {
setInboundAdaptersLoaded(true);
}
// loading properties
java.lang.reflect.Method loadPropsMethod = c.getMethod("loadProperties", HashMap.class, String.class);
loadPropsMethod.invoke(adaptersMap.get(adapterName), ac.getProperties(), adapterName);
} catch (Exception ex) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {
adaptersMap.put(adapterName, null);
System.out.println("Adapter " + adapterName + " configuration error: " + ex.getClass().getSimpleName());
System.out.println("ERROR: " + adapterName + "configuration: " + ex.getClass().getSimpleName());
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -338,34 +341,42 @@ public void runOnce() {
public void start() throws InterruptedException {
getAdapters();
getEventHooks();
if (isHttpHandlerLoaded()) {
System.out.println("Starting http listener ...");
if (isHttpHandlerLoaded() || isInboundAdaptersLoaded()) {

Runtime.getRuntime().addShutdownHook(
new Thread() {
public void run() {
try {
Thread.sleep(200);
shutdown();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
setHttpd(new Httpd(this));
getHttpd().run();

System.out.println("Running initialization tasks");
runInitTasks();

System.out.println("Starting listeners ...");
// run listeners for inbound adapters
runListeners();

System.out.println("Starting http listener ...");
setHttpd(new Httpd(this));
getHttpd().run();

long startedIn = System.currentTimeMillis() - startedAt;
printHeader(VERSION);
System.out.println("# Service "+getId()+ " is running");
System.out.println("# Service " + getId() + " is running");
System.out.println("#");
System.out.println("# HTTP listening on port " + getPort());
System.out.println("#");
System.out.println("# Started in " + startedIn + "ms. Press Ctrl-C to stop");
System.out.println();
while (true) {
Thread.sleep(200);
}
runFinalTasks();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

} else {
System.out.println("Couldn't find any http request hook method. Exiting ...");
System.exit(MIN_PRIORITY);
Expand All @@ -374,10 +385,31 @@ public void run() {

/**
* Could be overriden in a service implementation to run required code at
* the service start. As the last step of the service starting procedure.
* the service start. As the last step of the service starting procedure before
* HTTP service.
*/
protected void runInitTasks() {
}

/**
* Could be overriden in a service implementation to run required code at
* the service start. As the last step of the service starting procedure after
* HTTP service.
*/
protected void runFinalTasks() {

}

protected void runListeners() {
for (Map.Entry<String, Object> adapterEntry : getAdaptersMap().entrySet()) {
if (adapterEntry.getValue() instanceof org.cricketmsf.in.InboundAdapter) {
if (! (adapterEntry.getValue() instanceof org.cricketmsf.in.http.HttpAdapter)) {
(new Thread((InboundAdapter) adapterEntry.getValue())).start();
System.out.println(adapterEntry.getValue().getClass().getSimpleName());
}
}
}
}

public void shutdown() {

Expand Down Expand Up @@ -483,4 +515,18 @@ public void setProperties(HashMap<String, String> properties) {
this.properties = properties;
}

/**
* @return the inboundAdaptersLoaded
*/
public boolean isInboundAdaptersLoaded() {
return inboundAdaptersLoaded;
}

/**
* @param inboundAdaptersLoaded the inboundAdaptersLoaded to set
*/
public void setInboundAdaptersLoaded(boolean inboundAdaptersLoaded) {
this.inboundAdaptersLoaded = inboundAdaptersLoaded;
}

}
Loading

0 comments on commit c51019c

Please sign in to comment.