Skip to content

Commit c1eefa3

Browse files
committed
multiple changes
1. updated allconfig jar 2. repackaged tests to be identified by maven 3. changed database table to store EnvInfo in database and retreive it 4. cahnged register to throw app specific Exception 3000 when already registered
1 parent 80db55d commit c1eefa3

15 files changed

+556
-502
lines changed

conf.out.xml

Whitespace-only changes.

pom.xml

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>org.mouji</groupId>
54
<artifactId>moujinameserver</artifactId>
6-
<version>0.1.0</version>
75
<packaging>jar</packaging>
86

97
<parent>
@@ -12,6 +10,13 @@
1210
<version>0.1.0</version>
1311
<relativePath>../</relativePath>
1412
</parent>
13+
14+
<repositories>
15+
<repository>
16+
<id>libsrepo</id>
17+
<url>file://${project.basedir}/libs/</url>
18+
</repository>
19+
</repositories>
1520

1621
<build>
1722
<sourceDirectory>src</sourceDirectory>
@@ -36,13 +41,13 @@
3641
<groupId>org.mouji</groupId>
3742
<artifactId>moujicommon</artifactId>
3843
<version>0.1.0</version>
39-
<scope>provided</scope>
44+
<scope>compile</scope>
4045
</dependency>
4146
<dependency>
4247
<groupId>org.mouji</groupId>
4348
<artifactId>moujistub4j</artifactId>
4449
<version>0.1.0</version>
45-
<scope>provided</scope>
50+
<scope>compile</scope>
4651
</dependency>
4752

4853

@@ -52,7 +57,7 @@
5257
<artifactId>jetty-all</artifactId>
5358
<version>9.4.0.RC3</version>
5459
<type>pom</type>
55-
<scope>provided</scope>
60+
<scope>compile</scope>
5661
<optional>true</optional>
5762
</dependency>
5863
<!-- https://mvnrepository.com/artifact/com.github.briandilley.jsonrpc4j/jsonrpc4j -->
@@ -62,38 +67,38 @@
6267
<groupId>com.fasterxml.jackson.core</groupId>
6368
<artifactId>jackson-databind</artifactId>
6469
<version>2.9.0.pr4</version>
65-
<scope>provided</scope>
70+
<scope>compile</scope>
6671
<optional>true</optional>
6772
</dependency>
6873
<!-- https://mvnrepository.com/artifact/org.msgpack/msgpack-core -->
6974
<dependency>
7075
<groupId>org.msgpack</groupId>
7176
<artifactId>msgpack-core</artifactId>
7277
<version>0.8.13</version>
73-
<scope>provided</scope>
78+
<scope>compile</scope>
7479
<optional>true</optional>
7580
</dependency>
7681
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
7782
<dependency>
7883
<groupId>com.fasterxml.jackson.core</groupId>
7984
<artifactId>jackson-core</artifactId>
8085
<version>2.9.0.pr4</version>
81-
<scope>provided</scope>
86+
<scope>compile</scope>
8287
<optional>true</optional>
8388
</dependency>
8489
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
8590
<dependency>
8691
<groupId>com.fasterxml.jackson.core</groupId>
8792
<artifactId>jackson-annotations</artifactId>
8893
<version>2.9.0.pr4</version>
89-
<scope>provided</scope>
94+
<scope>compile</scope>
9095
</dependency>
9196
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
9297
<dependency>
9398
<groupId>javax.servlet</groupId>
9499
<artifactId>javax.servlet-api</artifactId>
95100
<version>4.0.0-b07</version>
96-
<scope>provided</scope>
101+
<scope>compile</scope>
97102
<optional>true</optional>
98103
</dependency>
99104
<!-- https://mvnrepository.com/artifact/javax.portlet/portlet-api -->
@@ -102,21 +107,27 @@
102107
<artifactId>portlet-api</artifactId>
103108
<version>3.0.0</version>
104109
<optional>true</optional>
105-
<scope>provided</scope>
110+
<scope>compile</scope>
106111
</dependency>
107112
<!-- https://mvnrepository.com/artifact/junit/junit -->
108113
<dependency>
109114
<groupId>junit</groupId>
110115
<artifactId>junit</artifactId>
111116
<version>4.12</version>
112-
<scope>test</scope>
117+
<scope>compile</scope>
113118
</dependency>
114119
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
115-
<dependency>
120+
<dependency>
116121
<groupId>org.xerial</groupId>
117122
<artifactId>sqlite-jdbc</artifactId>
118123
<version>3.20.0</version>
119-
<scope>provided</scope>
124+
<scope>compile</scope>
125+
</dependency>
126+
<dependency>
127+
<groupId>me.salimm</groupId>
128+
<artifactId>allconfig</artifactId>
129+
<version>1.0.0</version>
130+
<scope>compile</scope>
120131
</dependency>
121132
</dependencies>
122133
</project>

src/org/mouji/ns/core/constants/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ public interface Constants {
3030
public static final String DB_SQL_TABLE_COLUMN_NAME_NAME = "NAME";
3131
public static final String DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID = "SERVICE_ID";
3232
public static final String DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID = "SERVICE_PROVIDER_ID";
33+
public static final String DB_SQL_TABLE_COLUMN_NAME_ENV_LANG = "ENV_LANG";
34+
public static final String DB_SQL_TABLE_COLUMN_NAME_ENV_OS = "ENV_OS";
35+
public static final String DB_SQL_TABLE_COLUMN_NAME_ENV_COMPILER = "ENV_COMPILER";
3336

3437
}

src/org/mouji/ns/core/db/MySQLUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ public boolean createServiceProviderTable(Connection conn) {
3030
String sql = "CREATE TABLE IF NOT EXISTS `" + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + "` (`"
3131
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID + "` int(11) unsigned NOT NULL AUTO_INCREMENT, `"
3232
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID + "` int(11) DEFAULT NULL, `" + DB_SQL_TABLE_COLUMN_NAME_URL
33-
+ "` varchar(200) DEFAULT NULL, `" + DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID
34-
+ "` int(11) DEFAULT NULL, PRIMARY KEY (`" + DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID
33+
+ "` varchar(200) DEFAULT NULL, `" + DB_SQL_TABLE_COLUMN_NAME_PORT + "` int(11) DEFAULT NULL, `"
34+
+ DB_SQL_TABLE_COLUMN_NAME_ENV_OS + "` varchar(200) DEFAULT NULL, `" + DB_SQL_TABLE_COLUMN_NAME_ENV_LANG
35+
+ "` varchar(200) DEFAULT NULL, `" + DB_SQL_TABLE_COLUMN_NAME_ENV_COMPILER
36+
+ "` varchar(200) DEFAULT NULL , PRIMARY KEY (`" + DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID
3537
+ "`), KEY `SERVICE_IDX` (`" + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID
3638
+ "`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
3739
try {
@@ -64,7 +66,7 @@ public boolean createServiceSupportTable(Connection conn) {
6466

6567
return true;
6668
}
67-
69+
6870
@Override
6971
public boolean cleanAllTables(Connection conn) {
7072

src/org/mouji/ns/core/db/SQLBasedUtils.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99

1010
import org.mouji.common.db.DBUtils;
11+
import org.mouji.common.errors.ServiceProviderAlreadyExistsException;
1112
import org.mouji.common.info.SerializationFormat;
1213
import org.mouji.common.info.ServiceInfo;
1314
import org.mouji.common.info.ServiceProviderInfo;
@@ -21,12 +22,15 @@ public abstract class SQLBasedUtils implements DBUtils, Constants {
2122
public ServiceSupportInfo[] getProviders(Connection conn, ServiceInfo<?> service) throws SQLException {
2223
ArrayList<ServiceProviderInfo> list = new ArrayList<ServiceProviderInfo>();
2324

24-
String sql = "SELECT URL, PORT from " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " WHERE "
25-
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID + " = " + service.getId() + ";";
25+
String sql = "SELECT URL, PORT, ENV_OS, ENV_LANG, ENV_COMPILER from " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER
26+
+ " WHERE " + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID + " = " + service.getId() + ";";
2627
ResultSet rs = conn.createStatement().executeQuery(sql);
2728
while (rs.next()) {
2829
list.add(new ServiceProviderInfo(rs.getString(DB_SQL_TABLE_COLUMN_NAME_URL),
29-
rs.getInt(DB_SQL_TABLE_COLUMN_NAME_PORT), EnvironmentInfo.currentEnvInfo()));
30+
rs.getInt(DB_SQL_TABLE_COLUMN_NAME_PORT),
31+
new EnvironmentInfo(rs.getString(DB_SQL_TABLE_COLUMN_NAME_ENV_LANG),
32+
rs.getString(DB_SQL_TABLE_COLUMN_NAME_ENV_OS),
33+
rs.getString(DB_SQL_TABLE_COLUMN_NAME_ENV_COMPILER))));
3034
}
3135
rs.getStatement().close();
3236

@@ -133,7 +137,7 @@ public ServiceInfo<?> getServiceInfo(Connection conn, int serviceId) throws SQLE
133137
}
134138

135139
@Override
136-
public boolean register(Connection conn, ServiceSupportInfo support) {
140+
public boolean register(Connection conn, ServiceSupportInfo support) throws ServiceProviderAlreadyExistsException {
137141
ServiceInfo<?> service = support.getService();
138142
ServiceProviderInfo provider = support.getProvider();
139143

@@ -142,8 +146,11 @@ public boolean register(Connection conn, ServiceSupportInfo support) {
142146
+ service.getId() + ",'" + service.getName() + "' );";
143147

144148
String sql = "INSERT INTO " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " (" + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID
145-
+ ", " + DB_SQL_TABLE_COLUMN_NAME_URL + ", " + DB_SQL_TABLE_COLUMN_NAME_PORT + ") VALUES ("
146-
+ service.getId() + ",'" + provider.getURL() + "'," + provider.getPort() + ");";
149+
+ ", " + DB_SQL_TABLE_COLUMN_NAME_URL + ", " + DB_SQL_TABLE_COLUMN_NAME_PORT + ", "
150+
+ DB_SQL_TABLE_COLUMN_NAME_ENV_OS + ", " + DB_SQL_TABLE_COLUMN_NAME_ENV_LANG + ", "
151+
+ DB_SQL_TABLE_COLUMN_NAME_ENV_COMPILER + ") VALUES (" + service.getId() + ",'" + provider.getURL()
152+
+ "'," + provider.getPort() + ",'" + provider.getEnv().getOS() + "'" + ",'"
153+
+ provider.getEnv().getLang() + "'" + ",'" + provider.getEnv().getCompiler() + "'" + ");";
147154

148155
try {
149156
Statement stmt = conn.createStatement();
@@ -174,8 +181,8 @@ public boolean register(Connection conn, ServiceSupportInfo support) {
174181
}
175182
stmt.close();
176183
} catch (SQLException e) {
177-
e.printStackTrace();
178-
return false;
184+
// e.printStackTrace();
185+
throw new ServiceProviderAlreadyExistsException(provider);
179186
}
180187
return true;
181188
}

src/org/mouji/ns/core/db/SQLLiteDBUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public boolean createServiceProviderTable(Connection conn) {
2929
String sql = "CREATE TABLE IF NOT EXISTS " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " ("
3030
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID + " INTEGER PRIMARY KEY, "
3131
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID + " INT, " + DB_SQL_TABLE_COLUMN_NAME_URL + " TEXT, "
32-
+ DB_SQL_TABLE_COLUMN_NAME_PORT + " INT, CONSTRAINT SP_POINTER_UNQ UNIQUE ("
33-
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID + ", " + DB_SQL_TABLE_COLUMN_NAME_URL + ", "
34-
+ DB_SQL_TABLE_COLUMN_NAME_PORT + ")); CREATE INDEX SERIVCE_PROVIDER_SERVICE_ID_IDX ON "
35-
+ DB_SQL_TABLE_NAME_SERVICE_PROVIDER + "(" + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID
32+
+ DB_SQL_TABLE_COLUMN_NAME_PORT + " INT, " + DB_SQL_TABLE_COLUMN_NAME_ENV_OS + " TEXT, "
33+
+ DB_SQL_TABLE_COLUMN_NAME_ENV_LANG + " TEXT, " + DB_SQL_TABLE_COLUMN_NAME_ENV_COMPILER
34+
+ " TEXT , CONSTRAINT SP_POINTER_UNQ UNIQUE (" + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID + ", "
35+
+ DB_SQL_TABLE_COLUMN_NAME_URL + ", " + DB_SQL_TABLE_COLUMN_NAME_PORT
36+
+ ")); CREATE INDEX SERIVCE_PROVIDER_SERVICE_ID_IDX ON " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + "("
37+
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID
3638
+ ");CREATE INDEX IF NOT EXISTS SERIVCE_PROVIDER_URL_PORT_IDX ON " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER
3739
+ "(" + DB_SQL_TABLE_COLUMN_NAME_URL + "," + DB_SQL_TABLE_COLUMN_NAME_PORT + ");";
3840
try {

src/org/mouji/ns/core/rpc/DBBasedNameServer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
import org.mouji.common.db.DBInfo;
1010
import org.mouji.common.db.DBUtils;
11+
import org.mouji.common.errors.ApplicationSpecificErrorException;
1112
import org.mouji.common.errors.DatabaseNotSupported;
13+
import org.mouji.common.errors.ServiceProviderAlreadyExistsException;
1214
import org.mouji.common.info.ServiceInfo;
1315
import org.mouji.common.info.ServiceProviderInfo;
1416
import org.mouji.common.info.ServiceSupportInfo;
@@ -66,7 +68,8 @@ public DBBasedNameServer(DBInfo dbInfo, DBUtils dbUtils, int port, ProviderLoadB
6668
public DBBasedNameServer(Config conf, String url, int port, ProviderLoadBalancer balancer)
6769
throws UnknownHostException, PrefixNotANestedConfigException, ClassNotFoundException, SQLException,
6870
DatabaseNotSupported {
69-
this(DBFactory.getDBInfo(conf), DBFactory.getDBUtils(conf), Inet4Address.getLocalHost().getHostAddress(), port, balancer);
71+
this(DBFactory.getDBInfo(conf), DBFactory.getDBUtils(conf), Inet4Address.getLocalHost().getHostAddress(), port,
72+
balancer);
7073
}
7174

7275
public DBBasedNameServer(Config conf, int port, ProviderLoadBalancer balancer) throws UnknownHostException,
@@ -123,10 +126,17 @@ public ServiceSupportInfo[] getAllProviders() throws ClassNotFoundException, SQL
123126

124127
@Override
125128
public boolean register(ServiceSupportInfo support)
126-
throws ClassNotFoundException, SQLException, DatabaseNotSupported {
129+
throws ClassNotFoundException, SQLException, DatabaseNotSupported, ApplicationSpecificErrorException {
127130
Connection conn = DBFactory.getConnection(dbInfo);
128-
boolean flag = dbUtils.register(DBFactory.getConnection(dbInfo), support);
131+
boolean flag = false;
132+
try {
133+
flag = dbUtils.register(DBFactory.getConnection(dbInfo), support);
134+
} catch (ServiceProviderAlreadyExistsException e) {
135+
throw new ApplicationSpecificErrorException(ERROR_SERVICE_PROVIDER_EXISTS_CODE,
136+
ERROR_SERVICE_PROVIDER_EXISTS_MSG);
137+
}
129138
conn.close();
139+
130140
return flag;
131141
}
132142

src/org/mouji/ns/core/rpc/NameServerServiceProvider.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private boolean checkArgs(int requiredArgs, ServiceRequest request, Class<?>[] t
107107
}
108108

109109
private ServiceResponse<?> handleUnregister(ServiceRequest request)
110-
throws ClassNotFoundException, SQLException, DatabaseNotSupported, InvalidArgsException {
110+
throws ClassNotFoundException, SQLException, DatabaseNotSupported, InvalidArgsException, ApplicationSpecificErrorException {
111111
checkArgs(2, request, new Class[] { ServiceInfo.class, ServiceProviderInfo.class });
112112
boolean flag = nameServer.unregister((ServiceInfo<?>) request.getArgs()[0].getContent(),
113113
(ServiceProviderInfo) request.getArgs()[1].getContent());
@@ -117,7 +117,7 @@ private ServiceResponse<?> handleUnregister(ServiceRequest request)
117117
}
118118

119119
private ServiceResponse<?> handleCheckProviderStatus(ServiceRequest request)
120-
throws ClassNotFoundException, SQLException, DatabaseNotSupported, InvalidArgsException {
120+
throws ClassNotFoundException, SQLException, DatabaseNotSupported, InvalidArgsException, ApplicationSpecificErrorException {
121121
checkArgs(1, request, new Class[] { ServiceProviderInfo.class });
122122
boolean flag = nameServer.checkProviderStatus((ServiceProviderInfo) request.getArgs()[0].getContent());
123123

@@ -126,7 +126,7 @@ private ServiceResponse<?> handleCheckProviderStatus(ServiceRequest request)
126126
}
127127

128128
private ServiceResponse<?> handleUnregisterAll(ServiceRequest request)
129-
throws ClassNotFoundException, SQLException, DatabaseNotSupported, InvalidArgsException {
129+
throws ClassNotFoundException, SQLException, DatabaseNotSupported, InvalidArgsException, ApplicationSpecificErrorException {
130130
checkArgs(1, request, new Class[] { ServiceProviderInfo.class });
131131
boolean flag = nameServer.unregisterAll((ServiceProviderInfo) request.getArgs()[0].getContent());
132132

@@ -135,7 +135,7 @@ private ServiceResponse<?> handleUnregisterAll(ServiceRequest request)
135135
}
136136

137137
private ServiceResponse<?> handleRegister(ServiceRequest request)
138-
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported {
138+
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported, ApplicationSpecificErrorException {
139139
checkArgs(1, request, new Class[] { ServiceSupportInfo.class });
140140
boolean flag = nameServer.register((ServiceSupportInfo) request.getArgs()[0].getContent());
141141

@@ -144,7 +144,7 @@ private ServiceResponse<?> handleRegister(ServiceRequest request)
144144
}
145145

146146
private ServiceResponse<?> handleGetServiceInfoByName(ServiceRequest request)
147-
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported {
147+
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported, ApplicationSpecificErrorException {
148148
checkArgs(1, request, new Class[] { String.class });
149149
ServiceInfo<?> info = nameServer.getServiceInfoByName((String) request.getArgs()[0].getContent());
150150

@@ -153,7 +153,7 @@ private ServiceResponse<?> handleGetServiceInfoByName(ServiceRequest request)
153153
}
154154

155155
private ServiceResponse<?> handleGetServiceInfoById(ServiceRequest request)
156-
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported {
156+
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported, ApplicationSpecificErrorException {
157157
checkArgs(1, request, new Class[] { Integer.class });
158158
ServiceInfo<?> info = nameServer.getServiceInfoById((Integer) request.getArgs()[0].getContent());
159159

@@ -162,7 +162,7 @@ private ServiceResponse<?> handleGetServiceInfoById(ServiceRequest request)
162162
}
163163

164164
private ServiceResponse<?> handleGetAllProvidersService(ServiceRequest request)
165-
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported {
165+
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported, ApplicationSpecificErrorException {
166166
checkArgs(0, request, new Class[] {});
167167
ServiceSupportInfo[] info = nameServer.getAllProviders();
168168

@@ -171,7 +171,7 @@ private ServiceResponse<?> handleGetAllProvidersService(ServiceRequest request)
171171
}
172172

173173
private ServiceResponse<?> handleGetProvidersService(ServiceRequest request)
174-
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported {
174+
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported, ApplicationSpecificErrorException {
175175
checkArgs(1, request, new Class[] { ServiceInfo.class });
176176
ServiceSupportInfo[] info = nameServer.getProviders((ServiceInfo<?>) request.getArgs()[0].getContent());
177177

@@ -180,7 +180,7 @@ private ServiceResponse<?> handleGetProvidersService(ServiceRequest request)
180180
}
181181

182182
private ServiceResponse<?> handleGetProviderService(ServiceRequest request)
183-
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported {
183+
throws InvalidArgsException, ClassNotFoundException, SQLException, DatabaseNotSupported, ApplicationSpecificErrorException {
184184
checkArgs(1, request, new Class[] { ServiceInfo.class });
185185
ServiceSupportInfo info = nameServer.getProvider((ServiceInfo<?>) request.getArgs()[0].getContent());
186186

0 commit comments

Comments
 (0)