From c1d85d80c995f03f4d4b270fd30054c2ebd48632 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 18 Jun 2018 20:58:52 +0800 Subject: Fix package name, Unit Test for Context Album package org.onap.policy.core renamed to org.onap.policy.apex.core Added unit text for context exceptions and context album Issue-ID: POLICY-857 Change-Id: I24f376245d9bc46db6a6a47ac3e8e6c559a16465 Signed-off-by: liamfallon --- .../compile/singleclass/SingleClassBuilder.java | 133 +++++++++++++++++++++ .../singleclass/SingleClassByteCodeFileObject.java | 89 ++++++++++++++ .../singleclass/SingleClassCompilationUnit.java | 83 +++++++++++++ .../compile/singleclass/SingleClassLoader.java | 61 ++++++++++ .../compile/singleclass/SingleFileManager.java | 79 ++++++++++++ .../java/compile/singleclass/package-info.java | 28 +++++ .../compile/singleclass/SingleClassBuilder.java | 133 --------------------- .../singleclass/SingleClassByteCodeFileObject.java | 89 -------------- .../singleclass/SingleClassCompilationUnit.java | 83 ------------- .../compile/singleclass/SingleClassLoader.java | 61 ---------- .../compile/singleclass/SingleFileManager.java | 79 ------------ .../java/compile/singleclass/package-info.java | 28 ----- 12 files changed, 473 insertions(+), 473 deletions(-) create mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java create mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java create mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java create mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassLoader.java create mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleFileManager.java create mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/package-info.java delete mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java delete mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java delete mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java delete mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassLoader.java delete mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleFileManager.java delete mode 100644 core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/package-info.java (limited to 'core/core-infrastructure') diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java new file mode 100644 index 000000000..7a0b6048e --- /dev/null +++ b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java @@ -0,0 +1,133 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.java.compile.singleclass; + +import java.util.Arrays; +import java.util.List; + +import javax.tools.Diagnostic; +import javax.tools.DiagnosticCollector; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.ToolProvider; + +import org.onap.policy.apex.core.infrastructure.java.JavaHandlingException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class SingleClassBuilder is used to compile the Java code for a Java object and to create an instance of the + * object. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SingleClassBuilder { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(SingleClassBuilder.class); + + // The class name and source code for the class that we are compiling and instantiating + private final String className; + private final String sourceCode; + + // This specialized JavaFileManager handles class loading for the single Java class + private SingleFileManager singleFileManager = null; + + /** + * Instantiates a new single class builder. + * + * @param className the class name + * @param sourceCode the source code + */ + public SingleClassBuilder(final String className, final String sourceCode) { + // Save the fields of the class + this.className = className; + this.sourceCode = sourceCode; + } + + /** + * Compile the single class into byte code. + * + * @throws JavaHandlingException Thrown on compilation errors or handling errors on the single Java class + */ + public void compile() throws JavaHandlingException { + // Get the list of compilation units, there is only one here + final List compilationUnits = + Arrays.asList(new SingleClassCompilationUnit(className, sourceCode)); + + // Allows us to get diagnostics from the compilation + final DiagnosticCollector diagnosticListener = new DiagnosticCollector<>(); + + // Get the Java compiler + final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + + // Set up the target file manager and call the compiler + singleFileManager = new SingleFileManager(compiler, new SingleClassByteCodeFileObject(className)); + final JavaCompiler.CompilationTask task = + compiler.getTask(null, singleFileManager, diagnosticListener, null, null, compilationUnits); + + // Check if the compilation worked + if (!task.call()) { + final StringBuilder builder = new StringBuilder(); + for (final Diagnostic diagnostic : diagnosticListener.getDiagnostics()) { + builder.append("code:"); + builder.append(diagnostic.getCode()); + builder.append(", kind:"); + builder.append(diagnostic.getKind()); + builder.append(", position:"); + builder.append(diagnostic.getPosition()); + builder.append(", start position:"); + builder.append(diagnostic.getStartPosition()); + builder.append(", end position:"); + builder.append(diagnostic.getEndPosition()); + builder.append(", source:"); + builder.append(diagnostic.getSource()); + builder.append(", message:"); + builder.append(diagnostic.getMessage(null)); + builder.append("\n"); + } + + LOGGER.warn("error compiling Java code for class \"" + className + "\": " + builder.toString()); + throw new JavaHandlingException( + "error compiling Java code for class \"" + className + "\": " + builder.toString()); + } + } + + /** + * Create a new instance of the Java class using its byte code definition. + * + * @return A new instance of the object + * @throws InstantiationException if an instance of the object cannot be created, for example if the class has no + * default constructor + * @throws IllegalAccessException the caller does not have permission to call the class + * @throws ClassNotFoundException the byte code for the class is not found in the class loader + * @throws JavaHandlingException the java handling exception if the Java class source code is not compiled + */ + public Object createObject() + throws InstantiationException, IllegalAccessException, ClassNotFoundException, JavaHandlingException { + if (singleFileManager == null) { + LOGGER.warn("error instantiating instance for class \"" + className + "\": code may not be compiled"); + throw new JavaHandlingException( + "error instantiating instance for class \"" + className + "\": code may not be compiled"); + } + + return singleFileManager.getClassLoader(null).findClass(className).newInstance(); + } +} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java new file mode 100644 index 000000000..4b7225267 --- /dev/null +++ b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.java.compile.singleclass; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; + +import javax.tools.SimpleJavaFileObject; + +/** + * The Class SingleClassByteCodeFileObject is a specialization of the {@link SimpleJavaFileObject} class, which is + * itself an implementation of the {@code JavaFileObject} interface, which provides a file abstraction for tools + * operating on Java programming language source and class files. The {@link SimpleJavaFileObject} class provides simple + * implementations for most methods in {@code JavaFileObject}. This class is designed to be sub classed and used as a + * basis for {@code JavaFileObject} implementations. Subclasses can override the implementation and specification of any + * method of this class as long as the general contract of {@code JavaFileObject} is obeyed. + * + * This class holds the byte code for a single class in memory. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SingleClassByteCodeFileObject extends SimpleJavaFileObject { + + // The ByteArrayOutputStream holds the byte code for the class + private ByteArrayOutputStream byteArrayOutputStream; + + /** + * Instantiates the byte code for the class in memory. + * + * @param className the class name is used to compose a URI for the class + */ + public SingleClassByteCodeFileObject(final String className) { + super(URI.create("byte:///" + className + ".class"), Kind.CLASS); + } + + /* + * (non-Javadoc) + * + * @see javax.tools.SimpleJavaFileObject#openOutputStream() + */ + @Override + public OutputStream openOutputStream() { + // Create the byte array output stream that will hold the byte code for the class, when the class source code is + // compiled, this output stream is passed + // to the compiler and the byte code for the class is written into the output stream. + byteArrayOutputStream = new ByteArrayOutputStream(); + return byteArrayOutputStream; + } + + /* + * (non-Javadoc) + * + * @see javax.tools.SimpleJavaFileObject#openInputStream() + */ + @Override + public InputStream openInputStream() { + // No input stream for streaming out the byte code + return null; + } + + /** + * Gets the byte code of the class. + * + * @return the byte code of the class + */ + public byte[] getByteCode() { + return byteArrayOutputStream.toByteArray(); + } +} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java new file mode 100644 index 000000000..a683cb730 --- /dev/null +++ b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.java.compile.singleclass; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; + +import javax.tools.SimpleJavaFileObject; + +/** + * The Class SingleClassCompilationUnit is a container for the source code of the single Java class in memory. The class + * uses a {@link String} to hold the source code. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SingleClassCompilationUnit extends SimpleJavaFileObject { + + private final String source; + + /** + * Instantiates a new compilation unit. + * + * @param className the class name for the source code + * @param source the source code for the class + */ + public SingleClassCompilationUnit(final String className, final String source) { + // Create a URI for the source code of the class + super(URI.create("file:///" + className + ".java"), Kind.SOURCE); + this.source = source; + } + + /* + * (non-Javadoc) + * + * @see javax.tools.SimpleJavaFileObject#getCharContent(boolean) + */ + @Override + public CharSequence getCharContent(final boolean ignoreEncodingErrors) { + // Return the source code to toe caller, the compiler + return source; + } + + /* + * (non-Javadoc) + * + * @see javax.tools.SimpleJavaFileObject#openOutputStream() + */ + @Override + public OutputStream openOutputStream() { + throw new IllegalStateException(); + } + + /* + * (non-Javadoc) + * + * @see javax.tools.SimpleJavaFileObject#openInputStream() + */ + @Override + public InputStream openInputStream() { + // Return the source code as a stream + return new ByteArrayInputStream(source.getBytes()); + } +} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassLoader.java b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassLoader.java new file mode 100644 index 000000000..a7ad5e317 --- /dev/null +++ b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassLoader.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.java.compile.singleclass; + +/** + * The Class SingleClassLoader is responsible for class loading the single Java class being held in memory. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SingleClassLoader extends ClassLoader { + // The byte code of the class held in memory as byte code in a ByteCodeFileObject + private final SingleClassByteCodeFileObject byteCodeFileObject; + + /** + * Instantiates a new single class loader to load the byte code of the class that is being held in memory. + * + * @param byteCodeFileObject the byte code of the class + */ + public SingleClassLoader(final SingleClassByteCodeFileObject byteCodeFileObject) { + this.byteCodeFileObject = byteCodeFileObject; + } + + /* + * (non-Javadoc) + * + * @see java.lang.ClassLoader#findClass(java.lang.String) + */ + @Override + protected Class findClass(final String className) throws ClassNotFoundException { + // Creates a java Class that can be instantiated from the class defined in the byte code in the + // ByteCodeFileObejct + return defineClass(className, byteCodeFileObject.getByteCode(), 0, byteCodeFileObject.getByteCode().length); + } + + /** + * Gets the file object. + * + * @return the file object + */ + SingleClassByteCodeFileObject getFileObject() { + return byteCodeFileObject; + } +} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleFileManager.java b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleFileManager.java new file mode 100644 index 000000000..cd14b1a06 --- /dev/null +++ b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleFileManager.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.java.compile.singleclass; + +import java.io.IOException; + +import javax.tools.FileObject; +import javax.tools.ForwardingJavaFileManager; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; + +/** + * The Class SingleFileManager is a {@link ForwardingJavaFileManager} which in turn implements {@code JavaFileManager}. + * A {@code JavaFileManager} handles source files for Java language handling tools. A {@link ForwardingJavaFileManager} + * is an implementation of {@code JavaFileManager} that forwards the {@code JavaFileManager} methods to a given file + * manager. + * + * This class instantiates and forwards those requests to a {@link StandardJavaFileManager} instance to act as a + * {@code JavaFileManager} for a Java single file, managing class loading for the class. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SingleFileManager extends ForwardingJavaFileManager { + // THe class loader for our single class + private final SingleClassLoader singleClassLoader; + + /** + * Instantiates a new single file manager. + * + * @param compiler the compiler we are using + * @param byteCodeFileObject the byte code for the compiled class + */ + public SingleFileManager(final JavaCompiler compiler, final SingleClassByteCodeFileObject byteCodeFileObject) { + super(compiler.getStandardFileManager(null, null, null)); + singleClassLoader = new SingleClassLoader(byteCodeFileObject); + } + + /* + * (non-Javadoc) + * + * @see javax.tools.ForwardingJavaFileManager#getJavaFileForOutput(javax.tools.JavaFileManager.Location, + * java.lang.String, javax.tools.JavaFileObject.Kind, javax.tools.FileObject) + */ + @Override + public JavaFileObject getJavaFileForOutput(final Location notUsed, final String className, + final JavaFileObject.Kind kind, final FileObject sibling) throws IOException { + // Return the JavaFileObject to the compiler so that it can write byte code into it + return singleClassLoader.getFileObject(); + } + + /* + * (non-Javadoc) + * + * @see javax.tools.ForwardingJavaFileManager#getClassLoader(javax.tools.JavaFileManager.Location) + */ + @Override + public SingleClassLoader getClassLoader(final Location location) { + return singleClassLoader; + } +} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/package-info.java b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/package-info.java new file mode 100644 index 000000000..5ae0d4f59 --- /dev/null +++ b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Generates classes from source code by compiling source code and placing the resultant classes on the class path on + * the fly. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.core.infrastructure.java.compile.singleclass; diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java b/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java deleted file mode 100644 index 5765eee01..000000000 --- a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java +++ /dev/null @@ -1,133 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.core.infrastructure.java.compile.singleclass; - -import java.util.Arrays; -import java.util.List; - -import javax.tools.Diagnostic; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.ToolProvider; - -import org.onap.policy.apex.core.infrastructure.java.JavaHandlingException; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The Class SingleClassBuilder is used to compile the Java code for a Java object and to create an instance of the - * object. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class SingleClassBuilder { - // Logger for this class - private static final XLogger LOGGER = XLoggerFactory.getXLogger(SingleClassBuilder.class); - - // The class name and source code for the class that we are compiling and instantiating - private final String className; - private final String sourceCode; - - // This specialized JavaFileManager handles class loading for the single Java class - private SingleFileManager singleFileManager = null; - - /** - * Instantiates a new single class builder. - * - * @param className the class name - * @param sourceCode the source code - */ - public SingleClassBuilder(final String className, final String sourceCode) { - // Save the fields of the class - this.className = className; - this.sourceCode = sourceCode; - } - - /** - * Compile the single class into byte code. - * - * @throws JavaHandlingException Thrown on compilation errors or handling errors on the single Java class - */ - public void compile() throws JavaHandlingException { - // Get the list of compilation units, there is only one here - final List compilationUnits = - Arrays.asList(new SingleClassCompilationUnit(className, sourceCode)); - - // Allows us to get diagnostics from the compilation - final DiagnosticCollector diagnosticListener = new DiagnosticCollector<>(); - - // Get the Java compiler - final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - - // Set up the target file manager and call the compiler - singleFileManager = new SingleFileManager(compiler, new SingleClassByteCodeFileObject(className)); - final JavaCompiler.CompilationTask task = - compiler.getTask(null, singleFileManager, diagnosticListener, null, null, compilationUnits); - - // Check if the compilation worked - if (!task.call()) { - final StringBuilder builder = new StringBuilder(); - for (final Diagnostic diagnostic : diagnosticListener.getDiagnostics()) { - builder.append("code:"); - builder.append(diagnostic.getCode()); - builder.append(", kind:"); - builder.append(diagnostic.getKind()); - builder.append(", position:"); - builder.append(diagnostic.getPosition()); - builder.append(", start position:"); - builder.append(diagnostic.getStartPosition()); - builder.append(", end position:"); - builder.append(diagnostic.getEndPosition()); - builder.append(", source:"); - builder.append(diagnostic.getSource()); - builder.append(", message:"); - builder.append(diagnostic.getMessage(null)); - builder.append("\n"); - } - - LOGGER.warn("error compiling Java code for class \"" + className + "\": " + builder.toString()); - throw new JavaHandlingException( - "error compiling Java code for class \"" + className + "\": " + builder.toString()); - } - } - - /** - * Create a new instance of the Java class using its byte code definition. - * - * @return A new instance of the object - * @throws InstantiationException if an instance of the object cannot be created, for example if the class has no - * default constructor - * @throws IllegalAccessException the caller does not have permission to call the class - * @throws ClassNotFoundException the byte code for the class is not found in the class loader - * @throws JavaHandlingException the java handling exception if the Java class source code is not compiled - */ - public Object createObject() - throws InstantiationException, IllegalAccessException, ClassNotFoundException, JavaHandlingException { - if (singleFileManager == null) { - LOGGER.warn("error instantiating instance for class \"" + className + "\": code may not be compiled"); - throw new JavaHandlingException( - "error instantiating instance for class \"" + className + "\": code may not be compiled"); - } - - return singleFileManager.getClassLoader(null).findClass(className).newInstance(); - } -} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java b/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java deleted file mode 100644 index c0a2ef7a1..000000000 --- a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassByteCodeFileObject.java +++ /dev/null @@ -1,89 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.core.infrastructure.java.compile.singleclass; - -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URI; - -import javax.tools.SimpleJavaFileObject; - -/** - * The Class SingleClassByteCodeFileObject is a specialization of the {@link SimpleJavaFileObject} class, which is - * itself an implementation of the {@code JavaFileObject} interface, which provides a file abstraction for tools - * operating on Java programming language source and class files. The {@link SimpleJavaFileObject} class provides simple - * implementations for most methods in {@code JavaFileObject}. This class is designed to be sub classed and used as a - * basis for {@code JavaFileObject} implementations. Subclasses can override the implementation and specification of any - * method of this class as long as the general contract of {@code JavaFileObject} is obeyed. - * - * This class holds the byte code for a single class in memory. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class SingleClassByteCodeFileObject extends SimpleJavaFileObject { - - // The ByteArrayOutputStream holds the byte code for the class - private ByteArrayOutputStream byteArrayOutputStream; - - /** - * Instantiates the byte code for the class in memory. - * - * @param className the class name is used to compose a URI for the class - */ - public SingleClassByteCodeFileObject(final String className) { - super(URI.create("byte:///" + className + ".class"), Kind.CLASS); - } - - /* - * (non-Javadoc) - * - * @see javax.tools.SimpleJavaFileObject#openOutputStream() - */ - @Override - public OutputStream openOutputStream() { - // Create the byte array output stream that will hold the byte code for the class, when the class source code is - // compiled, this output stream is passed - // to the compiler and the byte code for the class is written into the output stream. - byteArrayOutputStream = new ByteArrayOutputStream(); - return byteArrayOutputStream; - } - - /* - * (non-Javadoc) - * - * @see javax.tools.SimpleJavaFileObject#openInputStream() - */ - @Override - public InputStream openInputStream() { - // No input stream for streaming out the byte code - return null; - } - - /** - * Gets the byte code of the class. - * - * @return the byte code of the class - */ - public byte[] getByteCode() { - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java b/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java deleted file mode 100644 index 9a001d2c2..000000000 --- a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassCompilationUnit.java +++ /dev/null @@ -1,83 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.core.infrastructure.java.compile.singleclass; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URI; - -import javax.tools.SimpleJavaFileObject; - -/** - * The Class SingleClassCompilationUnit is a container for the source code of the single Java class in memory. The class - * uses a {@link String} to hold the source code. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class SingleClassCompilationUnit extends SimpleJavaFileObject { - - private final String source; - - /** - * Instantiates a new compilation unit. - * - * @param className the class name for the source code - * @param source the source code for the class - */ - public SingleClassCompilationUnit(final String className, final String source) { - // Create a URI for the source code of the class - super(URI.create("file:///" + className + ".java"), Kind.SOURCE); - this.source = source; - } - - /* - * (non-Javadoc) - * - * @see javax.tools.SimpleJavaFileObject#getCharContent(boolean) - */ - @Override - public CharSequence getCharContent(final boolean ignoreEncodingErrors) { - // Return the source code to toe caller, the compiler - return source; - } - - /* - * (non-Javadoc) - * - * @see javax.tools.SimpleJavaFileObject#openOutputStream() - */ - @Override - public OutputStream openOutputStream() { - throw new IllegalStateException(); - } - - /* - * (non-Javadoc) - * - * @see javax.tools.SimpleJavaFileObject#openInputStream() - */ - @Override - public InputStream openInputStream() { - // Return the source code as a stream - return new ByteArrayInputStream(source.getBytes()); - } -} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassLoader.java b/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassLoader.java deleted file mode 100644 index befed6d40..000000000 --- a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleClassLoader.java +++ /dev/null @@ -1,61 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.core.infrastructure.java.compile.singleclass; - -/** - * The Class SingleClassLoader is responsible for class loading the single Java class being held in memory. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class SingleClassLoader extends ClassLoader { - // The byte code of the class held in memory as byte code in a ByteCodeFileObject - private final SingleClassByteCodeFileObject byteCodeFileObject; - - /** - * Instantiates a new single class loader to load the byte code of the class that is being held in memory. - * - * @param byteCodeFileObject the byte code of the class - */ - public SingleClassLoader(final SingleClassByteCodeFileObject byteCodeFileObject) { - this.byteCodeFileObject = byteCodeFileObject; - } - - /* - * (non-Javadoc) - * - * @see java.lang.ClassLoader#findClass(java.lang.String) - */ - @Override - protected Class findClass(final String className) throws ClassNotFoundException { - // Creates a java Class that can be instantiated from the class defined in the byte code in the - // ByteCodeFileObejct - return defineClass(className, byteCodeFileObject.getByteCode(), 0, byteCodeFileObject.getByteCode().length); - } - - /** - * Gets the file object. - * - * @return the file object - */ - SingleClassByteCodeFileObject getFileObject() { - return byteCodeFileObject; - } -} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleFileManager.java b/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleFileManager.java deleted file mode 100644 index ce92f8e4d..000000000 --- a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/SingleFileManager.java +++ /dev/null @@ -1,79 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.core.infrastructure.java.compile.singleclass; - -import java.io.IOException; - -import javax.tools.FileObject; -import javax.tools.ForwardingJavaFileManager; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.StandardJavaFileManager; - -/** - * The Class SingleFileManager is a {@link ForwardingJavaFileManager} which in turn implements {@code JavaFileManager}. - * A {@code JavaFileManager} handles source files for Java language handling tools. A {@link ForwardingJavaFileManager} - * is an implementation of {@code JavaFileManager} that forwards the {@code JavaFileManager} methods to a given file - * manager. - * - * This class instantiates and forwards those requests to a {@link StandardJavaFileManager} instance to act as a - * {@code JavaFileManager} for a Java single file, managing class loading for the class. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class SingleFileManager extends ForwardingJavaFileManager { - // THe class loader for our single class - private final SingleClassLoader singleClassLoader; - - /** - * Instantiates a new single file manager. - * - * @param compiler the compiler we are using - * @param byteCodeFileObject the byte code for the compiled class - */ - public SingleFileManager(final JavaCompiler compiler, final SingleClassByteCodeFileObject byteCodeFileObject) { - super(compiler.getStandardFileManager(null, null, null)); - singleClassLoader = new SingleClassLoader(byteCodeFileObject); - } - - /* - * (non-Javadoc) - * - * @see javax.tools.ForwardingJavaFileManager#getJavaFileForOutput(javax.tools.JavaFileManager.Location, - * java.lang.String, javax.tools.JavaFileObject.Kind, javax.tools.FileObject) - */ - @Override - public JavaFileObject getJavaFileForOutput(final Location notUsed, final String className, - final JavaFileObject.Kind kind, final FileObject sibling) throws IOException { - // Return the JavaFileObject to the compiler so that it can write byte code into it - return singleClassLoader.getFileObject(); - } - - /* - * (non-Javadoc) - * - * @see javax.tools.ForwardingJavaFileManager#getClassLoader(javax.tools.JavaFileManager.Location) - */ - @Override - public SingleClassLoader getClassLoader(final Location location) { - return singleClassLoader; - } -} diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/package-info.java b/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/package-info.java deleted file mode 100644 index 5e436818c..000000000 --- a/core/core-infrastructure/src/main/java/org/onap/policy/core/infrastructure/java/compile/singleclass/package-info.java +++ /dev/null @@ -1,28 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -/** - * Generates classes from source code by compiling source code and placing the resultant classes on the class path on - * the fly. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - -package org.onap.policy.core.infrastructure.java.compile.singleclass; -- cgit 1.2.3-korg