incubator-kato-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spo...@apache.org
Subject svn commit: r784667 [2/2] - in /incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti: ./ src/main/java/org/apache/kato/jvmti/javaruntime/ src/main/java/org/apache/kato/jvmti/javaruntime/model/ src/main/java/org/apache/kato/...
Date Mon, 15 Jun 2009 06:26:50 GMT
Added: incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/main/java/org/apache/kato/jvmti/reader/XMLReader.java
URL: http://svn.apache.org/viewvc/incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/main/java/org/apache/kato/jvmti/reader/XMLReader.java?rev=784667&view=auto
==============================================================================
--- incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/main/java/org/apache/kato/jvmti/reader/XMLReader.java
(added)
+++ incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/main/java/org/apache/kato/jvmti/reader/XMLReader.java
Mon Jun 15 06:26:49 2009
@@ -0,0 +1,646 @@
+package org.apache.kato.jvmti.reader;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Stack;
+import java.util.Vector;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.kato.image.CorruptDataException;
+import org.apache.kato.jvmti.javaruntime.model.JClass;
+import org.apache.kato.jvmti.javaruntime.model.JClassLoader;
+import org.apache.kato.jvmti.javaruntime.model.JField;
+import org.apache.kato.jvmti.javaruntime.model.JLocalVariable;
+import org.apache.kato.jvmti.javaruntime.model.JLocation;
+import org.apache.kato.jvmti.javaruntime.model.JMethod;
+import org.apache.kato.jvmti.javaruntime.model.JMonitor;
+import org.apache.kato.jvmti.javaruntime.model.JObject;
+import org.apache.kato.jvmti.javaruntime.model.JStackFrame;
+import org.apache.kato.jvmti.javaruntime.model.JThread;
+import org.apache.kato.jvmti.javaruntime.model.JValueHolder;
+import org.apache.kato.jvmti.javaruntime.model.Model;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+public class XMLReader {
+
+	private Model model=new Model();
+	
+	private final class KatoFormHandler extends DefaultHandler {
+
+		private final class FieldValue implements JValueHolder {
+			public Object value;
+			public int id;
+
+			public FieldValue(int id) {
+				this.id=id;
+			}
+
+			@Override
+			public void setValue(Object o) {
+				this.value=o;
+				
+			}
+		}
+		private String pad() {
+			StringBuilder sb=new StringBuilder();
+			
+			for(int i=0;i<depth;i++) {
+				sb.append(' ');
+			}
+			return new String(sb);
+		}
+		
+		Stack<Object> stack=new Stack<Object>();
+		
+		int depth=0;
+		
+		public KatoFormHandler() {
+			stack.push("START");
+		}
+		@Override
+		public void endElement(String uri, String localName, String name)
+				throws SAXException {
+				
+				if(name.equals("class")) {
+					stack.pop();
+				}
+				else if(name.equals("thread")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("method")) {
+					pop(JMethod.class);
+					
+				}
+				else if(name.equals("int")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("long")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("double")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("float")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("short")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("byte")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("char")) {
+					stack.pop();
+				}
+				else if(name.equals("bool")) {
+					stack.pop();
+					
+				}
+				else if(name.equals("frame")) {
+					pop(JStackFrame.class);
+					
+				}
+				else if(name.equals("var")) {
+					JLocalVariable var=(JLocalVariable) stack.pop(); 
+					JStackFrame parent=(JStackFrame) stack.peek();
+					parent.addVariable(var.slot,var.value);
+					
+				}
+				else if(name.equals("instance")) {
+					pop(JObject.class);
+					
+					
+				}
+				else if(name.equals("monitor")) {
+					// nothing to do
+					
+				}
+				else if(name.equals("fielddef")) {
+					Object top=	stack.pop();
+					
+					
+				}
+				else if(name.equals("field")) {
+					FieldValue v=(FieldValue) pop(FieldValue.class);
+					// parent should be Jonect
+					JObject parent=(JObject) stack.peek();
+					parent.addValue(v.id,v.value);
+					
+				}
+				else if(name.equals("threadgroup")) {
+					stack.pop();
+					
+				}
+				System.out.println(pad()+"<"+name+" ("+stack.peek()+")");
+				depth--;
+		}
+
+		private Object pop(Class clazz) {
+			Object o=stack.pop();
+			if(clazz.isAssignableFrom(o.getClass())==false) { 
+				throw new RuntimeException("expected "+clazz+" got "+o);
+			}
+			return o;
+			
+		}
+		@Override
+		public void startElement(String uri, String localName, String name,
+				Attributes attributes) throws SAXException {
+			depth++;
+			System.out.println(pad()+">"+name+" ("+stack.peek()+")");
+			if(name.equals("class")) {
+				loadClass(attributes);
+				
+			}
+			// values 
+			else if(name.equals("int")) {
+				loadIntValue(attributes);
+				
+			}
+			else if(name.equals("long")) {
+				loadLongValue(attributes);
+				
+			}
+			else if(name.equals("double")) {
+				loadDoubleValue(attributes);
+				
+			}
+			else if(name.equals("float")) {
+				loadFloatValue(attributes);
+				
+			}
+			else if(name.equals("short")) {
+				loadShortValue(attributes);
+				
+			}
+			else if(name.equals("byte")) {
+				loadByteValue(attributes);
+				
+			}
+			else if(name.equals("char")) {
+				loadCharValue(attributes);
+			}
+			else if(name.equals("bool")) {
+				loadBooleanValue(attributes);
+				
+			}
+			
+			// types 
+			
+			else if(name.equals("thread")) {
+				loadThead(attributes);
+				
+			}
+			else if(name.equals("method")) {
+				loadMethod(attributes);
+				
+			}
+			else if(name.equals("frame")) {
+				loadFrame(attributes);
+				
+			}
+			else if(name.equals("monitor")) {
+				loadMonitor(attributes);
+				
+			}
+			else if(name.equals("fielddef")) {
+				loadFieldDef(attributes);
+				
+			}
+			else if(name.equals("field")) {
+				loadInstanceField(attributes);
+				
+			}
+			else if(name.equals("instance")) {
+				loadInstance(attributes);
+				
+			}
+			else if(name.equals("var")) {
+				loadLocalVariable(attributes);
+				
+			}
+			else if(name.equals("threadgroup")) {
+				loadThreadGroup(attributes);
+				
+			}
+			
+			
+		}
+		private void loadInstanceField(Attributes attributes) {
+			
+			// parent should be JObject
+			// <field id="-1789980132" signature="Ljavax/naming/Context;" name="globalNamingContext">
+			JObject parent=(JObject) stack.peek();
+			try {
+				JClass c=(JClass) parent.getJavaClass();
+			} catch (CorruptDataException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+			int id=getInteger(attributes,"id");
+			
+			stack.push(new FieldValue(id));
+			
+		}
+
+		private void loadLocalVariable(Attributes attributes) {
+			// parent should be a JstackFrame
+			JStackFrame parent=(JStackFrame) stack.peek();
+			// <var slot="2">
+			int slot=getInteger(attributes,"slot");
+			JLocalVariable var=new JLocalVariable();
+			var.slot=slot;
+			stack.push(var);
+			
+		}
+
+		private void loadFloatValue(Attributes attributes) {
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				float[] array=new float[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+				
+			
+			String value=getString(attributes, "value");
+			Float i=Float.parseFloat(value);
+			JValueHolder v=(JValueHolder) stack.peek();
+			v.setValue(i);
+			stack.push(i);
+			}
+			
+		}
+
+		private void loadBooleanValue(Attributes attributes) {
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				boolean[] array=new boolean[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+				
+			
+			String value=getString(attributes, "value");
+			Boolean i=Boolean.parseBoolean(value);
+			JValueHolder v=(JValueHolder) stack.peek();
+			v.setValue(i);
+			stack.push(i);
+			}
+		}
+
+		private void loadCharValue(Attributes attributes) {
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				char[] array=new char[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+				
+		
+			String value=getString(attributes, "value");
+			Integer x=Integer.parseInt(value);
+			char i=(char) x.intValue();
+			JValueHolder v=(JValueHolder) stack.peek();
+			v.setValue(i);
+			stack.push(i);
+			}
+		}
+
+		private void loadByteValue(Attributes attributes) {
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				byte[] array=new byte[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+				
+			
+			String value=getString(attributes, "value");
+			Byte i=Byte.parseByte(value);
+			JValueHolder v=(JValueHolder) stack.peek();
+			v.setValue(i);
+			stack.push(i);
+			}
+		}
+
+		private void loadShortValue(Attributes attributes) {
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				short[] array=new short[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+				
+		
+			String value=getString(attributes, "value");
+			Short i=Short.parseShort(value);
+			JValueHolder v=(JValueHolder) stack.peek();
+			v.setValue(i);
+			stack.push(i);
+			}	
+		}
+
+		private void loadDoubleValue(Attributes attributes) {
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				double[] array=new double[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+				
+			
+			String value=getString(attributes, "value");
+			Double i=Double.parseDouble(value);
+			JValueHolder v=(JValueHolder) stack.peek();
+			v.setValue(i);
+			stack.push(i);
+			}
+		}
+
+		private void loadLongValue(Attributes attributes) {
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				long[] array=new long[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+				
+			
+			String value=getString(attributes, "value");
+			Long i=Long.parseLong(value);
+			JValueHolder v=(JValueHolder) stack.peek();
+			v.setValue(i);
+			stack.push(i);
+			}
+		}
+
+		private void loadIntValue(Attributes attributes) {
+			
+			// two forms 
+			// <int ref="-2133580592" length="6">
+			// <int value="45"/>
+			
+			if(hasAttribute(attributes,"ref")) {
+				int ref=getInteger(attributes, "ref");
+				int length=getInteger(attributes, "length");
+				int[] array=new int[length];
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(array);
+				stack.push(array);
+			}
+			else {
+		
+				String value=getString(attributes, "value");
+				Integer i=Integer.parseInt(value);
+				JValueHolder v=(JValueHolder) stack.peek();
+				v.setValue(i);
+				stack.push(i);
+			}
+		}
+
+		
+
+		private void loadInstance(Attributes attributes) {
+			// <instance classid="-2133632092" id="-2133632096">
+			
+			int objectid=getInteger(attributes,"id");
+			int classid=getInteger(attributes,"classid");
+			
+			JObject o=model.getObject(classid, objectid);
+			
+			stack.push(o);
+
+			
+		}
+
+		private void loadFieldDef(Attributes attributes) {
+			
+			int id=getInteger(attributes,"id");
+			
+			Object top=stack.peek();
+			JClass c=(JClass) top;
+			JField f=c.getField(id);
+			f.staticField=getBoolean(attributes,"static",false);
+			f.signature=getString(attributes, "signature");
+			f.name=getString(attributes, "name");
+			stack.push(f);
+						
+			
+			
+			
+		}
+
+		
+
+		private Boolean getBoolean(Attributes attributes, String attrName,
+				boolean defaultValue) {
+			
+			int index=attributes.getIndex(attrName);
+			if(index==-1) return defaultValue;
+			String value= attributes.getValue(index);
+			return Boolean.parseBoolean(value);
+		}
+
+		private void loadThreadGroup(Attributes attributes) {
+			stack.push(new Vector());
+			//<threadgroup id="-2133580548" parent="0" maxpriority="10" isdaemon="0">
+			int id=getInteger(attributes,"id");
+			
+		}
+
+		private void loadMonitor(Attributes attributes) {
+			// parent should be a Thread
+			JThread parent=(JThread) stack.peek();
+			// <monitor id="-1797624076"/>
+			int id=getInteger(attributes,"id");
+			JMonitor m=model.getMonitor(id);
+			parent.addMonitor(m);
+			
+			
+			
+		}
+
+		private void loadFrame(Attributes attributes) {
+			// parent should be a Thread
+			JThread parent=(JThread) stack.peek();
+			// <frame this="137639684" methodid="137639540" location="0">
+			
+			int tid=getInteger(attributes,"this");
+			int mid=getInteger(attributes,"methodid");
+			int loc=getInteger(attributes,"location");
+			
+			JStackFrame frame=new JStackFrame();
+			JLocation location=new JLocation();
+			location.method=model.getMethod(mid);
+			location.linenumber=loc;
+			frame.location=location;
+			
+			parent.addStackFrame(frame);
+			
+			stack.push(frame);
+			
+		}
+
+		private void loadMethod(Attributes attributes) {
+			
+			int id=getInteger(attributes,"id");
+			JMethod m=model.getMethod(id);
+			m.maxlocals=getInteger(attributes, "maxlocals");
+			m.name=getString(attributes, "name");
+			m.signature=getString(attributes, "sig");
+			
+			stack.push(m);
+		}
+
+		private void loadThead(Attributes attributes) {
+			int id=getInteger(attributes,"id");
+			JThread t=model.getThread(id);
+			t.threadName=getString(attributes, "name");
+			t.priority=(short) getInteger(attributes, "priority");
+			t.daemon=(short) getInteger(attributes, "isdaemon");
+			
+			id=getInteger(attributes,"contendedmonitor");
+			if(id!=0) {
+				JMonitor m=model.getMonitor(id);
+				t.contendedMonitor=m;
+			}
+			id=getInteger(attributes,"threadgroup");
+			if(id!=0) {
+				// FIXME
+			}
+			// <thread  threadgroup="137639676" contendedmonitor="137639680">
+			
+			stack.push(t);
+			
+		}
+
+		public void loadClass(Attributes attributes) {
+			int id=getInteger(attributes,"id");
+			JClass c=model.getClass(id);
+			c.classSig=getString(attributes,"name");
+			
+			id=getInteger(attributes,"loader");
+			JClassLoader loader=model.getLoader(id);
+			c.classloader=loader;
+			
+			stack.push(c);
+			
+		}
+		private boolean hasAttribute(Attributes attributes, String attrName) {
+			int index=attributes.getIndex(attrName);
+			return index!= -1;
+		}
+		private String getString(Attributes attributes, String attrName) {
+			int index=attributes.getIndex(attrName);
+			if(index==-1) return null;
+			return attributes.getValue(index);
+			
+		}
+		private int getInteger(Attributes attributes, String attrName) {
+			
+			int index=attributes.getIndex(attrName);
+			if(index==-1) return 0;
+			
+			String value=attributes.getValue(index);
+			if(value==null) return 0;
+			
+			if(value.trim().equals("")) return 0;
+			
+			return Integer.parseInt(value);
+			
+		}
+		private long getLong(Attributes attributes, String attrName) {
+			
+			int index=attributes.getIndex(attrName);
+			if(index==-1) return 0;
+			
+			String value=attributes.getValue(index);
+			if(value==null) return 0;
+			
+			if(value.trim().equals("")) return 0;
+			
+			return Long.parseLong(value);
+			
+		}
+		@Override
+		public void characters(char[] ch, int start, int length)
+				throws SAXException {
+			// called when we get a base64 data set
+			Object parent=stack.peek();
+			Class parentClass=parent.getClass();
+			if(parentClass.isArray()==false) return ; // not for us
+			
+			if(int[].class.isAssignableFrom(parentClass)) {
+				// int array
+				int[] data=(int[]) parent;
+				if(data.length==0) return; // nothing to do
+				String target=new String(ch,start,length);
+				byte[] result=Base64.decodeBase64(target.getBytes());
+				int a=1;
+				
+			}
+			else if(char[].class.isAssignableFrom(parentClass)) {
+				// int array
+				char[] data=(char[]) parent;
+				if(data.length==0) return; // nothing to do
+				String target=new String(ch,start,length);
+				byte[] result=Base64.decodeBase64(target.getBytes());
+				int a=1;
+				
+			}
+		}
+		
+	}
+
+	public XMLReader(File f) throws ParserConfigurationException, SAXException, IOException
{
+		
+		
+		SAXParserFactory factory= SAXParserFactory.newInstance();
+		factory.setNamespaceAware(true);
+		
+		SAXParser parser=factory.newSAXParser();
+	
+		parser.parse(f, new KatoFormHandler());
+		
+		
+
+	}
+
+	
+	
+}

Added: incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/test/java/test/apache/kato/jvmti/TestXMLReader.java
URL: http://svn.apache.org/viewvc/incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/test/java/test/apache/kato/jvmti/TestXMLReader.java?rev=784667&view=auto
==============================================================================
--- incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/test/java/test/apache/kato/jvmti/TestXMLReader.java
(added)
+++ incubator/kato/branches/experimental/maven_restructure/org.apache.kato/kato.jvmti/src/test/java/test/apache/kato/jvmti/TestXMLReader.java
Mon Jun 15 06:26:49 2009
@@ -0,0 +1,18 @@
+package test.apache.kato.jvmti;
+
+import java.io.File;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.kato.jvmti.reader.XMLReader;
+import org.xml.sax.SAXException;
+
+public class TestXMLReader extends TestCase {
+
+	public void testReadXMLFile() throws Exception, SAXException, IOException {
+		File f=new File("/home/spoole/workspace-maven/PyJVMTI/kato.xml");
+		XMLReader r=new XMLReader(f);
+		
+	}
+}



Mime
View raw message