Skip to content

Commit a34e56b

Browse files
committed
fixed to suppor changes in stubs. Also multiple fixes to db queries
1 parent d6833ec commit a34e56b

File tree

4 files changed

+156
-42
lines changed

4 files changed

+156
-42
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public ServiceSupportInfo[] getProviders(Connection conn, ServiceInfo<?> service
4747
+ " = " + service.getId() + " and sp." + DB_SQL_TABLE_COLUMN_NAME_URL + " = '" + provider.getURL()
4848
+ "' and sp." + DB_SQL_TABLE_COLUMN_NAME_PORT + "=" + provider.getPort() + " ;";
4949

50-
System.out.println(sql2);
51-
5250
ResultSet rs2 = conn.createStatement().executeQuery(sql2);
5351
List<SerializationFormat> tmp = new ArrayList<SerializationFormat>();
5452
while (rs2.next()) {
@@ -68,13 +66,14 @@ public ServiceSupportInfo[] getAllProviders(Connection conn) throws SQLException
6866
ArrayList<ServiceProviderInfo> list = new ArrayList<ServiceProviderInfo>();
6967
ArrayList<ServiceInfo<?>> services = new ArrayList<ServiceInfo<?>>();
7068

71-
String sql = "SELECT s.URL, s.PORT from " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + "as sp, "
72-
+ DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " as s WHERE s." + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID
69+
String sql = "SELECT sp.URL, sp.PORT, s.NAME as SERVICE_NAME, s.SERVICE_ID from " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " as sp, "
70+
+ DB_SQL_TABLE_NAME_SERVICE + " as s WHERE s." + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID
7371
+ " = sp." + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID + ";";
7472
ResultSet rs = conn.createStatement().executeQuery(sql);
7573
while (rs.next()) {
7674
list.add(new ServiceProviderInfo(rs.getString(DB_SQL_TABLE_COLUMN_NAME_URL),
7775
rs.getInt(DB_SQL_TABLE_COLUMN_NAME_PORT), StubEnvInfo.currentEnvInfo()));
76+
services.add(new ServiceInfo<>(rs.getString("SERVICE_NAME"), rs.getInt("SERVICE_ID")));
7877
}
7978
rs.getStatement().close();
8079

@@ -86,19 +85,20 @@ public ServiceSupportInfo[] getAllProviders(Connection conn) throws SQLException
8685
sup.setProvider(provider);
8786
sup.setService(services.get(i));
8887

89-
String sql2 = "SELECT * from " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " as sp, "
88+
String sql2 = "SELECT "+DB_SQL_TABLE_COLUMN_NAME_FORMAT_NAME+", "+DB_SQL_TABLE_COLUMN_NAME_FORMAT_VERSION+" from " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " as sp, "
9089
+ DB_SQL_TABLE_NAME_SERVICE_PROVIDER_SUPPORT + " as sps WHERE sp."
9190
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID + " = sps."
9291
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID + " and " + DB_SQL_TABLE_COLUMN_NAME_SERVICE_ID
93-
+ " = " + services.get(i).getId() + " and sp." + DB_SQL_TABLE_COLUMN_NAME_URL + " = '"
94-
+ provider.getURL() + "' and sp." + DB_SQL_TABLE_COLUMN_NAME_PORT + "=" + provider.getPort() + " ;";
92+
+ " = " + services.get(i).getId() + " and sp." + DB_SQL_TABLE_COLUMN_NAME_URL + " = '" + provider.getURL()
93+
+ "' and sp." + DB_SQL_TABLE_COLUMN_NAME_PORT + "=" + provider.getPort() + " ;";
9594

9695
ResultSet rs2 = conn.createStatement().executeQuery(sql2);
9796
List<SerializationFormat> tmp = new ArrayList<SerializationFormat>();
9897
while (rs2.next()) {
99-
tmp.add(new SerializationFormat());
98+
tmp.add(new SerializationFormat(rs2.getString(1),rs2.getString(2)));
10099
}
101100

101+
sup.setSerializers(tmp.toArray(new SerializationFormat[0]));
102102
out[i] = sup;
103103
}
104104

@@ -159,7 +159,6 @@ public boolean register(Connection conn, ServiceSupportInfo support) {
159159
}
160160

161161
for (SerializationFormat format : support.getSerializers()) {
162-
System.out.println(format);
163162
String sql2 = "INSERT INTO " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER_SUPPORT + " ("
164163
+ DB_SQL_TABLE_COLUMN_NAME_SERVICE_PROVIDER_ID + ", " + DB_SQL_TABLE_COLUMN_NAME_FORMAT_NAME
165164
+ ", " + DB_SQL_TABLE_COLUMN_NAME_FORMAT_VERSION + ") VALUES (" + pid + ",'" + format.getName()
@@ -185,9 +184,9 @@ public boolean register(Connection conn, ServiceSupportInfo support) {
185184
public boolean unregister(Connection conn, ServiceInfo<?> service, ServiceProviderInfo provider) {
186185

187186
try {
188-
String sql = "DELETE FROM TABLE " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " WHERE "
187+
String sql = "DELETE FROM " + DB_SQL_TABLE_NAME_SERVICE_PROVIDER + " WHERE "
189188
+ DB_SQL_TABLE_COLUMN_NAME_URL + " = '" + provider.getURL() + "' and "
190-
+ DB_SQL_TABLE_COLUMN_NAME_PORT + "=" + provider.getPort() + ";";
189+
+ DB_SQL_TABLE_COLUMN_NAME_PORT + "=" + provider.getPort() + " and SERVICE_ID="+service.getId()+" ;";
191190

192191
Statement stmt = conn.createStatement();
193192
stmt.executeUpdate(sql);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.mouji.common.info.responses.ServiceResponse;
1818
import org.mouji.common.services.NameServer;
1919
import org.mouji.common.services.NameServerServices;
20-
import org.mouji.common.services.ServiceProvider;import org.mouji.stub.java.stubs.NSServerStub;
20+
import org.mouji.common.services.ServiceProvider;
2121

2222
public class NameServerServiceProvider implements ServiceProvider {
2323

@@ -89,7 +89,7 @@ private boolean checkArgs(int requiredArgs, ServiceRequest request, Class<?>[] t
8989

9090
for (int i = 0; i < request.getArgs().length; i++) {
9191
// checking if the provided object is is instance of required type
92-
if (!(types[i].isAssignableFrom(request.getArgs()[0].getContent().getClass()))) {
92+
if (!(types[i].isAssignableFrom(request.getArgs()[i].getContent().getClass()))) {
9393
throw new InvalidArgsException("Argument number " + i + " expected to have type "
9494
+ types[i].getCanonicalName() + " but an object of type "
9595
+ request.getArgs()[i].getClass().getCanonicalName() + " was provided");
@@ -102,6 +102,7 @@ private boolean checkArgs(int requiredArgs, ServiceRequest request, Class<?>[] t
102102

103103
private ServiceResponse<?> handleUnregister(ServiceRequest request)
104104
throws ClassNotFoundException, SQLException, DatabaseNotSupported, InvalidArgsException {
105+
System.out.println(request);
105106
checkArgs(2, request, new Class[] { ServiceInfo.class, ServiceProviderInfo.class });
106107
boolean flag = nameServer.unregister((ServiceInfo<?>) request.getArgs()[0].getContent(),
107108
(ServiceProviderInfo) request.getArgs()[1].getContent());

src/org/mouji/ns/test/NameServerTest.java

Lines changed: 123 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,150 @@ public void testGetProvider() throws Exception {
103103

104104
@Test
105105
public void testGetProviders() throws Exception {
106-
int[] ports = new int[] { 1, 2, 3 };
106+
int[] ports = new int[] { 1, 2, 3, 4 };
107107

108108
ServiceInfo<Integer> service = new ServiceInfo<Integer>("test", 1);
109109
ServiceProviderInfo provider1 = new ServiceProviderInfo("tes1", ports[0], StubEnvInfo.currentEnvInfo());
110110
ServiceProviderInfo provider2 = new ServiceProviderInfo("test2", ports[1], StubEnvInfo.currentEnvInfo());
111111
ServiceProviderInfo provider3 = new ServiceProviderInfo("test3", ports[2], StubEnvInfo.currentEnvInfo());
112112

113+
ServiceInfo<Integer> service2 = new ServiceInfo<Integer>("tes2", 2);
114+
ServiceProviderInfo provider4 = new ServiceProviderInfo("tes1", ports[3], StubEnvInfo.currentEnvInfo());
115+
113116
NSClient client = new NSClient(nsInfo, new ArrayList<>());
114117
boolean flag = client.register(new ServiceSupportInfo(service, provider1,
115118
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
116119
boolean flag2 = client.register(new ServiceSupportInfo(service, provider2,
117120
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
118121
boolean flag3 = client.register(new ServiceSupportInfo(service, provider3,
119122
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
123+
boolean flag4 = client.register(new ServiceSupportInfo(service2, provider4,
124+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
120125

121-
Assert.assertEquals(true, flag & flag2 & flag3);
126+
Assert.assertEquals(true, flag & flag2 & flag3 & flag4);
122127

123128
ServiceSupportInfo[] providers = client.getProviders(service);
124129

125130
Assert.assertEquals(3, providers.length);
126131

127-
// Assert.assertEquals(sampleProvider, provider.getProvider());
132+
Assert.assertEquals(service, providers[0].getService());
133+
Assert.assertEquals(service, providers[1].getService());
134+
Assert.assertEquals(service, providers[2].getService());
135+
136+
Assert.assertEquals(1, providers[0].getSerializers().length);
137+
Assert.assertEquals(1, providers[1].getSerializers().length);
138+
Assert.assertEquals(1, providers[2].getSerializers().length);
139+
140+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[0].getSerializers()[0]);
141+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[1].getSerializers()[0]);
142+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[2].getSerializers()[0]);
143+
144+
Assert.assertEquals(provider1, providers[0].getProvider());
145+
Assert.assertEquals(provider2, providers[1].getProvider());
146+
Assert.assertEquals(provider3, providers[2].getProvider());
147+
148+
}
149+
150+
@Test
151+
public void testGetAllProviders() throws Exception {
152+
int[] ports = new int[] { 1, 2, 3, 4 };
153+
154+
ServiceInfo<Integer> service = new ServiceInfo<Integer>("test", 1);
155+
ServiceProviderInfo provider1 = new ServiceProviderInfo("tes1", ports[0], StubEnvInfo.currentEnvInfo());
156+
ServiceProviderInfo provider2 = new ServiceProviderInfo("test2", ports[1], StubEnvInfo.currentEnvInfo());
157+
ServiceProviderInfo provider3 = new ServiceProviderInfo("test3", ports[2], StubEnvInfo.currentEnvInfo());
158+
159+
ServiceInfo<Integer> service2 = new ServiceInfo<Integer>("tes2", 2);
160+
ServiceProviderInfo provider4 = new ServiceProviderInfo("tes1", ports[3], StubEnvInfo.currentEnvInfo());
161+
162+
NSClient client = new NSClient(nsInfo, new ArrayList<>());
163+
boolean flag = client.register(new ServiceSupportInfo(service, provider1,
164+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
165+
boolean flag2 = client.register(new ServiceSupportInfo(service, provider2,
166+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
167+
boolean flag3 = client.register(new ServiceSupportInfo(service, provider3,
168+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
169+
boolean flag4 = client.register(new ServiceSupportInfo(service2, provider4,
170+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
171+
172+
Assert.assertEquals(true, flag & flag2 & flag3 & flag4);
173+
174+
ServiceSupportInfo[] providers = client.getAllProviders();
175+
for (ServiceSupportInfo ssi : providers) {
176+
System.out.println(ssi);
177+
}
178+
179+
Assert.assertEquals(4, providers.length);
180+
181+
Assert.assertEquals(service, providers[0].getService());
182+
Assert.assertEquals(service, providers[1].getService());
183+
Assert.assertEquals(service, providers[2].getService());
184+
Assert.assertEquals(service2, providers[3].getService());
185+
186+
Assert.assertEquals(1, providers[0].getSerializers().length);
187+
Assert.assertEquals(1, providers[1].getSerializers().length);
188+
Assert.assertEquals(1, providers[2].getSerializers().length);
189+
Assert.assertEquals(1, providers[3].getSerializers().length);
190+
191+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[0].getSerializers()[0]);
192+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[1].getSerializers()[0]);
193+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[2].getSerializers()[0]);
194+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[3].getSerializers()[0]);
195+
196+
Assert.assertEquals(provider1, providers[0].getProvider());
197+
Assert.assertEquals(provider2, providers[1].getProvider());
198+
Assert.assertEquals(provider3, providers[2].getProvider());
199+
Assert.assertEquals(provider4, providers[3].getProvider());
200+
}
201+
202+
@Test
203+
public void testUnregister() throws Exception {
204+
int[] ports = new int[] { 1, 2, 3, 4 };
205+
206+
ServiceInfo<Integer> service = new ServiceInfo<Integer>("test", 1);
207+
ServiceProviderInfo provider1 = new ServiceProviderInfo("tes1", ports[0], StubEnvInfo.currentEnvInfo());
208+
ServiceProviderInfo provider2 = new ServiceProviderInfo("test2", ports[1], StubEnvInfo.currentEnvInfo());
209+
ServiceProviderInfo provider3 = new ServiceProviderInfo("test3", ports[2], StubEnvInfo.currentEnvInfo());
210+
211+
ServiceInfo<Integer> service2 = new ServiceInfo<Integer>("tes2", 2);
212+
ServiceProviderInfo provider4 = new ServiceProviderInfo("tes1", ports[3], StubEnvInfo.currentEnvInfo());
213+
214+
NSClient client = new NSClient(nsInfo, new ArrayList<>());
215+
boolean flag = client.register(new ServiceSupportInfo(service, provider1,
216+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
217+
boolean flag2 = client.register(new ServiceSupportInfo(service, provider2,
218+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
219+
boolean flag3 = client.register(new ServiceSupportInfo(service, provider3,
220+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
221+
boolean flag4 = client.register(new ServiceSupportInfo(service2, provider4,
222+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
223+
224+
boolean flag5 = client.unregister(service2, provider4);
225+
226+
Assert.assertEquals(true, flag & flag2 & flag3 & flag4 & flag5);
227+
228+
ServiceSupportInfo[] providers = client.getAllProviders();
229+
for (ServiceSupportInfo ssi : providers) {
230+
System.out.println(ssi);
231+
}
232+
233+
Assert.assertEquals(3, providers.length);
234+
235+
Assert.assertEquals(service, providers[0].getService());
236+
Assert.assertEquals(service, providers[1].getService());
237+
Assert.assertEquals(service, providers[2].getService());
238+
239+
Assert.assertEquals(1, providers[0].getSerializers().length);
240+
Assert.assertEquals(1, providers[1].getSerializers().length);
241+
Assert.assertEquals(1, providers[2].getSerializers().length);
128242

129-
// Assert.assertEquals(1, provider.getSerializers().length);
130-
// Assert.assertEquals(SerializationFormat.defaultFotmat().getName(), provider.getSerializers()[0].getName());
131-
// Assert.assertEquals(SerializationFormat.defaultFotmat().getVersion(),
132-
// provider.getSerializers()[0].getVersion());
243+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[0].getSerializers()[0]);
244+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[1].getSerializers()[0]);
245+
Assert.assertEquals(SerializationFormat.defaultFotmat(), providers[2].getSerializers()[0]);
133246

247+
Assert.assertEquals(provider1, providers[0].getProvider());
248+
Assert.assertEquals(provider2, providers[1].getProvider());
249+
Assert.assertEquals(provider3, providers[2].getProvider());
134250
}
135251

136252
@After

src/org/mouji/ns/test/TestMain.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,26 @@ public class TestMain {
2222

2323
public static void main(String[] args) throws ResponseContentTypeCannotBePrasedException,
2424
SerializationFormatNotSupported, RPCException, RPCProviderFailureException, IOException, Exception {
25-
// NameServerInfo nsInfo;
26-
// ServiceProviderInfo sampleProvider = new ServiceProviderInfo("test",
27-
// 5, StubEnvInfo.currentEnvInfo());
28-
//
29-
//
30-
//
31-
// ServiceProviderInfo nsSPInfo = new
32-
// ServiceProviderInfo(Inet4Address.getLocalHost().getHostAddress(),
33-
// 6161,
34-
// StubEnvInfo.currentEnvInfo());
35-
// nsInfo = new NameServerInfo(nsSPInfo.getURL(), nsSPInfo.getPort());
36-
//
37-
// ServiceInfo<Integer> service = new ServiceInfo<Integer>("test", 1);
38-
//
39-
// NSClient client = new NSClient(nsInfo, new ArrayList<>());
40-
//
41-
// ServiceSupportInfo provider = client.getProvider(service);
42-
//
43-
// System.out.println(provider);
44-
45-
System.out.println();
46-
System.out.println(ServiceSupportInfo[].class);
25+
NameServerInfo nsInfo;
26+
ServiceProviderInfo sampleProvider = new ServiceProviderInfo("test",
27+
5, StubEnvInfo.currentEnvInfo());
28+
29+
30+
31+
ServiceProviderInfo nsSPInfo = new
32+
ServiceProviderInfo(Inet4Address.getLocalHost().getHostAddress(),
33+
6161,
34+
StubEnvInfo.currentEnvInfo());
35+
nsInfo = new NameServerInfo(nsSPInfo.getURL(), nsSPInfo.getPort());
36+
37+
ServiceInfo<Integer> service = new ServiceInfo<Integer>("test", 1);
38+
39+
NSClient client = new NSClient(nsInfo, new ArrayList<>());
40+
41+
ServiceSupportInfo provider = client.getProvider(service);
42+
43+
System.out.println(provider);
44+
4745

4846
}
4947
}

0 commit comments

Comments
 (0)