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

84 lines
3.7 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.application.framework.core.util.QueryString;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.security.GCUBESecurityManager;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.CMSPortType1PortType;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.DocumentDescription;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.GetDocumentParameters;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.service.CMSPortType1ServiceAddressingLocator;
import org.gcube.contentmanagement.layerindependent.descriptions.BasicInfoObjectDescription;
import org.gridforum.jgss.ExtendedGSSCredential;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
/**
* @author Valia Tsagkalidou (NKUA)
*
*/
public class ContentInfoCacheEntryFactory implements CacheEntryFactory {
/**
* An atomic integer to get the CMS EPRs round-robin
*/
protected static AtomicInteger cmsId = new AtomicInteger(0);
/**
* @param key a QueryString representing pairs of keys and values.
* @return a DocumentDescription as it is returned from the CMS. The content is not transfered for storage and efficiency reasons
*/
public Object createEntry(Object key) throws Exception {
QueryString query = (QueryString) key;
DocumentDescription docDescription = null;
if(query.containsKey(CacheEntryConstants.dl) && query.containsKey(CacheEntryConstants.oid))
{
String[] cmsURIs = HarvestersManager.getInstance().getISInfo(GCUBEScope.getScope(query.get(CacheEntryConstants.dl))).getEndPointForCMSService("");
EndpointReferenceType serviceEPR = new EndpointReferenceType();
GetDocumentParameters docParams = new GetDocumentParameters();
docParams.setDocumentID(query.get(CacheEntryConstants.oid));
docParams.setTargetFileLocation(BasicInfoObjectDescription.RAW_CONTENT_DO_NOT_TRANSFER);
docParams.setUnrollLevels(3);
for(int i=0; i< cmsURIs.length; i++)
{
try
{
serviceEPR.setAddress(new Address(cmsURIs[cmsId.getAndIncrement()%cmsURIs.length]));
CMSPortType1ServiceAddressingLocator serviceLocator = new CMSPortType1ServiceAddressingLocator();
CMSPortType1PortType cms = serviceLocator.getCMSPortType1PortTypePort(serviceEPR);
GCUBESecurityManager secManager = new PortalSecurityManager(GCUBEScope.getScope(query.get(CacheEntryConstants.dl)));
if(secManager.isSecurityEnabled())
{
try {
ExtendedGSSCredential cred = ApplicationCredentials.getInstance().getCredential(query.get(CacheEntryConstants.dl));
secManager.useCredentials(cred);
} catch (Exception e) {
e.printStackTrace();
}
}
cms = GCUBERemotePortTypeContext.getProxy(cms, GCUBEScope.getScope(query.get(CacheEntryConstants.dl)), secManager);
// Retrieving dynamic configuration. E.g. which collections are
// currently available, if a Full text Index is available....
docDescription = cms.getDocument(docParams);
break;
}
catch (Exception e) {
e.printStackTrace();
}
}
}
return docDescription;
}
}