aslcore/src/org/gcube/application/framework/core/cache/factories/SearchConfigCacheEntryFacto...

76 lines
3.0 KiB
Java

package org.gcube.application.framework.core.cache.factories;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.axis.message.addressing.Address;
import org.apache.axis.message.addressing.EndpointReference;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.gcube.application.framework.core.cache.RIsManager;
import org.gcube.application.framework.core.security.ServiceContextManager;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.informationsystem.cache.SrvType;
import org.gcube.searchservice.searchmasterservice.stubs.SearchMasterPortType;
import org.gcube.searchservice.searchmasterservice.stubs.service.SearchMasterServiceAddressingLocator;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
/**
* @author Valia Tsagkalidou (NKUA)
*/
public class SearchConfigCacheEntryFactory implements CacheEntryFactory {
/**
* An atomic integer to get the Search EPRs round-robin
*/
protected static AtomicInteger SMid = new AtomicInteger();
/**
* @param key a String representing the DL name.
* @return a String - serialized the search configuration
*/
public Object createEntry(Object key) throws Exception {
EndpointReference[] searchMasterURIs = null;
System.out.println("GenericResource: inside search config cache create entry starting..");
for(int count =0; count < 5 ; count++)
{
searchMasterURIs = RIsManager.getInstance().getISCache(GCUBEScope.getScope(key.toString())).getEPRsFor("Search", "SearchMaster", SrvType.SIMPLE.name());
if(searchMasterURIs.length > 0)
break;
Thread.sleep(5000);
}
// Creating a client for Search Master.
EndpointReferenceType serviceEPR = new EndpointReferenceType();
String collections_xml = null;
System.out.println(searchMasterURIs.length + " search masters found on IS.");
for(int counter = 0; counter < searchMasterURIs.length; counter++)
{
try
{
String search = searchMasterURIs[SMid.getAndIncrement()%searchMasterURIs.length].getAddress().toString();
System.out.println("search Master URL: " + search);
serviceEPR.setAddress(new Address(search));
SearchMasterServiceAddressingLocator serviceLocator = new SearchMasterServiceAddressingLocator();
SearchMasterPortType searchMaster = serviceLocator.getSearchMasterPortTypePort(serviceEPR);
try {
searchMaster = ServiceContextManager.applySecurity(searchMaster, GCUBEScope.getScope(key.toString()), ApplicationCredentials.getInstance().getCredential(key.toString()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Retrieving dynamic configuration. E.g. which collections are
// currently available, if a Full text Index is available....
collections_xml = searchMaster.retrieveConfiguration("collections");
System.out.println("Search Configuration!!!" + collections_xml);
break;
}
catch (Exception e) {
e.printStackTrace();
}
}
return collections_xml;
}
}