Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
initial commit for GM version 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
iadgovadmin committed Jan 28, 2016
1 parent 01da9b2 commit e12732a
Show file tree
Hide file tree
Showing 1,234 changed files with 436,037 additions and 0 deletions.
45 changes: 45 additions & 0 deletions FPC4/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="FPC4" default="all" basedir=".">
<property name="class.dir" value="build/classes"/>
<property name="src.dir" value="src"/>
<property name="lib.dir" value="lib"/>
<property name="dist" value="dist"/>
<property name="compile.debug" value="false"/>

<target name="compile">
<mkdir dir="${class.dir}"/>
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${class.dir}" verbose="true" debug="${compile.debug}">

<classpath>
<pathelement path="lib/antlr-4.5-complete.jar"/>
<pathelement path="lib/ICSDefines.jar"/>
</classpath>

</javac>
<copy todir="${class.dir}">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>

<target name="jar" depends="compile">
<mkdir dir="${dist}"/>
<jar destfile="${dist}/${ant.project.name}.jar" basedir="${class.dir}">
<manifest>
<attribute name="FPC-Class" value="FPC"/>
<attribute name="Permission" value="High"/>
</manifest>
<zipgroupfileset dir="${lib.dir}" includes="**/*.jar"/>
</jar>
</target>

<target name="run" depends="compile,jar">
<java jar="${dist}/${ant.project.name}.jar" fork="true"/>
</target>

<target name="all" depends="compile, jar, run"/>

<target name="update-libs">
<copy file="../ICSDefines/dist/ICSDefines.jar" todir="${lib.dir}" />
</target>

</project>
13 changes: 13 additions & 0 deletions FPC4/fpc4.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="antlr-4.5-complete" level="project" />
<orderEntry type="library" name="ICSDefines" level="project" />
</component>
</module>
1 change: 1 addition & 0 deletions FPC4/generate_sources.BAT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xjc.exe .\src\TemplateEngine\Template4\fingerprint3.xsd -p TemplateEngine.Fingerprint3 -d src
1 change: 1 addition & 0 deletions FPC4/lib/Required_libs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
antlr-4.5-complete
43 changes: 43 additions & 0 deletions FPC4/src/TemplateEngine/Compiler/CompiledCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package TemplateEngine.Compiler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import javax.tools.SimpleJavaFileObject;

/**
* Created by BESTDOG on 11/23/2015.
*
* <pre>
* Buffers compiled code in memory while behaving like a FileObject to the JavaCompiler.
* </pre>
*/
public class CompiledCode extends SimpleJavaFileObject {

ByteArrayOutputStream os;

public CompiledCode(String className) throws URISyntaxException {
super( new URI(className), Kind.CLASS );
os = new ByteArrayOutputStream();
}

@Override
public OutputStream openOutputStream() throws IOException {
return os;
}

@Override
public boolean delete() {
try {
os.close();
} catch( Exception ex ) {}
os = null;
return true;
}

public byte[] getbytes() {
return os.toByteArray();
}
}
129 changes: 129 additions & 0 deletions FPC4/src/TemplateEngine/Compiler/FPCompiler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package TemplateEngine.Compiler;

import TemplateEngine.Data.FunctionalOperation;

import javax.tools.*;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Created by BESTDOG on 11/23/2015.
*
* Manages compilation of SourceCode by ClassName.
* Allows classes to be unloaded with {@link IsolatedClassLoader}.
*/
public class FPCompiler {

final JavaCompiler javac;
IsolatedClassLoader loader;

private static FPCompiler self;

public FPCompiler() {
findTools();
javac = ToolProvider.getSystemJavaCompiler();
if (FPCompiler.self == null) {
this.self = this;
}
}

public static FPCompiler getCompiler() {
return self;
}

/**
* Compiles a {@link SourceCode} java-code file and loads it into an {@link IsolatedClassLoader} so it may be unloaded
* from the JVM.
* @param sourceCode SourceCode to compile.
* @return true on successful compilation, else false.
*/
public boolean compile(SourceCode sourceCode) {
boolean res = false;
CompiledCode cc = null;

if(javac == null) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "JavaCompiler is unavailable.");
} else try {
cc = new CompiledCode(sourceCode.getClassName());

Iterable<JavaFileObject> units = Arrays.asList(sourceCode);

IsolatedClassLoader cl = getLoader();

StandardJavaFileManager sjfm = javac.getStandardFileManager(new DiagnosticListener() {
@Override
public void report(Diagnostic diagnostic) {
Logger.getLogger(FPCompiler.class.getName()).log(Level.SEVERE, diagnostic.getMessage(Locale.ENGLISH));
}
}, Locale.ENGLISH, Charset.defaultCharset());

List<String> options = new ArrayList<>();
//options.add("-Xlint:unchecked");

IsolatedJavaFileManager jfm = new IsolatedJavaFileManager(sjfm, cc, cl);
JavaCompiler.CompilationTask t = javac.getTask(null, jfm, null, options, null, units);
res = t.call();
} catch (URISyntaxException e) {
e.printStackTrace();
}

return res;
}

/**
* Compiles and loads a single source file.
*
* @param className Unique name of the class.
* @param rawCode Raw source code as a String.
* @return true on successful compilation, else false.
* @throws URISyntaxException Thrown when the JavaFileManagerEx cannot
* resolve the name of the compiled code in the StandardJavaFileManager.
*/
public boolean compile(String className, String rawCode) throws URISyntaxException {
SourceCode sc = new SourceCode(className, rawCode);
return this.compile(sc);
}

/**
* Sets the internal class loader reference to null and returns the last
* strong reference to it.
*
* @return The last strong reference to the class loader.
*/
public IsolatedClassLoader deleteLoader() {
IsolatedClassLoader l = loader;
if (loader != null) {
loader.clear();
}
loader = null;
return l;
}

public IsolatedClassLoader getLoader() {
if (loader == null) {
loader = new IsolatedClassLoader();
}
return loader;
}

/**
* Typically used to check if the loader persisted after being deleted.
* @return IsolatedClassLoader if exists, may be null.
*/
public IsolatedClassLoader getCurrentLoader() {
return this.loader;
}

/***
* Locates the JDK's tools.jar, typically jdk1.8.0_66 folder.
*/
private void findTools() {
/** this should be handled by implementing project. */
}
}
94 changes: 94 additions & 0 deletions FPC4/src/TemplateEngine/Compiler/IsolatedClassLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package TemplateEngine.Compiler;

import java.util.HashMap;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Created by BESTDOG on 11/23/2015.
*
* <pre>
* Classloader for Fingerprints and Detect interfaces.
* A classloader is created for each fingerprint so that it may be deleted.
* Loading a class into the system class loader means that class cannot be unloaded without unloading every other class.
* A class can only be unloaded by removing all references to it's classLoader, this is why we use {@link IsolatedClassLoader}.
* </pre>
*/
public class IsolatedClassLoader extends ClassLoader {

final HashMap<String, CompiledCode> units = new HashMap<>();
final HashMap<String, Class> classes = new HashMap<>();

public IsolatedClassLoader() {
super(new ClassLoader() {});
}

/**
* @return Map of all code units to their {@link CompiledCode#getName() } value.
*/
public HashMap<String, CompiledCode> getUnits() {
return units;
}

public void loadAll() throws ClassNotFoundException {
loadAll(false);
}

/**
* Loads all code units within this class loader ( typically just 1 )
*
* @param verbose logges errors if true, else false and silent.
* @throws ClassNotFoundException If {@link #findClass(java.lang.String) }
* is called on a class that has not been added.
*/
public void loadAll(Boolean verbose) throws ClassNotFoundException {
Set<String> names = getUnits().keySet();
for (String s : names) {
if (verbose) {
Logger.getLogger(IsolatedClassLoader.class.getName()).log(Level.INFO, "Loading, " + s);
}
Class c = this.findClass(s);
classes.put(s, c);
}
}

/**
* Adds a compiled code unit to this classloader.
*
* @param unit Compiled unit of code, its {@link CompiledCode#getName() }
* will be the name used to load the class with {@link #findClass(java.lang.String)
* }.
*/
public void setCode(CompiledCode unit) {
String name = unit.getName();
units.put(name, unit);
}

/**
* Clears all code units and loaded classes.
*/
public void clear() {
classes.clear();
units.values().forEach(CompiledCode::delete);
units.clear();
}

@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
Class c = classes.get(name);
if (c != null) {
return c;
}
CompiledCode unit = units.get(name);
if (unit == null) {
return super.findClass(name);
}
byte[] bytes = unit.getbytes();
return defineClass(name, bytes, 0, bytes.length);
}

public boolean isPresent(String className) {
return this.classes.containsKey(className);
}
}
43 changes: 43 additions & 0 deletions FPC4/src/TemplateEngine/Compiler/IsolatedJavaFileManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package TemplateEngine.Compiler;

import java.io.IOException;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
/**
* Created by BESTDOG on 11/23/2015.
*
* <pre>
* Used by the JavaCompiler to proxy the transition of SourceCode to CompiledCode objects.
* Helps the determine (when used by javac) to locate the compiled-code files in memory, vs on disk.
* </pre>
*/
public class IsolatedJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {

CompiledCode unit;
IsolatedClassLoader loader;

/**
* Constructs this file-manager from the manager it will forward files from.
* @param fileman FileManager to forward or get from when necessary.
* @param unit Code unit to supply when requested.
* @param loader Class loader used to load the CompiledCodeUnit within this FileManager.
*/
protected IsolatedJavaFileManager(JavaFileManager fileman, CompiledCode unit, IsolatedClassLoader loader) {
super(fileman);
this.unit = unit;
this.loader = loader;
this.loader.setCode(unit);
}

@Override
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
return unit;
}

@Override
public ClassLoader getClassLoader(JavaFileManager.Location location) {
return loader;
}
}
Loading

0 comments on commit e12732a

Please sign in to comment.