azizkhani.net

I know that I know nothing

Getting full string stack trace including inner exception

clock March 22, 2015 21:46 by author Administrator

public static String joinStackTrace(Throwable e) {
        StringWriter writer = null;
        try {
            writer = new StringWriter();
            joinStackTrace(e, writer);
            return writer.toString();
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (IOException e1) {
                    // ignore
                }
        }
    }

    public static void joinStackTrace(Throwable e, StringWriter writer) {
        PrintWriter printer = null;
        try {
            printer = new PrintWriter(writer);

            while (e != null) {

                printer.println(e);
                StackTraceElement[] trace = e.getStackTrace();
                for (int i = 0; i < trace.length; i++)
                    printer.println("\tat " + trace[i]);

                e = e.getCause();
                if (e != null)
                    printer.println("Caused by:\r\n");
            }
        } finally {
            if (printer != null)
                printer.close();
        }
    }



Use Spring JavaMailSender and Freemarker to send Email by template

clock March 18, 2015 19:32 by author Administrator

 

<bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="mailSender">
  <property name="host" value="${mail.host}"></property>
  <property name="username" value="${mail.username}"></property>
  <property name="password" value="${mail.password}"></property>
  <property name="javaMailProperties">
    <props>
      <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
      <prop key="mail.smtp.port">${mail.smtp.port}</prop>
      <prop key="mail.smtp.host">${mail.host}</prop>
      <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
  </property>
</bean>

 

<bean class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean" id="freemarkerConfiguration">
  <property name="preferFileSystemAccess" value="false"></property>
  <property name="templateLoaderPath" value="classpath:/"></property>
</bean>

 

 

 


 
import java.util.Map;
 
import javax.mail.internet.MimeMessage;
 
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
 
import freemarker.template.Configuration;


 @Service("mailService")
public class MailService {
 @Autowired
  private JavaMailSender javaMailSender;

@Autowired
  private Configuration freemarkerConfiguration;
  
  public void sendMail(final String from, final String to, final String subject, final Map<string object=""> model, final String template){
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
           public void prepare(MimeMessage mimeMessage) throws Exception {
              MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
              message.setFrom(from, "azizkhani");
              message.setTo(to);
              message.setSubject(subject);
              //template sample: "freemarker/confirm-register.ftl"
                String text =FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(template,"UTF-8"), model);
              message.setText(text, true);
           }
        };
    javaMailSender.send(preparator);
  }
 
}

 



freemarker template engine ------try catch if else loop

clock March 18, 2015 12:58 by author Administrator

 

i think one of best solution for generate code is using template engine like freemarker.
for using try catch or if else and some other expression in it using like this

<#attempt>
attempt block

<#recover>
recover block
</#attempt>



<#if x == 1> x is 1
<#elseif x == 2> x is 2
<#elseif x == 3> x is 3
</#if>


<#list fields as field>
<#list>




this template generate fileds for hibernate hbm.xml

<#list fields as field>
<#attempt>
<#if "${field.type.superclass}"=="class org.model.BaseEntity">
<many-to-one name="${field.name}" column="${field.name}" entity-name="${field.type.name}" not-null="true" />
<#else>
<property column="${field.name}" name="${field.name}" type="${field.type.simpleName}" not-null="false" />
</#if>
<#recover>
<property column="${field.name}" name="${field.name}" type="${field.type.simpleName}" not-null="false" />
</#attempt>
</#list>

 



How to determine and print jQuery version?

clock March 13, 2015 15:51 by author Administrator
$.fn.jquery


About the author

 Welcome to this web site . This page has two purposes: Sharing information about my professional life such as articles, presentations, etc.
This website is also a place where I would like to share content I enjoy with the rest of the world. Feel free to take a look around, read my blog


Java,J2EE,Spring Framework,JQuery,

Hibernate,NoSql,Cloud,SOA,Rest WebService and Web Stack tech...

RecentPosts

Month List

Sign In