Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Example of Annotation: Library
 Post subject: Example of Annotation: Library Post rating: 0   Post Posted: Fri 15 Aug, 2008, 07:41 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Could someone please give a simple example of how the 'Library' annotation should be used? I think it might be useful in my case, but I'm not too clued up on its functionality.
Thanks!


 
 Post subject: Re: Example of Annotation: Library? Post rating: 0   Post Posted: Fri 15 Aug, 2008, 08:45 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Take a look at this:

package jforex;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.dukascopy.api.*;

@Library("C:\\temp\\mail-1.4.jar")
public class TestMail implements IStrategy {

    @Configurable("SMTP Server")
    public String smtpServer = "localhost";


    private IContext context;

    private IConsole console;

    private IEngine engine;

    private IChart chart;

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStart(IContext context) throws JFException {
        this.context = context;
        this.engine = context.getEngine();
        this.console = context.getConsole();
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        try {
            print(smtpServer);
            sendMail("[email protected]", "[email protected]", "Hello", "SWFX");
        } finally {
            context.stop();
        }
    }

    private void print(String str) {
        console.getOut().println(str);
    }

    public boolean sendMail(String from, String to, String subj, String text) throws JFException {
        try {
            Properties props = new Properties();//System.getProperties();
            // Attaching to default Session, or we could start a new one
            props.setProperty("mail.smtp.host", smtpServer);
            Session session = null;
            session = Session.getInstance(props);
            // Create a new message
            Message msg = new MimeMessage(session);
            // Set the FROM field
            msg.setFrom(new InternetAddress(from));
            // Set the TO fields
            msg.setRecipient(RecipientType.TO, new InternetAddress(to));
            // Set the subject and body text
            msg.setSubject(subj);
            msg.setText(text);
            // Set some other header information
            msg.setHeader("SWFX-Mailer", "SWFX");
            msg.setSentDate(new Date());
            // Send the message
            Transport.send(msg);
            return true;
        } catch (Exception e) {
            throw new JFException(e);
        }
    }

}


This strategy sends email to [email protected]. To do so, it needs JavaMail API library that is not part of standard library set of JForex and have to be downloaded manually from here. You need to provide full path to the downloaded library in @Library annotation.


 
 Post subject: Re: Example of Annotation: Library? Post rating: 0   Post Posted: Mon 18 Aug, 2008, 00:50 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Thank you for the helpful reply! Unfortunately when I tried this, I got the following runtime error:

2008-08-18 11:18:55 Exception: java.lang.IllegalArgumentException: URI scheme is not "file"

This was generated with the following commands:

import com.dukascopy.api.*;
import java.util.*;
import java.io.*;
import com.jinvoke.CallingConvention;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;
import com.jinvoke.Charset;

@RequiresFullAccess
@Library("C:\\SignalFilters\\jinvoke.jar")
public class JFTest implements IStrategy{
...(etc) }


I tried changing @Library("C:\\SignalFilters\\jinvoke.jar") to @Library("file://C:/SignalFilters/jinvoke.jar") and variants thereof, but it wouldn't compile. Could you give me an idea of how to fix this?

Thanks very much


 
 Post subject: Re: Example of Annotation: Library? Post rating: 0   Post Posted: Mon 18 Aug, 2008, 03:46 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Some more info: I was able to run the javamail example successfully, so can only imagine that it is this particular .jar file that is causing problems. I know for example that it extracts its own helper .dll, though I didn't think this should cause a problem. If you have any idea regarding what to do, please let me know.


 
 Post subject: Re: Example of Annotation: Library? Post rating: 0   Post Posted: Mon 18 Aug, 2008, 10:39 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Yeh, problem is with this particular jar. It tries to load dll using path determined by calling JInvoke.class.getProtectionDomain().getCodeSource().getLocation() and it is different than they expect, because we are using our own class loader to load strategies. Maybe this library has some possibility to load dll in some other way?


 
 Post subject: Possible to load dlls? Post rating: 0   Post Posted: Tue 19 Aug, 2008, 23:00 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Thanks Dmitry, that was helpful to know. I ended up scrapping the JInvoke library and writing JNI code directly to interact with a dll of mine, but I just get a runtime error without much of a description:

2008-08-20 09:19:56 JFTest.checkworks()I @ JFTest.checkworks(Native Method)

'checkworks()' is a very simple function that returns the integer 42 and it works fine when I call it from a stand-alone Java class. I use the System.load("C:\checkworkslib.dll") command to load the library, but maybe this doesn't work with the Dukascopy class loader? Do you know of any successful way to call native code, or do I have to turn to the FIX API?

Thanks


 
 Post subject: Re: Example of Annotation: Library? Post rating: 0   Post Posted: Wed 20 Aug, 2008, 06:57 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Sorry - in desperation I began reading through some websites where others have reported similar problems and realised that the package name is part of the 'mangled' JNI dll, which of course changes between my stand-alone class and the jforex one. So I'll make the requisite changes, test them and report back, in case someone ever has the same problem.


 
 Post subject: Re: Example of Annotation: Library? Post rating: 0   Post Posted: Thu 21 Aug, 2008, 05:52 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Yes, name-mangling was the problem. I just had to ensure that the JForex class name and my JNI-dll export function names matched. Everything works fine now.


 
 Post subject: Re: Example of Annotation: Library Post rating: 0   Post Posted: Mon 09 Aug, 2010, 11:16 

User rating: 0
Joined: Mon 09 Aug, 2010, 10:23
Posts: 5
Can you please post an updated version?

I have replaced the library files with the updated ones (the ones you mention no longer exist), and replaced the @Library directive with the following:
@Library("c:/Documents and Settings/User/My Documents/JForex/Strategies/files/commons-pool-1.5.4.jar;c:/Documents and Settings/User/My Documents/JForex/Strategies/files/commons-dbcp-1.4.jar;c:/Documents and Settings/User/My Documents/JForex/Strategies/files/mysql-connector-java-5.1.13-bin.jar")

I'm not sure what I'm doing wrong but I'm getting the following compilation error messages (re The type Properties at line 30):
10:11:32 The type Properties is ambiguous
10:11:32 Properties properties = new Properties();

Thanks in advance


 
 Post subject: Re: Example of Annotation: Library Post rating: 0   Post Posted: Thu 12 Aug, 2010, 14:59 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you please check the Property class import. The "type Properties is ambiguous" error could appear because of the duplicate import.


 
 Post subject: Re: Example of Annotation: Library Post rating: 0   Post Posted: Mon 23 Aug, 2010, 09:16 

User rating: 0
Joined: Mon 09 Aug, 2010, 10:23
Posts: 5
Support wrote:
Could you please check the Property class import. The "type Properties is ambiguous" error could appear because of the duplicate import.


(As far as I know) I do not have any "Property" class import. Here's the prefix of the code:

package jforex;

import com.dukascopy.api.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.text.*;

import java.util.*;
import java.io.*;
import java.sql.*;
import javax.swing.filechooser.*;

@RequiresFullAccess
@Library("c:/Documents and Settings/User/My Documents/JForex/Strategies/files/commons-pool-1.5.4.jar;c:/Documents and Settings/User/My Documents/JForex/Strategies/files/commons-dbcp-1.4.jar;c:/Documents and Settings/User/My Documents/JForex/Strategies/files/mysql-connector-java-5.1.13-bin.jar")


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com