Archive for ◊ June, 2010 ◊

Author:
Thursday, June 03rd, 2010

Many guys think that Javascript is a client side scripting language, its just used for client side form validations. But this proved false once AJAX come into picture. We will discuss some questions on this.

Is Javascript Object Oriented scripting language?

Many people will say no. But it is Object oriented. Javascript is a prototype-based object-oriented  scripting language.

Now, another question arises, what is that “prototype-based”?

Wikipedia defines Prototype-based programming as a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based
languages) is performed via a process of cloning existing objects that serve as prototypes.

Anoter question arises “classes are not present”, then how will I create class and object????

In Javascript, your function = Class. hmmm, lot of confusions right?

I think it will be easy if we compare a programming language to understand it better. We will take Java as our reference language.

What is class?
A class is a construct that is used as a blueprint  (or template) to create objects  of that class.

What is Object?
An object is usually taken to mean a compilation of attributes (object elements) and behaviors (methods or subroutines) encapsulating  an entity.

We will write a simple Class in Java

public class Theatre{

        String theatreName;
        int noOfSeats = 80;

        public int getNoOfSeats() {
                return noOfSeats;
                }

        public String getTheatreName() {
                return theatreName;
                }

        public void setTheatreName(String newTheatre) {
                this.theatreName = newTheatre;
                }
}

Equivalent Javascript code

//Class Theatre

function Theatre(){
	this.theatreName='PVR Cinemas';
	this.noOfSeats = 80;

	this.getNoOfSeats = function() {
		return this.noOfSeats;
		}

	this.settheatreName = function(newTheatreName) {
		this.theatreName = newTheatreName;
		}

	this.gettheatreName = function(){
		return this.theatreName;
	}
}

// Create an instance of the Theatre Class
var theatre = new Theatre();
alert(theatre.gettheatreName());
theatre.settheatreName('Hitman Theatre');
alert(theatre.gettheatreName());
alert(theatre.getNoOfSeats());

I think after comparing these two code samples, you guys were clear with the concept of class in Javascript.

How to use NameSpace in Javascript?

In Java language, package names are used to prevent naming collisions and also for organizing Java classes similar to the modules of Modula. In javascript this can be achieved by object hierarchies. We will see with an example

//create the base Package object if it does not exist.
if (!Package) {
var Package = new Object();
}
// define Person under Package
Package.Person = function () {
	this.name='Albert';
	this.age = 27;
	this.changeHisAge = function(age) {
		this.age = age;
	}
}

//create an instance of the Person
var person = new Package.Person();
person.changeHisAge(28);
alert(person.name + ", age : " + person.age);

How to do Inheritance?

Here comes the keyword called “prototype”. Prototype is a type of inheritance in JavaScript. Its used to add a method to a class. Take the previous example. If we want to add a method “changeHisName”, then code follows below

Package.Person.prototype.changeHisName = function(name) {
	this.name = name;
}

How to handle Visibility or Access level of my variables and methods?

Public properties         this.variableName
Private Properties        var variableName

Public Method              ClassName.prototype.func_name = function() {}
Private Method             var func_name = function() {}
Privilige Method           this.func_name = function() {}

How to handle Singleton pattern in Javascript?

The singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. Its very simple, hold the current object in a variable, if it exists return the same object or create a new one. Implementation is as follows:

SingletonClass = function(name){
	if(SingletonClass.object !== undefined)
		return SingletonClass.object;
	this.name = name;
	SingletonClass.object = this;
} 

var firstInstance = new SingletonClass('iSense');
alert(firstInstance.name);
//Alerts "iSense"
var secondInstance = new SingletonClass('Prabhu');
alert(secondInstance.name);
//Alerts alerts "iSense"

You can download the code used to explain.

Uses of Object Oriented javascript:
1. Reusability of the code increases
2. Dynamic loading of objects
3. Objects are organized better

Dude, start using object oriented javascript, Enjoy Coding :)

Author:
Tuesday, June 01st, 2010
java.util.Concurrent was added to the Java platform in version 1.5 .
This package contains an Executor Framework, which is a flexible
interface-based task execution facility. Creating a work queue:
ExecutorService executor = Executors.newSingleThreadExecutor();
Here is how to submit a runnable for execution:
executor.execute(runnable);
And here is how to tell the executor to terminate gracefully (if you fail to do this,
it is likely that your VM will not exit):
executor.shutdown();
You can do many more things with an executor service. For example, you can
wait for a particular task to, you can retrieve the results of tasks one by one as
they complete (using an ExecutorCompletionService), you can wait for the executor
service’s graceful termination to complete (using the awaitTermination
method) , you can wait for any or all of a collection of tasks to complete
(using the invokeAny or invokeAll methods) and so on.
If you want more than one thread to process requests from the queue, simply
call a different static factory that creates a different kind of executor service called
a thread pool. You can create a it with a fixed or variable number of threads.
The java.util.concurrent.Executors class contains static factories that provide
most of the executors you’ll ever need. If, however, you want some thing out of the ordinary,
you can use the ThreadPoolExecutor class directly.
This ThreadPoolExecutor  class lets you control nearly every aspect of a thread pool’s operation.
Choosing the executor service for a particular application can be tricky. If
you’re writing a small program, or a lightly loaded server, using Executors.new-
CachedThreadPool is generally a good choice, as it demands no configuration
and generally €œdoes the right thing.€ But a cached thread pool is not a good choice
for a heavily loaded production server! In a cached thread pool, submitted tasks
are not queued but immediately handed off to a thread for execution. If no threads
are available, a new one is created. If a server is so heavily loaded that all of its
CPUs are fully utilized, and more tasks arrive, more threads will be created, which
will only make matters worse. Therefore, in a heavily loaded production server,
you are much better off using Executors.newFixedThreadPool, which gives you
a pool with a fixed number of threads, or using the ThreadPoolExecutor class
directly, for maximum control.
Not only should you refrain from writing your own work queues, but you
should generally refrain from working directly with threads. The key abstraction
is no longer Thread, which served as both the unit of work and the mechanism for
executing it. Now the unit of work and mechanism are separate. The key abstraction
is the unit of work, which is called a task. There are two kinds of tasks: Runnable
and its close cousin, Callable (which is like Runnable, except that it
returns a value). The general mechanism for executing tasks is the executor service.
If you think in terms of tasks and let an executor service execute them for
you, you gain great flexibility in terms of selecting appropriate execution policies.
In essence, the Executor Framework does for execution what the Collections
Framework did for aggregation.
ScheduledThreadPoolExecutor is  an Executor Framework  which is easier to use a timer which
is more flexible than the java.util.Timer. A timer uses only a single
thread for task execution, which can hurt timing accuracy in the presence of long running
tasks. If a timer’s sole thread throws an uncaught exception, the timer
ceases to operate. A scheduled thread pool executor supports multiple threads and
recovers gracefully from tasks that throw unchecked exceptions

java.util.Concurrent was added to the Java platform in version 1.5 . This package contains an Executor Framework, which is a flexible interface-based task execution facility. Creating a work queue:

ExecutorService executor = Executors.newSingleThreadExecutor();

Here is how to submit a runnable for execution:

executor.execute(runnable);

And here is how to tell the executor to terminate gracefully (if you fail to do this, it is likely that your VM will not exit):

executor.shutdown();

You can do many more things with an executor service. For example, you can wait for a particular task to, you can retrieve the results of tasks one by one as they complete (using an ExecutorCompletionService), you can wait for the executor service’s graceful termination to complete (using the awaitTermination method) , you can wait for any or all of a collection of tasks to complete (using the invokeAny or invokeAll methods) and so on.

If you want more than one thread to process requests from the queue, simply call a different static factory that creates a different kind of executor service called a thread pool. You can create a it with a fixed or variable number of threads. The java.util.concurrent.Executors class contains static factories that provide most of the executors you’ll ever need. If, however, you want some thing out of the ordinary, you can use the ThreadPoolExecutor class directly.This ThreadPoolExecutor  class lets you control nearly every aspect of a thread pool’s operation.

Choosing the executor service for a particular application can be tricky. If you’re writing a small program, or a lightly loaded server, using Executors.new-CachedThreadPool is generally a good choice, as it demands no configuration and generally €œdoes the right thing.€ But a cached thread pool is not a good choice for a heavily loaded production server! In a cached thread pool, submitted tasks are not queued but immediately handed off to a thread for execution. If no threads are available, a new one is created. If a server is so heavily loaded that all of its CPUs are fully utilized, and more tasks arrive, more threads will be created, which will only make matters worse. Therefore, in a heavily loaded production server, you are much better off using Executors.newFixedThreadPool, which gives you a pool with a fixed number of threads, or using the ThreadPoolExecutor class directly, for maximum control.

Not only should you refrain from writing your own work queues, but you should generally refrain from working directly with threads. The key abstraction is no longer Thread, which served as both the unit of work and the mechanism for executing it. Now the unit of work and mechanism are separate. The key abstraction is the unit of work, which is called a task. There are two kinds of tasks: Runnable and its close cousin, Callable (which is like Runnable, except that it returns a value). The general mechanism for executing tasks is the executor service. If you think in terms of tasks and let an executor service execute them for you, you gain great flexibility in terms of selecting appropriate execution policies. In essence, the Executor Framework does for execution what the Collections Framework did for aggregation.

ScheduledThreadPoolExecutor is  an Executor Framework  which is easier to use a timer which is more flexible than the java.util.Timer. A timer uses only a single thread for task execution, which can hurt timing accuracy in the presence of long running tasks. If a timer’s sole thread throws an uncaught exception, the timer ceases to operate. A scheduled thread pool executor supports multiple threads and recovers gracefully from tasks that throw unchecked exceptions

Category: Java  | One Comment
Author:
Tuesday, June 01st, 2010

Silverlight 4 come with an assortment of new features for development and particularly RIA service greatly simplifies some cumbersome tasks while developing silverlight 4 application.

The idea behind this demo is to get an understanding of what happens behind the scene while developing silverlight 4 application with RIA services. And to start with we will explore into the data paging capability of RIA service.

Note: The demo is split in two parts, the first part shows a simple way of getting started by dragging and dropping objects from Data Sources and the second part compares data paging while binding data dynamically using PagedCollectionView and DomainDataSource.


Part 1



Part 2

Category: .Net, Silverlight  | One Comment
Author:
Tuesday, June 01st, 2010

To run a test script from any intermediate step, use the €œRun from Step€ option to run a selected part of your test. This enables you to check a specific section of your AUT or to verify whether a certain part of the test script runs smoothly or not.

In the Expert View, you can use the €œRun from Step€ option to run your test from the selected step until the end of the action. If the action contains nested actions, QTP runs the nested actions for the defined number of iterations of the nested action. Also keep in mind that using Run from Step in this mode ignores any iteration.

In the Keyword View, you can use the €œRun from Step€ option to run your test from the selected step until the end of the test. Using €œRun from Step€ in this mode includes all iterations. The first iteration will run from the step you selected until the end of the test; all other remaining iterations will run from the beginning of the test.

If you only want to run single iteration of your test, select €œRun one iteration only€ from the Run pane in the Test Settings dialog box.

You can insert a breakpoint if you wish to run your test until a specific point within the test script (and not to the end of the action or test). The test will then run from the selected step or action until the breakpoint.

To run an entire action, or run a test or action from a selected step:

  1. Make sure AUT is in a state matching the action or step you want to run.
  2. Select the action or step where you want to start running the test. Make sure that the step or action you choose is not dependent on previous steps, such as a retrieved value or a parameter defined in a previous step.
  3. Select Automation > Run from Step or Run Current Action, or right-click and select Run from Step. The Run dialog box opens.
  4. In the Run dialog box, choose where to save the run session results, and define any input parameters you want to use.
  5. Click OK. The Run dialog box closes and the run session starts
Author:
Tuesday, June 01st, 2010

I had been a SharePoint developer for some days before shifting focus on to Silverlight and WCF.

But recently, in my new organization I took up the initiative for introducing SharePoint, thereby donning all the hats, but beginning with Administrator.

One of the primary questions was “What are the licensing models for SharePoint 2010?

To my bewilderment, there weren’t many straight answers to this question. Below I am just trying to make a mere mortal attempt to unravel mysteries surrounding SharePoint 2010 licensing.

To begin with there are two modes of hosting any application on SharePoint

  • On-Premise – We are hosting the SharePoint server within the organization premises.
  • SharePoint Online – Using cloud technologies provided by Microsoft to host the application.

On-Premise Hosting
To start off, SharePoint 2010 maintains the similar model to that of 2007 i.e. It has mainly 3 variations 

Well before actually getting to licensing models, MS uses the term Client Access Licenses (CAL) quite freely around these topics. More information on CAL can be found @ http://www.microsoft.com/licensing/about-licensing/client-access-license.aspx.

Now to the actual licensing modes, each version i.e. the “Standard” and “Enterprise” versions comes with 2 flavors of licensing model viz. “Intranet” and “Internet”

As the name suggests each licensing model is targeted to the specific usage of the platform.
 


As illustrated above, Enterprise editions can be upgraded with FAST (http://www.microsoft.com/enterprisesearch/en/us/fast.aspx) search licenses.

All said, the single eternal question that remains are :

“How much damn do I need to spend for all these !!!!!!!”

Well the simple answer for that would be – “It depends” :)

To elaborate on that, it depends

  • Whether you are using SharePoint for intranet or Internet or for both?
  • Do you require Enterprise/Standard/Foundation edition?
  • What kind of deployment?

The next question(atleast I had this) was isn’t SharePoint foundation completely free? So can I just use it?

SharePoint Foundation 2010 is itself a free product that requires no CALs. The caveat is, SharePoint foundation itself needs the following -

  • One or more servers which need to be correctly paid for and licensed

    = (Cost for the server) + (Server CALs for the users accessing the server ) + (Internet Connection License if server is connected to Internet). 

  • A database system to store its databases.
    • If “Basic” installation is used, then there will be single server installation which uses SQL 2008 express. SQL 2008 Express is completely free of any CAL’s.
    • If “Advanced” installation is used, then SharePoint 2010 foundation uses either SQL server 2005/2008, which in-turn needs to be licensed.

** In both the Server and the SQL Server cases, there are (“enterprise”) licensing options available that mean that no CALs are required .

Well that is pretty much it for On-Premise hosting of SharePoint, shall continue with cloud based options in the coming post.

Author:
Tuesday, June 01st, 2010

To implement a Custom EventArgs we need to follow these steps:

  1. Create your custom event argument class and derive it from the EventArgs class. The eventArgs class contains no data and is used with events that do not need to pass event state.
  2. Give your class a meaningful name ending in EventArgs, e.g.: MailReceivedEventArgs.
  3. Implement additional data members and properties to support event state that you need to pass to event handlers. It’s best to make state immutable, so we should use private read-only data members and use public properties to provide read-only access to data members.
  4. Implement a public constructor that allows you to set the initial configuration of the event state.
  5. Make your argument class as sealed if you do not intend other event argument classes to extend it.
  6. Make your event argument class serializable so that the runtime can marshal instances of it across application domain and machine boundaries.

 

Consider the below event argument class named MailReceivedEventArgs

Using system;

namespace DemoClass

{

[Serializable]

public sealed class MailReceivedEventArgs: EventArgs

{

  //private readonly members that hold the event state that is to be

 // distributed to all event handlers. The MailReceviedEventArgs class will specify

// who sent the received mail and what the subject is.

    private readonly string from;

    private readonly string subject;

 

   // Constructor

   public MailReceivedEventArgs(string from, string subject)

{

    this.form = form;

   this.subject = subject;

}

 

//Readonly properties to provide access to event state

public string From{ get {return from;}}

public string Subject { get {return subject;}}

 

//Class which uses the MailReceivedEventArgs

public class Test

{

  public static void Main()

{

 MailReceivedEventArgs args = new MailReceivedEventArgs(€œJyothi€, €œDocumet€);

 Console.WriteLine(€œFrom: {0}, Subject: {1}€, args.From, args.Subject);

Console.ReadLine();

}

}

}

 

}

Category: .Net  | Leave a Comment