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.spring;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.compass.core.Compass;
022: import org.compass.core.CompassTemplate;
023: import org.springframework.beans.factory.InitializingBean;
024:
025: /**
026: * Convenient super class for Compass data access objects.
027: * <p>
028: * Requires either a <code>Compass</code> or a <code>CompassTemplate</code>
029: * to be set.
030: * <p>
031: * The base class is intended for CompassTemplate usage, and it creates a new
032: * CompassTemplate if a Compass parameter is set.
033: *
034: * @author kimchy
035: */
036: public abstract class CompassDaoSupport implements InitializingBean {
037:
038: protected final Log log = LogFactory.getLog(getClass());
039:
040: private CompassTemplate compassTemplate;
041:
042: /**
043: * Sets the Compass to be used by this DAO. Will automatically create a
044: * CompassTemplate based on the given Compass.
045: *
046: * @param compass
047: */
048: public final void setCompass(Compass compass) {
049: this .compassTemplate = createCompassTemplate(compass);
050: }
051:
052: /**
053: * Creates a new {@link CompassTemplate} based on the provided {@link Compass}. Subclasses
054: * can override it to create a subclass of CompassTemplate, or change the configuration.
055: * <p>
056: * The method is only called when initializing the dao with Compass instance
057: * and not a CompassTemplate instance.
058: *
059: * @param compass
060: * @return A new {@link CompassTemplate} warpping the given {@link Compass} instance.
061: */
062: protected CompassTemplate createCompassTemplate(Compass compass) {
063: return new CompassTemplate(compass);
064: }
065:
066: /**
067: * Returns the Compass used by the DAO.
068: *
069: * @return compass instance used by the DAO
070: */
071: public final Compass getCompass() {
072: return (this .compassTemplate != null) ? this .compassTemplate
073: .getCompass() : null;
074: }
075:
076: /**
077: * Sets the CompassTemplate used by the DAO explicitly. It is an alternative
078: * to setting the Compass directly.
079: *
080: * @param compassTemplate
081: */
082: public final void setCompassTemplate(CompassTemplate compassTemplate) {
083: this .compassTemplate = compassTemplate;
084: }
085:
086: /**
087: * Returns the CompassTemplate for this DAO. Pre-initialized with Compass or
088: * set explicitly.
089: *
090: * @return The compass template for the DAO
091: */
092: public final CompassTemplate getCompassTemplate() {
093: return compassTemplate;
094: }
095:
096: public void afterPropertiesSet() throws Exception {
097: if (this .compassTemplate == null) {
098: throw new IllegalArgumentException(
099: "compass or compassTemplate property is required");
100: }
101: initDao();
102: }
103:
104: protected void initDao() throws Exception {
105:
106: }
107: }
|