0

PHP Exec function Alternative

Dalam projek linux aku ni, aku nak exec command linux untuk control proses x dapat nak buat sebab masalah dengan php exec function. Dia run tapi disebabkan priviledge problem x jadi2 aku buat. Habis aku configure safe_mode option kat php semua x jadi.

Last aku teringat Jimin cerita dia buat VPN pakai SSH. Aku pun try. Alhamdulillah jadik.
Script akan sentiasa looping.
Koronologinya :
  1. Start loop
  2. Ada satu script yang sentiasa monitor file command
  3. kalau file tu ada, dia exec
  4. sleep 2 saat
  5. delete file tersebut lepas dia execute
  6. kalau x de file tu, sleep 2 saat
  7. Loop lagi
  8. Tammat.
Contohnya mcm kat bawah :

#!/bin/sh
while [ 1 -ne 2 ]
do
if [ -f /opt/lampp/htdocs/ipvision-client/cmd.sh ]; then
/bin/chmod 777 /opt/lampp/htdocs/ipvision-client/cmd.sh
/bin/sh /opt/lampp/htdocs/ipvision-client/cmd.sh
sleep 2
/bin/rm /opt/lampp/htdocs/ipvision-client/cmd.sh
fi
sleep 2
done

p/s : boleh pendekkan lagi script ni.
0

Linux auto reboot on Kernel panic

Tertinggal henset kat rumah hari ni.
Takpe, kerja diteruskan. Aku tengah buat projek on Linux on tiny board.
Boot pakai usb. Kadang2 terjadi kernel panic. Carik punya jumpa cara yang simple.

pada bootloader config just tambah satu lagi paramter panic=5 , untuk saat. Nak lebih tambah sendirik. Hemo..hemoo..

Contonhnya macam kat bawah (sysLinux)

LABEL LINUX
KERNEL linux
APPEND root=/dev/sda3 rootdelay=10 panic=5
0

GWT HIbernate Native Query

Dalam aku dok try GWT + RPC + Hibernate, aku try la guna JPA (Java Persistent API) untuk manage database connection. Aku pakai EntityManager dengan annotation @persistentcontext.
Yang semua ok tapi untuk nativeQuery tanpa class object diberi aku dapat output yang berbeza.

Untuk toplink/EclipseLink : aku dapat sebagai List.
Pakai print toString outputnya = [[value1,value2],[value1,value2]]

Untuk yang lain (Hibernate/OpenJPA,KODO) : Ouputnya adalah object[][]
Pakai print toString outputnya = Object[][];

Contohnya macam kat bawah ni.

public List getSQLQuery(String sql) {
   Query query = em.createNativeQuery(sql);
   return query.getResultList();
}
0

GWT Custom Seriallizable HashTable

Untuk memudahkan data exchange antara server & client serta memudahkan client mencapai data, aku create satu seriallizable class ala hashtable. Pengguna boleh panggil data guna index atau key. Senang bukan ?

Anyway seperti, code dia ada kat bawah ni.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.com.client;

import com.gwtext.client.data.BooleanFieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author Peyo
*/
public class MyHashTable implements Serializable {

private ArrayList alValue = new ArrayList();
private ArrayList alKeys = new ArrayList();

public MyHashTable() {
}

public MyHashTable(List list, Object[] keys) {
for (int i = 0; i < list.size(); i++) {
for (int k = 0; k < keys.length; k++) {
add(keys[k], list.get(k));
}
}
}

public MyHashTable(Record record, RecordDef recordDef) {
alValue.clear();
alKeys.clear();
for (int i = 0; i < recordDef.getFields().length; i++) {
String fieldname = recordDef.getFields()[i].getName();
if (recordDef.getFields()[i].getClass().equals(StringFieldDef.class)) {
String value = record.getAsString(fieldname);
add(fieldname, value);
} else if (recordDef.getFields()[i].getClass().equals(IntegerFieldDef.class)) {
Integer value = record.getAsInteger(fieldname);
add(fieldname, value);
} else if (recordDef.getFields()[i].getClass().equals(FloatFieldDef.class)) {
Float value = record.getAsFloat(fieldname);
add(fieldname, value);
} else if (recordDef.getFields()[i].getClass().equals(BooleanFieldDef.class)) {
Object value = record.getAsBoolean(fieldname);
} else {
Object value = record.getAsObject(fieldname);
add(fieldname, value);
}
}
}

public final ArrayList getKeys() {
return alKeys;
}

public Object getValue(
Object key) {
Integer index = checkKeyExist(key);
if (index > -1) {
return alValue.get(index);
}

return null;
}

public Object getValue(Integer index) {
return alValue.get(index);
}

public Integer getCount() {
return Integer.valueOf(alKeys.size());
}

public void add(Object key, Object object) {
alValue.add(object);
alKeys.add(key);
}

public void updateItem(Object key, Object value) {
int index = checkKeyExist(key);
if (index > -1) {
alValue.set(index, value);
}
}

public void deleteItem(Object key) {
int index = checkKeyExist(key);
if (index > -1) {
alValue.remove(index);
alKeys.remove(index);
}
}



private Integer checkKeyExist(Object key) {
for (int i = 0; i <
alKeys.size(); i++) {
if (alKeys.get(i).equals(key)) {
return Integer.valueOf(i);
}

}
return Integer.valueOf(-1);
}

public Object[] toArray() {
return alValue.toArray();
}

public Record toRecord(
RecordDef recordDef) {
Object[] obj = new Object[recordDef.getFields().length];
for (int i = 0; i <
recordDef.getFields().length; i++) {
obj[i] = getValue(recordDef.getFields()[i].getName());
}

Record record = recordDef.createRecord(obj);
return record;
}
}
0

GWT RPC Custom Service Manager

Untuk memudahkan aku access kepada service GWT yang telah aku buat, aku create satu Service Manager class untuk manage service-service yang ada. Jadi x perlua setiap kali nak guna service tu declare satu persatu. Call aje, senang.

Actually, yang bahagian if else tu patutnya x ada, tapi sebab benda ni x stable aku terpaksa juga buat.


package my.com.client.common;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import my.com.client.GeneralSettings.GeneralSettingsService;
import my.com.client.GeneralSettings.GeneralSettingsServiceAsync;
import my.com.client.lecturer.LecturerService;
import my.com.client.lecturer.LecturerServiceAsync;
import my.com.client.login.LoginService;
import my.com.client.login.LoginServiceAsync;
import my.com.client.semester.SemesterService;
import my.com.client.semester.SemesterServiceAsync;
import my.com.client.subject.SubjectService;
import my.com.client.subject.SubjectServiceAsync;

/**
*
* @author Peyo
*/
public class ServiceManager {

public static LoginServiceAsync getLoginService() {
return (LoginServiceAsync) getService(LoginService.class,"loginservice");
}

public static SubjectServiceAsync getSubjectService() {
return (SubjectServiceAsync) getService(SubjectService.class,"subjectservice");
}

public static GeneralSettingsServiceAsync getGeneralSettingService() {
return (GeneralSettingsServiceAsync) getService(GeneralSettingsService.class,"generalsettingsservice");
}

public static SemesterServiceAsync getSemesterService(){
return (SemesterServiceAsync) getService(SemesterService.class,"semesterservice");
}

public static LecturerServiceAsync getLecturerService(){
return (LecturerServiceAsync) getService(LecturerService.class,"lecturerservice");
}

private static Object getService(Class myclass,String relativeurl) {
Object service=null;
if (relativeurl.equals("subjectservice")) {
service = GWT.create(SubjectService.class);
}else if(relativeurl.equals("loginservice")){
service = GWT.create(LoginService.class);
}else if(relativeurl.equals("generalsettingsservice")){
service = GWT.create(GeneralSettingsService.class);
}else if(relativeurl.equals("semesterservice")){
service = GWT.create(SemesterService.class);
}else if(relativeurl.equals("lecturerservice")){
service = GWT.create(LecturerService.class);
}

//service = GWT.create(myclass);
ServiceDefTarget endpoint = (ServiceDefTarget) service;
String moduleRelativeURL = GWT.getModuleBaseURL() + relativeurl;
endpoint.setServiceEntryPoint(moduleRelativeURL);
return service;
}
}
0

GWT Custom Exception

Masa guna gwt-rpc-java servlet aku kena buat server side error handling.
Untuk membolehkan exception tu boleh cross antara server side & client side aku kena buat satu exception class aku sendiri yang extend dari serializableException.
Code dia ada kat bawah tu aku pastekan.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.com.client.common;

import com.google.gwt.user.client.rpc.SerializationException;

/**
*
* @author Peyo
*/
public class MyException extends SerializationException {

public MyException() {
super();
}

public MyException(String string) {
super(string);
}

public MyException(Throwable cause) {
super(cause);
}

public MyException(String string,Throwable cause) {
super(string, cause);
}
}
 
Copyright © peyotest