KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spap > launcher > LauncherFactory


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.spap.launcher;
20
21 import java.util.Hashtable JavaDoc;
22
23
24 import sync4j.syncclient.spap.AssetInstallationException;
25 import sync4j.syncclient.spdm.DMException;
26 import sync4j.syncclient.spdm.DeviceManager;
27 import sync4j.syncclient.spdm.SimpleDeviceManager;
28
29 /**
30  * Manages <code>Laucher</code>
31  * <p>Supplies the method for obtain the appropriate <code>Launcher</code>
32  * for a given program
33  *
34  * @version $Id: LauncherFactory.java,v 1.3 2005/01/19 11:18:36 fabius Exp $
35  * @author Stefano Nichele
36  */

37
38 public class LauncherFactory {
39
40     // Contain the mapping from program's extension to appropriate launcher.
41
// It's inizilized through DM
42
private static Hashtable JavaDoc fileExtensionMapping = null;
43
44     static {
45         // init LauncherFactory with DM
46
DeviceManager dm = SimpleDeviceManager.getDeviceManager();
47
48         try {
49             fileExtensionMapping =
50                     dm.getManagementTree("").getNodeValues("spap/launcherFactoryConfig");
51         }
52         catch (DMException ex) {
53             throw new IllegalStateException JavaDoc("Error in LauncherFactory config");
54         }
55     }
56
57
58     // ---------------------------------------------------------- Public methods
59

60     /**
61      * Returns an instance of the appropriate <code>Launcher</code>
62      * for the given <i>programName</i>
63      * @param programName name of the program of which it wants
64      * the <code>Launcher</code>
65      * @return the appropriate <code>Launcher</code>
66      * @throws Exception if the appropriate <code>Launcher</code> is not found
67      * or if an error occurs during the inizialization
68      */

69     public static Launcher getLauncher(String JavaDoc programName) throws AssetInstallationException {
70         Launcher launcher = null;
71
72         int indexPoint = programName.lastIndexOf(".");
73         String JavaDoc extension = programName.substring(indexPoint + 1, programName.length());
74
75         String JavaDoc classLauncher = (String JavaDoc)fileExtensionMapping.get(extension);
76
77         if (classLauncher != null) {
78
79             try {
80                 launcher = (Launcher)(Class.forName(classLauncher).newInstance());
81             } catch (ClassNotFoundException JavaDoc ex) {
82                 throw new AssetInstallationException(
83                         "Error in launcher inizialization for class '" +
84                         classLauncher + "' (Class not found)"
85                                 );
86             } catch (Exception JavaDoc ex) {
87                 throw new AssetInstallationException(
88                         "Error in launcher inizialization for class '" +
89                         classLauncher + "' (" + ex.getMessage() + ")");
90             }
91
92         } else {
93             throw new AssetInstallationException("Launcher not defined for " +
94                                 extension + " program");
95         }
96         return launcher;
97     }
98
99
100 }
Popular Tags