View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2002 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   */
24  
25  package org.microemu.app.util;
26  
27  import java.io.File;
28  import java.net.MalformedURLException;
29  import java.net.URL;
30  
31  import org.microemu.EmulatorContext;
32  import org.microemu.app.Common;
33  import org.microemu.app.Config;
34  import org.microemu.log.Logger;
35  
36  import com.barteo.emulator.device.Device;
37  
38  public class DeviceEntry {
39  	
40  	private String name;
41  
42  	private String fileName;
43  
44  	private String descriptorLocation;
45  
46  	private boolean defaultDevice;
47  
48  	private boolean canRemove;
49  
50  	/**
51  	 * @deprecated
52  	 */
53  	private String className;
54  
55  	/**
56  	 * @deprecated
57  	 */
58  	private EmulatorContext emulatorContext;
59  
60  	public DeviceEntry(String name, String fileName, String descriptorLocation, boolean defaultDevice) {
61  		this(name, fileName, descriptorLocation, defaultDevice, true);
62  	}
63  
64  	public DeviceEntry(String name, String fileName, String descriptorLocation, boolean defaultDevice, boolean canRemove) {
65  		this.name = name;
66  		this.fileName = fileName;
67  		this.descriptorLocation = descriptorLocation;
68  		this.defaultDevice = defaultDevice;
69  		this.canRemove = canRemove;
70  	}
71  
72  	/**
73  	 * @deprecated use new DeviceEntry(String name, String fileName, String descriptorLocation, boolean defaultDevice);
74  	 */
75  	public DeviceEntry(String name, String fileName, boolean defaultDevice, String className,
76  			EmulatorContext emulatorContext) {
77  		this(name, fileName, null, defaultDevice, true);
78  
79  		this.className = className;
80  		this.emulatorContext = emulatorContext;
81  	}
82  
83  	public boolean canRemove() {
84  		return canRemove;
85  	}
86  
87  	public String getDescriptorLocation() {
88  		if (descriptorLocation == null) {
89  			URL[] urls = new URL[1];
90  			try {
91  				urls[0] = new File(Config.getConfigPath(), fileName).toURI().toURL();
92  				ClassLoader classLoader = Common.createExtensionsClassLoader(urls);
93  				Class deviceClass = Class.forName(className, true, classLoader);
94  				Device device = (Device) deviceClass.newInstance();
95  
96  				com.barteo.emulator.EmulatorContext oldContext = new com.barteo.emulator.EmulatorContext(emulatorContext);
97  
98  				device.init(oldContext);
99  				descriptorLocation = device.getDescriptorLocation();
100 			} catch (MalformedURLException ex) {
101 				Logger.error(ex);
102 			} catch (ClassNotFoundException ex) {
103 				Logger.error(ex);
104 			} catch (InstantiationException ex) {
105 				Logger.error(ex);
106 			} catch (IllegalAccessException ex) {
107 				Logger.error(ex);
108 			}
109 
110 		}
111 
112 		return descriptorLocation;
113 	}
114 
115 	public String getFileName() {
116 		return fileName;
117 	}
118 
119 	/**
120 	 * @deprecated
121 	 */
122 	public void setFileName(String fileName) {
123 		this.fileName = fileName;
124 	}
125 
126 	public String getName() {
127 		return name;
128 	}
129 
130 	public boolean isDefaultDevice() {
131 		return defaultDevice;
132 	}
133 
134 	public void setDefaultDevice(boolean b) {
135 		defaultDevice = b;
136 	}
137 
138 	public boolean equals(DeviceEntry test) {
139 		if (test == null) {
140 			return false;
141 		}
142 		if (test.getDescriptorLocation().equals(getDescriptorLocation())) {
143 			return true;
144 		}
145 
146 		return false;
147 	}
148 
149 	public String toString() {
150 		if (defaultDevice) {
151 			return name + " (default)";
152 		} else {
153 			return name;
154 		}
155 	}
156 
157 }