Configure JNDI/JDBC(MySQL) connection in JBoss

Small snippet that shows how you configure a JNDI connection in JBoss. This connection is a MySQL/JDBC connection. Just name it mysql-ds.xml and but it in the deploy folder on your JBoss-Server.

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/translations</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/translationsdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password></password>
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

Dart: Calculate all occurrences of a substring

I recently wanted to calculate all occurrences of a substring in a string with Dart. The code I used is nearly the same as you would use in Java to do this. So here’s my snippet for Dart:

int countOccurences(mainString, search) {
 int lInx = 0;
 int count =0;
 while(lInx != -1){
 lInx = mainString.indexOf(search,lInx);
 if( lInx != -1){
 count++;
 lInx+=search.length;
 }
 }
 return count;
}

You can now simply call it like that:

int countAll = countOccurences(sameRandomString, "smile");

Send Mail via Java

I recently needed to send an email via Java. This was the first time i tried to do this, so I’ll post the code here. Maybe someone can use this one day.

First of all, you need to get JavaMail library here: http://www.oracle.com/technetwork/java/javamail/index-138643.html

Then you can create a class called “Mail” or “MailService”. Then paste the following code:

import java.util.Properties;
 import javax.mail.Authenticator;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;

public class Mail {
 private static final String EMAIL = "user@gmail.com";
 private static final String PASSWORD = "supersecretpassword";
 private static final String USERNAME = "totallysillyusername";
 private static final int SMTP_PORT = 25;
 private static final String SMTP_HOST = oldschoolemailhost.com";

public static void send(String recipient, String subject, String text) throws AddressException, MessagingException {
 Properties properties = System.getProperties();
 properties.setProperty("mail.smtp.host", SMTP_HOST);
 properties.setProperty("mail.smtp.port", String.valueOf(SMTP_PORT));
 properties.setProperty("mail.smtp.auth", "true");
 Session session = Session.getDefaultInstance(properties, new MailAuthenticator(USERNAME, PASSWORD));
 MimeMessage msg = new MimeMessage(session);
 msg.setFrom(new InternetAddress(EMAIL));
 msg.setRecipients(Message.RecipientType.TO,
 InternetAddress.parse(recipient, false));
 msg.setSubject(subject);
 msg.setText(text);
 Transport.send(msg);
}
}

class MailAuthenticator extends Authenticator {
 private final String user;
 private final String password;

public MailAuthenticator(String user, String password) {
 this.user = user;
 this.password = password;
 }

@Override
 public PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(user, password);
 }
 }

Something special about this code is the MailAuthenticator. You normally don’t write an extra class for this, but it makes the code more readable I think.

Then you can call this class everywhere like that:

String recipient = "bobama@me.com";
String subject = "topic";
String text = "smile =)";

Mail.send(recipient, subject, text);

And thats it, it’s really easier then I expected. Try it out =).

Eclipse doesn’t highlight references anymore

I had this problem this morning. My eclipse wasn’t highlighting references anymore. The fix for this problem is quite easy. Just go to Window -> Preferences -> Java -> Editor -> Mark Occurrences an activate it there:

Screen Shot 2013-03-02 at 08.04.34