1 25 26 package org.objectweb.easybeans.persistence.xml; 27 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.LinkedList ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 import javax.sql.DataSource ; 39 40 import org.objectweb.easybeans.log.JLog; 41 import org.objectweb.easybeans.log.JLogFactory; 42 43 47 public final class JPersistenceUnitInfoHelper { 48 49 52 private static final String DEFAULT_PERSISTENCE_CONFIG = "org/objectweb/easybeans/persistence/conf/default-persistence.xml"; 53 54 57 private static JLog logger = JLogFactory.getLog(JPersistenceUnitInfoHelper.class); 58 59 64 private static JPersistenceUnitInfo defaultPersistenceunitInfo = null; 65 66 70 private static Map <String , JPersistenceUnitInfo> providersInfo = null; 71 72 75 private JPersistenceUnitInfoHelper() { 76 77 } 78 79 87 public static JPersistenceUnitInfo[] getPersistenceUnitInfo(final URL url) throws JPersistenceUnitInfoException { 88 loadDefaultValues(); 90 91 JPersistenceUnitInfo[] jPersistenceunitInfos = JPersistenceUnitInfoLoader.loadPersistenceUnitInfoImpl(url); 92 93 for (JPersistenceUnitInfo jPersistenceunitInfo : jPersistenceunitInfos) { 94 String jtaDsName = jPersistenceunitInfo.getJtaDataSourceName(); 96 97 if (jtaDsName != null && !jtaDsName.equals("")) { 99 DataSource ds = null; 100 try { 101 ds = (DataSource ) new InitialContext ().lookup(jtaDsName); 102 } catch (NamingException e) { 103 try { 106 logger.warn("Datasource named '" + jtaDsName 107 + "' was not found, use instead the default jndi name jdbc_1"); 108 ds = (DataSource ) new InitialContext ().lookup("jdbc_1"); 109 } catch (NamingException ne) { 110 throw new JPersistenceUnitInfoException("Cannot get jta DataSource with the JNDI name '" 111 + jtaDsName + "'.", ne); 112 } 113 } 114 jPersistenceunitInfo.setJtaDataSource(ds); 115 } 116 117 String nonJtaDsName = jPersistenceunitInfo.getNonJtaDataSourceName(); 119 if (nonJtaDsName != null && !nonJtaDsName.equals("")) { 121 DataSource ds = null; 122 try { 123 ds = (DataSource ) new InitialContext ().lookup(nonJtaDsName); 124 } catch (NamingException e) { 125 try { 128 logger.warn("Datasource named '" + jtaDsName 129 + "' was not found, use instead the default jndi name jdbc_1"); 130 ds = (DataSource ) new InitialContext ().lookup("jdbc_1"); 131 } catch (NamingException ne) { 132 throw new JPersistenceUnitInfoException("Cannot get non jta DataSource with the JNDI name '" 133 + nonJtaDsName + "'.", ne); 134 } 135 } 136 jPersistenceunitInfo.setNonJtaDataSource(ds); 137 } 138 139 if (jPersistenceunitInfo.getPersistenceProviderClassName() == null 141 || jPersistenceunitInfo.getPersistenceProviderClassName().equals("")) { 142 logger.info("No persistence provider was set, set to value {0}.", defaultPersistenceunitInfo 143 .getPersistenceProviderClassName()); 144 jPersistenceunitInfo.setPersistenceProviderClassName(defaultPersistenceunitInfo 145 .getPersistenceProviderClassName()); 146 } 147 148 JPersistenceUnitInfo providerDefaultConf = providersInfo.get(jPersistenceunitInfo 150 .getPersistenceProviderClassName()); 151 if (providerDefaultConf == null) { 152 logger.info("No default configuration for the persistence provider {0}", jPersistenceunitInfo 153 .getPersistenceProviderClassName()); 154 } else { 155 logger.info("Found a default configuration for the persistence provider {0}", jPersistenceunitInfo 156 .getPersistenceProviderClassName()); 157 Properties defaultProperties = providerDefaultConf.getProperties(); 158 Enumeration providerPropertiesEnum = defaultProperties.propertyNames(); 159 while (providerPropertiesEnum.hasMoreElements()) { 160 String key = (String ) providerPropertiesEnum.nextElement(); 161 String value = defaultProperties.getProperty(key); 162 if (jPersistenceunitInfo.getProperties().getProperty(key) == null) { 164 jPersistenceunitInfo.getProperties().setProperty(key, value); 165 logger.info("Setting the property {0} with value {1}", key, value); 166 } 167 } 168 169 } 170 } 171 return jPersistenceunitInfos; 172 } 173 174 179 private static synchronized void loadDefaultValues() throws JPersistenceUnitInfoException { 180 181 if (defaultPersistenceunitInfo != null && providersInfo != null) { 183 return; 184 } 185 defaultPersistenceunitInfo = new JPersistenceUnitInfo(); 186 providersInfo = new HashMap <String , JPersistenceUnitInfo>(); 187 188 ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); 190 Enumeration <URL > urlsConfig = null; 191 try { 192 urlsConfig = currentCL.getResources(DEFAULT_PERSISTENCE_CONFIG); 193 } catch (IOException e) { 194 throw new JPersistenceUnitInfoException("Cannot get resources with the name '" + DEFAULT_PERSISTENCE_CONFIG 195 + "' in the context classloader '" + currentCL + "'."); 196 } 197 198 LinkedList <URL > lstURLs = new LinkedList <URL >(); 200 while (urlsConfig.hasMoreElements()) { 201 lstURLs.addFirst(urlsConfig.nextElement()); 202 } 203 204 for (URL tmpURL : lstURLs) { 207 JPersistenceUnitInfo[] jPersistenceunitInfos = JPersistenceUnitInfoLoader 208 .loadPersistenceUnitInfoImpl(tmpURL); 209 if (jPersistenceunitInfos.length != 1) { 211 throw new JPersistenceUnitInfoException( 212 "Each default config file should have only one persistence unit '" + tmpURL + "'."); 213 } 214 JPersistenceUnitInfo pInfo = jPersistenceunitInfos[0]; 215 defaultPersistenceunitInfo.setPersistenceProviderClassName(pInfo.getPersistenceProviderClassName()); 216 217 Properties providersProperties = pInfo.getProperties(); 219 Enumeration providerNames = providersProperties.propertyNames(); 220 while (providerNames.hasMoreElements()) { 221 String providerName = (String ) providerNames.nextElement(); 222 223 JPersistenceUnitInfo existingProviderInfo = providersInfo.get(providerName); 225 if (existingProviderInfo == null) { 226 existingProviderInfo = new JPersistenceUnitInfo(); 227 providersInfo.put(providerName, existingProviderInfo); 228 } 229 230 Enumeration <URL > urlsProviderConf = null; 231 try { 232 urlsProviderConf = currentCL.getResources(providersProperties.getProperty(providerName)); 233 } catch (IOException e) { 234 throw new JPersistenceUnitInfoException("Cannot get resources with the name '" + providerName 235 + "' in the context classloader '" + currentCL + "'."); 236 } 237 LinkedList <URL > reverseProviderConfURLs = new LinkedList <URL >(); 239 while (urlsProviderConf.hasMoreElements()) { 240 reverseProviderConfURLs.addFirst(urlsProviderConf.nextElement()); 241 } 242 243 if (reverseProviderConfURLs.size() == 0) { 244 logger.warn("No default properties for persistence provider class named {0}", providerName); 245 } 246 247 for (URL providerURLConf : reverseProviderConfURLs) { 249 JPersistenceUnitInfo[] providerPersistenceunitInfos = JPersistenceUnitInfoLoader 250 .loadPersistenceUnitInfoImpl(providerURLConf); 251 if (providerPersistenceunitInfos.length != 1) { 253 throw new JPersistenceUnitInfoException( 254 "Each default config file should have only one persistence unit '" + providerURLConf 255 + "'."); 256 } 257 JPersistenceUnitInfo providerInfo = providerPersistenceunitInfos[0]; 258 259 Properties providerProperties = providerInfo.getProperties(); 261 Enumeration providerPropertiesEnum = providerProperties.propertyNames(); 262 while (providerPropertiesEnum.hasMoreElements()) { 263 String key = (String ) providerPropertiesEnum.nextElement(); 264 String value = providerProperties.getProperty(key); 265 existingProviderInfo.getProperties().setProperty(key, value); 267 } 268 269 } 270 } 271 272 } 273 274 logger.info("Default persistence provider set to value {0}.", defaultPersistenceunitInfo 275 .getPersistenceProviderClassName()); 276 } 277 278 } 279 | Popular Tags |