KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > jmsmanager > wizard > JMSProviderData


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.console.jmsmanager.wizard;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.zip.ZipEntry JavaDoc;
31 import java.util.zip.ZipInputStream JavaDoc;
32 import javax.portlet.PortletRequest;
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.geronimo.console.util.PortletManager;
39 import org.apache.geronimo.kernel.util.XmlUtil;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46 /**
47  * Loads data on JMS providers known to the console. Reads from a properties
48  * file on the class path.
49  *
50  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
51  */

52 public class JMSProviderData implements Serializable JavaDoc {
53     private final static Log log = LogFactory.getLog(JMSProviderData.class);
54     private String JavaDoc name;
55     private final String JavaDoc raURI;
56     private final String JavaDoc dependency;
57     private String JavaDoc defaultTransaction;
58     private ConfigPropertyData[] instanceConfigProperties;
59     private ConnectionDefinition[] connectionDefinitions;
60     private AdminObjectDefinition[] adminObjectDefinitions;
61
62     public JMSProviderData(String JavaDoc name, String JavaDoc raURI, String JavaDoc dependency) {
63         this.name = name;
64         this.raURI = raURI;
65         this.dependency = dependency;
66     }
67
68     public String JavaDoc getName() {
69         return name;
70     }
71
72     public String JavaDoc getRaURI() {
73         return raURI;
74     }
75
76     public String JavaDoc getDependency() {
77         return dependency;
78     }
79
80     public String JavaDoc getDefaultTransaction() {
81         return defaultTransaction;
82     }
83
84     public ConfigPropertyData[] getInstanceConfigProperties() {
85         return instanceConfigProperties;
86     }
87
88     public ConnectionDefinition[] getConnectionDefinitions() {
89         return connectionDefinitions;
90     }
91
92     public AdminObjectDefinition[] getAdminObjectDefinitions() {
93         return adminObjectDefinitions;
94     }
95
96     public static class ConfigPropertyData implements Serializable JavaDoc {
97         private final String JavaDoc name;
98         private final String JavaDoc type;
99         private final String JavaDoc defaultValue;
100         private final String JavaDoc description;
101
102         public ConfigPropertyData(String JavaDoc name, String JavaDoc type, String JavaDoc defaultValue, String JavaDoc description) {
103             this.name = name;
104             this.type = type;
105             this.defaultValue = defaultValue;
106             this.description = description;
107         }
108
109         public String JavaDoc getName() {
110             return name;
111         }
112
113         public String JavaDoc getType() {
114             return type;
115         }
116
117         public String JavaDoc getDefaultValue() {
118             return defaultValue;
119         }
120
121         public String JavaDoc getDescription() {
122             return description;
123         }
124     }
125
126     public static class ConnectionDefinition implements Serializable JavaDoc {
127         private final String JavaDoc connectionFactoryInterface;
128         private final ConfigPropertyData[] configProperties;
129
130         public ConnectionDefinition(String JavaDoc connectionFactoryInterface, ConfigPropertyData[] configProperties) {
131             this.connectionFactoryInterface = connectionFactoryInterface;
132             this.configProperties = configProperties;
133         }
134
135         public String JavaDoc getConnectionFactoryInterface() {
136             return connectionFactoryInterface;
137         }
138
139         public ConfigPropertyData[] getConfigProperties() {
140             return configProperties;
141         }
142     }
143
144     public static class AdminObjectDefinition implements Serializable JavaDoc {
145         private final String JavaDoc adminObjectInterface;
146         private final String JavaDoc adminObjectClass;
147         private final ConfigPropertyData[] configProperties;
148
149         public AdminObjectDefinition(String JavaDoc adminObjectInterface, String JavaDoc adminObjectClass, ConfigPropertyData[] configProperties) {
150             this.adminObjectInterface = adminObjectInterface;
151             this.adminObjectClass = adminObjectClass;
152             this.configProperties = configProperties;
153         }
154
155         public String JavaDoc getAdminObjectInterface() {
156             return adminObjectInterface;
157         }
158
159         public String JavaDoc getAdminObjectClass() {
160             return adminObjectClass;
161         }
162
163         public ConfigPropertyData[] getConfigProperties() {
164             return configProperties;
165         }
166     }
167
168
169     // *************** Static methods to access the data ****************
170

171     private static List JavaDoc all = null;
172     public static JMSProviderData[] getAllProviders() {
173         if(all == null) {
174             loadProviders();
175         }
176         return (JMSProviderData[]) all.toArray(new JMSProviderData[all.size()]);
177     }
178
179     public static JMSProviderData getProviderByName(String JavaDoc name) {
180         if(all == null) {
181             loadProviders();
182         }
183         for (int i = 0; i < all.size(); i++) {
184             JMSProviderData data = (JMSProviderData) all.get(i);
185             if(data.getName().equals(name)) {
186                 return data;
187             }
188         }
189         return null;
190     }
191
192     public static JMSProviderData getProviderData(String JavaDoc rar, PortletRequest request) throws IOException JavaDoc {
193         if(all == null) {
194             loadProviders();
195         }
196         for (int i = 0; i < all.size(); i++) {
197             JMSProviderData data = (JMSProviderData) all.get(i);
198             if(data.getRaURI().equals(rar)) {
199                 if(data.instanceConfigProperties == null) {
200                     loadRARData(data, request);
201                 }
202                 return data;
203             }
204         }
205         JMSProviderData data = new JMSProviderData(null, rar, null);
206         loadRARData(data, request);
207         all.add(data);
208         return data;
209     }
210
211     private static void loadRARData(JMSProviderData data, PortletRequest request) throws IOException JavaDoc {
212         File JavaDoc url = PortletManager.getRepositoryEntry(request, data.getRaURI());
213         if(url == null) {
214             throw new IOException JavaDoc("Unable to locate entry "+data.getRaURI()+" in repository");
215         }
216         ZipInputStream JavaDoc in = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(url));
217         ZipEntry JavaDoc entry;
218         Document JavaDoc doc = null;
219         try {
220             while((entry = in.getNextEntry()) != null) {
221                 if(entry.getName().equals("META-INF/ra.xml")) {
222                     DocumentBuilderFactory JavaDoc factory = XmlUtil.newDocumentBuilderFactory();
223                     factory.setValidating(false);
224                     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
225                     doc = builder.parse(in);
226                     in.close();
227                     in = null;
228                     break;
229                 } else in.closeEntry();
230             }
231         } catch (ParserConfigurationException JavaDoc e) {
232             log.error("Unable to read META-INF/ra.xml in RAR file '"+data.getRaURI()+"'", e);
233         } catch (SAXException JavaDoc e) {
234             log.error("Unable to read META-INF/ra.xml in RAR file '"+data.getRaURI()+"'", e);
235         } finally {
236             if (in != null)
237                 try {
238                     in.close();
239                 } catch (IOException JavaDoc ignore) {
240                 }
241         }
242         if(doc == null) {
243             throw new IOException JavaDoc("Unable to locate META-INF/ra.xml in RAR file '"+data.getRaURI()+"'");
244         }
245         Element JavaDoc root = doc.getDocumentElement();
246         if(data.getName() == null) {
247             NodeList JavaDoc displays = getChildren(root, "display-name");
248             if(displays != null && displays.getLength() > 0) {
249                 data.name = getText(displays.item(0));
250             }
251         }
252         Element JavaDoc ra = (Element JavaDoc) getChildren(root, "resourceadapter").item(0);
253         data.instanceConfigProperties = loadConfigs(ra);
254         Element JavaDoc outbound = (Element JavaDoc) getChildren(ra, "outbound-resourceadapter").item(0);
255         data.defaultTransaction = getTransactionSetting(getChildText(outbound, "transaction-support"));
256         data.connectionDefinitions = loadConnections(outbound);
257         data.adminObjectDefinitions = loadAdmins(ra);
258     }
259
260     private static String JavaDoc getTransactionSetting(String JavaDoc text) {
261         if(text == null) {
262             return null;
263         }
264         if(text.equals("XATransaction")) return "xa";
265         if(text.equals("LocalTransaction")) return "local";
266         if(text.equals("NoTransaction")) return "none";
267         return null;
268     }
269
270     private static ConfigPropertyData[] loadConfigs(Element JavaDoc parent) {
271         NodeList JavaDoc configs = getChildren(parent, "config-property");
272         if(configs == null || configs.getLength() == 0) {
273             return new ConfigPropertyData[0];
274         }
275         ConfigPropertyData[] results = new ConfigPropertyData[configs.getLength()];
276         for (int i = 0; i < results.length; i++) {
277             Element JavaDoc root = (Element JavaDoc) configs.item(i);
278             results[i] = new ConfigPropertyData(getChildText(root, "config-property-name"),
279                     getChildText(root, "config-property-type"), getChildText(root, "config-property-value"),
280                     getChildText(root, "description"));
281         }
282         return results;
283     }
284
285     private static NodeList JavaDoc getChildren(Element JavaDoc parent, String JavaDoc child) {
286         final List JavaDoc list = new ArrayList JavaDoc();
287         NodeList JavaDoc nodes = parent.getChildNodes();
288         for(int i=0; i<nodes.getLength(); i++) {
289             Node JavaDoc node = nodes.item(i);
290             if(node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(child)) {
291                 list.add(node);
292             }
293         }
294         return new NodeList JavaDoc() {
295             public Node JavaDoc item(int index) {
296                 return (Node JavaDoc) list.get(index);
297             }
298
299             public int getLength() {
300                 return list.size();
301             }
302         };
303     }
304
305     private static ConnectionDefinition[] loadConnections(Element JavaDoc outbound) {
306         NodeList JavaDoc defs = getChildren(outbound, "connection-definition");
307         if(defs == null || defs.getLength() == 0) {
308             return new ConnectionDefinition[0];
309         }
310         ConnectionDefinition[] results = new ConnectionDefinition[defs.getLength()];
311         for (int i = 0; i < results.length; i++) {
312             Element JavaDoc def = (Element JavaDoc) defs.item(i);
313             results[i] = new ConnectionDefinition(getChildText(def, "connectionfactory-interface"), loadConfigs(def));
314         }
315         return results;
316     }
317
318     private static AdminObjectDefinition[] loadAdmins(Element JavaDoc ra) {
319         NodeList JavaDoc defs = getChildren(ra, "adminobject");
320         if(defs == null || defs.getLength() == 0) {
321             return new AdminObjectDefinition[0];
322         }
323         AdminObjectDefinition[] results = new AdminObjectDefinition[defs.getLength()];
324         for (int i = 0; i < results.length; i++) {
325             Element JavaDoc def = (Element JavaDoc) defs.item(i);
326             results[i] = new AdminObjectDefinition(getChildText(def, "adminobject-interface"),
327                     getChildText(def, "adminobject-class"), loadConfigs(def));
328         }
329         return results;
330     }
331
332     private static String JavaDoc getChildText(Element JavaDoc root, String JavaDoc name) {
333         NodeList JavaDoc list = getChildren(root, name);
334         if(list == null || list.getLength() == 0) {
335             return null;
336         }
337         return getText(list.item(0));
338     }
339
340     private static String JavaDoc getText(Node JavaDoc node) {
341         StringBuffer JavaDoc buf = null;
342         NodeList JavaDoc list = node.getChildNodes();
343         if(list != null) {
344             for(int i=0; i<list.getLength(); i++) {
345                 Node JavaDoc current = list.item(i);
346                 if(current.getNodeType() == Node.TEXT_NODE) {
347                     if(buf == null) {
348                         buf = new StringBuffer JavaDoc();
349                     }
350                     buf.append(current.getNodeValue());
351                 }
352             }
353         }
354         return buf == null ? null : buf.toString();
355     }
356
357
358     private static void loadProviders() {
359         InputStream JavaDoc in = JMSProviderData.class.getResourceAsStream("/jms-resource-providers.properties");
360         if(in == null) {
361             log.error("Unable to locate JMS provider properties file");
362             return;
363         }
364         Properties JavaDoc props = new Properties JavaDoc();
365         try {
366             props.load(in);
367         } catch (IOException JavaDoc e) {
368             log.error("Unable to read JMS provider properties file", e);
369         } finally {
370             // load could fail, ensure stream is closed.
371
try {
372                 in.close();
373             } catch (IOException JavaDoc ignore) {
374                 // ignore
375
}
376         }
377         Set JavaDoc set = new HashSet JavaDoc();
378         // Find the names of the provider entries
379
for (Iterator JavaDoc it = props.keySet().iterator(); it.hasNext();) {
380             String JavaDoc key = (String JavaDoc) it.next();
381             int start = key.indexOf('.');
382             int end = key.indexOf('.', start+1);
383             if(start < 0 || end < 0) {
384                 continue;
385             }
386             set.add(key.substring(start+1, end));
387         }
388         List JavaDoc list = new ArrayList JavaDoc(set.size());
389         for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
390             String JavaDoc key = (String JavaDoc) it.next();
391             String JavaDoc name = props.getProperty("provider."+key+".name");
392             String JavaDoc rar = props.getProperty("provider."+key+".rar");
393             String JavaDoc dep = props.getProperty("provider."+key+".dependency");
394             list.add(new JMSProviderData(name, rar, dep));
395         }
396         all = list;
397     }
398 }
399
Popular Tags