-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ commit yaml/properties file utils && net related utils && string re…
…gex ops
- Loading branch information
1 parent
557e5a7
commit 8c72822
Showing
14 changed files
with
569 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/tech/sqlclub/common/context/PropertiesContext.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tech.sqlclub.common.context | ||
|
||
import java.io.{File, FileInputStream, InputStream} | ||
import java.util.Properties | ||
|
||
import tech.sqlclub.common.utils.{FileUtils, ParamMapUtils} | ||
|
||
object PropertiesContext { | ||
val resourcePath = this.getClass.getClassLoader.getResource("").getPath | ||
lazy val properties = new Properties() | ||
|
||
import scala.collection.JavaConversions._ | ||
private lazy val paramMap = { | ||
getAllPropertyFile.map(loadPropertyFile _ ) | ||
properties.toMap[String, AnyRef] | ||
} | ||
|
||
def getAllPropertyFile = FileUtils.lsfile(resourcePath,".*\\.properties") | ||
|
||
def loadPropertyFile(file:File) | ||
(implicit inputStream:InputStream=new FileInputStream(file)) = { | ||
try { | ||
properties.load(inputStream) | ||
} finally { | ||
inputStream.close() | ||
} | ||
} | ||
|
||
|
||
implicit class PropertiesContext_impl( _this_ : PropertiesContext.type ) extends ParamMapUtils(_this_.paramMap) | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/tech/sqlclub/common/context/YamlContext.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tech.sqlclub.common.context | ||
|
||
import java.io.{File, FileInputStream, InputStream} | ||
import java.util | ||
|
||
import org.yaml.snakeyaml.Yaml | ||
import tech.sqlclub.common.utils.{FileUtils, ParamMapUtils} | ||
|
||
|
||
object YamlContext { | ||
val resourcePath = this.getClass.getClassLoader.getResource("").getPath | ||
lazy val yaml = new Yaml() | ||
|
||
import scala.collection.JavaConversions._ | ||
private lazy val paramMap = getAllYamlFile.flatMap(loadYamlFile[util.Map[String,Object]](_)).toMap | ||
|
||
def getAllYamlFile = FileUtils.lsfile(resourcePath,".*\\.(yaml|yml)") | ||
|
||
def loadYamlFile[A](file:File) | ||
(implicit inputStream:InputStream=new FileInputStream(file)):A = { | ||
try { | ||
yaml.load[A](inputStream) | ||
} finally { | ||
inputStream.close() | ||
} | ||
} | ||
|
||
implicit class YamlContext_impl( _this_ : YamlContext.type ) extends ParamMapUtils(_this_.paramMap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package tech.sqlclub.common.net | ||
|
||
import java.io.IOException | ||
import java.net._ | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
object NetUtils { | ||
|
||
def getLocalServerIp: String = try { | ||
java.net.InetAddress.getLocalHost.getHostAddress | ||
} catch { | ||
case e: UnknownHostException => | ||
e.printStackTrace() | ||
null | ||
} | ||
|
||
|
||
def getLocalServerIpv4List: List[String] = { | ||
try { | ||
import scala.collection.JavaConversions._ | ||
NetworkInterface.getNetworkInterfaces.toList.filter(_.isUp) | ||
.flatMap(_.getInetAddresses) | ||
.filter { | ||
iaddr => | ||
iaddr.isInstanceOf[Inet4Address] && !iaddr.isLoopbackAddress | ||
}.map(_.getHostAddress) | ||
} catch { | ||
case e: Exception => | ||
e.printStackTrace() | ||
List.empty[String] | ||
} | ||
} | ||
|
||
def portAvailableAndReturn(hostname: String, port_min_number: Int, port_max_number: Int):Int = { | ||
var bindSuccess: Boolean = false | ||
var ss: Socket = null | ||
var ss1: Socket = null | ||
val start: AtomicInteger = new AtomicInteger(port_min_number) | ||
|
||
while (!bindSuccess && start.get() <= port_max_number) { | ||
try { | ||
ss = new Socket() | ||
ss.bind(new InetSocketAddress(hostname, start.get())) | ||
ss.close() | ||
if (hostname != "0.0.0.0") { | ||
ss1 = new Socket() | ||
ss1.bind(new InetSocketAddress("0.0.0.0", start.get())) | ||
ss1.close() | ||
} | ||
bindSuccess = true | ||
} catch { | ||
case e:IOException => | ||
bindSuccess = false | ||
start.set(start.get() + 1) | ||
} finally { | ||
socketClose(ss) | ||
socketClose(ss1) | ||
} | ||
} | ||
if (bindSuccess) start.get() else -1 | ||
} | ||
|
||
def socketClose(s:Socket)= { | ||
if (s != null && !s.isClosed){ | ||
try { | ||
s.close() | ||
} catch { | ||
case e:IOException => | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package tech.sqlclub.common.regex | ||
|
||
|
||
object RegexOp { | ||
implicit class RegexString(s:String) { | ||
/** | ||
* 判断字符串是否匹配正则 | ||
* @param pattern 正则表达式 | ||
* @return Boolean | ||
*/ | ||
def matching(pattern:String):Boolean = (pattern.r findFirstIn s).isDefined | ||
|
||
|
||
/** | ||
* 正则匹配按组获取值 | ||
* @param pattern 正则表达式 | ||
* @param group 第几组 | ||
* @return Iterator[String] | ||
*/ | ||
def matchGroup(pattern:String, group:Int):Iterator[String] = { | ||
val matchs = pattern.r findAllMatchIn s | ||
matchs.map(_.group(group)) | ||
} | ||
|
||
/** | ||
* 正则匹配所有值 | ||
* @param pattern 正则表达式 | ||
* @return List[String] | ||
*/ | ||
def matchAll(pattern:String):List[String] = (pattern.r findAllIn s).toList | ||
|
||
/** | ||
* 正则匹配第一个值 | ||
* @param pattern 正则表达式 | ||
* @return Option[String] | ||
*/ | ||
def matchFirst(pattern:String):Option[String] = pattern.r findFirstIn s | ||
|
||
|
||
/** | ||
* 判断字符串是否是数值格式 | ||
* @return Boolean | ||
*/ | ||
def isMumeric:Boolean = s matching "^(\\+|\\-)?(0+\\.[\\d]+|[1-9]+[\\d]*\\.[\\d]+|[\\d]+)$" | ||
|
||
|
||
/** | ||
* 提取字符串中所有的数值 | ||
* @return List[String] | ||
*/ | ||
def extractMumeric:List[String] = s matchAll "(\\+|\\-)?(0+\\.[\\d]+|[1-9]+[\\d]*\\.[\\d]+|[\\d]+)" | ||
|
||
/** | ||
* 判断字符串是否是手机号格式 | ||
* @param len 字符串长度 | ||
* @return Boolean | ||
*/ | ||
def isMobileNumber(implicit len:Int = s.length):Boolean = { | ||
val mobileNumberRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))[0-9]{8}$" | ||
// 截取后11位 | ||
val number = s.slice(len-11, len) | ||
number matching mobileNumberRegex | ||
} | ||
|
||
/** | ||
* 提取字符串所有手机号 | ||
* @return List[String] | ||
*/ | ||
def extractMobileNumber:List[String] = s matchAll "((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))[0-9]{8}" | ||
|
||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package tech.sqlclub.common.utils | ||
|
||
import java.io.{File, FileInputStream, InputStream} | ||
import java.util.Properties | ||
|
||
import org.apache.commons.lang3.StringUtils | ||
import org.yaml.snakeyaml.Yaml | ||
import tech.sqlclub.common.regex.RegexOp._ | ||
import scala.collection.JavaConversions._ | ||
|
||
/** | ||
* | ||
* Created by songgr on 2020/01/03. | ||
*/ | ||
object FileUtils { | ||
|
||
/** | ||
* 获取目录文件列表 | ||
* @param path 目录 | ||
* @param pattern 匹配正则 | ||
* @param recursive 是否递归子目录 | ||
* @return Array[File] | ||
*/ | ||
def lsfile(path:String, pattern:String = ".*", recursive:Boolean=false):Array[File] = { | ||
|
||
val file = new File(path) | ||
|
||
if (file.isDirectory){ | ||
|
||
val files = file.listFiles().filter(_.isFile).filter(_.getName matching pattern ) | ||
|
||
if (recursive) { | ||
val fs = file.listFiles().filter(_.isDirectory).flatMap{ | ||
f => | ||
lsfile(f.getAbsolutePath, pattern, recursive) | ||
} | ||
return files ++ fs | ||
} | ||
return files | ||
} | ||
Array.empty[File] | ||
} | ||
|
||
|
||
/** | ||
* 创建目录 | ||
* @param path 目录 | ||
* @return Boolean | ||
*/ | ||
def mkdirs(path:String): Boolean = { | ||
if (StringUtils.isNotBlank(path)) { | ||
val file = new File(path) | ||
if (!file.isDirectory){ | ||
if (!file.exists) file.mkdirs() | ||
} | ||
} | ||
false | ||
} | ||
|
||
def readYamlFile(path:String):Map[String,Object] = readYamlFile(new File(path)) | ||
|
||
def readYamlFile(file:File) | ||
(implicit inputStream:InputStream=new FileInputStream(file)):Map[String,Object] = { | ||
try { | ||
val yaml = new Yaml() | ||
yaml.load[java.util.Map[String,Object]](inputStream).toMap | ||
} finally { | ||
inputStream.close() | ||
} | ||
} | ||
|
||
|
||
def readPropertiesFile(path:String):Map[String,Object] = readPropertiesFile(new File(path)) | ||
|
||
def readPropertiesFile(file:File) | ||
(implicit inputStream:InputStream=new FileInputStream(file)):Map[String,Object] = { | ||
try { | ||
val properties = new Properties() | ||
properties.load(inputStream) | ||
properties.toMap[String,Object] | ||
} finally { | ||
inputStream.close() | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.