Wednesday, November 7, 2012

ls command howto


Output from ls -la Command

picard@spnode15$ ls -la

total 1600
drwx---s-x  11 picard   STAFF       1536 Jun 26 14:49 .
dr-xr-sr-x1300 bin      bin        20480 Jun 26 12:06 ..
-rw-------   1 picard   STAFF        948 Jun 06 09:46 .addressbook
-rw-------   1 picard   STAFF       3368 Jun 06 09:46 .addressbook.lu
-rw-------   1 picard   STAFF        193 Apr 02 10:06 .article
-rw-------   1 picard   STAFF       1035 May 20 12:30 .bash_history
drwx---S--   2 picard   STAFF        512 Jun 23 13:56 .mailpgp
-rw-------   1 picard   STAFF     128654 Jun 10 19:19 .newsrc
drwx------   4 picard   STAFF        512 May 29 07:01 .pgp
-rw-------   1 picard   STAFF      10196 Jun 26 14:33 .pinerc
-rwxr-xr-x   1 picard   STAFF       1047 May 27 14:15 .plan
-rw-------   1 picard   STAFF         35 Jun 17 09:23 .profile
-rw-------   1 picard   STAFF        371 Sep 08 1995  .signature
-rw-------   1 picard   STAFF      81691 Jun 20 10:34 3dtree.jpg
-rw-------   1 picard   STAFF      31156 Jan 03 10:19 HTMLBgnrGuide.txt
drwx---s-x   2 picard   STAFF        512 Apr 01 13:26 News
-rw-------   1 picard   STAFF      11760 Jul 23 1995  TUTORIAL
-rw-------   1 picard   STAFF        234 Feb 02 08:18 baen.txt
drwx---s-x   2 picard   STAFF        512 Mar 12 06:57 bin
-rw-------   1 picard   STAFF         71 Jul 31 1995  calendar
-rw-------   1 picard   STAFF     338912 May 02 1995  command.memos
-rw-------   1 picard   STAFF        747 Jun 24 13:12 dead.letter
-rw-------   1 picard   STAFF      10506 Jun 01 12:42 info.listserv
-rw-------   1 picard   STAFF     698675 Nov 01 1995  jim.kirk.letters
-rw-------   1 picard   STAFF        122 Jun 24 13:28 junk
drwx------   2 picard   STAFF       1536 Jun 25 12:40 mail
-rw-r-----   1 picard   STAFF       1397 May 28 12:50 mj.ultra
drwx---s-x   2 picard   STAFF        512 May 26 21:09 pine
-rw-------   1 picard   STAFF       1716 Jul 23 1995  print.txt
drwxr-sr-x   6 picard   STAFF       1024 Mar 27 10:54 public_html
drwx---s-x   3 picard   STAFF        512 Mar 31 07:24 rexx
drwx---s-x   2 picard   STAFF        512 Aug 08 1995  temp
-rwx--x--x   1 picard   STAFF        368 May 28 14:10 unpgp

picard@spnode15$

Let's look at this listing, and see what it tells us. At first glance, the left-most column looks like utter nonsense; drwxr--blah-blah-blah. What's going on here?

File Type. The first position/character in this column describes what type of entry each horizontal line represents. The first character will usually be either a - or a d. Symbols in the first (left most) position on the line represent:

-

    = Regular File
d

    = Directory

You can tell, just by looking at the output from ls -l which entries are files and which are directories.

Permissions. The rest of the first column (after the - or d) indicates access permissions which have been set either explicitly, by you, using the chmod command (which we'll look at later) or automatically, by the system, when the file (or directory) was created. We'll discuss this part of the listing in more detail when we get to the chmod command, which deals with setting file access permissions.

Directory Entries (and Hard-Link Count). The next column (immediately to the right of the access permissions) is a number which tells how many directory entries are under that item. For a regular file, this will typically be 1. For a directory, this will always be at least 2. The reason for this is that every directory always contains pointers to both itself, and its parent directory. You can see these two entries as the first two items in our example listing in Figure 12: the entries for . and .. (i.e. one period, and two periods). The single period . ), is the pointer to the current directory--the directory that you are "in" right now. Two periods .. ), point to the parent directory--the directory which contains the current directory. You can use these as convenient nicknames/shortcuts in some commands, when you want to describe a relative pathname.

Owner. The next column to the right displays the userid of the owner of this file or directory. In our listing, most of the files are owned by our hypothetical user, picard. When you create files and directories, you will be their owner, and your userid will show here, in place of picard.

Group. The next column (mostly "STAFF" in our example) shows the name of the user group which is connected with this entry. Each userid is a member of one or more groups, and access permissions may be set which determine what sort of access other members of the same group have to the file or directory named. Again, more on that when we get to the chmod command.

Size. The next column shows the file size in bytes (characters).

Date/Time. The next 3 columns show the date and time the file was last modified.

File Name. Finally, we have the file name. As discussed earlier, UNIX is very accommodating with regard to the length of file names. However there are some qualifications: File names cannot (usually) contain blank spaces, or the characters /, *, or ?. This is because / is used to separate levels of a pathname, * is a "wildcard" character which is expanded by the system to mean "any number of any characters," and ? is a wildcard representing "any single character." MS-DOS users in particular should recognize these "wildcard" characters.


http://docweb.cns.ufl.edu/docs/d0107/ar07s04.html



  • Link counter


Most file systems that support hard links use reference counting. An integer value is stored with each physical data section. This integer represents the total number of links that have been created to point to the data. When a new link is created, this value is increased by one. When a link is removed, the value is decreased by one. If the link count becomes zero, the operating system usually automatically deallocates the data space of the file if no process has the file opened for access. The maintenance of this value assists users in preventing data loss. This is a simple method for the file system to track the use of a given area of storage, as zero values indicate free space and nonzero values indicate used space.

On POSIX-compliant operating systems, such as many Unix-variants, the reference count for a file or directory is returned by the stat() or fstat() system calls in the st_nlink field of struct stat.



  • Link count: File vs Directory 

One of the results of the ls -l command  is the link count.

1. What is the link count of a file?
The link count of a file tells the total number of links a file has.the number of hard-links a file has.
The soft-link is not part of the link count since the soft-link's inode number is different from the original file.

2. How to find the link count of a file or directory?
any new file created will have a link count 1.
By default, a file will have a link count of 1

$ touch  test.c
$ ln test.c test-hardlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:44 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test.c
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test-hardlink.c

$ ln -s test.c test-softlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:45 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test.c
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test-hardlink.c
3145745 lrwxrwxrwx 1 vagrant vagrant    6 Mar 31 11:45 test-softlink.c -> test.c

3. Does the link count decrease whenever the hard-link is deleted?
When the hard link file is moved or deleted, the link count of the original file gets reduced.

rm test-hardlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:47 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 1 vagrant vagrant    0 Mar 31 11:44 test.c
3145745 lrwxrwxrwx 1 vagrant vagrant    6 Mar 31 11:45 test-softlink.c -> test.c

4. When does the link count of a directory change?
A directory "xyz" is created and the default link count of any directory is 2. The extra count is because for every directory created, a link gets created in the parent directory to point to this new directory.
link count of a directory minus 2 gives you the total number of sub-directories present in the directory.

mkdir xyz
ls -ld xyz/
drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:50 xyz/
mkdir -p xyz/abc
mkdir -p xyz/efg
ls -ldia xyz
3145746 drwxrwxr-x 4 vagrant vagrant 4096 Mar 31 11:51 xyz

http://www.theunixschool.com/2012/10/link-count-file-vs-directory.html

  • Q1: The Unix inode structure contains a reference count. What is the reference count for? Why can't we just remove the inode without checking the reference count when a file is deleted?


    Inodes contain a reference count due to hard links. The reference count is equal to the number of directory entries that reference the inode. For hard-linked files, multiple directory entries reference a single inode. The inode must not be removed until no directory entries are left (ie, the reference count is 0) to ensure that the filesystem remains consistent.


  • On a storage device, a file or directory is contained in a collection of blocks

Information about a file is contained in an inode, which records information such as the owner, when the file was last accessed, how large it is, whether it is a directory or not, and who can read from or write to it. The inode number is also known as the file serial number and is unique within a particular filesystem
https://developer.ibm.com/tutorials/l-lpic1-104-6/

Sunday, November 4, 2012

n-tiered application



  • The J2EE platform is a multi-tiered system

A tier is a logical or functional partitioning of a system

Client tier represents Web browser, a Java or other application, Applet, WAP phone etc. The client tier makes requests to the Web server who will be serving the request by either returning static content if it is present in the Web server or forwards the request to either Servlet or JSP in the application server for either static or dynamic content.

Presentation tier encapsulates the presentation logic required to serve clients. A Servlet or JSP in the presentation tier intercepts client requests, manages logons, sessions, accesses the business services, and finally constructs a response, which gets delivered to client.

Business tier provides the business services. This tier contains the business logic and the business data. All the business logic is centralized into this tier as opposed to 2-tier systems where the business logic is scattered between the front end and the backend. The benefit of having a centralized business tier is that same business logic can support different types of clients like browser, WAP, other stand-alone applications etc.

Integration tier is responsible for communicating with external resources such as databases, legacy systems, ERP systems, messaging systems like MQSeries etc. The components in this tier use JDBC, JMS, J2EE Connector Architecture (JCA) and some proprietary middleware to access the resource tier.

Resource tier is the external resource such as a database, ERP system, Mainframe system etc responsible for storing the data. This tier is also known as Data Tier or EIS (Enterprise Information System) Tier.



The advantages of a 3-tiered or n-tiered application:
3-tier or multi-tier architectures force separation among presentation logic, business logic and database logic

Manageability: Each tier can be monitored, tuned and upgraded independently and different people can have clearly defined responsibilities.

·         Scalability: More hardware can be added and allows clustering (i.e. horizontal scaling).

·         Maintainability: Changes and upgrades can be performed without affecting other components.

·         Availability: Clustering and load balancing can provide availability.

·         Extensibility: Additional features can be easily added.
http://allu.wordpress.com/2007/08/18/j2ee-3-tier-or-n-tier-architecture/



  • In software engineering, multi-tier architecture (often referred to as n-tier architecture) is a client–server architecture in which presentation, application processing, and data management functions are logically separated. For example, an application that uses middleware to service data requests between a user and a database employs multi-tier architecture. The most widespread use of multi-tier architecture is the three-tier architecture.

http://en.wikipedia.org/wiki/Multitier_architecture



  • Using J2EE to develop n-tier applications involves breaking apart the different layers in the two-tier architecture into multiple tiers. An n-tier application could provide separate layers for each of the following services:


    Presentation: In a typical Web application, a browser running on the client machine handles presentation.
    Dynamically generated presentation: Although a browser could handle some dynamically generated presentation, for the widest support of different browsers much of the action should be done on the Web server using JSPs, servlets, or XML (Extensible Markup Language) and XSL (Extensible Stylesheet Language).
    Business logic: Business logic is best implemented in Session EJBs (described later).
    Data access: Data access is best implemented in Entity EJBs (described later) and using JDBC.
    Backend system integration: Integration with backend systems may use a variety of technologies. The best choice will depend upon the exact nature of the backend system.
http://www.javaworld.com/jw-12-2000/jw-1201-weblogic.html

Proxy Pattern



  • The Proxy Pattern

Intent
Provide a surrogate or placeholder for another object to control access to it

A proxy is
› a person authorized to act for another person
› an agent or substitute
› the authority to act for another

There are situations in which a client does not or can not reference an
object directly, but wants to still interact with the object

A proxy object can act as the intermediary between the client and the target
object

http://userpages.umbc.edu/~tarr/dp/lectures/Proxy.pdf



  • Intent

    Provide a surrogate or placeholder for another object to control access to it.
    Use an extra level of indirection to support distributed, controlled, or intelligent access.
    Add a wrapper and delegation to protect the real component from undue complexity.

Problem

You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.

Example

The Proxy provides a surrogate or place holder to provide access to an object. A check or bank draft is a proxy for funds in an account. A check can be used in place of cash for making purchases and ultimately controls access to cash in the issuer’s account.

Rules of thumb

    Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
    Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
 
http://sourcemaking.com/design_patterns/proxy



  • Proxy pattern

A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.
http://en.wikipedia.org/wiki/Proxy_pattern

flyweight pattern



Basit nesneler olusturmak istiyorsunuz ve bu nesnelerin bazi
veri degerleri farklidir. Nesnelerdem çok fazla
olusturdugunuzda bellek fazla kullanilacaksa, nesnelerden 1
tane olusturup, farkli verileri bu nesneye metot parametresi
olarak göndermek daha iyi olacaktir.

Bu kullanim flyweight sablonudur.

Örnegin; bir kelime islemci uygulamasindaki her harf için bir
nesne olusturmaktansa harfin bir kopyasi olusturulur ve
kullanilacagi yerlerde bu kopyanin referansi kullanilir.

http://members.comu.edu.tr/msahin/courses/ust_duzey_files/patterns/flyweight.pdf

Abstract Factory pattern


Abstract Factory pattern

Intent

Provide an interface for creating families of related or dependent objects
without specifying their concrete classes.

The Abstract Factory pattern is very similar to the Factory Method pattern.
One difference between the two is that with the Abstract Factory pattern, a
class delegates the responsibility of object instantiation to another object
via composition whereas the Factory Method pattern uses inheritance and
relies on a subclass to handle the desired object instantiation.

Actually, the delegated object frequently uses factory methods to perform
the instantiation


Applicability
Use the Abstract Factory pattern in any of the following situations:

A system should be independent of how its products are created,
composed, and represented
class can't anticipate the class of objects it must create
A system must use just one of a set of families of products
A family of related product objects is designed to be used together, and you
need to enforce this constrain

http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf

Saturday, November 3, 2012

bridge pattern



  • bridge pattern

The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently
The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes
http://en.wikipedia.org/wiki/Bridge_pattern


  • Usages 


  1. when you want to avoid permanent binding between abstraction and its implementation
  2. when you want to let abstractions and their implementations be extensible by subclassing
  3. changes in the implementation of abstractions should have no impact on clients so that client codes should not be recompiled
  4. mostly used in drivers like database drivers

Factory Pattern


  • Define an interface for creating object.But let subclasses decide which class to instantiate




  • Bir factory ÅŸablonu, kendisine verilen parametre ve deÄŸerlere göre mümkün olan birkaç sınıftan istediÄŸimizi oluÅŸturur ve bize döndürür. Genellikle geriye döndürülen tüm sınıflar ortak bir ebeveyn sınıfından yada arayüzünden oluÅŸturulmuÅŸtur.
http://members.comu.edu.tr/msahin/courses/ust_duzey_files/patterns/factory.pdf