javalotus-dominols2j

access to a java interface in lotuscript with LS2J?


enter code hereI try to acess a java class by his interface I make a class in Ls2J instead the java interface because i don't know how i do and i have the error :

error : LS2J Error: Java constructor failed to execute

An idea ? ^^

thanks !

my class java in lotus

Class ILogWebService
sessionJava As JAVASESSION      
classJava As JAVACLASS
objectJava As JAVAOBJECT        
methodJava As JAVAMETHOD

Public Sub New(logDTO As LogWSDTO)
    Set sessionJava = New JAVASESSION
    Set classJava = sessionJava.GetClass("com.pasquier.DAO.WS/ILogWebService")
    Set objectJava = classJava.CreateObject     
End Sub

Public Sub notesMain()
    Set methodJava = classJava.GetMethod("notesMain","()V")     
    objectJava.notesMain        
End Sub 

End Class

my java interface:

package com.pasquier.IDAO.WS;

import com.pasquier.MesExceptions.ExceptionWS;

public interface ILogWebService {

void notesMain() throws ExceptionWS;    

}

my java class :

public class LogWebService implements ILogWebService {
private Session session;
private LogWSDTO logDTO = null;

public LogWebService(LogWSDTO log) {
    this.logDTO = log;
}


public void notesMain() throws ExceptionWS  {
    try 
    {
        try 
        {           
            session =  WebServiceBase.getCurrentSession();              
                        ILogWSDAO ilog = FactoryWS.getInstance().createLogWSDAO(session, logDTO);

            ilog.ajouterLog();  
        } finally
        {
            UtilMemoire.purgeMemoire(); 
        }
    } catch (NullPointerException e1)
    {   
        System.out.println("ERREUR NullPointer sur NotesMain sur LogWebService");
        throw new ExceptionWS("ERREUR NullPointer sur NotesMain sur LogWebService", e1, logDTO);
    } catch (ExceptionWS e) 
    {
        System.out.println( e.getMessage());        
    } catch (Exception e2) 
    {           
        System.out.println("ERREUR sur NotesMain sur LogWebService " + e2.getMessage());    
        throw new ExceptionWS("ERREUR sur NotesMain sur LogWebService " + e2.getMessage(), e2, logDTO);
    }
}

my lotuscript :

Class LogGetAllDocumentsDAO

Private ilog As ILogWebService 
Private logDTO As LogWSDTO
Private docDTO As LogGetAllDocumentsDTO
Private utilDt As UtilDate

Public Sub New(logWSparam As LogWSDTO, docDTOparam As LogGetAllDocumentsDTO)
    Set Me.logDTO = logWSparam
    Set Me.docDTO = docDTOparam

End Sub 

Public Sub appelLog()       
    Call ajouterParam()         
    Set ilog = New ILogWebService(logDTO)       
    'Call ilog.notesMain()
End Sub 

Public Sub ajouterParam()

    Dim retourWS As String 
    Dim paramWS As String

    'paramètres d'envoi de web services
    paramWS = "dbserver=" + docDTO.getDbServer
    paramWS = paramWS + "dbpath="  + docDTO.getDbPath
    paramWS = paramWS + "nomVue="  + docDTO.getNomVue
    paramWS = paramWS + "key="  + docDTO.getKey
    paramWS = paramWS + "sep="  + docDTO.getSep

    logDTO.setParamWS(paramWS)

    'paramètres de retour de web services
    retourWS = "Nombre d'Identifiants : ...." 
    logDTO.setRetourWS(retourWS)

    'logDTO.setDateHeureDebutWS(utilDt.dateTimeDuJour(session, logDTO))     

End Sub     

End Class

thanks for your help !!

i come back because it didn't work i try this to call my class as if the interface doesn't exist but it says error : "LS2J Error : Java constructor failed to execute"

Sub Click(Source As Button)
Dim mySession As JAVASESSION    
Dim myClass As JAVACLASS
Dim myObjet As JAVAOBJECT
Dim toto As String
Dim toto2 As String
Dim myCollection As JavaMethodCollection
Dim myMethod As JavaMethod
Dim myMethod2 As JavaMethod
toto2 = "test"

Set mySession = New  JAVASESSION        
Set myClass = mySession.GetClass("com.pasquier.launcher/LogWebService") 
Set myObjet = myClass.CreateObject("(Lcom/pasquier/DTO/WS/LogWSDTO;)V")


Messagebox (toto2)

Set myCollection = myClass.getClassMethods()

Set myMethod = myCollection.getFirst()
Do
    If myCollection.count <> 0 Then 
        toto2 =  myMethod.MethodName & " " & myMethod.signature
        Messagebox (toto2)          

    End If
    Set myMethod = myCollection.getNext()       
Loop While myCollection.current <> 1    

End Sub

could you help me ?


Solution

  • Let us assume we have a package com.pasquier.IDAO.WS and the following classes/interface in it:

    LS2J is a strange thing and does not work as it should be in some cases. And a developer reacts like this when LS2J reports an error which should not be occured.

    We assume that LogWSDTO has default constructor, so we create its instance:

    Set logWSDTOClass = javaSession.getClass("com/pasquier/IDAO/WS/LogWSDTO")
    Set logWSDTOObject = logWSDTOClass.CreateObject()
    

    Then we create object, that uses this log in it's constructor.

    We get the class:

    Set logWebServiceClass = javaSession.getClass("com/pasquier/IDAO/WS/LogWebService")
    

    And calling:

    Set logWebServiceObject = logWebServiceClass.CreateObject("(Lcom/pasquier/IDAO/WS/LogWSDTO;)V", logWSDTOObject)
    

    TA-DAA!, that's does not work. We get a famous "Java constructor failed to execute". Why? Because the constructor has one parameter with a correct type and this error should not occur. But it is LS2J, and we need to suffer :)

    So, we need a workaround to solve this task.

    We are modifying our LogWebService class to declare a default constructor, and we're adding a new method:

    public void setLog(LogWSDTO log) {
       this.logDTO = log;       
    }
    

    After that we do:

    Set logWebServiceObject = logWebServiceClass.CreateObject()
    Call logWebServiceObject.setLog(logWSDTOObject)
    Call logWebServiceObject.notesMain()
    

    It works. Magic.

    Below there is listing of all classes/code you will need for this example:


    package com.pasquier.IDAO.WS;
    
    public class LogWSDTO {
    
        public LogWSDTO() {
            System.out.println("@@@ LogWSDTO constructor called!");
        }
    
        public void log() {
            System.out.println("@@@ LogWSDTO DO called!");
        }
    
    }
    

    package com.pasquier.IDAO.WS;
    
    public interface ILogWebService {
        void notesMain();
    }
    

    package com.pasquier.IDAO.WS;
    
    import lotus.domino.Session;
    
    public class LogWebService implements ILogWebService {
    
        private Session session;
        private LogWSDTO logDTO = null; 
    
        public LogWebService() {
            System.out.println("LogWebService: Default Constructor called!");
        }
    
        public void setLog(LogWSDTO log) {
            this.logDTO = log;      
        }
    
        public LogWebService(LogWSDTO log) {
            this.logDTO = log;
            System.out.println("~~~ LogWebService.constructor!!!");
        }
    
        public void notesMain() {
            // TODO Auto-generated method stub
            System.out.println("~~~ LogWebService.notesmain called!!!");
            this.logDTO.log();
    
        }
    }
    

    And lotusscript code:

    Sub Click(Source As Button)
        Dim javaSession As New JAVASESSION
        Dim javaError As JAVAERROR
    
        On Error Goto ErrorHandling
    
        Dim logWSDTOClass As JAVACLASS
        Dim logWSDTOObject As JavaObject
    
        Dim logWebServiceClass As JAVACLASS
        Dim logWebServiceObject As JavaObject
    
        Set logWSDTOClass = javaSession.getClass("com/pasquier/IDAO/WS/LogWSDTO")
        Set logWSDTOObject = logWSDTOClass.CreateObject()
    
        Set logWebServiceClass = javaSession.getClass("com/pasquier/IDAO/WS/LogWebService")
    
        'Does not work, but should 
        'Set logWebServiceObject = logWebServiceClass.CreateObject("(Lcom/pasquier/IDAO/WS/LogWSDTO;)V", logWSDTOObject)
    
        'workaround
        Set logWebServiceObject = logWebServiceClass.CreateObject()
        Call logWebServiceObject.setLog(logWSDTOObject)
        Call logWebServiceObject.notesMain()
    
        Msgbox "done"
    
        Exit Sub
    ErrorHandling:
        Msgbox Error '
        Set javaError = javaSession.getLastJavaError
        Msgbox "Java error: " + javaError.ErrorMsg 
    End Sub
    

    To view output messages (via System.out.println) open Java Debug Console via menu "Tools - Show Java Debug Console". Sometimes this console does not show up and you need to restart lotus client and designer.