Skip to Content.
Sympa Menu

perfsonar-dev - [pS-dev] [GEANT/SA2/ps-java-services] r5821 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType

Subject: perfsonar development work

List archive

[pS-dev] [GEANT/SA2/ps-java-services] r5821 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType


Chronological Thread 
  • From:
  • To:
  • Subject: [pS-dev] [GEANT/SA2/ps-java-services] r5821 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType
  • Date: Fri, 29 Jul 2011 15:50:31 +0100

Author: psnc.pikusa
Date: 2011-07-29 15:50:31 +0100 (Fri, 29 Jul 2011)
New Revision: 5821

Added:

trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/MetadataKeyMessageHandler.java
Log:
added files from current rrd-ma

Added:
trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/MetadataKeyMessageHandler.java
===================================================================
---
trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/MetadataKeyMessageHandler.java
(rev 0)
+++
trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/MetadataKeyMessageHandler.java
2011-07-29 14:50:31 UTC (rev 5821)
@@ -0,0 +1,462 @@
+/**
+ * $Id: GenericMessageHandler.java 4932 2009-02-02 14:35:16Z mac $
+ * Project: perfSONAR
+ */
+
+package org.perfsonar.service.measurementArchive.rrdType;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.perfsonar.base2.service.authn.component.AuthNComponent;
+import org.perfsonar.base2.service.configuration.Configuration;
+import org.perfsonar.base2.service.configuration.ConfigurationManager;
+import org.perfsonar.base2.service.exceptions.PerfSONARException;
+import org.perfsonar.base2.service.messages.MessageHandler;
+import org.perfsonar.base2.service.requesthandler.ServiceEngine;
+import org.perfsonar.base2.service.requesthandler.ServiceMessage;
+import org.perfsonar.base2.service.util.ResultCodesUtil;
+import org.perfsonar.base2.xml.nmwg.Data;
+import org.perfsonar.base2.xml.nmwg.Message;
+import org.perfsonar.base2.xml.nmwg.Metadata;
+
+
+/**
+ * Abstract class that does all the things that a "request" message
+ * handler will need to do.
+ *
+ * @author Maciej Glowiak
+ * @author Roman Lapacz
+ * @author Jeff Boote
+ */
+public class MetadataKeyMessageHandler implements MessageHandler {
+
+
+ private static final Logger logger =
Logger.getLogger(MetadataKeyMessageHandler.class.getName());
+
+
+ public MetadataKeyMessageHandler() throws PerfSONARException {
+
+ }
+
+
+ protected ServiceEngine getServiceEngine(String serviceEngineClassName)
+ throws Exception {
+
+ ServiceEngine se = (ServiceEngine)Class.forName(
+ serviceEngineClassName ).newInstance();
+
+ return se;
+ }
+
+ protected void checkAuth(Configuration c, String messageType) throws
PerfSONARException {
+ try {
+ String authR =
c.getMessageHandler(messageType).getOption("authR").getValue();
+ logger.debug("Checking authorization: "+authR);
+ if ("yes".equals(authR)) {
+ AuthNComponent authComponent =
(AuthNComponent)c.getAuxiliaryComponent("auth");
+ authComponent.requestAuthR(messageType, null);
+ }
+ else {
+ String authN =
c.getMessageHandler(messageType).getOption("authN").getValue();
+ logger.debug("Checking authentication: "+authN);
+ if ("yes".equals(authN)) {
+ AuthNComponent authComponent =
(AuthNComponent)c.getAuxiliaryComponent("auth");
+ authComponent.requestAuthN(messageType);
+ }
+ }
+ } catch (RuntimeException ex) { }
+ }
+
+ public void execute(ServiceMessage request, ServiceMessage response)
+ throws PerfSONARException {
+
+ Message reqMessage = (Message) request.getElement();
+
+ logger.debug("Message Handler started for message type
["+reqMessage.getType()+"]");
+
//logger.debug("--------------------\n"+reqMessage+"\n------------------------\n");
+
+ //get Service engine for this message
+ String messageType = null;
+ String serviceEngineClassName = null;
+ ServiceEngine serviceEngine = null;
+ try {
+ messageType = reqMessage.getType();
+ Configuration c =
ConfigurationManager.getInstance().getConfiguration();
+ //authentication
+ checkAuth(c, messageType);
+ //service engine
+ serviceEngineClassName = c.getServiceEngineOption(messageType);
+ serviceEngine = getServiceEngine(serviceEngineClassName);
+ if (serviceEngine == null) throw new NullPointerException(
+ "Cannot Instantiate ServiceEngine class
["+serviceEngineClassName+"]");
+
+ } catch (Exception ex) {
+
+ String m = "Cannot find ServiceEngine ["+serviceEngineClassName +
+ "] for message type ["+messageType+"], nested exception
was:"+
+ ex.toString();
+ PerfSONARException pse = new PerfSONARException(
+ "no_service_engine", m);
+ logger.error(m,ex);
+
+ response.setElement(
+ ResultCodesUtil.generateResultMessage(null, pse));
+ return; //finish when cannot execute ServiceEngine
+
+ }
+
+
+ ArrayList<MessagePair> output = new ArrayList<MessagePair>();
+
+ List<Message> inputMessages=extractMultipleRequests(reqMessage);
+ if(inputMessages.isEmpty()){
+ //TODO: url event types
+ throw new
PerfSONARException("error.common.no_metadata_data_pair","There were no
metadata/data pairs found in the message");
+ }
+
+ for (Message inputMessage: inputMessages) {
+
+ ServiceMessage reqSM = new ServiceMessage(inputMessage);
+ ServiceMessage resSM = new ServiceMessage();
+
+ try {
+
+ logger.debug("Request.execute: run Service Engine for
extracted request");
+
+ //run service engine and add result to output list
+ try {
+
+ serviceEngine.takeAction(reqSM, resSM);
+
+ } catch (RuntimeException rex) {
+ //just in case of unexpected runtime exception such as
+ //null, indexoutofbounds, classcast, etc.
+ logger.error(rex,rex);
+ throw new PerfSONARException(
+ "runtime_exception", "Runtime exception in
ServiceEngine: "+rex.getMessage());
+ }
+ } catch (PerfSONARException e) {
+
+ //if an exception was thrown, convert it to result code
+ //and add as a new message (to be joined) to array
+ logger.error("Message handler result code: "+e);
+
resSM.setElement(ResultCodesUtil.generateResultMessage(e.getResultCode(),e.getMessage()));
+
+ }
+
+ //add response from ServiceEngine or Result code generated above
+ output.add(new MessagePair(reqSM, resSM));
+
+ }
+
+ MessagePair messagePair = mergeMultipleResponses(output);
+ response.setElement(messagePair.getResponse().getElement());
+ response.setText(messagePair.getResponse().getText());
+
+ Message respMessage = (Message) response.getElement();
+ if (respMessage != null) {
+ respMessage.setType(messageType.replaceFirst("Request",
"Response"));
+ respMessage.setMessageIdRef(reqMessage.getId());
+ }
+
+ }
+
+
+ protected ArrayList<Message> extractMultipleRequests(Message reqMessage)
{
+
+ ArrayList<Message> messages = new ArrayList<Message>();
+ Collection<Data> dataTriggers = reqMessage.getDataMap().values();
+
+ for (Data data : dataTriggers) {
+
+ String metadataIdRef = data.getMetadataIdRef();
+ Message message = new Message();
+ try {
+
+ Map<String,Metadata> metadatas = new
HashMap<String,Metadata>();
+ getChaining(reqMessage, metadataIdRef, metadatas);
+ Map<String,Metadata> mergedMetadatas =
mergeAllMetadata(metadatas);
+
+
+ for (Metadata metadata : mergedMetadatas.values())
+ message.setMetadata(metadata);
+
+ message.setId(reqMessage.getId());
+ message.setType(reqMessage.getType());
+ message.setData(data);
+
+ messages.add(message);
+
+ } catch (NoSuchMetadataException e) {
+
+ //wrong MetadataIdRef, remove it
+ //TODO: ?
+ e.printStackTrace();
+
+ }
+
+ }
+
+ return messages;
+
+ }
+
+
+ public class MessagePair {
+ private ServiceMessage request = null;
+ private ServiceMessage response = null;
+ public MessagePair(ServiceMessage request, ServiceMessage response) {
+ this.request = request;
+ this.response = response;
+ }
+ public void setRequest(ServiceMessage request) { this.request =
request; }
+ public void setResponse(ServiceMessage response) { this.response =
response; }
+ public ServiceMessage getRequest() { return request; }
+ public ServiceMessage getResponse() { return response; }
+ }
+
+
+ protected MessagePair mergeMultipleResponses(ArrayList<MessagePair>
messagePairs){
+
+ final Map<String, String> ids = new Hashtable<String, String>();
+
+ if (messagePairs == null || messagePairs.size() == 0)
+ return null;
+
+
+
+
+ /*
+ * BUG 508 fixing begin
+ */
+ else if (messagePairs.size() == 1 &&
messagePairs.get(0).getResponse().getText() != null &&
messagePairs.get(0).getResponse().getElement() == null){
+
+ StringBuffer responseText = new StringBuffer();
+ String t =
messagePairs.get(0).getRequest().getElement().getAttribute("type");
+ String type = (t == null? "": t.replaceFirst("Request",
"Response"));
+ responseText.append("<nmwg:message type=\"" + type + "\"
xmlns:nmwg=\"http://ggf.org/ns/nmwg/base/2.0/\";
xmlns=\"http://ggf.org/ns/nmwg/base/2.0/\";
xmlns:nmwgt=\"http://ggf.org/ns/nmwg/topology/2.0/\";>");
+
responseText.append(messagePairs.get(0).getResponse().getText());
+ responseText.append("</nmwg:message>");
+
messagePairs.get(0).getResponse().setText(responseText.toString());
+ return messagePairs.get(0);
+ /*
+ * BUG 508 fixing end
+ */
+
+ }else if(messagePairs.size() == 1)
+ return messagePairs.get(0);
+
+ else {
+
+ int metaid=0;
+ int dataid=0;
+
+ //create new common response message
+ Message respMessage = new Message();
+
+
+
+ /*
+ * Bug 508 fixing - BEGIN
+ */
+ //Prepare Message Text
+ StringBuffer responseText = new StringBuffer();
+ String t =
messagePairs.get(0).getRequest().getElement().getAttribute("type");
+ String type = (t == null? "": t.replaceFirst("Request",
"Response"));
+ responseText.append("<nmwg:message type=\"" + type + "\"
xmlns:nmwg=\"http://ggf.org/ns/nmwg/base/2.0/\";
xmlns=\"http://ggf.org/ns/nmwg/base/2.0/\";
xmlns:nmwgt=\"http://ggf.org/ns/nmwg/topology/2.0/\";>");
+
+ //Concatenate Messages
+ int messageCounter = 0;
+ for (MessagePair messagePair : messagePairs) {
+
+ if(messagePair.getResponse().getText() != null &&
messagePair.getResponse().getElement() == null){
+ //If it's true it means that on message pairs list
there is no response which has element object so responses can be concatenated
+
+
responseText.append(messagePair.getResponse().getText());
+ messageCounter++;
+
+ }
+ else{
+ break;
+ }
+ }
+ responseText.append("</nmwg:message>");
+
+ //If it's true it means that on message pairs list there is no
response which has element object so responses can be concatenated
+ if(messagePairs.size() == messageCounter){
+
messagePairs.get(0).getResponse().setText(responseText.toString());
+ return messagePairs.get(0);
+
+ }
+ /*
+ * Bug 508 fixing - END
+ */
+
+
+ for (MessagePair messagePair : messagePairs) {
+
+ //ServiceMessage request = messagePair.getRequest();
+ ServiceMessage response = messagePair.getResponse();
+ Message m = (Message) response.getElement();
+
+
+
+ if (m == null) return messagePair;
+
+ Collection<Metadata> metadatas = m.getMetadataMap().values();
+ Collection<Data> datas = m.getDataMap().values();
+
+ //for each metadata
+ for (Metadata meta1 : metadatas) {
+
+ //get old meta id
+ String meta1id = meta1.getId();
+ //compute new meta id
+ String newMeta1id = meta1id+"_"+metaid;
+
+ //set new id
+ meta1.setId(newMeta1id);
+
+ metaid++;
+
+ ids.put(meta1id, newMeta1id);
+
+ /*
+ * TODO:
+ * subject chaining reference
+ * if there is metadata/subject with
+ * metadataRefId, it should be changed
+ *
+ * perhaps some metadata-id mapping table
+ * should be prepared and then all metadata
+ * should be iterated and subjects changed?
+ *
+ * something like:
+ *
+ * mapping.add(oldId, newId);
+ *
+ */
+ if ((meta1.getSubject() != null)
+ && (meta1.getSubject().getMetadataIdRef() !=
null)) {
+ if (ids.get(meta1.getSubject().getMetadataIdRef()) !=
null)
+
meta1.getSubject().setMetadataIdRef(ids.get(meta1.getSubject().getMetadataIdRef()));
+ }
+
+ //put metadata to new common response message
+ respMessage.setMetadata(meta1);
+
+ //find all data linked to meta
+ for (Data data1 : datas) {
+
+ //if data's metadata id res == metadata id
+ //put data
+ if (data1.getMetadataIdRef().equals(meta1id)) {
+
+ data1.setId(data1.getId() + "_"+dataid);
+ //change data metadata id ref to new meta id
+ data1.setMetadataIdRef(newMeta1id);
+
+ //put changed data to response common message
+ respMessage.setData(data1);
+
+ dataid++;
+
+ } //if
+ } //datas
+ } //metadata
+
+ }
+
+ return new MessagePair(null, new ServiceMessage(respMessage));
+
+ }
+ }
+
+
+ /**
+ * Find metadata chain for metadata ID (the last metadata in chain)
+ *
+ * @param inputMessage message to process (won't be changed)
+ * @param lastMetadataId id of metadata which is last in chain
+ * @return map <id,metadata>
+ * @throws NoSuchMetadataException thrown if there is no metadata with
given ID
+ */
+ protected Map<String,Metadata> getChaining(
+ Message inputMessage, String metadataId, Map<String,Metadata>
metadatas)
+ throws NoSuchMetadataException {
+
+ if (metadataId == null) throw new NoSuchMetadataException();
+ Metadata metadata = inputMessage.getMetadata(metadataId);
+ if (metadata == null) throw new NoSuchMetadataException();
+
+ metadatas.put(metadata.getId(), metadata);
+
+ //metadataIdRef in Subject
+ //prevent endless loop
+ if (metadata.getSubject() != null) {
+
+ if ( (metadata.getSubject().getMetadataIdRef()!=null)&&
+ !("".equals(metadata.getSubject().getMetadataIdRef()))&&
+
!(metadata.getId().equals(metadata.getSubject().getMetadataIdRef()))) {
+
+ getChaining(
+ inputMessage,
+ metadata.getSubject().getMetadataIdRef(),
+ metadatas);
+ }
+
+ }
+
+ String chainedMetadataId = metadata.getMetadataIdRef();
+
+ //get other chain, prevent endless loop
+ if ( (chainedMetadataId!=null)&&
+ !("".equals(chainedMetadataId))&&
+ !(metadata.getId().equals(chainedMetadataId))) {
+ getChaining(inputMessage, chainedMetadataId, metadatas);
+ }
+
+
+ return metadatas;
+
+ }
+
+
+ /**
+ * Merge chained metadata elements
+ * @param metadatas map of metadata elements
+ * @return metadatas map of merged metadata elements
+ */
+ public Map<String,Metadata> mergeAllMetadata(Map<String,Metadata>
metadatas) {
+ return metadatas;
+ };
+
+
+ /**
+ * Exception thrown when Metadata could not be found. It's internal
+ * Message Handler
+ * exception.
+ *
+ * @author Maciej Glowiak
+ *
+ */
+ protected class NoSuchMetadataException extends Exception {
+
+ private static final long serialVersionUID = -3725261560519487460L;
+
+ }
+
+
+} //GenericMessageHandler
+
+
+
+
+
+



  • [pS-dev] [GEANT/SA2/ps-java-services] r5821 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType, svn-noreply, 07/29/2011

Archive powered by MHonArc 2.6.16.

Top of Page