Adding MetadataRecord and MetadataWriter to the stubs library

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Collector@32417 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2010-11-30 00:21:27 +00:00
parent 08d36da4f8
commit 68e19b3670
5 changed files with 228 additions and 135 deletions

View File

@ -1,103 +0,0 @@
package org.gcube.informationsystem.collector.impl.resources;
import java.util.Calendar;
/**
*
* Metadata Record for gCube Profiles and Instance States
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class MetadataRecord {
private Calendar getTerminationTime;
private String source, key, groupKey, entryKey, type;
/**
* @return the getTerminationTime
*/
public Calendar getGetTerminationTime() {
return getTerminationTime;
}
/**
* @param getTerminationTime the getTerminationTime to set
*/
public void setGetTerminationTime(Calendar getTerminationTime) {
this.getTerminationTime = getTerminationTime;
}
/**
* @return the source
*/
public String getSource() {
return source;
}
/**
* @param source the source to set
*/
public void setSource(String source) {
this.source = source;
}
/**
* @return the key
*/
public String getKey() {
return key;
}
/**
* @param key the key to set
*/
public void setKey(String key) {
this.key = key;
}
/**
* @return the groupKey
*/
public String getGroupKey() {
return groupKey;
}
/**
* @param groupKey the groupKey to set
*/
public void setGroupKey(String groupKey) {
this.groupKey = groupKey;
}
/**
* @return the entryKey
*/
public String getEntryKey() {
return entryKey;
}
/**
* @param entryKey the entryKey to set
*/
public void setEntryKey(String entryKey) {
this.entryKey = entryKey;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
}

View File

@ -31,7 +31,7 @@ public class MetadataReader {
public Calendar getTerminationTime() {
//value is in seconds
String value = this.metadata.getElementsByTagName("TerminationTime").item(0).getTextContent();
String value = this.metadata.getElementsByTagName("TimeToLive").item(0).getTextContent();
Calendar now = new GregorianCalendar();
now.setTimeZone(TimeZone.getTimeZone("GMT"));
//add seconds to obtain the effective termination time

View File

@ -0,0 +1,180 @@
package org.gcube.informationsystem.collector.stubs.metadata;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import com.sun.xml.bind.StringInputStream;
/**
*
* Metadata Record for gCube Profiles and Instance States
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class MetadataRecord {
public static enum TYPE {INSTANCESTATE("InstanceState"),GCUBERESOURCE("Profile");
String name;
TYPE(String name) {this.name = name;}
public String toString() {return this.name;}
};
private Calendar terminationTime;
private Integer time;
private String source, key, groupKey, entryKey;
private TYPE type;
protected MetadataRecord() {}
/**
* @return the getTerminationTime
*/
public Calendar getGetTerminationTime() {
return terminationTime;
}
/**
* @param time this amount of time (in seconds) will be added to the current time to determine the
* resource's termination time
*/
public void setTimeToLive(Integer time) {
terminationTime = new GregorianCalendar();
terminationTime.setTimeZone(TimeZone.getTimeZone("GMT"));
//add seconds to obtain the effective termination time
terminationTime.add(Calendar.SECOND, time);
this.time = time;
}
/**
* @return the source
*/
public String getSource() {
return source;
}
/**
* @param source the source to set
*/
public void setSource(String source) {
this.source = source;
}
/**
* @return the key
*/
public String getKey() {
return key;
}
/**
* @param key the key to set
*/
public void setKey(String key) {
this.key = key;
}
/**
* @return the groupKey
*/
public String getGroupKey() {
return groupKey;
}
/**
* @param groupKey the groupKey to set
*/
public void setGroupKey(String groupKey) {
this.groupKey = groupKey;
}
/**
* @return the entryKey
*/
public String getEntryKey() {
return entryKey;
}
/**
* @param entryKey the entryKey to set
*/
public void setEntryKey(String entryKey) {
this.entryKey = entryKey;
}
/**
* @return the type
*/
public TYPE getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(TYPE type) {
this.type = type;
}
/**
*
* @return the time to live in seconds
*/
public Integer getTimeToLive() {
return this.time;
}
/**
* Gets a {@link Document} representation of the record
* @return a document object representing the record as XML document
* @throws SAXException
* @throws IOException
* @throws ParserConfigurationException
*/
public Document getAsDocument()
throws SAXException, IOException, ParserConfigurationException {
return DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new StringInputStream(getAsBuilder().toString()));
}
/**
* {@inheritDoc}
*/
public String toString() {
return getAsBuilder().toString();
}
/**
*
* @return a {@link StringBuilder} holding the string serialization of the record
*/
private StringBuilder getAsBuilder() {
StringBuilder builder = new StringBuilder();
builder.append("<Metadata>");
builder.append("<Type>").append(this.getType().toString()).append("</Type>");
builder.append("<Source>").append(this.getSource()).append("</Source>");
builder.append("<TimeToLive>").append(this.getTimeToLive()).append("</TimeToLive>");
builder.append("<GroupKey>").append(this.getGroupKey()).append("</GroupKey>");
builder.append("<EntryKey>").append(this.getEntryKey()).append("</EntryKey>");
builder.append("<Key>").append(this.getKey()).append("</Key>");
builder.append("</Metadata>");
return builder;
}
}

View File

@ -0,0 +1,41 @@
package org.gcube.informationsystem.collector.stubs.metadata;
import org.gcube.informationsystem.collector.stubs.metadata.MetadataRecord.TYPE;
import org.gcube.informationsystem.collector.stubs.metadata.MetadataRecord;
/**
*
* A metadata writer for IC records
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class MetadataWriter {
private MetadataRecord metadata;
/**
* Creates a new writer
* @param type
* @param source
* @param timeToLive lifetime in seconds
* @param groupkey
* @param key
* @param entrykey
*/
public MetadataWriter(TYPE type, String source,
Integer timeToLive, String groupkey, String key, String entrykey) {
this.metadata = new MetadataRecord();
this.metadata.setType(type);
this.metadata.setSource(source);
this.metadata.setEntryKey(entrykey);
this.metadata.setGroupKey(groupkey);
this.metadata.setTimeToLive(timeToLive);
}
public MetadataRecord getRecord() {
return this.metadata;
}
}

View File

@ -1,17 +1,16 @@
package org.gcube.informationsystem.collector.stubs.testsuite.wsdaix;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.axis.message.MessageElement;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
import org.gcube.informationsystem.collector.stubs.metadata.MetadataWriter;
import org.gcube.informationsystem.collector.stubs.metadata.MetadataRecord.TYPE;
import org.gcube.informationsystem.collector.stubs.wsdai.DataResourceUnavailableFaultType;
import org.gcube.informationsystem.collector.stubs.wsdai.InvalidResourceNameFaultType;
import org.gcube.informationsystem.collector.stubs.wsdai.NotAuthorizedFaultType;
@ -24,9 +23,6 @@ import org.gcube.informationsystem.collector.stubs.wsdaix.XMLCollectionAccessPT;
import org.gcube.informationsystem.collector.stubs.wsdaix.XMLWrapperType;
import org.gcube.informationsystem.collector.stubs.wsdaix.service.WsdaixServiceAddressingLocator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import com.sun.xml.bind.StringInputStream;
/**
*
@ -100,9 +96,10 @@ public class AddDocumentsTester {
XMLWrapperType wrapper = new XMLWrapperType();
MessageElement msgElement = new MessageElement(documents[i].getDocumentElement());
MessageElement msgElement2;
MetadataWriter writer = new MetadataWriter(TYPE.INSTANCESTATE, "http://source",
600, "MyGroupKey", "MyKey", "MyEntryKey");
try {
msgElement2 = new MessageElement(createMetadata(type, "http://source",
600, "MyGroupKey", "MyKey", "MyEntryKey").getDocumentElement());
msgElement2 = new MessageElement(writer.getRecord().getAsDocument().getDocumentElement());
} catch (Exception e) {
logger.error("Unable to add document " + documentNames[i], e);
continue;
@ -123,27 +120,5 @@ public class AddDocumentsTester {
return stubs.addDocuments(request);
}
private static Document createMetadata(String type, String source,
Integer tt, String groupkey, String key, String entrykey)
throws SAXException, IOException, ParserConfigurationException {
StringBuilder builder = new StringBuilder();
builder.append("<Metadata>");
builder.append("<Type>").append(type).append("</Type>");
builder.append("<Source>").append(source).append("</Source>");
builder.append("<TerminationTime>").append(String.valueOf(tt)).append("</TerminationTime>");
builder.append("<GroupKey>").append(groupkey).append("</GroupKey>");
builder.append("<EntryKey>").append(entrykey).append("</EntryKey>");
builder.append("<Key>").append(key).append("</Key>");
builder.append("</Metadata>");
System.out.println(builder.toString());
return DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new StringInputStream(builder.toString()));
}
}