Initial commit from SVN.

This commit is contained in:
wcrisman
2014-05-30 10:31:51 -07:00
commit b45e56b890
1968 changed files with 370949 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 2008,2009 Declarative Engineering LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Declarative Engineering LLC
* verson 1 which accompanies this distribution, and is available at
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
*/
package com.declarativeengineering.builder;
import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.HashMap;
import com.common.debug.Debug;
import com.common.debug.DefaultLog;
import com.common.util.LiteList;
import com.foundation.view.builder.IVmlBuilder;
/**
* This is a simple interface for the plugin to interact with the VML builder code that is distributed with the framework.
* <p>Note: The caller of the VML builder must create a new instance for each build thread. The instance should be released when the full build is complete so that application classes can be modified by the user.</p>
*/
public class VmlBuilder implements IVmlBuilder {
private VmlBuilderClassLoader classLoader = null;
/**
* VmlBuilder constructor.
*/
public VmlBuilder() {
super();
}//VmlBuilder()//
/**
* Builds a vml file.
* <p><b>Warning: This is not a thread safe method (and can't easily be made so due to the Debug Log).</b></p>
* @param vmlStream The input stream containing the source VML being built.
* @param builderPath The path to the builder jar containing the core code base for the VML builder.
* @param componentTypePaths The directories containing the VML component files, one directory per component (usually).
* @param packageName The package name for the generated VML/Java source code.
* @param applicationClassPaths The class paths known by the application whose VML file is being built. This is used to load class metadata while building the java source from the VML.
* @param results The mapping of results by their identifiers (to allow for more of a separation between the framework based builder code and the plugin).
*/
public void build(InputStream vmlStream, File builderPath, File[] componentTypePaths, String packageName, File[] applicationClassPaths, final HashMap results) {
final StringBuffer log = new StringBuffer(10000);
Debug.setLog(new DefaultLog() {
public void log(String note, Throwable exception, boolean forceDisplay) {
if(note != null) {
log.append(note);
log.append("\r\n");
}//if//
if(exception != null) {
StringWriter stringWriter = new StringWriter(1000);
PrintWriter printWriter = new PrintWriter(stringWriter);
exception.printStackTrace(printWriter);
log.append(stringWriter.toString());
log.append("\r\n");
}//if//
}//log()//
});
if(classLoader == null) {
if(getClass().getName().startsWith("com")) {
classLoader = new VmlBuilderClassLoader(builderPath, componentTypePaths, LiteList.EMPTY_LIST, LiteList.EMPTY_LIST, null);
}//if//
else {
//TODO: Pass security...
//Warning: If the included/excluded packages are modified they might also need modifying in the ant build for the builder.jar, and JetsonApplication, and SprocketApplication (both in the deployment workspace).//
LiteList includedPackages = new LiteList(new String[] {"com/de22", "com/common", "com/foundation", "com/declarativeengineering"});
//LiteList includedPackages = new LiteList(new String[0]);
LiteList excludedPackages = new LiteList(new String[] {"com/foundation/view/swt/builder", "com/foundation/tcv/swt/builder", "com/declarativeengineering/jetson", "com/foundation/de22", "com/foundation/novocode", "com/de22/swt"});
classLoader = new VmlBuilderClassLoader(builderPath, componentTypePaths, excludedPackages, includedPackages, null);
}//else//
}//if//
//Note: We are using reflection here because we need to load the VmlBuilderStaging class in a different class loader that will create the zjar's from the control jars in all the control directories.//
//Warning: We have to do this again in the VmlBuilderStaging class to demangle the java source that is generated.//
try {
Object staging = Class.forName("com.declarativeengineering.builder.VmlBuilderStaging", true, classLoader).newInstance();
Method buildMethod = null;
//Try the unmangled name first.//
// DEBUG
try {
buildMethod = staging.getClass().getMethod("build", new Class[] {InputStream.class, File[].class, String.class, File[].class, HashMap.class});
}//try//
catch(Throwable e) {
//Debug.log(e);
}//catch//
if(buildMethod == null) {
try {
buildMethod = staging.getClass().getMethod("ec06440700069006", new Class[] {InputStream.class, File[].class, String.class, File[].class, HashMap.class});
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
}//if//
buildMethod.invoke(staging, new Object[] {vmlStream, componentTypePaths, packageName, applicationClassPaths, results});
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
//Place the log output into the results mapping.//
results.put(RESULT_LOG, log.toString());
}//build()//
}//VmlBuilder//

View File

@@ -0,0 +1,358 @@
/*
* Copyright (c) 2003,2009 Declarative Engineering LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Declarative Engineering LLC
* verson 1 which accompanies this distribution, and is available at
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
*/
package com.declarativeengineering.builder;
import java.io.*;
import java.util.zip.*;
import java.util.jar.*;
import com.common.comparison.Comparator;
import com.common.debug.Debug;
import com.common.io.StreamSupport;
import com.common.io.SymmetricInputStream;
import com.common.security.ISymmetricAlgorithm;
import com.common.util.*;
import com.de22.obfuscation.RuntimeMangler;
import com.foundation.util.xml.*;
import com.foundation.view.builder.ViewBuilderException;
import com.foundation.view.definition.ComponentType;
import com.foundation.view.definition.IComponentType;
import com.foundation.view.definition.TypeDefinitionException;
/**
* Used to load the view component builders only.
*/
public class VmlBuilderClassLoader extends ClassLoader {
public static final String TYPE_FILE_EXTENSION = ".cml"; //Control Modeling Language.//
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
/** The path to the builder code. */
private File builderJar = null;
/** The path to the jars containing builders for specific components. */
private File[] jars = null;
/** The mapping of unmangled class names indexed by mangled class names, used to reverse mangling in the generated java source. */
private IHashMap mangleMap = new LiteHashMap(40);
/** The mangler used to generate the zjar files. */
private RuntimeMangler mangler = null;
/** The optional algorithm to use to encrypt/decrypt the zjar entries. */
private ISymmetricAlgorithm algorithm = null;
/**
* VmlBuilderClassLoader constructor.
* @param codeBaseJarPath The path to the jar containing the core code base for the VML builder.
* @param componentPaths The paths to the component directories where the component jars will be kept.
* @param excludedPackages The collection of packages not to be included for any reason in the mangling (includes subpackages). (example: com/myproject/donotmangle)
* @param includedPackages The collection of packages to include in the mangling if not explicitly excluded (includes subpackages). (example: com/myproject/domangle)
* @param algorithm The optional algorithm used to encrypt the zjar contents.
*/
public VmlBuilderClassLoader(File codeBaseJarPath, File[] componentPaths, IList excludedPackages, IList includedPackages, ISymmetricAlgorithm algorithm) {
super(null);
long ts = System.currentTimeMillis();
LiteList jars = new LiteList(componentPaths.length, 20);
File[] jarFiles = null;
LiteHashSet jarSet = new LiteHashSet(40, LiteHashSet.DEFAULT_LOAD_FACTOR, Comparator.getLogicalComparator(), LiteHashSet.STYLE_NO_DUPLICATES);
System.out.println("Initializing VmlBuilderClassLoader");
this.builderJar = codeBaseJarPath;
jarSet.add(codeBaseJarPath.getName());
//Note: The control directories are the folders containing component jars. Only one jar of a perticular name will be allowed for any set of control directories.//
//Collect all the jars in the control directories.//
for(int controlDirectoryIndex = 0; controlDirectoryIndex < componentPaths.length; controlDirectoryIndex++) {
File nextControlDirectory = componentPaths[controlDirectoryIndex];
File[] files = nextControlDirectory.listFiles();
for(int fileIndex = 0; fileIndex < files.length; fileIndex++) {
File nextFile = files[fileIndex];
if(nextFile.isFile() && nextFile.canRead() && nextFile.getName().endsWith(".jar") && !jarSet.containsValue(nextFile.getName()) && !nextFile.getName().equalsIgnoreCase("builder.jar")) {
jars.add(nextFile);
jarSet.add(nextFile.getName());
}//if//
}//for//
}//for//
//Mangle the component jars (generating zjar files).//
jars.toArray(jarFiles = new File[jars.getSize()]);
/* DEBUG
try {
Debug.log("Loading Jars: ");
for(int index = 0; index < jarFiles.length; index++) {
Debug.log(jarFiles[index].getCanonicalPath());
}//for//
Debug.log("End Jars");
}catch(Throwable e) {Debug.log(e);}
*/
mangler = new RuntimeMangler(new File[] {codeBaseJarPath}, excludedPackages, includedPackages, mangleMap);
try {
mangler.mangleJars(jarFiles, algorithm);
}//try//
catch(Throwable e) {
Debug.log(e, "Failed to pre-mangle the VML component jars.");
}//catch//
this.jars = new File[jarFiles.length];
//Convert the paths to the jars to paths to the zjars.//
for(int index = 0; index < jarFiles.length; index++) {
String path = jarFiles[index].getAbsolutePath();
this.jars[index] = new File(path.substring(0, path.length() - 3) + "zjar");
}//for//
System.out.println("Finished Initializing VmlBuilderClassLoader taking " + (System.currentTimeMillis() - ts) + "ms.");
}//VmlBuilderClassLoader()//
/**
* Removes mangled class names from the java source.
* @param source The java source potentially containing mangled class names.
* @return The source without the mangled class names.
*/
public String demangleJavaSource(String source) {
//TODO: Make this non-static in the mangler - and remove the need to have an external mangle map.
return mangler.demangleSource(source, mangleMap);
}//demangleJavaSource()//
/* (non-Javadoc)
* @see java.lang.ClassLoader#findClass(java.lang.String)
*/
protected Class findClass(String className) throws ClassNotFoundException {
Class type = null;
String classFileName = className.replace('.', '/') + ".class";
//Debug.log("Got to builder.jar's VmlBuilderClassLoader. Looking for class: " + className);
/*
try {
type = super.findClass(className);
}//try//
catch(ClassNotFoundException e) {
//Ignore.//
}//catch//
*/
if(type == null) {
type = findClass(builderJar, className, classFileName);
for(int index = 0; type == null && index < jars.length; index++) {
if(jars[index].exists() && jars[index].canRead()) {
type = findClass(jars[index], className, classFileName);
}//if//
}//while//
}//if//
if(type == null) {
throw new ClassNotFoundException("Couldn't find the class: " + className);
}//if//
return type;
}//findClass()//
/**
* Finds the given class by searching the given file or directory.
* @param file The file or directory to search.
* @param classFileName The name of the class to find.
*/
private Class findClass(File file, String className, String classFileName) throws ClassNotFoundException {
Class type = null;
if(file.exists()) {
byte[] bytes = null;
try {
String path = file.getCanonicalPath();
if(path.endsWith(".jar")) {
JarFile jar = new JarFile(file, false, JarFile.OPEN_READ);
try {
JarEntry entry = jar.getJarEntry(classFileName);
if(entry != null) {
InputStream in = jar.getInputStream(entry);
bytes = StreamSupport.readBytes(in, in.available());
in.close();
}//if//
}//try//
finally {
if(jar != null) {
jar.close();
}//if//
}//finally//
}//if//
else if(path.endsWith(".zjar")) {
JarFile jar = new JarFile(file, false, JarFile.OPEN_READ);
try {
JarEntry entry = jar.getJarEntry(classFileName);
if(entry != null) {
InputStream in = jar.getInputStream(entry);
bytes = StreamSupport.readBytes(in, in.available());
in.close();
//This reads in the bytes after decrypting and decompressing them - see RuntimeMangler for the code that creates the zjar.//
if(algorithm != null) {
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
SymmetricInputStream sin = new SymmetricInputStream(bin, algorithm);
ZipInputStream zin = new ZipInputStream(sin);
bytes = StreamSupport.readBytes(zin, bytes.length * 2);
}//if//
}//if//
}//try//
finally {
if(jar != null) {
jar.close();
}//if//
}//finally//
}//else if//
if(bytes != null) {
type = defineClass(className, bytes, 0, bytes.length);
}//if//
}//try//
catch(IOException e) {
throw new ViewBuilderException(e, -1, -1, -1);
}//catch//
}//if//
return type;
}//findClass()//
/**
* Loads component type objects from all *.cml files in the given paths.
* The result collection will be filled with ComponentType instances indexed by the component type's name (String instances).
* @param paths The directories or specific *.cml files to be loaded. Subdirectories will not be searched.
* @param results The optional collection in which to place the resulting ComponentType instances.
* @return The result mapping of component type instances indexed by their names. This will be the same object as the results parameter if results is not null.
*/
private static IHashMap loadComponentTypes(java.io.File[] paths, IHashMap results) {
LiteHashSet searchedPaths = new LiteHashSet(20);
if(results == null) {
results = new LiteHashMap(100);
}//if//
for(int pathIndex = 0; pathIndex < paths.length; pathIndex++) {
java.io.File path = paths[pathIndex];
if(path.exists()) {
if(path.isDirectory()) {
String canonicalPath = null;
try {
path.getCanonicalPath();
}//try//
catch(Throwable e) {
//Ignore.//
}//catch//
if(canonicalPath == null || !searchedPaths.containsValue(canonicalPath)) {
java.io.File[] files = path.listFiles(new java.io.FilenameFilter() {
public boolean accept(File directory, String name) {
return name.endsWith(TYPE_FILE_EXTENSION);
}//accept()//
});
searchedPaths.add(canonicalPath);
for(int fileIndex = 0; fileIndex < files.length; fileIndex++) {
loadComponentType(files[fileIndex], results);
}//for//
}//if//
}//if//
else if((path.isFile()) && (path.getName().endsWith(TYPE_FILE_EXTENSION))) {
loadComponentType(path, results);
}//else if//
else {
//Ignore.//
}//else//
}//if//
}//if//
initializeComponentTypes(results);
return results;
}//loadComponentTypes()//
/**
* Loads component type objects from all *.cml files in the given paths.
* @param path The file containing component type information.
* @param results The collection in which to place the resulting ComponentType instance.
*/
private static void loadComponentType(File file, IHashMap types) {
FileInputStream in = null;
try {
DocumentBuilder builder = new DocumentBuilder(" while parsing the file: " + file.getAbsolutePath());
IDocument document = null;
IIterator iterator = null;
in = new FileInputStream(file);
builder.setIgnoreComments(Boolean.TRUE);
builder.setIgnoreTextWhitespace(Boolean.TRUE);
document = builder.readDocument(in, null);
iterator = document.getElements().iterator();
while(iterator.hasNext()) {
IElement element = (IElement) iterator.next();
if((element.isNode()) && (((INode) element).getName().equalsIgnoreCase(IComponentType.NODE_NAME))) {
ComponentType componentType = new ComponentType();
try {
componentType.readXml((INode) element);
//Don't add duplicate types.//
if(!types.containsKey(componentType.getName())) {
types.put(componentType.getName(), componentType);
//Update the path to the builder code library.//
if((componentType.getBuilderDefinition() != null) && (componentType.getBuilderDefinition().getCodeLocation() != null)) {
componentType.getBuilderDefinition().setCodeLocation(new File(file.getParent(), componentType.getBuilderDefinition().getCodeLocation()).getAbsolutePath());
}//if//
}//if//
else {
//Don't throw an exception because this may not be an error.//
//throw new ViewBuilderException("Found duplicate component type: " + componentType.getName(), -1, -1, -1);
}//else//
}//try//
catch(ViewBuilderException e) {
throw e;
}//catch//
catch(Throwable e) {
Debug.log(e);
throw new ViewBuilderException(e, -1, -1, -1);
}//catch//
}//if//
}//while//
}//try//
catch(IOException e) {
throw new ViewBuilderException(e, -1, -1, -1);
}//catch//
finally {
if(in != null) {
try {in.close();} catch(IOException e) {} //Ignore the exception.//
}//if//
}//finally//
}//loadComponentType()//
/**
* Initializes each of the component types once all types have been loaded.
* @param types The mapping of component types by the unique type name.
*/
private static void initializeComponentTypes(IHashMap types) {
IIterator iterator = types.getKeys(new LiteList(types.getSize())).iterator();
while(iterator.hasNext()) {
String typeName = (String) iterator.next();
try {
((ComponentType) types.get(typeName)).initialize(types);
}//try//
catch(TypeDefinitionException e) {
types.remove(typeName);
throw new ViewBuilderException(e, -1, -1, -1);
}//catch//
}//while//
}//initializeComponentTypes()//
}//BuilderClassLoader//

View File

@@ -0,0 +1,156 @@
/*
* Copyright (c) 2008,2009 Declarative Engineering LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Declarative Engineering LLC
* verson 1 which accompanies this distribution, and is available at
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
*/
package com.declarativeengineering.builder;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import com.common.debug.Debug;
import com.common.util.IList;
import com.common.util.LiteList;
import com.foundation.view.builder.BuildFailedException;
import com.foundation.view.builder.ClassMetadataLoader;
import com.foundation.view.builder.IClassMetadataLoader;
import com.foundation.view.builder.IVmlBuilder;
import com.foundation.view.builder.ViewBuilderException;
import com.foundation.view.builder.ViewSourceBuilder;
import com.foundation.view.definition.Message;
import com.foundation.view.definition.ViewModel;
public class VmlBuilderStaging implements IVmlBuilder {
private IClassMetadataLoader loader = null;
/**
* VmlBuilderStaging constructor.
*/
public VmlBuilderStaging() {
}//VmlBuilderStaging()//
/**
* Builds a vml file.
* @param vmlStream The input stream containing the source VML being built.
* @param componentTypePaths The directories containing the VML component files, one directory per component (usually).
* @param packageName The package name for the generated VML/Java source code.
* @param applicationClassPaths The class paths known by the application whose VML file is being built. This is used to load class metadata while building the java source from the VML.
* @param results The mapping of results by their identifiers (to allow for more of a separation between the framework based builder code and the plugin).
*/
public void build(InputStream vmlStream, File[] componentTypePaths, String packageName, File[] applicationClassPaths, final HashMap results) {
String source = null;
IList messageStack = new LiteList(10, 100);
ArrayList messages;
//Initialize and operate the view builder to generate the source and package name.//
try {
ViewSourceBuilder viewBuilder = null;
ViewModel viewModel = null;
if(loader == null) {
loader = new ClassMetadataLoader(applicationClassPaths);
}//if//
//Initialize the view model and builder.//
viewModel = new ViewModel(loader, messageStack) {
protected void storeViewData(byte[] document) {
//TODO: Place the modified VML in the results mapping.
results.put(RESULT_VML_SOURCE, document);
}//storeViewData()//
};
viewModel.initialize(ViewModel.loadComponentTypes(componentTypePaths, null), vmlStream, packageName);
if(!hasErrors(messageStack)) {
viewBuilder = new ViewSourceBuilder(viewModel, loader);
//Get the view's java source code.//
source = viewBuilder.getViewSource(null);
//Get the package name we are to use for the source.//
packageName = viewModel.getViewMetadata().getPackage();
}//if//
}//try//
catch(ViewBuilderException e) {
messageStack.add(new Message(true, null, e, e.getLineNumber(), e.getCharacterStart(), e.getCharacterEnd()));
}//catch//
catch(BuildFailedException e) {
//Ignore.//
}//catch//
catch(Throwable e) {
messageStack.add(new Message(true, null, e));
while(e.getCause() != null) {
e = e.getCause();
messageStack.add(new Message(true, "Caused by:", e));
}//while//
}//catch//
//Place the messages into the results mapping.//
messages = new ArrayList(messageStack.getSize());
for(int index = 0; index < messageStack.getSize(); index++) {
Message message = (Message) messageStack.get(index);
HashMap messageMap = new HashMap(10);
messageMap.put(RESULT_MESSAGE_TEXT, message.getMessage());
messageMap.put(RESULT_MESSAGE_EXCEPTION, message.getException());
messageMap.put(RESULT_MESSAGE_START_CHARACTER, new Integer(message.getStartCharacter()));
messageMap.put(RESULT_MESSAGE_END_CHARACTER, new Integer(message.getEndCharacter()));
messageMap.put(RESULT_MESSAGE_LINE_NUMBER, new Integer(message.getLineNumber()));
messageMap.put(RESULT_MESSAGE_IS_ERROR, new Boolean(message.isError()));
//log.append("VML Build Message [text: " + message.getMessage() + " |isError: " + message.isError() + "]\n");
messages.add(messageMap);
}//for//
//Write the source code to the java file.//
if((source != null) && (source.length() > 0) && (packageName != null)) {
Method method = null;
// DEBUG
try {
method = getClass().getClassLoader().getClass().getMethod("demangleJavaSource", new Class[] {String.class});
}//try//
catch(Throwable e) {
//Debug.log(e);
}//catch//
if(method == null) {
try {
method = getClass().getClassLoader().getClass().getMethod("e7300af2217feb13", new Class[] {String.class});
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
}//if//
try {
source = (String) method.invoke(getClass().getClassLoader(), new Object[] {source});
}//try//
catch(Throwable e) {
Debug.log(e, "Caught while de-mangling the java source.");
}//catch//
}//if//
//Place the compiler messages into the results mapping.//
results.put(RESULT_MESSAGES, messages);
//Place the java source into the results mapping.//
results.put(RESULT_JAVA_SOURCE, source);
//Place the package name into the results mapping.//
results.put(RESULT_PACKAGE_NAME, packageName);
}//build()//
/**
* Tests the message stack for errors.
* @param messageStack The collection of compiler messages.
* @return Whether any of the messages are errors.
*/
private boolean hasErrors(IList messageStack) {
for(int index = 0; index < messageStack.getSize(); index++) {
if(((Message) messageStack.get(index)).isError()) {
return true;
}//if//
}//for//
return false;
}//hasErrors()//
}//VmlBuilderStaging//