java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder

0

Posted by Jagadeesh VP | Posted in

In hibernate this error is common.

Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)

The hibernate distribution package have slf4j-api-x.x.x.jar file which does not have the impl package.

To avoid this error download slf4j-x.x.x.zip / .gz package from http://slf4j.org/download.html.

In this package you will find slf4j-simple-x.x.x.jar which has this org.slf4j.impl package. Add this jar in your classpath and run your application.

Hibernate Manny-to-Many Mapping

0

Posted by Jagadeesh VP | Posted in


package com.vpj;

import java.util.Set;
import com.vpj.Boo;

public class Foo {
private String fooId;
private Storing name;

private Set<Boo> boo;

/* Getters & Setters */
}

package com.vpj;

import java.util.Set;
import com.vpj.Foo;

public class Boo {
private String booId;
private String name;

private Set<Foo> foo;

/* Getters & Setters */
}

.hbm.xml :

...
<class type="com.vpj.Foo">
<id name="id" column="id">
<genetator class="increment" />
</id>

<property name="name" type="string" column="name" />

<set name="boo" table="Foo_Boo">
<key column="fooId" />
<many-to-many column="booId" class="Boo" />
</set>
</class>

<class type="com.vpj.Boo">
<id name="id" column="id">
<generator class="increment" />
</id>

<property name="name" type="string" column="name" />

<set name="foo" table="Foo_Boo">
<key column="booId" />
<many-to-many column="fooId" class="Foo" />
</set>
</class>
...

Attachment - Java Mail

0

Posted by Jagadeesh VP | Posted in


Session session = createMailSession(); // Create session with your
authentication data
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@vpj.com"));
message.setRecipients(new InternetAddress("to@vpj.com"));
message.setSubject("[SUBJECT]");

BodyPart textPart = new MimeBodyPart();
textPart.setText(emailBody);

Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(textPart);

DataSource source = new FileDataSource("[FILE_PATH]");

BodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName("[FILE_NAME]");

multiPart.addBodyPart(attachmentPart);

message.setContent(multiPart);

Transport.send(message);

Authentication - Java Mail

0

Posted by Jagadeesh VP | Posted in ,


import java.net.URLEncoder;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class MyMail {

private String d_email = "";
private String d_password = "";

public void sendMail(String tomailid, String subject, String message)
throws Exception {

Properties mailProps = new Properties();

mailProps.load( this.getClass().getResourceAsStream("data/MailData.properties"));

d_email = mailProps.getProperty("smtp.user");
d_password = mailProps. getProperty("smtp.password");

Authenticator auth = new SMTPAuthenticator();

Session session = Session.getInstance(mailProps, auth);

MimeMessage msg = new MimeMessage(session);
msg.setHeader("Content-Type", "text/html;");

InternetAddress from = new InternetAddress(mailProps.get("mail.user").toString(),
"[EMAIL_HEADING]");
msg.setFrom(from);
InternetAddress to = new InternetAddress(tomailid);

msg.addRecipient(Message.RecipientType.TO, to);
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress("emailid@test.in"));
msg.setSubject(subject);
msg.setContent(message, "text/html");

Transport.send(msg);
}

private class SMTPAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}

}

InputStream to String

0

Posted by Jagadeesh VP | Posted in ,


import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class StreamToString {

public static void main(String[] args) throws Exception {
StreamToString sts = new StreamToString();

InputStream is = sts.getClass().getResourceAsStream("/data.txt");

/*
* Call the method to convert the
* stream to string
*/
System.out.println(sts.convertStreamToString(is));
}

public String convertStreamToString(InputStream is) throws IOException {
/*
* To convert the InputStream to String
* we use the BufferedReader.readLine()
* method. Each line will appended to a
* StringBuilder and returned as String.
*/
if (is != null) {
StringBuilder sb = new StringBuilder();
String line = null;

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");

} catch (Exception e) {
//Do something..
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
}
}

GWT Startup

0

Posted by Jagadeesh VP | Posted in




Gwt [Server & Client Side] :

Models :

Pojos with Serializable implementation


RPC :


RemoteInterface --> Class extends the com.google.gwt.user.client.rpc.RemoteService
RemoteAsynInterface --> Class [RemoteIntefaceName]+"Async"
All the methods in RemoteInterface should defined in this interface with an extra parameter AsyncCallback.


Server : [ServerClass]


Class should implement RemoteInterface and extends com.google.gwt.user.server.rpc.RemoteServiceServlet


web.xml :


Tells the default page.
Also ServletClass & Mapping.

CallBack: [CallbackClass]


This the the class responsible for AJAX type updates to clients from Server (i.e., ServerClass)
CallbackClass Should implements com.google.gwt.user.client.rpc.AsyncCallback
Two methods:
public void onSuccess(T resultToClient)
The interface is parametrized, thus it supports strong typing. We can proceed with logic instead of worrying about type-casting.
public void onFailure(Throwable exception);


Client:


Probably like a Backing-Bean which has all the component's reference/Object that our page should have.
It should implement com.google.gwt.core.client.EntryPoint
This EntryPoint has the method onLoadModule()
We can override this method to load components in our page.



Project XML : [Project_Name.gwt.xml]


Specifies the compiler which is the Entry_point class.
It also specifies the name of other applications [via tag] in the same container, to which this application has the access.
It also has the URL - Servlet Mapping [As in struts-config file], to which URL which servlet/class is invoked.

[Typically like a WSDL file, since this also the remote call]



HTML Page:


GWT will generate this file, no need to take care on this.