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.needle.coherence;
018:
019: import java.io.File;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import com.tangosol.net.cache.CacheStore;
026: import com.tangosol.util.Base;
027: import org.compass.core.Compass;
028: import org.compass.core.CompassCallbackWithoutResult;
029: import org.compass.core.CompassException;
030: import org.compass.core.CompassSession;
031: import org.compass.core.CompassTemplate;
032: import org.compass.core.config.CompassConfiguration;
033: import org.compass.core.config.CompassConfigurationFactory;
034:
035: /**
036: * A simple Compass implementation of Coherence CacheStore implementing the store and
037: * eraze methods (no implementation for load).
038: *
039: * <p>Note, this implementation relies on the fact that the map key is the id of the
040: * map value.
041: *
042: * @author kimchy
043: */
044: public class CompassCacheStore extends Base implements CacheStore {
045:
046: private String entityName;
047:
048: private Compass compass;
049:
050: private CompassTemplate compassTemplate;
051:
052: public CompassCacheStore(String entityName) {
053: this .entityName = entityName;
054: CompassConfiguration configuration = CompassConfigurationFactory
055: .newConfiguration();
056: configuration.configure();
057: compass = configuration.buildCompass();
058: compassTemplate = new CompassTemplate(compass);
059: }
060:
061: public CompassCacheStore(String entityName, String resource) {
062: this .entityName = entityName;
063: CompassConfiguration configuration = CompassConfigurationFactory
064: .newConfiguration();
065: configuration.configure(resource);
066: compass = configuration.buildCompass();
067: compassTemplate = new CompassTemplate(compass);
068: }
069:
070: public CompassCacheStore(String entityName, File configurationFile) {
071: this .entityName = entityName;
072: CompassConfiguration configuration = CompassConfigurationFactory
073: .newConfiguration();
074: configuration.configure(configurationFile);
075: compass = configuration.buildCompass();
076: compassTemplate = new CompassTemplate(compass);
077: }
078:
079: public Object load(Object o) {
080: return null;
081: }
082:
083: public Map loadAll(Collection collection) {
084: return new HashMap();
085: }
086:
087: public void store(Object key, Object value) {
088: compassTemplate.save(entityName, value);
089: }
090:
091: public void storeAll(final Map entries) {
092: compassTemplate.execute(new CompassCallbackWithoutResult() {
093: protected void doInCompassWithoutResult(
094: CompassSession session) throws CompassException {
095: for (Iterator it = entries.values().iterator(); it
096: .hasNext();) {
097: session.save(entityName, it.next());
098: }
099: }
100: });
101: }
102:
103: public void erase(Object key) {
104: compassTemplate.delete(entityName, key);
105: }
106:
107: public void eraseAll(final Collection keys) {
108: compassTemplate.execute(new CompassCallbackWithoutResult() {
109: protected void doInCompassWithoutResult(
110: CompassSession session) throws CompassException {
111: for (Iterator it = keys.iterator(); it.hasNext();) {
112: session.delete(entityName, it.next());
113: }
114: }
115: });
116: }
117: }
|