/*******************************************************************************
* Copyright 2007 Neptuny s.r.l. - www.neptuny.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.neptuny.xgrapher.cli.loader;
import java.io.StringWriter;
import java.util.List;
import javax.xml.bind.JAXBException;
import com.neptuny.xgrapher.gen.model.Edge;
import com.neptuny.xgrapher.gen.model.Graph;
import com.neptuny.xgrapher.gen.model.MetricAttribute;
import com.neptuny.xgrapher.gen.model.Node;
import com.neptuny.xgrapher.gen.model.ObjectFactory;
/**
*
* @author battlehorse
*/
public class DefaultLoader implements XGrapherLoader {
public String getGraphFromIdnodeAsString(String nodeId) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Graph getGraphFromIdnode(String nodeId) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Graph getStartGraph() {
return getJaxbGraph();
}
public String getStartGraphAsString() {
try {
javax.xml.bind.JAXBContext context = null;
try {
context = javax.xml.bind.JAXBContext.newInstance("com.neptuny.XGrapher.gen.model");
} catch (javax.xml.bind.JAXBException e) {
e.printStackTrace();
}
com.neptuny.xgrapher.gen.model.Graph g = getJaxbGraph();
javax.xml.bind.Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_SCHEMA_LOCATION, "http://XGrapher.sourceforge.net http://XGrapher.sourceforge.net");
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, java.lang.Boolean.TRUE);
StringWriter sw = new StringWriter();
marshaller.marshal(g, sw);
return sw.toString() ;
} catch (JAXBException ex) {
ex.printStackTrace();
return null ;
}
}
private Graph getJaxbGraph() {
ObjectFactory factory=new ObjectFactory();
Graph gra=factory.createGraph();
List nodi=gra.getNode();
List archi=gra.getEdge();
Node nodo1=factory.createNode();
nodo1.setId("31457");
nodo1.setName("CPU_UTIL");
nodo1.setDeepness(0);
//creo la lista di metricattribute
List metrat=nodo1.getMetricAttribute();
MetricAttribute metr=factory.createMetricAttribute();
metr.setName("SIZE");
metr.setValue(1.0);
metrat.add(metr);
//aggiungo il nodo alla lista di nodi del grafo
nodi.add(nodo1);
//creo secondo nodo
Node nodo2=factory.createNode();
nodo2.setId("31616");
nodo2.setName("CPU_UTIL");
nodo2.setDeepness(0);
//creo la lista di metricattribute del 2� nodo
metrat=nodo2.getMetricAttribute();
metr=factory.createMetricAttribute();
metr.setName("SIZE");
metr.setValue(0.1);
metrat=nodo2.getMetricAttribute();
metrat.add(metr);
//aggiungo il 2� nodo
nodi.add(nodo2);
Node nodo3=factory.createNode();
nodo3.setId("31616b");
nodo3.setName("CPU_UTIL");
nodo3.setDeepness(0);
//creo la lista di metricattribute del 2� nodo
metrat=nodo3.getMetricAttribute();
metr=factory.createMetricAttribute();
metr.setName("SIZE");
metr.setValue(0.5);
metrat=nodo3.getMetricAttribute();
metrat.add(metr);
//aggiungo il 2 nodo
nodi.add(nodo3);
//creo l'arco
Edge edg1=factory.createEdge();
edg1.setId("31457-31616");
edg1.setName("CPU_UTIL-CPU_UTIL");
edg1.setDeepness(0);
edg1.setNodeFromId("31457");
edg1.setNodeToId("31616");
//creo lista metricattribute per arco
metrat=edg1.getMetricAttribute();
metr=factory.createMetricAttribute();
metr.setName("SIZE");
metr.setValue(0.9);
metrat.add(metr);
//aggiungo arco
archi.add(edg1);
Edge edg2=factory.createEdge();
edg2.setId("31457-31616b");
edg2.setDeepness(0);
edg2.setNodeFromId("31457");
edg2.setNodeToId("31616b");
//creo lista metricattribute per arco
metrat=edg2.getMetricAttribute();
metr=factory.createMetricAttribute();
metr.setName("SIZE");
metr.setValue(0.2);
metrat.add(metr);
//aggiungo arco
archi.add(edg2);
return gra;
}
}