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 --- .../org/onap/policy/apex/context/ContextAlbum.java | 7 + .../policy/apex/context/impl/ContextAlbumImpl.java | 17 +- .../policy/apex/context/ContextExceptionTest.java | 41 +++ .../apex/context/impl/ContextAlbumImplTest.java | 288 +++++++++++++++++++++ .../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 -- 16 files changed, 825 insertions(+), 474 deletions(-) create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java 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 diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java index 73ef668a0..559139321 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java @@ -111,6 +111,13 @@ public interface ContextAlbum extends Map { */ void unlockForWriting(String key) throws ContextException; + /** + * Get the stack of artifact keys currently using this context item. + * + * @return the keys of the artifacts using the context album at the moment + */ + AxConcept[] getUserArtifactStack(); + /** * Set the stack of artifact keys currently using this context item. * diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java index a68271609..fbaeb2c91 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java @@ -79,6 +79,10 @@ public final class ContextAlbumImpl implements ContextAlbum { */ public ContextAlbumImpl(final AxContextAlbum albumDefinition, final Distributor distributor, final Map albumMap) throws ContextException { + Assertions.argumentNotNull(albumDefinition, "Context album definition may not be null"); + Assertions.argumentNotNull(distributor, "Distributor may not be null"); + Assertions.argumentNotNull(albumMap, "Album map may not be null"); + this.albumDefinition = albumDefinition; // Use the context distributor passed to us @@ -190,6 +194,17 @@ public final class ContextAlbumImpl implements ContextAlbum { userArtifactStack); } + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.context.ContextAlbum#getUserArtifactStack() + */ + @Override + public AxConcept[] getUserArtifactStack() { + return userArtifactStack; + } + /* * (non-Javadoc) * @@ -440,7 +455,7 @@ public final class ContextAlbumImpl implements ContextAlbum { public Object remove(final Object key) { if (!albumDefinition.isWritable()) { final String returnString = "album \"" + albumDefinition.getID() - + "\" remove() not allowed on read only albums for key=\"" + key; + + "\" remove() not allowed on read only albums for key=\"" + key + "\""; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java new file mode 100644 index 000000000..15560f7cc --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.context; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +public class ContextExceptionTest { + + @Test + public void testContextException() { + assertEquals("context exception message", new ContextException("context exception message").getMessage()); + assertEquals("context exception message", new ContextException("context exception message", new IOException()).getMessage()); + } + + @Test + public void testContextRuntimeException() { + assertEquals("context exception message", new ContextRuntimeException("context exception message").getMessage()); + assertEquals("context exception message", new ContextRuntimeException("context exception message", new IOException()).getMessage()); + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java new file mode 100644 index 000000000..e5cc61fad --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java @@ -0,0 +1,288 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.context.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; + +public class ContextAlbumImplTest { + @BeforeClass + public static void prepareForTest() { + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getLockManagerParameters() + .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager"); + ParameterService.registerParameters(ContextParameters.class, contextParameters); + + final SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.registerParameters(SchemaParameters.class, schemaParameters); + } + + @Test + public void testNullsOnConstructor() { + try { + new ContextAlbumImpl(null, null, null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Context album definition may not be null", e.getMessage()); + } catch (ContextException e) { + fail("this test should throw an IllegalArgumentException"); + } + + try { + new ContextAlbumImpl(new AxContextAlbum(), null, null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Distributor may not be null", e.getMessage()); + } catch (ContextException e) { + fail("this test should throw an IllegalArgumentException"); + } + + try { + new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Album map may not be null", e.getMessage()); + } catch (ContextException e) { + fail("this test should throw an IllegalArgumentException"); + } + + try { + new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), new LinkedHashMap()); + fail("this test should throw an exception"); + } catch (ApexRuntimeException e) { + assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas not found in model service", + e.getMessage()); + } catch (ContextException e) { + fail("this test should throw an ApexRuntimeException"); + } + } + + @Test + public void testAlbumInterface() throws ContextException { + AxContextSchemas schemas = new AxContextSchemas(); + AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), + "JAVA", "java.lang.String"); + schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); + ModelService.registerModel(AxContextSchemas.class, schemas); + + AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", + true, AxArtifactKey.getNullKey()); + + AxContextAlbum axContextAlbumRO = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", + false, simpleStringSchema.getKey()); + + try { + new ContextAlbumImpl(axContextAlbum, new JVMLocalDistributor(), new LinkedHashMap()); + fail("this test should throw an exception"); + } catch (ContextException e) { + assertEquals("could not initiate schema management for context album AxContextAlbum", + e.getMessage().substring(0, 69)); + } + + axContextAlbum.setItemSchema(simpleStringSchema.getKey()); + Distributor distributor = new JVMLocalDistributor(); + distributor.init(axContextAlbum.getKey()); + ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap()); + ContextAlbum albumRO = new ContextAlbumImpl(axContextAlbumRO, distributor, new LinkedHashMap()); + + assertEquals("TestContextAlbum", album.getName()); + assertEquals("TestContextAlbum:0.0.1", album.getKey().getID()); + assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getID()); + assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getID()); + + try { + album.containsKey(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("null values are illegal on method parameter \"key\"", e.getMessage()); + } + assertEquals(false, album.containsKey("Key0")); + + try { + album.containsValue(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("null values are illegal on method parameter \"value\"", e.getMessage()); + } + assertEquals(false, album.containsValue("some value")); + + try { + album.get(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage()); + } + + try { + album.put(null, null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage()); + } + + try { + album.put("KeyNull", null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()", + e.getMessage()); + } + + try { + albumRO.put("KeyReadOnly", "A value for a Read Only Album"); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums " + + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage()); + } + + Map putAllData = new LinkedHashMap<>(); + putAllData.put("AllKey0", "vaue of AllKey0"); + putAllData.put("AllKey1", "vaue of AllKey1"); + putAllData.put("AllKey2", "vaue of AllKey2"); + + try { + albumRO.putAll(putAllData); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage()); + } + + try { + albumRO.remove("AllKey0"); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"", + e.getMessage()); + } + + try { + album.remove(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage()); + } + + try { + albumRO.clear(); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage()); + } + + // The following locking tests pass because the locking protects access to Key0 across all + // copies of the distributed album whether the key exists or not + album.lockForReading("Key0"); + assertEquals(null, album.get("Key0")); + album.unlockForReading("Key0"); + assertEquals(null, album.get("Key0")); + + album.lockForWriting("Key0"); + assertEquals(null, album.get("Key0")); + album.unlockForWriting("Key0"); + assertEquals(null, album.get("Key0")); + + // Test write access, trivial test because Integration Test does + // a full test of access locking over albums in different JVMs + album.lockForWriting("Key0"); + assertEquals(null, album.get("Key0")); + album.put("Key0", "value of Key0"); + assertEquals("value of Key0", album.get("Key0")); + album.unlockForWriting("Key0"); + assertEquals("value of Key0", album.get("Key0")); + + // Test read access, trivial test because Integration Test does + // a full test of access locking over albums in different JVMs + album.lockForReading("Key0"); + assertEquals("value of Key0", album.get("Key0")); + album.unlockForReading("Key0"); + + AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1"); + AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState"); + + AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState }; + album.setUserArtifactStack(userArtifactStack); + assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getID()); + assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getID()); + + assertEquals(true, album.keySet().contains("Key0")); + assertEquals(true, album.values().contains("value of Key0")); + assertEquals(1, album.entrySet().size()); + + // The flush() operation fails because the distributor is not initialized with the album which + // is fine for unit test + try { + album.flush(); + fail("test should throw an exception"); + } catch (ContextException e) { + assertEquals("map flush failed, supplied map is null", e.getMessage()); + } + + assertEquals(1, album.size()); + assertEquals(false, album.isEmpty()); + + album.put("Key0", "New value of Key0"); + assertEquals("New value of Key0", album.get("Key0")); + + album.putAll(putAllData); + + putAllData.put("AllKey3", null); + try { + album.putAll(putAllData); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()", + e.getMessage()); + } + + assertEquals("New value of Key0", album.remove("Key0")); + + album.clear(); + assertTrue(album.isEmpty()); + + ModelService.clear(); + } +} 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