AbstractDevice.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
eclipseme.core.model.impl |
![]() |
![]() |
EclipseME |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/**
* Copyright (c) 2003-2005 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.core.model.impl;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.mortbay.util.MultiException;
import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.overtheair.OTAServer;
import eclipseme.core.internal.preverification.builder.PreverificationBuilder;
import eclipseme.core.internal.utils.TemporaryFileManager;
import eclipseme.core.internal.utils.Utils;
import eclipseme.core.launching.ILaunchConstants;
import eclipseme.core.model.APIType;
import eclipseme.core.model.Classpath;
import eclipseme.core.model.ILibrary;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.IPreverifier;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.persistence.IBundleReferencePersistable;
import eclipseme.core.persistence.IPersistenceProvider;
import eclipseme.core.persistence.PersistenceException;
/**
* Abstract superclass of the various device implementations.
* <p />
* Copyright (c) 2003-2005 Craig Setera<br>
* All Rights Reserved.<br>
* Licensed under the Eclipse Public License - v 1.0<p/>
* <br>
* $Revision: 1.9 $
* <br>
* $Date: 2006/12/03 13:58:46 $
* <br>
* @author Craig Setera
*/
public abstract class AbstractDevice implements IDevice, IBundleReferencePersistable {
// Variables that define this device
protected String bundle;
protected Classpath classpath;
protected boolean debugServer;
protected String description;
protected Properties deviceProperties;
protected File executable;
protected String name;
protected String[] protectionDomains;
protected String groupName;
protected IPreverifier preverifier;
protected String launchCommandTemplate;
/**
* Test equality on a AbstractDevice and return a boolean
* indicating equality.
*
* @param device
* @return
*/
public boolean equals(AbstractDevice device) {
return
classpath.equals(device.classpath) &&
executable.equals(device.executable) &&
name.equals(device.name) &&
groupName.equals(device.groupName);
}
/**
* @see eclipseme.core.persistence.IBundleReferencePersistable#getBundle()
*/
public String getBundle() {
return bundle;
}
/**
* @see eclipseme.core.model.device.IDevice#getClasspath()
*/
public Classpath getClasspath() {
return classpath;
}
/**
* @see eclipseme.core.model.device.IDevice#getConfigurationLibrary()
*/
public ILibrary getConfigurationLibrary() {
return getLibraryWithType(APIType.CONFIGURATION);
}
/**
* @see eclipseme.core.model.device.IDevice#getDescription()
*/
public String getDescription() {
return description;
}
/**
* @see eclipseme.core.model.device.IDevice#getDeviceProperties()
*/
public Properties getDeviceProperties() {
return deviceProperties;
}
/**
* @return Returns the executable.
*/
public File getExecutable() {
return executable;
}
/**
* @see eclipseme.core.model.device.IDevice#getName()
*/
public String getName() {
return name;
}
/**
* @see eclipseme.core.model.device.IDevice#getPreverifier()
*/
public IPreverifier getPreverifier() {
return preverifier;
}
/**
* @see eclipseme.core.model.device.IDevice#getProfileLibrary()
*/
public ILibrary getProfileLibrary() {
return getLibraryWithType(APIType.PROFILE);
}
/**
* @see eclipseme.core.model.device.IDevice#getProtectionDomains()
*/
public String[] getProtectionDomains() {
return protectionDomains;
}
/**
* @see eclipseme.core.model.device.IDevice#getGroupName()
*/
public String getGroupName() {
return groupName;
}
/**
* @see eclipseme.core.model.device.IDevice#isDebugServer()
*/
public boolean isDebugServer() {
return debugServer;
}
/**
* @see eclipseme.core.model.device.IDevice#isPredeploymentRequired()
* @deprecated this method is no longer called during launching
*/
public boolean isPredeploymentRequired() {
return false;
}
/**
* @see eclipseme.core.persistence.IPersistable#loadUsing(eclipseme.core.persistence.IPersistenceProvider)
*/
public void loadUsing(IPersistenceProvider persistenceProvider)
throws PersistenceException
{
classpath = (Classpath) persistenceProvider.loadPersistable("classpath");
debugServer = persistenceProvider.loadBoolean("debugServer");
description = persistenceProvider.loadString("description");
deviceProperties = persistenceProvider.loadProperties("deviceProperties");
name = persistenceProvider.loadString("name");
protectionDomains = new String[persistenceProvider.loadInteger("protectionDomainsCount")];
for (int i = 0; i < protectionDomains.length; i++) {
protectionDomains[i] = persistenceProvider.loadString("protectionDomain" + i).trim();
}
groupName = persistenceProvider.loadString("groupName");
preverifier = (IPreverifier) persistenceProvider.loadPersistable("preverifier");
launchCommandTemplate = persistenceProvider.loadString("rawLaunchCommand");
String executableString = persistenceProvider.loadString("executable");
if ((executableString != null) && (executableString.trim().length() > 0)) {
executable = new File(executableString);
}
}
/**
* @param classpath The classpath to set.
*/
public void setClasspath(Classpath classpath) {
this.classpath = classpath;
}
/**
* @param debugServer The debugServer to set.
*/
public void setDebugServer(boolean debugServer) {
this.debugServer = debugServer;
}
/**
* @param description The description to set.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @param deviceProperties The deviceProperties to set.
*/
public void setDeviceProperties(Properties deviceProperties) {
this.deviceProperties = deviceProperties;
}
/**
* @param executable The executable to set.
*/
public void setExecutable(File executable) {
this.executable = executable;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @param predeploymentRequired The predeploymentRequired to set.
* @deprecated
*/
public void setPredeploymentRequired(boolean predeploymentRequired) {
}
/**
* @param preverifier The preverifier to set.
*/
public void setPreverifier(IPreverifier preverifier) {
this.preverifier = preverifier;
}
/**
* @param protectionDomains The protectionDomains to set.
*/
public void setProtectionDomains(String[] protectionDomains) {
this.protectionDomains = protectionDomains;
// Trim any spaces that could cause problems
for (int i = 0; i < protectionDomains.length; i++) {
protectionDomains[i] = protectionDomains[i].trim();
}
}
/**
* @param groupName The groupName to set.
*/
public void setGroupName(String groupName) {
this.groupName = groupName;
}
/**
* @see eclipseme.core.persistence.IPersistable#storeUsing(eclipseme.core.persistence.IPersistenceProvider)
*/
public void storeUsing(IPersistenceProvider persistenceProvider)
throws PersistenceException
{
persistenceProvider.storePersistable("classpath", classpath);
persistenceProvider.storeBoolean("debugServer", debugServer);
persistenceProvider.storeString("description", description);
persistenceProvider.storeProperties("deviceProperties", deviceProperties);
persistenceProvider.storeString("name", name);
persistenceProvider.storeInteger("protectionDomainsCount", protectionDomains.length);
for (int i = 0; i < protectionDomains.length; i++) {
persistenceProvider.storeString("protectionDomain" + i, protectionDomains[i]);
}
persistenceProvider.storeString("groupName", groupName);
persistenceProvider.storePersistable("preverifier", preverifier);
persistenceProvider.storeString("rawLaunchCommand", launchCommandTemplate);
if (executable != null) {
persistenceProvider.storeString("executable", executable.toString());
}
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer(getGroupName());
sb.append("/").append(getName());
return sb.toString();
}
/**
* @see eclipseme.core.persistence.IBundleReferencePersistable#setBundle(java.lang.String)
*/
public void setBundle(String bundle) {
this.bundle = bundle;
}
/**
* @return Returns the launchCommandTemplate.
*/
public String getLaunchCommandTemplate() {
return launchCommandTemplate;
}
/**
* @param launchCommandTemplate The launchCommandTemplate to set.
*/
public void setLaunchCommandTemplate(String launchCommandTemplate) {
this.launchCommandTemplate = launchCommandTemplate;
}
/**
* Add a value from the launch configuration to the execution
* properties map.
*
* @param executionProperties
* @param propertyName
* @param launchConfiguration
* @param launchConstant
* @throws CoreException
*/
protected void addLaunchConfigurationValue(
Map executionProperties,
String propertyName,
ILaunchConfiguration launchConfiguration,
String launchConstant)
throws CoreException
{
String configValue = launchConfiguration.getAttribute(launchConstant, (String) null);
if (configValue != null) {
executionProperties.put(propertyName, configValue);
}
}
/**
* Copy the deployed jar and jad for use during launching. Return the
* temporary folder into which the copy has been made.
*
* @param project
* @param monitor
* @return
* @throws IOException
*/
protected File copyForLaunch(IMidletSuiteProject project, IProgressMonitor monitor)
throws CoreException
{
File tempFolder = null;
try {
String folderName = project.getProject().getName().replace(' ', '_');
tempFolder = TemporaryFileManager.instance.createTempDirectory(folderName, ".launch");
IFolder projectTempFolder =
project.getProject().getFolder(IEclipseMECoreConstants.TEMP_FOLDER_NAME);
IFolder emulationFolder =
projectTempFolder.getFolder(IEclipseMECoreConstants.EMULATION_FOLDER_NAME);
File emulationDirectory = emulationFolder.getLocation().toFile();
Utils.copyDirectory(emulationDirectory, tempFolder, null);
} catch (IOException e) {
EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
}
return tempFolder;
}
/**
* Return the JAD file to use for launching from the specified temporary directory.
*
* @param midletSuite
* @param temporaryDirectory
* @param monitor
* @return
*/
protected File getJadForLaunch(IMidletSuiteProject midletSuite, File temporaryDirectory, IProgressMonitor monitor)
{
return new File(temporaryDirectory, midletSuite.getJadFile().getName());
}
/**
* Return the url specified by the user for direct launching
* or <code>null</code> if none was specified.
*
* @param launchConfiguration
* @return
* @throws CoreException
*/
protected String getSpecifiedJadURL(ILaunchConfiguration launchConfiguration)
throws CoreException
{
return launchConfiguration.getAttribute(
ILaunchConstants.SPECIFIED_JAD_URL,
(String) null);
}
/**
* Return a boolean indicating whether or not the emulation
* should just directly launch a specified JAD URL without
* the other things that normally happen during launching.
*
* @param configuration
* @return
* @throws CoreException
*/
protected boolean shouldDirectLaunchJAD(ILaunchConfiguration configuration)
throws CoreException
{
return configuration.getAttribute(
ILaunchConstants.DO_JAD_LAUNCH,
false);
}
/**
* Return a boolean indicating whether or not the emulation should
* be launched as OTA.
*
* @param configuration
* @return
* @throws CoreException
*/
protected boolean shouldDoOTA(ILaunchConfiguration configuration)
throws CoreException
{
return configuration.getAttribute(
ILaunchConstants.DO_OTA,
true);
}
/**
* Encode the specified information for a URL.
*
* @param value
* @return
*/
protected String urlEncoded(String value) {
String encoded = null;
try {
encoded = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
// Should never happen
}
return encoded;
}
/**
* Look for the library with the specified type and return it.
* If it cannot be found, return <code>null</code>
*
* @param type
* @return
*/
private ILibrary getLibraryWithType(APIType type) {
ILibrary library = null;
Classpath classpath = getClasspath();
ILibrary[] libraries = classpath.getEntries();
for (int i = 0; i < libraries.length; i++) {
if (libraries[i].getAPI(type) != null) {
library = libraries[i];
break;
}
}
return library;
}
/**
* Return the Over the Air URL for accessing the JAD file
* via the built-in OTA HTTP server.
* @param launchConfig
* @return
* @throws CoreException
*/
protected String getOTAURL(
ILaunchConfiguration launchConfig,
IMidletSuiteProject midletSuite)
throws CoreException
{
// If we are doing OTA launching, make sure that the
// OTA server has been started
try {
OTAServer.instance.start();
} catch (MultiException e) {
EclipseMECorePlugin.log(IStatus.WARNING, "launch", e);
}
String projectName = midletSuite.getProject().getName();
String jadName = midletSuite.getJadFile().getName();
StringBuffer sb = new StringBuffer();
sb
.append("http://localhost:")
.append(OTAServer.getPort())
.append("/ota/")
.append(urlEncoded(projectName))
.append('/')
.append(urlEncoded(jadName));
return sb.toString();
}
/**
* Return the classpath string to be used when launching the specified
* project.
*
* @param midletSuite
* @return
* @throws CoreException
*/
protected String getProjectClasspathString(
IMidletSuiteProject midletSuite,
File temporaryDirectory,
IProgressMonitor monitor)
throws CoreException
{
de.schlichtherle.io.File deployedJar = PreverificationBuilder.getRuntimeJar(midletSuite.getProject(), monitor);
return new File(temporaryDirectory, deployedJar.getName()).getAbsolutePath();
}
}
The table below shows all metrics for AbstractDevice.java.




