KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > pref > CayennePreferenceService


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.pref;
57
58 import java.util.Collections JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Timer JavaDoc;
62 import java.util.TimerTask JavaDoc;
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 /**
73  * A Cayenne-based PreferenceService.
74  *
75  * @author Andrei Adamchik
76  */

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 JavaDoc SAVE_INTERVAL_KEY = "saveInterval";
82
83     protected int saveInterval = DEFAULT_SAVE_INTERVAL;
84     protected Timer JavaDoc saveTimer;
85     protected DataContext dataContext;
86     protected String JavaDoc defaultDomain;
87
88     public CayennePreferenceService(String JavaDoc 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             // save to preferences
109
getPreferenceDomain()
110                     .getDetail(SAVE_INTERVAL_KEY, true)
111                     .setIntProperty(SAVE_INTERVAL_KEY, ms);
112         }
113     }
114
115     /**
116      * Returns a top-level domain.
117      */

118     public Domain getDomain(String JavaDoc name, boolean create) {
119         List JavaDoc 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 JavaDoc(0));
143         domain.setName(name);
144         savePreferences();
145
146         return domain;
147     }
148
149     /**
150      * Configures service to run stopService on JVM shutdown.
151      */

152     public void stopOnShutdown() {
153         Thread JavaDoc shutdown = new Thread JavaDoc("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     /**
177      * Initializes service own prefrences.
178      */

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     /**
192      * Helper method that provides an easy way for subclasses to create preferences schema
193      * on the fly.
194      */

195     protected void initSchema() {
196         DataDomain domain = dataContext.getParentDataDomain();
197
198         Iterator JavaDoc 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 JavaDoc th) {
209                 throw new PreferenceException("Error creating preferences DB", th);
210             }
211         }
212     }
213
214     /**
215      * Starts preferences save timer.
216      */

217     protected void startTimer() {
218         TimerTask JavaDoc saveTask = new TimerTask JavaDoc() {
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 JavaDoc(true);
229         saveTimer.schedule(saveTask, interval, interval);
230     }
231 }
Popular Tags