mirror of
https://github.com/Dansen999/migor.git
synced 2026-01-11 05:24:16 +00:00
Initial import (partially)
This commit is contained in:
15
org.migor.shared/pom.xml
Normal file
15
org.migor.shared/pom.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>org.migor.server</artifactId>
|
||||
<groupId>org.migor.server</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>org.migor.shared</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
</project>
|
||||
136
org.migor.shared/src/main/java/org/migor/shared/McctxId.java
Normal file
136
org.migor.shared/src/main/java/org/migor/shared/McctxId.java
Normal file
@@ -0,0 +1,136 @@
|
||||
package org.migor.shared;
|
||||
|
||||
import com.eaio.uuid.UUID;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Daniel Scheidle
|
||||
* @since 11/4/13 10:24 PM
|
||||
*/
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public class McctxId {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(McctxId.class);
|
||||
|
||||
private static final String ENV_KEY_BIND_ADDRESS = "jboss.bind.address";
|
||||
|
||||
private static final long MAX_HEADER_LEN = 2048;
|
||||
|
||||
private McctxId() {
|
||||
// util class, no constructor needed
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets bind address.
|
||||
*
|
||||
* @return the bind address
|
||||
*/
|
||||
@NotNull
|
||||
public static String getBindAddress() {
|
||||
String ip = System.getProperty(ENV_KEY_BIND_ADDRESS);
|
||||
try {
|
||||
return StringUtils.isBlank(ip) ? InetAddress.getLocalHost().getHostName() : ip;
|
||||
} catch (Exception e) {
|
||||
return "localhost";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add UUID to thread name.
|
||||
*
|
||||
* @param u the u
|
||||
*/
|
||||
public static void addUUIDToThreadName(@NotNull final String u) {
|
||||
Thread thread = Thread.currentThread();
|
||||
|
||||
if (StringUtils.indexOf(thread.getName(), "|") != -1) {
|
||||
removeUUIDFromThreadName();
|
||||
}
|
||||
|
||||
thread.setName(thread.getName() +"|"+ u);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove UUID from thread name.
|
||||
*/
|
||||
public static void removeUUIDFromThreadName() {
|
||||
final Thread temp = Thread.currentThread();
|
||||
final String currentName = temp.getName();
|
||||
temp.setName(currentName.substring(0, currentName.length()-37));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create uUID.
|
||||
*
|
||||
* @return the uUID
|
||||
*/
|
||||
@NotNull
|
||||
public static String create() {
|
||||
return new UUID().toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create UUID.
|
||||
*
|
||||
* @param currentUUID the current uUID
|
||||
* @return the uUID
|
||||
*/
|
||||
@NotNull
|
||||
public static UUID create(@Nullable final String currentUUID) {
|
||||
if (!StringUtils.isBlank(currentUUID)) {
|
||||
return new UUID(currentUUID);
|
||||
} else {
|
||||
return new UUID();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create path.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
@NotNull
|
||||
public static String createPath() {
|
||||
return addPath(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add path.
|
||||
*
|
||||
* @param currentPath the current path
|
||||
* @return the string
|
||||
*/
|
||||
@NotNull
|
||||
public static String addPath(@Nullable final String currentPath) {
|
||||
|
||||
String path = "";
|
||||
if (!StringUtils.isBlank(currentPath)) {
|
||||
path += StringUtils.trim(currentPath) + ";";
|
||||
}
|
||||
|
||||
path += getBindAddress() + "," + new Date().getTime();
|
||||
try {
|
||||
while (path.length() > MAX_HEADER_LEN) {
|
||||
path = StringUtils.substring(path, path.indexOf(";") + 1, path.length()-1);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Host removed - new path: " + path);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("Given path could not be shortened due to incorrect formatting");
|
||||
path = path + "," + new Date().getTime();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.migor.shared;
|
||||
|
||||
/**
|
||||
* @author Daniel Scheidle
|
||||
* @since 11/4/13 10:24 PM
|
||||
*/
|
||||
public enum StatusCode {
|
||||
|
||||
OK(0),
|
||||
ERROR(1),
|
||||
PERMISSION_DENIED(2);
|
||||
|
||||
private int code;
|
||||
|
||||
private StatusCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package org.migor.shared.parser;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Daniel Scheidle
|
||||
* @since 11/4/13 10:46 PM
|
||||
*/
|
||||
public class GenericParser {
|
||||
|
||||
public static final String DEFAULT_DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final String DEFAULT_ARRAY_DELIMITER = ",";
|
||||
|
||||
|
||||
private static String dateFormatPattern = GenericParser.DEFAULT_DATE_FORMAT_PATTERN;
|
||||
private static String arrayDelimiter = DEFAULT_ARRAY_DELIMITER;
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T parse(Class<T> clazz, String value) throws ParseException {
|
||||
try {
|
||||
if (StringUtils.isEmpty(value)) {
|
||||
return null;
|
||||
} else if (clazz.isPrimitive()) {
|
||||
if (clazz.toString().equals("float")) {
|
||||
return (T)Float.valueOf(value);
|
||||
} else if (clazz.toString().equals("double")) {
|
||||
return (T)Double.valueOf(value);
|
||||
} else if (clazz.toString().equals("long")) {
|
||||
return (T)Long.valueOf(value);
|
||||
} else if (clazz.toString().equals("int")) {
|
||||
return (T)Integer.valueOf(value);
|
||||
} else if (clazz.toString().equals("boolean")) {
|
||||
return (T)Boolean.valueOf(value);
|
||||
}
|
||||
} else if (clazz.equals(Boolean.class)) {
|
||||
return (T)Boolean.valueOf(value);
|
||||
} else if (clazz.equals(Long.class)) {
|
||||
return (T)Long.valueOf(value);
|
||||
} else if (clazz.equals(Integer.class)) {
|
||||
return (T)Integer.valueOf(value);
|
||||
} else if (clazz.equals(Double.class)) {
|
||||
return (T)Double.valueOf(value);
|
||||
} else if (clazz.equals( Float.class )) {
|
||||
return (T)Float.valueOf(value);
|
||||
} else if (clazz.equals( Date.class )) {
|
||||
try {
|
||||
return (T)(new SimpleDateFormat(getDateFormatPattern()).parse(value));
|
||||
} catch (Exception t) {
|
||||
throw new RuntimeException("Failed to parse date: " + t.getMessage());
|
||||
}
|
||||
} else if (clazz.isEnum()) {
|
||||
Object[] defined_values = clazz.getEnumConstants();
|
||||
for (Object t : defined_values) {
|
||||
if (t.toString().equalsIgnoreCase(value)) {
|
||||
return (T)t;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Unable to convert: Defined value "+value+" is not an option of "+clazz.toString());
|
||||
} else if (clazz.isAssignableFrom(Parsable.class)) {
|
||||
return (T) ((Parsable) clazz.newInstance()).parse(value);
|
||||
} else if (clazz.isAssignableFrom(String.class)) {
|
||||
return (T)value;
|
||||
}
|
||||
|
||||
throw new ParseException("Type can not by cast by this method: "+clazz.toString());
|
||||
} catch (ParseException e) {
|
||||
throw e;
|
||||
} catch (Exception t) {
|
||||
throw new ParseException(t.getMessage(), t);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings( "unchecked" )
|
||||
public static <T> T[] parseArray(Class<T> clazz, String value) throws ParseException {
|
||||
try {
|
||||
String[] values = value.split(getArrayDelimiter());
|
||||
|
||||
T[] array = (T[]) Array.newInstance(clazz, values.length);
|
||||
|
||||
for (int pos = 0; pos < values.length; pos++) {
|
||||
array[pos] = parse(clazz, values[pos]);
|
||||
}
|
||||
return array;
|
||||
} catch (Exception t) {
|
||||
throw new ParseException(t.getMessage(), t);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDateFormatPattern() {
|
||||
return dateFormatPattern;
|
||||
}
|
||||
|
||||
public static void setDateFormatPattern( final String newDateFormatPattern ) {
|
||||
dateFormatPattern = newDateFormatPattern;
|
||||
}
|
||||
|
||||
public static String getArrayDelimiter() {
|
||||
return arrayDelimiter;
|
||||
}
|
||||
|
||||
public static void setArrayDelimiter( final String newArrayDelimiter ) {
|
||||
arrayDelimiter = newArrayDelimiter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.migor.shared.parser;
|
||||
|
||||
/**
|
||||
* @author Daniel Scheidle
|
||||
* @since 11/4/13 10:50 PM
|
||||
*/
|
||||
public interface Parsable<T> {
|
||||
|
||||
public T parse(final String string) throws ParseException;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.migor.shared.parser;
|
||||
|
||||
/**
|
||||
* @author Daniel Scheidle
|
||||
* @since 11/4/13 10:20 PM
|
||||
*/
|
||||
public class ParseException extends Exception {
|
||||
|
||||
public ParseException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ParseException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user