Commit 03ecadaf by amir

initial

parents
Pipeline #73 skipped in 0 seconds
Showing with 4477 additions and 0 deletions
/build/
/bin/
.gradle
.settings
.idea
.lck
.project
.classpath
gradle/
group 'com.ipgallery.common'
version '1.0.0'
apply plugin: 'java'
apply plugin: 'maven-publish'
sourceCompatibility = 1.8
repositories {
//mavenCentral()
maven { url "http://172.16.1.132:8081/repository/internal" }
maven { url "http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository" }
}
dependencies {
compile 'log4j:log4j:1.2.17'
compile 'commons-io:commons-io:2.0.1'
compile 'redis.clients:jedis:2.4.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
compile 'com.googlecode.libphonenumber:libphonenumber:5.8'
compile 'com.google.gdata:gdata-core-1.0:1.36.0'
compile 'org.apache.httpcomponents:httpclient:4.1'
compile 'org.apache.httpcomponents:httpmime:4.1'
compile 'javax.ws.rs:javax.ws.rs-api:2.0'
compile 'com.ipgallery.common:itc:1.0.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
publishing {
publications {
repositories.maven {
url 'http://172.16.1.132:8081/repository/internal'
credentials {
username "admin"
password "giptmgr1"
}
}
mavenJava(MavenPublication) {
//artifactId 'group-service'
from components.java
}
}
}
\ No newline at end of file
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
rootProject.name = 'utils'
rootProject.name = 'utils'
package utils.common;
public interface CacheClient
{
/**
* set/update with the default expiration
* @param key
* @param value
*/
public void set(String key, String value);
public void set(String key, String value, int expiration);
public void setExpiration(String key, int expiration);
public String get(String key);
public void delete(String key);
public void deleteByPattern(String pattern);
public String[] getByPattern(String pattern);
}
package utils.common;
import java.util.Map.Entry;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
public class ConfigFile {
private SimpleLogger m_Log = null;
private Properties m_Props = null;
/*
* constructor
**/
public ConfigFile(SimpleLogger Log) {
this.m_Log = Log;
}
/*
* Initialize
*/
public boolean Initialize(String cfgName, String cfgPath) {
m_Props = new Properties();
try
{
// read the configuration file name
String cfgFile = cfgPath + "/" + cfgName;
InputStream is = new FileInputStream(cfgFile);
// load the properties file
m_Props.load(is);
PrintPropertiesToLogFile();
return true;
} catch (IOException e) {
m_Log.log(Level.SEVERE, "Failed to initialized config file: " + cfgName + " e:" + e.getMessage().toString());
return false;
}
}
public void PrintPropertiesToLogFile()
{
for(Entry<Object, Object> prop : m_Props.entrySet()) {
String logMsg = "ConfigProperty[" + prop.getKey().toString() + "=" + prop.getValue().toString() + "]";
m_Log.log(Level.INFO, logMsg);
}
}
public String getProperty(String propKey) {
return m_Props.getProperty(propKey);
}
public int getPropertyAsInt(String propKey) {
String strValue = m_Props.getProperty(propKey);
if (strValue != null) {
return Integer.parseInt(strValue);
}
return -1;
}
}
package utils.common;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.gdata.util.common.base.PercentEscaper;
public class General
{
public static final String C_SIP_SCHEME = "sip:";
private static PercentEscaper pes = new PercentEscaper(PercentEscaper.SAFECHARS_URLENCODER, false);
private static PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
private static final ThreadLocal<DateFormat> DF = new ThreadLocal<DateFormat>(){
private static final String DATE_FORMAT = "dd/MM/yy HH:mm:ss";
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat(DATE_FORMAT);
}
};
public static String encodeURL(String strToEncode)
{
return pes.escape(strToEncode);
}
/**
* this is are placement for Math.abs since for Integer.MIN_VALUE
* we get negative result
* @param x
* @return
*/
public static int abs(int x)
{
if (x == Integer.MIN_VALUE) {
return 0;
}
return Math.abs(x);
}
// public static long abs(long x)
// {
// if (x == Long.MIN_VALUE) {
// return 0;
// }
// return Math.abs(x);
// }
//
/**
* Retrieve only the SubId part from a full name in the format SubId@HostName.
* If the full name lacks '@' character, it is returned whole.
* @param fullName
* @return SubId part
*/
public static String extractUserPartFromUri(final String fullName)
{
String user = null;
int atIndex = fullName.indexOf('@');
if (atIndex != -1)
user = fullName.substring(0, atIndex);
else
user = fullName;
/*
* searching for '<'
*/
atIndex = user.indexOf('<');
if (atIndex != -1)
user = user.substring(atIndex+1);
return user;
}
public static String extractUserPartFromSipUri(final String sipUri)
{
String uri = null;
if (sipUri != null) {
if (sipUri.startsWith(C_SIP_SCHEME))
uri = extractUserPartFromUri(sipUri.substring(C_SIP_SCHEME.length()));
else
uri = extractUserPartFromUri(sipUri);
}
return uri;
}
public static String format (Timestamp date) {
return DF.get().format(date);
}
/**
* Returns true if all characters in the given string are numeric digits.
* @param str
*/
public static boolean IsNumber(String str) {
if ( str != null && !str.isEmpty() ) {
for (int i=0; i<str.length(); i++)
if ( (str.charAt(i)<'0') || (str.charAt(i)>'9') )
return false;
return true;
}
return false;
}
/*
* remove start and end signs from String
* NOTE: the startSign has to be the first char in the Sting
*/
public static String removeSigns(String str, char startSign, char endSign)
{
if (str.charAt(0) == startSign)
{
int iOffset = str.indexOf(endSign,1);
if (iOffset == -1)
iOffset = str.length();
return str.substring(1, iOffset);
}
return str;
}
public static String getE164Number(String number, String defaultCountry, boolean bWithPlus)
{
boolean isNumberValid = false;
String e164Number = null;
try {
PhoneNumber phoneNumber = phoneUtil.parseAndKeepRawInput(number, defaultCountry);
isNumberValid = phoneUtil.isValidNumber(phoneNumber);
e164Number = isNumberValid ? phoneUtil.format(phoneNumber, PhoneNumberFormat.E164) : null;
if (!bWithPlus && e164Number.charAt(0) == '+')
e164Number = e164Number.substring(1);
} catch (Exception e)
{
}
return e164Number;
}
}
package utils.common;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonHandler {
private static final Log4jHandler logger = new Log4jHandler(JsonHandler.class.getName());
private static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
static {
SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
SORTED_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static String getObjectAsJsonString(Object obj) {
String objString = null;
try {
objString = SORTED_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
logger.error("in getObjectAsJsonString", e);
}
return objString;
}
public static Object readObjectFromString(String jsonStr, Class<?> ObjClass) {
Object obj = null;
try {
obj = SORTED_MAPPER.readValue(jsonStr, ObjClass);
}
catch(Exception ex) {
}
return obj;
}
// public static <T> T readObjectFromString(String jsonStr, Class<T> ObjClass) {
// T obj = null;
// try {
// obj = SORTED_MAPPER.readValue(jsonStr, ObjClass);
// }
// catch(Exception ex) {
// }
// return obj;
// }
public static String jsonNode2String(final JsonNode node) {
Object obj;
String json = null;
try {
obj = SORTED_MAPPER.treeToValue(node, Object.class);
json = SORTED_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
public static String getParamString(JsonNode jsonNode, String name) {
if (jsonNode != null) {
JsonNode jn = jsonNode.path(name);
if (!jn.isMissingNode()) {
return General.removeSigns(jn.toString(), '"', '"');
}
}
return null;
}
public static Long getParamLong(JsonNode jsonNode, String name) {
if (jsonNode != null) {
JsonNode jn = jsonNode.path(name);
if (!jn.isMissingNode() && jn.isNumber()) {
return new Long(jn.longValue());
}
}
return null;
}
public static Integer getParamInt(JsonNode jsonNode, String name) {
if (jsonNode != null) {
JsonNode jn = jsonNode.path(name);
if (!jn.isMissingNode() && jn.isNumber()) {
return new Integer(jn.intValue());
}
}
return null;
}
public static Boolean getParamBool(JsonNode jsonNode, String name) {
if (jsonNode != null) {
JsonNode jn = jsonNode.path(name);
if (!jn.isMissingNode() && jn.isBoolean()) {
return new Boolean(jn.booleanValue());
}
}
return null;
}
public static Object getObject(JsonNode jsonNode, String name, Class<?> objClass) {
ObjectReader objectReader = SORTED_MAPPER.reader(objClass);
if (jsonNode != null && !(jsonNode.isNull()) && !(jsonNode.isMissingNode()) && objectReader != null) {
JsonNode jn = jsonNode.path(name);
if (!jn.isMissingNode()) {
try {
return objectReader.readValue(jn);
} catch (Exception e) {
}
}
}
return null;
}
public static Object getNodeAsObject(JsonNode jsonNode, Class<?> objClass) {
ObjectReader objectReader = SORTED_MAPPER.reader(objClass);
if (jsonNode != null && !(jsonNode.isNull()) && !(jsonNode.isMissingNode()) && objectReader != null) {
try {
return objectReader.readValue(jsonNode);
} catch (Exception e) {
}
}
return null;
}
public static JsonNode getJsonNodeFromObject(Object obj){
JsonNode node = null;
try{
node = SORTED_MAPPER.valueToTree(obj);
}catch (Exception e) {
}
return node;
}
public static JsonNode getJsonNodeFromString(String jsonString){
JsonNode node = null;
try{
node = SORTED_MAPPER.readTree(jsonString);
}catch (Exception e) {
}
return node;
}
}
package utils.common;
import java.io.File;
import java.util.Date;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
/**
* @author ziv
*
*/
public class Log4jFileAlterationListenerImpl implements FileAlterationListener {
Log4jHandler logger = new Log4jHandler(Log4jFileAlterationListenerImpl.class.getName());
/**
* {@inheritDoc}
*/
@Override
public void onStart(final FileAlterationObserver observer) {
// logger.debug("The WindowsFileListener has started on " + observer.getDirectory().getAbsolutePath());
}
/**
* {@inheritDoc}
*/
@Override
public void onDirectoryCreate(final File directory) {
// logger.debug(directory.getAbsolutePath() + " was created.");
}
/**
* {@inheritDoc}
*/
@Override
public void onDirectoryChange(final File directory) {
// logger.debug(directory.getAbsolutePath() + " was modified");
}
/**
* {@inheritDoc}
*/
@Override
public void onDirectoryDelete(final File directory) {
// logger.debug(directory.getAbsolutePath() + " was deleted.");
}
/**
* {@inheritDoc}
*/
@Override
public void onFileCreate(final File file) {
// logger.debug(file.getAbsoluteFile() + " was created.");
// logger.debug("----------> length: " + file.length());
// logger.debug("----------> last modified: " + new Date(file.lastModified()));
// logger.debug("----------> readable: " + file.canRead());
// logger.debug("----------> writable: " + file.canWrite());
// logger.debug("----------> executable: " + file.canExecute());
}
/**
* {@inheritDoc}
*/
@Override
public void onFileChange(final File file) {
String fileName = file.getAbsolutePath();
if ( !fileName.endsWith("~") )
{//don't allow changes from the linux automatic backup file
// logger.debug(fileName + " was modified.");
// logger.debug("----------> length: " + file.length());
// logger.debug("----------> last modified: " + new Date(file.lastModified()));
// logger.debug("----------> readable: " + file.canRead());
// logger.debug("----------> writable: " + file.canWrite());
// logger.debug("----------> executable: " + file.canExecute());
try {
org.apache.log4j.xml.DOMConfigurator.configure(fileName);
logger.warning( "Log4j configuration has been reloaded from file < "+ fileName + " >.");
}catch(Exception e) {
logger.debug("Failed to load XML File with exception: " + e);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void onFileDelete(final File file) {
// logger.debug(file.getAbsoluteFile() + " was deleted.");
}
/**
* {@inheritDoc}
*/
@Override
public void onStop(final FileAlterationObserver observer) {
// logger.debug("The WindowsFileListener has stopped on " + observer.getDirectory().getAbsolutePath());
}
}
package utils.common;
import java.io.File;
//import Util.Log4jHandler;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
/**
* @author ziv
*
*/
public class Log4jFileChangeListener{
private ListenThread listenThread = null;
// static final Log4jHandler logger;
private static Log4jFileChangeListener sInstance = null;
private final static String LOG4J_CONF_FILE_PATH;
static {
LOG4J_CONF_FILE_PATH = System.getProperty("log4j.conf.file.path", "/logs/conf/log4j.xml");
// logger = new Log4jHandler(Log4jFileChangeListener.class.getName());
}
public class ListenThread extends Thread
{
public ListenThread(String log4jConfFilePath)
{
super();
if ( log4jConfFilePath.endsWith(".xml") )
log4jConfFilePath = log4jConfFilePath.substring(0, log4jConfFilePath.lastIndexOf('/'));
final File directory = new File(log4jConfFilePath);
FileAlterationObserver fao = new FileAlterationObserver(directory);
fao.addListener(new Log4jFileAlterationListenerImpl());
final FileAlterationMonitor monitor = new FileAlterationMonitor();
monitor.addObserver(fao);
System.out.println("Starting monitor. CTRL+C to stop.");
// logger.info("Starting monitor. CTRL+C to stop.");
try {
monitor.start();
} catch (Exception e) {
System.out.println("Could not start log4j config file monitor with exception : " + e);
// logger.info("Could not start log4j config file monitor with exception : " + e);
}
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Stopping monitor.");
// logger.info("Stopping monitor.");
monitor.stop();
} catch (Exception ignored) {
}
}
}));
}
@Override
public void run()
{
try
{
} catch (Exception e)
{
System.out.println(e.toString());
// logger.info(e.toString());
}
}
@Override
public void destroy()
{
}
}
public synchronized static void createInstance()
{
if (sInstance == null)
sInstance = new Log4jFileChangeListener();
}
public static Log4jFileChangeListener getInstance()
{
if (sInstance == null)
createInstance();
return sInstance;
}
/**
* starting the listen thread
*/
public synchronized void start(String log4jConfFilePath)
{
String filePath;
if ( log4jConfFilePath != null && log4jConfFilePath.length() > 0)
filePath = log4jConfFilePath;
else
filePath = LOG4J_CONF_FILE_PATH;
if (listenThread == null)
{
System.out.println("staring Log4jFileChangeListener thread...");
// logger.info("staring Log4jFileChangeListener thread...");
listenThread = new ListenThread( filePath );
listenThread.start();
}
}
/**
* stopping the listen thread
*/
public synchronized void stop()
{
if (listenThread != null)
{
System.out.println("stopping Log4jFileChangeListener thread...");
// logger.info("stopping Log4jFileChangeListener thread...");
listenThread.destroy();
listenThread = null;
}
}
}
package utils.common;
import java.io.File;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
/**
* @author ziv
*
*/
public class Log4jFileMonitor{
public static void main(String filePath) throws Exception {
// Change this to match the environment you want to watch.
final File directory = new File(filePath);
FileAlterationObserver fao = new FileAlterationObserver(directory);
fao.addListener(new Log4jFileAlterationListenerImpl());
final FileAlterationMonitor monitor = new FileAlterationMonitor();
monitor.addObserver(fao);
System.out.println("Starting monitor. CTRL+C to stop.");
monitor.start();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Stopping monitor.");
monitor.stop();
} catch (Exception ignored) {
}
}
}));
}
}
package utils.common;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* This class wraps the Log4jWrapper class.
* It adds the calling class name to all log messages without any stackTrace calls.
* It handles a single log file - fit for products with one log file.
* It enables all the log4j regular APIs,
* @author ziv
*
*/
public class Log4jHandler{
Logger classLogger;
protected String projectName = "Main"; //default value
private static Log4jWrapper log4jWrapper = null;
private static Log4jFileChangeListener fileChangeListener = null;
boolean bFirst = false;
/**
* Constructor for the log handler
* @param className - name of the class declaring Log4jWrapper for the log messages.
* This constructor inits the logger with all values from the log4j.properties file.
*/
public Log4jHandler(String className){
if (log4jWrapper == null)
bFirst = true;
log4jWrapper = Log4jWrapper.getInstance();
if ( className != null && className.length() > 0 )
{
classLogger = LogManager.getLogger(className);
fileChangeListener = Log4jFileChangeListener.getInstance();
if ( bFirst )//init first time only
{
//if ("mcz.util.InitContextListener".equals(className) || "mcz.web.MczSessionListener".equals(className))
//return;//don't init first time from InitContextListener as config.properties isn't loaded yet.
log4jWrapper.init();
fileChangeListener.start(null);
this.info("logger for class " + className + " of the main project has initialized successfully and prints in file " + log4jWrapper.getFileName());
}
else
this.info("logger for class " + className + " of the main project has been added as a client to print in file " + log4jWrapper.getFileName());
}
}
/**
* Constructor for the log handler
* @param projectName - name of the project printed in the file. Use this if you need a different project name than the default name.
* @param className - name of the class declaring Log4jWrapper for the log messages.
* This constructor inits the logger with default level of DEBUG (all logs are printed), and
* with the log file path defined in config.properties under log4j.dir.path.
*/
public Log4jHandler(String projName, String className){
if (log4jWrapper == null)
bFirst = true;
if ( projName != null && projName.length() > 0 )
{
projectName = projName;
log4jWrapper = Log4jWrapper.getInstance();
if ( className != null && className.length() > 0 )
{
classLogger = LogManager.getLogger(className);
if ( bFirst )//init first time only
{
log4jWrapper.init();
this.info("logger for class " + className + " of project " + projectName + " has initialized successfully and prints in file " + log4jWrapper.getFileName());
}
else
this.info("logger for class " + className + " of project " + projectName + " has been added as a client to print in file " + log4jWrapper.getFileName());
}
}
}
/**
* Constructor for the log handler - this is the API to the log for all classes.
* @param projectName - name of the log file to be printed.
* @param className - name of the class declaring ProductLog4jWrapper for the log messages.
* @param severityLevel - must supply a valid level.
* @param logPath - supply a valid path or 'null' to get the log file path defined in config.properties under log4j.dir.path.
*/
public Log4jHandler(String projName, String className, Level severityLevel, String logPath){
if (log4jWrapper == null)
bFirst = true;
if ( projName != null && projName.length() > 0 )
{
projectName = projName;
log4jWrapper = Log4jWrapper.getInstance();
if ( className != null && className.length() > 0 )
{
classLogger = LogManager.getLogger(className);
if ( bFirst )//init first time only
{
if ( logPath != null && logPath.length()>0 )
log4jWrapper.init(logPath, severityLevel);
else
{//init with the config.properties log path
log4jWrapper.init();
//log4jWrapper.setSeverityLevel(severityLevel);
}
this.info("logger for class " + className + " of project " + projectName + " has initialized successfully and prints in file " + log4jWrapper.getFileName());
}
else
this.info("logger for class " + className + " of project " + projectName + " has been added as a client to print in file " + log4jWrapper.getFileName());
}
}
}
public void log(Level level, String msg)
{
log4jWrapper.log(level, msg, classLogger);
}
public void log(Level level, String msg, Exception e)
{
log4jWrapper.log(level, msg, e, classLogger);
}
/**
* Sends a FATAL log message
* Levels: * FATAL *, error, warn, info, debug, trace
*/
public void fatal(String msg)
{
log(Level.FATAL, msg);
}
public void fatal(String msg, Exception e)
{
log(Level.FATAL, msg, e);
}
/**
* Sends a FATAL log message
* For backwards compatibility only. Use fatal(msg) instead.
*/
public void severe(String msg)
{
log(Level.FATAL, msg);
}
public void severe(String msg, Exception e)
{
log(Level.FATAL, msg, e);
}
/**
* Sends an ERROR log message
* Levels: fatal, * ERROR *, warn, info, debug, trace
*/
public void error(String msg)
{
log(Level.ERROR, msg);
}
public void error(String msg, Exception e)
{
log(Level.ERROR, msg, e);
}
/**
* Sends a WARN log message
* Levels: fatal, error, * WARN *, info, debug, trace
*/
public void warning(String msg)
{
log(Level.WARN, msg);
}
public void warning(String msg, Exception e)
{
log(Level.WARN, msg, e);
}
/**
* Sends an INFO log message
* Levels: fatal, error, warn, * INFO *, debug, trace
*/
public void info(String msg)
{
log(Level.INFO, msg);
}
public void info(String msg, Exception e)
{
log(Level.INFO, msg, e);
}
/**
* Sends a INFO log message
* For backwards compatibility only. Use info(msg) instead.
*/
public void fine(String msg)
{
log(Level.INFO, msg);
}
public void fine(String msg, Exception e)
{
log(Level.INFO, msg, e);
}
/**
* Sends a DEBUG log message
* Levels: fatal, error, warn, info, * DEBUG *, trace
*/
public void debug(String msg)
{
log(Level.DEBUG, msg);
}
public void debug(String msg, Exception e)
{
log(Level.DEBUG, msg, e);
}
/**
* Sends a DEBUG log message
* For backwards compatibility only. Use debug(msg) instead.
*/
public void finer(String msg)
{
log(Level.DEBUG, msg);
}
public void finer(String msg, Exception e)
{
log(Level.DEBUG, msg, e);
}
/**
* Sends a TRACE log message
* Levels: fatal, error, warn, info, debug, * TRACE *
*/
public void trace(String msg)
{
log(Level.TRACE, msg);
}
public void trace(String msg, Exception e)
{
log(Level.TRACE, msg, e);
}
/**
* Sends a TRACE log message
* For backwards compatibility only. Use trace(msg) instead.
*/
public void finest(String msg)
{
log(Level.TRACE, msg);
}
public void finest(String msg, Exception e)
{
log(Level.TRACE, msg, e);
}
public boolean changeFormat(String conversionPattern)
{
return log4jWrapper.changeFormat(conversionPattern);
}
/**
* @param severityLevel(org.apache.log4j.Level)
* Log Severity levels in descending order:
* fatal, error, warn, info, debug, trace.
*/
public void setSeverityLevel(String string) {
log4jWrapper.setSeverityLevel(string);
}
public void loadXMLFile() {
if (log4jWrapper.getLogger() == null)
log4jWrapper = Log4jWrapper.getInstance();
log4jWrapper.loadXMLFile();
}
/* The following table associates JUL levels with log4j levels:
* // JUL log4j
*==================
* SEVERE = fatal
* error
* WARNING = warn
* INFO = info
* CONFIG
* FINE = debug
* FINER = trace
* FINEST = trace
*/
}
package utils.common;
import org.apache.log4j.FileAppender;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
/**
* Log4jWrapper is a singleton directly handling the log4j logger.
* @author ziv *
*/
public class Log4jWrapper {
protected static Logger logger = null;
protected FileAppender defaultFileHandler = null;
protected PatternLayout layout = new PatternLayout();
protected static String conversionPattern = "%d{yyyy-MM-ddTHH:mm:ss.SSSS} %-6p: %m%n";
static String dirPath = null;
protected static String logFileName = "LogEvents";//default value
final String logFileSuffix = ".log";
protected String sevLevel = null;
Level defaultSeverityLevel = Level.DEBUG; //default value
private final static String LOG4J_PROP_FILE_PATH;
static {
LOG4J_PROP_FILE_PATH = System.getProperty("log4j.conf.file.path", "/logs/conf/log4j.xml");
}
private static Log4jWrapper instance = null;
private static Object mutex = new Object();
private Log4jWrapper(){
}
public Logger getLogger() {
return logger;
}
/**
* Get new instance only if the file name is new.
* @return
*/
public static Log4jWrapper getInstance(){
if (instance == null) {
synchronized (mutex){
if (instance == null)
instance = new Log4jWrapper();
}
}
return instance;
}
/**
* Log constructor - write logging events with DEBUG level and up in dirPath with the specified log file name.
*/
public void init() {
Initialize(dirPath, Level.DEBUG);
}
/**
* @param logName - name of the specific log fileinfo
* @param logPath - full path up to the log file name
* @param severityLevel - severity of this logger (fatal, error, warn, info, debug, trace).
* All logging events to this logger with this priority or higher are printed.
* Use: Level.FATAL, Level.ERROR, Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE
*/
public void init(String logPath, Level severityLevel) {
Initialize(logPath, severityLevel);
}
private boolean Initialize(String logPath, Level severityLevel)
{
loadXMLFile();
logger = LogManager.getLogger(Log4jWrapper.class.getName());
return true;
}
public void loadXMLFile() {
try {
org.apache.log4j.xml.DOMConfigurator.configure(LOG4J_PROP_FILE_PATH);
}catch(Exception e) {
logger.fatal( "Log4j wrapper failed to load XML File with exception: ", e);
}
}
/**
* @param sevLev - string with one of the following values (case insensitive): fatal, error, warn, info, debug, trace.
* @return log4J.Level represented by this string, or null if value is illegal.
*/
public Level GetSeverityLevelFromString(String sevLev) {
if (sevLev.equalsIgnoreCase("fatal"))
return Level.FATAL;
if (sevLev.equalsIgnoreCase("error"))
return Level.ERROR;
if (sevLev.equalsIgnoreCase("warn"))
return Level.WARN;
if (sevLev.equalsIgnoreCase("info"))
return Level.INFO;
if (sevLev.equalsIgnoreCase("debug"))
return Level.DEBUG;
if (sevLev.equalsIgnoreCase("trace"))
return Level.TRACE;
return null;
}
public void setSeverityLevel(String sevLev) {
logger.setLevel(GetSeverityLevelFromString(sevLev));
}
// /**
// * Change severity level of the root logger and all the loggers in the system too.
// * @param severityLevel
// */
// public void setSeverityLevel(Level severityLevel) {
// if ( severityLevel != null ){
// Enumeration allLoggers = logger.getLoggerRepository().getCurrentCategories();
//
// logger.setLevel(severityLevel);
//
// while (allLoggers.hasMoreElements()){
// Category tmpLogger = (Category) allLoggers.nextElement();
// tmpLogger.setLevel(Level.ERROR);
// }
// }
// }
/**
* Determines the standard logging events format.
* Currently it's hard coded but it may be configured within the log4j.properties file later.
*/
public void format()
{
if (conversionPattern == null)//never supposed to happen
return;
layout.setConversionPattern(conversionPattern);
defaultFileHandler.setLayout(layout);
}
private static boolean setConversionPattern(String convPattern)
{
if (convPattern != null && !convPattern.isEmpty() )
{
conversionPattern = convPattern;
return true;
}
return false;
}
/**
* Change the logging events format.
* Check out https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html for details.
* @param conversionPattern
* @return true if successful, false otherwise.
*/
public boolean changeFormat(String conversionPattern)
{
if (setConversionPattern(conversionPattern))
{
format();
return true;
}
return false;
}
public String getFileName()
{
return logFileName;
}
public void log(Level level, String msg, Logger tmpLogger)
{
if (tmpLogger != null)
tmpLogger.log(level, msg);
}
public void log(Level level, String msg, Exception e, Logger tmpLogger)
{
if (tmpLogger != null)
tmpLogger.log(level, msg, e);
}
}//end of class
package utils.common;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Network {
public static final String LOCALHOST = "localhost";
public static String getLocalIpAddress()
{
String host = null;
InetAddress myIp;
try {
myIp = InetAddress.getLocalHost();
host = myIp.getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return host;
}
}
package utils.common;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisCacheClient implements CacheClient
{
private static int CACHE_EXPIRATION_TIMEOUT;
public static int INITIAL_CAPACITY = 64;
private static int DEFAULT_REDIS_DATABASE_INDEX;
static {
CACHE_EXPIRATION_TIMEOUT = Integer.valueOf(System.getProperty("cache.expiration.timeout","300")).intValue();
DEFAULT_REDIS_DATABASE_INDEX = Integer.valueOf(System.getProperty("cache.redis.database.index.timeout","1")).intValue();
}
private JedisPool jedisPool = null;
public RedisCacheClient()
{
initPool("localhost");
}
public RedisCacheClient(String host)
{
initPool(host);
}
private void initPool(String host) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(200);
jedisPoolConfig.setMaxIdle(100);
jedisPoolConfig.setMinIdle(50);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setMaxWaitMillis(500);
jedisPool = new JedisPool(jedisPoolConfig, host);
}
private Jedis getJedis()
{
Jedis jedis = jedisPool.getResource();
jedis.select(DEFAULT_REDIS_DATABASE_INDEX);
return jedis;
}
@Override
protected void finalize() throws Throwable
{
// TODO Auto-generated method stub
super.finalize();
}
@Override
public void set(String key, String value)
{
set(key,value,CACHE_EXPIRATION_TIMEOUT);
}
@Override
public void set(String key, String value, int expiration)
{
Jedis jedis = getJedis();
try {
/// ... do stuff here ... for example
value = jedis.setex(key,expiration,value);
} finally {
/// ... it's time to release alive/broken Jedis instance...
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}
}
@Override
public void setExpiration(String key, int expiration)
{
Jedis jedis = getJedis();
try {
/// ... do stuff here ... for example
jedis.expire(key, expiration); // extend time
} finally {
/// ... it's time to release alive/broken Jedis instance...
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}
}
@Override
public String get(String key)
{
String cacheValue = null;
Jedis jedis = getJedis();
try {
/// ... do stuff here ... for example
cacheValue = jedis.get(key);
} finally {
/// ... it's time to release alive/broken Jedis instance...
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}
return cacheValue;
}
@Override
public void delete(String key)
{
Jedis jedis = getJedis();
try
{
jedis.del(key);
}
finally
{
/// ... it's time to release alive/broken Jedis instance...
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}
}
@Override
public void deleteByPattern(String keyPattern)
{
Jedis jedis = getJedis();
try
{
Set<String> keys = jedis.keys(keyPattern);
for (String redisKey : keys)
jedis.del(redisKey);
}
finally
{
/// ... it's time to release alive/broken Jedis instance...
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}
}
@Override
public String[] getByPattern(String keyPattern) {
String[] keysArray = null;
Jedis jedis = getJedis();
try
{
Set<String> keys = jedis.keys(keyPattern);
if (keys != null)
keysArray = keys.toArray(new String[keys.size()]);
}
finally
{
/// ... it's time to release alive/broken Jedis instance...
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}
return keysArray;
}
}
package utils.common;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class SimpleLogger {
protected Logger logger = null;
protected FileHandler defaultFileHandler = null;
public SimpleLogger() {
}
public SimpleLogger(String logName, String logPath, Level debugLevel) {
Initialize(logName, logPath, debugLevel);
}
public boolean Initialize(String logName, String logPath, Level debugLevel)
{
logger = Logger.getLogger(logName);
logger.setLevel(debugLevel);
try {
// log to a specific location
String LogFile = logPath + "/" + logName + ".log";
defaultFileHandler = new FileHandler(LogFile);
logger.addHandler(defaultFileHandler);
format();
} catch (IOException e) {
logger.severe("Failed to initialize: " + logName + "e:" + e.getMessage().toString());
e.printStackTrace();
}
return true;
}
public void setDebugLevel(Level debugLevel) {
logger.setLevel(debugLevel);
}
/*
* a hook for determine the line format. you can override the 'format' method
* and change the line format to your needs
*/
public void format()
{
// format the log message with a simple formatter
defaultFileHandler.setFormatter(new SimpleFormatter());
}
public void log(Level level, String msg)
{
logger.log(level, msg);
}
public void log(Level level, String msg, Exception e)
{
logger.log(level, msg, e);
}
// public Level CovertToEnum(String level) {
// switch (level) {
// case "ALL":
// return Level.ALL;
//
// case "INFO":
// return Level.INFO;
//
// case "WARNING":
// return Level.WARNING;
//
// case "SEVERE":
// return Level.SEVERE;
//
// case "SEVERE":
// return Level.;
//
//
// }
// return null;
// }
}
package utils.common;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XmlHelper {
private SimpleLogger logger = null;
public XmlHelper(SimpleLogger logger) {
this.logger = logger;
}
public Document LoadXMLDoc(String file) throws SAXException, IOException, ParserConfigurationException {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File(file));
return doc;
} catch (SAXException e) {
String msg = "LoadServiceIndexFile - SAX Error. File: " + file;
logger.log(Level.SEVERE, msg, e);
return null;
}
}
}
package utils.common.configuration;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
public class ConfigProperties
{
public static final String CONFIG_FILE_LOCATION;
static {
CONFIG_FILE_LOCATION = System.getProperty("configFile.location",null);
}
static ConfigProperties instance = null;
Map<String,PropertyEntry> propsHash = new HashMap<String, PropertyEntry>();
ListenThread listenThread = null;
List<ConfigPropertiesChangeListener> propsChangeListeners = null;
List<IConfigurationProvider> configurationProviderList = null;
public class ListenThread extends Thread
{
final long pollingInterval = 5 * 1000;
final FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval);
FileAlterationObserver fao = null;
final String fileToListen;
public ListenThread(String fileName)
{
super();
fileToListen = fileName;
this.setName("ConfigProperties.ListenThread");
String directoryName = fileName.substring(0, fileName.lastIndexOf('/'));
final File directory = new File(directoryName);
fao = new FileAlterationObserver(directory);
FileAlterationListener listener = new FileAlterationListenerAdaptor(){
@Override
public void onFileCreate(File file) {
}
// Is triggered when a file is deleted from the monitored folder
@Override
public void onFileDelete(File file) {
}
@Override
public void onFileChange(File file)
{
System.out.println("File changed :" + file.getName());
if (file.getPath().equals(fileToListen))
{
reloadProperties();
notifyListeners();
}
super.onFileChange(file);
}
};
fao.addListener(listener);
monitor.addObserver(fao);
System.out.println("Starting monitor...");
try {
monitor.start();
} catch (Exception e) {
System.out.println("Could not start..." + e);
}
// Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
//
// @Override
// public void run() {
// try {
// //logger.info("Stopping monitor.");
// monitor.stop();
// } catch (Exception ignored) {
// }
// }
// }));
}
@Override
public void run()
{
// synchronized(this)
// {
// try
// {
// wait();
// } catch (InterruptedException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}
@Override
public void destroy()
{
try
{
System.out.println("Stopping monitor...");
monitor.stop();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ConfigProperties()
{
if (listenThread == null)
{
//logger.info("staring Log4jFileChangeListener thread...");
listenThread = new ListenThread( CONFIG_FILE_LOCATION );
listenThread.start();
}
propsChangeListeners = new LinkedList<ConfigPropertiesChangeListener>();
configurationProviderList = new LinkedList<IConfigurationProvider>();
}
@Override
protected void finalize() throws Throwable
{
// TODO Auto-generated method stub
listenThread.destroy();
listenThread.join(2000);
listenThread = null;
super.finalize();
}
public static synchronized void createInstance()
{
if (instance == null)
instance = new ConfigProperties();
}
public static ConfigProperties getInstance()
{
if (instance == null)
createInstance();
return instance;
}
public void stop()
{
try
{
listenThread.destroy();
listenThread.join(2000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
listenThread = null;
instance = null;
}
public void addChangeListener(ConfigPropertiesChangeListener listener)
{
propsChangeListeners.add(listener);
}
/**
* load properties to env
*/
public void loadProperties()
{
if (CONFIG_FILE_LOCATION != null)
{
// set up new properties object
// from file "myProperties.txt"
FileInputStream propFile;
try
{
propFile = new FileInputStream(CONFIG_FILE_LOCATION);
Properties p = new Properties(System.getProperties());
p.load(propFile);
addPropertiesFromProviders(p);
// set the system properties
System.setProperties(p);
// display new properties
System.getProperties().list(System.out);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void addPropertiesFromProviders(Properties p) {
for (IConfigurationProvider provider: configurationProviderList)
{
for (Entry<String, String> propEntry: provider.getAllProperties())
{
p.setProperty(propEntry.getKey(),propEntry.getValue());
}
}
}
public Object addConfigurationPropertyToHash(String key,String defaultValue, EnumPropertyType eType)
{
String valueStr = System.getProperty(key, defaultValue);
if (valueStr != null)
{
Object value = null;
switch (eType)
{
case E_STRING:
value = valueStr;
break;
case E_LONG:
value = Long.valueOf(valueStr);
break;
case E_BOOLEAN:
value = Boolean.valueOf(valueStr);
break;
}
if (value != null)
{
PropertyEntry propEntry = new PropertyEntry(value, defaultValue, eType);
propsHash.put(key, propEntry);
return value;
}
}
return null;
}
public Object getConfigurationProperty(String key)
{
PropertyEntry pe = propsHash.get(key);
if (pe != null)
return pe.value;
return null;
}
/**
* reloading config properties into hash
*/
public synchronized void reloadProperties()
{
loadProperties();
Set<Entry<String, PropertyEntry>> entrySet = propsHash.entrySet();
for (Entry<String, PropertyEntry> entry : entrySet)
{
if (entry != null)
addConfigurationPropertyToHash(entry.getKey(),entry.getValue().defaultValue,entry.getValue().eType);
}
}
public void notifyListeners()
{
for (ConfigPropertiesChangeListener listener : propsChangeListeners)
{
if (listener != null)
listener.propertiesChange();
}
}
public void addConfigurationProvider(IConfigurationProvider iProvider)
{
configurationProviderList.add(iProvider);
}
}
package utils.common.configuration;
public interface ConfigPropertiesChangeListener
{
public void propertiesChange();
}
package utils.common.configuration;
/**
* Created by amir on 04/04/16.
*/
public enum EnumPropertyType {
E_STRING,
E_LONG,
E_BOOLEAN
}
package utils.common.configuration;
import java.util.List;
import java.util.Map;
/**
* Created by amir on 04/04/16.
*/
public interface IConfigurationProvider {
List<Map.Entry<String,String>> getAllProperties();
String getPropertyAsString(String key);
}
package utils.common.configuration;
/**
* Created by amir on 04/04/16.
*/
public class PropertyEntry {
public PropertyEntry(Object value, String defaultValue, EnumPropertyType eType) {
super();
this.value = value;
this.defaultValue = defaultValue;
this.eType = eType;
}
public Object value;
public String defaultValue;
public EnumPropertyType eType;
}
/**
*
*/
/**
* @author adi
*
*/
package utils.common;
\ No newline at end of file
package utils.common;
import java.util.UUID;
public class uniqueKeyHandler {
public static String generateUuid()
{
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
}
package utils.http;
public class HttpConst {
public static enum HttpResponseCode {
E_DEFAULT(0),
E_OK(200),
E_ACCEPTED(202),
E_PARTIAL_INFORMATION(203),
E_BAD_REQUEST(400),
E_NOT_FOUND(404),
E_INTENAL_ERROR(500),
E_NOT_IMPLMENETED(501);
public static HttpResponseCode StrToE(String val) {
if (val == "0")
return E_DEFAULT;
else if (val.equals("200"))
return E_OK;
else if (val.equals("202"))
return E_ACCEPTED;
else if (val.equals("203"))
return E_PARTIAL_INFORMATION;
else if (val.equals("400"))
return E_BAD_REQUEST;
else if (val.equals("404"))
return E_NOT_FOUND;
else if (val.equals("500"))
return E_INTENAL_ERROR;
else if (val.equals("501"))
return E_NOT_IMPLMENETED;
return null;
}
private int value;
private HttpResponseCode(int value) {
this.value = value;
}
public int getValue() { return value; }
}
}
package utils.http;
public class StringResponse {
private int statusCode = 0;
private String content = null;;
public StringResponse(int statusCode, String content) {
this.statusCode = statusCode;
this.content = content;
}
public int getStatusCode() {
return this.statusCode;
}
public String getContent() {
return this.content;
}
}
package utils.http.simpleHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import javax.ws.rs.core.HttpHeaders;
//import javax.ws.rs.core.MediaType;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
//import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import utils.http.simpleHttpClient.SimpleHttpRequest;
import utils.http.simpleHttpClient.SimpleHttpResponse;
public class SimpleHttpClient {
private DefaultHttpClient defaulthttpClient = null;
private ThreadSafeClientConnManager connManager = null;
private CookieStore cookieStore = null;
public SimpleHttpClient()
{
}
public void Initialize(int maxConnection)
{
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
registry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
connManager = new ThreadSafeClientConnManager(registry);
connManager.setDefaultMaxPerRoute(maxConnection);
connManager.setMaxTotal(maxConnection);
this.defaulthttpClient = new DefaultHttpClient(connManager);
this.cookieStore = defaulthttpClient.getCookieStore();
}
public void shutdown() throws Throwable
{
defaulthttpClient.getConnectionManager().shutdown();
}
public SimpleHttpResponse processRequest(SimpleHttpRequest request) throws UnsupportedEncodingException {
HttpUriRequest uriRequest = request.build();
org.apache.http.HttpResponse resultResp = null;
SimpleHttpResponse response = null;
try {
// add cookies
if (request.getCookies().size() > 0) {
for (Cookie cookie: request.getCookies()) {
cookieStore.addCookie(cookie);
}
}
defaulthttpClient.setCookieStore(cookieStore);
// send the request
resultResp = defaulthttpClient.execute(uriRequest);
if (resultResp != null) {
response = ParseResponseAndCookies(resultResp, defaulthttpClient.getCookieStore());
return response;
}
} catch (Exception e) {
return new SimpleHttpResponse(500, e.toString());
}
return null;
}
public SimpleHttpResponse ParseResponseAndCookies(org.apache.http.HttpResponse response, CookieStore cookieStore) {
SimpleHttpResponse resp = new SimpleHttpResponse();
// get status code
resp.setStatusCode(response.getStatusLine().getStatusCode());
// get headers
resp.headers = new HashMap<String, String>();
Header[] headers = response.getAllHeaders();
for (Header hdr: headers) {
resp.headers.put(hdr.getName(), hdr.getValue());
}
// get cookies
List<Cookie> cookies = cookieStore.getCookies();
resp.setCookies(cookies);
// get content
try {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
String content = result.toString();
resp.setContent(content);
}
} catch (IOException e) {
return resp;
}
return resp;
}
}
package utils.http.simpleHttpClient;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
//import javax.ws.rs.core.HttpHeaders;
//import javax.ws.rs.core.MediaType;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import com.google.gdata.util.common.base.PercentEscaper;
public class SimpleHttpRequest {
private static PercentEscaper pes = new PercentEscaper(PercentEscaper.SAFEQUERYSTRINGCHARS_URLENCODER/*SAFECHARS_URLENCODER*/, false);
public enum Method {
GET{
public String toString() {
return "GET";
}
},
POST{
public String toString() {
return "POST";
}
},
PUT{
public String toString() {
return "PUT";
}
},
DELETE{
public String toString() {
return "DELETE";
}
},
}
protected String protocol = "http"; // default is http
protected Method method = null;
protected String domain = null;
protected int port = 0;
protected String path = null;
protected String queryString = null;
protected Map<String, String> headers = null;
protected String contentType = "application/x-www-form-urlencoded; charset=UTF-8";
protected String content = null;
protected List<Cookie> cookies = null;
public SimpleHttpRequest() {
headers = new HashMap<String, String>();
cookies = new ArrayList<Cookie>();
}
// get/set methods
public String getProtocol() { return protocol; }
public void setProtocol(String protocol) { this.protocol = protocol; }
public Method getMethod() { return method; }
public void setMethod(Method method) { this.method = method; }
public String getDomain() { return domain; }
public void setDomain(String domain) { this.domain = domain; }
public int getPort() { return port; }
public void setPort(int port) { this.port = port; }
public String getQueryString() { return queryString; }
public void setQueryString(String queryString) { this.queryString = EscapeQueryString(queryString); /*this.queryString = pes.escape(queryString);*/ }
public String getContentType() { return contentType;}
public void setContentType(String contentType) {this.contentType = contentType; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getPath() { return path; }
public void setPath(String path) { this.path = pes.escape(path); }
public void addHeader(String hdrName, String hdrValue) {
headers.put(hdrName, hdrValue);
}
public Map<String, String> getHeaders() { return headers; }
public List<Cookie> getCookies() {
return cookies;
}
public void setCookies(List<Cookie> cookies) {
this.cookies = cookies;
}
public void addCookie(Cookie cookie) {
this.cookies.add(cookie);
}
public void InitQueryStringBuilder() {
this.queryString = "";
}
public void addQueryStringParam(String name, String value) {
String escName = pes.escape(name);
String escValue = pes.escape(value);
if (!queryString.equals(""))
queryString += "&";
queryString += escName + "=" + escValue;
}
private String EscapeQueryString(String queryString) {
StringBuilder sb = new StringBuilder();
String[] paramTokens = queryString.split("&");
if (paramTokens != null) {
for (String param: paramTokens) {
// split the param into name and value
String[] fieldTokens = new String[2];
int equalPos = param.indexOf('=');
if (equalPos > 0) {
fieldTokens[0] = param.substring(0, equalPos);
fieldTokens[1] = param.substring(equalPos + 1);
if (fieldTokens != null && fieldTokens.length == 2) {
String field = pes.escape(fieldTokens[0]) + "=" + pes.escape(fieldTokens[1]);
if (sb.length() > 0)
sb.append("&");
sb.append(field);
}
}
else
sb.append(pes.escape(param));
}
return sb.toString();
}
else
return pes.escape(queryString);
}
public HttpUriRequest build() throws UnsupportedEncodingException {
switch (method) {
case GET:
return buildGetRequest();
case POST:
return buildPostRequest();
case PUT:
return buildPutRequest();
case DELETE:
return buildDeleteRequest();
}
return null;
}
// http://domain:port/path?query_string
protected HttpUriRequest buildGetRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
sb.append(port);
}
if (path != null) {
sb.append("/");
sb.append(path);
}
if (queryString != null) {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpGet getRequest = new HttpGet(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
getRequest.addHeader(hdr.getKey(), hdr.getValue());
}
return getRequest;
}
protected HttpUriRequest buildPostRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
sb.append(port);
}
sb.append("/");
if (path != null)
sb.append(path);
if (queryString != null) {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpPost postRequest = new HttpPost(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
postRequest.addHeader(hdr.getKey(), hdr.getValue());
}
// add content
if (content != null) {
try {
StringEntity contentEntity = new StringEntity(content,"UTF-8");
contentEntity.setContentType(contentType);
postRequest.setEntity(contentEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return postRequest;
}
protected HttpUriRequest buildPutRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
sb.append(port);
}
sb.append("/");
if (path != null)
sb.append(path);
if (queryString != null) {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpPut putRequest = new HttpPut(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
putRequest.addHeader(hdr.getKey(), hdr.getValue());
}
// add content
if (content != null) {
try {
StringEntity contentEntity = new StringEntity(content);
//contentEntity.setContentType("application/json");
contentEntity.setContentType(contentType);
putRequest.setEntity(contentEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return putRequest;
}
protected HttpUriRequest buildDeleteRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
sb.append(port);
}
sb.append("/");
if (path != null)
sb.append(path);
if (queryString != null) {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpDelete deleteRequest = new HttpDelete(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
deleteRequest.addHeader(hdr.getKey(), hdr.getValue());
}
return deleteRequest;
}
}
package utils.http.simpleHttpClient;
import java.util.List;
import java.util.Map;
import org.apache.http.cookie.Cookie;
public class SimpleHttpResponse {
protected int statusCode = 0;
protected Map<String, String> headers;
protected List<Cookie> cookies;
protected String content = null;
public SimpleHttpResponse() {
}
public SimpleHttpResponse(int statusCode, String content) {
this.statusCode = statusCode;
this.content = content;
}
public List<Cookie> getCookies() { return cookies; }
public void setCookies(List<Cookie> cookies) { this.cookies = cookies;}
public int getStatusCode() { return statusCode; }
public void setStatusCode(int statusCode) { this.statusCode = statusCode; }
public Map<String, String> getHeaders() { return headers; }
public void setHeaders(Map<String, String> headers) { this.headers = headers; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
}
package utils.http.simpleRestClient;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import utils.http.StringResponse;
import utils.http.simpleHttpClient.SimpleHttpClient;
import utils.http.simpleHttpClient.SimpleHttpRequest;
import utils.http.simpleHttpClient.SimpleHttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gdata.util.common.base.PercentEscaper;
public class SimpleRestClient extends SimpleHttpClient {
private static final String EMPTY_RESPONSE = "Empty response";
protected String domain = null;
protected String app = null;
protected int port = 0;
protected ObjectMapper objMapper = new ObjectMapper();
protected Map<String,String> headers = null;
public SimpleRestClient(String app, String ip, int port) {
super();
this.app = app;
this.domain = ip;
this.port = port;
}
public SimpleRestClient(String app, String IpPort) {
super();
this.app = app;
String[] parts = IpPort.split(":");
this.domain = parts[0];
this.port = Integer.parseInt(parts[1]);
}
public void setCustomHeader(String headerName, String headerValue) {
if (this.headers == null)
this.headers = new HashMap<String, String>();
this.headers.put(headerName, headerValue);
}
/******************************************************************************
*
* HTTP get methods
*
*****************************************************************************/
public SimpleRestResponse get(String entity, String[] params, String requestParams) {
String paramPath = "";
if (params != null) {
for (int i=0; i<params.length; i++) {
paramPath += "/" +params[i];
}
return get(entity, paramPath, requestParams);
}
else
return get(entity, (String)null, requestParams);
}
public SimpleRestResponse get(String entity, String params, String requestParams) {
SimpleHttpRequest request = buildGetRequest(entity, params, requestParams);
try {
SimpleHttpResponse httpResp = processRequest(request);
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public StringResponse getAsString(String entity, String[] params, String requestParams) {
String paramPath = "";
if (params != null) {
for (int i=0; i<params.length; i++) {
paramPath += "/" + params[i];
}
return getAsString(entity, paramPath, requestParams);
}
else
return getAsString(entity, (String)null, requestParams);
}
public StringResponse getAsString(String entity, String params, String requestParams) {
SimpleHttpRequest request = buildGetRequest(entity, params, requestParams);
try {
SimpleHttpResponse resp = processRequest(request);
if (resp != null)
return new StringResponse(resp.getStatusCode(), resp.getContent());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/******************************************************************************
*
* HTTP post methods
*
*****************************************************************************/
public SimpleRestResponse post(String entity, String[] params, String requestParams, String content) {
String paramPath = "";
if (params != null) {
for (int i=0; i<params.length; i++) {
paramPath += "/" + params[i];
}
return post(entity, paramPath, requestParams, content);
}
else
return post(entity, (String)null, requestParams, content);
}
public SimpleRestResponse post(String entity, String params, String requestParams, String content) {
SimpleHttpRequest request = buildPostRequest(entity, params, requestParams, content);
try {
SimpleHttpResponse httpResp = processRequest(request);
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String postAsString(String entity, String[] params, String requestParams, String content) {
String paramPath = "";
if (params != null) {
for (int i=0; i<params.length; i++) {
paramPath += "/" + params[i];
}
return postAsString(entity, paramPath, requestParams, content);
}
else
return postAsString(entity, (String[])null, requestParams, content);
}
public String postAsString(String entity, String params, String requestParams, String content) {
SimpleHttpRequest request = buildPostRequest(entity, params, requestParams, content);
try {
SimpleHttpResponse resp = processRequest(request);
if (resp != null)
return resp.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/******************************************************************************
*
* HTTP put methods
*
*****************************************************************************/
public SimpleRestResponse put(String entity, String[] params, String requestParams, String content) {
String paramPath = "";
if (params != null) {
for (int i=0; i<params.length; i++) {
paramPath += "/" + params[i];
}
return put(entity, paramPath, requestParams, content);
}
else
return put(entity, (String)null, requestParams, content);
}
public SimpleRestResponse put(String entity, String params, String requestParams, String content) {
SimpleHttpRequest request = buildPutRequest(entity, params, requestParams, content);
try {
SimpleHttpResponse httpResp = processRequest(request);
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/******************************************************************************
*
* HTTP delete methods
*
*****************************************************************************/
public SimpleRestResponse delete(String entity, String[] params, String requestParams) {
String paramPath = "";
if (params != null) {
for (int i=0; i<params.length; i++)
paramPath += "/" + params[i];
return delete(entity, paramPath, requestParams);
}
else
return delete(entity, (String)null, requestParams);
}
public SimpleRestResponse delete(String entity, String params, String requestParams) {
SimpleHttpRequest request = buildDeleteRequest(entity, params, requestParams);
try {
SimpleHttpResponse httpResp = processRequest(request);
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String deleteAsString(String entity, String[] params, String requestParams) {
String paramPath = "";
if (params != null) {
for (int i=0; i<params.length; i++) {
paramPath += "/" + params[i];
}
return deleteAsString(entity, paramPath, requestParams);
}
else
return deleteAsString(entity, (String)null, requestParams);
}
public String deleteAsString(String entity, String params, String requestParams) {
SimpleHttpRequest request = buildDeleteRequest(entity, params, requestParams);
try {
SimpleHttpResponse resp = processRequest(request);
if (resp != null)
return resp.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/******************************************************************************
*
* helper methods
*
*****************************************************************************/
protected SimpleRestResponse getResponseHandler(String respString)
{
SimpleRestResponse rr = null;
if (respString != null && !respString.isEmpty())
{
try
{
rr = objMapper.readValue(respString, SimpleRestResponse.class);
if (rr != null)
rr.prepare(objMapper);
} catch (Exception e)
{
rr = new SimpleRestResponse(false, e.toString(),null);
e.printStackTrace();
}
}
else
{
rr = new SimpleRestResponse(false, EMPTY_RESPONSE,null);
}
return rr;
}
protected SimpleHttpRequest buildGetRequest(String entity, String params, String requestParams) {
SimpleHttpRequest request = new SimpleHttpRequest();
request.setMethod(SimpleHttpRequest.Method.GET);
request.setDomain(this.domain);
request.setPort(this.port);
String path = buildPath(entity, params, requestParams);
request.setPath(path);
// query string
if (requestParams != null)
request.setQueryString(requestParams);
buildHeaders(request);
//request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
return request;
}
protected SimpleHttpRequest buildPostRequest(String entity, String params, String requestParams, String content) {
SimpleHttpRequest request = new SimpleHttpRequest();
request.setMethod(SimpleHttpRequest.Method.POST);
request.setDomain(this.domain);
request.setPort(this.port);
String path = buildPath(entity, params, requestParams);
request.setPath(path);
// query string
if (requestParams != null)
request.setQueryString(requestParams);
if (content != null)
request.setContent(content);
buildHeaders(request);
// request.addHeader("content-type", "application/x-www-form-urlencoded");
// request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
return request;
}
protected SimpleHttpRequest buildPutRequest(String entity, String params, String requestParams, String content) {
SimpleHttpRequest request = new SimpleHttpRequest();
request.setMethod(SimpleHttpRequest.Method.PUT);
request.setDomain(this.domain);
request.setPort(this.port);
String path = buildPath(entity, params, requestParams);
request.setPath(path);
// query string
if (requestParams != null)
request.setQueryString(requestParams);
if (content != null)
request.setContent(content);
buildHeaders(request);
// request.addHeader("content-type", "application/x-www-form-urlencoded");
// request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
return request;
}
protected SimpleHttpRequest buildDeleteRequest(String entity, String params, String requestParams) {
SimpleHttpRequest request = new SimpleHttpRequest();
request.setMethod(SimpleHttpRequest.Method.DELETE);
request.setDomain(this.domain);
request.setPort(this.port);
String path = buildPath(entity, params, requestParams);
request.setPath(path);
// query string
if (requestParams != null)
request.setQueryString(requestParams);
buildHeaders(request);
// request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
return request;
}
protected void buildHeaders(SimpleHttpRequest request) {
if (headers == null) {
// set default headers
if (request.getMethod() == SimpleHttpRequest.Method.GET) {
request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
}
else if (request.getMethod() == SimpleHttpRequest.Method.POST) {
request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
request.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate");
} else if (request.getMethod() == SimpleHttpRequest.Method.DELETE) {
request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
}
}
else {
Iterator<Entry<String, String>> it = headers.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> header = it.next();
request.addHeader(header.getKey(), header.getValue());
}
}
}
protected String buildPath(String entity, String params, String requestParams) {
// build path
StringBuilder sb = new StringBuilder();
if (this.app != null)
sb.append(this.app);
if (entity != null) {
if (entity.charAt(0) != '/')
sb.append("/" + entity);
else
sb.append(entity);
}
if (params != null) {
if (params.charAt(0) != '/')
sb.append("/" + params);
else
sb.append(params);
}
return sb.toString();
}
}
package utils.http.simpleRestClient;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
//import Util.General;
public class SimpleRestResponse
{
public boolean success = true;
public String error = null;
public JsonNode objectNode = null;
// protected General genUtils = null;
@JsonIgnore
private ObjectMapper objMapper;
public SimpleRestResponse() {
}
@JsonCreator
public SimpleRestResponse(@JsonProperty("success")boolean success,
@JsonProperty("error")String error,
@JsonProperty("respObject")JsonNode objectNode)
{
super();
this.success = success;
this.error = error;
this.objectNode = objectNode;
// this.genUtils = new General();
}
public void prepare(ObjectMapper objMapper) { this.objMapper = objMapper; }
@JsonIgnore
public ObjectMapper getObjectMapper() { return objMapper; }
public boolean isSuccess () { return success; }
public String getError() { return error; }
@JsonIgnore
public String getRawString()
{
if (objectNode != null)
return objectNode.toString();
return null;
}
/**
* getting the object map
* @param objMapper
* @return
*/
@JsonIgnore
public JsonNode getRootNode() { return objectNode; }
/**
* get node by name
* @param name
* @return
*/
public JsonNode getNode(String name)
{
if (objectNode != null)
return objectNode.path(name);
return null;
}
// public String getParamString(String name)
// {
// if (objectNode != null)
// {
// JsonNode jn = objectNode.path(name);
// if (!jn.isMissingNode())
// return genUtils.removeSigns(jn.toString(), '"', '"');
// }
// return null;
// }
public Long getParamLong(String name)
{
if (objectNode != null)
{
JsonNode jn = objectNode.path(name);
if (!jn.isMissingNode() && jn.isNumber())
return new Long(jn.longValue());
}
return null;
}
public Integer getParamInt(String name)
{
if (objectNode != null)
{
JsonNode jn = objectNode.path(name);
if (!jn.isMissingNode() && jn.isNumber())
return new Integer(jn.intValue());
}
return null;
}
public Boolean getParamBool(String name)
{
if (objectNode != null)
{
JsonNode jn = objectNode.path(name);
if (!jn.isMissingNode() && jn.isBoolean())
return new Boolean(jn.booleanValue());
}
return null;
}
public Object getObject(String name, Class<?> objClass) {
ObjectReader objectReader = objMapper.reader(objClass);
if (objectNode != null && objMapper != null) {
JsonNode jn = objectNode.path(name);
if (!jn.isMissingNode()) {
try {
return objectReader.readValue(jn);
} catch (Exception e) {
}
}
}
return null;
}
public Object getNodeAsObject(JsonNode jn, Class<?> objClass) {
ObjectReader objectReader = objMapper.reader(objClass);
if (!jn.isMissingNode() && objMapper != null) {
try {
return objectReader.readValue(jn);
} catch (Exception e) {
}
}
return null;
}
}
package utils.itm;
/*
*
*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.concurrent.Semaphore;
import utils.common.SimpleLogger;
import itc.ItcMessage;
import itc.ItcMessageQueue;
/**
* The SessionManager implements a dispatcher thread & a pool of workers threads
* while each manager a list of sessions
*/
public abstract class ITMManager
{
private final String className = this.getClass().getName();
protected boolean bManagerStarted = false;
protected String moduleName = null;
protected SimpleLogger logger = null;
protected List <ItcMessageQueue> thrdQList = null;
protected List <ITMThread> thrdList = null;
protected int numOfThreads = 0;
protected int maxQueueSize = 0;
protected boolean bRunning = false;
protected int timeout;
protected String logName = null;
protected String logPath = null;
// static protected rabbitmq.server.RMQServerWrapper mRMQServer = null;
Semaphore semThrdStart = null;
/**
* initialize the session manager: including logger & threads
*
* @param nSessionThrds - number of worker threads in the pool
* @param maxQueueSize - maximum of con-current events can be handled within a single thread
* @param timeout - timeout (in mill-secs) which the ReceiveMsg expires
* @param logName - logger file name
* @param logPath - logger file full path
* @return true/false
*/
public boolean Init(int nThrds,
int maxQueueSize,
int timeout,
String moduleName,
SimpleLogger logger)
{
this.numOfThreads = nThrds;
this.maxQueueSize = maxQueueSize;
this.timeout = timeout;
this.moduleName = moduleName;
this.logger = logger;
if (InitListenQ() == false)
return false;
return true;
}
/**
* initialize an abstract listener for incoming messages
*
*/
public abstract boolean InitListenQ();
/**
* initialize an abstract message receiver
*/
public abstract byte[] ReceiveMsg();
/**
* initialize an abstract message receiver
*/
public abstract ITMThread CreateThread();
/**
* start running the thread pool.
*
*/
public boolean start()
{
// avoid restarting the threads
if (bManagerStarted)
return true;
// start the thread
semThrdStart = new Semaphore(-numOfThreads, true);
thrdQList = new ArrayList<ItcMessageQueue>();
thrdList = new ArrayList<ITMThread>();
for (int i = 0; i < numOfThreads; i++)
{
ItcMessageQueue thrdQueue = new ItcMessageQueue(maxQueueSize);
thrdQList.add(thrdQueue);
ITMThread thrd = CreateThread();
String thrdName = moduleName + "Thrd" + i;
thrd.SetMandatoryParams(i, thrdName, thrdQueue, logger, semThrdStart);
thrdList.add(thrd);
// start the threads
thrd.start();
}
// wait until all threads releases their semaphore
try {
semThrdStart.acquire(0);
} catch (InterruptedException e) {
logger.log(Level.SEVERE, "ITMManager::startThreads - Module=" + moduleName + " Threads started");
e.printStackTrace();
}
return true;
}
public synchronized boolean stopManager()
{
if (!bManagerStarted)
return true;
logger.log(Level.WARNING, "Stopping the " + moduleName + " session threads...");
for (ITMThread thrd : thrdList)
{
if (thrd != null)
{
try
{
thrd.stopRunning();
thrd.join(1000);
thrd = null;
} catch (InterruptedException e){ logger.log(Level.SEVERE, e.toString()); }
}
}
thrdList.clear();
logger.log(Level.WARNING, moduleName + " session threads stopped...");
return true;
}
/**
* getting the thread number for the session thread handling this subscriber
* @param key - any string representing a unique key
* @return
*/
public int getThreadNumber(String key)
{
int thrdIndex;
if (key != null && key != null)
{
thrdIndex = Math.abs((key.hashCode() % numOfThreads));
}
else {
thrdIndex = -1; // not found valid thread index
}
return thrdIndex;
}
public boolean Run()
{
// run the thread pool
if (start() == false)
return false;
logInfo("ITMManager:Run", "Manager started successfully");
bManagerStarted = true;
bRunning = true;
while (bRunning)
{
try
{
HandleMsg();
} catch (Exception e)
{
logSevere("ITMManager:Run", "Run:HandleMsg failed", e);
}
}
return true;
}
/*
* generic message handler.
* The default implementation handles an ITC message input that dispatches the message
* to one of the threads.
* This behavior can be override in a descendant manager
*/
public void HandleMsg() throws ClassNotFoundException, IOException
{
byte[] baMsg = ReceiveMsg();
if (baMsg != null)
{
DispatchMsgToThread(baMsg);
}
else
{
logSevere("ITMManager:HandleMsg", "Mismatch sigature for incoming msg", null);
}
}
/*
* generic message dispatcher
* The default implementation dispatches a giem message according to a given key
* This behavior can be override in a descendant manager
*/
public void DispatchMsgToThread(byte[] byteMsg) throws ClassNotFoundException, IOException
{
ITMMessage msg = deserializeObject(byteMsg);
// validate mandatory parameters
if (msg.getKey() == null) {
logSevere("ITMManager:DispatchMsgToThread", "DispatchMsgToThread - Failed. key is NULL. opCode: " + msg.getOpCode(), null);
return;
}
int thrdIndex = getThreadNumber(msg.getKey());
if (thrdIndex >= 0 && thrdIndex < numOfThreads)
{
// get the thread's queue
ItcMessageQueue thrdQ = thrdQList.get(thrdIndex);
if (thrdQ != null)
{
ItcMessage itcMsg = new ItcMessage(msg.getOpCode(), byteMsg, null, false);
thrdQ.forwardMsg(itcMsg);
}
else
logSevere("ITMManager:DispatchMsgToThread", "DispatchMsgToThread - Failed to find Q for thrdIndex: " + thrdIndex, null);
}
else
logSevere("ITMManager:DispatchMsgToThread", "Thread index out of range", null);
}
public int getmNumOfSessionThreads()
{
return numOfThreads;
}
public void logInfo(String method, String msg)
{
String logMsg = "[" + moduleName + "]";
if (msg != null)
logMsg += msg;
logger.log(Level.INFO, logMsg);
}
public void logSevere(String method, String msg, Exception e)
{
String logMsg = moduleName;
if (method != null)
logMsg += "@" + method + " - ";
if (msg != null)
logMsg += msg;
if (e != null)
logMsg += e.toString();
logger.log(Level.SEVERE, logMsg);
}
private static ITMMessage deserializeObject(byte[] objectToSerialize) throws IOException, ClassNotFoundException
{
ByteArrayInputStream bis = new ByteArrayInputStream(objectToSerialize);
ObjectInput in = new ObjectInputStream(bis);
Object readObject;
try {
readObject = in.readObject();
} finally {
}
return (ITMMessage) readObject;
}
}
package utils.itm;
import java.io.Serializable;
/*
* base message for use by any implementation that uses the ITM library
* any messages through the ITM must inherit from ITMMessage
*/
public class ITMMessage implements Serializable
{
private static final long serialVersionUID = 1L;
protected int opCode = 0;
protected String Key = null;
public ITMMessage()
{
}
public ITMMessage(int opCode,
String key)
{
this.opCode = opCode;
this.Key = key;
}
public int getOpCode() { return this.opCode; }
public void setOpCode(int opCode) { this.opCode = opCode; }
public String getKey() { return this.Key; }
public void setKey(String key) { this.Key = key;}
}
package utils.itm;
import java.io.IOException;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import com.fasterxml.jackson.core.JsonParseException;
import utils.common.SimpleLogger;
import itc.ItcMessage;
import itc.ItcMessageQueue;
public abstract class ITMThread extends Thread
{
protected final String className = this.getClass().getName();
protected SimpleLogger logger = null;
protected boolean bRunning = true;
protected int thrdIndex = -1;
protected ItcMessageQueue thrdQueue;
protected String thrdName = null;
protected Semaphore thrdStartSemaphore = null;
public abstract void HandleMsg(int opCode, Object msg) throws InterruptedException, JsonParseException, IOException;
public void SetMandatoryParams(int thrdIndex, String thrdName, ItcMessageQueue thrdQueue, SimpleLogger logger, Semaphore thrdStartSemaphore)
{
this.thrdIndex = thrdIndex;
this.logger = logger;
this.thrdName = thrdName;
this.thrdQueue = thrdQueue;
this.thrdStartSemaphore = thrdStartSemaphore;
}
@Override
public void run()
{
thrdStartSemaphore.release();
while (bRunning) {
ItcMessage itcMsg = (ItcMessage)thrdQueue.recieveMsg(true);
if (itcMsg != null) {
try {
HandleMsg(itcMsg.getOpCode(), itcMsg.getMsgObj());
}
catch (Exception e) {
String logMsg = "ITMThread:run - HandleMsg failed. " + "Thrd" + thrdIndex + " e:" + e.getMessage();
logger.log(Level.SEVERE, logMsg);
}
}
else
System.out.println("ITMThread - Received NULL msg");
}
}
@Override
public void destroy()
{
}
public void stopRunning()
{
bRunning = false;
this.interrupt();
}
}
package utils.mtqm;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import utils.common.Log4jHandler;
public class MultiThreadQueueHander
{
private final static Log4jHandler logger = new Log4jHandler(MultiThreadQueueHander.class.getName());
private static MultiThreadQueueHander sInstance = null;
Map<String, MultiThreadQueueManager> mtqmMap = new HashMap<String, MultiThreadQueueManager>();
public static class QueueManagerStatus
{
public int queueMaxSize;
public int numOfThreads;
public int[] currentQueueSize;
}
public synchronized static void createInstance()
{
if (sInstance == null)
sInstance = new MultiThreadQueueHander();
}
public static MultiThreadQueueHander getInstance()
{
if (sInstance == null)
createInstance();
return sInstance;
}
public synchronized MultiThreadQueueManager createMultiThreadQueue(String name, int numOfThreads,int defaultQueueSize)
{
MultiThreadQueueManager mtqm = null;
if (name != null && !name.isEmpty() && numOfThreads > 0 && defaultQueueSize > 0)
{
mtqm = mtqmMap.get(name);
if (mtqm == null)
{
mtqm = new MultiThreadQueueManager();
if (mtqm.startManager(numOfThreads, defaultQueueSize) )
{
mtqmMap.put(name, mtqm);
}
else
logger.error("failed to start queue manager: " + name);
}
}
return mtqm;
}
public MultiThreadQueueManager getMultiThreadQueue(String name)
{
return mtqmMap.get(name);
}
public synchronized void stop()
{
logger.warning("Stopping the managers...");
for (Entry<String, MultiThreadQueueManager> mtqmEntry : mtqmMap.entrySet())
{
if (mtqmEntry != null)
{
mtqmEntry.getValue().stopManager();
}
}
logger.warning("Stopped all managers, clearing...");
mtqmMap.clear();
}
public Map<String, QueueManagerStatus> getCurrentStatus()
{
Map<String, QueueManagerStatus> statusMap = new HashMap<String, QueueManagerStatus>();
for (Entry<String, MultiThreadQueueManager> mtqmEntry : mtqmMap.entrySet())
{
if (mtqmEntry != null)
{
QueueManagerStatus qms = new QueueManagerStatus();
qms.numOfThreads = mtqmEntry.getValue().getNumOfThreads();
qms.queueMaxSize = mtqmEntry.getValue().getQueueSize();
qms.currentQueueSize = mtqmEntry.getValue().getCurrentStatus();
statusMap.put(mtqmEntry.getKey(), qms);
}
}
return statusMap;
}
}
package utils.mtqm;
import itc.ItcMessage;
import itc.ItcMessageQueue;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import utils.common.Log4jHandler;
/**
* this manager holds thread that handle async jobs
* each thread had it's own queue to wait on , recieve the job and handle it
* NOTE: This is not a singletone
* @author amir
* USE: create and call startManager at init
* send job with sendJob
* at destroy call stopManager
*/
public class MultiThreadQueueManager
{
protected List<JobThread> mJobThreadArray = null;
protected List <ItcMessageQueue> mJobQueueArray = null;
private final static Log4jHandler logger = new Log4jHandler(MultiThreadQueueManager.class.getName());
protected int mNumOfThreads = 0;
protected int mQueueSize = 0;
protected int mReceiveBulkSize = 0;
public class JobThread extends Thread
{
ItcMessageQueue queue;
int threadNumber;
private boolean mbKeepRunning = true;
public JobThread(ItcMessageQueue queue, int threadNumber)
{
super();
this.queue = queue;
this.threadNumber = threadNumber;
this.setName("JobThread" + String.valueOf(threadNumber));
}
@Override
public void run()
{
while (mbKeepRunning)
{
try
{
// ItcMessage msg = queue.recieveMsg(true);
Collection<ItcMessage> msgList = new ArrayDeque<ItcMessage>(mReceiveBulkSize);
int msgCount = queue.recieveMsgs(msgList, mReceiveBulkSize);
if (msgCount > 0 && !msgList.isEmpty() )
{
for (ItcMessage msg : msgList)
{
if (msg != null)
handleReceiveMsg(msg);
}
}
} catch (Exception e)
{
logger.info(e.toString());
}
}
}
protected ItcMessageQueue getQueue()
{
return queue;
}
protected int getThreadNumber()
{
return threadNumber;
}
protected void handleReceiveMsg(ItcMessage msg)
{
Runnable runJob = (Runnable) msg.getMsgObj();
if (msg.getFromSender() != null)
logger.info("running job: " + runJob.getClass().getName() + ", sent from: " + msg.getFromSender() +
" on thread: " + this.threadNumber);
runJob.run();
}
@Override
public void destroy()
{
// TODO Auto-generated method stub
}
public void stopRunning()
{
mbKeepRunning = false;
this.interrupt();
}
}
/**
* starting
* @param numOfThreads number of work threads
* @param defaultQueueSize the default size of the queue
* @return
*/
public boolean startManager(int numOfThreads,int defaultQueueSize)
{
mNumOfThreads = numOfThreads;
mQueueSize = defaultQueueSize;
mReceiveBulkSize = mQueueSize / 3;
mJobQueueArray = new ArrayList<ItcMessageQueue>();
mJobThreadArray = new ArrayList<JobThread>();
for (int i = 0; i < mNumOfThreads; i++)
{
ItcMessageQueue queue = new ItcMessageQueue(defaultQueueSize);
mJobQueueArray.add(queue);
JobThread thr = new JobThread(queue,i);
mJobThreadArray.add(thr);
thr.start();
}
return true;
}
public boolean stopManager()
{
logger.warning("Stopping the threads...");
for (JobThread thr : mJobThreadArray)
{
if (thr != null)
{
try
{
thr.stopRunning();
// thr.interrupt();
thr.join(1000);
thr = null;
} catch (InterruptedException e)
{
logger.severe(e.toString());
}
}
}
mJobThreadArray.clear();
logger.warning("threads stopped... and i did it my way!");
return true;
}
/**
* sending the job
* @param opcode
* @param job
* @param extraMsgObj
* @param queueNum the queue to send to, e.g from getQueueNumber(subId)
* @param fromSender
* @return
*/
public boolean sendJob(int opcode, Runnable job, Object extraMsgObj, int queueNum,String fromSender)
{
if (queueNum < mNumOfThreads)
{
return mJobQueueArray.get(queueNum).sendMsg(opcode, job, extraMsgObj, fromSender);
}
else
return false;
}
public int getNumOfThreads()
{
return mNumOfThreads;
}
public int getQueueSize()
{
return mQueueSize;
}
public int getQueueNumber(String key)
{
return (utils.common.General.abs(key.hashCode()) % this.mNumOfThreads);
}
/**
* for round-robin
* @param currentNumber
* @return
*/
public int getNextQueueNumber(int currentNumber)
{
return (++currentNumber % this.mNumOfThreads);
}
public int[] getCurrentStatus()
{
int[] queues = new int[mNumOfThreads];
for (int i = 0; i < mNumOfThreads; i++)
{
queues[i] = mJobQueueArray.get(i).getCurrentWaitingMsgCount();
}
return queues;
}
}
package utils.otm;
public abstract class IOfflineTaskJob implements Runnable
{
private static final long MAX_TARDINESS = 100;
public abstract boolean continueNextTime();
public long getMaxTardiness() { return MAX_TARDINESS; }; // the min time to sufficiently execute the job
public void jobCancelled() {}
}
package utils.otm;
import java.util.TimerTask;
public class OfflineTask extends TimerTask
{
String key;
int queueNumber;
IOfflineTaskJob job;
long delay;
long periodicTO;
int retries;
/**
*
* @param key
* @param queueNumber
* @param job
* @param delay
* @param periodicTO
* @param retries number of retries, or continous if -1
*/
public OfflineTask(String key, int queueNumber, IOfflineTaskJob job,
long delay, long periodicTO, int retries)
{
super();
this.key = key;
this.queueNumber = queueNumber;
this.job = job;
this.delay = delay;
this.periodicTO = periodicTO;
this.retries = retries;
}
public boolean isValid() { return (key != null && job != null); }
@Override
public void run()
{
/*
* check if we need to run the job
*/
if (enoughTimeToRun())
// execute
OfflineTaskManager.getInstance().executeTask(key,queueNumber);
}
public boolean resolveContinue()
{
if (retries > 0)
retries--;
if (retries == 0)
return false;
else
return job.continueNextTime();
}
public boolean enoughTimeToRun()
{
if (retries == -1 &&
(System.currentTimeMillis() - scheduledExecutionTime() >= job.getMaxTardiness()) )
return false;
else
return true;
}
public void runJob()
{
job.run();
}
public void cancelJob()
{
job.jobCancelled();
}
}
package utils.otm;
import itc.ItcMessage;
import itc.ItcMessageQueue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Timer;
import java.util.concurrent.atomic.AtomicInteger;
import utils.common.Log4jHandler;
import utils.mtqm.MultiThreadQueueManager;
/**
* need to change to ScheduledThreadPoolExecutor and concurrent hash map
*
* @author amir
*
*/
public class OfflineTaskManager extends MultiThreadQueueManager
{
private final static int START_TASK = 1;
private final static int CANCEL_TASK = 2;
private final static int EXECUTE_JOB = 3;
private final static String MY_NAME = MultiThreadQueueManager.class.getName();
private final static Log4jHandler logger = new Log4jHandler(OfflineTaskManager.class.getName());
private static OfflineTaskManager sInstance = null;
AtomicInteger currentThread = new AtomicInteger(0);
public class OfflineTaskManagerJobThread extends MultiThreadQueueManager.JobThread
{
Timer jobTimer = null;
Map<String,OfflineTask> taskMap = null;
public OfflineTaskManagerJobThread(ItcMessageQueue queue,
int threadNumber)
{
super(queue, threadNumber);
jobTimer = new Timer(OfflineTaskManagerJobThread.class.getName() + String.valueOf(this.getThreadNumber()));
this.setName("OfflineTaskManagerJobThread" + String.valueOf(this.getThreadNumber()));
taskMap = new HashMap<String, OfflineTask>();
}
@Override
public void stopRunning()
{
jobTimer.cancel();
super.stopRunning();
}
@Override
protected void handleReceiveMsg(ItcMessage msg)
{
/*
* handle op-code
*/
switch (msg.getOpCode())
{
case START_TASK:
handleStartTask(msg);
break;
case CANCEL_TASK:
handleCancelTask(msg,true);
break;
case EXECUTE_JOB:
handleExecuteJob(msg);
break;
default:
break;
}
}
private void handleStartTask(ItcMessage msg)
{
/*
* validate
* add Task To Hash
*/
String key = (String)msg.getExtraMsgObj();
OfflineTask ot = (OfflineTask)msg.getMsgObj();
if (ot.isValid())
{
logger.info("start : " + ot.job.getClass().getName() + ", sent from: " + msg.getFromSender() + " on thread: " + this.getThreadNumber());
System.out.println("start : " + key + ", sent from: " + msg.getFromSender() + " on thread: " + this.getThreadNumber());
this.taskMap.put(key, ot);
/*
* start timer
*/
if (ot.retries != 0)
this.jobTimer.schedule(ot, ot.delay,ot.periodicTO);
else
this.jobTimer.schedule(ot, ot.delay);
}
else
logger.severe("Got invalid task");
}
private void handleExecuteJob(ItcMessage msg)
{
/*
* get the task from hash
*/
String key = (String)msg.getExtraMsgObj();
if (key != null)
{
OfflineTask ot = this.taskMap.get(key);
if (ot != null)
{
//System.out.println("execute : " + key + ", before enoughTimeToRun, sent from: " + msg.getFromSender() + " on thread: " + this.getThreadNumber());
if (ot.enoughTimeToRun())
{
logger.info("execute : " + ot.job.getClass().getName() + ", sent from: " + msg.getFromSender() + " on thread: " + this.getThreadNumber());
//System.out.println("execute : " + key + ", sent from: " + msg.getFromSender() + " on thread: " + this.getThreadNumber());
ot.runJob();
if (!ot.resolveContinue())
{
/*
* cancel task
*/
handleCancelTask(msg,false);
}
}
else
logger.info("not enough time to execute : " + ot.job.getClass().getName() + " on thread: " + this.getThreadNumber());
}
}
}
/**
*
* @param msg
* @param forced - from request by client
*/
private void handleCancelTask(ItcMessage msg, boolean forced)
{
String key = (String)msg.getExtraMsgObj();
if (key != null)
{
logger.info("removing task for: " + key + " from hash" + ", sent from: " + msg.getFromSender() + " on thread: " + this.getThreadNumber());
//System.out.println("removing task for: " + key + " from hash" + ", sent from: " + msg.getFromSender() + " on thread: " + this.getThreadNumber());
OfflineTask ot = this.taskMap.remove(key);
if (ot != null)
{
ot.cancel();
if (forced)
ot.cancelJob();
}
}
}
}
public synchronized static void createInstance()
{
sInstance = new OfflineTaskManager();
}
public static OfflineTaskManager getInstance() {
if (sInstance == null)
createInstance();
return sInstance;
}
@Override
public boolean startManager(int numOfThreads, int defaultQueueSize)
{
mNumOfThreads = numOfThreads;
mJobQueueArray = new ArrayList<ItcMessageQueue>();
mJobThreadArray = new ArrayList<JobThread>();
for (int i = 0; i < mNumOfThreads; i++)
{
ItcMessageQueue queue = new ItcMessageQueue(defaultQueueSize);
mJobQueueArray.add(queue);
JobThread thr = new OfflineTaskManagerJobThread(queue,i);
mJobThreadArray.add(thr);
thr.start();
}
return true;
}
/**
* push an offline task
* @param key NOTE: key must be unique among different tasks per user e.g. key = user@task_name
* @param job - the job task
* @param delay - start delay
* @param periodicTO - between jobs
* @param retries number of retries, or continous if -1
*/
public boolean pushTask(String key,IOfflineTaskJob job,long delay,long periodicTO,int retries)
{
/*
* validate params
*/
if (validateParams(key,job,delay,periodicTO,retries))
{
/*
* getting the queueNumber
*/
int queueNum = currentThread.getAndIncrement() % this.mNumOfThreads; //this.getQueueNumber(key);
/*
* allocate offline task
*/
OfflineTask ot = new OfflineTask(key, queueNum, job, delay, periodicTO, retries);
/*
* send task (start)
*/
return this.sendJob(START_TASK, ot, key, queueNum,MY_NAME);
}
else
logger.warning("invalid params");
return false;
}
private boolean validateParams(String key, IOfflineTaskJob job, long delay, long periodicTO, int retries)
{
boolean bValid = false;
if (key != null && job != null)
{
switch (retries)
{
case 0:
// one time
bValid = true;
break;
case -1:
default:
// forever (GTC)
if (periodicTO > 0)
bValid = true;
break;
}
}
return bValid;
}
/**
* cancel the task
* @param key
*/
public void cancelTask(String key)
{
/*
* getting the queueNumber
*/
int queueNum = this.getQueueNumber(key);
mJobQueueArray.get(queueNum).sendMsg(CANCEL_TASK, null, key, MY_NAME);
}
public void executeTask(String key, int queueNumber)
{
mJobQueueArray.get(queueNumber).sendMsg(EXECUTE_JOB, null, key, MY_NAME);
}
public final Map<String,String> getCurrentRunningTasks()
{
Map<String,String> tasksMap = new HashMap<String, String>();
for (JobThread jt : mJobThreadArray)
{
if (jt != null)
{
OfflineTaskManagerJobThread otjt = (OfflineTaskManagerJobThread)jt;
Set<Entry<String, OfflineTask>> set = otjt.taskMap.entrySet();
if (set != null && !set.isEmpty())
{
for (Entry<String, OfflineTask> entry : set)
{
if (entry != null)
{
tasksMap.put("Thread: " + otjt.getName(),"Key: " + entry.getKey() + ", Task: " + entry.getValue().getClass().getName() +
", scheduledExecutionTime: " + String.valueOf(entry.getValue().scheduledExecutionTime()));
}
}
}
}
}
return tasksMap;
}
}
package utils.timerManager;
public interface ITimerEventListener
{
public void timerElapsed(String key, String tag,Object appData);
public void timerStopped(String key, String tag);
}
package utils.timerManager;
import java.util.Map;
import java.util.UUID;
import utils.common.Log4jHandler;
import utils.otm.IOfflineTaskJob;
import utils.otm.OfflineTaskManager;
/**
*
* @author amir
*
*/
public class TimerManager
{
private static final String NO_TAG = "no-tag";
private class TimerManagerTask extends IOfflineTaskJob
{
String key;
String tag;
Object appData;
public TimerManagerTask(String key, String tag, Object appData)
{
super();
this.key = key;
this.tag = tag;
this.appData = appData;
}
@Override
public void run()
{
logger.info("TimerManagerTask.run: " + key + ",tag: " + tag);
if (listener != null)
listener.timerElapsed(key, tag, appData);
}
@Override
public boolean continueNextTime()
{
return false;
}
public void jobCancelled()
{
logger.info("TimerManagerTask.jobCancelled: " + key + ",tag: " + tag);
if (listener != null)
listener.timerStopped(key, tag);
}
}
private class TimerManagerPeriodicTask extends TimerManagerTask
{
public TimerManagerPeriodicTask(String key, String tag, Object appData)
{
super(key,tag,appData);
}
@Override
public boolean continueNextTime()
{
return true;
}
}
private final static Log4jHandler logger = new Log4jHandler(TimerManager.class.getName());
ITimerEventListener listener = null;
OfflineTaskManager offlineTaskManager = null;
public boolean init(ITimerEventListener listener, int threadPoolSize, int timerQueueSize)
{
this.listener = listener;
offlineTaskManager = OfflineTaskManager.getInstance();// new OfflineTaskManager();
boolean result = offlineTaskManager.startManager(threadPoolSize,timerQueueSize);
return result;
}
/**
* start timer
* @param toMilliSeconds - timeout in milliseconds
* @param key - key must be unique
* @param tag - tagging the timer
* @param appData - application metadata, optional
* @return the timer key on success, null on fail
*/
public String startTimer(int toMilliSeconds, String key, String tag, Object appData)
{
tag = (tag != null) ? tag :NO_TAG;
key = (key != null) ? key : UUID.randomUUID().toString();
logger.info("starting timer for: " + key + ", tag: " + tag + ", duration: " + String.valueOf(toMilliSeconds));
TimerManagerTask timerManagerTask = new TimerManagerTask(key,tag,appData);
if(offlineTaskManager.pushTask(key, timerManagerTask, toMilliSeconds, 0, 0))
return key;
return null;
}
/**
* starting a periodic timer - loop until cancelled
* @param toMilliSeconds
* @param key
* @param tag
* @return the timer key on success, null on fail
*/
public String startPeriodicTimer(int toMilliSeconds, String key, String tag, Object appData)
{
tag = (tag != null) ? tag :NO_TAG;
key = (key != null) ? key : UUID.randomUUID().toString();
logger.info("starting timer for: " + key + ", tag: " + tag + ", duration: " + String.valueOf(toMilliSeconds));
TimerManagerTask timerManagerTask = new TimerManagerPeriodicTask(key,tag,appData);
if (offlineTaskManager.pushTask(key, timerManagerTask,0, toMilliSeconds, -1))
return key;
return null;
}
/**
* stopping the selected timer
* @param key
*/
public void stopTimer(String key)
{
logger.info("cancel timer for: " + key);
offlineTaskManager.cancelTask(key);
}
public final Map<String,String> getRunningTimerTasks()
{
return offlineTaskManager.getCurrentRunningTasks();
}
}
import org.junit.Test;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Created by amir on 19/05/16.
*/
public class TestSchedulars {
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("beep");
}
};
final ScheduledFuture<?> beeperHandle1 =
scheduler.scheduleAtFixedRate(beeper, 10, 15, SECONDS);
final ScheduledFuture<?> beeperHandle2 =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle1.cancel(true); }
}, 60 * 60, SECONDS);
}
}
@Test
public void testBeeper() throws InterruptedException {
BeeperControl bc = new BeeperControl();
bc.beepForAnHour();
Thread.sleep(60000);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment