00001
00026 package org.objectweb.jonas.db;
00027
00028 import java.util.ArrayList;
00029 import java.util.List;
00030 import java.util.StringTokenizer;
00031
00032 import javax.naming.Context;
00033 import javax.naming.NamingException;
00034
00035 import org.objectweb.jonas.common.Log;
00036 import org.objectweb.jonas.service.AbsServiceImpl;
00037 import org.objectweb.jonas.service.ServiceException;
00038 import org.objectweb.util.monolog.api.BasicLevel;
00039 import org.objectweb.util.monolog.api.Logger;
00040
00046 public abstract class AbsDBServiceImpl extends AbsServiceImpl {
00047
00051 private static final String PORT_NUMBER = "jonas.service.db.port";
00052
00056 private static final String DATABASE_NAME = "jonas.service.db.dbname";
00057
00061 private static final String DEFAULT_DATABASE_NAME = "db_jonas";
00062
00066 private static final String USERS = "jonas.service.db.user";
00067
00071 private static Logger logger = null;
00072
00079 protected void doInit(Context ctx) throws ServiceException {
00080 logger = Log.getLogger(Log.JONAS_DB_PREFIX);
00081
00082
00083 String port = null;
00084 try {
00085 port = (String) ctx.lookup(PORT_NUMBER);
00086 } catch (NamingException ne) {
00087 logger.log(BasicLevel.INFO, "Use the default port as the property is missing in jonas.properties file");
00088 }
00089
00090
00091 String databaseName = null;
00092 try {
00093 databaseName = (String) ctx.lookup(DATABASE_NAME);
00094 } catch (NamingException ne) {
00095 logger.log(BasicLevel.INFO, "Use the default databsename '" + DEFAULT_DATABASE_NAME
00096 + "' as the property is missing in jonas.properties file.");
00097 databaseName = DEFAULT_DATABASE_NAME;
00098 }
00099
00100
00101 List users = new ArrayList();
00102 boolean hasUsers = true;
00103 int i = 1;
00104 while (hasUsers) {
00105 try {
00106 String usernameAndPass = (String) ctx.lookup(USERS + i);
00107 logger.log(BasicLevel.DEBUG, "Adding user/password '" + usernameAndPass + "'.");
00108
00109 StringTokenizer st = new StringTokenizer(usernameAndPass, ":");
00110 String name = st.nextToken();
00111 String pass = "";
00112 if (st.hasMoreTokens()) {
00113 pass = st.nextToken();
00114 }
00115 users.add(new User(name, pass));
00116 } catch (NamingException ne) {
00117 logger.log(BasicLevel.DEBUG, "No more users (length =" + i + ").");
00118 hasUsers = false;
00119 }
00120 i++;
00121 }
00122
00123 initServer(users, databaseName, port);
00124 }
00125
00133 protected abstract void initServer(List users, String databaseName, String portNumber);
00134
00139 protected void doStart() throws ServiceException {
00140 logger.log(BasicLevel.DEBUG, "");
00141 }
00142
00147 protected void doStop() throws ServiceException {
00148 logger.log(BasicLevel.DEBUG, "");
00149 }
00150
00154 public static Logger getLogger() {
00155 return logger;
00156 }
00157 }