KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > store > BasePersistentStore


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.server.store;
20
21 import java.util.Map JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 import java.sql.*;
25
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NameNotFoundException JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30
31 import sync4j.framework.server.store.PersistentStoreException;
32 import sync4j.framework.logging.Sync4jLogger;
33
34 import org.apache.commons.lang.StringUtils;
35
36 /**
37  * This is a base class for <i>PersistenStore</i> objects. It does not persist
38  * any object, but it provides services common to concrete implementations.
39  *
40  * @author Stefano Fornari @ Funambol
41  *
42  * @version $Id: BasePersistentStore.java,v 1.8 2005/06/02 13:08:19 stefano_fornari Exp $
43  *
44  */

45 public abstract class BasePersistentStore {
46     
47     // --------------------------------------------------------------- Constants
48

49     public static final String JavaDoc CONFIG_JNDI_DATA_SOURCE_NAME = "jndi-data-source-name";
50     
51     /**
52      * Logger
53      */

54     protected transient final Logger JavaDoc log = Sync4jLogger.getLogger();
55     
56     // -------------------------------------------------------------- Properties
57

58     /**
59      * The JNDI name of the datasource to be used
60      */

61     protected String JavaDoc jndiDataSourceName = null;
62     
63     public String JavaDoc getJndiDataSourceName() {
64         return this.jndiDataSourceName;
65     }
66     
67     public void setJndiDataSourceName(String JavaDoc jndiDataSourceName) throws PersistentStoreException {
68         this.jndiDataSourceName = jndiDataSourceName;
69         
70         if (jndiDataSourceName == null) {
71             dataSource = null;
72         }
73         
74         //
75
// we first try with the name as it is; if we get a NameNotFound
76
// exception than we try prepending 'java:/comp/env/'
77
//
78
try {
79             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
80             try {
81                 dataSource = (DataSource JavaDoc) ctx.lookup(jndiDataSourceName);
82             } catch (NameNotFoundException JavaDoc e) {
83                 if (jndiDataSourceName.startsWith("java:/")) {
84                     jndiDataSourceName = (jndiDataSourceName.length()>6)
85                                        ? jndiDataSourceName.substring(6)
86                                        : jndiDataSourceName
87                                        ;
88                 }
89                 
90                 dataSource = (DataSource JavaDoc) ctx.lookup("java:/comp/env/" + jndiDataSourceName);
91             }
92         } catch (NamingException JavaDoc e) {
93             throw new PersistentStoreException("Data source "
94             + jndiDataSourceName
95             + " not found"
96             , e
97             );
98         }
99     }
100     
101     // ---------------------------------------------------------- Protected data
102

103     protected transient DataSource JavaDoc dataSource = null;
104     
105     // ------------------------------------------------------------ Constructors
106

107     // ---------------------------------------------------------- Public methods
108

109     /** Configures the persistent store
110      *
111      * @param config an <i>Map</i> containing configuration parameters.
112      *
113      * @throws ConfigPersistentStoreException
114      *
115      */

116     public void configure(Map JavaDoc config) throws ConfigPersistentStoreException {
117         
118         checkConfigParams(config);
119         
120         try {
121             setJndiDataSourceName((String JavaDoc) config.get(CONFIG_JNDI_DATA_SOURCE_NAME));
122         } catch (PersistentStoreException e) {
123             throw new ConfigPersistentStoreException( "Error creating the datasource: "
124                                                     + e.getMessage()
125                                                     , e
126                                                     );
127         }
128     }
129         
130     // ------------------------------------------------------- Protected methods
131

132     // ------------------------------------------------------- Protected methods
133

134     // --------------------------------------------------------- Private methods
135

136     /**
137      * Checks if the given configuration parameters contain all required
138      * parameters. If not a <i>ConfigPersistentStoreException</i> is thrown.
139      *
140      * @param config the <i>Map</i> containing the configuration parameters
141      *
142      * @throws ConfigPersistentStoreException in case of missing parameters
143      */

144     private void checkConfigParams(Map JavaDoc config)
145     throws ConfigPersistentStoreException {
146         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
147         
148         if (StringUtils.isEmpty((String JavaDoc) config.get(CONFIG_JNDI_DATA_SOURCE_NAME))) {
149             sb.append(CONFIG_JNDI_DATA_SOURCE_NAME);
150         }
151     }
152 }
153
154
Popular Tags