1 56 package org.objectstyle.cayenne.modeler.pref; 57 58 import java.io.File ; 59 import java.sql.SQLException ; 60 import java.util.ArrayList ; 61 import java.util.Collection ; 62 import java.util.Collections ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import javax.sql.DataSource ; 68 69 import org.apache.log4j.Level; 70 import org.objectstyle.cayenne.CayenneRuntimeException; 71 import org.objectstyle.cayenne.DataObjectUtils; 72 import org.objectstyle.cayenne.access.DataContext; 73 import org.objectstyle.cayenne.conf.Configuration; 74 import org.objectstyle.cayenne.conf.DataSourceFactory; 75 import org.objectstyle.cayenne.conn.PoolManager; 76 import org.objectstyle.cayenne.exp.Expression; 77 import org.objectstyle.cayenne.exp.ExpressionFactory; 78 import org.objectstyle.cayenne.modeler.Application; 79 import org.objectstyle.cayenne.pref.DomainPreference; 80 import org.objectstyle.cayenne.pref.HSQLEmbeddedPreferenceService; 81 import org.objectstyle.cayenne.project.CayenneUserDir; 82 import org.objectstyle.cayenne.query.SelectQuery; 83 84 92 public class PreferencesDataSourceFactory implements DataSourceFactory { 93 94 protected int minPoolSize; 95 protected int maxPoolSize; 96 97 public PreferencesDataSourceFactory() { 98 this(1, 5); 100 } 101 102 public PreferencesDataSourceFactory(int minPoolSize, int maxPoolSize) { 103 this.minPoolSize = minPoolSize; 104 this.maxPoolSize = maxPoolSize; 105 } 106 107 public int getMaxPoolSize() { 108 return maxPoolSize; 109 } 110 111 public int getMinPoolSize() { 112 return minPoolSize; 113 } 114 115 public void initializeWithParentConfiguration(Configuration configuaration) { 116 } 118 119 public DataSource getDataSource(String location) throws Exception { 120 return getDataSource(location, Level.INFO); 121 } 122 123 127 public DataSource getDataSource(final String location, Level logLevel) 128 throws Exception { 129 if (location == null) { 130 throw new NullPointerException ("Null location"); 131 } 132 133 135 String configuredName = System.getProperty(Application.APPLICATION_NAME_PROPERTY); 139 String name = (configuredName != null) 140 ? configuredName 141 : Application.DEFAULT_APPLICATION_NAME; 142 143 String subdir = System.getProperty(Application.PREFERENCES_VERSION_PROPERTY); 144 145 if (subdir == null) { 146 subdir = Application.PREFERENCES_VERSION; 147 } 148 149 File dbDir = new File (CayenneUserDir.getInstance().resolveFile( 150 Application.PREFERENCES_DB_SUBDIRECTORY), subdir); 151 152 if (!dbDir.isDirectory()) { 154 throw new CayenneRuntimeException( 155 "No preferences database directory exists: " + dbDir); 156 } 157 else if (!new File (dbDir, "db.properties").exists()) { 158 throw new CayenneRuntimeException( 159 "No preferences database exists in directory " + dbDir); 160 } 161 162 String preferencesDB = new File (dbDir, "db").getAbsolutePath(); 163 164 HSQLEmbeddedPreferenceService service = new HSQLEmbeddedPreferenceService( 166 preferencesDB, 167 Application.PREFERENCES_MAP_PACKAGE, 168 name) { 169 170 protected void startTimer() { 171 } 173 174 protected void initPreferences() { 175 } 177 178 protected void initSchema() { 179 throw new CayenneRuntimeException("No preferences matching location: " 181 + location); 182 } 183 }; 184 185 try { 186 service.startService(); 187 return toDataSource(service.getDataContext(), location); 188 } 189 finally { 190 try { 192 service.stopService(); 193 } 194 catch (Throwable th) { 195 } 197 } 198 } 199 200 DataSource toDataSource(DataContext context, String location) throws Exception { 201 202 Expression locationFilter = ExpressionFactory.matchExp( 204 DomainPreference.KEY_PROPERTY, 205 location); 206 List preferences = context.performQuery(new SelectQuery( 207 DomainPreference.class, 208 locationFilter)); 209 210 if (preferences.isEmpty()) { 211 throw new CayenneRuntimeException("No preferences matching location: " 212 + location); 213 } 214 215 Collection ids = new ArrayList (preferences.size()); 216 Iterator it = preferences.iterator(); 217 while (it.hasNext()) { 218 DomainPreference pref = (DomainPreference) it.next(); 219 ids.add(DataObjectUtils.pkForObject(pref)); 220 } 221 222 Expression qualifier = Expression.fromString("db:" 223 + DBConnectionInfo.ID_PK_COLUMN 224 + " in $ids"); 225 Map params = Collections.singletonMap("ids", ids); 226 SelectQuery query = new SelectQuery(DBConnectionInfo.class, qualifier 227 .expWithParameters(params)); 228 229 List connectionData = context.performQuery(query); 231 232 if (connectionData.isEmpty()) { 233 throw new CayenneRuntimeException("No preferences matching location: " 234 + location); 235 } 236 237 if (connectionData.size() > 1) { 238 throw new CayenneRuntimeException( 239 "More than one preference matched location: " + location); 240 } 241 242 DBConnectionInfo info = (DBConnectionInfo) connectionData.get(0); 243 244 if (info.getJdbcDriver() == null) { 245 throw new CayenneRuntimeException( 246 "Incomplete connection info: no JDBC driver set."); 247 } 248 249 if (info.getUrl() == null) { 250 throw new SQLException ("Incomplete connection info: no DB URL set."); 251 } 252 253 return new PoolManager(info.getJdbcDriver(), info.getUrl(), 1, 5, info 256 .getUserName(), info.getPassword()); 257 } 258 } 259 | Popular Tags |