Saturday, December 15, 2012

Adding user in MySQL and give grant


CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL ON db1.* TO 'jeffrey'@'localhost';

Backup database using PostgreSQL

pg_dump dbname | gzip > filename

Create servlet to serving Download


This tutorial shows how to create a link button on web for "Download". You will need have a servlet on the server side to provide this feature. In the end of this tutorial you will have a download link to download a text file.

Create a servlet

public class DownloadServlet extends HttpServlet {

 

  protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    // Set the response content type

    response.setContentType("text/plain"); // for text file

 

    response.addHeader("Content-disposition","attachment; filename=yourfilename.txt");

 

    out.print("HELLO");

    out.close();

 

 }

 

}

Register servlet in web.xml

<web-app>

 

   <servlet>

      <servlet-name>DownloadServlet</servlet-name>

      <servlet-class>com.mycompany.DownloadServlet</servlet-class>

   </servlet>

 

<servlet-mapping>

      <servlet-name>DownloadServlet</servlet-name>

      <url-pattern>/download</url-pattern>

   </servlet-mapping>

</web-app>

Create HTML page

<a href="download">Go to download</a>


Create logging using log4j

Put log4j.properties in your classpath


#Set the level of the root loger to DEBUG and set its appender log4j.rootCategory=DEBUG, CONSOLE, DAILY log4j.logger.com.dappit.Dapper.parser=ERROR log4j.logger.org.w3c.tidy=FATAL
# CONSOLE - Console Appender log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender #set the layout for the appender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n # FILE - File Appender #log4j.appender.FILE=org.apache.log4j.FileAppender #log4j.appender.FILE.File=logs/jlego.log # Define the layout for file appender #log4j.appender.FILE.layout=org.apache.log4j.PatternLayout #log4j.appender.FILE.layout.conversionPattern=%m%n # DAILY - DailyRollingFile Appender log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender log4j.appender.DAILY.File=${catalina.home}/logs/mylog.log log4j.appender.DAILY.DatePattern='.'yyyy-MM-dd # Define the layout for DAILY file appender log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout log4j.appender.DAILY.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n # Rolling file appender in HTML #------------------------------------------------------------------------------ # # The following properties configure the Rolling File appender in HTML. # See http://logging.apache.org/log4j/docs/api/index.html for details. # #------------------------------------------------------------------------------ log4j.appender.HTML=org.apache.log4j.RollingFileAppender log4j.appender.HTML.File=${catalina.home}/logs/mylog.html log4j.appender.HTML.MaxFileSize=100KB log4j.appender.HTML.Append=false log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout log4j.appender.HTML.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n


Set this

log4j.logger.org.w3c.tidy=FATAL

Will cause org.w3c.tidy log only will shown if FATAL occurs

Saturday, September 8, 2012

Java Developer / Mobile Web Developer Needed

Java Developer Needed


  • at least have little experience in Java
  • will have training for enablement
  • will work with our framework, including using Spring, JPA Hibernate, Java Server Faces, Maven, etc.
  • Mail me now for detail at boylevantz@gmail.com
Mobile Web Developer Needed
  • have experience in HTML, CSS is a must
  • strong experience in Javascript, jQuery
  • experience in jQuery mobile would be an advantages
  • Mail me now for detail at boylevantz@gmail.com

C# Developer Needed

C# Developer Needed

Check this out!
www.contenga.net/career/
http://projectgetaway.com/jobs/?page_id=929

Saturday, August 25, 2012

Orion BB Start! for BlackBerry Java Development


Start a project on google code for BlackBerry Java Development
http://code.google.com/p/orion-bb/

For BlackBerry Java Development


Features

  • Splash Screen
  • Advanced UI Components such as Accordions, Tab, Tooltip, etc.
  • Barcode scanner
  • More features
Anyone interested in contributing please email me at boylevantz@gmail.com


Check it out!


Monday, August 13, 2012

Executing Javascript code in Java

Here some snippet how to execute Javascript code in Java


final String script = "function test(){ return 'abc' + 'def'; } test();";

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

Object result = engine.eval(script);

Assert.assertEquals("java.lang.String", result.getClass().getName());
Assert.assertEquals("abcdef", result.toString());

And some other code


final String script = "function execute(){ return { 'U.1' : function(){ return { 'u': '2', 'r': '', 'f':'U' }; }, 'R.5': function(){ return 'B' } } } execute();";

ScriptEngine js = new ScriptEngineManager().getEngineByExtension("js");
js.getContext().setAttribute("out", System.out, ScriptContext.ENGINE_SCOPE);

Object jsResult = js.eval(script);
NativeObject obj = (NativeObject) jsResult;
Object objUserRole = NativeObject.getProperty(obj, "U.1");

SimpleBindings bindings = new SimpleBindings();
bindings.put("foomethod", objUserRole);
log.debug("RESULT: " + js.eval("foomethod();", bindings));
log.debug("RESULT: " + js.eval("foomethod().u", bindings));
log.debug("RESULT: " + js.eval("foomethod().r", bindings));
log.debug("RESULT: " + js.eval("foomethod().f", bindings));

Assert.assertEquals("2", js.eval("foomethod().u", bindings).toString());
Assert.assertEquals("", js.eval("foomethod().r", bindings).toString());
Assert.assertEquals("U", js.eval("foomethod().f", bindings).toString());

objUserRole = NativeObject.getProperty(obj, "R.5");
bindings.put("foomethod", objUserRole);
log.debug("RESULT: " + js.eval("foomethod();", bindings));
Assert.assertEquals("B",js.eval("foomethod();", bindings).toString());

Using Expression Language

This is an example using expression language in Java



import javax.el.ValueExpression;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.jlego.core.Dto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.odysseus.el.util.SimpleContext;

public class TestExpression extends TestCase {

private static final Logger log = LoggerFactory.getLogger(TestExpression.class);

public void testJuel(){
java.util.Properties properties = new java.util.Properties();
properties.put("javax.el.cacheSize", "5000");
javax.el.ExpressionFactory factory = new de.odysseus.el.ExpressionFactoryImpl(properties);

Map map= new HashMap();
map .put("data", "ali");

SimpleContext context = new SimpleContext();
context.setVariable("request", factory.createValueExpression(map, HashMap.class));
ValueExpression expr = factory.createValueExpression(context, "${request.data == 'ali'}", Object.class);

Assert.assertEquals("true", expr.getValue(context).toString());

ValueExpression expr2 = factory.createValueExpression(context, "${ (1==0) ? 'ali' : ((2==2) ? 'not not ali' : 'not ali') }", Object.class);

Assert.assertEquals("not not ali",expr2.getValue(context));
}
}

I'm using Maven, so make sure you have these dependencies:


<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel</artifactId>
<version>2.1.3</version>
</dependency>

Of course JUnit as well


<dependency>
        <groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>



Monday, July 23, 2012

Sorting your pom.xml

When you are using Maven, it's better to sort your pom.xml based on Maven pom.xml reference.
There's an useful plugin for that.

Put the plugin on your pom.xml, inside build element.

<build>

 <plugins>
      <plugin>
        <groupId>com.google.code.sortpom</groupId>
        <artifactId>maven-sortpom-plugin</artifactId>
        <version>1.3.1</version>
        <executions>
          <execution>
            <phase>verify</phase>
            <goals>
              <goal>sort</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
</plugins>
</build>


Then run mvn sortpom:sort

Sunday, April 15, 2012

Configuring Axis 2 to enable JSON

Edit conf/axis2.xml, find for <messageFormatters>


<messageFormatters>
        <messageFormatter contentType="application/x-www-form-urlencoded"
                          class="org.apache.axis2.transport.http.XFormURLEncodedFormatter"/>
        <messageFormatter contentType="multipart/form-data"
                          class="org.apache.axis2.transport.http.MultipartFormDataFormatter"/>
        <messageFormatter contentType="application/xml"
                          class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>
        <messageFormatter contentType="text/xml"
                          class="org.apache.axis2.transport.http.SOAPMessageFormatter"/>
        <messageFormatter contentType="application/soap+xml"
                          class="org.apache.axis2.transport.http.SOAPMessageFormatter"/>
         
        <!-- Enabling JSON -->       
        <messageFormatter contentType="application/json"
                         class="org.apache.axis2.json.JSONMessageFormatter"/>
    </messageFormatters>

Find for  <messageBuilders>

<messageBuilders>
        <messageBuilder contentType="application/xml"
                        class="org.apache.axis2.builder.ApplicationXMLBuilder"/>
        <messageBuilder contentType="application/x-www-form-urlencoded"
                        class="org.apache.axis2.builder.XFormURLEncodedBuilder"/>
        <messageBuilder contentType="multipart/form-data"
                        class="org.apache.axis2.builder.MultipartFormDataBuilder"/>
                         
        <!-- Enabling JSON -->               
        <messageBuilder contentType="application/json"
                         class="org.apache.axis2.json.JSONOMBuilder"/>
                         
    </messageBuilders>

Add some library if is not exist in the lib folder
  • axis2-json-1.6.0.jar
  • jettison-1.0-RC2.jar
You may need this axis2-transport JAR files
  • axis2-transport-http.jar
  • axis2-transport-local.jar
  • axis2-transport-tcp.jar