Permalink
Browse files

refactoring eh

  • Loading branch information...
1 parent 9b0ed0e commit 78fe4a746deb21400033902ea29c04d8ddef62b0 @leonlee committed Dec 20, 2012
@@ -1 +1 @@
-modulename=org.riderzen.ogs.mod.sys.auth
+modulename=org.riderzen.ogs.mod.sys-auth
@@ -7,6 +7,7 @@ import net.sf.oval.constraint.MaxLength
import net.sf.oval.constraint.MinLength
import net.sf.oval.constraint.NotNull
import org.riderzen.ogs.common.BaseModel
+import org.riderzen.ogs.common.EventHelper
import org.riderzen.ogs.common.Tools
/**
@@ -17,7 +18,8 @@ class User extends BaseModel {
@MinLength(6)
@MaxLength(64)
String username
- @NotNull
+ @MinLength(6)
+ @MaxLength(32)
String password
@NotNull
UserType type
@@ -33,21 +35,24 @@ class User extends BaseModel {
Long lastNodeId
Long lastLoginOn
- static User register(String username, String password, String email = null,
- String mobile = null, String deviceToken = null, String clientVersion = null) {
+ static User register(EventHelper eh) {
+ def param = eh.param
+
User user = new User()
- user.username = username
- user.password = Tools.encrypt(password)
- user.email = email
- user.mobile = mobile
- user.deviceToken = deviceToken
- user.clientVersion = clientVersion
-
- if (email) {
+ user.username = param.username
+ user.password = param.password
+ user.email = param.email
+ user.mobile = param.mobile
+ user.deviceToken = param.deviceToken
+ user.clientVersion = param.clientVersion
+
+ if (param.email) {
user.emailBindOn = Tools.currentTime()
}
- User.save()
+ user.save(eh) {
+ user.password = Tools.encrypt(user.password)
+ }
}
}
@@ -0,0 +1,22 @@
+import org.riderzen.ogs.auth.User
+import org.riderzen.ogs.common.Address
+import org.riderzen.ogs.common.EventHelper
+import org.vertx.groovy.core.eventbus.Message
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-20
+ */
+
+def config = container.config
+def eb = vertx.eventBus
+def logger = container.logger
+
+logger.info "starting sys-auth"
+
+eb.registerHandler(config?.address ?: Address.sysAuthRegister.val) { Message message ->
+ logger.debug("received message ${message.body}")
+ def eh = new EventHelper(container: container, vertx: vertx, message: message)
+ User user = User.register(eh)
+}
+
@@ -0,0 +1,8 @@
+log4j.rootCategory=ERROR, stdout
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n
+
+log4j.category.org.vertx=ERROR
+log4j.category.org.riderzen=DEBUG
@@ -0,0 +1,3 @@
+{
+ "main": "sys-auth.groovy"
+}
@@ -1,5 +1,6 @@
dependencies {
compile "net.sf.oval:oval:$ovalVsn"
compile "commons-codec:commons-codec:$commonsCodecVsn"
+ compile "org.apache.commons:commons-lang3:$commonsLang3Vsn"
compile "date4j:date4j:$date4jVsn"
}
@@ -2,4 +2,5 @@ modulename=org.riderzen.ogs.platform.common
ovalVsn=1.81
commonsCodecVsn=1.7
+commonsLang3Vsn=3.1
date4jVsn=1.2.0
@@ -4,11 +4,14 @@ package org.riderzen.ogs.common
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-16
*/
-enum E {
- appProtocol("org.riderzen.ogs.protocol")
+enum Address {
+ appProtocol("org.riderzen.ogs.protocol"),
+ sysAuthRegister("org.riderzen.ogs.mod.sys-auth.register"),
+ platJdbc("org.riderzen.ogs.plat.jdbc")
+
public String val;
- E(String val) {
+ Address(String val) {
this.val = val
}
@@ -1,14 +1,17 @@
package org.riderzen.ogs.common
-import hirondelle.date4j.DateTime
import net.sf.oval.Validator
import net.sf.oval.constraint.NotNull
+import java.util.concurrent.atomic.AtomicInteger
+
/**
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-7
*/
class BaseModel {
+ AtomicInteger eventCounter = new AtomicInteger(0)
+
@NotNull
Long id
@NotNull
@@ -37,7 +40,7 @@ class BaseModel {
propMap
}
- def save() {
+ def save(EventHelper eh, afterValidate = null, afterSave = null) {
if (!id){
id = DBHelper.nextID()
createdOn = Tools.currentTime()
@@ -46,7 +49,11 @@ class BaseModel {
}
if (validate()) {
-
+ afterValidate?.call()
+ eh.newProcess(1)
+ eh.eb.send(Address.platJdbc.val, this) { message ->
+ eh.sendOK()
+ }
}
}
@@ -0,0 +1,84 @@
+package org.riderzen.ogs.common
+
+import org.vertx.groovy.core.Vertx
+import org.vertx.groovy.core.eventbus.EventBus
+import org.vertx.groovy.core.eventbus.Message
+import org.vertx.groovy.deploy.Container
+import org.vertx.java.core.eventbus.Message
+import org.vertx.java.core.json.JsonObject
+import org.vertx.java.core.logging.Logger
+
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.atomic.AtomicInteger
+import java.util.concurrent.atomic.AtomicLong
+
+/**
+ * Event Helper
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-20
+ */
+class EventHelper {
+ private AtomicInteger counter
+
+ Container container
+ Vertx vertx
+ Message message
+
+ EventHelper(Container container, Vertx vertx, Message message) {
+ this.container = container
+ this.vertx = vertx
+ this.message = message
+ }
+
+ def newProcess(int max) {
+ counter = new AtomicInteger(max)
+ }
+
+ EventBus getEb() {
+ vertx?.eventBus
+ }
+
+ Logger getLogger() {
+ container?.logger
+ }
+
+ def getConfig() {
+ container?.config
+ }
+
+ def getParam() {
+ message?.body
+ }
+
+ void sendOK(Message<JsonObject> message) {
+ sendOK(message, null)
+ }
+
+ void sendStatus(String status, Message<JsonObject> message) {
+ sendStatus(status, message, null)
+ }
+
+ void sendStatus(String status, Message<JsonObject> message, JsonObject json) {
+ if (counter.decrementAndGet()) return
+
+ if (json == null) {
+ json = new JsonObject()
+ }
+ json.putString("status", status)
+ message.reply(json)
+ }
+
+ void sendOK(Message<JsonObject> message, JsonObject json) {
+ sendStatus("ok", message, json)
+ }
+
+ void sendError(Message<JsonObject> message, String error) {
+ sendError(message, error, null)
+ }
+
+ void sendError(Message<JsonObject> message, String error, Exception e) {
+ logger.error(error, e)
+ JsonObject json = new JsonObject().putString("status", "error").putString("message", error)
+ message.reply(json)
+ }
+}
@@ -0,0 +1,10 @@
+package org.riderzen.ogs.common
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-20
+ */
+class MK {
+ static final String ExInvalidModel = "ex.invalidModel"
+ static final String ExSystem = "ex.system"
+}
@@ -2,21 +2,70 @@ package org.riderzen.ogs.common
import hirondelle.date4j.DateTime
import org.apache.commons.codec.digest.DigestUtils
-
-import java.security.MessageDigest
+import org.apache.commons.lang3.LocaleUtils
/**
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-19
*/
class Tools {
- static salt = "sJKVn3IGFwjdty37"
+ private static ResourceBundle bundle
+
+ static final String MESSAGES = "messages"
+ static logger = EventHelper.container.logger
+ static salt = "sJK" + "Vn3I" + "GFwj" + "dty37"
static encrypt(message) {
- DigestUtils.md5Hex(message + salt)
+ if(message) {
+ DigestUtils.md5Hex(message + salt())
+ }
+ }
+
+ static String salt() {
+ DigestUtils.md2Hex(salt)
}
static currentTime() {
DateTime.now(TimeZone.getDefault()).getMilliseconds(TimeZone.getDefault())
}
+
+ static ok(data) {
+ [status: 0, data: data]
+ }
+
+ static error(String key, String...params) {
+ [status: 1, message: t(key, params)]
+ }
+
+ static t(String key, String... params) {
+ String message = bundle().getString(key)
+ if (params) {
+ params.each {
+ message = message.replaceFirst('\\{\\}', it)
+ }
+ }
+ message
+ }
+
+ synchronized static bundle() {
+ if (!bundle) {
+ String lang = System.getProperty("lang")
+ Locale locale;
+ if (lang) {
+ try{
+ locale = LocaleUtils.toLocale(lang)
+ } catch (e) {
+ logger.error("Property lang is invalid: $lang", e)
+ }
+ } else {
+ locale = Locale.default
+ }
+ try {
+ bundle = ResourceBundle.getBundle(MESSAGES, locale)
+ } catch (e) {
+ logger.error("Can't get bundle 'messages' with locale ${locale}", e)
+ bundle = ResourceBundle.getBundle(MESSAGES)
+ }
+ }
+ }
}
@@ -0,0 +1,2 @@
+ex.invalidModel=invalid model with errors: {}
+ex.system=System exception {}
@@ -8,7 +8,7 @@ import org.vertx.java.core.buffer.Buffer
import org.vertx.java.core.eventbus.Message
import org.vertx.java.core.parsetools.RecordParser
import com.google.gson.Gson
-import org.riderzen.ogs.common.E
+import org.riderzen.ogs.common.Address
/**
* User: Leon Lee <mail.lgq@gmail.com>
@@ -26,7 +26,7 @@ class ProtocolParser extends BusModBase {
}
def init() {
- address = getOptionalStringConfig("address", E.appProtocol.val)
+ address = getOptionalStringConfig("address", Address.appProtocol.val)
def msgHandler = { Buffer message ->
@@ -1,7 +1,7 @@
package org.riderzen.ogs.tcp
import org.vertx.java.busmods.BusModBase
-import org.riderzen.ogs.common.E
+import org.riderzen.ogs.common.Address
/**
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-7
@@ -22,7 +22,7 @@ class TcpServer extends BusModBase {
def onConnected = { sock ->
sock.dataHandler { buffer ->
logger.debug("received ${buffer.lenght} bytes of data")
- eb.send(E.appProtocol.val, buffer) { message ->
+ eb.send(Address.appProtocol.val, buffer) { message ->
sock << message
}
}

0 comments on commit 78fe4a7

Please sign in to comment.