001: package com.jofti.plugin;
002:
003: import java.util.Properties;
004:
005: import com.ibm.websphere.objectgrid.BackingMap;
006: import com.ibm.websphere.objectgrid.ObjectGrid;
007: import com.ibm.websphere.objectgrid.ObjectGridException;
008: import com.ibm.websphere.objectgrid.ObjectGridManager;
009: import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
010: import com.ibm.websphere.objectgrid.ObjectGridRuntimeException;
011: import com.ibm.websphere.objectgrid.TxID;
012: import com.ibm.websphere.objectgrid.plugins.LogSequence;
013: import com.ibm.websphere.objectgrid.plugins.index.MapIndexInfo;
014: import com.ibm.websphere.objectgrid.plugins.index.MapIndexPlugin;
015: import com.jofti.api.IndexManager;
016: import com.jofti.cache.adapter.ObjectGridListenerAdapter;
017: import com.jofti.cache.adapter.listener.ObjectGridEventListener;
018: import com.jofti.config.DefaultIndexConfig;
019: import com.jofti.exception.JoftiException;
020: import com.jofti.manager.IndexManagerImpl;
021:
022: /**
023: * <p>
024: * Implementation of ObjectGrid's MapIndexPlugin. The implmentation configures itself lazily on either the first
025: * call to getIndexProxy or the first commit callback.
026: * </p>
027: * <p>
028: * Due to the information passed into the Plug-In it is also necessary to configure the GridName and the BackingMapName.
029: * </p>
030: * @author steve
031: * @version 1.1
032: * @since 1.2
033: *
034: */
035: public class ObjectGridIndexPlugIn implements MapIndexPlugin {
036:
037: IndexManager manager = new IndexManagerImpl();
038: DefaultIndexConfig config = new DefaultIndexConfig();
039: private boolean started = false;
040: private String configFile = null;
041: private String backingMapName = null;
042: private String gridName = null;
043:
044: ObjectGridEventListener listener = null;
045:
046: public ObjectGridIndexPlugIn() {
047:
048: config = new DefaultIndexConfig();
049: Properties props = new Properties();
050: props.put("plugin", "true");
051: config.setAdapterProperties(props);
052: config
053: .setCacheAdapter("com.jofti.cache.adapter.ObjectGridListenerAdapter");
054:
055: }
056:
057: public void setName(String name) {
058: config.setName(name);
059:
060: }
061:
062: public void setConfigFile(String fileName) {
063: this .configFile = fileName;
064: }
065:
066: public void setGridName(String name) {
067: this .gridName = name;
068: }
069:
070: public void setBackingMapName(String name) {
071: this .backingMapName = name;
072: }
073:
074: private synchronized void init(String objectGridName, String mapName)
075: throws JoftiException {
076:
077: if (!started) {
078: System.out.println("initialising");
079: ObjectGridManager oGridManager = ObjectGridManagerFactory
080: .getObjectGridManager();
081: ObjectGrid grid = oGridManager
082: .getObjectGrid(objectGridName);
083:
084: if (grid == null) {
085: throw new JoftiException("Unable to find grid:"
086: + objectGridName);
087: }
088: BackingMap map = grid.getMap(mapName);
089:
090: if (map == null) {
091: throw new JoftiException("Unable to find map:"
092: + mapName);
093: }
094: if (config.getName() == null) {
095: throw new JoftiException("Index name cannot be null");
096: }
097: ObjectGridListenerAdapter adapter = null;
098: if (configFile != null) {
099: adapter = (ObjectGridListenerAdapter) manager.addIndex(
100: config, map, configFile);
101: } else {
102: adapter = (ObjectGridListenerAdapter) manager.addIndex(
103: config, map);
104: }
105:
106: listener = adapter.getEventListener();
107: started = true;
108: }
109: }
110:
111: /* (non-Javadoc)
112: * @see com.ibm.websphere.objectgrid.plugins.index.MapIndexPlugin#doBatchUpdate(com.ibm.websphere.objectgrid.TxID, com.ibm.websphere.objectgrid.plugins.LogSequence)
113: */
114: public void doBatchUpdate(TxID txid, LogSequence logsequence)
115: throws ObjectGridRuntimeException {
116:
117: try {
118: init(logsequence.getObjectGridName(), logsequence
119: .getMapName());
120: } catch (JoftiException e) {
121: throw new ObjectGridRuntimeException(e);
122: }
123: listener.doBatchUpdate(txid, logsequence);
124:
125: }
126:
127: /* (non-Javadoc)
128: * @see com.ibm.websphere.objectgrid.plugins.index.MapIndexPlugin#getIndexProxy(com.ibm.websphere.objectgrid.plugins.index.MapIndexInfo)
129: */
130: public Object getIndexProxy(MapIndexInfo mapindexinfo) {
131:
132: if (!started && (gridName == null || backingMapName == null)) {
133: throw new RuntimeException(
134: "gridname and backingMap name must be set in Plug-In config");
135: }
136:
137: try {
138: init(gridName, backingMapName);
139: } catch (JoftiException e) {
140: throw new ObjectGridRuntimeException(e);
141: }
142: return listener.getIndexProxy(mapindexinfo);
143: }
144:
145: public String getName() {
146: return config.getName();
147: }
148:
149: public void setAttributeName(String s) {
150: // we do not care here
151: }
152:
153: public void undoBatchUpdate(TxID txid, LogSequence logsequence)
154: throws ObjectGridException {
155: listener.undoBatchUpdate(txid, logsequence);
156:
157: }
158:
159: }
|