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

86 lines
3.3 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.EndpointReferenceType;
import org.gcube.application.framework.core.cache.HarvestersManager;
import org.gcube.application.framework.core.security.PortalSecurityManager;
import org.gcube.application.framework.core.util.CacheEntryConstants;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.security.GCUBESecurityManager;
import org.gcube.searchservice.searchlibrary.isharvester.ISInfo;
import org.gcube.searchservice.searchmasterservice.stubs.SearchMasterPortType;
import org.gcube.searchservice.searchmasterservice.stubs.service.SearchMasterServiceAddressingLocator;
import org.gridforum.jgss.ExtendedGSSCredential;
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 {
ISInfo disInfo = HarvestersManager.getInstance().getISInfo(GCUBEScope.getScope(key.toString()));
String[] searchMasterURIs = null;
for(int count =0; count < 5 ; count++)
{
searchMasterURIs = disInfo.getEndPointForMasterService();
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
{
serviceEPR.setAddress(new Address(searchMasterURIs[SMid.getAndIncrement()%searchMasterURIs.length]));
SearchMasterServiceAddressingLocator serviceLocator = new SearchMasterServiceAddressingLocator();
SearchMasterPortType searchMaster = serviceLocator.getSearchMasterPortTypePort(serviceEPR);
GCUBESecurityManager secManager = new PortalSecurityManager(GCUBEScope.getScope(key.toString()));
if(secManager.isSecurityEnabled())
{
try {
secManager.useCredentials(ApplicationCredentials.getInstance().getCredential(key.toString()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
searchMaster = GCUBERemotePortTypeContext.getProxy(searchMaster, GCUBEScope.getScope(key.toString()), secManager);
} 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");
break;
}
catch (Exception e) {
e.printStackTrace();
}
}
return collections_xml;
}
}