org.compass.core.CompassTemplate Maven / Gradle / Ivy
Show all versions of compass Show documentation
/*
* Copyright 2004-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.compass.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.compass.core.CompassTransaction.TransactionIsolation;
import org.compass.core.config.CompassSettings;
/**
* Helper class that simplifies the Compass access code using the template
* design pattern.
*
* The central method is "execute", supporting Compass code implementing the
* CompassCallback interface. It provides Compass Session handling such that
* neither the CompassCallback implementation nor the calling code needs to
* explicitly care about retrieving/closing Compass Sessions, handling Session
* lifecycle exceptions, or managing transactions. The template code is similar
* to
*
*
* CompassSession session = compass.openSession();
* CompassTransaction tx = null;
* try {
* tx = session.beginTransaction();
* Object result = compassCallback.doInCompass(session);
* tx.commit();
* return result;
* } catch (RuntimeException e) {
* if (tx != null) {
* tx.rollback();
* }
* throw e;
* } finally {
* session.close();
* }
*
*
*
* The template must have a Compass reference set, either using the tempalte
* constructor or using the set method.
*
* CompassTemplate also provides the same operations available when working with
* CompassSession, just that they are executed using the "execute" template
* method, which means that they enjoy it's session lifecycle and transaction
* support.
*
* @author kimchy
*/
public class CompassTemplate implements CompassOperations {
private static Log log = LogFactory.getLog(CompassTemplate.class);
private Compass compass;
private CompassSettings globalSessionSettings = new CompassSettings();
/**
* Creates a new CompassTemplate instance (remember to set Compass using the
* setCompass method).
*/
public CompassTemplate() {
}
/**
* Creates a new CompassTemplate instance, already initialized with a
* Compass instance.
*/
public CompassTemplate(Compass compass) {
this.compass = compass;
}
/**
* Sets the compass instance that will be used by the template.
*/
public void setCompass(Compass compass) {
this.compass = compass;
}
/**
* Returns the compass instance used by the template.
*
* @return the compass instance
*/
public Compass getCompass() {
return compass;
}
/**
* Executes the compass callback within a session and a transaction context.
*
* @param action The action to execute witin a compass transaction
* @return An object as the result of the compass action
* @throws CompassException
*/
public T execute(CompassCallback action) throws CompassException {
return execute(null, action);
}
/**
* Executes the compass callback within a session and a transaction context.
* Applies the given transaction isolation level.
*
* @param transactionIsolation The transaction isolation
* @param action The action to execute witin a compass transaction
* @return An object as the result of the compass action
* @throws CompassException
*/
public T execute(TransactionIsolation transactionIsolation, CompassCallback action) throws CompassException {
CompassSession session = compass.openSession();
session.getSettings().addSettings(globalSessionSettings);
CompassTransaction tx = null;
try {
tx = session.beginTransaction(transactionIsolation);
T result = action.doInCompass(session);
tx.commit();
return result;
} catch (RuntimeException e) {
if (tx != null) {
try {
tx.rollback();
} catch (Exception e1) {
log.error("Failed to rollback transaction, ignoring", e1);
}
}
throw e;
} catch (Error err) {
if (tx != null) {
try {
tx.rollback();
} catch (Exception e1) {
log.error("Failed to rollback transaction, ignoring", e1);
}
}
throw err;
} finally {
session.close();
}
}
/**
* Executes the compass callback within a session and a local transaction context.
* Applies the given transaction isolation level.
*
* @param action The action to execute witin a compass transaction
* @return An object as the result of the compass action
* @throws CompassException
*/
public T executeLocal(CompassCallback action) throws CompassException {
CompassSession session = compass.openSession();
session.getSettings().addSettings(globalSessionSettings);
CompassTransaction tx = null;
try {
tx = session.beginLocalTransaction();
T result = action.doInCompass(session);
tx.commit();
return result;
} catch (RuntimeException e) {
if (tx != null) {
try {
tx.rollback();
} catch (Exception e1) {
log.error("Failed to rollback transaction, ignoring", e1);
}
}
throw e;
} catch (Error err) {
if (tx != null) {
try {
tx.rollback();
} catch (Exception e1) {
log.error("Failed to rollback transaction, ignoring", e1);
}
}
throw err;
} finally {
session.close();
}
}
/**
* A helper execute method for find operations.
*
* @param action the callback to execute.
* @return The hits that match the query
* @throws CompassException
*/
public CompassHitsOperations executeFind(CompassCallback action) throws CompassException {
return execute(action);
}
// Compass Operations
public CompassSettings getSettings() {
throw new CompassException("getSettings should not be used with CompassTemplate. Either use getGlobalSettings or execute");
}
public void create(final Object obj) throws CompassException {
execute(new CompassCallback