2013년 10월 21일 월요일

java - implements Object.toString() methods

0. Implements toString() methods
JAVA에서 System.out.println()으로 객체를 출력하면 해당 객체의 toString() method가 호출되어 출력이 됩니다. 해당 method를 override 하지 않았다면 객체의 이름과 Hashcode가 출력이 됩니다. 객체의 맴버 변수가 많을 때 편하게 toString()을 override 해보죠.
Apache commons project에 해당 내용이 있습니다.

1. The Commons Lang Package
http://commons.apache.org/proper/commons-lang/

The standard Java libraries fail to provide enough methods for manipulation of its core classes. Apache Commons Lang provides these extra methods.
Lang provides a host of helper utilities for the java.lang API, notably String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization and System properties. Additionally it contains basic enhancements to java.util.Date and a series of utilities dedicated to help with building methods, such as hashCode, toString and equals.

2. Class ToStringBuilder
아래는 API 문서의 내용입니다. (http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/builder/ToStringBuilder.html)
Assists in implementing Object.toString() methods.
This class enables a good and consistent toString() to be built for any class or object. This class aims to simplify the process by:
  • allowing field names
  • handling all types consistently
  • handling nulls consistently
  • outputting arrays and multi-dimensional arrays
  • enabling the detail level to be controlled for Objects and Collections
  • handling class hierarchies
 이렇게 간단하게 사용하면 됩니다. 이건 JAVA reflection을 사용한 예제 입니다. reflection 이란 쉽게 말해 runtime에 java object의 정보를 가공하거나 가져올 수 있는 기능입니다. JAVA Framework Spring의 dependency injection, eclipse의 자동 완성 기능 같은 것도 이런 reflection API를 JAVA가 제공하기 때문에 가능한 것이죠.
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}
이런 형식으로 출력되죠.
Person@7f54[name=Stephen,age=29,smoker=false]
 다만 explicitly 하게 사용하는게 성능상 좋다고 합니다.
Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionToString, uses AccessibleObject.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than testing explicitly.

2013년 10월 17일 목요일

excel - File formats that are supported in Excel

0. JAVA Excel API
 JAVA Excel API크게 두가지가 있다.
1. Exception in thread "main" java.lang.IllegalArgumentException: Sheet index (0) is out of range (0..-1)
 그런데 위 API를 사용하던 중 위 에러를 만나게 되었는데, 삽질도 이런 삽질이 없다. -_-; 
 원인은 Excel 2007 부터 다양한 포맷을 지원하기 때문인데 POI의 overview를 보면..
HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
 위와 같이 나와 있는데 결국은 OOXML 파일 포맷을 지원한다는 말이다.
 즉 파일 확장자는 xls 이지만 파일 포멧을 확인하지 않고 작업을 하면 위의 에러를 만나게 된다.

2. File formats that are supported in Excel
http://office.microsoft.com/en-us/excel-help/file-formats-that-are-supported-in-excel-HP010014103.aspx
 위 사이트를 방문해 보면 Excel도 크게 Excel formats, Text formats, Other formats, Clipboard formats 이렇게 나눠져 있는 것을 알수 있다. 작업할 excel 파일을 열고 save as 해보면 어떤 포맷으로 저장되어 있는지 확인 할 수 있다.

3. JAVA HTML Parser
저의 경우 Excel 파일을 가장한 HTML 파일이더군요. 그래서 이번에는 HTML Parser가 필요합니다. jsoup 이라는 library가 있네요.
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.
http://jsoup.org/

2013년 10월 15일 화요일

aes - java, actionscript(flex), javascript

0. AES 
Web Server/Client 간 암호화된 메시지를 전달하기 위하여 AES (Advanced Encryption Standard)를 사용하였습니다. 암호화 방법은 AES/ECB/PKCS5Padding을 기준으로 합니다.

Java, Javascript, Actionscript(FLEX) AES example download --> Click

1. JAVA
javax.crypto.*의 함수를 사용했습니다.

2. Actionscript
as3crypto라는 프로젝트 (https://code.google.com/p/as3crypto/)를 참고했습니다.
제가 첨부한 코드를 사용하기 위해서는 위 사이트에서 as3crypto.swc lib를 다운받아 프로젝트에 추가해야 합니다.

3. Javascript
crypto-js 프로젝트 (https://code.google.com/p/crypto-js/) 를 참고 했습니다.
For the key, when you pass a string, it's treated as a passphrase and used to derive an actual key and IV. Or you can pass a WordArray that represents the actual key. If you pass the actual key, you must also pass the actual IV.
주의하실 부분이 위 내용처럼 string을 key로 넣게 되면 자동 생성되기 때문에 고정된 key를 사용할 수 없게 됩니다.  그래서 아래 처럼 사용했습니다.
var key = CryptoJS.enc.Hex.parse('6b6579'); // "key"

2013년 10월 6일 일요일

ria - flex vs html5

0. RIA - Rich Internet Applilcation
 rich Internet application (RIA) is a Web application that has many of the characteristics of desktop application software, typically delivered by way of a site-specific browser, a browser plug-in, an independent sandbox, extensive use of JavaScript, or a virtual machine. 
1. FLEX vs HTML5
 아래는 구글링을 통해서 제가 나름 정리한 FLEX/HTML5의 pros and cons 입니다.

FLEXHTML5
Maturitysince 2004WIP (work in progress)
Accuracyvery accuratestill a bit cumbersome
Toolsfamiliar tools for java developers (eclipse)not as advanced as FLEX
ThreadSingle threaded
(network IO requests are asynchronous and non-blocking, and hence this issue is not as significant as it first sounds )
Multithreaded
Pluginyesno
BrowseriOS most likely will never ship Flash player in Safari
(educated guess)
Maximum reach (browser / OS / platform)
- supported by mobile browsers
- Features not present in old browsers
Designernot muchCSS / HTML are designer friendly
Skills availabilitynot muchmany
WeightHeavyweight – Flex libraries required No plugin = lightweight
Interaction with HTMLrequires JavaScriptHTML5

Maturity/Accuracy - FLEX는 2004년 부터 출시 되어 다양한 분야에서 검증된 plugin 입니다. 아무래도 상용이다 보니 깔끔한 디자인과 다양한 라이브러리들이 제공되어 디테일한 화면 처리에 HTML5 보다는 강점이 있습니다.

Tools - FLEX는 Flash builder라는 개발 툴을 제공하는데, eclipse plugin 형식으로 되어 있어서 기존 java 개발자라면 익숙하게 개발을 진행할 수 있습니다.

Thread - 이쪽은 어떤 쪽으로 영향이 있을지 잘 모르겠습니다.

Plugin/Weight - FLEX는 browser plugin 형식으로 동작합니다. 그래서 처음 접속하게 되면 flash player가 설치가 되어 실행됩니다. 따라서 브라우저의 flash player의 버전에도 영향을 받게 됩니다. 또한 최초 실행시에 swf파일을 다운로드하게 됩니다. (flash player가 swf파일을 실행하는 구조) 이런점 때문에 아무래도 HTML5 보다는 무거워 지는 원인이 됩니다.

Browser - 잡스가 flash는 좋아하지 않았죠, safari에서는 동작안합니다.

Designer/Skills/Interaction with HTML - 이런 점들은 어떤 기술을 적용해야 하나 고민해야하는 관리자들이 고려해 보아야 할 부분입니다. 일단 FLEX 디자이너나 개발자들은 상대적으로 소수이기 때문에 적재 적소에 필요한 사람을 구하기 힘듭니다. 또한 기존 시스템(기존 페이지등)과 연동하기 위해서 FLEX와 별개로 JavaScript를 추가로 개발해야 될지도 모릅니다.
얼마전까지만 해도 FLEX를 사용하기 위해서 Flash Builder를 구입해야 했지만, 지금은 Apache project로 편입되어 무료가 되었는지도 확인이 필요합니다.