Tips and Notes on Java

ÀÚ¹Ù ¾ð¾î¿¡ ´ëÇÑ ÆÁÀ» »ý°¢³ª´Â ´ë·Î Àû°í ÀÖ½À´Ï´Ù.
1. Jar URL À¯°¨
ÀÚ¹Ù´Â Jar¸¦ URL·Î »ç¿ëÇÒ ¼ö ÀÖ°Ô ÇÏ°í ÀÖÀ¸¸ç ÀÌ¿¡ ´ëÇÑ ¿¬°áÀ» JarURLConnectionÀ» ÅëÇØ ±¸ÇöÇÏ°í ÀÖ½À´Ï´Ù.
±×·±µ¥ ¹®Á¦´Â JarURLConnectionÀÌ ¼öÇà ¼º´ÉÀÇ °³¼±À» À§ÇØ URL·ÎºÎÅÍ ´Ù¿î ·ÎµåÇÑ Jar ÆÄÀÏÀ» ¹«ÇÑÁ¤ ij½ÃÇÏ°í ÀÖ´Ù´Â Á¡ÀÔ´Ï´Ù.

µû¶ó¼­ class loader µîÀÇ ±¸Çö¿¡¼­ jar URL ¹æ½ÄÀº classloader¸¦ °¡ºñÁöÄ÷ºÆ®ÇÏ´õ¶óµµ url connection ±¸Çö ÂÊ¿¡¼­ jar ÆÄÀÏÀ» °è¼ÓÇؼ­ ij½ÃÇÏ°Ô µÇ¹Ç·Î, À©µµ¿ìÀÇ °æ¿ì ÆÄÀÏ¿¡ ´ëÇÑ Àá±Ýµµ ¹ß»ýÇÏ°í, jar ÆÄÀÏÀ» º¯°æÇÏ´õ¶óµµ ¹Ý¿µÀÌ µÇÁö ¾Ê°Ô µË´Ï´Ù.

jar ÆÄÀÏÀÇ °æ¿ì¿¡´Â URLClassLoader ¿ª½Ã »ç¿ëÇÏÁö ¾Ê´Â °ÍÀÌ ÁÁ°Ú±¸¿ä.
Jar¸¦ »ç¿ëÇÑ ClassLoader¸¦ ±¸ÇöÇÒ ¶§¿¡´Â URLÀ» »ç¿ëÇÏÁö ¾Ê°í Á÷Á¢ JarFile (ȤÀº ZipFile)À» »ç¿ëÇÏ¿© ±¸ÇöÇÏ´Â °ÍÀÌ ¹æ¹ýÀÌ µÇ°Ú½À´Ï´Ù.

¾Æ·¡ ¸µÅ©´Â Jar URLÀÇ ÀÌ Æ¯¼º ¶§¹®¿¡ jar ÆÄÀÏÀÌ »èÁ¦°¡ ¾È µÇ´Â °ÍÀ» ÇÇÇØ°¡±â À§ÇØ jar ÆÄÀÏÀ» ³ªÁß¿¡ ÀÚµ¿À¸·Î Áö¿öÁöµµ·Ï ÇÏ´Â delete on close ¸ðµå¸¦ JarFileÀ̳ª ZipFile¿¡ Ãß°¡ÇÏ¿´´Ù´Â ³»¿ëÀÔ´Ï´Ù.(¸ðµå ÀÎÀÚ·Î OPEN_READ | OPEN_DELETE ¸¦ ÇÔ²² ÁÖ¸é ÀÌ·¸°Ô µË´Ï´Ù.)
±×·¸°Ô ÇÏ´õ¶óµµ ³ªÁß¿¡... Á¤È®ÇÑ ½ÃÁ¡Àº ¾Ë ¼ö ¾øÀÌ, ¿­¾ú´ø Jar ÆÄÀÏÀ» ´ÝÀ¸¸é¼­ »èÁ¦´Â ÇØÁÖ°ÚÁö¸¸, ClassLoader¿Í °°Àº °æ¿ì¿¡´Â µµ¿òÀÌ µÇ´Â ³»¿ëÀº ¾Æ´Õ´Ï´Ù.

2. Joshua BlochÀÇ Collection
java.util ÆÐÅ°ÁöÀÇ Ä÷º¼ÇÀ» ÁÖµµÀûÀ¸·Î Á¤ÀÇÇÑ »ç¶÷ÀÌ Joshua Bloch¶ó´Â °ÍÀº Àß ¾Ë·ÁÁø »ç½ÇÀÔ´Ï´Ù.
Collection ¶óÀ̺귯¸®µéÀÌ ¾ó¸¶³ª Àß Á¤ÀǵǾî ÀÖ´ÂÁö °¡²û ±× ³»Àû ¿Ï°á¼º¿¡ ³î¶ó°Ô µË´Ï´Ù. ±×¸¸Å­ ÁÖµµ¸é¹ÐÇÑ ¼º°ÝÀ» °¡Áø »ç¶÷ÀÌ ¾Æ´Ï¾úÀ»±î ½Í½À´Ï´Ù.
Collections³ª Arrays¿Í °°Àº À¯Æ¿¸®Æ¼¸¦ ¸ð¾ÆµÐ Ŭ·¡½º¸¦ º¸¸é, Ä÷º¼ÇÀÇ ÇÙ½ÉÀ¸·Î Àß Á¤ÀǵÇÁö ¸øÇÏÁö¸¸, ³Ê¹« ÀÚÁÖ »ç¿ëµÉ ±â´ÉµéÀ» ÀÌ·¸°Ô Ä¡¹ÐÇÏ°Ô helper class¿¡ ¸ð¾ÆµÐ °ÍÀ» º¸¸é ¿ª½Ã ÆÐÅ°Áö¿Í Ŭ·¡½ºµé¸¸À¸·Îµµ ³»Àû ¿¬°ü¼ºÀ» µÎ¾î ÇϳªÀÇ ¿Ï°áµÈ ¿ìÁÖ¸¦ ±¸¼ºÇÏ·Á ÇÏ´Â °Ô ¾Æ´Ñ°¡ ÇÏ´Â »ý°¢ÀÌ µé Á¤µµÀÔ´Ï´Ù.
°¡²û Collection¿¡ BagÀÌ Æ÷ÇԵǾúÀ¸¸é ÇÏ´Â ¶§°¡ ÀÖ±ä ÇÏÁö¸¸... ^^;

3. HttpURLConnection Tip
http: URLÀ» openConnection()ÇÏ¸é ¾òÀ» ¼ö ÀÖ´Â java.net.HttpURLConnectionÀº »ó´çÈ÷ °­·ÂÇÑ ±â´ÉÀ» °¡Áö°í ÀÖ´Â HTTP Ŭ¶óÀ̾ðÆ®ÀÔ´Ï´Ù.

  connection.setDoOutput(true);
  connection.setRequestMethod("POST");
  OutputStream out = connection.getOutputStream();
ÀÌ¿Í °°ÀÌ Çϸé HTTP URL connectionÀ» »ç¿ëÇÏ¿© POST È£ÃâÀ» ÇÒ ¼ö ÀÖ½À´Ï´Ù.
HttpURLConnectionÀ» »ç¿ëÇÏ¿© ÀÌ¿Í °°Àº È£ÃâÀ» ÇÑ ÈÄ °á°ú¸¦ ¹ÞÀ» ¶§, Á¤»óÀûÀÎ °æ¿ì¿¡´Â getInputStream()À» ÅëÇÏ¿© ¹Þ°Ô µË´Ï´Ù.
ÇÏÁö¸¸, response code°¡ HTTP_OK°¡ ¾Æ´Ñ °æ¿ì¿¡´Â getErrorStream()À» ÅëÇÏ¿© ½ÇÁ¦ ÀÀ´ä µ¥ÀÌÅ͸¦ ¹Þ°Ô µÇ¾î ÀÖÀ¸¹Ç·Î ÁÖÀÇÇØ¾ß ÇÕ´Ï´Ù.
µû¶ó¼­, Á»´õ ÀϹÝÀûÀÎ ¹æ¹ýÀ» HttpURLConnectionÀÇ ÀÀ´äÀ» ó¸®ÇÏ·Á¸é ´ÙÀ½°ú °°Àº ¼ø¼­·Î ±¸ÇöÇÏ¸é µË´Ï´Ù.
  1. ¸ÕÀú getResponseCode()¸¦ °Ë»çÇÕ´Ï´Ù.
  2. response code°¡ HTTP_OK (200)ÀÎ °æ¿ì¿¡´Â getInputStream()À» ¿­¾î ¸Þ½ÃÁö¸¦ ¹ÞÀ¸¸é µË´Ï´Ù.
  3. ±×·¸Áö ¾ÊÀº °æ¿ì, (¿¹¸¦ µé¸é SOAP Fault°¡ »ç¿ëÇÏ´Â INTERNAL_ERROR (500)ÀÎ °æ¿ì) getResponseMessage()¸¦ ÇÏ¿© ÀÀ´ä Äڵ忡 µû¶ó ¿À´Â ¸Þ½ÃÁö¸¦ °¡Á®¿À°í, getErrorStream()À» ¿­¾î ½ÇÁ¦ ÀÀ´ä ¸Þ½ÃÁö¸¦ ¹ÞÀ» ¼ö ÀÖ½À´Ï´Ù.
HttpURLConnectionÀ» »ç¿ëÇÒ ¶§ TimeoutÀ» ÁöÁ¤ÇÏ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°½À´Ï´Ù. ÀÌ°ÍÀº JDK 1.4.x À̻󿡼­¸¸ Áö¿øÇÏ´Â ºñÇ¥ÁØ ±â´ÉÀÔ´Ï´Ù.
openConnection()À» Çϱâ Àü¿¡ ´ÙÀ½°ú °°ÀÌ ½Ã½ºÅÛ ¼Ó¼ºÀ» ÁöÁ¤ÇÕ´Ï´Ù.
  System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
  System.setProperty("sun.net.client.defaultReadTimeout", "30000");
ÀÌ·¸°Ô Çϸé connectÇÏ´Â µ¥ °É¸®´Â timeout°ú ½ÇÁ¦ fetchÇÏ´Â µ¥ °É¸®´Â timeoutÀ» ÁöÁ¤ÇÒ ¼ö ÀÖ½À´Ï´Ù. °ªÀÇ ´ÜÀ§´Â milli secondÀÔ´Ï´Ù.
timeoutÀÌ ¹ß»ýÇϸé java.net.SocketTimeoutExceptionÀÌ ´øÁ®Áö°Ô µË´Ï´Ù. À̸¦ catchÇÏ¿© ÀûÀýÇÑ Á¶Ã³¸¦ ÃëÇÏ¸é µÇ°ÚÁö¿ä.

4. Ant Javac Task and endorsed directory path
Ant´Â ÀÚ¹Ù °³¹ßÀÚ¶ó¸é ´©±¸¿¡°Ô³ª Ä£¼÷ÇÒ ºôµå ÅøÀÔ´Ï´Ù.
ÇѹøÀº javac ÄÄÆÄÀÏ·¯ÀÇ Ä¿¸Çµå ¶óÀÎÀ̶ó¸é ´ÙÀ½°ú °°Àº ³»¿ëÀ» Áà¾ß ÇÒ ÀÏÀÌ »ý°å½À´Ï´Ù.

  javac -J-Djava.endorsed.dirs=/some/where/endores/dir *.java

AntÀÇ javac task¿¡¼­´Â ÀÌ°ÍÀ» ¾î¶»°Ô ó¸®ÇÒ ¼ö ÀÖÀ»±î¿ä? javac task¿¡´Â java task¿Í ´Þ¸® sysproperty°¡ ¾ø½À´Ï´Ù.
ant.batÀ» ¼öÁ¤ÇÏ¿© ant ÀÚü¿¡¼­ ÀÌ °ªÀ» ÁöÁ¤Çϸé ant°¡ ½ÇÇà½ÃÅ°´Â javac¿¡¼­µµ ¹°·Ð ÀÌ ¼Ó¼ºÀÌ ÁöÁ¤µÇ±ä ÇÕ´Ï´Ù.
ÇÏÁö¸¸, ¿øÇÏ´Â task ÇÑ °÷¿¡¼­¸¸ ÁöÁ¤µÉ ÇÊ¿ä°¡ ÀÖ´Ù¸é ÀÌ°ÍÀº Çö¸íÇÑ ¹æ¹ýÀº ¾Æ´Õ´Ï´Ù.
°í½É ³¡¿¡ ãÀº ÇØ°áÃ¥Àº ´ÙÀ½ÀÔ´Ï´Ù.
    <javac srcdir="${src.dir}" 
	   destdir="${target.dir}"
	   includes="**/*.java"
	   deprecation="on"
	   debug="on"
	   optimize="off"
	   source="1.4"
	   compiler="extJavac">

      <compilerarg value="-J-Djava.endorsed.dirs=${endorsed.lib.dir}"/>
      <classpath>
	<path refid="compile.classpath"/>
      </classpath>
    </javac>
compiler¸¦ extJavac¸¦ ¼±ÅÃÇÏ¿© ¿ÜºÎ javac ÇÁ·Î¼¼½º°¡ ½ÇÇàµÉ ¶§ ³Ñ°ÜÁØ compiler argument¸¦ ó¸®Çϵµ·Ï ÇÏ´Â ¹æ¹ýÀÔ´Ï´Ù.
»ý°¢Çغ¸¸é javac task¿¡¼­ sysproperty¸¦ Áö¿øÇÏ°Ô ÇÏ´Â ¹æ¹ýÀÌ ´õ °£´ÜÇÏ°í ÅëÀϼº ÀÖ¾î º¸À̴±º¿ä. ½Ã°£ÀÌ ³ª¸é(¤Ð_¤Ð;;) ¼öÁ¤Çؼ­ Ant °³¹ßÀÚ¿¡°Ô ¸ÞÀÏÀ» º¸³»¾ß°Ú³×¿ä.

5. JAAS Security debugging Tip
Security °ü·Ã ÄÚµùÀ» ÇÒ ¶§ ÇöÀçÀÇ Subject³ª Permission, ȤÀº ProtectionDomain¿¡ ´ëÇÑ Á¤º¸¸¦ ½±°Ô ÃßÀûÇÒ ¼ö ¾ø¾î ´ä´äÇÑ ÀÏÀÌ ¹ß»ýÇÕ´Ï´Ù.
ÀÌ·² °æ¿ì¸¦ À§ÇØ SunÀÇ ¿£Áö´Ï¾îµéÀº java.security.debug¶ó´Â ½Ã½ºÅÛ ¼Ó¼ºÀ» µÎ¾ú½À´Ï´Ù.

  java -Djava.security.debug="access,domain,stack,failure" SomeClass

access¸¦ ÄѸé AccessControlContext¿¡ Á¢±ÙÇÒ ¶§¸¶´Ù ·Î±×¸¦ ³²±â°Ô µË´Ï´Ù. domain ¿É¼ÇÀº ProtectionDomain ³»¿ëÀ» ¸ðµÎ ´ýÇÁÇÒ °ÍÀΰ¡¸¦ °áÁ¤ÇÕ´Ï´Ù.
stack ¿É¼ÇÀº access denialÀÌ ¹ß»ýÇßÀ» ¶§ Call StackÀ» Âï´Â ¿É¼ÇÀÌ°í, failure´Â access denial »óȲ¿¡¼­ ½ÇÆÐÇÑ ProtectionDomainÀ» ´ýÇÁÇÏ´Â ¿É¼ÇÀÔ´Ï´Ù.
´Ù¸¥ ¿É¼ÇµéÀº access ¿É¼ÇÀÌ ÄÑÁ³À» ¶§¿¡¸¸ µ¿ÀÛÇϹǷΠaccess ¿É¼ÇÀ» Ä×À» ´ë¿¡¸¸ ´Ù¸¥ ¿É¼ÇµéÀÌ Àû¿ëµË´Ï´Ù.
°¢ ¿É¼Çµé¿¡ ´ëÇÑ °ø½Ä ¼³¸íÀº ´ÙÀ½°ú °°½À´Ï´Ù. ÀÌ ³»¿ëÀº sun.security.util.Debug Ŭ·¡½º¿¡ ÀÖ½À´Ï´Ù.

	all           turn on all debugging
	access        print all checkPermission results
	combiner      SubjectDomainCombiner debugging
	jar           jar verification
	logincontext  login context results
	policy        loading and granting
	provider      security provider debugging
	scl           permissions SecureClassLoader assigns

	The following can be used with access:

	stack     include stack trace
	domain    dumps all domains in context
	failure   before throwing exception, dump stack
	          and domain that didn't have permission

	Note: Separate multiple options with a comma

6. JMX remoting debugging Tip
JMX remotingÀÇ °æ¿ì JDK 1.4ºÎÅÍ Æ÷ÇÔµÈ logging API¸¦ »ç¿ëÇÏ¿© ÃßÀûÇÒ ¼ö ÀÖ´Â ·Î±×¸¦ Æ÷ÇÔÇÏ°í ÀÖ½À´Ï´Ù.
µû¶ó¼­, javax.management.remote ÀÇ ·Î±× ·¹º§À» ALL·Î Çϸé ÇØ´çÇÏ´Â ·Î±×¸¦ ¸ðµÎ º¼ ¼ö ÀÖ½À´Ï´Ù.


[Class Loader, Dynamic Proxy, XML, JDBC, JNDI, LDAP, Transaction, Security, Cryptography, SSL, Authentication, Login, Authorization, CORBA, RMI, RMI-IIOP, ... º»°ÝÀûÀÎ ÀÚ¹Ù °³¹ßÀÚ·Î °¡±â À§ÇÑ (Ã¥) ÀÚ¹Ù 2 SDK 1.4 ½ÃÀÛ ±×¸®°í ¿Ï¼º]

Yoon Kyung Koo <yoonforh at yahoo dot com>
ÀÌ ÆäÀÌÁö´Â 2004³â 7¿ù 22ÀÏ¿¡ óÀ½ ¸¸µé¾îÁ³½À´Ï´Ù.
Last modified: Sat Dec 11 23:12:48 +0900 2004