public static void setLdapAttribute(String username,String password,MessageHeaderVO messageHeaderVO){
String attributeName[] ={"extensionattribute1","extensionattribute2","extensionattribute3"};
DirContext ctx=null;
try{
Hashtable env = new Hashtable();
//String password ="password";
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
String providerurl =CommonUtil.getResourceBundleMsg("ads_service_url", "com.ibm.ahb.idt.nl.AlHilalPortalPortletResourceMessages");
env.put(Context.PROVIDER_URL,providerurl);
//env.put(Context.PROVIDER_URL,PortletConstant.LDAP_PROVIDER_URL);
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"CN="+username+",CN=Users,DC=testbank,DC=ae");
env.put(Context.SECURITY_CREDENTIALS,password);
ctx=new InitialDirContext(env);
Attributes attrs = ctx.getAttributes("CN="+username+",CN=Users,DC=testbank,DC=ae",attributeName);
NamingEnumeration answer = attrs.getAll();
while (answer.hasMoreElements()) {
Attribute attr = (Attribute) answer.nextElement();
NamingEnumeration attributevalues= attr.getAll();
while(attributevalues.hasMoreElements())
{
if(attr.getID().equalsIgnoreCase(attributeName[0])){
messageHeaderVO.setRequestorRole((String)attributevalues.nextElement());
}
if(attr.getID().equalsIgnoreCase(attributeName[1])){
messageHeaderVO.setIsManagerFlag((String)attributevalues.nextElement());
}
if(attr.getID().equalsIgnoreCase(attributeName[2])){
messageHeaderVO.setBranchCode((String)attributevalues.nextElement());
}
}
}
}catch (AuthenticationException authe) {
logger.error("Error while creating Ldapcontext " + authe.getMessage());
}catch (Exception e) {
logger.error("Error while creating Ldapcontext" + e.getMessage());
}finally{
try{
ctx.close();
}catch(Exception Ignore)
{
}
}
}
Tuesday, May 10, 2011
Access LDAP from IBM Portlet Code
<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,com.ibm.jsr168.* ,com.ibm.portal.um.*,com.ibm.portal.um.portletservice.PumaHome,com.ibm.portal.portlet.service.PortletServiceHome"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model" prefix="portlet-client-model" %>
<%
ArrayList GROUP_ATTRS = null;
ArrayList EDIT_USER_ATTRS = null;
ArrayList USER_ATTRS = null;
GROUP_ATTRS = new ArrayList();
USER_ATTRS = new ArrayList();
EDIT_USER_ATTRS = new ArrayList();
USER_ATTRS.add("givenName");
USER_ATTRS.add("sn");
USER_ATTRS.add("uid");
GROUP_ATTRS = new ArrayList();
GROUP_ATTRS.add("cn");
GROUP_ATTRS.add("description");
EDIT_USER_ATTRS = new ArrayList();
EDIT_USER_ATTRS.add("uid");
// EDIT_USER_ATTRS.add("userPassword");
EDIT_USER_ATTRS.add("givenName");
EDIT_USER_ATTRS.add("sn");
// EDIT_USER_ATTRS.add("ibm-primaryEmail");
//EDIT_USER_ATTRS.add("preferredLanguage");
EDIT_USER_ATTRS.add("extensionattribute1");
EDIT_USER_ATTRS.add("extensionattribute2");
EDIT_USER_ATTRS.add("extensionattribute3");
com.ibm.jsr168.Jsr168PortletSessionBean sessionBean = (com.ibm.jsr168.Jsr168PortletSessionBean)renderRequest.getPortletSession().getAttribute(com.ibm.jsr168.Jsr168Portlet.SESSION_BEAN);
try{
PortletServiceHome psh;
javax.naming.Context ctx = new javax.naming.InitialContext();
psh = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
com.ibm.portal.um.portletservice.PumaHome service = (com.ibm.portal.um.portletservice.PumaHome) psh.getPortletService(com.ibm.portal.um.portletservice.PumaHome.class);
PumaProfile pp = service.getProfile(renderRequest);
PumaLocator pl = service.getLocator(renderRequest);
List userList = pl.findUsersByAttribute("uid", "idtsso1");
System.out.println("Test1"+userList);
User editUser = (User)userList.get(0);
Map userMap = pp.getAttributes(editUser, EDIT_USER_ATTRS);
Object attrObj = userMap.get("givenName");
ArrayList attrArray = null;
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1givenName"+(String)attrArray.get(0));
} else {
System.out.println("Test1givenName"+(String) attrObj);
}
attrObj = userMap.get("sn");
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1sn 1"+(String)attrArray.get(0));
} else {
System.out.println("Test1sn 1"+(String) attrObj);
}
attrObj = userMap.get("uid");
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Harishuid"+(String)attrArray.get(0));
} else {
System.out.println("Test1uid"+(String) attrObj);
}
attrObj = userMap.get("extensionattribute1");
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1extensionattribute1"+(String)attrArray.get(0));
} else {
System.out.println("Test1hextensionattribute1"+(String) attrObj);
}
}catch(Exception e){
System.out.println("Test1JSR168 "+e.getMessage());
}
%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model" prefix="portlet-client-model" %>
<%
ArrayList GROUP_ATTRS = null;
ArrayList EDIT_USER_ATTRS = null;
ArrayList USER_ATTRS = null;
GROUP_ATTRS = new ArrayList();
USER_ATTRS = new ArrayList();
EDIT_USER_ATTRS = new ArrayList();
USER_ATTRS.add("givenName");
USER_ATTRS.add("sn");
USER_ATTRS.add("uid");
GROUP_ATTRS = new ArrayList();
GROUP_ATTRS.add("cn");
GROUP_ATTRS.add("description");
EDIT_USER_ATTRS = new ArrayList();
EDIT_USER_ATTRS.add("uid");
// EDIT_USER_ATTRS.add("userPassword");
EDIT_USER_ATTRS.add("givenName");
EDIT_USER_ATTRS.add("sn");
// EDIT_USER_ATTRS.add("ibm-primaryEmail");
//EDIT_USER_ATTRS.add("preferredLanguage");
EDIT_USER_ATTRS.add("extensionattribute1");
EDIT_USER_ATTRS.add("extensionattribute2");
EDIT_USER_ATTRS.add("extensionattribute3");
com.ibm.jsr168.Jsr168PortletSessionBean sessionBean = (com.ibm.jsr168.Jsr168PortletSessionBean)renderRequest.getPortletSession().getAttribute(com.ibm.jsr168.Jsr168Portlet.SESSION_BEAN);
try{
PortletServiceHome psh;
javax.naming.Context ctx = new javax.naming.InitialContext();
psh = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
com.ibm.portal.um.portletservice.PumaHome service = (com.ibm.portal.um.portletservice.PumaHome) psh.getPortletService(com.ibm.portal.um.portletservice.PumaHome.class);
PumaProfile pp = service.getProfile(renderRequest);
PumaLocator pl = service.getLocator(renderRequest);
List userList = pl.findUsersByAttribute("uid", "idtsso1");
System.out.println("Test1"+userList);
User editUser = (User)userList.get(0);
Map userMap = pp.getAttributes(editUser, EDIT_USER_ATTRS);
Object attrObj = userMap.get("givenName");
ArrayList attrArray = null;
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1givenName"+(String)attrArray.get(0));
} else {
System.out.println("Test1givenName"+(String) attrObj);
}
attrObj = userMap.get("sn");
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1sn 1"+(String)attrArray.get(0));
} else {
System.out.println("Test1sn 1"+(String) attrObj);
}
attrObj = userMap.get("uid");
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Harishuid"+(String)attrArray.get(0));
} else {
System.out.println("Test1uid"+(String) attrObj);
}
attrObj = userMap.get("extensionattribute1");
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1extensionattribute1"+(String)attrArray.get(0));
} else {
System.out.println("Test1hextensionattribute1"+(String) attrObj);
}
}catch(Exception e){
System.out.println("Test1JSR168 "+e.getMessage());
}
%>
Access LDAP from Java Code
<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,com.ibm.jsr168.* ,com.ibm.portal.um.*,com.ibm.portal.um.PumaHome,javax.naming.*,javax.naming.directory.*"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model" prefix="portlet-client-model" %>
<%
DirContext ctx=null;
Hashtable env = null;
String password ="123";
env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"Ldap://127.0.0.1:389");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
// env.put(Context.SECURITY_PROTOCOL, "ssl");
/*
* sample website: company.com.tw
*
* DN: DC=company,DC=com,DC=tw
*/
env.put(Context.SECURITY_PRINCIPAL,"cn="+acct+",cn=users,DC=COMPANY DOMAIN");//,DC=com,DC=tw");
env.put(Context.SECURITY_CREDENTIALS,password);
try{
System.out.println("connecting...");
ctx=new InitialDirContext(env);
// Ask for all attributes of the object
Attributes attrs = ctx.getAttributes("CN="+user+",CN=Users,DC=ldapname,DC=ae");
NamingEnumeration answer = attrs.getAll();
while (answer.hasMoreElements()) {
//System.out.println("Inside while1....");
Attribute attr = (Attribute) answer.nextElement();
System.out.println(attr.getID() + "=");
NamingEnumeration answer1 = attr.getAll();
while(answer1.hasMoreElements())
{ System.out.println(attr.getID() + ":Value =" + (String)answer1.nextElement());
}
}
Attribute attr1 = attrs.get("departmentNumber");
Attribute attr2 =attrs.get("localityName");
Attribute attr3 =attrs.get("description");
if(attr1 != null)
{System.out.println("departmentNumber" + attr1.toString());
}
else
{System.out.println("departmentNumber is null");
}
if(attr2 != null)
{System.out.println("localityName" + attr2.toString());
}
else
{System.out.println("localityName is null");
}
if(attr3 != null)
{System.out.println("description" + attr3.toString());
}
else
{System.out.println("description is null");
}
System.out.println("success");
}catch(AuthenticationException authe){
System.out.println("fail");
authe.printStackTrace();
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}finally{
try{
ctx.close();
}catch(Exception Ignore)
{
}
}
%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model" prefix="portlet-client-model" %>
<%
DirContext ctx=null;
Hashtable env = null;
String password ="123";
env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"Ldap://127.0.0.1:389");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
// env.put(Context.SECURITY_PROTOCOL, "ssl");
/*
* sample website: company.com.tw
*
* DN: DC=company,DC=com,DC=tw
*/
env.put(Context.SECURITY_PRINCIPAL,"cn="+acct+",cn=users,DC=COMPANY DOMAIN");//,DC=com,DC=tw");
env.put(Context.SECURITY_CREDENTIALS,password);
try{
System.out.println("connecting...");
ctx=new InitialDirContext(env);
// Ask for all attributes of the object
Attributes attrs = ctx.getAttributes("CN="+user+",CN=Users,DC=ldapname,DC=ae");
NamingEnumeration answer = attrs.getAll();
while (answer.hasMoreElements()) {
//System.out.println("Inside while1....");
Attribute attr = (Attribute) answer.nextElement();
System.out.println(attr.getID() + "=");
NamingEnumeration answer1 = attr.getAll();
while(answer1.hasMoreElements())
{ System.out.println(attr.getID() + ":Value =" + (String)answer1.nextElement());
}
}
Attribute attr1 = attrs.get("departmentNumber");
Attribute attr2 =attrs.get("localityName");
Attribute attr3 =attrs.get("description");
if(attr1 != null)
{System.out.println("departmentNumber" + attr1.toString());
}
else
{System.out.println("departmentNumber is null");
}
if(attr2 != null)
{System.out.println("localityName" + attr2.toString());
}
else
{System.out.println("localityName is null");
}
if(attr3 != null)
{System.out.println("description" + attr3.toString());
}
else
{System.out.println("description is null");
}
System.out.println("success");
}catch(AuthenticationException authe){
System.out.println("fail");
authe.printStackTrace();
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}finally{
try{
ctx.close();
}catch(Exception Ignore)
{
}
}
%>
Thursday, April 7, 2011
Java TLD
package com.test;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;
public class ErrorCodes extends TagSupport {
private static Logger logger = Logger.getLogger(ErrorCodes.class);
private String codes;
private String portletnamespace;
public void setCodes(String codes) {
this.codes = codes;
}
public void setPortletnamespace(String namespace) {
this.portletnamespace = namespace;
}
public int doStartTag() throws JspException {
try {
StringBuffer sb = new StringBuffer();
StringTokenizer tokenizer = new StringTokenizer(codes,",");
ResourceBundle bundle = ResourceBundle.getBundle("com.ibm.ahb.idt.nl.AlHilalPortalPortletResourceMessages");
while (tokenizer.hasMoreElements()) {
String token = (String) tokenizer.nextElement();
String value=bundle.getString(token);
if(value!=null)
sb.append("");
}
pageContext.getOut().println(sb.toString());
} catch (IOException ioe) {
logger.error("Error: ErrorCodes while writing to client");
}
return SKIP_BODY;
}
}
-----------------------------------------------------------------------
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-
jsptaglibrary_2_0.xsd"
version="2.0">
errorcodes
com.ibm.ahb.idt.tag.ErrorCodes
empty
Tag with Parameter
codes
true
true
portletnamespace
true
true
---------------------------------------------------------------------------
Please write below code in jsp
<%@ taglib uri="/WEB-INF/lib/errorcodetaglib.tld" prefix="codes" %>
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;
public class ErrorCodes extends TagSupport {
private static Logger logger = Logger.getLogger(ErrorCodes.class);
private String codes;
private String portletnamespace;
public void setCodes(String codes) {
this.codes = codes;
}
public void setPortletnamespace(String namespace) {
this.portletnamespace = namespace;
}
public int doStartTag() throws JspException {
try {
StringBuffer sb = new StringBuffer();
StringTokenizer tokenizer = new StringTokenizer(codes,",");
ResourceBundle bundle = ResourceBundle.getBundle("com.ibm.ahb.idt.nl.AlHilalPortalPortletResourceMessages");
while (tokenizer.hasMoreElements()) {
String token = (String) tokenizer.nextElement();
String value=bundle.getString(token);
if(value!=null)
sb.append("");
}
pageContext.getOut().println(sb.toString());
} catch (IOException ioe) {
logger.error("Error: ErrorCodes while writing to client");
}
return SKIP_BODY;
}
}
-----------------------------------------------------------------------
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-
jsptaglibrary_2_0.xsd"
version="2.0">
---------------------------------------------------------------------------
Please write below code in jsp
<%@ taglib uri="/WEB-INF/lib/errorcodetaglib.tld" prefix="codes" %>
Monday, March 28, 2011
Data sorting by java Comparator,Comparable
package com.test;
public class Employee implements Comparable {
private int empId;
private String name;
private int age;
public Employee(int empId, String name, int age) {
this.empId = empId;
this.name = name;
this.age = age;
}
// getters & setters
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Employee o) {
return this.empId - o.empId ;
// return this.age - o.age ;
}
}
---------------------------------------------------------------------
package com.test;
import java.util.*;
public class Util {
public static List getEmployees() {
List col = new ArrayList();
col.add(new Employee(5, "Frank", 28));
col.add(new Employee(1, "Jorge", 19));
col.add(new Employee(6, "Bill", 34));
col.add(new Employee(3, "Michel", 10));
col.add(new Employee(7, "Simpson", 8));
col.add(new Employee(4, "Clerk",16 ));
col.add(new Employee(8, "Lee", 40));
col.add(new Employee(2, "Mark", 30));
return col;
}
}
-----------------------------------------------------------------------
package com.test;
import java.util.*;
public class TestEmployeeSort {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List coll = Util.getEmployees();
// System.out.println(coll);
Collections.sort(coll); // sort method
//Collections.sort(coll, new EmpSortByName());
//Collections.sort(coll, new EmpSortByEmpId());
// Collections.sort(coll, new EmpSortByAge());
//System.out.println(coll);
printList(coll);
}
private static void printList(List list) {
System.out.println("EmpId\tName\tAge");
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
}
}
}
-------------------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34
----------------------------------------------------------------------
package com.test;
import java.util.*;
public class EmpSortByName implements Comparator{
public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
}
--------------------------------------------------
Result
EmpId Name Age
6 Bill 34
4 Clerk 16
5 Frank 28
1 Jorge 19
8 Lee 40
2 Mark 30
3 Michel 10
7 Simpson 8
---------------------------------------------------------
package com.test;
import java.util.Comparator;
public class EmpSortByAge implements Comparator{
public int compare(Employee o1, Employee o2) {
return o1.getAge() - o2.getAge();
}
}
-----------------------------------------------------------
Result
EmpId Name Age
7 Simpson 8
3 Michel 10
4 Clerk 16
1 Jorge 19
5 Frank 28
2 Mark 30
6 Bill 34
8 Lee 40
--------------------------------------------------------------
package com.test;
import java.util.*;
public class EmpSortByEmpId implements Comparator{
public int compare(Employee o1, Employee o2) {
return o1.getEmpId() - o2.getEmpId();
}
}
----------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34
7 Simpson 8
8 Lee 40
-----------------------------------------------------------------
public class Employee implements Comparable
private int empId;
private String name;
private int age;
public Employee(int empId, String name, int age) {
this.empId = empId;
this.name = name;
this.age = age;
}
// getters & setters
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Employee o) {
return this.empId - o.empId ;
// return this.age - o.age ;
}
}
---------------------------------------------------------------------
package com.test;
import java.util.*;
public class Util {
public static List
List
col.add(new Employee(5, "Frank", 28));
col.add(new Employee(1, "Jorge", 19));
col.add(new Employee(6, "Bill", 34));
col.add(new Employee(3, "Michel", 10));
col.add(new Employee(7, "Simpson", 8));
col.add(new Employee(4, "Clerk",16 ));
col.add(new Employee(8, "Lee", 40));
col.add(new Employee(2, "Mark", 30));
return col;
}
}
-----------------------------------------------------------------------
package com.test;
import java.util.*;
public class TestEmployeeSort {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List coll = Util.getEmployees();
// System.out.println(coll);
Collections.sort(coll); // sort method
//Collections.sort(coll, new EmpSortByName());
//Collections.sort(coll, new EmpSortByEmpId());
// Collections.sort(coll, new EmpSortByAge());
//System.out.println(coll);
printList(coll);
}
private static void printList(List
System.out.println("EmpId\tName\tAge");
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
}
}
}
-------------------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34
----------------------------------------------------------------------
package com.test;
import java.util.*;
public class EmpSortByName implements Comparator
public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
}
--------------------------------------------------
Result
EmpId Name Age
6 Bill 34
4 Clerk 16
5 Frank 28
1 Jorge 19
8 Lee 40
2 Mark 30
3 Michel 10
7 Simpson 8
---------------------------------------------------------
package com.test;
import java.util.Comparator;
public class EmpSortByAge implements Comparator
public int compare(Employee o1, Employee o2) {
return o1.getAge() - o2.getAge();
}
}
-----------------------------------------------------------
Result
EmpId Name Age
7 Simpson 8
3 Michel 10
4 Clerk 16
1 Jorge 19
5 Frank 28
2 Mark 30
6 Bill 34
8 Lee 40
--------------------------------------------------------------
package com.test;
import java.util.*;
public class EmpSortByEmpId implements Comparator
public int compare(Employee o1, Employee o2) {
return o1.getEmpId() - o2.getEmpId();
}
}
----------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34
7 Simpson 8
8 Lee 40
-----------------------------------------------------------------
Monday, March 21, 2011
Java Unicode converter
package com.test;
public class Test {
public static void main(String arg[]) {
//System.out.print("كل ما تحب معرفته عن");
System.out.print(convert("كل ما تحب معرفته عن"));;
}
public static String convert(String str)
{
StringBuffer ostr = new StringBuffer();
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if ((ch >= 0x0020) && (ch <= 0x007e)) // Does the char need to be
// converted to unicode?
{
ostr.append(ch); // No.
} else // Yes.
{
ostr.append("\\u"); // standard unicode format.
String hex = Integer.toHexString(str.charAt(i) & 0xFFFF); // Get
// hex
// value
// of
// the
// char.
for (int j = 0; j < 4 - hex.length(); j++)
// Prepend zeros because unicode requires 4 digits
ostr.append("0");
ostr.append(hex.toLowerCase()); // standard unicode format.
// ostr.append(hex.toLowerCase(Locale.ENGLISH));
}
}
return (new String(ostr)); // Return the stringbuffer cast as a string.
}
}
public class Test {
public static void main(String arg[]) {
//System.out.print("كل ما تحب معرفته عن");
System.out.print(convert("كل ما تحب معرفته عن"));;
}
public static String convert(String str)
{
StringBuffer ostr = new StringBuffer();
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if ((ch >= 0x0020) && (ch <= 0x007e)) // Does the char need to be
// converted to unicode?
{
ostr.append(ch); // No.
} else // Yes.
{
ostr.append("\\u"); // standard unicode format.
String hex = Integer.toHexString(str.charAt(i) & 0xFFFF); // Get
// hex
// value
// of
// the
// char.
for (int j = 0; j < 4 - hex.length(); j++)
// Prepend zeros because unicode requires 4 digits
ostr.append("0");
ostr.append(hex.toLowerCase()); // standard unicode format.
// ostr.append(hex.toLowerCase(Locale.ENGLISH));
}
}
return (new String(ostr)); // Return the stringbuffer cast as a string.
}
}
DailyRollingFileAppender
With the following Log4j configuration
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=sample.log
log4j.appender.R.DatePattern='.'yyyy-MM-dd
The sample.log will be copied to sample.log.2006-06-06, example,
sample.log
sample.log.2006-06-06
sample.log.2006-06-05
---------------------------------------------------------------------------------
name="Rolling" class="org.apache.log4j.RollingFileAppender"> - name="file" value="${catalina.home}/logs/test.log" />
- name="maxFileSize" value="10000KB" />
- name="maxBackupIndex" value="25" />
class ="org.apache.log4j.PatternLayout">- name="ConversionPattern" value="[%t] %d{HH:mm:ss,SSS} %-5p %l - %m%n" />
name="Daily" class="org.apache.log4j.DailyRollingFileAppender"> - name="file" value="${catalina.home}/logs/test.log" />
- name="DatePattern" value="'.'yyyy-MM-dd" />
- name="MaxBackupIndex" value="10" />
class ="org.apache.log4j.PatternLayout">- name="ConversionPattern" value="[%t] %d{HH:mm:ss,SSS} %-5p %l - %m%n" />
-
Subscribe to:
Posts (Atom)