Wednesday, 12 November 2014

BTrace trace ClassLoader Leaking

If you want to trace the common ClassLoader leaking, you just probe where to invoke "ClassLoader#defineClass". The BTrace script as follow:
import com.sun.btrace.AnyType;
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class Trace {

   @OnMethod(clazz = "+java.lang.ClassLoader", 
             method = "defineClass")
    public static void traceClassLoaderLeak(@ProbeClassName String clazz, @ProbeMethodName String method, @TargetInstance Object instance) {
        println("\n==== java.lang.ClassLoader#defineClass ====");
        jstack();
    }
}

Thursday, 8 May 2014

Java client read content from URL with SSL

  • Java client
  • import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLSession;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URISyntaxException;
    import java.net.URL;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class PDFTester {
        private static final int FILE_BUFFER_SIZE = 2048;
        public static final String CONTENT_TYPE_PDF = "application/pdf";
        public static byte[] readPDFFromURL(URL url) throws IOException {
            System.setProperty("javax.net.ssl.trustStore", "C:/temp/myCA.jks");
            System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
    
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            httpsURLConnection.connect();
    
            byte[] buffer = new byte[FILE_BUFFER_SIZE];
            int lengthOfByteToBeWrite;
            try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                 InputStream inputStream = httpsURLConnection.getInputStream()) {
                while ((lengthOfByteToBeWrite = inputStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, lengthOfByteToBeWrite);
                }
                byteArrayOutputStream.flush();
                return byteArrayOutputStream.toByteArray();
            } catch (IOException e) {
                throw e;
            }
        }
    
        public static void main(String[] args) throws IOException, URISyntaxException {
            URL url = new URL("https://www.sample.com/pdf");
            byte[] bytes = readPDFFromURL(url);
            Path path = Paths.get("c:/temp/test.pdf");
            Files.write(path, bytes);
        }
    }
    
  • SSL base knowledge:
    Implementing SSL / TLS using Java

Sunday, 6 April 2014

Hide unnecessary Horizontal Scrollbar on jqgrid

use this css to remove it.



Thursday, 6 March 2014

Share value between main report, multi sub reports



How to shared value between multi crystal report sub report?
1.       Create two sub reports

2.       Define Formula Field (defineGlobalPersonName)  in subReport1

3.       Tricky: This formula field (defineGlobalPersonName) must use in somewhere in sub report (subReport1) where inject value into the shared variable. Otherwise, other sub report where reference the shared value (personName)  can’t get the value when printing records.


4.       Used shared variable in other sub report (subReport2)
(a)    Declare another “formula field (retreiveBackPersonName)” to retrieve value from above shared variable (personName)


(b)   Use this formula field (retreiveBackPersonName)  in your sub report (subReport2)
5.       Preview final report


6.       If you want shared value to main report as well.  You just only declare one “formula field (sharedValue)”  in main report. Then you can use it

 



Thursday, 6 February 2014

use external icons in Bootstrap

  • 1. Define you own css
  • .icon-pdf-file {
        background-image: url(../img/document-pdf.png);
        background-position: center center;
        height: 16px;
        width: 16px;
    }
       
  • 2. Use it in you page with &lgt;i>
  • 
    

Wednesday, 11 September 2013

create own support Ctrl+v, Ctrl+c, Ctrl+X and 'Numeric only' jquery plugin

(function ($) {
    $.fn.forceNumeric = function () {
        return this.each(function () {
            // bind handler of 'paste' event
            $(this).bind(
                {
                    paste: function (e) {
                        var content = e.originalEvent.clipboardData.getData('text/plain');
                        var regExp = /^\d+$/;
                        if (!regExp.test($.trim(content))) {
                            e.preventDefault();
                        }
                    }
                });
            $(this).keydown(function (e) {
                var key = e.which || e.keyCode;

                if (!e.shiftKey && !e.altKey) {
                    if ((key >= 48 && key <= 57 ||
                        // Numeric keypad
                        key >= 96 && key <= 105 ||
                        // comma, period and minus, . on keypad
                        key == 190 || key == 188 || key == 109 || key == 110 ||
                        // Backspace and Tab and Enter
                        key == 8 || key == 9 || key == 13 ||
                        // Home and End
                        key == 35 || key == 36 ||
                        // left and right arrows
                        key == 37 || key == 39 ||
                        // Del and Ins
                        key == 46 || key == 45) && !e.ctrlKey) {
                        return true;
                    }

                    // Allow: Ctrl+C, Ctrl+V, Ctrl+X
                    if ((e.keyCode == 67 || e.keyCode == 86 || e.keyCode == 88) &&
                        (e.ctrlKey === true || e.metaKey === true)) {
                        return true;
                    }
                }

                return e.preventDefault();
            });
        });
    };
}(jQuery));

Monday, 26 August 2013

tips of jqgrid

  • Define own date formatter
  • $.extend($.fn.fmatter , {
        dateFormatter : function(cellvalue, options, rowObject) {
            if (cellvalue) {
                // parseExact just returns 'null' if the date you are trying to
                // format is not in the exact format specified
                var parsedDate = Date.parseExact(cellvalue, "yyyy-MM-ddTHH:mm:ss");
                if(parsedDate == null )
                    parsedDate = new Date(cellvalue);
    
                // if parsed date is null, just used the passed cell value; otherwise,
                // transform the date to desired format
                var formattedDate = parsedDate ? parsedDate.toString("yyyy-MM-dd HH:mm:ss") : cellvalue;
    
                return formattedDate;
            } else {
                return '';
            }
        }
    });
    
  • Add 'link' column to table
  • $.extend($.fn.fmatter , {
        dynamicLink : function (cellValue, options, rowObject) {
            return "<a href='#' onclick=\"viewReport('" + rowObject.custodyRefNumber + "', '" + rowObject.docketNumber + "');return false;\">view report</a>";
        }
    });
    
  • dynamic setting width/height to match parent component
  • function resizeJqgridWidth() {
        if (grid = $('.ui-jqgrid-btable:visible')) {
            grid.each(function(index) {
                gridId = $(this).attr('id');
                gridParentWidth = $('#gbox_' + gridId).parent().width() - 5;
                $('#' + gridId).setGridWidth(gridParentWidth);
            });
        }
    }
    
  • convert string to javascript json object
  • function convertDotNotationStringToNestJsonObject(data) {
        var obj = {}, t, parts, part;
        for (var k in data) {
            t = obj;
            parts = k.split('.');
            var key = parts.pop();
            while (parts.length) {
                part = parts.shift();
                t = t[part] = t[part] || {};
            }
            t[key] = data[k]
        }
        return obj;
    }