Permalink
Browse files

...

  • Loading branch information...
1 parent 34eab8f commit 19ac466d5c2962fe96b73115491861d1ef795790 @leonlee committed Dec 27, 2012
View
@@ -3,7 +3,7 @@ def emptyProjects = ['modules', 'platform',]
configure(subprojects.findAll { project -> !emptyProjects.find {it == project.name} }) {
apply plugin: 'java'
apply plugin: 'groovy'
- apply plugin: 'idea'
+// apply plugin: 'idea'
group = 'org.riderzen.ogs'
@@ -79,7 +79,7 @@ configure(subprojects.findAll { project -> !emptyProjects.find {it == project.na
}
task installMod(type: Copy, dependsOn: 'copyMod') {
- into "${System.getenv('VERTX_HOME')?.replaceAll('\\\\', '/')?:rootProject.projectDir + '/build'}/mods"
+ into "${System.getenv('VERTX_HOME')?.replaceAll('\\\\', '/')?:rootProject.projectDir.absolutePath + '/build'}/mods"
from "build/mod"
}
@@ -1,24 +1,43 @@
package org.riderzen.ogs.auth
import org.riderzen.ogs.common.Address
-import DBHelper
+import org.riderzen.ogs.common.BaseProcessor
import org.riderzen.ogs.common.DataStoreOperation
-import org.riderzen.ogs.common.EventHelper
+import org.riderzen.ogs.common.EntityEvent
+import org.riderzen.ogs.common.EventType
+import org.riderzen.ogs.common.ProcessHelper
+import org.riderzen.ogs.common.Tools
+import org.vertx.groovy.core.eventbus.Message
/**
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-21
*/
-class RegisterProcessor{
- EventHelper eh
+class RegisterProcessor extends BaseProcessor{
+
+ RegisterProcessor(Long pid, ProcessHelper ph) {
+ super(pid, ph)
+ }
def process() {
- def user = User.populate(eh.param)
+ def user = User.populate(ph.param)
+ EntityEvent event = new EntityEvent(type: EventType.create, rid: ph.rid, pid: pid, id: Tools.nextId(EntityEvent.name))
+ event.entity = user.entityName
+ event.body = user.toJson()
if (user) {
- eh.newProcess(1)
- eh.eb.send(Address.dataStore.val, [operation: DataStoreOperation.create, params: user.toJson()]) { message ->
- eh.sendOK(user.toJson())
+ ph.newProcess(2)
+ ph.eb.send(Address.dataStore.val, [action: DataStoreOperation.beginTx, rid: ph.rid])
+
+ ph.eb.send(Address.dataStore.val, [action: DataStoreOperation.create, event: event.toJson()]) { Message reply ->
+ ph.sendOK('user', user.toJson())
+ }
+ ph.eb.send(Address.messageBoard.val, [rid: ph.rid, params: messageEvent]) { reply ->
+ ph.sendOK()
}
+
+
+ ph.eb.send(Address.dataStore.val, [action: DataStoreOperation.commit, rid: ph.rid])
+
}
}
}
@@ -6,15 +6,14 @@ import net.sf.oval.constraint.Email
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.BaseEntity
import org.riderzen.ogs.common.Tools
/**
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-19
*/
-class User extends BaseModel {
+class User extends BaseEntity {
@MinLength(6)
@MaxLength(64)
String username
@@ -52,6 +51,11 @@ class User extends BaseModel {
validate() ? user : null
}
+
+ @Override
+ String getEntityName() {
+ return 'auth_user'
+ }
}
enum UserType {
@@ -1,7 +1,7 @@
import org.riderzen.ogs.auth.RegisterProcessor
-import org.riderzen.ogs.auth.User
import org.riderzen.ogs.common.Address
-import org.riderzen.ogs.common.EventHelper
+import org.riderzen.ogs.common.ProcessHelper
+import org.riderzen.ogs.common.Tools
import org.vertx.groovy.core.eventbus.Message
/**
@@ -17,7 +17,7 @@ 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)
- new RegisterProcessor(eh: eh).process()
+ def eh = new ProcessHelper(container: container, vertx: vertx, message: message)
+ new RegisterProcessor(pid: Tools.nextId(RegisterProcessor.name), eh: eh).process()
}
@@ -7,7 +7,8 @@ package org.riderzen.ogs.common
enum Address {
appProtocol("ogs.protocol"),
sysAuthRegister("ogs.mod.sys-auth.register"),
- dataStore("ogs.plat.data-store")
+ dataStore("ogs.plat.data-store"),
+ messageBoard('ogs.plat.message-board')
public String val;
@@ -9,28 +9,28 @@ import net.sf.oval.constraint.NotNull
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-7
*/
-class BaseModel {
+abstract class BaseEntity {
Long id
@NotNull
Long createdOn
@NotNull
Long updatedOn
Long vsn
- static def transientAttributes = ['class', 'metaClass', 'transientAttributes', 'tableName']
-
- BaseModel() {
+ BaseEntity() {
}
- static def excludeAttributes(fields) {
- transientAttributes.addAll(fields)
+ static def transientAttributes = ['class', 'metaClass', 'transientAttributes', 'tableName']
+
+ def getTransientAttributes() {
+ return transientAttributes
}
- def pAttributes() {
+ def getpAttributes() {
def propMap = properties;
- transientAttributes?.each {
+ getTransientAttributes()?.each {
propMap.remove(it)
}
@@ -65,9 +65,12 @@ class BaseModel {
static GsonBuilder createBuilder() {
new GsonBuilder()
-// .serializeNulls()
+ .serializeNulls()
.excludeFieldsWithoutExposeAnnotation()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
}
+ String getShardBy() {}
+
+ abstract String getEntityName()
}
@@ -0,0 +1,16 @@
+package org.riderzen.ogs.common
+
+ /**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-27
+ */
+
+class BaseProcessor {
+ Long pid
+ ProcessHelper ph
+
+ BaseProcessor(Long pid, ProcessHelper ph) {
+ this.pid = pid
+ this.ph = ph
+ }
+}
@@ -5,13 +5,13 @@ package org.riderzen.ogs.common
* Date: 12-12-23
*/
class DataStoreHelper {
- def static String generateInsertSql(model) {
+ def static String generateInsertSql(BaseEntity entity) {
StringBuilder buffer = new StringBuilder("insert into ")
- buffer.append(model.tableName)
+ buffer.append(entity.entityName)
buffer.append(" (")
StringBuilder paramBuffer = new StringBuilder()
boolean first = true
- for (String column : model.pAttributes().keySet()) {
+ for (String column : entity.pAttributes.keySet()) {
if (first) {
first = false
paramBuffer.append("?")
@@ -5,5 +5,5 @@ package org.riderzen.ogs.common
* Date: 12-12-23
*/
enum DataStoreOperation {
- create, read, update, delete, count, readOne, createBatch, updateBatch, deleteBatch
+ create, read, update, delete, count, readOne, createBatch, updateBatch, deleteBatch, beginTx, commit, roollbak
}
@@ -0,0 +1,39 @@
+package org.riderzen.ogs.common
+
+import com.google.gson.FieldNamingPolicy
+import com.google.gson.GsonBuilder
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-27
+ */
+class EntityEvent {
+ EventType type
+ Long rid
+ Long pid
+ Long id
+ String entity
+ String body
+
+ def String toJson() {
+ createBuilder()
+ .create()
+ .toJson(this)
+ }
+
+ def fromJson(String json) {
+ createBuilder()
+ .create()
+ .fromJson(json, this.class)
+ }
+
+ static GsonBuilder createBuilder() {
+ new GsonBuilder()
+ .serializeNulls()
+ .excludeFieldsWithoutExposeAnnotation()
+ }
+}
+
+enum EventType {
+ create, update, delete
+}
@@ -14,17 +14,22 @@ import java.util.concurrent.atomic.AtomicInteger
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-20
*/
-class EventHelper {
+class ProcessHelper {
private AtomicInteger counter
+ Long rid
+ Long pid
Container container
Vertx vertx
Message message
+ Map results
+ Map errors
- EventHelper(Container container, Vertx vertx, Message message) {
+ ProcessHelper(Container container, Vertx vertx, Message message) {
this.container = container
this.vertx = vertx
this.message = message
+ rid = message.body.rid
}
def newProcess(int max) {
@@ -44,25 +49,26 @@ class EventHelper {
}
def getParam() {
- message?.body
+ message?.body?.params
}
void sendOK() {
- sendOK(Status.OK)
+ sendStatus(Status.OK)
}
void sendStatus(int status) {
sendStatus(status, null)
}
- void sendStatus(int status, Object data) {
+ void sendStatus(int status, String name, Object data) {
+ results.put(name, data)
if (counter.decrementAndGet()) return
- message.reply(new Gson().toJson([status: status, data: data]))
+ message.reply(new Gson().toJson([status: status, data: results]))
}
- void sendOK(Object data) {
- sendStatus(Status.OK, data)
+ void sendOK(String name, Object data) {
+ sendStatus(Status.OK, name, data)
}
void sendError(String error) {
@@ -6,19 +6,25 @@ import org.apache.commons.lang3.LocaleUtils
import org.vertx.java.core.impl.Context
import org.vertx.java.core.logging.impl.LoggerFactory
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.ConcurrentMap
+import java.util.concurrent.atomic.AtomicLong
+
/**
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-19
*/
class Tools {
private static ResourceBundle bundle
+ private static AtomicLong idCounter = new AtomicLong(0)
+ private static ConcurrentMap<String, AtomicLong> idMap = new ConcurrentHashMap<>()
static final String MESSAGES = "messages"
static logger = LoggerFactory.getLogger(Tools.class)
static salt = "sJK" + "Vn3I" + "GFwj" + "dty37"
static encrypt(message) {
- if(message) {
+ if (message) {
DigestUtils.md5Hex(message + salt())
}
}
@@ -35,15 +41,15 @@ class Tools {
[status: 0, data: data]
}
- static error(String key, String...params) {
+ 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 = message.replaceFirst('\\{\\}', it)
}
}
message
@@ -54,7 +60,7 @@ class Tools {
String lang = System.getProperty("lang")
Locale locale;
if (lang) {
- try{
+ try {
locale = LocaleUtils.toLocale(lang)
} catch (e) {
logger.error("Property lang is invalid: $lang", e)
@@ -71,10 +77,21 @@ class Tools {
}
}
- static def nextRid() {
+ static def uuid() {
UUID.randomUUID().toString();
}
+ static long nextId(String name = null) {
+ if (!name) {
+ return idCounter.getAndIncrement()
+ } else {
+ if (!idMap.containsKey(name)) {
+ idMap.put(name, new AtomicLong(0))
+ }
+ return idMap.get(name).getAndIncrement()
+ }
+ }
+
static getLogger() {
Context.context.deploymentHandle.logger
}
Oops, something went wrong.

0 comments on commit 19ac466

Please sign in to comment.