KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > db > AbsDBServiceImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: AbsDBServiceImpl.java,v 1.2 2005/04/28 08:43:26 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas.db;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.NamingException JavaDoc;
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 /**
42  * Abstract database service to be implemented by implementation of Java
43  * databases
44  * @author Florent Benoit
45  */

46 public abstract class AbsDBServiceImpl extends AbsServiceImpl {
47
48     /**
49      * Property for the port number
50      */

51     private static final String JavaDoc PORT_NUMBER = "jonas.service.db.port";
52
53     /**
54      * Property for the name of the database
55      */

56     private static final String JavaDoc DATABASE_NAME = "jonas.service.db.dbname";
57
58     /**
59      * Property for the default database name
60      */

61     private static final String JavaDoc DEFAULT_DATABASE_NAME = "db_jonas";
62
63     /**
64      * Property for looking up users (suffix by 1..n)
65      */

66     private static final String JavaDoc USERS = "jonas.service.db.user";
67
68     /**
69      * Logger used by this service
70      */

71     private static Logger logger = null;
72
73     /**
74      * Initialize the service.
75      * @param ctx the configuration context of the service.
76      * @throws ServiceException if the initialization failed.
77      */

78
79     protected void doInit(Context JavaDoc ctx) throws ServiceException {
80         logger = Log.getLogger(Log.JONAS_DB_PREFIX);
81
82         // Lookup port number
83
String JavaDoc port = null;
84         try {
85             port = (String JavaDoc) ctx.lookup(PORT_NUMBER);
86         } catch (NamingException JavaDoc ne) {
87             logger.log(BasicLevel.INFO, "Use the default port as the property is missing in jonas.properties file");
88         }
89
90         // Database name
91
String JavaDoc databaseName = null;
92         try {
93             databaseName = (String JavaDoc) ctx.lookup(DATABASE_NAME);
94         } catch (NamingException JavaDoc 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         // Users
101
List JavaDoc users = new ArrayList JavaDoc();
102         boolean hasUsers = true;
103         int i = 1;
104         while (hasUsers) {
105             try {
106                 String JavaDoc usernameAndPass = (String JavaDoc) ctx.lookup(USERS + i);
107                 if (logger.isLoggable(BasicLevel.DEBUG)) {
108                     logger.log(BasicLevel.DEBUG, "Adding user/password '" + usernameAndPass + "'.");
109                 }
110
111                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(usernameAndPass, ":");
112                 String JavaDoc name = st.nextToken();
113                 String JavaDoc pass = "";
114                 if (st.hasMoreTokens()) {
115                     pass = st.nextToken();
116                 }
117                 users.add(new User(name, pass));
118             } catch (NamingException JavaDoc 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     /**
131      * Create a database with the specified arguments. Need to be implemented by
132      * classes extending this one.
133      * @param users user/password (separated by a ":")
134      * @param databaseName name of the database
135      * @param portNumber port number of the database
136      */

137     protected abstract void initServer(List JavaDoc users, String JavaDoc databaseName, String JavaDoc portNumber);
138
139     /**
140      * Start the service.
141      * @throws ServiceException if the startup failed.
142      */

143     protected void doStart() throws ServiceException {
144         if (logger.isLoggable(BasicLevel.DEBUG)) {
145             logger.log(BasicLevel.DEBUG, "");
146         }
147     }
148
149     /**
150      * Stop the service.
151      * @throws ServiceException if the stop failed.
152      */

153     protected void doStop() throws ServiceException {
154         if (logger.isLoggable(BasicLevel.DEBUG)) {
155             logger.log(BasicLevel.DEBUG, "");
156         }
157     }
158
159     /**
160      * @return the logger.
161      */

162     public static Logger getLogger() {
163         return logger;
164     }
165 }
Popular Tags