MidletSuiteProject.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.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.J2MEClasspathContainer;
import eclipseme.core.internal.PreferenceAccessor;
import eclipseme.core.internal.preverification.builder.PreverificationBuilder;
import eclipseme.core.internal.preverifier.EmbeddedPreverifier;
import eclipseme.core.internal.utils.FilteringClasspathEntryVisitor;
import eclipseme.core.internal.utils.Utils;
import eclipseme.core.model.ApplicationDescriptor;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.IPreverifier;
import eclipseme.core.model.ISignatureProperties;
import eclipseme.core.model.SymbolDefinitionSet;
import eclipseme.core.model.SymbolDefinitionSetRegistry;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.persistence.PersistenceException;
import eclipseme.preverifier.results.PreverificationError;
/**
* Implementation of the IMidletSuiteProject interface
* providing access to midlet suite specific information.
* <p>
* <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
* change before reaching stability. It is being made available at this early stage to solicit feedback
* from pioneering adopters on the understanding that any code that uses this API will almost
* certainly be broken as the API evolves.
* </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.25 $
* <br>
* $Date: 2006/11/12 01:11:03 $
* <br>
* @author Craig Setera
*/
public class MidletSuiteProject implements IMidletSuiteProject {
/** The verified subdirectory for classes */
public static final String CLASSES_DIRECTORY = "classes";
/** The verified subdirectory for libraries */
public static final String LIBS_DIRECTORY = "libs";
// The up-to-date flag for the deployed jar file
private static final QualifiedName DEPLOYMENT_UP_TO_DATE_PROP =
new QualifiedName(IEclipseMECoreConstants.PLUGIN_ID, "deployed_up_to_date");
// Storage for the symbol definition sets
private static final QualifiedName SYMBOL_SETS_PROP =
new QualifiedName(IEclipseMECoreConstants.PLUGIN_ID, "symbol_sets");
/**
* Classpath entry visitor implementation for collecting up
* the classpath.
*/
private static class ClasspathCollectionVisitor extends FilteringClasspathEntryVisitor
{
private Set cpEntries;
private IPath classesPath;
private IPath libsPath;
/** Constructor */
private ClasspathCollectionVisitor(IPath classesPath, IPath libsPath) {
this.classesPath = classesPath;
this.libsPath = libsPath;
cpEntries = new LinkedHashSet();
}
/**
* @return Returns the cpEntries.
*/
public Set getCpEntries() {
return cpEntries;
}
/**
* @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitLibraryEntry(org.eclipse.jdt.core.IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
*/
public void visitLibraryEntry(
IClasspathEntry entry,
IJavaProject javaProject,
IProgressMonitor monitor)
throws CoreException
{
File resolvedEntry = Utils.getResolvedClasspathEntryFile(entry);
if (resolvedEntry.isFile()) {
IPath libPath = libsPath.append(entry.getPath().lastSegment());
cpEntries.add(libPath);
}
}
/**
* @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitSourceEntry(org.eclipse.jdt.core.IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
*/
public void visitSourceEntry(
IClasspathEntry entry,
IJavaProject javaProject,
IProgressMonitor monitor)
throws CoreException
{
cpEntries.add(classesPath);
}
}
/**
* Return the default JAD file name for the specified project.
*
* @param project
* @return
*/
public static String getDefaultJadFileName(IProject project) {
String projectName = project.getName();
return projectName.replace(' ', '_') + ".jad";
}
/**
* Return a boolean indicating whether the project contains the
* J2ME classpath container.
*
* @param javaProject the project to be tested
* @return whether the project has the J2ME classpath container
* @throws JavaModelException
*/
public static boolean containsJ2MEClasspathContainer(IJavaProject javaProject)
throws JavaModelException
{
boolean contains = false;
IClasspathEntry[] classpath = javaProject.getRawClasspath();
for (int i = 0; i < classpath.length; i++) {
IClasspathEntry entry = classpath[i];
if ((entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) &&
(entry.getPath().segment(0).equals(J2MEClasspathContainer.J2ME_CONTAINER)))
{
contains = true;
break;
}
}
return contains;
}
// The metadata about this project
private MetaData metaData;
// The java project on which this midlet suite is based
private IJavaProject javaProject;
private String tempKeystorePassword;
private String tempKeyPassword;
/**
* Private constructor for singleton instances of the
* class.
*/
public MidletSuiteProject(IJavaProject javaProject) {
super();
this.javaProject = javaProject;
initializeMetadata();
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getApplicationDescriptor()
*/
public ApplicationDescriptor getApplicationDescriptor() {
ApplicationDescriptor descriptor = null;
IFile jadFile = getJadFile();
if (!jadFile.exists()) {
// Create JAD file
}
try {
File jFile = jadFile.getLocation().toFile();
descriptor = new ApplicationDescriptor(jFile);
} catch (IOException e) {
EclipseMECorePlugin.log(IStatus.ERROR, "getApplicationDescriptor", e);
}
return descriptor;
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#createPackage(org.eclipse.core.runtime.IProgressMonitor, boolean)
*/
public void createPackage(IProgressMonitor monitor, boolean obfuscate)
throws CoreException
{
Map args = new HashMap(2);
args.put(PreverificationBuilder.ARG_DO_PACKAGE, Boolean.TRUE);
args.put(PreverificationBuilder.ARG_DO_OBFUSCATION, Boolean.valueOf(obfuscate));
args.put(PreverificationBuilder.ARG_UPDATE_VERSION, Boolean.TRUE);
PreverificationBuilder.cleanProject(getProject(), true, monitor);
this.getProject().build(
IncrementalProjectBuilder.FULL_BUILD,
IEclipseMECoreConstants.J2ME_PREVERIFIER_ID,
args,
monitor);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getClasspath(org.eclipse.core.runtime.IProgressMonitor)
*/
public File[] getClasspath(IProgressMonitor monitor)
throws CoreException
{
// The verified output locations
IPath classesPath = getVerifiedClassesOutputFolder(monitor).getLocation();
IPath libsPath = getVerifiedLibrariesOutputFolder(monitor).getLocation();
// Build up the list of IPath entries based on the project
// classpath setup to maintain relative ordering
ClasspathCollectionVisitor visitor =
new ClasspathCollectionVisitor(classesPath, libsPath);
visitor.getRunner().run(javaProject, visitor, monitor);
int index = 0;
Set cpEntries = visitor.getCpEntries();
File[] entries = new File[cpEntries.size()];
Iterator iterator = cpEntries.iterator();
while (iterator.hasNext()) {
IPath path = (IPath) iterator.next();
entries[index++] = path.toFile();
}
return entries;
}
/**
* Return the device referenced by this project.
*/
public IDevice getDevice() {
return metaData.getDevice();
}
public ISignatureProperties getSignatureProperties()
throws CoreException
{
return(metaData.getSignatureProperties());
}
public void setSignatureProperties(ISignatureProperties props)
{
metaData.setSignatureProperties(props);
}
public void saveMetaData()
throws CoreException
{
// Don't save the metadata if this is a preprocessed project,
// as it belongs to the base project
if (!isPreprocessedProject()) {
metaData.saveMetaData();
}
}
public String getTempKeystorePassword()
{
return(tempKeystorePassword);
}
public void setTempKeystorePassword(String pass)
{
tempKeystorePassword = pass;
}
public String getTempKeyPassword()
{
return(tempKeyPassword);
}
public void setTempKeyPassword(String pass)
{
tempKeyPassword = pass;
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getJadFile()
*/
public IFile getJadFile() {
IFile jadFile = null;
IProject project = javaProject.getProject();
String filename = getMetaData().getJadFile();
if ((filename == null) || (filename.length() == 0)) {
String defaultName = getDefaultJadFileName(project);
jadFile = project.getFile(defaultName);
} else {
jadFile = project.getFile(filename);
}
return jadFile;
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getJarFilename()
*/
public String getJarFilename() {
String filename = null;
String jarUrl = getApplicationDescriptor().getMidletJarURL();
if (jarUrl != null) {
// If this is really a URL, we want to just get the name
// from the end of the URL.
int lastSlashIndex = jarUrl.lastIndexOf('/');
if (lastSlashIndex != -1) {
filename = jarUrl.substring(lastSlashIndex + 1);
} else {
filename = jarUrl;
}
}
if (filename == null) {
filename = getProjectNameWithoutSpaces() + ".jar";
}
return filename;
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getJavaProject()
*/
public IJavaProject getJavaProject() {
return javaProject;
}
/**
* Return the metadata for this midlet suite.<br/>
* NOTE: This method is not considered part of the API and
* is only available to aid the implementation.
*
* @return the metadata.
*/
public MetaData getMetaData() {
return metaData;
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getProject()
*/
public IProject getProject() {
return javaProject.getProject();
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getVerifiedClassesOutputFolder(org.eclipse.core.runtime.IProgressMonitor)
*/
public IFolder getVerifiedClassesOutputFolder(IProgressMonitor monitor)
throws CoreException
{
return getVerifiedOutputFolder(monitor).getFolder(CLASSES_DIRECTORY);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getVerifiedLibrariesOutputFolder(org.eclipse.core.runtime.IProgressMonitor)
*/
public IFolder getVerifiedLibrariesOutputFolder(IProgressMonitor monitor)
throws CoreException
{
return getVerifiedOutputFolder(monitor).getFolder(LIBS_DIRECTORY);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getVerifiedOutputFolder(org.eclipse.core.runtime.IProgressMonitor)
*/
public IFolder getVerifiedOutputFolder(IProgressMonitor monitor)
throws CoreException
{
IFolder tempFolder = getProject().getFolder(IEclipseMECoreConstants.TEMP_FOLDER_NAME);
return tempFolder.getFolder(IEclipseMECoreConstants.VERIFIED_FOLDER_NAME);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#isDeployedJarUpToDate()
*/
public boolean isDeployedJarUpToDate() throws CoreException {
String upToDateString = getProject().getPersistentProperty(DEPLOYMENT_UP_TO_DATE_PROP);
return (upToDateString == null) ? false : upToDateString.equals("true");
}
/**
* Return a boolean indicating whether the underlying project is a preprocessed project.
*
* @return
* @throws CoreException
*/
public boolean isPreprocessedProject()
throws CoreException
{
IProject project = getProject();
return
project.exists() &&
project.isOpen() &&
project.hasNature(IEclipseMECoreConstants.J2ME_PREPROCESSED_NATURE_ID);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#preverify(org.eclipse.core.resources.IResource[], org.eclipse.core.resources.IFolder, org.eclipse.core.runtime.IProgressMonitor)
*/
public PreverificationError[] preverify(IResource[] toVerify, IFolder outputFolder, IProgressMonitor monitor)
throws CoreException, IOException
{
return getPreverifier().preverify(
this,
toVerify,
outputFolder,
monitor);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#preverifyJarFile(java.io.File, org.eclipse.core.resources.IFolder, org.eclipse.core.runtime.IProgressMonitor)
*/
public PreverificationError[] preverifyJarFile(File jarFile, IFolder outputFolder, IProgressMonitor monitor) throws CoreException, IOException {
return getPreverifier().preverifyJarFile(
this,
jarFile,
outputFolder,
monitor);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#setDeployedJarFileUpToDate(boolean)
*/
public void setDeployedJarFileUpToDate(boolean upToDate) throws CoreException {
String value = upToDate ? "true" : "false";
getProject().setPersistentProperty(DEPLOYMENT_UP_TO_DATE_PROP, value);
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#setDevice(eclipseme.core.model.device.IDevice, org.eclipse.core.runtime.IProgressMonitor)
*/
public void setDevice(IDevice device, IProgressMonitor monitor)
throws CoreException
{
if ((metaData.getDevice() == null) || (!metaData.getDevice().equals(device))) {
metaData.setDevice(device);
// Make sure that we have a place to put the
// classpath updates
addClasspathContainerIfMissing(monitor);
// Reset the classpath container for the project so the
// initializer will be called again
JavaCore.setClasspathContainer(
new Path(J2MEClasspathContainer.J2ME_CONTAINER),
new IJavaProject[] { getJavaProject() },
new IClasspathContainer[] { null },
monitor);
// Force a rebuild
getProject().build(IncrementalProjectBuilder.FULL_BUILD, monitor);
}
}
/**
* @throws CoreException
* @see eclipseme.core.model.IMidletSuiteProject#setJadFileLocation(org.eclipse.core.runtime.IPath)
*/
public void setJadFileLocation(IPath projectRelativePath)
throws CoreException
{
getMetaData().setJadFile(projectRelativePath.toString());
saveMetaData();
}
/**
* Add the J2ME classpath container to the java project
* we wrap if it is currently missing. This step provides
* migration for projects created with earlier releases
* of EclipseME as well as providing a means to fix
* projects that have lost their platform definition
* association.
*
* @param monitor
* @throws JavaModelException
*/
private void addClasspathContainerIfMissing(IProgressMonitor monitor)
throws CoreException
{
boolean hasClasspathContainer =
containsJ2MEClasspathContainer(getJavaProject());
boolean hasPreprocessingNature = isPreprocessedProject();
if (!hasClasspathContainer && !hasPreprocessingNature) {
// Create a new classpath entry for the classpath
// container
IPath entryPath =
new Path(J2MEClasspathContainer.J2ME_CONTAINER);
IClasspathEntry newEntry =
JavaCore.newContainerEntry(entryPath);
// Get the current classpath entries
IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
Set currentClasspath = new LinkedHashSet(rawClasspath.length);
for (int i = 0; i < rawClasspath.length; i++) {
IClasspathEntry entry = rawClasspath[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
entry = JavaCore.getResolvedClasspathEntry(entry);
}
currentClasspath.add(entry);
}
// The classpath entries that are provided by
// the platform definition currently
IClasspathEntry[] platformEntries =
getDevice().getClasspath().asClasspathEntries();
// Remove the classpath entries from the project
// if they are provided by the platform definition
for (int i = 0; i < platformEntries.length; i++) {
IClasspathEntry entry = platformEntries[i];
if (currentClasspath.contains(entry)) {
currentClasspath.remove(entry);
}
}
// Set the updated classpath
currentClasspath.add(newEntry);
IClasspathEntry[] newClasspath =
(IClasspathEntry[]) currentClasspath.toArray(
new IClasspathEntry[currentClasspath.size()]);
javaProject.setRawClasspath(newClasspath, monitor);
}
}
/**
* Return the preverifier to use for resources in this project.
*/
private IPreverifier getPreverifier() {
IPreverifier preverifier = null;
if (isEmbeddedPreverifierEnabled()) {
preverifier = new EmbeddedPreverifier();
} else {
IDevice device = getDevice();
preverifier = (device == null) ?
new EmbeddedPreverifier() : device.getPreverifier();
}
return preverifier;
}
/**
* Get the project name, replacing spaces with underscores.
*
* @return
*/
private String getProjectNameWithoutSpaces()
{
String projectName = javaProject.getProject().getName();
return projectName.replace(' ', '_');
}
/**
* Return a boolean indicating whether or not to use the embedded preverifier.
*
* @param project
* @return
*/
private boolean isEmbeddedPreverifierEnabled() {
return PreferenceAccessor.instance.getUseBuiltInPreverifier(getProject());
}
/**
* Initialize the project metadata.
*/
private void initializeMetadata() {
try {
if (isPreprocessedProject()) {
ICommand preverifierCommand = null;
ICommand[] buildCommands = getProject().getDescription().getBuildSpec();
for (int i = 0; i < buildCommands.length; i++) {
ICommand command = buildCommands[i];
if (command.getBuilderName().equals(IEclipseMECoreConstants.J2ME_PREVERIFIER_ID)) {
preverifierCommand = command;
break;
}
}
if (preverifierCommand != null) {
// Pull out the target project and use its metadata
String targetProjectName =
(String) preverifierCommand.getArguments().get(IPreverifier.BUILD_ARG_PREVERIFY_TARGET);
IProject targetProject =
ResourcesPlugin.getWorkspace().getRoot().getProject(targetProjectName);
if (targetProject != null) {
metaData = new MetaData(targetProject);
}
}
}
} catch (CoreException e) {
EclipseMECorePlugin.log(IStatus.ERROR, e);
}
if (metaData == null) {
metaData = new MetaData(this);
}
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#getEnabledSymbolDefinitionSet()
*/
public SymbolDefinitionSet getEnabledSymbolDefinitionSet()
throws CoreException, PersistenceException
{
SymbolDefinitionSet set = null;
String setName = getProject().getPersistentProperty(SYMBOL_SETS_PROP);
if (setName != null) {
set = SymbolDefinitionSetRegistry.singleton.getSymbolDefinitionSet(setName);
}
return set;
}
/**
* @see eclipseme.core.model.IMidletSuiteProject#setEnabledSymbolDefinitionSet(eclipseme.core.model.SymbolDefinitionSet)
*/
public void setEnabledSymbolDefinitionSet(SymbolDefinitionSet set)
throws CoreException, PersistenceException
{
String setName = null;
if (set != null) {
setName = set.getName();
}
getProject().setPersistentProperty(SYMBOL_SETS_PROP, setName);
}
}
The table below shows all metrics for MidletSuiteProject.java.




