package com.dish.cache; import java.io.File; import java.util.HashMap; import java.util.Map; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; import net.sf.ehcache.config.CacheConfiguration; import net.sf.ehcache.config.SearchAttribute; import net.sf.ehcache.config.Searchable; import net.sf.ehcache.store.MemoryStoreEvictionPolicy; import com.wm.app.b2b.client.Context; import com.wm.app.b2b.client.ServiceException; import com.wm.data.IData; import com.wm.data.IDataCursor; import com.wm.data.IDataFactory; import com.wm.data.IDataUtil; public class Cache { /** * @param args */ public static void main(String[] args) { IData pipeline=IDataFactory.create(); IData fileName=IDataFactory.create(); IData loadAs=IDataFactory.create(); IData result=IDataFactory.create(); IData body=IDataFactory.create(); IData string=IDataFactory.create(); IDataCursor fileNameCursor=null; IDataCursor pipeLineCursor=pipeline.getCursor(); IDataCursor loadAsCursor=loadAs.getCursor(); IDataCursor resultCursor=result.getCursor(); IDataCursor bodyCursor=null; IDataCursor stringCursor=null; Object keyObject=null; Context context=null; String server="some value"; String username="some value"; String password="some value"; String keyToCache=null; String dataToCache=null; String xmlFileData=null; String xmlFileName=null; CacheManager cacheManager=null; CacheConfiguration cacheConfig=null; Ehcache cacheObject=null; Element element=null; Searchable searchable=new Searchable(); SearchAttribute sa=null; File file=null; Map mapObject=null; try { try { context=new Context(); context.connect(server,username,password); } catch(Exception e) { e.printStackTrace(); throw new ServiceException("\n"+"Could not connect to the Server"); } file=new File("C:/Documents and Settings/Desktop/ehcache.xml"); try { keyToCache=IDataUtil.getString(pipeLineCursor,"KeyToCache"); dataToCache=IDataUtil.getString(pipeLineCursor,"DataToCache"); } catch(Exception e) { e.printStackTrace(); throw new ServiceException("\n"+"Could not retrieve data from pipeline"); } element=new Element(keyToCache,dataToCache); mapObject=new HashMap(); xmlFileName=file.getAbsolutePath(); cacheManager=new CacheManager(xmlFileName); cacheConfig=new CacheConfiguration("cache",300).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) .overflowToDisk(false).eternal(false).timeToLiveSeconds(60).timeToIdleSeconds(30) .diskPersistent(false).diskExpiryThreadIntervalSeconds(0); cacheObject=new net.sf.ehcache.Cache(cacheConfig); cacheManager.addCache(cacheObject); cacheObject=cacheManager.getEhcache("cache"); cacheObject.put(element); for(Object key:cacheObject.getKeys()) { mapObject.put((String) key, (Object) cacheObject.get(key).getObjectValue()); keyObject=key; sa=new SearchAttribute(); sa.setName((String) keyObject); sa.setExpression((String) cacheObject.get(keyObject).getObjectValue()); cacheConfig.addSearchable(searchable); searchable.addSearchAttribute(sa); } fileNameCursor=fileName.getCursor(); bodyCursor=body.getCursor(); stringCursor=string.getCursor(); //fileNameCursor.insertAfter("filename",xmlFileName); //fileNameCursor.insertDataAfter(xmlFileName); fileNameCursor.setValue(((Object)xmlFileName)); String s="string"; loadAsCursor.setValue(((Object) s)); System.out.println(fileName); try { context.invoke("pub.file","getFile", loadAs); result=context.invoke("pub.file","getFile",fileName); } catch(Exception e) { e.printStackTrace(); throw new ServiceException("\n"+"Could not invoke the file"); } try { if(resultCursor.first("body")) { body=IDataUtil.getIData(resultCursor,"body"); bodyCursor=body.getCursor(); if(bodyCursor.first("string")) { xmlFileData=IDataUtil.getString(bodyCursor,"string"); /*string=IDataUtil.getIData(bodyCursor,"string"); stringCursor=string.getCursor(); xmlFileData=(String) stringCursor.getValue();*/ } } } catch(Exception e) { e.printStackTrace(); throw new ServiceException("\n"+"Could not retrieve data from the file"); } } catch(Exception e) { e.printStackTrace(); } finally { IData key=null; IData value=null; IDataCursor keyCursor=null; IDataCursor valueCursor=null; try { IDataUtil.put(pipeLineCursor, "XMLData", (Object) xmlFileData); key=IDataFactory.create(); value=IDataFactory.create(); keyCursor=key.getCursor(); valueCursor=value.getCursor(); for(int i=0;i