1 22 package org.jboss.mx.loading; 23 24 import java.util.Set ; 25 import java.util.HashSet ; 26 import java.util.StringTokenizer ; 27 import java.util.NoSuchElementException ; 28 29 import java.net.URL ; 30 import java.net.MalformedURLException ; 31 32 import java.text.ParseException ; 33 34 import java.io.IOException ; 35 import java.io.BufferedReader ; 36 import java.io.InputStreamReader ; 37 38 import org.jboss.logging.Logger; 39 40 49 public class MLetParser 50 implements MBeanFileParser 51 { 52 private static final Logger log = Logger.getLogger(MLetParser.class); 53 54 56 66 public Set parseMBeanFile(String url) throws ParseException , MalformedURLException 67 { 68 return parseMBeanFile(new URL (url)); 69 } 70 71 79 public Set parseMBeanFile(URL url) throws ParseException 80 { 81 Set mlets = new HashSet (); 82 MBeanElement element = null; 83 84 try 85 { 86 BufferedReader reader = new BufferedReader (new InputStreamReader (url.openStream())); 87 int c = -1; 88 89 while((c = reader.read()) != -1) 91 { 92 if (c == '<') 94 { 95 StringBuffer buf = new StringBuffer (1000); 96 boolean readMore = true; 97 98 while(readMore) 100 { 101 c = reader.read(); 102 103 if (c == -1) 104 throw new ParseException ("Unexpected end of file. Tag was not closed: " + buf.toString().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim(), 0); 105 106 if (c == '>') 107 { 108 readMore = false; 109 break; 110 } 111 112 buf.append((char)c); 113 } 114 115 StringTokenizer tokenizer = new StringTokenizer (buf.toString(), "= \n\t\r"); 117 String tagName = null, attributeName = null, attributeValue = null; 118 119 if (tokenizer.hasMoreTokens()) 121 tagName = tokenizer.nextToken().trim(); 122 123 if (tagName.equals("MLET")) 125 { 126 element = new MBeanElement(); 127 128 while(tokenizer.hasMoreTokens()) 129 { 130 try 131 { 132 attributeName = tokenizer.nextToken("= \n\t\r").trim(); 134 attributeValue = tokenizer.nextToken(" \n\t\r").trim(); 135 136 if (attributeValue.equals("=")) 137 attributeValue = tokenizer.nextToken(); 138 139 if (attributeName.equals("CODE")) 141 { 142 element.setCode(attributeValue); 143 } 144 145 else if (attributeName.equals("OBJECT")) 147 element.setObject(attributeValue); 148 149 else if (attributeName.equals("ARCHIVE")) 151 element.setArchive(attributeValue); 153 154 else if (attributeName.equals("CODEBASE")) 156 element.setCodebase(attributeValue); 157 158 else if (attributeName.equals("NAME")) 160 element.setName(attributeValue); 161 162 else if (attributeName.equals("VERSION")) 164 element.setVersion(attributeValue); 165 } 166 catch (NoSuchElementException e) 167 { 168 171 log.warn("No value found for attribute '" + attributeName); 172 } 173 } 174 175 if (element.getCode() == null && element.getObject() == null) 176 throw new ParseException ("<" + buf.toString().replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').trim() + "> is missing mandatory CODE | OBJECT attribute", 0); 177 if (element.getArchives().size() == 0) 178 throw new ParseException ("<" + buf.toString().replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').trim() + "> is missing mandatory ARCHIVE attribute", 0); 179 } 180 181 else if (tagName.equals("/MLET")) 183 { 184 mlets.add(element); 185 element = null; 186 } 187 188 else if (tagName.equals("ARG")) 190 { 191 try 192 { 193 if (!tokenizer.nextToken().equals("TYPE")) 195 continue; 196 197 String type = tokenizer.nextToken(); 198 199 if (!tokenizer.nextToken().equals("VALUE")) 201 continue; 202 203 String value = tokenizer.nextToken(" \n\t\r"); 204 205 if (element != null) 207 element.addArg(type, value); 208 } 209 catch (NoSuchElementException e) 210 { 211 element = null; 213 214 log.warn("Malformed element: <" + buf.toString() + ">"); 215 } 216 } 217 } } 220 mlets.remove(null); 222 return mlets; 223 } 224 catch (IOException e) 225 { 226 throw new ParseException (e.toString(), 0); 227 } 228 } 229 230 } 231 232 233 234 235 | Popular Tags |