⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metadata.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 ********************************************************************
 *
 *	Filename    :   MetaData.java
 *	Package     :   eclipseme.core.model.impl
 *	System      :   eclipseme.core
 *	Author      :   Craig Setera
 *	Description :   Class for managing metadata for J2ME projects
 *	                
 * 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
 *
 * Refactored, and signature additions by Kevin Hunter, 2004
 *
 *	CVS
 *	$$Source: /cvsroot/eclipseme/eclipseme.core/src/eclipseme/core/model/impl/MetaData.java,v $$
 *	$$Author: setera $$
 *	$$Date: 2006/11/12 01:11:03 $$
 *	$$Revision: 1.15 $$
 *
 ********************************************************************
 */

package eclipseme.core.model.impl;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import eclipseme.core.EclipseMECoreErrors;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.signing.Base64EncDec;
import eclipseme.core.internal.utils.XMLUtils;
import eclipseme.core.model.ISignatureProperties;
import eclipseme.core.model.Version;
import eclipseme.core.model.device.DeviceRegistry;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.persistence.PersistenceException;
import eclipseme.core.signing.RC4Engine;
import eclipseme.core.signing.SignatureProperties;


/**
 * This class holds the metadata for the midlet suite
 * project.  This information is persisted to a file called
 * ".eclipseme" in the project's root directory.
 * <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>
 */
public class MetaData 
{
	public static final String METADATA_FILE = ".eclipseme";

	private static final String ELEM_ROOT_NAME = "eclipsemeMetadata";
	private static final String ATTR_JAD_FILE = "jad";
	
	private static final String ELEM_SIGNING = "signing";
	private static final String ATTR_SIGN_PROJECT = "signProject";
	private static final String ELEM_KEYSTORE = "keystore";
	private static final String ELEM_ALIAS = "alias";
	private static final String ELEM_PROVIDER = "provider";
	private static final String ELEM_KEYSTORETYPE = "keystoreType";
	private static final String ELEM_PASSWORDS = "passwords";
	private static final String ATTR_STOREPASSWORDS = "storePasswords";
	private static final String ELEM_PWD_KEYSTORE = "keystore";
	private static final String ELEM_PWD_KEY = "key";
	
	// Device related constants
	private static final String ELEM_DEVICE = "device";
	private static final String ATTR_DEVICEGROUP = "group";
	private static final String ATTR_DEVICENAME = "name";
	
	private static final String cryptoPass = "EclipseME";
	
	private static final String KEYRING_URL_BASE = "http://projects.eclipseme/";
	private static final String KEYRING_REALM = "projects";
	private static final String KEYRING_SCHEME = "EclipseME";
	private static final String KEYRING_KEYSTOREPASS_KEY = "KeystorePass";
	private static final String KEYRING_KEYPASS_KEY = "KeyPass";
	
	private IProject project;
	
	private Version version;
	private IDevice device;
	private String jadFile;
	private SignatureProperties signatureProps;
	
	/**
	 * Construct a new metadata object for the
	 * midlet suite project.
	 * 
	 * @param suite
	 */
	public MetaData(MidletSuiteProject suite) {
		this(suite.getJavaProject().getProject());
	}
	
	/**
	 * Construct a new metadata object for the
	 * midlet suite project.
	 * 
	 * @param suite
	 */
	public MetaData(IProject project) {
		this.project = project;
		
		try {
			loadMetaData();
		} catch (CoreException e) {
			// Failure to load the metadata, log and initialize to defaults
			EclipseMECorePlugin.log(IStatus.ERROR, "loadMetaData() failed", e);
			initializeToDefaults();
		}
	}

	/**
	 * Return the IFile instance in which the metadata is to be stored
	 * or <code>null</code> if the file is not yet available.
	 * 
	 * @return
	 */
	private IFile getStoreFile()
	{
		IFile storeFile = null;
		
		if (project != null) {
			storeFile = project.getFile(METADATA_FILE);
		}
		
		return storeFile;
	}
	
	/**
	 * Initialize the metadata to default values 
	 */
	private void initializeToDefaults() {
		signatureProps = new SignatureProperties();
	}
	
	private void loadMetaData() throws CoreException
	{
		boolean bRewrite = false;

		loadMetaDataFromFile();
		
		if (signatureProps == null)
		{
			signatureProps = new SignatureProperties();
			signatureProps.clear();
			bRewrite = true;
		}
		
		if (bRewrite)
		{
			// NOTE: Commented out for now.  The point at which this is likely to
			// happen (during startup), the workspace is locked against updates
			// and the save will fail.  (Actually the workspace refresh will fail)
//			saveMetaData();
		}
	}

	private void loadMetaDataFromFile() throws CoreException
	{
		IFile storeFile = getStoreFile();
		if ((storeFile != null) && (storeFile.exists())) {
			try
			{
				File localFile = storeFile.getLocation().toFile();
				Document document = XMLUtils.readDocument(localFile);
				if (document == null)
				{
					return;
				}
				
				Element rootElement = document.getDocumentElement();
				if (!rootElement.getNodeName().equals(ELEM_ROOT_NAME))
				{
					return;
				}
				
				version = XMLUtils.getVersion(document);
				jadFile = rootElement.getAttribute(ATTR_JAD_FILE);
				loadDevice(rootElement);
				loadSignatureProperties(rootElement);
			}
			catch(ParserConfigurationException pce)
			{
				EclipseMECorePlugin.throwCoreException(
						IStatus.WARNING,
						99999,
						pce);
			}
			catch(SAXException se)
			{
				EclipseMECorePlugin.throwCoreException(
						IStatus.WARNING,
						99999,
						se);
			}
			catch(IOException ioe)
			{
				EclipseMECorePlugin.throwCoreException(
						IStatus.WARNING,
						99999,
						ioe);
			} catch (PersistenceException e) {
				EclipseMECorePlugin.throwCoreException(
						IStatus.WARNING,
						99999,
						e);
			}
		} else {
			initializeToDefaults();
		}
	}
	
	private void loadDevice(Element rootElement) 
		throws PersistenceException 
	{
		Element deviceElement = XMLUtils.getFirstElementWithTagName(rootElement, ELEM_DEVICE);
		if (deviceElement != null) {
			String deviceGroup = deviceElement.getAttribute(ATTR_DEVICEGROUP);
			String deviceName = deviceElement.getAttribute(ATTR_DEVICENAME);
			device = DeviceRegistry.singleton.getDevice(deviceGroup, deviceName);
		}
	}

	private void loadSignatureProperties(Element rootElement) throws CoreException
	{
		SignatureProperties sp = new SignatureProperties();
		
		Element signRoot = XMLUtils.getFirstElementWithTagName(rootElement, ELEM_SIGNING);
		if (signRoot == null)
		{
			return;
		}
		
		String attr = signRoot.getAttribute(ATTR_SIGN_PROJECT);
		if (attr == null)
		{
			return;
		}
		
		if (!Boolean.valueOf(attr).booleanValue())
		{
			sp.setSignProject(false);
			signatureProps = sp;
			return;
		}
		
		sp.setSignProject(true);
		
		Element e;
		
		e = XMLUtils.getFirstElementWithTagName(signRoot, ELEM_KEYSTORE);
		if (e == null)
		{
			return;
		}
		sp.setKeyStoreDisplayPath(XMLUtils.getElementText(e));
		
		e = XMLUtils.getFirstElementWithTagName(signRoot, ELEM_ALIAS);
		if (e == null)
		{
			return;
		}
		sp.setKeyAlias(XMLUtils.getElementText(e));
		
		e = XMLUtils.getFirstElementWithTagName(signRoot, ELEM_PROVIDER);
		if (e != null)
		{
			sp.setKeyStoreProvider(XMLUtils.getElementText(e));
		}
		
		e = XMLUtils.getFirstElementWithTagName(signRoot, ELEM_KEYSTORETYPE);
		if (e != null)
		{
			sp.setKeyStoreType(XMLUtils.getElementText(e));
		}
		
		e = XMLUtils.getFirstElementWithTagName(signRoot, ELEM_PASSWORDS);
		if (e != null)
		{
			attr = e.getAttribute(ATTR_STOREPASSWORDS);
			int nMethod = ISignatureProperties.PASSMETHOD_PROMPT;
			if (attr != null)
			{
				try
				{
					nMethod = Integer.valueOf(attr).intValue();
				}
				catch(Exception ex)
				{
				}
			}
			
			Map keyMap;
			
			switch(nMethod)
			{
			case ISignatureProperties.PASSMETHOD_IN_KEYRING:
				sp.setPasswordStorageMethod(nMethod);
				keyMap = Platform.getAuthorizationInfo(getKeyringURL(), KEYRING_REALM, KEYRING_SCHEME);
				if (keyMap != null)
				{
					sp.setKeyStorePassword((String)keyMap.get(KEYRING_KEYSTOREPASS_KEY));
					sp.setKeyPassword((String)keyMap.get(KEYRING_KEYPASS_KEY));
				}
				break;
			case ISignatureProperties.PASSMETHOD_IN_PROJECT:
				sp.setPasswordStorageMethod(nMethod);
				sp.setKeyStorePassword(decodePassword(e, ELEM_PWD_KEYSTORE));
				sp.setKeyPassword(decodePassword(e, ELEM_PWD_KEY));
				break;
			case ISignatureProperties.PASSMETHOD_PROMPT:
			default:
				sp.setPasswordStorageMethod(ISignatureProperties.PASSMETHOD_PROMPT);
				sp.setKeyStorePassword(null);
				sp.setKeyPassword(null);
				break;
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -