<?xml version="1.0"?>
<ruleset xmlns="http://windup.jboss.org/schema/jboss-ruleset" id="environment-dependent-calls"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd">
    <metadata>
        <description>
            This ruleset provides generic rules that should match when migrating from different server
            to JBoss EAP 6+.
        </description>
        <dependencies>
            <addon id="org.jboss.windup.rules,windup-rules-javaee,2.3.0.Final" />
            <addon id="org.jboss.windup.rules,windup-rules-java,2.3.0.Final" />
        </dependencies>
        <targetTechnology id="eap" versionRange="[6,8)" />
        <targetTechnology id="java-ee" versionRange="[6,)" />
    </metadata>
    <rules>
        <rule id="environment-dependent-calls-01000">
            <when>
                <javaclass references="java.lang.Class.forName({*}">
                    <location>METHOD_CALL</location>
                </javaclass>
            </when>
            <perform>
                 <hint title="Dynamic class instantiation" effort="0" category-id="optional">
                     <message>
                       <![CDATA[
                       The class is dynamically loaded within application. During the migration, multiple classes that are provided on classpath by a different server may not be present anymore.

                       Please review the class-loading mechanisms and ensure that the dynamically loaded class is available in JBoss EAP.
                       ]]>
                     </message>
                     <link title="Class Loading and Modules in JBoss EAP 7" href="https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html-single/development_guide/#class_loading_and_modules"/>
                     <link title="Class Loading and Modules in JBoss EAP 6" href="https://access.redhat.com/documentation/en-us/JBoss_Enterprise_Application_Platform/6.4/html-single/Development_Guide/index.html#chap-Class_Loading_and_Modules"/>
                     <link title="Article about Classloading in JBoss EAP 6" href="https://access.redhat.com/solutions/361343" />
                     <tag>classloader</tag>
                 </hint>
            </perform>
        </rule>
        <rule id="environment-dependent-calls-02000">
            <when>
                <javaclass references="javax.naming.Context.lookup{*}" as="default">
                    <location>METHOD_CALL</location>
                </javaclass>
            </when>
            <perform>
                <iteration>
                    <hint title="Call of JNDI lookup" effort="1" category-id="mandatory">
                        <message>
                           <![CDATA[
                           This method lookups an object using a JNDI String. During the migration process, some entity JNDI bindings may change.
                           Ensure that the JNDI Name does not need to change for JBoss EAP.

                           *For Example:*

                           ```java
                           (ConnectionFactory)initialContext.lookup("weblogic.jms.ConnectionFactory");
                           ```

                           *should become:*

                           ```java
                           (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
                           ```
                           ]]>
                        </message>
                        <link title="JBoss EAP 6 - JNDI Reference" href="https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/6.4/html-single/development_guide/#chap-Remote_JNDI_Lookup" />
                        <link title="JNDI Changes" href="https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/6.4/html-single/migration_guide/#sect-JNDI_Changes" />
                        <tag>jndi</tag>
                    </hint>
                </iteration>
            </perform>
        </rule>
        <rule id="environment-dependent-calls-03000">
            <when>
                <javaclass references="javax.naming.InitialContext(java.util.Hashtable{*}" as="default">
                    <location>CONSTRUCTOR_CALL</location>
                </javaclass>
            </when>
            <perform>
                <iteration>
                    <hint title="Proprietary InitialContext initialization" effort="1" category-id="mandatory">
                    <message>
                      <![CDATA[
                      In JBoss EAP, the `InitialContext` should be instantiated with no arguments. Once an instance is constructed, look up the service using portable JNDI lookup syntax. Ensure also that in case system properties for `InitialContext` are provided, they do not need to be changed for the JBoss EAP.

                      ```java
                      InitialContext context = new InitialContext();
                      Service service = (Service) context.lookup( "java:app/service/" + ServiceImpl.class.getSimpleName() );
                      ```
                      ]]>
                    </message>
                    <link href="https://access.redhat.com/articles/1496973" title="Migrate Applications From Other Platforms to Use Portable JNDI Syntax in Red Hat JBoss Enterprise Application Platform" />
                    <tag>jndi</tag>
                    </hint>
                </iteration>
            </perform>
        </rule>
        <rule id="environment-dependent-calls-03500">
            <when>
                <file filename="jndi.properties" as="default"/>
            </when>
            <perform>
                <iteration>
                    <classification issue-display-mode="all" title="JNDI properties file" category-id="mandatory" effort="1">
                        <description>
                          <![CDATA[
                          The JNDI automatically reads the application resource files from all components in the applications' classpaths.
                          The JNDI then makes the properties from these files available to the service providers.

                          Please ensure the property values listed in this file are available to JBoss EAP.
                          ]]>
                        </description>
                        <tag>webservice</tag>
                    </classification>
                </iteration>
            </perform>
        </rule>
        <rule id="environment-dependent-calls-04000">
        <!--could also check JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES in the provided hash map-->
            <when>
                <javaclass references="javax.management.{suffix}{*}" as="default">
                    <location>CONSTRUCTOR_CALL</location>
                </javaclass>
            </when>
            <perform>
                <iteration>
                    <hint title="JMX MBean object name (javax.management.{suffix})" effort="1" category-id="mandatory">
                        <message>
                          <![CDATA[
                          After migration, some of the JMX beans provided by the previous server may not be present anymore.
                          Ensure that the `javax.management.{suffix}` does not need to change for JBoss EAP.
                          ]]>
                        </message>
                        <tag>jmx</tag>
                    </hint>
                </iteration>
            </perform>
            <where param="suffix">
                <matches pattern="(ObjectName)" />
            </where>
        </rule>
        <rule id="environment-dependent-calls-04001">
            <!--could also check JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES in the provided hash map-->
            <when>
                <javaclass references="javax.management.{suffix}{*}" as="default">
                    <location>CONSTRUCTOR_CALL</location>
                </javaclass>
            </when>
            <perform>
                <iteration>
                    <hint title="JMX API connector server address (javax.management.{suffix})" effort="1" category-id="mandatory">
                        <message>
                            <![CDATA[
                          After migration, some of the JMX beans provided by the previous server may not be present anymore.
                          Ensure that the `javax.management.{suffix}` does not need to change for JBoss EAP.
                          ]]>
                        </message>
                        <tag>jmx</tag>
                    </hint>
                </iteration>
            </perform>
            <where param="suffix">
                <matches pattern="(remote.JMXServiceURL)" />
            </where>
        </rule>
        <rule id="environment-dependent-calls-05000">
            <when>
                <javaclass references="javax.management.remote.JMXConnectorFactory.connect({*})" as="default">
                    <location>METHOD_CALL</location>
                </javaclass>
            </when>
            <perform>
                <iteration>
                    <hint title="JMX connection factory parameters" effort="1" category-id="mandatory">
                       <message>
                          <![CDATA[
                          After migration, JMX connection String or parameters may be different than the one provided by previous server.
                          As an example, `javax.management.remote.JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES` will change.
                          Ensure that any of the parameters does not need to change for JBoss EAP.
                          ]]>
                       </message>
                       <tag>jmx</tag>
                    </hint>
                </iteration>
            </perform>
        </rule>
    </rules>
</ruleset>
