KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > services > binding > XMLServicesStore


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.services.binding;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38 import org.jboss.util.StringPropertyReplacer;
39 import org.jboss.logging.Logger;
40
41 /**
42  * XML implementation of ServicesStore.
43  *
44  * <p>Reads/writes/manages the XML config file for the ServiceBinding Manager module
45  *
46  * @author <a HREF="mailto:bitpushr@rochester.rr.com">Mike Finn</a>.
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 37459 $
49  */

50 public class XMLServicesStore implements ServicesStore
51 {
52    private final Logger log = Logger.getLogger(getClass());
53
54    /** A thread-safe map of Map<ObjectName,ServiceConfig> keyed by server name.
55     */

56    private Map JavaDoc servers = Collections.synchronizedMap(new HashMap JavaDoc());
57
58    /** This method is not usable in this implementation as XMLServiceStore is read-only
59     *
60     * @param serverName
61     * @param serviceName
62     * @param config
63     * @exception DuplicateServiceException never thrown
64     * @exception UnsupportedOperationException("XMLServiceStore is read-only") always thrown
65     */

66    public void addService(String JavaDoc serverName, ObjectName JavaDoc serviceName, ServiceConfig config)
67       throws DuplicateServiceException
68    {
69       throw new UnsupportedOperationException JavaDoc("XMLServiceStore is read-only");
70    }
71
72    /**
73     * Looks up a service, by server name and service name.
74     * If the server or service does not exist, a null object is returned.
75     *
76     * @param serverName The name of the server in the config file
77     * @param serviceName The name of the service (i.e. the JMX object name)
78     *
79     * @returns ServiceConfig object. Null if server or service is not found.
80     */

81    public ServiceConfig getService(String JavaDoc serverName, ObjectName JavaDoc serviceName)
82    {
83       Map JavaDoc serverMap = (Map JavaDoc) this.servers.get(serverName);
84       ServiceConfig config = null;
85       if( serverMap != null )
86       {
87          config = (ServiceConfig) serverMap.get(serviceName);
88       }
89       return config;
90    }
91
92    /** This method is not usable in this implementation as XMLServiceStore is read-only
93     *
94     * @param serverName
95     * @param serviceName
96     * @exception UnsupportedOperationException("XMLServiceStore is read-only") always thrown
97     */

98    public void removeService(String JavaDoc serverName, ObjectName JavaDoc serviceName)
99    {
100       throw new UnsupportedOperationException JavaDoc("XMLServiceStore is read-only");
101    }
102
103    /** Loads XML config file into memory and parses it into ServiceConfig
104     * objects.
105     *
106     * @throws Exception on any parse error
107     */

108    public void load(URL JavaDoc cfgURL)
109       throws Exception JavaDoc
110    {
111       DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
112       DocumentBuilder JavaDoc parser = factory.newDocumentBuilder();
113       InputStream JavaDoc cfgIS = cfgURL.openStream();
114       Document JavaDoc configDoc = parser.parse(cfgIS, cfgURL.toString());
115       Element JavaDoc serviceBindings = configDoc.getDocumentElement();
116       NodeList JavaDoc servers = serviceBindings.getElementsByTagName("server");
117       int length = servers.getLength();
118       for(int s = 0; s < length; s ++)
119       {
120          Element JavaDoc server = (Element JavaDoc) servers.item(s);
121          parseServer(server);
122       }
123    }
124
125    /** Store is a noop as this is a read-only store
126     */

127    public void store(URL JavaDoc cfgURL)
128       throws Exception JavaDoc
129    {
130    }
131
132    /** Parse /service-bindings/server/service-config element into
133     a Map<ObjectName,ServiceConfig> objects that are stored in the servers
134     map keyed by server.name.
135     */

136    private void parseServer(Element JavaDoc server)
137       throws Exception JavaDoc
138    {
139       String JavaDoc serverName = server.getAttribute("name");
140       HashMap JavaDoc serverConfigurations = new HashMap JavaDoc();
141       NodeList JavaDoc serviceConfigs = server.getElementsByTagName("service-config");
142       int length = serviceConfigs.getLength();
143       for(int c = 0; c < length; c ++)
144       {
145          Element JavaDoc config = (Element JavaDoc) serviceConfigs.item(c);
146          ServiceConfig serviceConfig = new ServiceConfig();
147          ObjectName JavaDoc serviceObjectName = parseConfig(config, serviceConfig);
148          serverConfigurations.put(serviceObjectName, serviceConfig);
149       }
150       this.servers.put(serverName, serverConfigurations);
151    }
152
153    /** Parse /service-bindings/server/service-config element into
154     the given ServiceConfig object.
155     */

156    private ObjectName JavaDoc parseConfig(Element JavaDoc config, ServiceConfig serviceConfig)
157       throws Exception JavaDoc
158    {
159       String JavaDoc serviceName = config.getAttribute("name");
160       ObjectName JavaDoc serviceObjectName = new ObjectName JavaDoc(serviceName);
161       serviceConfig.setServiceName(serviceName);
162
163       // Parse the delegate info
164
String JavaDoc delegateClass = config.getAttribute("delegateClass");
165       if( delegateClass.length() == 0 )
166          delegateClass = "org.jboss.services.binding.AttributeMappingDelegate";
167       Element JavaDoc delegateConfig = null;
168       NodeList JavaDoc delegateConfigs = config.getElementsByTagName("delegate-config");
169       if( delegateConfigs.getLength() > 0 )
170          delegateConfig = (Element JavaDoc) delegateConfigs.item(0);
171       serviceConfig.setServiceConfigDelegateClassName(delegateClass);
172       serviceConfig.setServiceConfigDelegateConfig(delegateConfig);
173
174       // Parse the service bindings
175
ArrayList JavaDoc bindingsArray = new ArrayList JavaDoc();
176       NodeList JavaDoc bindings = config.getElementsByTagName("binding");
177       int length = bindings.getLength();
178       for(int b = 0; b < length; b ++)
179       {
180          Element JavaDoc binding = (Element JavaDoc) bindings.item(b);
181          ServiceBinding sb = parseBinding(binding);
182          bindingsArray.add(sb);
183       }
184       ServiceBinding[] tmp = new ServiceBinding[bindingsArray.size()];
185       bindingsArray.toArray(tmp);
186       serviceConfig.setBindings(tmp);
187       return serviceObjectName;
188    }
189
190    /** Parse /service-bindings/server/service-config/binding element into
191     a ServiceBinding object. Any attributes whose value contains a system
192     property reference of the form ${x} will be replaced with the correcsponding
193     System.getProperty("x") value if one exists.
194     */

195    private ServiceBinding parseBinding(Element JavaDoc binding)
196       throws Exception JavaDoc
197    {
198       String JavaDoc name = binding.getAttribute("name");
199       if (name != null)
200       {
201          name = StringPropertyReplacer.replaceProperties(name);
202       }
203       String JavaDoc hostName = binding.getAttribute("host");
204       if (hostName != null)
205       {
206          hostName = StringPropertyReplacer.replaceProperties(hostName);
207       }
208       if (hostName.length() == 0)
209          hostName = null;
210       String JavaDoc portStr = binding.getAttribute("port");
211       if (portStr != null)
212       {
213          portStr = StringPropertyReplacer.replaceProperties(portStr);
214       }
215       if (portStr.length() == 0)
216          portStr = "0";
217       log.debug("parseBinding, name='" + name + "', host='" + hostName + "'"
218          + ", port='" + portStr + "'");
219       int port = Integer.parseInt(portStr);
220       ServiceBinding sb = new ServiceBinding(name, hostName, port);
221       return sb;
222    }
223 }
224
Popular Tags