KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > metadata > ServiceDeploymentParser


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.system.metadata;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.jboss.deployment.DeploymentException;
28 import org.jboss.logging.Logger;
29 import org.jboss.mx.loading.LoaderRepositoryFactory;
30 import org.jboss.mx.loading.LoaderRepositoryFactory.LoaderRepositoryConfig;
31 import org.jboss.util.StringPropertyReplacer;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * ServiceDeploymentParser
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 1.1 $
41  */

42 public class ServiceDeploymentParser
43 {
44    /** The log */
45    private static final Logger log = Logger.getLogger(ServiceDeploymentParser.class);
46    
47    /** The document */
48    private Document JavaDoc document;
49    
50    /**
51     * Create a new service deployment parser
52     *
53     * @param document the xml config
54     */

55    public ServiceDeploymentParser(Document JavaDoc document)
56    {
57       if (document == null)
58          throw new IllegalArgumentException JavaDoc("Null document");
59       
60       this.document = document;
61    }
62
63    /**
64     * Parse the xml
65     *
66     * @return the service deployment
67     * @throws DeploymentException for any error
68     */

69    public ServiceDeployment parse() throws DeploymentException
70    {
71       ServiceDeployment parsed = new ServiceDeployment();
72
73       List JavaDoc<ServiceDeploymentClassPath> classPaths = parseXMLClasspath(document);
74       parsed.setClassPaths(classPaths);
75
76       LoaderRepositoryConfig repository = parseLoaderRepositoryConfig(document);
77       if (repository != null)
78          parsed.setLoaderRepositoryConfig(repository);
79
80       // We can't parse the services yet, because it requires the classloader
81
parsed.setConfig(document.getDocumentElement());
82       return parsed;
83    }
84
85    /**
86     * Parse the xml classpath
87     *
88     * @param document the document
89     * @return the list of classpaths
90     * @throws DeploymentException for any error
91     */

92    private List JavaDoc<ServiceDeploymentClassPath> parseXMLClasspath(Document JavaDoc document) throws DeploymentException
93    {
94       ArrayList JavaDoc<ServiceDeploymentClassPath> classPaths = null;
95       
96       NodeList JavaDoc children = document.getDocumentElement().getChildNodes();
97       for (int i = 0; i < children.getLength(); ++i)
98       {
99          if (children.item(i).getNodeType() == Node.ELEMENT_NODE)
100          {
101             Element JavaDoc classpathElement = (Element JavaDoc)children.item(i);
102             if (classpathElement.getTagName().equals("classpath"))
103             {
104                log.debug("Found classpath element: " + classpathElement);
105                if (classpathElement.hasAttribute("codebase") == false)
106                   throw new DeploymentException("Invalid classpath element missing codebase: " + classpathElement);
107
108                String JavaDoc codebase = classpathElement.getAttribute("codebase").trim();
109                codebase = StringPropertyReplacer.replaceProperties(codebase);
110
111                String JavaDoc archives = null;
112                if (classpathElement.hasAttribute("archives"))
113                {
114                   archives = classpathElement.getAttribute("archives").trim();
115                   archives = StringPropertyReplacer.replaceProperties(archives);
116                   if ("".equals(archives))
117                      archives = null;
118                }
119                
120                if (classPaths == null)
121                   classPaths = new ArrayList JavaDoc<ServiceDeploymentClassPath>();
122
123                ServiceDeploymentClassPath classPath = new ServiceDeploymentClassPath(codebase, archives);
124                classPaths.add(classPath);
125             }
126          }
127       }
128       return classPaths;
129    }
130
131    /**
132     * Parse the loader repository config
133     *
134     * @param document the document
135     * @return the config
136     * @throws DeploymentException for any error
137     */

138    private LoaderRepositoryConfig parseLoaderRepositoryConfig(Document JavaDoc document) throws DeploymentException
139    {
140       // Check for a custom loader-repository for scoping
141
NodeList JavaDoc loaders = document.getElementsByTagName("loader-repository");
142       if( loaders.getLength() > 0 )
143       {
144          Element JavaDoc loader = (Element JavaDoc) loaders.item(0);
145          try
146          {
147             return LoaderRepositoryFactory.parseRepositoryConfig(loader);
148          }
149          catch (Exception JavaDoc e)
150          {
151             throw DeploymentException.rethrowAsDeploymentException("Unable to parse loader repository config", e);
152          }
153       }
154       return null;
155    }
156 }
157
Popular Tags