1 18 19 package sync4j.framework.server.store; 20 21 import java.util.Map ; 22 import java.util.logging.Logger ; 23 24 import java.sql.*; 25 26 import javax.naming.InitialContext ; 27 import javax.naming.NameNotFoundException ; 28 import javax.naming.NamingException ; 29 import javax.sql.DataSource ; 30 31 import sync4j.framework.server.store.PersistentStoreException; 32 import sync4j.framework.logging.Sync4jLogger; 33 34 import org.apache.commons.lang.StringUtils; 35 36 45 public abstract class BasePersistentStore { 46 47 49 public static final String CONFIG_JNDI_DATA_SOURCE_NAME = "jndi-data-source-name"; 50 51 54 protected transient final Logger log = Sync4jLogger.getLogger(); 55 56 58 61 protected String jndiDataSourceName = null; 62 63 public String getJndiDataSourceName() { 64 return this.jndiDataSourceName; 65 } 66 67 public void setJndiDataSourceName(String jndiDataSourceName) throws PersistentStoreException { 68 this.jndiDataSourceName = jndiDataSourceName; 69 70 if (jndiDataSourceName == null) { 71 dataSource = null; 72 } 73 74 try { 79 InitialContext ctx = new InitialContext (); 80 try { 81 dataSource = (DataSource ) ctx.lookup(jndiDataSourceName); 82 } catch (NameNotFoundException e) { 83 if (jndiDataSourceName.startsWith("java:/")) { 84 jndiDataSourceName = (jndiDataSourceName.length()>6) 85 ? jndiDataSourceName.substring(6) 86 : jndiDataSourceName 87 ; 88 } 89 90 dataSource = (DataSource ) ctx.lookup("java:/comp/env/" + jndiDataSourceName); 91 } 92 } catch (NamingException e) { 93 throw new PersistentStoreException("Data source " 94 + jndiDataSourceName 95 + " not found" 96 , e 97 ); 98 } 99 } 100 101 103 protected transient DataSource dataSource = null; 104 105 107 109 116 public void configure(Map config) throws ConfigPersistentStoreException { 117 118 checkConfigParams(config); 119 120 try { 121 setJndiDataSourceName((String ) 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 132 134 136 144 private void checkConfigParams(Map config) 145 throws ConfigPersistentStoreException { 146 StringBuffer sb = new StringBuffer (); 147 148 if (StringUtils.isEmpty((String ) config.get(CONFIG_JNDI_DATA_SOURCE_NAME))) { 149 sb.append(CONFIG_JNDI_DATA_SOURCE_NAME); 150 } 151 } 152 } 153 154 | Popular Tags |