001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.compass.core.CompassTransaction.TransactionIsolation;
022: import org.compass.core.config.CompassSettings;
023:
024: /**
025: * Helper class that simplifies the Compass access code using the template
026: * design pattern.
027: * <p>
028: * The central method is "execute", supporting Compass code implementing the
029: * CompassCallback interface. It provides Compass Session handling such that
030: * neither the CompassCallback implementation nor the calling code needs to
031: * explicitly care about retrieving/closing Compass Sessions, handling Session
032: * lifecycle exceptions, or managing transactions. The template code is similar
033: * to
034: *
035: * <pre>
036: * CompassSession session = compass.openSession();
037: * CompassTransaction tx = null;
038: * try {
039: * tx = session.beginTransaction();
040: * Object result = compassCallback.doInCompass(session);
041: * tx.commit();
042: * return result;
043: * } catch (RuntimeException e) {
044: * if (tx != null) {
045: * tx.rollback();
046: * }
047: * throw e;
048: * } finally {
049: * session.close();
050: * }
051: * </pre>
052: *
053: * <p>
054: * The template must have a Compass reference set, either using the tempalte
055: * constructor or using the set method.
056: * <p>
057: * CompassTemplate also provides the same operations available when working with
058: * CompassSession, just that they are executed using the "execute" template
059: * method, which means that they enjoy it's session lifecycle and transaction
060: * support.
061: *
062: * @author kimchy
063: */
064: public class CompassTemplate implements CompassOperations {
065:
066: private static Log log = LogFactory.getLog(CompassTemplate.class);
067:
068: private Compass compass;
069:
070: private CompassSettings globalSessionSettings = new CompassSettings();
071:
072: /**
073: * Creates a new CompassTemplate instance (remember to set Compass using the
074: * setCompass method).
075: */
076: public CompassTemplate() {
077:
078: }
079:
080: /**
081: * Creates a new CompassTemplate instance, already initialized with a
082: * Compass instance.
083: */
084: public CompassTemplate(Compass compass) {
085: this .compass = compass;
086: }
087:
088: /**
089: * Sets the compass instance that will be used by the template.
090: */
091: public void setCompass(Compass compass) {
092: this .compass = compass;
093: }
094:
095: /**
096: * Returns the compass instance used by the template.
097: *
098: * @return the compass instance
099: */
100: public Compass getCompass() {
101: return compass;
102: }
103:
104: /**
105: * Executes the compass callback within a session and a transaction context.
106: *
107: * @param action The action to execute witin a compass transaction
108: * @return An object as the result of the compass action
109: * @throws CompassException
110: */
111: public <T> T execute(CompassCallback<T> action)
112: throws CompassException {
113: return execute(null, action);
114: }
115:
116: /**
117: * Executes the compass callback within a session and a transaction context.
118: * Applies the given transaction isolation level.
119: *
120: * @param transactionIsolation The transaction isolation
121: * @param action The action to execute witin a compass transaction
122: * @return An object as the result of the compass action
123: * @throws CompassException
124: */
125: public <T> T execute(TransactionIsolation transactionIsolation,
126: CompassCallback<T> action) throws CompassException {
127: CompassSession session = compass.openSession();
128: session.getSettings().addSettings(globalSessionSettings);
129: CompassTransaction tx = null;
130: try {
131: tx = session.beginTransaction(transactionIsolation);
132: T result = action.doInCompass(session);
133: tx.commit();
134: return result;
135: } catch (RuntimeException e) {
136: if (tx != null) {
137: try {
138: tx.rollback();
139: } catch (Exception e1) {
140: log.error(
141: "Failed to rollback transaction, ignoring",
142: e1);
143: }
144: }
145: throw e;
146: } catch (Error err) {
147: if (tx != null) {
148: try {
149: tx.rollback();
150: } catch (Exception e1) {
151: log.error(
152: "Failed to rollback transaction, ignoring",
153: e1);
154: }
155: }
156: throw err;
157: } finally {
158: session.close();
159: }
160: }
161:
162: /**
163: * Executes the compass callback within a session and a <b>local</b> transaction context.
164: * Applies the given transaction isolation level.
165: *
166: * @param action The action to execute witin a compass transaction
167: * @return An object as the result of the compass action
168: * @throws CompassException
169: */
170: public <T> T executeLocal(CompassCallback<T> action)
171: throws CompassException {
172: CompassSession session = compass.openSession();
173: session.getSettings().addSettings(globalSessionSettings);
174: CompassTransaction tx = null;
175: try {
176: tx = session.beginLocalTransaction();
177: T result = action.doInCompass(session);
178: tx.commit();
179: return result;
180: } catch (RuntimeException e) {
181: if (tx != null) {
182: try {
183: tx.rollback();
184: } catch (Exception e1) {
185: log.error(
186: "Failed to rollback transaction, ignoring",
187: e1);
188: }
189: }
190: throw e;
191: } catch (Error err) {
192: if (tx != null) {
193: try {
194: tx.rollback();
195: } catch (Exception e1) {
196: log.error(
197: "Failed to rollback transaction, ignoring",
198: e1);
199: }
200: }
201: throw err;
202: } finally {
203: session.close();
204: }
205: }
206:
207: /**
208: * A helper execute method for find operations.
209: *
210: * @param action the callback to execute.
211: * @return The hits that match the query
212: * @throws CompassException
213: */
214: public CompassHitsOperations executeFind(
215: CompassCallback<CompassHitsOperations> action)
216: throws CompassException {
217: return execute(action);
218: }
219:
220: // Compass Operations
221:
222: public CompassSettings getSettings() {
223: throw new CompassException(
224: "getSettings should not be used with CompassTemplate. Either use getGlobalSettings or execute");
225: }
226:
227: public void create(final Object obj) throws CompassException {
228: execute(new CompassCallback<Object>() {
229: public Object doInCompass(CompassSession session)
230: throws CompassException {
231: session.create(obj);
232: return null;
233: }
234: });
235: }
236:
237: public void create(final String alias, final Object obj)
238: throws CompassException {
239: execute(new CompassCallback<Object>() {
240: public Object doInCompass(CompassSession session)
241: throws CompassException {
242: session.create(alias, obj);
243: return null;
244: }
245: });
246: }
247:
248: public void delete(final Object obj) throws CompassException {
249: execute(new CompassCallback<Object>() {
250: public Object doInCompass(CompassSession session)
251: throws CompassException {
252: session.delete(obj);
253: return null;
254: }
255: });
256: }
257:
258: public void delete(final Resource resource) throws CompassException {
259: execute(new CompassCallback<Object>() {
260: public Object doInCompass(CompassSession session)
261: throws CompassException {
262: session.delete(resource);
263: return null;
264: }
265: });
266: }
267:
268: public void delete(final Class clazz, final Object obj)
269: throws CompassException {
270: execute(new CompassCallback<Object>() {
271: public Object doInCompass(CompassSession session)
272: throws CompassException {
273: session.delete(clazz, obj);
274: return null;
275: }
276: });
277: }
278:
279: public void delete(final String alias, final Object obj)
280: throws CompassException {
281: execute(new CompassCallback<Object>() {
282: public Object doInCompass(CompassSession session)
283: throws CompassException {
284: session.delete(alias, obj);
285: return null;
286: }
287: });
288: }
289:
290: public void delete(final CompassQuery query)
291: throws CompassException {
292: execute(new CompassCallback<Object>() {
293: public Object doInCompass(CompassSession session)
294: throws CompassException {
295: session.delete(query);
296: return null;
297: }
298: });
299: }
300:
301: public CompassHits find(final String query) throws CompassException {
302: return execute(new CompassCallback<CompassHits>() {
303: public CompassHits doInCompass(CompassSession session)
304: throws CompassException {
305: return session.find(query);
306: }
307: });
308: }
309:
310: public CompassDetachedHits findWithDetach(final String query)
311: throws CompassException {
312: return execute(new CompassCallback<CompassDetachedHits>() {
313: public CompassDetachedHits doInCompass(
314: CompassSession session) throws CompassException {
315: return session.find(query).detach();
316: }
317: });
318: }
319:
320: public CompassDetachedHits findWithDetach(final String query,
321: final int from, final int size) throws CompassException {
322: return execute(new CompassCallback<CompassDetachedHits>() {
323: public CompassDetachedHits doInCompass(
324: CompassSession session) throws CompassException {
325: return session.find(query).detach(from, size);
326: }
327: });
328: }
329:
330: public <T> T get(final Class<T> clazz, final Object id)
331: throws CompassException {
332: return execute(new CompassCallback<T>() {
333: public T doInCompass(CompassSession session)
334: throws CompassException {
335: return session.get(clazz, id);
336: }
337: });
338: }
339:
340: public Object get(final String alias, final Object id)
341: throws CompassException {
342: return execute(new CompassCallback<Object>() {
343: public Object doInCompass(CompassSession session)
344: throws CompassException {
345: return session.get(alias, id);
346: }
347: });
348: }
349:
350: public Resource getResource(final Class clazz, final Object id)
351: throws CompassException {
352: return (Resource) execute(new CompassCallback<Object>() {
353: public Object doInCompass(CompassSession session)
354: throws CompassException {
355: return session.getResource(clazz, id);
356: }
357: });
358: }
359:
360: public Resource getResource(final String alias, final Object id)
361: throws CompassException {
362: return (Resource) execute(new CompassCallback<Object>() {
363: public Object doInCompass(CompassSession session)
364: throws CompassException {
365: return session.getResource(alias, id);
366: }
367: });
368: }
369:
370: public <T> T load(Class<T> clazz, Object... ids)
371: throws CompassException {
372: return load(clazz, (Object) ids);
373: }
374:
375: public <T> T load(final Class<T> clazz, final Object id)
376: throws CompassException {
377: return execute(new CompassCallback<T>() {
378: public T doInCompass(CompassSession session)
379: throws CompassException {
380: return session.load(clazz, id);
381: }
382: });
383: }
384:
385: public Object load(String alias, Object... ids)
386: throws CompassException {
387: return load(alias, (Object) ids);
388: }
389:
390: public Object load(final String alias, final Object id)
391: throws CompassException {
392: return execute(new CompassCallback<Object>() {
393: public Object doInCompass(CompassSession session)
394: throws CompassException {
395: return session.load(alias, id);
396: }
397: });
398: }
399:
400: public Resource loadResource(final Class clazz, final Object id)
401: throws CompassException {
402: return (Resource) execute(new CompassCallback<Object>() {
403: public Object doInCompass(CompassSession session)
404: throws CompassException {
405: return session.loadResource(clazz, id);
406: }
407: });
408: }
409:
410: public Resource loadResource(final String alias, final Object id)
411: throws CompassException {
412: return (Resource) execute(new CompassCallback<Object>() {
413: public Object doInCompass(CompassSession session)
414: throws CompassException {
415: return session.loadResource(alias, id);
416: }
417: });
418: }
419:
420: public void save(final Object obj) throws CompassException {
421: execute(new CompassCallback<Object>() {
422: public Object doInCompass(CompassSession session)
423: throws CompassException {
424: session.save(obj);
425: return null;
426: }
427: });
428: }
429:
430: public void saveResource(final Resource resource)
431: throws CompassException {
432: execute(new CompassCallback<Object>() {
433: public Object doInCompass(CompassSession session)
434: throws CompassException {
435: session.saveResource(resource);
436: return null;
437: }
438: });
439: }
440:
441: public void save(final String alias, final Object obj)
442: throws CompassException {
443: execute(new CompassCallback<Object>() {
444: public Object doInCompass(CompassSession session)
445: throws CompassException {
446: session.save(alias, obj);
447: return null;
448: }
449: });
450: }
451:
452: public void evict(final Object obj) {
453: execute(new CompassCallbackWithoutResult() {
454: protected void doInCompassWithoutResult(
455: CompassSession session) throws CompassException {
456: session.evict(obj);
457: }
458: });
459: }
460:
461: public void evict(final String alias, final Object id) {
462: execute(new CompassCallbackWithoutResult() {
463: protected void doInCompassWithoutResult(
464: CompassSession session) throws CompassException {
465: session.evict(alias, id);
466: }
467: });
468: }
469:
470: public void evict(final Resource resource) {
471: execute(new CompassCallbackWithoutResult() {
472: protected void doInCompassWithoutResult(
473: CompassSession session) throws CompassException {
474: session.evict(resource);
475: }
476: });
477: }
478:
479: public void evictAll() {
480: execute(new CompassCallbackWithoutResult() {
481: protected void doInCompassWithoutResult(
482: CompassSession session) throws CompassException {
483: session.evictAll();
484: }
485: });
486: }
487:
488: public Resource getResource(Class clazz, Object... ids)
489: throws CompassException {
490: return getResource(clazz, (Object) ids);
491: }
492:
493: public Resource getResource(String alias, Object... ids)
494: throws CompassException {
495: return getResource(alias, (Object) ids);
496: }
497:
498: public Resource loadResource(Class clazz, Object... ids)
499: throws CompassException {
500: return loadResource(clazz, (Object) ids);
501: }
502:
503: public Resource loadResource(String alias, Object... ids)
504: throws CompassException {
505: return loadResource(alias, (Object) ids);
506: }
507:
508: public void delete(String alias, Object... ids)
509: throws CompassException {
510: delete(alias, (Object) ids);
511: }
512:
513: public void delete(Class clazz, Object... ids)
514: throws CompassException {
515: delete(clazz, (Object) ids);
516: }
517:
518: public <T> T get(Class<T> clazz, Object... ids)
519: throws CompassException {
520: return get(clazz, (Object) ids);
521: }
522:
523: public Object get(String alias, Object... ids)
524: throws CompassException {
525: return get(alias, (Object) ids);
526: }
527: }
|