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

83 lines
4.1 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.application.framework.core.util.CacheEntryConstants;
import org.gcube.application.framework.core.util.QueryString;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.informationsystem.cache.SrvType;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.CMSPortType1PortType;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.DocumentDescription;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.DocumentPropertyDescription;
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 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);
/** Object logger. */
protected final GCUBELog logger = new GCUBELog(this);
/**
* @param key a QueryString representing pairs of keys and values: needed keys are "vre" and "oid"
* @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.vre) && query.containsKey(CacheEntryConstants.oid))
{
EndpointReference[] cmsURIs = RIsManager.getInstance().getISCache(GCUBEScope.getScope(query.get(CacheEntryConstants.vre))).getEPRsFor("ContentManagement","ContentManagementService",SrvType.SIMPLE.name());
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].getAddress().toString()));
CMSPortType1ServiceAddressingLocator serviceLocator = new CMSPortType1ServiceAddressingLocator();
CMSPortType1PortType cms = serviceLocator.getCMSPortType1PortTypePort(serviceEPR);
cms = (CMSPortType1PortType) ServiceContextManager.applySecurity(cms, GCUBEScope.getScope(query.get(CacheEntryConstants.vre)), ApplicationCredentials.getInstance().getCredential(query.get(CacheEntryConstants.vre)));
docDescription = cms.getDocument(docParams);
long size = cms.getContentLength(query.get(CacheEntryConstants.oid));
DocumentPropertyDescription[] properties = docDescription.getProperties();
DocumentPropertyDescription[] newProperties = new DocumentPropertyDescription[properties.length + 1];
for(int j=0; j < properties.length; j++)
{
newProperties[j] = properties[j];
}
logger.debug("Size of file:" + size);
newProperties[properties.length] = new DocumentPropertyDescription("LengthOfRawContent", query.get(CacheEntryConstants.oid),"","" + size);
docDescription.setProperties(newProperties);
break;
}
catch (Exception e) {
logger.error("",e);
}
}
}
return docDescription;
}
}