Class 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 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 stored
        sampleFolder - folder where created samples should be created
        ssl - whether the targeted RMI endpoint uses ssl on the registry
        followRedirects - 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 file
        sampleContent - content of the sample file
        Throws:
        UnexpectedCharacterException - is thrown if the filenames are violating the security settings
        IOException - 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 file
        sampleContent - content of the sample file
        subfolder - sub folder within the sub folder of the sample folder
        Throws:
        UnexpectedCharacterException - is thrown if the filenames are violating the security settings
        IOException - 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 for
        className - underlying class name of the corresponding bound name (usually an interface)
        unknownClass - whether the class to create is unknown
        methods - available remote methods represented by MethodCandidates
        rmi - RMIEndpoint to the currently targeted RMI endpoint
        Throws:
        UnexpectedCharacterException - is thrown if class or bound names violate the security policies
        javassist.NotFoundException - should not occur
        IOException - if an IO operation fails
        javassist.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 unknown
        boundName - bound name where the corresponding class name is available
        method - MethodCandidate to create the sample for
        remoteHost - currently targeted RMI host
        remotePort - currently targeted RMI registry port
        Throws:
        UnexpectedCharacterException - is thrown if bound names or class names violate security policies
        javassist.NotFoundException - should not occur
        IOException - if an IO operation fails
        javassist.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 name
        className - class name of the remote interface or stub
        methods - MethodCandidates that should be part of the interface
        Throws:
        UnexpectedCharacterException - is thrown if bound names or class names violate security policies
        IOException - if an IO operation fails
        javassist.CannotCompileException - should never occur
        javassist.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 interface
        methods - MethodCandidates that should be included
        Throws:
        UnexpectedCharacterException - is thrown if bound or class names violate the security policies
        IOException - if an IO operation fails
        javassist.CannotCompileException - should never occur
        javassist.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 name
        className - class name of the remote stub
        methods - MethodCandidates that should be included
        Throws:
        UnexpectedCharacterException - is thrown when bound or class names violate the security policies
        IOException - if an IO operation fails
        javassist.CannotCompileException - should never occur
        javassist.NotFoundException - should never occur