KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > loading > MLetParser


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.loading;
23
24 import java.util.Set JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28
29 import java.net.URL JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31
32 import java.text.ParseException JavaDoc;
33
34 import java.io.IOException JavaDoc;
35 import java.io.BufferedReader JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37
38 import org.jboss.logging.Logger;
39
40 /**
41  * Parses an MLet text file confirming to spec format.
42  *
43  * @see javax.management.MLet
44  * @see javax.management.MBeanFileParser
45  *
46  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
47  * @version $Revision: 37459 $
48  */

49 public class MLetParser
50    implements MBeanFileParser
51 {
52    private static final Logger log = Logger.getLogger(MLetParser.class);
53
54    // MBeanFileParser implementation --------------------------------
55

56    /**
57     * Reads an MLet text file from a given URL.
58     *
59     * @param url URL to MLet text file
60     * @return set containing <tt>MBeanElement</tt> objects representing
61     * the parsed MBean entries
62     * @throws ParseException if there was error in reading the MLet text file
63     * @throws MalformedURLException if the <tt>url</tt> argument cannot be used to
64     * construct a valid URL.
65     */

66    public Set JavaDoc parseMBeanFile(String JavaDoc url) throws ParseException JavaDoc, MalformedURLException JavaDoc
67    {
68       return parseMBeanFile(new URL JavaDoc(url));
69    }
70
71    /**
72     * Reads an MLet text file from a given URL.
73     *
74     * @param url URL to MLet text file
75     * @return set containing <tt>MBeanElement</tt> objects representing the parsed
76     * MBean entries
77     * @throws ParseException if there was an error in reading the MLet text file
78     */

79    public Set JavaDoc parseMBeanFile(URL JavaDoc url) throws ParseException JavaDoc
80    {
81       Set JavaDoc mlets = new HashSet JavaDoc();
82       MBeanElement element = null;
83
84       try
85       {
86          BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(url.openStream()));
87          int c = -1;
88       
89          // read the file
90
while((c = reader.read()) != -1)
91          {
92             // read and parse one tag at a time
93
if (c == '<')
94             {
95                StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1000);
96                boolean readMore = true;
97                
98                // read the element contents
99
while(readMore)
100                {
101                   c = reader.read();
102                   
103                   if (c == -1)
104                      throw new ParseException JavaDoc("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                // tokenize the element contents
116
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(buf.toString(), "= \n\t\r");
117                String JavaDoc tagName = null, attributeName = null, attributeValue = null;
118                
119                // first token is the tag name
120
if (tokenizer.hasMoreTokens())
121                   tagName = tokenizer.nextToken().trim();
122                
123                // parse MLET tag
124
if (tagName.equals("MLET"))
125                {
126                   element = new MBeanElement();
127                   
128                   while(tokenizer.hasMoreTokens())
129                   {
130                      try
131                      {
132                         // following tokens are attribute name=value pairs
133
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                         // CODE attribute
140
if (attributeName.equals("CODE"))
141                         {
142                            element.setCode(attributeValue);
143                         }
144                         
145                         // OBJET attribute
146
else if (attributeName.equals("OBJECT"))
147                            element.setObject(attributeValue);
148                         
149                         // ARCHIVE attribute
150
else if (attributeName.equals("ARCHIVE"))
151                            // FIXME: according to spec "archivelist" must be in quotes, we don't enforce that
152
element.setArchive(attributeValue);
153                         
154                         // CODEBASE attribute
155
else if (attributeName.equals("CODEBASE"))
156                            element.setCodebase(attributeValue);
157                         
158                         // NAME attribute
159
else if (attributeName.equals("NAME"))
160                            element.setName(attributeValue);
161                         
162                         // VERSION attribute
163
else if (attributeName.equals("VERSION"))
164                            element.setVersion(attributeValue);
165                      }
166                      catch (NoSuchElementException JavaDoc e)
167                      {
168                         // couldn't find a valid attribute, value pair
169
// ignore and move to next one
170

171                         log.warn("No value found for attribute '" + attributeName);
172                      }
173                   }
174                   
175                   if (element.getCode() == null && element.getObject() == null)
176                      throw new ParseException JavaDoc("<" + 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 JavaDoc("<" + buf.toString().replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').trim() + "> is missing mandatory ARCHIVE attribute", 0);
179                }
180                
181                // parse </MLET> tag
182
else if (tagName.equals("/MLET"))
183                {
184                   mlets.add(element);
185                   element = null;
186                }
187            
188                // parse <ARG> tag
189
else if (tagName.equals("ARG"))
190                {
191                   try
192                   {
193                      // if second token is not TYPE then skip the attribute
194
if (!tokenizer.nextToken().equals("TYPE"))
195                         continue;
196                         
197                      String JavaDoc type = tokenizer.nextToken();
198                      
199                      // if fourth token is not VALUE then skip the attribute
200
if (!tokenizer.nextToken().equals("VALUE"))
201                         continue;
202                         
203                      String JavaDoc value = tokenizer.nextToken(" \n\t\r");
204                      
205                      // element is non-null if we're within <MLET> </MLET> tags
206
if (element != null)
207                         element.addArg(type, value);
208                   }
209                   catch (NoSuchElementException JavaDoc e)
210                   {
211                      // malformed ARG tag means the MBean can't be instantiated
212
element = null;
213                      
214                      log.warn("Malformed element: <" + buf.toString() + ">");
215                   }
216                }
217             } // end of if (c == '<')
218
} // while((c = reader.read()) != -1)
219

220          // get rid of any null elements
221
mlets.remove(null);
222          return mlets;
223       }
224       catch (IOException JavaDoc e)
225       {
226          throw new ParseException JavaDoc(e.toString(), 0);
227       }
228    }
229    
230 }
231       
232
233
234
235
Popular Tags