View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.util.launcher;
21  
22  import java.io.File;
23  import java.net.URL;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Hashtable;
27  import java.util.Iterator;
28  
29  import org.xnap.XNap;
30  import org.xnap.util.Preferences;
31  import org.xnap.util.QuotedStringTokenizer;
32  
33  /***
34   * Manager class providing static functions which handle the OS dependent
35   * opening of files with the correct program.
36   */
37  public class LauncherManager
38  {
39  
40      //--- Data field(s) ---
41  
42      private static Preferences prefs = Preferences.getInstance();
43      //private static Logger logger = Logger.getLogger(LauncherManager.class);
44      private static LauncherManager singleton;
45  
46      private Hashtable launcherByKey = new Hashtable();
47      
48      //--- Constructor(s) ---
49  
50      private LauncherManager() 
51      {
52  		add(new WindowsLauncher());
53  		add(new MacOSXLauncher());
54  		add(new KDELauncher());
55  		add(new DefaultLauncher("other", XNap.tr("Other"), ""));
56      }
57  
58      //--- Method(s) ---
59  
60      public static LauncherManager getInstance()
61      {
62  		if (singleton == null) {
63  			synchronized (LauncherManager.class) {
64  				if (singleton == null) {
65  					singleton = new LauncherManager();
66  				}
67  			}
68  		}
69  
70  		return singleton;
71      }
72  
73      public static boolean isEnabled()
74      {
75  		Launcher launcher = getInstance().get(prefs.getFileLauncherType());
76  		return (launcher != null) ? launcher.isEnabled() : false;
77      }
78  
79      public static void open(File file) throws IOException
80      {
81  		Launcher launcher = getInstance().get(prefs.getFileLauncherType());
82  		if (launcher == null) {
83  			throw new IOException(XNap.tr("No launcher selected."));
84  		}
85  		launcher.open(file);
86      }
87  
88  	public static void open(URL url) throws IOException
89  	{
90  		Launcher launcher = getInstance().get(prefs.getFileLauncherType());
91  		if (launcher == null) {
92  			throw new IOException(XNap.tr("No launcher selected."));
93  		}
94  		launcher.open(url);
95  	}
96  
97      public void add(Launcher launcher)
98      {
99  		launcherByKey.put(launcher.getKey(), launcher);
100     }
101 
102     public Launcher get(String type)
103     {
104 		return (Launcher)launcherByKey.get(type);
105     }
106 
107     public Iterator iterator()
108     {
109 		return launcherByKey.values().iterator();
110     }
111 
112     public void remove(Launcher launcher)
113     {
114 		launcherByKey.remove(launcher.getKey());
115     }
116     
117     /***
118      * Executes <code>args</code> using the system runtime.
119      * Writes the output to console if "captureLauncherOutput"
120      * preference is true.
121      */
122     public static Process exec(String[] args) throws IOException
123     {
124 		Process opener = Runtime.getRuntime().exec(args);
125 		if (Preferences.getInstance().getCaptureLauncherOutput()) {
126 			//  Console.getInstance().add(opener);
127 		}
128 		return opener;
129     }
130 
131     /***
132      * Executes command. Ever occurence of "{}" is replaced by
133      * <code>files</code>. If <code>command</code> does not contain
134      * "{}", <code>files</code> are appended.
135      *
136      * @param command tokenized by {@link QuotedStringTokenizer}
137      * @param files if null, no additional arguments are appended
138      */
139     public static Process exec(String command, File[] files) 
140 		throws IOException
141     {
142 		boolean inserted = false;
143 		QuotedStringTokenizer t = new QuotedStringTokenizer(command);
144 		ArrayList args = new ArrayList(t.countTokens() + files.length);
145 		while (t.hasMoreTokens()) {
146 			String s = t.nextToken();
147 			if (s.equals("{}")) {
148 				insertFiles(args, files);
149 				inserted = true;
150 			}
151 			else {
152 				args.add(s);
153 			}
154 		}
155 	
156 		if (!inserted) {
157 			insertFiles(args, files);
158 		}
159 
160 		return exec((String[])args.toArray(new String[0]));
161     }
162 
163     private static void insertFiles(ArrayList args, File[] files)
164     {
165 		if (files != null) {
166 			for (int i = 0; i < files.length; i++) {
167 				if (files[i] != null || files[i].length() != 0) {
168 					args.add(files[i].getAbsolutePath());
169 				}
170 			}
171 		}
172     }
173 
174 }
175 
176