Skip to content

Commit

Permalink
#223 Content Service default language
Browse files Browse the repository at this point in the history
  • Loading branch information
g.skorupa@gmail.com committed Jan 7, 2020
1 parent 2b8e08c commit b142823
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ RUN mkdir /cricket
RUN mkdir /cricket/data
RUN mkdir /cricket/files
RUN mkdir /cricket/backup
COPY dist/cricket-1.3.7.jar /cricket
COPY dist/cricket-1.3.8.jar /cricket
COPY dist/config/cricket.json /cricket/config/
COPY dist/www /cricket/www
COPY dist/data/cricket_publickeystore.jks /cricket/data
VOLUME /cricket
volume /cricket/www
WORKDIR /cricket

CMD ["java", "--illegal-access=deny", "--add-modules", "java.xml.bind", "--add-modules", "java.activation", "-jar", "cricket-1.3.7.jar", "-r", "-c", "/cricket/config/cricket.json", "-s", "Microsite"]
CMD ["java", "--illegal-access=deny", "--add-modules", "java.xml.bind", "--add-modules", "java.activation", "-jar", "cricket-1.3.8.jar", "-r", "-c", "/cricket/config/cricket.json", "-s", "Microsite"]
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</target>
<!-- set global properties for this build -->
<property name="targetJava10+" value="true"/>
<property name="mainVersion" value="1.3.7"/>
<property name="mainVersion" value="1.3.8"/>
<property name="version" value="${mainVersion}"/>
<!-- default JDK == 1.12 -->
<!-- alternate JDK compiler will be used if declared (not empty) -->
Expand Down
4 changes: 2 additions & 2 deletions src/java/BasicService-help.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Usage:
java -jar cricketms-1.3.7.jar [options]
java -cp cricketms-1.3.7.jar org.cricketmsf.Runner [options]
java -jar cricketms-1.3.8.jar [options]
java -cp cricketms-1.3.8.jar org.cricketmsf.Runner [options]

Runs selected service class.

Expand Down
1 change: 1 addition & 0 deletions src/java/cricket.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"properties": {
"host": "0.0.0.0",
"port": "7070",
"wsport": "7071",
"ssl": "false",
"keystore": "./data/cricket_publickeystore.jks",
"keystore-password": "cricket15$#17",
Expand Down
4 changes: 2 additions & 2 deletions src/java/cricket.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Wed, 18 Dec 2019 13:01:39 +0100
version=1.3.7
#Tue, 07 Jan 2020 12:59:09 +0100
version=1.3.8
53 changes: 45 additions & 8 deletions src/java/org/cricketmsf/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public abstract class Kernel {
// http server
private String host = null;
private int port = 0;
private int websocketPort = 0;
private String sslAlgorithm = "";
private Httpd httpd;
private boolean httpHandlerLoaded = false;
private boolean inboundAdaptersLoaded = false;
private WebsocketServer websocketServer;
private int shutdownDelay = 5;

//private static long eventSeed = System.currentTimeMillis();
Expand Down Expand Up @@ -303,8 +305,8 @@ public static long getEventId() {

/**
* Must be used to set adapter variables after instantiating them according
* to the configuration in the configuration file (settings.json).
* Look at EchoService example.
* to the configuration in the configuration file (settings.json). Look at
* EchoService example.
*/
public abstract void getAdapters();

Expand All @@ -319,17 +321,19 @@ public static Kernel getInstanceWithProperties(Class c, Configuration config, Co
try {
Configuration cfg = config;
instance = (Kernel) c.newInstance();
Configuration defaultCfg=null;
Configuration defaultCfg = null;
if (null != instance.configurationBaseName) {
defaultCfg = defaultConfigSet.getConfigurationById(instance.configurationBaseName);
if (null != defaultCfg) {
cfg = cfg.join(defaultCfg);
}else{
defaultConfigSet.getServices().forEach(cf->{System.out.println(cf.getId()+" "+cf.getService());});
} else {
defaultConfigSet.getServices().forEach(cf -> {
System.out.println(cf.getId() + " " + cf.getService());
});
}
}
System.out.println("DEFAULT CONFIG: " + instance.configurationBaseName+ " with "
+(defaultCfg!=null?defaultCfg.getAdapters().size():"unknown")+" adapters");
System.out.println("DEFAULT CONFIG: " + instance.configurationBaseName + " with "
+ (defaultCfg != null ? defaultCfg.getAdapters().size() : "unknown") + " adapters");
((Kernel) instance).setUuid(UUID.randomUUID());
((Kernel) instance).setId(config.getId());
((Kernel) instance).setName((String) cfg.getProperties().getOrDefault("SRVC_NAME_ENV_VARIABLE", "CRICKET_NAME"));
Expand Down Expand Up @@ -395,6 +399,11 @@ private synchronized void loadAdapters(Configuration config) throws Exception {
} catch (Exception e) {
e.printStackTrace();
}
try {
setWebsocketPort(Integer.parseInt(config.getProperty("wsport", "0")));
} catch (Exception e) {
e.printStackTrace();
}
getLogger().print("\tport=" + getPort());
try {
setShutdownDelay(Integer.parseInt(config.getProperty("shutdown-delay", "2")));
Expand Down Expand Up @@ -429,7 +438,7 @@ private synchronized void loadAdapters(Configuration config) throws Exception {
java.lang.reflect.Method loadPropsMethod = c.getMethod("loadProperties", HashMap.class, String.class);
loadPropsMethod.invoke(adaptersMap.get(adapterName), ac.getProperties(), adapterName);
setEventDispatcher(((Adapter) adaptersMap.get(adapterName)).getDispatcher());
if(adaptersMap.get(adapterName) instanceof org.cricketmsf.out.log.LoggerAdapterIface){
if (adaptersMap.get(adapterName) instanceof org.cricketmsf.out.log.LoggerAdapterIface) {
setFineLevel(((LoggerAdapterIface) adaptersMap.get(adapterName)).isFineLevel());
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {
Expand Down Expand Up @@ -614,6 +623,13 @@ public void run() {
} else {
getLogger().print("# HTTP listening on port " + getPort());
}
if (getWebsocketPort() > 0) {
websocketServer = new WebsocketServer(this);
websocketServer.start();
getLogger().print("# Websocket server listening on port " + getWebsocketPort());
}else{
getLogger().print("# Websocket server not listening (port not configured)");
}
getLogger().print("#");
getLogger().print("# Started in " + startedIn + "ms. Press Ctrl-C to stop");
getLogger().print("");
Expand Down Expand Up @@ -671,6 +687,13 @@ public void shutdown() {
getHttpd().stop();
} catch (NullPointerException e) {
}
try {
if(null!=websocketServer){
websocketServer.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Kernel stopped\r\n");
}

Expand Down Expand Up @@ -955,4 +978,18 @@ public void setFineLevel(boolean fineLevel) {
this.fineLevel = fineLevel;
}

/**
* @return the websocketPort
*/
public int getWebsocketPort() {
return websocketPort;
}

/**
* @param websocketPort the websocketPort to set
*/
public void setWebsocketPort(int websocketPort) {
this.websocketPort = websocketPort;
}

}
97 changes: 97 additions & 0 deletions src/java/org/cricketmsf/WebsocketServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2019 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.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import org.cricketmsf.in.websocket.WebsocketAdapter;

/**
*
* @author Grzegorz Skorupa <g.skorupa at gmail.com>
*/
public class WebsocketServer implements Runnable {

private ServerSocket server = null;
private boolean running = false;
ArrayList<WebsocketAdapter> clients = new ArrayList<>();

public WebsocketServer(Kernel service) {
String host = service.getHost();
int backlog = 0;
try {
backlog = Integer.parseInt((String) service.getProperties().getOrDefault("wsthreads", "0"));
} catch (NumberFormatException | ClassCastException e) {
}
if (null != host) {
if (host.isEmpty() || "0.0.0.0".equals(host) || "*".equals(host)) {
host = null;
}
}
try {
server = new ServerSocket();
if (backlog > 0) {
if (null == host) {
server.bind(new InetSocketAddress(service.getWebsocketPort()), backlog);
} else {
server.bind(new InetSocketAddress(host, service.getWebsocketPort()), backlog);
}
} else {
if (null == host) {
server.bind(new InetSocketAddress(service.getWebsocketPort()));
} else {
server.bind(new InetSocketAddress(host, service.getWebsocketPort()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

public void start() {
Thread t = new Thread(this);
t.start();
}

@Override
public void run() {
running = true;
while (running) {
try {
Socket socket=server.accept();
WebsocketAdapter client = new WebsocketAdapter(socket);
client.start();
clients.add(client);
} catch (IOException waitException) {
throw new IllegalStateException("Could not wait for client connection", waitException);
}
}
}

public void stop() {
try {
running = false;
server.close();
} catch (IOException ex) {
ex.printStackTrace();
}
clients.forEach(client->{client.stop();});
}

}
9 changes: 7 additions & 2 deletions src/java/org/cricketmsf/in/http/HttpAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,13 @@ public void doHandle(HttpExchange exchange, long rootEventId) throws IOException
switch (result.getCode()) {
case SC_MOVED_PERMANENTLY:
case SC_MOVED_TEMPORARY:
headers.set("Location", result.getMessage());
responseData = ("moved to ".concat(result.getMessage())).getBytes("UTF-8");
if(!headers.containsKey("Location")){
String newLocation = result.getMessage()!=null?result.getMessage():"/";
headers.set("Location", newLocation);
responseData = ("moved to ".concat(newLocation)).getBytes("UTF-8");
}else{
responseData="".getBytes();
}
break;
case SC_NOT_FOUND:
headers.set("Content-type", "text/html");
Expand Down
Loading

0 comments on commit b142823

Please sign in to comment.