KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > ide > ui > JBPluginUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.jboss4.ide.ui;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FilenameFilter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.ServerSocket JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
32 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
33 import org.openide.ErrorManager;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 /**
40  *
41  * @author Ivan Sidorkin
42  */

43 public class JBPluginUtils {
44     
45     public static final String JavaDoc SERVER_XML = File.separator + "deploy" + File.separator +
46                 "jbossweb-tomcat55.sar" + File.separator + "server.xml";
47     
48     
49     //------------ getting exists servers---------------------------
50
/**
51      * returns Hashmap
52      * key = server name
53      * value = server folder full path
54      */

55     public static Hashtable JavaDoc getRegisteredDomains(String JavaDoc serverLocation){
56         Hashtable JavaDoc result = new Hashtable JavaDoc();
57         // String domainListFile = File.separator+"common"+File.separator+"nodemanager"+File.separator+"nodemanager.domains"; // NOI18N
58

59         if (isGoodJBServerLocation4x(new File JavaDoc(serverLocation)) ||
60             isGoodJBServerLocation5x(new File JavaDoc(serverLocation)))
61         {
62            File JavaDoc file = new File JavaDoc(serverLocation + File.separator + "server"); // NOI18N
63

64             String JavaDoc[] files = file.list(new FilenameFilter JavaDoc(){
65                 public boolean accept(File JavaDoc dir, String JavaDoc name){
66                     if ((new File JavaDoc(dir.getAbsolutePath()+File.separator+name)).isDirectory()) return true;
67                     return false;
68                 }
69             });
70             
71             for(int i =0; i<files.length; i++){
72                 String JavaDoc path = file.getAbsolutePath() + File.separator + files[i];
73                 
74                 if (isGoodJBInstanceLocation4x(new File JavaDoc(path)) ||
75                     isGoodJBInstanceLocation5x(new File JavaDoc(path)))
76                 {
77                     result.put(files[i], path);
78                 }
79             }
80         }
81         return result;
82     }
83     
84     
85     //--------------- checking for possible domain directory -------------
86
private static List JavaDoc<String JavaDoc> domainRequirements4x = new LinkedList JavaDoc<String JavaDoc>();
87     static {
88         domainRequirements4x.add("conf"); // NOI18N
89
domainRequirements4x.add("deploy"); // NOI18N
90
domainRequirements4x.add("lib"); // NOI18N
91
domainRequirements4x.add("conf/jboss-service.xml"); // NOI18N
92
domainRequirements4x.add("lib/jboss-j2ee.jar"); // NOI18N
93
domainRequirements4x.add("lib/jboss.jar"); // NOI18N
94
domainRequirements4x.add("lib/jbosssx.jar"); // NOI18N
95
domainRequirements4x.add("lib/jboss-transaction.jar"); // NOI18N
96
domainRequirements4x.add("lib/jmx-adaptor-plugin.jar"); // NOI18N
97
domainRequirements4x.add("lib/jnpserver.jar"); // NOI18N
98
domainRequirements4x.add("lib/log4j.jar"); // NOI18N
99
domainRequirements4x.add("lib/xmlentitymgr.jar"); // NOI18N
100
domainRequirements4x.add("deploy/jmx-invoker-service.xml"); // NOI18N
101
}
102     
103     private static List JavaDoc<String JavaDoc> domainRequirements5x = new LinkedList JavaDoc<String JavaDoc>();
104
105     static {
106         domainRequirements5x.add("conf"); // NOI18N
107
domainRequirements5x.add("deploy"); // NOI18N
108
domainRequirements5x.add("lib"); // NOI18N
109
domainRequirements5x.add("conf/jboss-service.xml"); // NOI18N
110
domainRequirements5x.add("lib/jboss-j2ee.jar"); // NOI18N
111
domainRequirements5x.add("lib/jboss.jar"); // NOI18N
112
domainRequirements5x.add("lib/jbosssx.jar"); // NOI18N
113
domainRequirements5x.add("lib/jboss-transaction.jar"); // NOI18N
114
domainRequirements5x.add("lib/jmx-adaptor-plugin.jar"); // NOI18N
115
domainRequirements5x.add("lib/jnpserver.jar"); // NOI18N
116
domainRequirements5x.add("lib/log4j.jar"); // NOI18N
117
domainRequirements5x.add("deploy/jmx-invoker-service.xml"); // NOI18N
118
}
119     
120     private static boolean isGoodJBInstanceLocation(File JavaDoc candidate, List JavaDoc<String JavaDoc> requirements){
121         if (null == candidate ||
122                 !candidate.exists() ||
123                 !candidate.canRead() ||
124                 !candidate.isDirectory() ||
125                 !hasRequiredChildren(candidate, requirements)) {
126             return false;
127         }
128         return true;
129     }
130     
131     public static boolean isGoodJBInstanceLocation4x(File JavaDoc candidate){
132         return isGoodJBInstanceLocation(candidate, domainRequirements4x);
133     }
134     
135     public static boolean isGoodJBInstanceLocation5x(File JavaDoc candidate){
136         return isGoodJBInstanceLocation(candidate, domainRequirements5x);
137     }
138     
139     //--------------- checking for possible server directory -------------
140
private static List JavaDoc<String JavaDoc> serverRequirements4x = new LinkedList JavaDoc<String JavaDoc>();
141     
142     static {
143         serverRequirements4x.add("bin"); // NOI18N
144
serverRequirements4x.add("client"); // NOI18N
145
serverRequirements4x.add("lib"); // NOI18N
146
serverRequirements4x.add("server"); // NOI18N
147
serverRequirements4x.add("lib/jboss-common.jar"); // NOI18N
148
serverRequirements4x.add("lib/endorsed/resolver.jar"); // NOI18N
149
}
150     
151     private static List JavaDoc<String JavaDoc> serverRequirements5x = new LinkedList JavaDoc<String JavaDoc>();
152     
153     static {
154         serverRequirements5x.add("bin"); // NOI18N
155
serverRequirements5x.add("client"); // NOI18N
156
serverRequirements5x.add("lib"); // NOI18N
157
serverRequirements5x.add("server"); // NOI18N
158
serverRequirements5x.add("lib/jboss-common-core.jar"); // NOI18N
159
serverRequirements5x.add("lib/endorsed/resolver.jar"); // NOI18N
160
}
161     
162     private static boolean isGoodJBServerLocation(File JavaDoc candidate, List JavaDoc<String JavaDoc> requirements){
163         if (null == candidate ||
164                 !candidate.exists() ||
165                 !candidate.canRead() ||
166                 !candidate.isDirectory() ||
167                 !hasRequiredChildren(candidate, requirements)) {
168             return false;
169         }
170         return true;
171     }
172     
173     public static boolean isGoodJBServerLocation4x(File JavaDoc candidate){
174         return isGoodJBServerLocation(candidate, serverRequirements4x);
175     }
176     
177     public static boolean isGoodJBServerLocation4x(JBDeploymentManager dm){
178         String JavaDoc installDir = dm.getInstanceProperties().getProperty(JBPluginProperties.PROPERTY_ROOT_DIR);
179         return isGoodJBServerLocation4x(new File JavaDoc(installDir));
180     }
181     
182     public static boolean isGoodJBServerLocation5x(File JavaDoc candidate){
183         return isGoodJBServerLocation(candidate, serverRequirements5x);
184     }
185     
186     private static boolean hasRequiredChildren(File JavaDoc candidate, List JavaDoc<String JavaDoc> requiredChildren) {
187         if (null == candidate)
188             return false;
189         String JavaDoc[] children = candidate.list();
190         if (null == children)
191             return false;
192         if (null == requiredChildren)
193             return true;
194         Iterator JavaDoc iter = requiredChildren.iterator();
195         while (iter.hasNext()){
196             String JavaDoc next = (String JavaDoc)iter.next();
197             File JavaDoc test = new File JavaDoc(candidate.getPath()+File.separator+next);
198             if (!test.exists())
199                 return false;
200         }
201         return true;
202     }
203     
204     //--------------------------------------------------------------------
205

206     /**
207      *
208      *
209      */

210     public static String JavaDoc getDeployDir(String JavaDoc domainDir){
211         String JavaDoc result="";
212         result = domainDir + File.separator + "deploy"; //NOI18N
213
return result;
214         //todo: get real deploy path
215
}
216     
217     public static String JavaDoc getHTTPConnectorPort(String JavaDoc domainDir){
218         String JavaDoc defaultPort = "8080";
219         String JavaDoc serverXml = domainDir + SERVER_XML; //NOI18N
220

221         File JavaDoc serverXmlFile = new File JavaDoc(serverXml);
222         if(!serverXmlFile.exists()){
223             return defaultPort;
224         }
225         
226         InputStream JavaDoc inputStream = null;
227         Document JavaDoc document = null;
228         try{
229             inputStream = new FileInputStream JavaDoc(serverXmlFile);
230             document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
231             
232             // get the root element
233
Element JavaDoc root = document.getDocumentElement();
234             
235             NodeList JavaDoc children = root.getChildNodes();
236             for (int i = 0; i < children.getLength(); i++) {
237                 Node JavaDoc child = children.item(i);
238                 if (child.getNodeName().equals("Service")) { // NOI18N
239
NodeList JavaDoc nl = child.getChildNodes();
240                     for (int j = 0; j < nl.getLength(); j++){
241                         Node JavaDoc ch = nl.item(j);
242                         
243                         if (ch.getNodeName().equals("Connector")) { // NOI18N
244
return ch.getAttributes().getNamedItem("port").getNodeValue();
245                         }
246                     }
247                 }
248             }
249         }catch(Exception JavaDoc e){
250             e.printStackTrace();
251             // it is ok
252
// it optional functionality so we don't need to look at any exception
253
}
254         
255         return defaultPort;
256     }
257
258
259     public static String JavaDoc getJnpPort(String JavaDoc domainDir){
260         
261         String JavaDoc serviceXml = domainDir+File.separator+"conf"+File.separator+"jboss-service.xml"; //NOI18N
262
File JavaDoc xmlFile = new File JavaDoc(serviceXml);
263         if (!xmlFile.exists()) return "";
264         
265         InputStream JavaDoc inputStream = null;
266         Document JavaDoc document = null;
267         try{
268             inputStream = new FileInputStream JavaDoc(xmlFile);
269             document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
270             
271             // get the root element
272
Element JavaDoc root = document.getDocumentElement();
273             
274             // get the child nodes
275
NodeList JavaDoc children = root.getChildNodes();
276             for (int i = 0; i < children.getLength(); i++) {
277                 Node JavaDoc child = children.item(i);
278                 if (child.getNodeName().equals("mbean")) { // NOI18N
279
NodeList JavaDoc nl = child.getChildNodes();
280                     if (!child.getAttributes().getNamedItem("name").getNodeValue().equals("jboss:service=Naming")) //NOI18N
281
continue;
282                     for (int j = 0; j < nl.getLength(); j++){
283                         Node JavaDoc ch = nl.item(j);
284                         
285                         if (ch.getNodeName().equals("attribute")) { // NOI18N
286
if (!ch.getAttributes().getNamedItem("name").getNodeValue().equals("Port")) //NOI18N
287
continue;
288                              return ch.getFirstChild().getNodeValue();
289                         }
290                     }
291                 }
292             }
293         }catch(Exception JavaDoc e){
294             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
295         }
296         return "";
297     }
298   
299     public static String JavaDoc getRMINamingServicePort(String JavaDoc domainDir){
300         
301         String JavaDoc serviceXml = domainDir+File.separator+"conf"+File.separator+"jboss-service.xml"; //NOI18N
302
File JavaDoc xmlFile = new File JavaDoc(serviceXml);
303         if (!xmlFile.exists()) return "";
304         
305         InputStream JavaDoc inputStream = null;
306         Document JavaDoc document = null;
307         try{
308             inputStream = new FileInputStream JavaDoc(xmlFile);
309             document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
310             
311             // get the root element
312
Element JavaDoc root = document.getDocumentElement();
313             
314             // get the child nodes
315
NodeList JavaDoc children = root.getChildNodes();
316             for (int i = 0; i < children.getLength(); i++) {
317                 Node JavaDoc child = children.item(i);
318                 if (child.getNodeName().equals("mbean")) { // NOI18N
319
NodeList JavaDoc nl = child.getChildNodes();
320                     if (!child.getAttributes().getNamedItem("name").getNodeValue().equals("jboss:service=Naming")) //NOI18N
321
continue;
322                     for (int j = 0; j < nl.getLength(); j++){
323                         Node JavaDoc ch = nl.item(j);
324                         
325                         if (ch.getNodeName().equals("attribute")) { // NOI18N
326
if (!ch.getAttributes().getNamedItem("name").getNodeValue().equals("RmiPort")) //NOI18N
327
continue;
328                              return ch.getFirstChild().getNodeValue();
329                         }
330                     }
331                 }
332             }
333         }catch(Exception JavaDoc e){
334             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
335         }
336         return "";
337     }
338   
339     public static String JavaDoc getRMIInvokerPort(String JavaDoc domainDir){
340         
341         String JavaDoc serviceXml = domainDir+File.separator+"conf"+File.separator+"jboss-service.xml"; //NOI18N
342
File JavaDoc xmlFile = new File JavaDoc(serviceXml);
343         if (!xmlFile.exists()) return "";
344         
345         InputStream JavaDoc inputStream = null;
346         Document JavaDoc document = null;
347         try{
348             inputStream = new FileInputStream JavaDoc(xmlFile);
349             document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
350             
351             // get the root element
352
Element JavaDoc root = document.getDocumentElement();
353             
354             // get the child nodes
355
NodeList JavaDoc children = root.getChildNodes();
356             for (int i = 0; i < children.getLength(); i++) {
357                 Node JavaDoc child = children.item(i);
358                 if (child.getNodeName().equals("mbean")) { // NOI18N
359
NodeList JavaDoc nl = child.getChildNodes();
360                     if (!child.getAttributes().getNamedItem("name").getNodeValue().equals("jboss:service=invoker,type=jrmp")) //NOI18N
361
continue;
362                     for (int j = 0; j < nl.getLength(); j++){
363                         Node JavaDoc ch = nl.item(j);
364                         
365                         if (ch.getNodeName().equals("attribute")) { // NOI18N
366
if (!ch.getAttributes().getNamedItem("name").getNodeValue().equals("RMIObjectPort")) //NOI18N
367
continue;
368                              return ch.getFirstChild().getNodeValue();
369                         }
370                     }
371                 }
372             }
373         }catch(Exception JavaDoc e){
374             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
375         }
376         return "";
377     }
378   
379       /** Return true if the specified port is free, false otherwise. */
380     public static boolean isPortFree(int port) {
381         ServerSocket JavaDoc soc = null;
382         try {
383             soc = new ServerSocket JavaDoc(port);
384         } catch (IOException JavaDoc ioe) {
385             return false;
386         } finally {
387             if (soc != null)
388                 try { soc.close(); } catch (IOException JavaDoc ex) {} // noop
389
}
390         
391         return true;
392     }
393     
394
395 }
396
Popular Tags