1 26 package org.objectweb.jonas.db; 27 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.StringTokenizer ; 31 32 import javax.naming.Context ; 33 import javax.naming.NamingException ; 34 35 import org.objectweb.jonas.common.Log; 36 import org.objectweb.jonas.service.AbsServiceImpl; 37 import org.objectweb.jonas.service.ServiceException; 38 import org.objectweb.util.monolog.api.BasicLevel; 39 import org.objectweb.util.monolog.api.Logger; 40 41 46 public abstract class AbsDBServiceImpl extends AbsServiceImpl { 47 48 51 private static final String PORT_NUMBER = "jonas.service.db.port"; 52 53 56 private static final String DATABASE_NAME = "jonas.service.db.dbname"; 57 58 61 private static final String DEFAULT_DATABASE_NAME = "db_jonas"; 62 63 66 private static final String USERS = "jonas.service.db.user"; 67 68 71 private static Logger logger = null; 72 73 78 79 protected void doInit(Context ctx) throws ServiceException { 80 logger = Log.getLogger(Log.JONAS_DB_PREFIX); 81 82 String port = null; 84 try { 85 port = (String ) ctx.lookup(PORT_NUMBER); 86 } catch (NamingException ne) { 87 logger.log(BasicLevel.INFO, "Use the default port as the property is missing in jonas.properties file"); 88 } 89 90 String databaseName = null; 92 try { 93 databaseName = (String ) ctx.lookup(DATABASE_NAME); 94 } catch (NamingException ne) { 95 logger.log(BasicLevel.INFO, "Use the default databsename '" + DEFAULT_DATABASE_NAME 96 + "' as the property is missing in jonas.properties file."); 97 databaseName = DEFAULT_DATABASE_NAME; 98 } 99 100 List users = new ArrayList (); 102 boolean hasUsers = true; 103 int i = 1; 104 while (hasUsers) { 105 try { 106 String usernameAndPass = (String ) ctx.lookup(USERS + i); 107 if (logger.isLoggable(BasicLevel.DEBUG)) { 108 logger.log(BasicLevel.DEBUG, "Adding user/password '" + usernameAndPass + "'."); 109 } 110 111 StringTokenizer st = new StringTokenizer (usernameAndPass, ":"); 112 String name = st.nextToken(); 113 String pass = ""; 114 if (st.hasMoreTokens()) { 115 pass = st.nextToken(); 116 } 117 users.add(new User(name, pass)); 118 } catch (NamingException ne) { 119 if (logger.isLoggable(BasicLevel.DEBUG)) { 120 logger.log(BasicLevel.DEBUG, "No more users (length =" + i + ")."); 121 } 122 hasUsers = false; 123 } 124 i++; 125 } 126 127 initServer(users, databaseName, port); 128 } 129 130 137 protected abstract void initServer(List users, String databaseName, String portNumber); 138 139 143 protected void doStart() throws ServiceException { 144 if (logger.isLoggable(BasicLevel.DEBUG)) { 145 logger.log(BasicLevel.DEBUG, ""); 146 } 147 } 148 149 153 protected void doStop() throws ServiceException { 154 if (logger.isLoggable(BasicLevel.DEBUG)) { 155 logger.log(BasicLevel.DEBUG, ""); 156 } 157 } 158 159 162 public static Logger getLogger() { 163 return logger; 164 } 165 } | Popular Tags |