Saturday, May 15, 2010

Few Simple Steps to Setup TWiki

TWiki is a Perl based structured Wiki application which can be used as team portal to share documents, presentations, blogs and many more within an organization. Thus it acts as a complete knowledge base. I did the setup for my team (on a Ubuntu Linux Server) and thought probably I should share the installation process, so that more and more people can easily get the benefit from this application-

Installation Steps-

  1. Install Apache Web Server: (httpd-2.2.15.tar.bz2)- http://httpd.apache.org/docs/2.0/install.html
  2. Make sure Perl (v5.10.0) is installed.
  3. Download Twiki- (TWiki-4.3.1.tgz)- http://sourceforge.net/projects/twiki/files/

Decompress using user account (not root), the uncompressed files automatically takes the access permission of the user account which uncompressed it. Place it inside of htdocs i.e. WEB_ROOT folder of Apache Installation e.g. for my setup I placed twiki inside of- /home/pprasanta/apache/htdocs/

  1. Configure TWiki, you can follow these tutorials

http://www.physics.carleton.ca/research/experimental/twiki/INSTALL.html

http://www.gentoo-wiki.info/HOWTO_TWiki

http://galeon.sourceforge.net/TWiki/TWikiInstallationGuide#StepTwo

  1. Give writable permission to “data”, “lib” and “working”. (chmod 777).
  2. Rename 2 existing sample configuration files- twiki_httpd_conf.txt to twiki_httpd.conf (in main folder of /twiki) and LocalLib.cfg.txt to LocalLib.cfg (you can find it in /twiki/bin)
  3. Define twiki lib path in LocalLib.cfg-

$twikiLibPath = "/home/pprasanta/apache/htdocs/twiki/lib";

  1. Update twiki_httpd.conf with the correct absolute (not relative) path of different twiki directories e.g. for my setup, lib path is /home/pprasanta/apache/htdocs/twiki/lib
  2. Update twiki_httpd.conf : Give access only to your IP address to access Configuration section i.e. /bin/configure. This is important as you don’t want unwanted person to change your configuration.

SetHandler cgi-script

Order Deny,Allow

Deny from all

Allow from 127.0.0.1 YOUR_IP

Require user JohnDoe

Satisfy Any

  1. Link the twiki_httpd.conf into http.conf

# TWiki

Include /home/pprasanta/apache/htdocs/twiki/twiki_httpd.conf

  1. Restart Apache and access the configuration page: http:///twiki/bin/configure
  2. When you are doing the configuration for the first time i.e. you are accessing- http:///twiki/bin/configure First time configuration will create- LocalSite.cfg which will store all configuration information of TWiki. /home/pprasanta/apache/htdocs/twiki/lib/LocalSite.cfg
  3. While configurations, for “Store Settings”, use RcsLite (this is the revision control system, which TWiki uses to manage document versions).
  4. If everything goes fine, start using TWiki- http:///twiki/bin/view
  5. IMPORTANT: You need to setup Email Notification setting with you SMTP HOST details. TWiki registration will not work without successful Email Notification setup.

Troubleshooting

  1. Configuration file /home/pprasanta/apache/htdocs/twiki/lib/LocalSite.cfg does not exist. For first time configuration, this error will come as there is no LocalSite.cfg file in your TWiki installation and it will create a new one. Make sure you have already followed step-4 i.e. already given access rights to mentioned directories.
  2. IMPORTANT: If you face problem like Can't locate Unicode/String.pm this means your PERL installation doesn’t have Unicode/String.pm file installed. Run following command as “Root” user to install Unicode/String.pm module- (Depending upon the user account permission, if you run the following command from any other user account doesn’t have sufficient permission, will result in failed String.pm setup)

# perl -MCPAN -e 'install Unicode::String'

Kindly Note:

I am not a TWiki Setup expert; I did this setup for my team and thought a small reference document might help others.

Wednesday, May 5, 2010

Android Custom TextView

Custom Text View
It will have following behavior-
1. A Single TextView will be having 2 segments of texts with different font and color options e.g. first and last name having different appearance
2. Two custom attributes to specify these 2 segments of text from XML layout e.g. “first_name” and “last_name”.
Following sections will guide you with the changes you need to make on Java code and XML files-
Creating Custom View
Important methods to overwrite-
-onMeasure()
-onDraw()
-Constructor (extend TextView class and its default constructors)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
widthMeasureSpec – this contains some integer constant value rather than actual width. It indicates whether user has mentioned custom width or standard width definition e.g. fill_parent
heightMeasureSpec- constant value which indicates the procedure to find out drawing area height.
Width and Height calculation should be based on the text size (pixel width) and font height, unless user specifies values. Once width and height calculation is done using (widthMeasureSpec and heightMeasureSpec), you should set the required drawing area using-
setMeasuredDimension(reqWidth, reqHeight);
Note: It’s really important to define the correct size of the required drawing area. Android platform will not let you draw anywhere except your defined drawing area.
protected void onDraw(Canvas canvas)
Note: Android color is 4 byte value and Most Significant byte indicates Alpha value (00- complete transparent i.e. background will be visible and FF- complete non transparent, background is not visible)
Create Paint object with desired Font and Color information. Paint objects need to be passed as design briefing while drawing text or shapes.
Constructor- if you are not passing information of any attribute through layout XML.
Constructor with Attributes- if you want to define the TextView attributes e.g. width, height, initial text etc.
Adding Custom Attributes
1. Define a new NameSpace: xmlns:my="http://schemas.android.com/apk/res/com.my" (“com.my” is the package name defined in Manifest.xml)
2. Define the custom attributes in /res/values/attrs.xml file. Android platform need to know the new attributes that you will be using. Attrs.xml file solves the purpose. Specify the attributes within . Multiple “attr” tags can be grouped inside of “declare-styleable”. Please refer “Custom Layout Resources”. We will define 2 attributes- “first_name” and “last_name”.
xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="first_name" format="string"/>
<attr name="last_name" format="string"/>
declare-styleable>
resources>
3. Using custom attributes (first_name and last_name) in layout file-
Define attribute values in XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/com.my"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.my.ui.MyTextView
android:id="@+id/MyTextView"
android:layout_width="fill_parent"
android:layout_height="50px"
my:first_name="Phil"
my:last_name="Jones"
/>
Read Attribute values in Java Code
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
for(int i=0; i < getAttributeCount(); i++){
Log.i(TAG, attrs.getAttributeName(i));
/*
* Read value of custom attributes
*/
this.firstText = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.my", "first_name");
this.lastText = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.my", "last_name");
}
Download the complete sample application, CustomTextView.zip

My Day with Eclipse MAT and Dominator Tree

Eclipse MAT (Memory Analyzing Tool) is a feature rich Heap Dump analysis tool. This helps you to track down important pointers on MOB (Memory Out of Bound) Exception. Sun JVM (1.4.2 onwards) supports the feature of creating HPROF HeapDump. MAT parses heap dump file (i.e. .hprof) and gives graphics intuitive description of Memory Leak suspects.

The most important information of Heap Dump is “Dominator Tree”, in simple word, it lists the objects and the amount of memory space that they are holding. Before understanding “Dominator Tree”, you need to know 3 important concepts of Java Objects- Shallow Size, Retain Size and Retain Set. There is a nice article which explains this concept.

How to find out the Biggest Retain Set ?

The best possible approach to find out the biggest “Retain Set” and hence the maximum prospect of releasing heap storage on next Garbage Collection (GC), is “Dominator Tree” graph. Dominator Tree Algorithm converts the Object Reference Graph to Dominator Tree, the fastest way to find out the biggest “Retain Set”.

In the following section I'll try to explain how to analyze Dominator Tree with an example code where I have added multiple inter-dependent object references and an infinite loop to get MOB Exception.

Dominator Tree (Eclipse MAT)



Dominator Tree (java_pid1076.hprof)



Retained Heap = 96 (Shallow Heap of Data i.e. 32 + Retained Heap of A, B, C, D i.e. 48)

Shallow Size of Data = 32 {8 (header) + 4 (String header) + 4 (A header) + 4 (B header) + 4 (C header) + 4 (D header)}

Note:You need to set VM argument (-XX:+HeapDumpOnOutOfMemoryError) to get HPROF heap dump if there is any MOB Exception.

Object Reference graph to Dominator Tree Conversion


Object Reference Graph

Data-> {a, b, c, d}

b-> {c}

d-> {b, c}

Dominator Tree (Retain Set)

Data-> {Data, a, b, c, d}

a -> {a}

b -> {b}

c -> {c}

d -> {d}

Note: Here a, b, c, d are instances/objects of A, B, C and D classes.

Above example uses following classes-

public class HeapDemo {

public void eatUpMemo(){

// do some

ArrayList fatMan = new ArrayList();

for(;;){

Data d = new Data();

fatMan.add(d);

}

}

}

public class Data {

String title = "ABC";

A a = new A();

B b = new B();

C c = b.getC();

D d = new D(b);

}

public class A {

int x = 10;

}

public class B {

int y = 20;

C c = new C();

public C getC(){

return c;

}

}

public class C {

int z = 30;

}

public class D {

C c;

B b;

public D(B b){

this.b = b;

c = b.getC();

}

}

Reference: