📄 motoroladeviceimporter.java
字号:
/**
* Copyright (c) 2003-2006 Craig Setera
* All Rights Reserved.
* Licensed under the Eclipse Public License - v 1.0
* For more information see http://www.eclipse.org/legal/epl-v10.html
*/
package eclipseme.toolkit.motorola.impl;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.osgi.framework.Bundle;
import eclipseme.core.importer.IDeviceImporter;
import eclipseme.core.importer.LibraryImporter;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.model.Classpath;
import eclipseme.core.model.ILibrary;
import eclipseme.core.model.IPreverifier;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.model.impl.StandardPreverifierFactory;
import eclipseme.toolkit.motorola.internal.MotorolaPlugin;
/**
* A device importer implementation for the Motorola toolkits.
* <p />
* Copyright (c) 2003-2006 Craig Setera<br>
* All Rights Reserved.<br>
* Licensed under the Eclipse Public License - v 1.0<p/>
* <br>
* $Revision: 1.2 $
* <br>
* $Date: 2006/02/20 20:59:04 $
* <br>
* @author Craig Setera
* @author Kevin Hunter
*/
public class MotorolaDeviceImporter implements IDeviceImporter {
// Properties file holding emulator/device information
private static final String PROPS_FILE = "moto_emulators.properties";
// Various property file keys
private static final String PROPS_EMULATORS_LIST = "emulators.list";
private static final String LAUNCH_COMMAND_SUFFIX = "_launch.command";
private static final String EXECUTABLE_SUFFIX = "_executable";
private static final String PREDEPLOY_REQUIRED_SUFFIX = "_predeploy.required";
private static final String DEBUG_SERVER_SUFFIX = "_debug.server";
private static final String DEVICES_SUFFIX = "_devices";
// Maintains information about an individual emulator
// within a Motorola SDK
private class EmulatorData {
public String emulator;
public String[] devices;
public String executable;
public String launchCommandTemplate;
public boolean debugServer;
public boolean predeployRequired;
}
private Map emulatorDataMap;
/**
* @see eclipseme.core.importer.IDeviceImporter#getMatchingDevices(java.io.File, org.eclipse.core.runtime.IProgressMonitor)
*/
public IDevice[] getMatchingDevices(File directory, IProgressMonitor monitor) {
IDevice[] devices = null;
File resourcesDirectory = new File(directory, "resources");
if (resourcesDirectory.exists()) {
devices = getResourcesDevices(resourcesDirectory, monitor);
}
return devices;
}
/**
* Create a new device based on the specified information.
*
* @param propsFile
* @param deviceProperties
* @param devicename
* @return
*/
private IDevice createDevice(
File propsFile,
Properties deviceProperties,
String devicename)
{
IDevice device = null;
EmulatorData emulatorData = getEmulatorData(devicename);
if (emulatorData != null) {
device = createDevice(propsFile, deviceProperties, devicename, emulatorData);
}
return device;
}
/**
* Create a new device based on the specified information.
*
* @param propsFile
* @param deviceProperties
* @param devicename
* @param emulatorData
* @return
*/
private IDevice createDevice(
File propsFile,
Properties deviceProperties,
String devicename,
EmulatorData emulatorData)
{
// We need to look at the contents of parent directories
File emulatorBinDirectory = propsFile.getParentFile().getParentFile();
File emulatorDirectory = emulatorBinDirectory.getParentFile();
File sdkDirectory = emulatorDirectory.getParentFile();
MotorolaDevice device = new MotorolaDevice();
try {
device.setBundle(MotorolaPlugin.getDefault().getBundle().getSymbolicName());
device.setClasspath(getClasspath(emulatorDirectory));
device.setDebugServer(emulatorData.debugServer);
device.setDescription(devicename);
device.setDeviceProperties(deviceProperties);
device.setExecutable(new File(emulatorBinDirectory, emulatorData.executable));
device.setGroupName(sdkDirectory.getName());
device.setName(devicename);
device.setPredeploymentRequired(emulatorData.predeployRequired);
device.setPreverifier(getPreverifier(emulatorBinDirectory, sdkDirectory));
device.setPropertiesFile(propsFile);
device.setProtectionDomains(new String[0]);
device.setLaunchCommandTemplate(emulatorData.launchCommandTemplate);
} catch (CoreException e) {
EclipseMECorePlugin.log(IStatus.WARNING, "Error importing device " + devicename, e);
device = null;
}
return device;
}
/**
* Find a preverify executable in the specified directory or
* subdirectories.
*
* @param directory
* @return
*/
private File findPreverifyExecutable(File directory) {
File[] files = directory.listFiles(new FileFilter() {
public boolean accept(File pathname) {
String name = pathname.getName();
return pathname.isDirectory() ||
(name.equals("preverify.exe") || name.equals("preverify"));
}
});
File executable = null;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
executable = findPreverifyExecutable(files[i]);
} else {
executable = files[i];
break;
}
}
return executable;
}
/**
* Return the classpath given the specified emulator directory.
* The current implementation assumes that the classpath is created
* using all of the files in the lib directory.
*
* @param emulatorDirectory
* @return
*/
private Classpath getClasspath(File emulatorDirectory) {
Classpath classpath = new Classpath();
File libDirectory = new File(emulatorDirectory, "lib");
File[] libraries = libDirectory.listFiles(new FileFilter() {
public boolean accept(File pathname) {
String name = pathname.getName();
return
pathname.isFile() &&
(name.endsWith(".zip") || name.endsWith(".jar"));
}
});
LibraryImporter libImporter = new LibraryImporter();
for (int i = 0; i < libraries.length; i++) {
ILibrary library = libImporter.createLibraryFor(libraries[i]);
classpath.addEntry(library);
}
return classpath;
}
/**
* Return the emulator data associated with the specified
* device name or <code>null</code> if none can be found.
*
* @param deviceName
* @return
*/
private EmulatorData getEmulatorData(String deviceName) {
return (EmulatorData) getEmulatorDataMap().get(deviceName);
}
/**
* Return the map of emulator data. Each entry
* is keyed by the device name mapped to an
* instance of EmulatorData.
*
* @return
*/
private Map getEmulatorDataMap() {
if (emulatorDataMap == null) {
emulatorDataMap = readEmulatorDataMap();
}
return emulatorDataMap;
}
/**
* Return a preverifier for use with the specified emulator. If a
* specific preverifier cannot be found in the specified emulator
* directory, search for a backup preverify.exe in the SDK.
*
* @param emulatorBinDirectory
* @param sdkDirectory
* @return
* @throws CoreException
*/
private IPreverifier getPreverifier(File emulatorBinDirectory, File sdkDirectory)
throws CoreException
{
File preverifyExecutable = findPreverifyExecutable(emulatorBinDirectory);
if (preverifyExecutable == null) {
preverifyExecutable = findPreverifyExecutable(sdkDirectory);
}
return StandardPreverifierFactory.createPreverifier(preverifyExecutable);
}
/**
* Return a properties object for the properties in the file.
*
* @param propsFile
* @return
*/
private Properties getProperties(File propsFile) {
Properties properties = new Properties();
InputStream is = null;
try {
is = new FileInputStream(propsFile);
properties.load(is);
} catch (IOException e) {
EclipseMECorePlugin.log(
IStatus.WARNING,
"Error reading properties file " + propsFile,
e);
} finally {
if (is != null) {
try { is.close(); } catch (IOException e) {}
}
}
return properties;
}
/**
* Return the devices found in the specified resources directory.
*
* @param resourcesDirectory
* @param monitor
* @return
*/
private IDevice[] getResourcesDevices(File resourcesDirectory, IProgressMonitor monitor) {
File[] propsFiles = resourcesDirectory.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isFile() && pathname.getName().endsWith(".props");
}
});
ArrayList devices = new ArrayList();
for (int i = 0; i < propsFiles.length; i++) {
File propsFile = propsFiles[i];
Properties deviceProperties = getProperties(propsFile);
String devicename = deviceProperties.getProperty("devicename");
if (devicename != null) {
IDevice device = createDevice(propsFile, deviceProperties, devicename);
if (device != null) {
devices.add(device);
}
}
}
return (devices.size() == 0) ?
null :
(IDevice[]) devices.toArray(new IDevice[devices.size()]);
}
/**
* Parse out the emulator data information from the properties.
*
* @param emulatorsProperties
* @return
*/
private Map parseEmulatorDataProperties(Properties emulatorsProperties) {
Map emulatorDataMap = new HashMap();
String emulatorsList = emulatorsProperties.getProperty(PROPS_EMULATORS_LIST, "");
String[] emulators = emulatorsList.split(",");
for (int i = 0; i < emulators.length; i++) {
parseEmulatorDataProperties(emulatorDataMap, emulatorsProperties, emulators[i]);
}
return emulatorDataMap;
}
/**
* Parse out the information for the specific emulator.
*
* @param emulatorMap
* @param emulatorsProperties
* @param emulator
*/
private void parseEmulatorDataProperties(
Map emulatorMap,
Properties emulatorsProperties,
String emulator)
{
String devicesString =
emulatorsProperties.getProperty(emulator + DEVICES_SUFFIX, "");
String debugServerString =
emulatorsProperties.getProperty(emulator + DEBUG_SERVER_SUFFIX, "");
String predeployString =
emulatorsProperties.getProperty(emulator + PREDEPLOY_REQUIRED_SUFFIX, "");
EmulatorData data = new EmulatorData();
data.emulator = emulator;
data.executable =
emulatorsProperties.getProperty(emulator + EXECUTABLE_SUFFIX, "");
data.launchCommandTemplate =
emulatorsProperties.getProperty(emulator + LAUNCH_COMMAND_SUFFIX, "");
if (devicesString.length() > 0) {
data.devices = devicesString.split(",");
} else {
data.devices = new String[0];
}
data.debugServer = debugServerString.equalsIgnoreCase("true");
data.predeployRequired = predeployString.equalsIgnoreCase("true");
for (int i = 0; i < data.devices.length; i++) {
String device = data.devices[i];
emulatorMap.put(device, data);
}
}
/**
* Read in the emulator data map.
*
* @return
*/
private Map readEmulatorDataMap() {
// Read the properties the define the emulator data
Properties emulatorsProperties = new Properties();
InputStream emulatorsStream = null;
try {
Bundle bundle = MotorolaPlugin.getDefault().getBundle();
URL propsFileURL = bundle.getEntry(PROPS_FILE);
emulatorsStream = propsFileURL.openStream();
if (emulatorsStream != null) {
emulatorsProperties.load(emulatorsStream);
}
} catch (IOException e) {
EclipseMECorePlugin.log(IStatus.ERROR, "Error loading Motorola emulator properties", e);
} finally {
if (emulatorsStream != null) {
try { emulatorsStream.close(); } catch (IOException e) {}
}
}
return parseEmulatorDataProperties(emulatorsProperties);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -