KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > eclipse > actions > Utilities


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.forrest.eclipse.actions;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FilenameFilter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.net.UnknownHostException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  *
34  * Some handy utilities for working with plugins.
35  */

36 public class Utilities {
37
38     /**
39      * @param directory
40      * @return @throws
41      * FileNotFoundException
42      */

43     static public List JavaDoc getFileListing(File JavaDoc directory)
44             throws FileNotFoundException JavaDoc {
45         class JARFilter implements FilenameFilter JavaDoc {
46             public boolean accept(File JavaDoc dir, String JavaDoc name) {
47                 return (name.endsWith(".jar"));
48             }
49         }
50
51         List JavaDoc result = new ArrayList JavaDoc();
52         File JavaDoc[] filesAndDirs = directory.listFiles();
53         List JavaDoc filesDirs = Arrays.asList(filesAndDirs);
54         Iterator JavaDoc filesIter = filesDirs.iterator();
55         File JavaDoc file = null;
56
57         while (filesIter.hasNext()) {
58             file = (File JavaDoc) filesIter.next();
59
60             if (!file.isFile()) {
61                 List JavaDoc deeperList = getFileListing(file);
62                 result.addAll(deeperList);
63             } else {
64                 result.add(file);
65             }
66         }
67
68         return result;
69     }
70     
71     /**
72      * Checks to see if the port is available.
73      * @return true if the port is available
74      */

75     static public boolean isPortFree(int portNumber) {
76         try {
77             Socket JavaDoc echoSocket = new Socket JavaDoc("localhost", portNumber);
78             echoSocket.close();
79             return false;
80         } catch (UnknownHostException JavaDoc e) {
81             return true;
82             
83         } catch (IOException JavaDoc e) {
84             return true;
85             
86         }
87         
88     };
89
90     /**
91      * @param cmdString
92      */

93     static public void RunExtCommand(final String JavaDoc cmdString) {
94         Runnable JavaDoc r = new Runnable JavaDoc() {
95             public void run() {
96                 Process JavaDoc p;
97                 try {
98                     System.out.println(cmdString);
99                     p = Runtime.getRuntime().exec(cmdString);
100
101                     BufferedReader JavaDoc br = new BufferedReader JavaDoc(
102                             new InputStreamReader JavaDoc(p.getInputStream()));
103                     String JavaDoc str;
104                     while ((str = br.readLine()) != null)
105                         System.out.println(str);
106
107                     p.waitFor();
108                 } catch (Exception JavaDoc e) {
109                     System.out.println(e);
110                 }
111             }
112         };
113         r.run();
114     }
115 }
116
Popular Tags