1 56 package org.objectstyle.cayenne.pref; 57 58 import java.util.Collections ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Timer ; 62 import java.util.TimerTask ; 63 64 import org.objectstyle.cayenne.CayenneRuntimeException; 65 import org.objectstyle.cayenne.access.DataContext; 66 import org.objectstyle.cayenne.access.DataDomain; 67 import org.objectstyle.cayenne.access.DataNode; 68 import org.objectstyle.cayenne.access.DbGenerator; 69 import org.objectstyle.cayenne.dba.DbAdapter; 70 import org.objectstyle.cayenne.map.DataMap; 71 72 77 public abstract class CayennePreferenceService implements PreferenceService { 78 79 public static final int MIN_SAVE_INTERVAL = 500; 80 public static final int DEFAULT_SAVE_INTERVAL = 20000; 81 public static final String SAVE_INTERVAL_KEY = "saveInterval"; 82 83 protected int saveInterval = DEFAULT_SAVE_INTERVAL; 84 protected Timer saveTimer; 85 protected DataContext dataContext; 86 protected String defaultDomain; 87 88 public CayennePreferenceService(String defaultDomain) { 89 this.defaultDomain = defaultDomain; 90 } 91 92 public DataContext getDataContext() { 93 return dataContext; 94 } 95 96 public void setDataContext(DataContext preferencesContext) { 97 this.dataContext = preferencesContext; 98 } 99 100 public int getSaveInterval() { 101 return saveInterval; 102 } 103 104 public void setSaveInterval(int ms) { 105 if (this.saveInterval != ms) { 106 this.saveInterval = ms; 107 108 getPreferenceDomain() 110 .getDetail(SAVE_INTERVAL_KEY, true) 111 .setIntProperty(SAVE_INTERVAL_KEY, ms); 112 } 113 } 114 115 118 public Domain getDomain(String name, boolean create) { 119 List results = getDataContext().performQuery( 120 "TopLevelDomain", 121 Collections.singletonMap("name", name), 122 false); 123 124 if (results.size() > 1) { 125 throw new CayenneRuntimeException("Found " 126 + results.size() 127 + " Domain objects for name '" 128 + name 129 + "', only one expected."); 130 } 131 132 if (results.size() == 1) { 133 return (Domain) results.get(0); 134 } 135 136 if (!create) { 137 return null; 138 } 139 140 Domain domain = (Domain) getDataContext() 141 .createAndRegisterNewObject(Domain.class); 142 domain.setLevel(new Integer (0)); 143 domain.setName(name); 144 savePreferences(); 145 146 return domain; 147 } 148 149 152 public void stopOnShutdown() { 153 Thread shutdown = new Thread ("CayennePrefrencesService Shutdown") { 154 155 public void run() { 156 stopService(); 157 } 158 }; 159 160 Runtime.getRuntime().addShutdownHook(shutdown); 161 } 162 163 public void savePreferences() { 164 DataContext context = this.dataContext; 165 166 if (context != null) { 167 context.commitChanges(); 168 } 169 } 170 171 protected Domain getPreferenceDomain() { 172 Domain defaultDomain = getDomain(this.defaultDomain, true); 173 return defaultDomain.getSubdomain(getClass()); 174 } 175 176 179 protected void initPreferences() { 180 Domain preferenceDomain = getPreferenceDomain(); 181 PreferenceDetail saveInterval = preferenceDomain.getDetail( 182 SAVE_INTERVAL_KEY, 183 false); 184 if (saveInterval != null) { 185 setSaveInterval(saveInterval.getIntProperty( 186 SAVE_INTERVAL_KEY, 187 DEFAULT_SAVE_INTERVAL)); 188 } 189 } 190 191 195 protected void initSchema() { 196 DataDomain domain = dataContext.getParentDataDomain(); 197 198 Iterator maps = domain.getDataMaps().iterator(); 199 while (maps.hasNext()) { 200 DataMap map = (DataMap) maps.next(); 201 DataNode node = domain.lookupDataNode(map); 202 DbAdapter adapter = node.getAdapter(); 203 DbGenerator generator = new DbGenerator(adapter, map); 204 205 try { 206 generator.runGenerator(node.getDataSource()); 207 } 208 catch (Throwable th) { 209 throw new PreferenceException("Error creating preferences DB", th); 210 } 211 } 212 } 213 214 217 protected void startTimer() { 218 TimerTask saveTask = new TimerTask () { 219 220 public void run() { 221 savePreferences(); 222 } 223 }; 224 225 int interval = (saveInterval > MIN_SAVE_INTERVAL) 226 ? saveInterval 227 : MIN_SAVE_INTERVAL; 228 saveTimer = new Timer (true); 229 saveTimer.schedule(saveTask, interval, interval); 230 } 231 } | Popular Tags |