Package eu.tneitzel.rmg.io
Class SampleWriter
- java.lang.Object
-
- eu.tneitzel.rmg.io.SampleWriter
-
public class SampleWriter extends Object
The SampleWriter class handles the dynamic creation of RMI code. It uses samples defined within the template folder of the project and replaces some place holders that are defined within them. The result should be compileable Java code that can be used to perform RMI operations.- Author:
- Tobias Neitzel (@qtc_de)
-
-
Constructor Summary
Constructors Constructor Description SampleWriter(String templateFolder, String sampleFolder, boolean ssl, boolean followRedirects)Creates a SampleWriter object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcreateInterface(String boundName, String className, List<MethodCandidate> methods)Creates an interface file for the remote method call.voidcreateInterfaceSample(String boundName, String className, List<MethodCandidate> methods)Create Java code for an remote interface.voidcreateLegacyStub(String boundName, String className, List<MethodCandidate> methods)Creates Java code for a legacy Stub object.voidcreateSample(String className, boolean unknownClass, String boundName, MethodCandidate method, String remoteHost, int remotePort)Creates a sample to invoke the specified MethodCandidate on the specified remoteHost.voidcreateSamples(String boundName, String className, boolean unknownClass, List<MethodCandidate> methods, RMIEndpoint rmi)Creates samples for a bound name and the corresponding available remote methods.StringloadTemplate(String templateName)Reads a template from the template folder and returns the corresponding content.StringloadTemplateFile(String templateName)Reads a template from the template folder and returns the corresponding content.StringloadTemplateStream(String templateName)Reads a template file form the internal template folder and returns its contents.voidwriteSample(String sampleFolder, String sampleName, String sampleContent)Wrapper around writeSamples with additional subfolder argument.voidwriteSample(String sampleFolder, String sampleName, String sampleContent, String subfolder)Writes a sample file into the sample folder.
-
-
-
Constructor Detail
-
SampleWriter
public SampleWriter(String templateFolder, String sampleFolder, boolean ssl, boolean followRedirects) throws IOException
Creates a SampleWriter object. During the creation, a samples folder may be generated if not already present. If the specified template folder is null or empty, rmg defaults to use its internal template folder that is packed into the JAR file.- Parameters:
templateFolder- folder where template files are storedsampleFolder- folder where created samples should be createdssl- whether the targeted RMI endpoint uses ssl on the registryfollowRedirects- whether redirects of remote objects should be followed- Throws:
IOException- may be thrown when one of the required folders is not present
-
-
Method Detail
-
loadTemplate
public String loadTemplate(String templateName) throws IOException
Reads a template from the template folder and returns the corresponding content. Depending on the contents of this.templateFolder, an external template folder or the internal from the JAR file is used.- Parameters:
templateName- name of the template file- Returns:
- template content
- Throws:
IOException- is thrown when an IO operation fails.
-
loadTemplateStream
public String loadTemplateStream(String templateName) throws IOException
Reads a template file form the internal template folder and returns its contents. As the internal template folder is contained within the JAR file, getResourceAsStream is used to load the template.- Parameters:
templateName- name of the template file- Returns:
- template content
- Throws:
IOException- is thrown when an IO operation fails.
-
loadTemplateFile
public String loadTemplateFile(String templateName) throws IOException
Reads a template from the template folder and returns the corresponding content. This function is called when external template folders are used.- Parameters:
templateName- name of the template file- Returns:
- template content
- Throws:
IOException- is thrown when an IO operation fails.
-
writeSample
public void writeSample(String sampleFolder, String sampleName, String sampleContent) throws UnexpectedCharacterException, IOException
Wrapper around writeSamples with additional subfolder argument.- Parameters:
sampleFolder- sub folder within the sample folder to write the files in.sampleName- name of the sample filesampleContent- content of the sample file- Throws:
UnexpectedCharacterException- is thrown if the filenames are violating the security settingsIOException- is thrown if an IO operation fails
-
writeSample
public void writeSample(String sampleFolder, String sampleName, String sampleContent, String subfolder) throws UnexpectedCharacterException, IOException
Writes a sample file into the sample folder. Performs basic security checks on the filenames.- Parameters:
sampleFolder- sub folder within the sample folder to write the files in.sampleName- name of the sample filesampleContent- content of the sample filesubfolder- sub folder within the sub folder of the sample folder- Throws:
UnexpectedCharacterException- is thrown if the filenames are violating the security settingsIOException- is thrown if an IO operation fails
-
createSamples
public void createSamples(String boundName, String className, boolean unknownClass, List<MethodCandidate> methods, RMIEndpoint rmi) throws UnexpectedCharacterException, javassist.NotFoundException, IOException, javassist.CannotCompileException
Creates samples for a bound name and the corresponding available remote methods.- Parameters:
boundName- bound name to create the sample forclassName- underlying class name of the corresponding bound name (usually an interface)unknownClass- whether the class to create is unknownmethods- available remote methods represented by MethodCandidatesrmi- RMIEndpoint to the currently targeted RMI endpoint- Throws:
UnexpectedCharacterException- is thrown if class or bound names violate the security policiesjavassist.NotFoundException- should not occurIOException- if an IO operation failsjavassist.CannotCompileException- should not occur
-
createSample
public void createSample(String className, boolean unknownClass, String boundName, MethodCandidate method, String remoteHost, int remotePort) throws UnexpectedCharacterException, javassist.NotFoundException, IOException, javassist.CannotCompileException
Creates a sample to invoke the specified MethodCandidate on the specified remoteHost. Creating the sample is basically an ugly find an replace over the template files.- Parameters:
className- class name of the remote interface or the RMI stub (if legacy is used)unknownClass- whether the class to create is unknownboundName- bound name where the corresponding class name is availablemethod- MethodCandidate to create the sample forremoteHost- currently targeted RMI hostremotePort- currently targeted RMI registry port- Throws:
UnexpectedCharacterException- is thrown if bound names or class names violate security policiesjavassist.NotFoundException- should not occurIOException- if an IO operation failsjavassist.CannotCompileException- should not occur
-
createInterface
public void createInterface(String boundName, String className, List<MethodCandidate> methods) throws UnexpectedCharacterException, IOException, javassist.CannotCompileException, javassist.NotFoundException
Creates an interface file for the remote method call. In the case of currently used RMI techniques (proxy invocation) an interface file is sufficient. For legacy RMI we also need to create the Java code for the corresponding stub object.- Parameters:
boundName- targeted bound nameclassName- class name of the remote interface or stubmethods- MethodCandidates that should be part of the interface- Throws:
UnexpectedCharacterException- is thrown if bound names or class names violate security policiesIOException- if an IO operation failsjavassist.CannotCompileException- should never occurjavassist.NotFoundException- should never occur
-
createInterfaceSample
public void createInterfaceSample(String boundName, String className, List<MethodCandidate> methods) throws UnexpectedCharacterException, IOException, javassist.CannotCompileException, javassist.NotFoundException
Create Java code for an remote interface. This interface extends Remote and contains all specified MethodCandidates.- Parameters:
boundName- targeted bound name (only used for the file name)className- class name of the remote interfacemethods- MethodCandidates that should be included- Throws:
UnexpectedCharacterException- is thrown if bound or class names violate the security policiesIOException- if an IO operation failsjavassist.CannotCompileException- should never occurjavassist.NotFoundException- should never occur
-
createLegacyStub
public void createLegacyStub(String boundName, String className, List<MethodCandidate> methods) throws UnexpectedCharacterException, IOException, javassist.CannotCompileException, javassist.NotFoundException
Creates Java code for a legacy Stub object. These code samples are a little bit harder to create, as each remote method needs to be hardcoded into the corresponding stub object. Theoretically, you could also create this stub code using rmic, but rmic is no longer shipped with current Java releases. When facing a legacy RMI server and only having e.g. Java11 installed, this method can be handy to create the corresponding stub code.- Parameters:
boundName- currently targeted bound nameclassName- class name of the remote stubmethods- MethodCandidates that should be included- Throws:
UnexpectedCharacterException- is thrown when bound or class names violate the security policiesIOException- if an IO operation failsjavassist.CannotCompileException- should never occurjavassist.NotFoundException- should never occur
-
-