วันเสาร์ที่ 25 ธันวาคม พ.ศ. 2553

ค้นหา Open Source ซอฟแวร์ในภาษาจาวา

โน๊ตไว้สั้นๆ กันลืมกับเว็บดีๆอย่าง java-source.net ไว้ค้นหา Open Source ซอฟแวร์ในภาษาจาวา มีการจัดหมดหมู่ไว้หาง่ายดี

วันเสาร์ที่ 18 ธันวาคม พ.ศ. 2553

ยกเลิกการแชร์ไฟล์ด้วยคำสั่ง net

เท้าความก่อนว่าทำไมจึงเกิดบล็อกนี้ขึ้น
ปกติการแชร์ไฟล์ใน Ubuntu ทำได้ง่ายๆโดยผ่าน nautilus ซึ่งเป็น file manager ที่มากับ Ubuntu อยู่แล้วเพียงแค่คลิ๊กขวาที่โฟลเดอร์เลือกแชร์ก็จบ แต่เกิดปัญหาเมื่อไปสั่งแชร์โฟลเดอร์ที่มีสัญลักษณ์พิเศษเข้า พอสั่ง create share มันจะแจ้งความผิดพลาดขึ้น
แต่พอ smb://localhost ปุ๊บอ้าวเฮ้ยทำไมไอ้ที่บอกว่าแชร์ไม่ได้กลับโผล่มาได้ไง nautilus ก็ยังแสดงโฟลเดอร์ว่าไม่ได้แชร์ ทีนี้จะปลดแชร์ได้ไงฟ่ะ

ค้นหาจนไปเจอคำสั่ง net ซึ่งสามารถจัดการเกี่ยวกับการแชร์ไฟล์ได้ รายละเอียดคำสั่งเกี่ยวแชร์ไฟล์มีดังนี้(ใช้ man ดู)
net usershare add sharename path [comment] [acl] [guest_ok=[y|n]] - to add or change a user defined share.
net usershare delete sharename - to delete a user defined share.
net usershare info [-l|--long] [wildcard sharename] - to print info about a user defined share.
net usershare list [-l|--long] [wildcard sharename] - to list user defined shares.

วิธียกเลิกแชร์ไฟล์ให้สั่ง
#net usershare list
เพื่อหาชื่อที่แชร์

ต่อจากนั้นค่อยสั่ง
#net usershare delete "myshare"
ตรง sharename ก็เอาชื่อที่ได้จากคำสั่งแรกใส่เข้าไปแต่ต้องครอบด้วยฟันหนู เพราะมันมีสัญลักษณ์พิเศษอยู่

หรืออีกวิธีให้เข้าไปลบใน /var/lib/samba/usershares/ ก็ได้เหมือนกัน (วิธีนี้ยังไม่ได้ลอง)

วันอาทิตย์ที่ 12 ธันวาคม พ.ศ. 2553

เชื่อม Struts กับ EJB3 ด้วย Spring

ลงไว้ที่ spring66 ด้วย ลงบล็อกส่วนตัวด้วยจะได้มีอะไรอัพเดทบ้าง =="

ได้มีโอกาศสอนการใช้ Struts 1.x ร่วมกับ EJB3 จริงๆอยากให้เค้าใช้ Spring MVC มากกว่า แต่กลัวเกิดปัญหาเรื่องความเชี่ยวชาญและความคุ้นเคยหากมาใช้ Spring MVC เดิมเค้าใช้ Struts อยู่แล้ว ไม่แน่ใจว่าระยะเวลาในการปรับให้เข้ากับเฟรมเวิร์คใหม่ด้วย เดี๋ยวมันจะออกทะเลเหมือนที่เกิดกับ Struts

ทีนี้การเชื่อม Struts กับ EJB3 จริงๆทำได้ไม่ยากคือ lookup JNDI ผ่าน InitialContext แต่การ lookup ทุกครั้งที่ใช้มันทำให้สมรรถนะออกมาไม่ดี วิธีส่วนใหญ่ที่ใช้กันคือสร้าง Delegate คลาสขึ้นมาเพื่อทำหน้าที่ในการติดต่อ EJB โดยเก็บ remote/local ที่ lookup เอาไว้ จะได้ไม่เสียเวลาเรียกใหม่

class  DelegateService {

private UserService userService;
//many EJB service
...

private DelegateService() {}

private static getEJB(String ejbName) {
.....
// process to call EJB (Session Bean)
.....
}
public static void getUserService() {
if (userService == null)
userService = getEJB("CustomerServiceBean/remote");
return userService;
}
...
}


เวลาเรียกใช้ Delegate ใน Action ก็จะได้ประมาณนี้

public class UserAction extends Action {
...
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
UserService service = DelegateService.getUserService();
...
}
}


ซึ่งตรงนี้เราจะใช้ Spring จัดการ โดยให้ String ติดต่อไปยัง EJB แทนหลังจากนั้นเอาอินสแตนท์ที่ได้ฉีดให้กับ Struts action โค้ดจะออกมาสะอาดขึ้น ตัว Action ก็ไม่ขึ้นกับ Delegate คลาส การทำ Unit testing ทำได้ง่ายขึ้นเยอะ และนอกจากนี้ยังสามารถใช้ Spring เป็นจุดเชื่อมต่อกับเทคโนโลยีหรือเฟรมเวิร์คอื่นได้อีกด้วย เขียนจาวาไม่ใช้ spring บาปใช่มั๊ย ^__^
Struts action ก็จะออกมาประมาณนี้

@Service("/users")
public class UserAction extends Action {
@Autowired
private UserService service;
...
}


ในตัวอย่าง UserAction เป็น Struts action และเป็น Spring bean ด้วย(จาก Annotation Service ของ Spring) ส่วน UserService คืออินเทอร์เฟสคลาสซึ่งอิมพลิเม็นท์อาจเป็น Session bean หรือเป็น POJO Service ที่อยู่ในฝั่ง web เองก็ได้ขึ้นกับการคอนฟิค Spring ว่าจะให้ฉีดอะไรลงมา

เข้าเรื่องการเชื่อม Spring เข้ากับ Struts มีสองวิธี

1. ActionSupport Classes
เริ่มด้วยวิธีง่ายๆก่อน Spring ได้เตรียม WebApplicationContextUtils เพื่อดึง bean ภายใต้ Spring context อีกที

ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
MyService service = (MyService) ctx.getBean("myService");


ต้องคอนฟิค Spring context ใน web.xml ด้วย

<web-app>
...
<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param><!-- Doesn't need but config for mapping other config -->

<param-name>contextConfigLocation</param-name><!-- This parameter is specified as a list of paths -->

<param-value>

/WEB-INF/applicationContext.xml

</param-value>

</context-param>

</web-app>


เพื่อให้ง่ายต่อการใช้งาน Spring ได้เตรียมคลาสสำหรับ Struts action พื้นฐานต่างๆไว้คือ ActionSupport, DispatchActionSupport, LookupDispatchActionSupport และ MappingDispatchActionSupport คลาสเหล่านี้มีเครื่องมืออำนวจความสะดวกต่างๆเช่นเมธตอด getWebApplicationContext() เพื่อเรียก WebApplicationContext

public class UserAction extends DispatchActionSupport {

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'delete' method...");
}
WebApplicationContext ctx = getWebApplicationContext();
UserManager mgr = (UserManager) ctx.getBean("userManager");
// talk to manager for business logic
return mapping.findForward("success");
}
}


จะเห็นได้ว่าวิธีนี้เรียบง่ายแต่ไม่ค่อยมีพลังเท่าไหร่ ซึ่งในวิธีหลังนี้เราจะสร้าง Struts action ให้เป็น Spring bean ทำให้สามารถใช้ความสามารถต่างของ Spring กับ Action ของเราได้เช่นทำ Dependency Injection, AOP ฯลฯ

2. ContextLoaderPlugin
วิธีนี้ Struts action จะถูกสร้างและจัดการโดย Spring ทำให้สามารถใช้ความสามารถต่างๆของ Spring กับ Struts action ของเราได้

Spring เตรียม plugin สำหรับ Struts เพื่อโหลด Spring context โดยคอนฟิค struts-config.xml ดังนี้

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>


กรณีไม่กำหนดไฟล์คอนฟิค Spring จะใช้ค่าพื้นฐานดังนี้ /WEB-INF/${action}-servlet.xml ซึ่ง action คือชื่อ servlet-name ของ ActionServlet ของ Struts ใน web.xml นั่นเอง หากต้องการระบุไฟล์คอนฟิคก็ทำได้ดังนี้

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>


หลังจากคอนฟิค plugin เสร็จขั้นตอนต่อไปเป็นการแม็พระหว่าง Struts กับ Spring มีสองวิธี แต่ทั้งคู่ใช้วิธีการแม็พที่เหมือนกันคือ Struts จะใช้ action path แม็พเข้ากับ bean name ของ Spring

struts-config.xml
<action path="/users" .../>


action-servlet.xml
<bean name="/users" class="com.whatever.struts.UserAction" .../>


2.1 DelegatingRequestProcessor
วิธีนี้อาศัยการปรับแต่ง controller ของ Struts โดยเซ็ต processorClass ใหม่ซึ่ง Spring เตรียมไว้คือ DelegatingRequestProcessor

struts-config.xml
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>


action path จะถูกแม็พเข้ากับ bean name ของ Spring การคอนฟิค action ของ Struts จึงไม่จำเป็นต้องกำหนด action type

<action path="/user" type="com.whatever.struts.UserAction"/>
<action path="/user"/><!-- type ไม่จำเป็น -->


ถ้าใช้ Struts module ต้องเติม module prefix หน้า bean name ของ Spring ด้วยเช่น <action path="/user"/> ที่มี module ชื่อ admin จะต้องกำหนด bean name ดังนี้ <bean name="/admin/user"/>

ถ้าใช้ Tiles กับ Struts ให้เปลี่ยน DelegatingRequestProcessor ไปใช้ DelegatingTilesRequestProcessor

2.2. DelegatingActionProxy
ใช้กรณีที่มีการปรับแต่ง controller ด้วย RequestProcessor ตัวอื่นทำให้ไม่สามารถใช้ DelegatingRequestProcessor หรือ DelegatingTilesRequestProcessor ได้ ทางแก้ก็คือแก้ไข RequestProcessor ใหม่เลียนแบบ DelegatingRequestProcessor หากไม่สามารถทำได้ ให้ใช้ DelegatingActionProxy เป็น action type แทนดังนี้

<action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"/>


จบการเชื่อม Spring กับ Struts ขอสรุปอีกทีเป็นตัวอย่างการใช้วิธี 2.1 ร่วมกับ Spring Annotation
web.xml คอนฟิค Struts ตามปกติ

struts-config.xml
คอนฟิค plugin
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>

คอนฟิค controller
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>

คอนฟิค action
<action path="/user" ...>
<forward name="success" .../>
<forward name="failure" .../>
</action>


applicationContext.xml
<context:component-scan base-package="com.whatever.struts.actions" /> <!-- ระบุแพคเกจที่จะให้ Spring เข้ามองหา -->
<jee:remote-slsb id="userService" business-interface="com.whatever.ejb.service.UserService" jndi-name="UserServiceBean/remote" /><!-- lookup หา EJB -->


com.whatever.struts.actions.UserAction.java
@Service("/users") // สร้าง Struts Action ให้เป็น Spring bean และแม็พเข้ากับ action path ของ Struts
public class UserAction extends Action {
@Autowired
private UserService service;
...
}


ใช้ maven กำหนด library ที่จะใช้
pom.xml
...
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-core</artifactId>
<version>${org.apache.struts}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-taglib</artifactId>
<version>${org.apache.struts}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-struts</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>struts</groupId>
<artifactId>struts</artifactId>
</exclusion>
</exclusions>
</dependency>
...


สั้นๆง่ายๆ แต่มีง่ายกว่านี้คือใช้ Spring MVC ซะ ชาบูๆ