KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > plugin > ObjectGridIndexPlugIn


1 package com.jofti.plugin;
2
3 import java.util.Properties JavaDoc;
4
5 import com.ibm.websphere.objectgrid.BackingMap;
6 import com.ibm.websphere.objectgrid.ObjectGrid;
7 import com.ibm.websphere.objectgrid.ObjectGridException;
8 import com.ibm.websphere.objectgrid.ObjectGridManager;
9 import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
10 import com.ibm.websphere.objectgrid.ObjectGridRuntimeException;
11 import com.ibm.websphere.objectgrid.TxID;
12 import com.ibm.websphere.objectgrid.plugins.LogSequence;
13 import com.ibm.websphere.objectgrid.plugins.index.MapIndexInfo;
14 import com.ibm.websphere.objectgrid.plugins.index.MapIndexPlugin;
15 import com.jofti.api.IndexManager;
16 import com.jofti.cache.adapter.ObjectGridListenerAdapter;
17 import com.jofti.cache.adapter.listener.ObjectGridEventListener;
18 import com.jofti.config.DefaultIndexConfig;
19 import com.jofti.exception.JoftiException;
20 import com.jofti.manager.IndexManagerImpl;
21
22 /**
23  * <p>
24  * Implementation of ObjectGrid's MapIndexPlugin. The implmentation configures itself lazily on either the first
25  * call to getIndexProxy or the first commit callback.
26  * </p>
27  * <p>
28  * Due to the information passed into the Plug-In it is also necessary to configure the GridName and the BackingMapName.
29  * </p>
30  * @author steve
31  * @version 1.1
32  * @since 1.2
33  *
34  */

35 public class ObjectGridIndexPlugIn implements MapIndexPlugin{
36
37     
38     IndexManager manager = new IndexManagerImpl();
39     DefaultIndexConfig config = new DefaultIndexConfig();
40     private boolean started = false;
41     private String JavaDoc configFile =null;
42     private String JavaDoc backingMapName =null;
43     private String JavaDoc gridName =null;
44     
45     ObjectGridEventListener listener =null;
46     
47     
48     public ObjectGridIndexPlugIn(){
49             
50         config = new DefaultIndexConfig();
51         Properties JavaDoc props = new Properties JavaDoc();
52         props.put("plugin", "true");
53         config.setAdapterProperties(props);
54         config.setCacheAdapter("com.jofti.cache.adapter.ObjectGridListenerAdapter");
55          
56     }
57     
58     public void setName(String JavaDoc name){
59         config.setName(name);
60     
61     }
62     
63     public void setConfigFile(String JavaDoc fileName){
64         this.configFile = fileName;
65     }
66     
67     public void setGridName(String JavaDoc name){
68         this.gridName =name;
69     }
70     
71     public void setBackingMapName(String JavaDoc name){
72         this.backingMapName = name;
73     }
74     private synchronized void init(String JavaDoc objectGridName, String JavaDoc mapName) throws JoftiException{
75         
76         if (!started){
77             System.out.println("initialising");
78             ObjectGridManager oGridManager=
79                  ObjectGridManagerFactory.getObjectGridManager();
80             ObjectGrid grid = oGridManager.getObjectGrid(objectGridName);
81             
82             if (grid == null){
83                 throw new JoftiException("Unable to find grid:"+objectGridName);
84             }
85             BackingMap map = grid.getMap(mapName);
86             
87             if (map ==null){
88                 throw new JoftiException("Unable to find map:"+mapName);
89             }
90             if (config.getName() == null){
91                 throw new JoftiException("Index name cannot be null");
92             }
93             ObjectGridListenerAdapter adapter =null;
94             if (configFile != null){
95                 adapter = (ObjectGridListenerAdapter) manager.addIndex(config,map,configFile );
96             }else{
97                 adapter = (ObjectGridListenerAdapter)manager.addIndex(config, map);
98             }
99             
100             listener = adapter.getEventListener();
101             started =true;
102         }
103     }
104     
105     /* (non-Javadoc)
106      * @see com.ibm.websphere.objectgrid.plugins.index.MapIndexPlugin#doBatchUpdate(com.ibm.websphere.objectgrid.TxID, com.ibm.websphere.objectgrid.plugins.LogSequence)
107      */

108     public void doBatchUpdate(TxID txid, LogSequence logsequence) throws ObjectGridRuntimeException {
109         
110         try {
111             init(logsequence.getObjectGridName(), logsequence.getMapName());
112         }catch (JoftiException e){
113             throw new ObjectGridRuntimeException(e);
114         }
115         listener.doBatchUpdate(txid, logsequence);
116         
117     }
118
119     /* (non-Javadoc)
120      * @see com.ibm.websphere.objectgrid.plugins.index.MapIndexPlugin#getIndexProxy(com.ibm.websphere.objectgrid.plugins.index.MapIndexInfo)
121      */

122     public Object JavaDoc getIndexProxy(MapIndexInfo mapindexinfo) {
123         
124         if (!started && (gridName == null || backingMapName ==null)){
125             throw new RuntimeException JavaDoc("gridname and backingMap name must be set in Plug-In config");
126         }
127         
128         try {
129             init(gridName, backingMapName);
130         }catch (JoftiException e){
131             throw new ObjectGridRuntimeException(e);
132         }
133         return listener.getIndexProxy(mapindexinfo);
134     }
135
136     public String JavaDoc getName() {
137         return config.getName();
138     }
139
140     public void setAttributeName(String JavaDoc s) {
141         // we do not care here
142
}
143
144     public void undoBatchUpdate(TxID txid, LogSequence logsequence) throws ObjectGridException {
145         listener.undoBatchUpdate(txid, logsequence);
146         
147     }
148
149 }
150
Popular Tags