Remember the callback daze

Javascript is everywhere.. it took off to be the most predominant language that applications are developed on both server (node.js)  &  client side (web, mobile, desktop) with plethora of technologies & frameworks.  While coding/debugging nodejs modules, I understand the asynchronous pattern of callbacks functions and how they fit into the event-based execution model of Javascript. But sometimes, the callbackhell is quite overwhelming and I get lost in the daze of callback functions, which ends up in myself strolling through the blogs for a better, clear understanding of the concept. This post is a synthesis of one such instance.

Coming from a Java/C# Eco system, where traditionally functions are defined as per contract/definition and executed sequentially (well…most of the time, other than EventHandlers, callback interfaces, etc.) & every function/method has an object input, output & possible exception handling. Functions in javascript are different as they can be defined traditionally with input arguments & output/return values. They can also be expressed, manipulated passed around like objects. Functions can be also be returned as local variables (closures) and in strict mode, they bleed ‘undefined’ profusely. All good, then what exactly is an callback ?


Callbacks are functions that are executed asynchronously, or at a later time. Instead of the code reading top to bottom procedurally, async programs may execute different functions at different times based on the order and speed that earlier functions like http requests or file system reads happen. (ref:art-of-node#callbacks)


Best explanation I found:
http://recurial.com/programming/understanding-callback-functions-in-javascript
Mike Vollmer did an fantastic job in explaining what are callbacks & how callbacks are used to do things asynchronously in javascript vs. traditional programming languages.

In addition,The Difference Between Call() and Apply() in Javascript would further clarify operations on callback. (ex: callback-apply.js). Another simple explanation: http://www.impressivewebs.com/callback-functions-javascript .

Key points noted are:

  • explicitly making sure callback is a function before use.  (typeof(callback) === ‘function’)
  • callback executes after the asynchronous action begins, but NOT before it completes (Not always) (ex: callback-timing.js).

For well versed javascript coders, it must be pretty basic stuff, but this has been an shift in programming paradigm for me. I do use callbacks and anti-pattern libraries like async.js for node modules & yet sometimes I cannot wrap my head around these callbacks. Modularising code and getting more familiar with asynchronous programming and node.js patterns seems to be solution for the confusion… till then…I need to remember the callback daze..

 

Tagged with: ,
Posted in JavaScript, Node.JS

Blogging: Re-Resurrection

I am not a good blogger, (admitted). but I wanted to stick to the habit of writing/blogging regularly for several reasons, one of the primary being that “Writing makes you smarter“. Nathanmarz nailed it in his blog “You should blog even if you have no readers” . Expressing my ideas and experiences in an coherent way has been an challenge for me and I hope that by blogging regularly would provide me an opportunity to be better at it.

Inspired by some of my peer’s blogs, I started blogging early 2011 and was experimenting it. I could never make it a habit with endless excuses and this blog became dormant.

Its my goal to blog at least once a month and to keep blogging. Joel’s post “5 realizations that helped me write regularly” is one of my key inspirations to keep me focused. Thank you Joel.

Posted in Blogging

To set the Title for a BPEL instance Name in Oracle SOA 11G.

use ora:setCompositeInstanceTitle() in Java_Embedding or in an Assign Statement.

for example :


<assign name="assign_InstanceTitle">
<copy>
<from>ora:setCompositeInstanceTitle(concat("LOGIN:",string($inputVariable.request/ns5:Username))))</from>
<to>$instanceTitle</to>
</copy>
</assign>

puts the instance name as “LOGIN:User1” in the em console

Posted in ORACLE SOA 11G, Oracle SOA BPEL, SOA BPEL 11g

Prevent CSRF (Cross Site Request Forgery) Attacks in SharePoint Application Pages.

Cross-Site Request Forgery (CSRF) is an attack outlined in the OWASP Top 10 whereby a malicious website will send a request to a web application that a user is already authenticated against from a different website.  Much detailed & better explanation is here

To prevent CSRF attacks in SharePoint application pages, which POST’s (http) data to the server, use SharePoint FormDigest Control. This FormDigest Control inserts a generated digest (token) into the form page, when it is requested (usually through GET )& in the code behind we can validate this token using SPUtility.ValidateFormDigest()to make sure that the form/page is not tampered with.  Its a good practice to validate the FormDigest in the code behind, which writes data to the DB/Server/SP list,..ideally any write operation that uses  RunWithElevatedPrivileges . Two steps:

1. Initialize FormDigest control in the custom application page.

 <SharePoint:FormDigest runat="server"/>

2. Check for

SPUtility.ValidateFormDigest()

in the code behind POST methods of application pages to make sure that the form is not tampered with.

Posted in C#, Sharepoint 2010

Deploying referenced project assemblies to GAC through sharepoint WSP

To deploy referenced project assemblies to GAC through SharePoint WSP:

1. Open the Package designer in the SharePoint project.
2. Select the Advanced tab.
3. Click the Add button.
4. Select the Add Assembly from Project Output menu item.
5. Use the Source Project dropdown to select which project in your solution should have its assembly included.
6. Make sure the Deployment Target is set to GlobalAssemblyCache.
7. Click OK, Build & Deploy the Solution, the referenced assemblies should be in GAC.

 

Posted in Sharepoint 2010

Get maxPwdAge for a Domain in AD.

Get Domain level directory entry with username/password, get the property value for ‘maxPwdAge‘ (which would be System.__ComObject)
Convert System.__ComObject to datetime/int.
We have to use COM Interop Libraries for this one, ActiveDs.dll (you can find it on the web)

System.Int64 largeInt=0;
IADsLargeInteger int64Val = (IADsLargeInteger) ent.Properties["maxPwdAge"].Value;
largeInt = int64Val.HighPart * 0x100000000 + int64Val.LowPart;
Console.WriteLine(largeInt);
long ticks = Math.Abs((long)largeInt);
TimeSpan passwordAge = TimeSpan.FromTicks(ticks);
Console.WriteLine("PasswordAge={0}",passwordAge );
Console.WriteLine("PasswordAge in Days={0}", passwordAge.Days);

For more information on converting Large Integer Property Type, please check http://msdn.microsoft.com/en-us/library/ms817837

Posted in Active Directory, C#

“Please close SPWeb objects when you are done with all objects obtained from them, but not before”

DO NOT use SPList directly from methods in webparts & application pages. SPList is a sharepoint object & will instantiate a new SPWeb Object,
if used outside of its parent SPweb object. Always perform operation on SPObjects with in the SPWeb(parent SPObject) context.

or else
We will get the exception with this message : “Please close SPWeb objects when you are done with all objects obtained from them, but not before

Muchos Muchos Gracias to Jonas for the explanation & pattern in this article 

Posted in Sharepoint 2010
%d bloggers like this: