Skip to content

Commit 03f8878

Browse files
committed
initial version full code
not tested
0 parents  commit 03f8878

28 files changed

+912
-0
lines changed

libs/allconfig.jar

5.19 KB
Binary file not shown.

libs/jackson-annotations-2.7.0.jar

49.7 KB
Binary file not shown.

libs/jackson-core-2.7.0.jar

246 KB
Binary file not shown.

libs/jackson-databind-2.7.1.jar

1.14 MB
Binary file not shown.

libs/javax.servlet.jar

81.6 KB
Binary file not shown.

libs/jetty-all-8.1.9.v20130131.jar

1.76 MB
Binary file not shown.
96.6 KB
Binary file not shown.

libs/jsonrpc4j-1.2.0.jar

101 KB
Binary file not shown.

libs/portlet-api-2.0.jar

46.9 KB
Binary file not shown.

libs/servlet-api-2.5.jar

103 KB
Binary file not shown.

libs/sqlite-jdbc-3.19.3.jar

5.93 MB
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package me.salimm.jrns;
2+
3+
import java.sql.SQLException;
4+
5+
import com.allConfig.conf.XMLConfig;
6+
7+
import me.salimm.jrns.errors.DatabaseNotSupported;
8+
import me.salimm.jrns.server.NameServer;
9+
10+
public class NameServerRunner {
11+
12+
public static void main(String[] args) throws ClassNotFoundException, SQLException, DatabaseNotSupported, Exception {
13+
NameServer server = new NameServer(new XMLConfig("conf.xml"));
14+
server.start();
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package me.salimm.jrns.constants;
2+
3+
public interface Constants {
4+
5+
public static final String CONF_TAG_NAME_DB_TYPE = "CONF.DBINFO.DB_TYPE";
6+
public static final String CONF_TAG_NAME_DB_USER = "CONF.DBINFO.USER";
7+
public static final String CONF_TAG_NAME_DB_PASS = "CONF.DBINFO.PASSWORD";
8+
public static final String CONF_TAG_NAME_DB_URL = "CONF.DBINFO.URL";
9+
public static final String CONF_TAG_NAME_DB_PORT = "CONF.DBINFO.PORT";
10+
public static final String CONF_TAG_NAME_DB_DBNAME = "CONF.DBINFO.DBNAME";
11+
12+
public static final String ORACLE_CONN_DRIVER_CLASSPATH = "oracle.jdbc.driver.OracleDriver";
13+
public static final String MYSQL_CONN_DRIVER_CLASSPATH = "com.mysql.jdbc.Driver";
14+
public static final String SQLLITE_CONN_DRIVER_CLASSPATH = "org.sqlite.JDBC";
15+
16+
public static final String CONF_TAG_NAME_VIPE_API_PORT = "CONF.RPC.PORT";
17+
18+
public static final int TYPE_SERVICE_IO_IN = 0;
19+
public static final int TYPE_SERVICE_IO_OUT = 1;
20+
21+
}

src/me/salimm/jrns/db/DBFactory.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package me.salimm.jrns.db;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.SQLException;
6+
import java.sql.Statement;
7+
8+
import com.allConfig.conf.AbstractConfig;
9+
10+
import me.salimm.jrns.constants.Constants;
11+
import me.salimm.jrns.errors.DatabaseNotSupported;
12+
import me.salimm.jrns.types.DBType;
13+
14+
/**
15+
*
16+
* DBConnections contains a set of static functions to create connections to
17+
* databases.
18+
*
19+
* @author Salim
20+
*
21+
*/
22+
public class DBFactory implements Constants {
23+
24+
/**
25+
*
26+
* Create Oracle connection
27+
*
28+
* @return
29+
* @throws ClassNotFoundException
30+
* @throws SQLException
31+
*/
32+
public static Connection createOracleConnection(String DB_CONN, String USER, String PASS)
33+
throws ClassNotFoundException, SQLException {
34+
Class.forName(ORACLE_CONN_DRIVER_CLASSPATH);
35+
Connection connection = DriverManager.getConnection(DB_CONN, USER, PASS);
36+
return connection;
37+
}
38+
39+
/**
40+
*
41+
*
42+
* Create MySQL Connection
43+
*
44+
* @param JDBC_DRIVER
45+
* @param DB_URL
46+
* @param USER
47+
* @param PASS
48+
* @return
49+
* @throws ClassNotFoundException
50+
* @throws SQLException
51+
*/
52+
public static Connection createMySQLConnection(String DB_URL, String USER, String PASS)
53+
throws ClassNotFoundException, SQLException {
54+
Connection conn = null;
55+
Class.forName(MYSQL_CONN_DRIVER_CLASSPATH);
56+
conn = DriverManager.getConnection(DB_URL, USER, PASS);
57+
return conn;
58+
}
59+
60+
/**
61+
*
62+
*
63+
* Create SQLLite Connection
64+
*
65+
* @param JDBC_DRIVER
66+
* @param DB_URL
67+
* @param USER
68+
* @param PASS
69+
* @return
70+
* @throws ClassNotFoundException
71+
* @throws SQLException
72+
*/
73+
public static Connection createSQLLiteConnection(String DB_URL, String USER, String PASS)
74+
throws ClassNotFoundException, SQLException {
75+
Connection conn = null;
76+
Class.forName(SQLLITE_CONN_DRIVER_CLASSPATH);
77+
conn = DriverManager.getConnection(DB_URL, USER, PASS);
78+
79+
String sql1 = "CREATE TABLE IF NOT EXISTS SERVICE (SID integer, NAME text);";
80+
// 0:in, 1: out
81+
String sql2 = "CREATE TABLE IF NOT EXISTS SERVICE_IO (SID integer, CLSPATH text, TYPE integer);";
82+
String sql3 = "CREATE TABLE IF NOT EXISTS SERVICE_PROVIDER (PID integer,SID integer, URL text, PORT integer);";
83+
84+
Statement stmt = conn.createStatement();
85+
86+
stmt.executeUpdate(sql1);
87+
stmt.executeUpdate(sql2);
88+
stmt.executeUpdate(sql3);
89+
90+
stmt.close();
91+
92+
return conn;
93+
}
94+
95+
/**
96+
* Get DBUtils instance based on database type in conf file
97+
*
98+
* @return
99+
* @throws ClassNotFoundException
100+
* @throws SQLException
101+
* @throws DatabaseNotSupported
102+
*/
103+
public static DBUtils getDBUtils(AbstractConfig conf)
104+
throws ClassNotFoundException, SQLException, DatabaseNotSupported {
105+
if (conf.getValue(CONF_TAG_NAME_DB_TYPE).equals(DBType.MYSQL.name())) {
106+
return new MySQLUtils();
107+
} else if (conf.getValue(CONF_TAG_NAME_DB_TYPE).equals(DBType.ORACLE.name())) {
108+
return new OracleUtils();
109+
} else if (conf.getValue(CONF_TAG_NAME_DB_TYPE).equals(DBType.SQLLITE.name())) {
110+
return new SQLLiteDBUtils();
111+
} else {
112+
throw new DatabaseNotSupported(CONF_TAG_NAME_DB_TYPE);
113+
}
114+
115+
}
116+
117+
/**
118+
* Get specific db connection based on database info in conf file
119+
*
120+
* @return
121+
* @throws ClassNotFoundException
122+
* @throws SQLException
123+
* @throws DatabaseNotSupported
124+
*/
125+
public static Connection getConnection(DBInfo dbInfo)
126+
throws ClassNotFoundException, SQLException, DatabaseNotSupported {
127+
if (dbInfo.getType().equals(DBType.MYSQL)) {
128+
return DBFactory.createMySQLConnection(dbInfo.getDbUrl(), dbInfo.getDbUser(), dbInfo.getDbPass());
129+
} else if (dbInfo.getType().equals(DBType.ORACLE)) {
130+
return DBFactory.createOracleConnection(dbInfo.getDbUrl(), dbInfo.getDbUser(), dbInfo.getDbPass());
131+
} else if (dbInfo.getType().equals(DBType.SQLLITE)) {
132+
return DBFactory.createOracleConnection(dbInfo.getDbUrl(), dbInfo.getDbUser(), dbInfo.getDbPass());
133+
}
134+
return null;
135+
136+
}
137+
138+
public static DBInfo getDBInfo(AbstractConfig conf) {
139+
return new DBInfo(conf.getValue(CONF_TAG_NAME_DB_URL), conf.getValue(CONF_TAG_NAME_DB_DBNAME),
140+
conf.getValue(CONF_TAG_NAME_DB_USER), conf.getValue(CONF_TAG_NAME_DB_PASS),
141+
DBType.fromString(conf.getValue(CONF_TAG_NAME_DB_TYPE)));
142+
}
143+
144+
}

src/me/salimm/jrns/db/DBInfo.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package me.salimm.jrns.db;
2+
3+
import me.salimm.jrns.types.DBType;
4+
5+
/**
6+
*
7+
* abstract class for required information to create database connection
8+
*
9+
* @author salimm
10+
*
11+
*/
12+
public class DBInfo {
13+
14+
/**
15+
* url to database
16+
*/
17+
private String dbUrl;
18+
/**
19+
* name of the database to connect to
20+
*/
21+
private String dbName;
22+
/**
23+
* username for authentication
24+
*/
25+
private String dbUser;
26+
/**
27+
* password for authentication
28+
*/
29+
private String dbPass;
30+
31+
private DBType type;
32+
33+
public DBInfo(String dbUrl, String dbName, String dbUser, String dbPass, DBType type) {
34+
this.setType(type);
35+
this.setDbUser(dbUser);
36+
this.setDbPass(dbPass);
37+
this.setDbName(dbName);
38+
this.setDbUrl(dbUrl);
39+
40+
}
41+
42+
public String getDbUrl() {
43+
return dbUrl;
44+
}
45+
46+
public void setDbUrl(String dbUrl) {
47+
this.dbUrl = dbUrl;
48+
}
49+
50+
public String getDbName() {
51+
return dbName;
52+
}
53+
54+
public void setDbName(String dbName) {
55+
this.dbName = dbName;
56+
}
57+
58+
public String getDbUser() {
59+
return dbUser;
60+
}
61+
62+
public void setDbUser(String dbUser) {
63+
this.dbUser = dbUser;
64+
}
65+
66+
public String getDbPass() {
67+
return dbPass;
68+
}
69+
70+
public void setDbPass(String dbPass) {
71+
this.dbPass = dbPass;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return " dbName: " + dbName + " dbUser: " + dbUser + " dbURL: " + dbUrl + " dbPass:" + dbPass;
77+
}
78+
79+
public DBType getType() {
80+
return type;
81+
}
82+
83+
public void setType(DBType type) {
84+
this.type = type;
85+
}
86+
}

src/me/salimm/jrns/db/DBUtils.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package me.salimm.jrns.db;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
7+
import me.salimm.jrns.rpc.ServiceInfo;
8+
import me.salimm.jrns.rpc.ServiceProviderInfo;
9+
10+
public interface DBUtils {
11+
12+
/**
13+
* get Server info that implements service with given id
14+
*
15+
* @param serviceId
16+
* @return
17+
* @throws SQLException
18+
*/
19+
public ServiceProviderInfo getServer(Connection conn,int serviceId) throws SQLException;
20+
21+
/**
22+
* get Server info that implements service with given name
23+
*
24+
* @param serviceName
25+
* @return
26+
* @throws SQLException
27+
*/
28+
public ServiceProviderInfo getServer(Connection conn,String serviceName) throws SQLException;
29+
30+
/**
31+
* get list of all Server info that implements service with given id
32+
*
33+
* @param serviceId
34+
* @return
35+
* @throws SQLException
36+
*/
37+
public ServiceProviderInfo[] getAllServer(Connection conn,int serviceId) throws SQLException;
38+
39+
/**
40+
*
41+
* get list of all Server info that implements service with given name
42+
*
43+
* @param serviceName
44+
* @return
45+
* @throws SQLException
46+
*/
47+
public ServiceProviderInfo[] getAllServer(Connection conn,String serviceName) throws SQLException;
48+
49+
/**
50+
*
51+
* get ServiceInfo for given service name
52+
*
53+
* @param serviceName
54+
* @return
55+
* @throws SQLException
56+
* @throws ClassNotFoundException
57+
*/
58+
public ServiceInfo getServiceInfo(Connection conn,String serviceName) throws SQLException, ClassNotFoundException;
59+
60+
/**
61+
*
62+
* get ServiceInfo for given service id
63+
*
64+
* @param serviceId
65+
* @return
66+
* @throws SQLException
67+
* @throws ClassNotFoundException
68+
*/
69+
public ServiceInfo getServiceInfo(Connection conn,int serviceId) throws SQLException, ClassNotFoundException;
70+
71+
/**
72+
*
73+
* register given server for given service
74+
*
75+
* @param service
76+
* @param provider
77+
* @return
78+
*/
79+
public boolean registerServiceProvider(Connection conn,ServiceInfo service, ServiceProviderInfo provider);
80+
81+
/**
82+
* remove given server from given service
83+
*
84+
*
85+
* @param service
86+
* @param provider
87+
* @return
88+
*/
89+
public boolean removeServiceProvider(Connection conn,ServiceInfo service, ServiceProviderInfo provider);
90+
91+
}

src/me/salimm/jrns/db/MySQLUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package me.salimm.jrns.db;
2+
3+
public class MySQLUtils extends SQLBasedUtils{
4+
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package me.salimm.jrns.db;
2+
3+
public class OracleUtils extends SQLBasedUtils{
4+
5+
6+
}

0 commit comments

Comments
 (0)