ป้ายกำกับ

Programming (19) How to (15) Software Engineering (9) App. Guide (3) Theory (1)
แสดงบทความที่มีป้ายกำกับ Programming แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Programming แสดงบทความทั้งหมด

วันพฤหัสบดีที่ 3 พฤศจิกายน พ.ศ. 2559

JavaScript onload vs onpageshow

บันทึกไว้เพื่อเตือนความจำ

The onpageshow event is similar to the onload event, except that it occurs after the onload event when the page first loads. Also, the onpageshow event occurs every time the page is loaded, whereas the onload event does not occur when the page is loaded from the cache.

Reference

วันจันทร์ที่ 3 ตุลาคม พ.ศ. 2559

Writing Java Desktop Application with Android Studio

วันนี้พอจะมีเวลาและอยากจะเขียนโปรแกรมที่ติดค้างมานานแล้ว เป็นโปรแกรมภาษา Java และจะเขียนแบบง่ายๆเป็น Desktop Application แต่เนื่องจากห่างหายจากการเขียนแบบนี้มานานมาก เมื่อก่อนเคยใช้ NetBeans เขียนเป็นประจำ แต่ว่า ตั้งแต่บริษัท Sun Microsystems ขายกิจการไปให้บริษัท Oracle Corporation (ปีคศ. 2010) มันเหมือนเรารู้สึกเคว้งคว้างไปด้วย ไม่รู้ว่าอนาคตของภาษา Java จะเป็นอย่างไร ตั้งแต่นั้นก็ไม่ได้ใช้ NetBeans อีกเลย ก็หันมาใช้ Eclipse อยู่พักหนึ่ง แต่ก็ไม่ค่อยถูกใจมากนัก สุดท้าย 2-3 ปีมานี้ หันมาจับ Android Studio เสียส่วนใหญ่ วันนี้ก็เลยคิดอยากจะเขียน Java Desktop Application ด้วย Android Studio ขึ้นมา

เอ้า พอลองค้นๆดูว่าทำได้หรือเปล่า ก็พบว่ามีคนแนะนำไว้พอสมควรครับ แต่จะนำมาสรุปบันทึกไว้เพื่อช่วยจำและจะได้เป็นประโยชน์กับคนอื่นๆดังนี้

Create New Module

ปกติแล้ว Project ที่เราสร้างด้วย Android Studio นั้น จะเป็น Android Application ซึ่งเมื่อสร้างแล้วจะได้ Module แรกชื่อ app ขึ้นมา ในกรณีนี้ เราจะไม่ใช้ Module นี้ บางความเห็นที่ค้นๆมาบอกว่าให้ลบทิ้ง แต่ผมคิดว่าไม่จำเป็นต้องลบทิ้ง เผื่อว่าเราอาจจะเปลี่ยนใจพัฒนาเป็น Android Application ก็จะได้ไม่ต้องสร้างใหม่ให้ยุ่งยาก ก็ปล่อยไว้แบบนั้นไปก่อน

เมื่อต้องการสร้าง Java Application เราจะใช้วิธีสร้าง Module ใหม่ โดยเลือกเมนู File/New/New Module แล้วเลือก Module แบบ Java Library ดังภาพ
จากนั้นก็กรอก Library name (ชื่อของ Module) Java package name (ชื่อ Package หลัก) และ Java class name ชื่อคลาสไฟล์ที่จะถูกสร้างขึ้นไฟล์แรก

Set Main Class

เมื่อสร้าง Module เสร็จเรียบร้อยแล้ว เราจะได้คลาสไฟล์แรกมาด้วย ถ้าต้องการให้ไฟล์นี้เป็นโปรแกรมหลัก ให้เพิ่มฟังก์ชัน main() ไว้ที่ไฟล์นี้ เช่น
package com.example;

public class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
เมื่อเขียน main() เสร็จแล้วควร Build ด้วยเมนู Build/Make Module ... เสียก่อน จากนั้น เราจะสามารถรันโปรแกรมได้จากเมนู Run/Run... จะปรากฎป๊อบอัพเมนูให้เลือกรัน MyClass ดังภาพ
หากเคยเลือกรันไปแล้วครั้งหนึ่ง จะปรากฏชื่อคลาสตรงปุ่มข้างๆปุ่มรันที่ Toolbar ดังภาพ
แต่หากไม่ปรากฎชื่อคลาสหลักเพื่อจะรันโปรแกรม เราก็สามารถสร้างใหม่เองได้ด้วยการเลือก Edit Configurations... แล้วเพิ่ม Configuration แบบ Application ด้วยการกดปุ่มเครื่องหมาย + ตรงมุมบนซ้าย
กำหนด Name และ Main class ให้ถูกต้อง ก็จะได้ Configuration สำหรับรันโปรแกรมหลัก

Create Jar File with Dependencies

ปกติแล้วเมื่อ Build Module แล้วจะได้ Jar ไฟล์ของโปรแกรมอยู่ในโฟลเดอร์
<โปรเจ็คโฟลเดอร์>/<โมดูลโฟลเดอร์>/build/libs
เช่น AndroidStudioProjects/MyApplication/lib/build/libs เป็นต้น
แต่หากโปรแกรมมีการเรียกใช้ Library อื่นๆด้วย Jar ไฟล์ของ Library ที่จำเป็นต้องใช้งานร่วมกันนั้น อาจจะไม่ได้รวมอยู่ใน Jar ไฟล์เดียวกัน ซึ่งจะทำให้ไม่สะดวกในการนำไปใช้งานก็เป็นได้ ซึ่งเราสามารถเพิ่ม Task สำหรับการสร้าง All-in-One Jar ไฟล์ได้ ด้วยการแก้ไขไฟล์ build.gradle script ของโมดูลนี้ โดยเพิ่ม Task ชื่อ fatJar ดังนี้
version='1.0' 
task fatJar(type: Jar) {
manifest {
        attributes 'Implementation-Title': 'MyClass Jar File Example',
        'Implementation-Version': version,
        'Main-Class': 'com.example.MyClass'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}
 เมื่อเพิ่ม Task ชื่อ fatJar เข้าไปแล้ว และสามารถ Sync Gradle ได้อย่างถูกต้อง เราจะพบชื่อ Task นี้ในหน้าต่างของ Gradle Properties ดังภาพ
ซึ่งสามารถเรียกใช้ได้ด้วยการ Double-Click ที่ชื่อ หรือคลิ๊กเมาส์ปุ่มขวาแล้วเลือกเมนูรันได้เลย ซึ่งผลลัพธ์จะได้ Jar ไฟล์อยู่ในโฟลเดอร์เดียวกับ Jar ไฟล์ปกติ แต่ชื่อไฟล์จะมี -all เพิ่มเข้ามา

อ้างอิง

วันอาทิตย์ที่ 14 กันยายน พ.ศ. 2557

Visual Studio Image Library

เมื่อก่อนตอนสมัยที่ผมข้ามจาก Visual Studio 6 มาใช้ Visual Studio 2008 นั้น ผมเพิ่งจะได้รู้ว่า ไมโครซอฟต์ได้แถมคลังรูปภาพไว้ใช้สำหรับทำปุ่มต่างๆในสไตล์ของไมโครซอฟต์มาให้ เรียกว่า Visual Studio Image Library ซึ่งความจริง Image Library นี้มีมาตั้งแต่ Visual Studio รุ่น 2005 โดยไฟล์จะอยู่ในโฟลเดอร์ ดังตาราง

Visual Studio 2005C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary\VS2005ImageLibrary.zip
Visual Studio 2008C:\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\VS2008ImageLibrary.zip
Visual Studio 2010C:\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1033\VS2010ImageLibrary.zip

ตอนนั้นน่าจะมีภาพอยู่ประมาณ 1,000 ภาพ แต่ตั้งแต่ Visual Studio 2012 เป็นต้นมา ไมโครซอฟต์ไม่ได้รวมชุด Visual Studio Image Library ไว้ใน Released Package ด้วย ซึ่งผมก็ไม่ได้สนใจหามาเพราะมีไฟล์เก่าของ VS2008 อยู่ จนกระทั่งวันนี้ ผมได้ติดตั้ง Visual Studio 2013 และต้องการใช้ Image Library ขึ้นมา ก็เลยค้นหาดู พบว่า ไมโครซอฟต์มีให้ดาวโหลด อยู่ที่
http://www.microsoft.com/en-us/download/details.aspx?id=35825
เห็นเขียนไว้ในรายละเอียดว่ามีภาพถึง 5,000 ภาพ เช่น


 ใครสนใจก็ลองดาวโหลดมาใช้ดู แต่อย่าลืมอ่านเงื่อนไขการใช้งานด้วยนะครับ

อ้างอิง

  1. Microsoft Corporation, How to Get High-Quality Icons Using Visual Studio Image Library

วันอังคารที่ 1 กรกฎาคม พ.ศ. 2557

ข้อควรระวังในการใช้ Static References เมื่อเขียน API

ช่วงนี้ผมกำลังเขียนโปรแกรมเกี่ยวกับ Hadoop และ HBase อยู่ และกะว่าจะทำเป็น API สำหรับใช้งานกับโปรแกรมอื่นๆได้ด้วย บังเอิญว่า ผมต้องการใช้ Logging เพื่อให้สะดวกต่อการนำไปใช้และตรวจสอบปัญหา ซึ่งเมื่อ 10 กว่าปีก่อน ผมก็เคยศึกษามาบ้าง แต่เนื่องจากไม่ค่อยได้ใช้ จึงไม่ได้ลงลึกอะไรมากนัก แต่ตอนนี้ผมต้องเลือกว่าจะใช้ Framework ตัวไหนดี ที่ผมมองๆอยู่ก็มี 4 ตัว คือ
  1. Java Logging ซึ่งเป็นมาตรฐานหลักเพราะมีอยู่ใน JRE อยู่แล้ว ถ้าเอามาใช้ก็จะได้ไม่ต้องพ่วง Library อะไรให้ยุ่งยาก แต่ Java Logging ไม่ค่อยจะเก่งสักเท่าไหร่ การใช้งานจึงไม่สะดวกนัก
  2. Apache log4j ซึ่งเป็น Logging API ที่มีมานานมาก ถ้าจำไม่ผิดน่าจะก่อน Java Logging เสียอีก และเท่าที่ได้กลับไปดูหน้าเว็บของโครงการ Apache Logging Services พบว่ามีการพัฒนาไปเป็น Apache log4php log4net และ log4cxx อีกด้วย
  3. Apache หรือ Jakarta Commons Logging (JCL) มีแนวคิด ซึ่งออกแบบมาให้เป็น abstract หรือ wrapper เพื่อให้ผู้ใช้สามารถเลือกตัว Logger ที่ต้องการใช้งานจริงๆได้ ถอดเปลี่ยนได้ จึงสามารถใช้งานร่วมกับ Logger ตัวอื่นๆได้ เช่น log4j, Java Logging และ Avalon LogKit
  4. Simple Logging Facade for Java (SLF4J) ตัวนี้ HBase ใช้งานอยู่ ออกแบบมาให้เป็น abstract หรือ wrapper เช่นเดียวกับ JCL สามารถใช้งานร่วมกับ Logger ตัวอื่นๆได้ เช่น log4j, Java Logging และ Logback
แต่ประเด็นที่ผมนำมาเขียนบทความนี้ไม่ใช่ว่าจะเลือกตัวไหนดีนะครับ ประเด็นก็คือปัญหาเรื่องการใช้ static ในการสร้างตัว Logger ซึ่งมักจะทำไปเพราะเป็นที่คุ้นเคย เช่น
public class Root {
    private static Log log = LogFactory.getLog(Root.class);
    ....
}
การประกาศให้ตัว Logger เป็น static (class variable) จะช่วยให้ไม่สิ้นเปลือง เพราะมี reference เพียงแค่ตัวเดียวในระบบ ผิดกับการสร้าง Logger ที่ไม่เป็น static (instance variable) ทั้งนี้ ประสิทธิภาพจะแตกต่างกันมากน้อยก็ขึ้นอยู่กับว่า ในระบบของเรามี instance เกิดขึ้นมากแค่ไหน และมีการเรียกใช้ Logger บ่อยแค่ไหนด้วย

Stand-Alone Application

ประเด็นปัญหาของการสร้าง Logger เป็น static จะไม่เกิดขึ้นเลยหากโค้ดที่เราเขียนเป็น Stand-alone Application แต่จะเป็นปัญหาก็ต่อเมื่อ โค้ดที่เราเขียนเป็น API และถูกนำไปใช้งานโดย Application หลายๆตัวที่ทำงานอยู่บน Container เดียวกัน เช่น Web Application เนื่องจาก Web Server อย่าง Tomcat (Servlet Container) จะมีตัว ClassLoader หลายตัวในการโหลด Web Application แต่ละตัวขึ้นมา ซึ่งทำให้ Application แต่ละตัวใช้ Logger ตัวเดียวกันได้ (เนื่องจากเป็น static) เกิดปัญหาว่าข้อมูล Log ของ Application แต่ละตัวจะปะปนกัน ไม่สามารถแยกออกได้

ปัญหานี้ไม่ใช่จะเกิดขึ้นกับเฉพาะ Logger เท่านั้น แต่จะเกิดกับตัวแปร static อื่นๆด้วยเช่นกัน จึงทำให้ผมต้องคิดหนักขึ้นกับกรณีอื่นๆ และเป็นที่มาของการทดลอง เพื่อแสดงให้เห็นถึงผลลัพธ์ที่เกิดขึ้นกับปัญหานี้

CLASSPATH and ClassLoader

ก่อนอื่นขออธิบายเรื่อง ClassLoader สักนิดหนึ่ง เผื่อว่าบางคนอาจจะยังไม่เข้าใจนะครับ (สำหรับคนที่เข้าใจเรื่องนี้แล้วก็ข้ามหัวข้อนี้ไปได้เลย) การโหลดโปรแกรมมาทำงานของ java (Java Virtual Machine, JVM) จะทำโดย ClassLoader ซึ่ง ClassLoader หลักคือ Bootstrap ClassLoader จะทำการอ่านคลาสต่างๆที่จำเป็น และค้นหาคลาสไปตามโฟลเดอร์ต่างๆใน CLASSPATH ที่ตั้งไว้ โปรแกรมใดต้องการใช้คลาสอะไรบ้างก็ต้องระบุที่อยู่ของมันไว้ใน CLASSPATH ให้ถูกต้อง มิฉะนั้นจะเกิด ClassNotFoundException ขึ้นและโปรแกรมก็จะทำงานไม่ได้ นี่คือหลักปกติ ทุกอย่างจะต้องเตรียมไว้ให้ครบถ้วนก่อนที่จะรันโปรแกรม

แต่ในความเป็นจริงแล้ว กลไกนี้สามารถเพิ่มเติมหรือขยายขอบเขตของโปรแกรมออกไปได้ด้วยการสร้าง ClassLoader เพิ่มเติมในโปรแกรมของเรา อันนี้เป็นความพิเศษของภาษา Java เรียกว่า Dynamic Class Loading จะทำให้โปรแกรมมีความยืดหยุ่นสูง และการทำ Plug-in เป็นไปได้ง่ายมาก (รายละเอียดศึกษาเพิ่มเติมในเรื่อง ClassLoader และเรื่อง Reflection) เมื่อโปรแกรมของเราทำงานและมีการสร้าง ClassLoader เพิ่มเติมขึ้นมา โปรแกรมของเราก็จะสามารถโหลดคลาสใหม่ๆเข้ามาทำงานโดยที่คลาสนั้นๆไม่จำเป็นต้องอยู่ใน CLASSPATH และเราไม่จำเป็นต้องหยุดการทำงานของโปรแกรมเพื่อจัดการและรันใหม่ โดยที่กลไกการสร้าง ClassLoader นั้นจะกำหนดให้เราต้องสร้างและเชื่อมต่อกันเป็นทอดๆ กลายเป็น Tree ของ ClassLoader โดยมี Bootstrap ClassLoader เป็นจุดบนสุด (root) ของ Tree หาก ตัว ClassLoader ใด ต้องการหาคลาสที่จะโหลด จะต้องเรียกไปใหั Parent ของมันค้นหาก่อน และส่งต่อกันเป็นทอดๆ จนไปสุดที่จุดสูงสุดของ Tree หากหาไม่พบจึงจะค่อยๆหาย้อนกลับมาจนหมดแล้วจึงเกิด ClassNotFoundException แต่ถ้าหาพบโดย ClassLoader ตัวใด คลาสนั้นก็จะถูกโหลดมาด้วย CassLoader นั้นๆ นั่นหมายความว่า หากมีคลาสวางอยู่ใน CLASSPATH แม้เราตั้งใจจะให้โหลดจากที่อื่นก็จะทำไม่ได้ (ยกเว้นว่าเราจะสร้าง ClassLoader แบบของเราเองและไม่ทำตามกฎ)

ด้วยโครงสร้างอย่างนี้ คลาสที่ถูกโหลดเข้ามาในโปรแกรมด้วย ClassLoader ที่อยู่คนละกิ่งของ Tree จะมีขอบเขตของการมองเห็นคลาสอื่นๆจำกัดเฉพาะเส้นทางในกิ่งของตัวเองเท่านั้น แต่ก็จะมีจุดร่วมที่กิ่งมารวมกันเพราะมี Parent ร่วมกันอยู่ ตรงนี้จึงเป็นที่มาของปัญหาหากไม่จัดการให้ถูกต้อง

Preparations

source code ของโปรแกรมทดสอบ สามารถดาวโหลดได้ที่ Google Drive ไฟล์ที่ดาวโหลดมา เป็น zip ของโฟลเดอร์โปรเจ็คใน Eclipse เมื่อแตกไฟล์มาแล้ว ในโฟลเดอร์ src มี source code ทั้งหมดด้วยกัน 4 ไฟล์คือ
  1. StaticFieldTest.java เป็น main โปรแกรมที่จะรันทดสอบ
  2. test/App1.java เป็นคลาสที่จำลองว่าเป็น Application ตัวที่หนึ่ง
  3. test/App2.java เป็นคลาสที่จำลองว่าเป็น Application ตัวที่สอง
  4. test/StaticField.java เป็นคลาสที่จำลองว่าเป็น Library ที่ทั้งสอง Application เรียกใช้งาน
ก่อนอื่นต้องขออธิบายโปรแกรมหลักก่อนนะครับ ไฟล์ StaticFieldTest.java เป็นดังนี้

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;

public class StaticFieldTest {
  public static void main(String[] args) throws Exception {
    System.out.println("SystemClassLoader: "+ClassLoader.getSystemClassLoader());
    ClassLoader sftCL = StaticFieldTest.class.getClassLoader();
    System.out.println("StaticFieldTest ClassLoader: "+sftCL);
    System.out.println("StaticFieldTest ParentClassLoader: "+sftCL.getParent());
    URLClassLoader shareCL = new URLClassLoader(new URL[] { new URL("file:///tmp/test/share/") });
    URLClassLoader app1CL = new URLClassLoader(new URL[] { new URL("file:///tmp/test/app1/") }, shareCL);
    URLClassLoader app2CL = new URLClassLoader(new URL[] { new URL("file:///tmp/test/app2/") }, shareCL);
    Object obj1 = app1CL.loadClass("test.App1").newInstance(); // Load app1
    Runnable app1 = (Runnable)obj1;
    app1.run(); // Run app1
    Object obj2 = app2CL.loadClass("test.App2").newInstance(); // Load app2
    Runnable app2 = (Runnable)obj2;
    app2.run(); // Run app2
  }
}

โปรแกรมนี้จำลองการทำงานว่าตัวเองเป็น Application Container กล่าวคือ โปรแกรมจะโหลด Application App1 และ App2 เข้ามารันผ่านทาง URLClassLoader ในที่นี้ผมสร้างเตรียมไว้ 3 ตัวคือ shareCL เป็นตัวโหลดคลาสต่างๆที่อยู่ในโฟลเดอร์ /tmp/test/share ส่วน app1CL และ app2CL จะโหลดคลาสต่างๆที่อยู่ในโฟลเดอร์ /tmp/test/app1 และ /tmp/test/app2 ตามลำดับ โดยที่ app1CL และ app2CL จะถูกสร้างให้มี parent คือ shareCL หากใครจะทดลองตามก็ขอให้สร้างโฟลเดอร์นี้ไว้ หรือแก้ไข source code ไปที่โฟลเดอร์ที่ต้องการก่อนนะครับ

หากใครไม่ได้ใช้ Eclipse ผมก็ขอแนะนำ command-line สำหรับ compile โปรแกรมง่ายๆคือ
cd StaticFieldTest
javac -d bin src/*.java src/test/*.java
จะได้ .class ไฟล์ที่โฟลเดอร์ bin ซึ่งประกอบไปด้วย 4 ไฟล์คือ
  1. StaticFieldTest.class
  2. test/App1.class
  3. test/App2.class
  4. test/StaticField.class

การทดลองที่ 1: Stand-alone Application

เมื่อ compile เสร็จแล้ว เราจะรันโปรแกรมโดยมีทุกๆคลาสอยู่ใน CLASSPATH (มองจาก working dir) นั่นคือ หากรันโปรแกรมด้วยคำสั่ง
cd bin
java StaticFieldTest
จะได้ผลลัพธ์ดังนี้

SystemClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
StaticFieldTest ClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
StaticFieldTest ParentClassLoader: sun.misc.Launcher$ExtClassLoader@42aab87f
Running App1
App1 ClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
App1 ParentClassLoader: sun.misc.Launcher$ExtClassLoader@42aab87f
Static Initialize StaticField
StaticField ClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
StaticField ParentClassLoader: sun.misc.Launcher$ExtClassLoader@42aab87f
StaticField.getSvar(): 0
End of Initialize StaticField
StaticField.getSvar(): 0
StaticField.setSvar(10): 10
StaticField.increaseSvar(): 11
End of App1
Running App2
App2 ClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
App2 ParentClassLoader: sun.misc.Launcher$ExtClassLoader@42aab87f
StaticField.getSvar(): 11
StaticField.setSvar(10): 10
StaticField.increaseSvar(): 11
End of App2

กรณีนี้จะเหมือน Stand-alone Application นั่นคือ Bootstrap ClassLoader จะเป็นตัวโหลดคลาสต่างๆเข้ามา เนื่องจากมีคลาสต่างๆอยู่ใน CLASSPATH  อยู่แล้ว shareCL, app1CL และ app2CL ส่งการค้นหามาให้ Parent โหลดก่อน ClassLoader ของ App1, StaticField และ App2 จึงเป็นตัวเดียวกัน โดยผลลัพธ์แสดงให้เห็นว่า App1 และ App2 ใชั StaticField ร่วมกันโดยสังเกตได้จากค่า StaticField เริ่มต้น initialize เพียงครั้งเดียวจากค่า 0 และ App1 เซ็ตค่าให้เป็น 10 จากนั้นก็เพิ่มเป็น 11 ส่วน App2 ก็เริ่มที่ค่า 11 จากผลของ App1 และเซ็ตค่าเป็น 10 แล้วก็เพิ่มค่าเป็น 11

การทดลองที่ 2: ย้าย App1 และ App2 และ Share ไปไว้คนละที่

ทำการย้ายไฟล์ test/App1.class ไปไว้ที่ /tmp/test/app1/test/App1.class ย้าย test/App2.class ไปไว้ที่ /tmp/test/app2/test/App2.class และ test/StaticField.class ไปไว้ที่ /tmp/test/share/test/StaticField.class สังเกตว่า เราจะต้องย้ายไปทั้ง package นะครับ นั่นคือแต่ละคลาสอยู่ใน package test ก็เลยต้องมีโฟลเดอร์ย่อยชื่อ test ติดไปด้วย ไม่อย่างนั้นจะเกิด ClassNotFoundException ครับ เมื่อรันเหมือนเดิม คือ java StaticFieldTest ที่โฟลเดอร์ bin เหมือนเดิม จะได้ผลลัพธ์ดังนี้

SystemClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
StaticFieldTest ClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
StaticFieldTest ParentClassLoader: sun.misc.Launcher$ExtClassLoader@42aab87f
Running App1
App1 ClassLoader: java.net.URLClassLoader@27021e58
App1 ParentClassLoader: java.net.URLClassLoader@7c163769
Static Initialize StaticField
StaticField ClassLoader: java.net.URLClassLoader@7c163769
StaticField ParentClassLoader: sun.misc.Launcher$AppClassLoader@1978b0f9
StaticField.getSvar(): 0
End of Initialize StaticField
StaticField.getSvar(): 0
StaticField.setSvar(10): 10
StaticField.increaseSvar(): 11
End of App1
Running App2
App2 ClassLoader: java.net.URLClassLoader@5f0a94c5
App2 ParentClassLoader: java.net.URLClassLoader@7c163769
StaticField.getSvar(): 11
StaticField.setSvar(10): 10
StaticField.increaseSvar(): 11
End of App2

จะเห็นว่า App1, App2 และ StaticField ถูกโหลดมาด้วย ClassLoader คนละตัวกัน แต่เนื่องจากผมสร้างให้ StaticField โหลดมาจาก shareCL ที่ใช้ร่วมกันระหว่าง App1 และ App2 จึงทำให้ StaticField มีการ initialize เพียงครั้งเดียวเช่นเดิม และผลลัพธ์ค่าของ StaticField ยังคงเหมือนเดิม คือ App1 กำหนดค่าให้เป็น 11 แล้วพอมาที่ App2 ก็เป็นค่า 11

การทดลองที่ 3: App1 และ App2 ใช้ StaticField คนละตัวกัน

ทำการก็อปปี้ /tmp/test/share/test/StaticField.class ไปไว้ยัง /tmp/test/app1/test/StaticField.class และ /tmp/test/app2/test/StaticField.class  แยกเป็นสองไฟล์ในแต่ละโปรแกรม จากนั้นลบไฟล์ /tmp/test/share/test/StaticField.class ทิ้งไป เพื่อไม่ให้ shareCL สามารถโหลดคลาสมาได้ เมื่อรันเหมือนเดิม คือ java StaticFieldtest ที่โฟลเดอร์ bin เหมือนเดิม จะได้ผลลัพธ์ดังนี้

SystemClassLoader: sun.misc.Launcher$AppClassLoader@6521f956
StaticFieldTest ClassLoader: sun.misc.Launcher$AppClassLoader@6521f956
StaticFieldTest ParentClassLoader: sun.misc.Launcher$ExtClassLoader@1978b0f9
Running App1
App1 ClassLoader: java.net.URLClassLoader@6d15a113
App1 ParentClassLoader: java.net.URLClassLoader@27021e58
Static Initialize StaticField
StaticField ClassLoader: java.net.URLClassLoader@6d15a113
StaticField ParentClassLoader: java.net.URLClassLoader@27021e58
StaticField.getSvar(): 0
End of Initialize StaticField
StaticField.getSvar(): 0
StaticField.setSvar(10): 10
StaticField.increaseSvar(): 11
End of App1
Running App2
App2 ClassLoader: java.net.URLClassLoader@fe9e47
App2 ParentClassLoader: java.net.URLClassLoader@27021e58
Static Initialize StaticField
StaticField ClassLoader: java.net.URLClassLoader@fe9e47
StaticField ParentClassLoader: java.net.URLClassLoader@27021e58
StaticField.getSvar(): 0
End of Initialize StaticField
StaticField.getSvar(): 0
StaticField.setSvar(10): 10
StaticField.increaseSvar(): 11
End of App2

เราจะพบว่า StaticField ที่เรียกใช้โดย App1 กับ App2 กลายเป็นคนละตัวกัน และแยกจากกันโดยสิ้นเชิง ทำให้การ initialize static field และการกำหนดค่า static field ของแต่ละโปรแกรม แยกจากกันโดยเด็ดขาดได้

สรุป

หากต้องการจะหลีกเลี่ยงปัญหาที่ static field มีการใช้งานร่วมกันระหว่างโปรแกรมหลายๆโปรแกรมที่ถูกโหลดมาจาก ClassLoader แตกต่างกันแล้วไม่สามารถควบคุมให้แยกจากกันได้ วิธีการแก้ก็คือ จะต้องวางคลาสไฟล์ หรือ JAR ไฟล์ของ API ตัวนั้นไว้ที่เดียวกับโปรแกรมนั้นๆ เพื่อให้ ClassLoader ของโปรแกรมนั้นมองเห็นโดยตรง จะต้องไม่ไปวางไว้ในโฟลเดอร์ที่แชร์ร่วมกัน

References

วันอังคารที่ 8 ตุลาคม พ.ศ. 2556

Professional jQuery

Title:Professional JQuery
Authors:Cesar Otero & Rob Larsen
Formats:PDF
Ids:GoogleDouban Books,9781118026687
Tags:JavaScript, jQuery, Programming Language, Web
Publishers:John Wiley & Sons
Author Sort:Otero, Cesar & Larsen, Rob
Title Sort:Professional JQuery
Date:08 ต.ค. 2013
uuid:0d8f471b-3b3a-428d-bec3-fe77f0382add
id:5953
Published:พ.ค. 2012
Modified:08 ต.ค. 2013
Size:22.69MB
Languages:English
Type:book

Over the past several years JavaScript has undergone a remarkable transformation. It is now one of the most important programming languages in the world. With the ongoing importance of Ajax-based development and the rise of full-featured JavaScript libraries, most of the stigma around JavaScript has vanished. The most beginner-friendly library, jQuery, is responsible for most of this turnaround. jQuery is used at some of the largest organizations in the world, including Amazon, IBM, Twitter, NBC, Best Buy, and Dell. In 2011 there were three major releases in jQuery and the community surrounding it continues to grow. jQuery is prominently featured at the front end of Java/Spring, PHP, .NET, Ruby on Rails, and Python/Django stacks all over the Web. If you have experience with HTML, CSS, and JavaScript, this book is for you. It will expand your jQuery knowledge by focusing on the core library with the benefit of strong core JavaScript expertise in many of the lessons.This book is aimed at three groups of readers: Experienced server-side web application developers looking to move into the client-side using the world's most popular front-end libraryExperienced JavaScript programmers looking to ramp up quickly on jQueryNovice to intermediate jQuery developers looking to expand their jQuery knowledge into more advanced topicsThis book is not aimed at beginners. For those looking to start with the basics of HTML, CSS, and JavaScript/jQuery development, " Beginning JavaScript and CSS Development with jQuery (Wrox Programmer to Programmer)" by Richard York will most likely help you more.This book covers a lot of information about jQuery including a developer-level introduction as well as providing an in-depth look into some of the more advanced features. The book is divided into two parts, jQuery Fundamentals and Applied jQuery. jQuery Fundamentals introduces the core concepts while Applied jQuery focuses on more advanced subjects. The first part of the book offers an in-depth introduction to jQuery fundamentals, which includes selecting elements, manipulating the DOM, and binding and reacting to browser books. After providing a solid foundation, the book will then outline more advanced topics such as plugin development, unit testing with JavaScript, and other advanced features of the library. The book also focuses on features available as of jQuery 1.7.1. but also tries to incorporate feature support in older versions of the library wherever it is relevant.The first few chapters will help you set up a development environment and they also review important JavaScript concepts. Chapters 3-7 examine the jQuery core concepts. The second half of the book focuses on applying jQuery in the real world as well as detailing jQuery UI, plugin development, and templates among other lessons.Part 1, jQuery Fundamentals, contains chapters 1-7. Chapter 1 sets up an environment for developing and debugging jQuery and JavaScript code. It also defines the code standards that will be used throughout the book. Chapter 2 goes through the basics of JavaScript programming language to strengthen the foundation that the rest of the book is built upon. Chapter 3 introduces the basic functions that make up the library and illuminates usages of the core jQuery functions. It also introduces many of the functions that you will need to perform varieties of tasks. Chapter 4 goes in-depth into one of the core features of jQuery, which is the ability to select and manipulate HTML elements. Chapter 5 introduces another feature of jQuery: the cross-browser ability to bind and manage browser events. Chapter 6 explores one of the biggest revolutions in web development in recent years--Ajax. Chapter 7 focuses on some of the shortcuts jQuery offers for animating components in your web applications. These include moving, fading, toggling, and resizing elements.Part 2, Applied jQuery, contains chapters 8-14. Chapter 8 introduces jQuery UI, which is an associated user interface library for jQuery and contains things such as widgets, effects, animations, and interactions. Chapter 9 explores additional jQuery UI features including moving, sorting, resizing, and selection elements with a mouse. Chapter 10 teaches a variety of techniques, best practices, and patterns that you can apply to your code to immediately make it more efficient, maintainable, and clear. Chapter 11 focuses on the jQuery Template plugin. Templates are a standard way of marrying data and markup snippets. Chapter 12 focuses on authoring jQuery plugins. It is important to know how to extend the power of jQuery with custom methods as it is a fundamental skill for a top jQuery developer. Chapter 13 introduces the jQuery Deferred Object, which was introduced in version 1.5. It is a chainable utility object that provides control over the way callback functions are handled. Chapter 14 introduces the general concept of unit testing and goes into detail with the specific unit testing framework created by and used by the jQuery project itself, QUnit.In order to use this book effectively, you will need one of the following web browsers to run the samples provided within the book: Firefox 3.6, Current -1 VersionInternet Explorer 6+Safari 5.0xOpera current - 1 VersionChrome Current - 1 VersionCesar Otero is a freelance web developer. His technical interests include Python, Django, JavaScript, and jQuery. He sometimes contributes articles to IBM's developer works. He holds a degree in electrical engineering.Rob Larsen has many years' experience as a front-end engineer and team leader. He has built websites and applications for some of the world's biggest brands. He is currently a Senior Specialist, Platform at Sapient Global markets. Rob is an active writer and speaker on web technology with a special focus on emerging standards like HTML5, CSS3, and JavaScript.

jQuery Ui 1.8

Title:jQuery Ui 1.8
Authors:Wellman, Dan
Formats:PDF
Ids:GoogleDouban Books,9781849516525
Tags:JavaScript, jQuery, Programming Language, UI, Web Programming
Publishers:Packt Publishing, Limited
Author Sort:Dan, Wellman,
Title Sort:jQuery Ui 1.8
Date:08 ต.ค. 2013
uuid:4ba7f0a4-6a55-4ba5-82aa-80865000c5d1
id:5952
Published:ส.ค. 2011
Modified:08 ต.ค. 2013
Size:7.11MB
Languages:English
Type:book

jQuery UI, the official suite of plugins for the jQuery JavaScript library, gives you a solid platform on which to build rich and engaging interfaces with maximum compatibility, stability, and a minimum of time and effort.jQuery UI has a series of ready-made user interface widgets and a comprehensive set of core interaction helpers to reduce the amount of code that you need to write to take a project from conception to completion.jQuery UI 1.8: The User Interface Library for jQuery has been specially revised for version 1.8 of jQuery UI. It is written to maximize your experience with the library by breaking down each component and walking you through examples that progressively build upon your knowledge, taking you from beginner to advanced usage in a series of easy-to-follow steps.Throughout the book, you'll learn how each component can be initialized in a basic default implementation and then customize and configure each component to tailor it to your application. You'll look at the configuration options and the methods exposed by each component's API to see how these can be used to bring out the best of the library. Each chapter will also show you the custom events fired by the component covered and how these events can be intercepted and acted upon.What you will learn from this book :How the CSS framework consistently themes the widgets and how these styles can be used in your own codeCreate a custom jQuery UI widget using the widget libraryThe underlying mark-up required by different components, and how each widget or interaction helper is initializedConfigure the different components, and the different options that each component usesWork with each widget programmatically using its methods and event hooksPractical examples of how each component can be usedAdd flair to your interface with animation effectsApproachAn example-based approach leads you step-by-step through the implementation and customization of each library component and its associated resources. Reference tables of each configuration option, method, and event for each component are provided, alongside detailed explanations of how each widget is used.Who this book is written forThis book is for front-end designers and developers who need to quickly learn how to use jQuery UI. To get the most out of this book you should have a good working knowledge of HTML, CSS, and JavaScript, and should ideally be comfortable using jQuery.

jQuery Pocket Reference

Title:jQuery Pocket Reference
Authors:David Flanagan
Formats:PDF
Ids:GoogleDouban Books,9781449397227
Tags:HTML, JavaScript, jQuery, Programming Languages, Reference, Web, Web Programming
Publishers:O'Reilly
Author Sort:Flanagan, David
Title Sort:jQuery Pocket Reference
Date:08 ต.ค. 2013
uuid:e6a094f5-2e20-412d-a9cb-0d176e71c239
id:5949
Published:ธ.ค. 2010
Modified:08 ต.ค. 2013
Size:0.71MB
Languages:English
Type:book

"As someone who uses jQuery on a regular basis, it was surprising to discover how much of the library I’m not using. This book is indispensable for anyone who is serious about using jQuery for non-trivial applications." -- Raffaele Cecco, longtime developer of video games, including Cybernoid, Exolon, and Stormlord
jQuery is the "write less, do more" JavaScript library. Its powerful features and ease of use have made it the most popular client-side JavaScript framework for the Web. This book is jQuery's trusty companion: the definitive "read less, learn more" guide to the library.
jQuery Pocket Reference explains everything you need to know about jQuery, completely and comprehensively. You'll learn how to:
* Select and manipulate document elements
* Alter document structure
* Handle and trigger events
* Create visual effects and animations
* Script HTTP with Ajax utilities
* Use jQuery's selectors and selection methods, utilities, plugins and more
The 25-page quick reference summarizes the library, listing all jQuery methods and functions, with signatures and descriptions.

jQuery in Action

Title:jQuery in Action
Authors:Bear Bibeault & Yehuda Katz
Formats:PDF
Ids:GoogleDouban Books,9781933988351
Tags:HTML, JavaScript, jQuery, Programming Languages, Web
Publishers:Manning Publications
Rating:★★★★
Author Sort:Bibeault, Bear & Katz, Yehuda
Title Sort:jQuery in Action
Date:08 ต.ค. 2013
uuid:aa51abf2-05cd-40d9-bafa-b0efa2693463
id:5948
Published:ก.พ. 2008
Modified:08 ต.ค. 2013
Size:23.17MB
Languages:English
Type:book

A good web development framework anticipates what you need to do and makes those tasks easier and more efficient; jQuery practically reads your mind. Developers of every stripe-hobbyists and professionals alike-fall in love with jQuery the minute they've reduced 20 lines of clunky JavaScript into three lines of elegant, readable code. This new, concise JavaScript library radically simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. "jQuery in Action," like jQuery itself, is a concise tool designed to make you a more efficient and effective web developer. In a short 300 pages, this book introduces you to the jQuery programming model and guides you through the major features and techniques you'll need to be productive immediately. The book anchors each new concept in the tasks you'll tackle in day-to-day web development and offers unique lab pages where you immediately put your jQuery knowledge to work. There are dozens of JavaScript libraries available now, with major companies like Google, Yahoo and AOL open-sourcing their in-house tools. This book shows you how jQuery stacks up against other libraries and helps you navigate interaction with other tools and frameworks. "jQuery in Action" offers a rich investigation of the up-and-coming jQuery library for client-side JavaScript. This book covers all major features and capabilities in a manner focused on getting the reader up and running with jQuery from the very first sections. Web Developers reading this book will gain a deep understanding of how to use jQuery to simplify their pages and lives, as well as learn the philosophy behind writingjQuery-enhanced pages.

WebKit คืออะไร

WebKit คำนี้ หลายคนที่เคยเขียนเว็ป น่าจะเคยผ่านตากันมาบ้างแล้ว เพราะมันเป็นส่วนหนึ่งที่สำคัญมากๆของเว็ปบราวเซอร์หลายๆตัว เช่น Safari และ Chrome รวมทั้งยังมีการนำไปใช้ในโปรแกรมอื่นๆอีกมากมาย สงสัยไหมล่ะครับว่ามันคืออะไร

WebKit คือ

WebKit (webkit.org) เป็นโครงการ open source ที่สร้าง render engine สำหรับ web browser หรืออาจจะเรียกได้ว่ามันคือ render engine ของผลิตภัณฑ์หลายๆตัวใน Mac OS X System Framework  เช่น Safari Dashboard Mail และอื่นๆอีกมากมาย ซึ่งต้องขอย้ำว่า WebKit ไม่ใช่ browser แต่เป็น render engine สำหรับ browser โดยมีการนำ WebKit ไปสร้าง browser มากมาย ดังรายชื่อที่มีคนรวบรวมไว้ที่ Wiki ไม่ต่ำกว่า 30 ตัว

ความสามารถของ WebKit

  1. CSS (CSS1/CSS2.1/CSS3)
  2. DOM (HTML DOM, XML DOM)
  3. HTML/XHTML
  4. HTML Editing
  5. HTML Forms
  6. JavaScript (ECMAScript)
  7. MathML
  8. Plug-ins
  9. Printing
  10. SVG (SVG 1.1)
  11. Accessibility
  12. XML
  13. XSLT

Port

หลักๆแล้ว WebKit สร้างขึ้นสำหรับระบบปฏิบัติการ Mac OS X ซึ่งแกนหลักเป็น Linux ดังนั้นเวลาจะ build โปรแกรมให้ใช้งานบน Windows ได้ จะต้องมีโปรแกรมจำลองสภาพแวดล้อมให้เป็น Linux และที่เขาแนะนำไว้ก็คือโปรแกรม cygwin ส่วนระบบอื่นๆก็ยังมีอีก เช่น Qt GTK+ EFL และอื่นๆอีกมากมายเป็นต้น ซึ่งล้วนเป็น Linux เพียงแต่เปลี่ยน UI เท่านั้น โดยก่อนหน้านี้ WebKit เขียนด้วย Objective-C และกำลังเปลี่ยนผ่านมาสู่ WebCore ซึ่งเป็น C++ ทำให้สามารถ port ไปยังระบบอื่นๆได้ง่ายขึ้น

สรุป

ใครสนใจสร้างโปรแกรมที่ต้องการความสามารถบางส่วนเป็น browser ก็ลองศึกษาเพิ่มเติมดูตามลิ้งค์ที่ให้ไว้ได้เลยนะครับ

วันพุธที่ 25 กันยายน พ.ศ. 2556

HTML5 & CSS3 in the Real World

Title:HTML5 & CSS3 in the Real World
Authors:Estelle Weyl & Louis Lazaris &Alexis Goldstein
Formats:PDF
Ids:GoogleDouban Books,9780980846904
Tags:CSS3, Design, HTML5, Programming Languages, Web, Web Development, Web Page Design
Publishers:SitePoint
Author Sort:Weyl, Estelle & Lazaris, Louis & Goldstein, Alexis
Title Sort:HTML5 & CSS3 in the Real World
Date:25 ก.ย. 2013
uuid:d388ecbd-8c63-4083-9dc9-6753ba0690ce
id:6095
Published:มิ.ย. 2011
Modified:25 ก.ย. 2013
Size:4.26MB
Languages:English
Type:book

HTML5 and CSS3 for the Real World is ideal for any web designer or developer who wants to start using the latest generation of web technologies in their everyday work. This book will teach you how to: * understand the new semantic markup available in HTML5 * use CSS3 to make sites beautiful without sacrificing clean markup or resorting to complex workarounds * employ native HTML5 video and audio in your pages * use web fonts to enrich a site's typography * make web applications accessible using WAI-ARIA * make the most of the new HTML form elements. This easy-to-follow guide is illustrated with lots of examples, and leads readers through the process of creating great websites from start to finish using HTML5 and CSS3. It also features: * details on all the new HTML5 and CSS3 elements and features * information on the current level of browser support * advice for creating great experiences for all users thanks to progressive enhancement.

HTML5 and CSS3

Title:HTML5 and CSS3
Authors:Hogan, Brian P.
Formats:PDF
Ids:GoogleDouban Books,9781934356685
Tags:CSS3, Design, HTML5, Programming Languages, Standard, Web, Web Application, Web Development, Web Page Design
Publishers:The Pragmatic Bookshelf
Rating:★★★
Author Sort:P., Hogan, Brian
Title Sort:HTML5 and CSS3
Date:25 ก.ย. 2013
uuid:2c6dc834-509f-44a1-aa8b-008cbe0948d0
id:6093
Published:ก.ย. 2010
Modified:25 ก.ย. 2013
Size:1.68MB
Languages:English
Type:book

HTML5 and CSS3 are the future of web development, but you don't have to wait to start using them. Even though the specification is still in development, many modern browsers and mobile devices already support HTML5 and CSS3. This book gets you up to speed on the new HTML5 elements and CSS3 features you can use right now, and backwards compatible solutions ensure that you don't leave users of older browsers behind.
This book gets you started working with many useful new features of HTML5 and CSS3 right away. Gone are the days of adding additional markup just to style a button differently or stripe tables. You'll learn to use HTML5's new markup to create better structure for your content and better interfaces for your forms, resulting in cleaner, easier-to-read code that can be understood by both humans and programs.
You'll find out how to embed audio, video, and vector graphics into your pages without using Flash. You'll see how web sockets, client-side storage, offline caching, and cross-document messaging can ease the pain of modern web development. And you'll discover how simple CSS3 makes it to style sections of your page. Throughout the book, you'll learn how to compensate for situations where your users can't take advantage of HTML5 and CSS3 yet, developing solutions that are backwards compatible and accessible.
You'll find what you need quickly with this book's modular structure, and get hands-on with a tutorial project for each new HTML5 and CSS3 feature covered. "Falling Back" sections show you how to create solutions for older browsers, and "The Future" sections at the end of each chapter get you excited about the possibilities when HTML5 and CSS3 reach widespread adoption. Get ready for the future---in fact, it's here already.

Dreamweaver Cs5.5 Mobile and Web Development With HTML5, CSS3, and jQuery

Title:Dreamweaver CS5.5 Mobile and Web Development With HTML5, CSS3, and jQuery
Authors:David Karlins
Formats:PDF
Ids:GoogleDouban Books,9781849691581
Tags:CSS3, Design, HTML5, jQuery, Mobile, Web, Web Application, Web Development, Web Page Design, Web Programming
Publishers:Packt Publishing, Limited
Author Sort:Karlins, David
Title Sort:Dreamweaver CS5.5 Mobile and Web Development With HTML5, CSS3, and jQuery
Date:25 ก.ย. 2013
uuid:1b58858a-231a-4709-9519-fa65c3c1e8da
id:6091
Published:ก.ย. 2011
Modified:25 ก.ย. 2013
Size:11.42MB
Languages:English
Type:book

Dreamweaver is the most powerful and industry-leading web design software that utilizes cutting edge web technologies such as HTML5, CSS3, and jQuery for web and mobile development. These technologies have radically reconfigured the process of designing Web content and function in the widest possible range of browsing environments ranging from desktops to mobile devices.For experienced Dreamweaver designers and for designers new to Dreamweaver, this book explains in detail how to take advantage of the new features available in the latest releases of Dreamweaver that add support for HTML5, CSS3, and jQuery. In addition to this, the book also contains detailed step-by-step directions for building mobile apps in Dreamweaver CS5.5.This book starts off by teaching you to create web pages in Dreamweaver using the latest technology and approaches — HTML5, CSS3, and JavaScript. It demonstrates how to create or customize pages with HTML5 layouts and add multimedia to these pages with HTML5 elements. Then you will learn to add various CSS3 effects to web pages. The book also covers different techniques of adding interactivity to web pages. The later chapters show how to optimize web pages with Dreamweaver for display in various browsing environments. You will also learn to build jQuery-based mobile apps from scratch in the later chapters. By the time you're finished, you'll have learned several techniques to use the latest features of Dreamweaver for web and mobile development.