Echo Current Date and Time in Batch File

January 24, 2012

How stupid I am! In one batch file I need to print the current date and time and I googled for 5 mins to find it out. :P So here it is. For the other guys like me. he he.

ECHO Starting stored procedures execution at %date% – %time% >> log_file.log

Happy Coding. :)

Adding Glow to a Shape in AS3

January 12, 2012

Adding a glow to a programatically drawn shape is very easy in actionscript. We just need to create the GlowFilter object and apply it to the filters property. A quick example is follows

var whiteGlow:GlowFilter = new GlowFilter(0xFFFFFF, 0.8, 30, 30, 1, 1, false, false);

var sp:Sprite = new Sprite();
this.addChild(sp);
sp.graphics.drawCircle(100,100,50);
sp.filters = [whiteGlow];

That’s it. Similary you can add other filters such as GradientFilter, BlurFilter, etc. For a detail list of the available display filters refer the livedocs : Available Display Filters.

Happy Coding guys. :)

Drawing Circle Segment using Flex / AS3

January 12, 2012

I was working on an application which required to draw a circle using three different circular segment. First it seemed to be easy, but later found out that I needed to learn by trigonometry books again to achieve this. So here it is drawing a circle segment. You need to know the start point and end point of the arc, the center cordinates and the radius.

public function drawSegment(holder:Sprite,  startAngle:Number, endAngle:Number, segmentRadius:Number, xpos:Number, ypos:Number, step:Number, lineColor:Number, fillColor:Number):void {
     //holder-display object, xpos and ypos-center
     holder.graphics.lineStyle(2, lineColor);
     holder.graphics.beginFill(fillColor);

     //If we need to draw a segment from greater angle to smaller angle, split it at 360 degrees and draw two times
     var originalEnd:Number = -1;
     if(startAngle > endAngle){
          originalEnd = endAngle;
          endAngle = 360;
     }
     var degreesPerRadian:Number = Math.PI / 180;
     var theta:Number;
     startAngle *= degreesPerRadian;
     endAngle *= degreesPerRadian;
     step *= degreesPerRadian;

     // Draw the segment
     holder.graphics.moveTo(xpos, ypos);
     for (theta = startAngle; theta < endAngle; theta += Math.min(step, endAngle – theta)) {
          holder.graphics.lineTo(xpos + segmentRadius * Math.cos(theta), ypos + segmentRadius * Math.sin(theta));
     }
     holder.graphics.lineTo(xpos + segmentRadius * Math.cos(endAngle), ypos + segmentRadius * Math.sin(endAngle));

     if(originalEnd > -1){ // Start angle was greater than end angle and drawing the second segment
          startAngle = 0;
          endAngle = originalEnd * degreesPerRadian;
          for (theta = startAngle; theta < endAngle; theta += Math.min(step, endAngle – theta)) {
               holder.graphics.lineTo(xpos + segmentRadius * Math.cos(theta), ypos + segmentRadius * Math.sin(theta));
          }
          holder.graphics.lineTo(xpos + segmentRadius * Math.cos(endAngle), ypos + segmentRadius * Math.sin(endAngle));
     }
     holder.graphics.lineTo(xpos, ypos);
     holder.graphics.endFill();
     }

From the updateDisplayList method you can call the drawSegment method like this :

drawSegment(this, 270, 30, radius, uw/2, uh/2, 2, 0xEEEEEE, 0x003da8);

The idea is very simple. First draw line from the centre point to the starting point of the arc. Then keep on finding the next point in the circumference through which our arc will be passing. Then draw lines between these points until the end point is reached. After that draw the last line from the end point of the arc to the centre point.

The method considers horizontal point to the right side as angle 0. If we need to draw the line between say, angles 270 and 120, we will split this into two arcs. One from 270 to 360 and second from 0 to 120. We can tune it further to create simpler methods.

That’s it guys. Happy Coding. :-)

Flex : Creating a partial link label using HTML Text

August 29, 2011

Sometimes, we may need to make some part of a label as a link, which will navigate to some url. This can be easily achieved by using HTMLText property of either Label or Text(which is a sub class of Label itself). Below is a sample code.

<?xml version=”1.0″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
borderStyle=”solid”
backgroundGradientColors=”[#FFFFFF, #FFFFFF]“>

<mx:Script>
<![CDATA[
import flash.events.TextEvent;

public function linkHandler(event:TextEvent):void {
myTA.text="The link was clicked.";

// Open the link in a new browser window.
navigateToURL(new URLRequest(event.text), '_blank')
}
]]>
</mx:Script>

<mx:Label selectable=”true” link=”linkHandler(event);”>
<mx:htmlText>
<![CDATA[No reports attatched. You can attach one <a href='event:http://www.adobe.com'><u><font style='color:red;'>here</font></u></a>]]>
</mx:htmlText>
</mx:Label>

<mx:TextArea id=”myTA”/>
</mx:Application>

View Example

Some points for the usage as above :

  • This will work only if the selectable property is set to true. Thanks to the restrictions by the framework :P
  • You can directly call the link by just giving the href property of the anchor tag. In case you need to call a flex method before redirecting to the link, you can follow the approach in my example. For this just append the url of the anchor tag with ‘event:’. This will create an event of type TextEvent, with the url stored in the ‘text’ property of the event.
  • You can right click the link and the flash player will give three optiong : Open, Open in New Window and Copy Link Location. But the point to note here is that, if you are using the url as ‘event:’ as used by me, then all these above said options will give the url starting with ‘event:’ and will break.

That’s it guys. Enjoy and happy coding. :)

Design Patterns by Examples – Decorator Pattern (via Zeeshan Bilal’s Blog)

August 23, 2011

One of the best articles I had seen so far on decorator pattern

Design Patterns by Examples – Decorator Pattern Introduction This series of articles will help you to build a good understanding of design patterns using different examples from real life and some from well-known frameworks or APIs. There are many articles around the web that discuss design patterns but they sometimes lack appropriate examples to quote. So, either their purpose stays unclear or we cannot memorize the patterns longer and sooner they slip out of mind. More importantly, we unders … Read More

via Zeeshan Bilal’s Blog

Debug using Apache Tomcat and Eclipse

May 16, 2011

It is always useful to debug the code in a line by line fashion to track where the code went wrong. Since I am using Apache Tomcat server for development purpose this article deals with enabling debugging on Apache Tomcat with Eclipe IDE.

First we need to set up the Tomcat server to enable debugging. Usually the debugging port used for tomcat is 8000, but you can set to any free port.

1. Open the startup.bat file from Tomcat directory (<Tomcat Installation Directory>/bin/)

2. After the script initializes the command line arguements, most probably towards end of file, add these two lines :

set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket

3. Change the line where it calls the EXECUTABLES to :

call “%EXECUTABLE%” jpda start %CMD_LINE_ARGS% (add jdpa start).

Save the file and start the tomcat. Now the first line of the tomcat startup console will show :

Listening for transport dt_socket at address: 8000

At this point your Tomcat server is ready to be run in debug mode. Now do the following steps at Eclipse IDE for debugging in Eclipse.

1. Open one class where you want to put breakpoint and add one breakpoint. (This can be done by clicking on the left margin on the corresponding line.

2. From Run menu, select Debug Configurations

3. From the left side, right click on Remote Java Application and click new

4. Fill out the form appearing. Only thing to take care is the server name should be localhost and port number 8000. This can be changed if you are debugging on a remote server.

5. Click on debug and now the eclipse is running in debug mode.

Now you can run the application and whenever the thread reaches the line, where you have put the breakpoint, it will pause execution. Then you can see the current values of the variables, you can step by step execute the line and so on.

That’s it guys. Happy Coding :)

Pausing Execution of Java Program

March 31, 2011

Sometimes, we may need to wait for in between the execution of our java program. Thread.sleep() is the best way to do so. A sample code taken directly from Oracle Site :

public static void main(String args[]) throws InterruptedException {
String importantInfo[] = {
“Mares eat oats”,
“Does eat oats”,
“Little lambs eat ivy”,
“A kid will eat ivy too”
};

for (int i = 0; i < importantInfo.length; i++) {
Thread.sleep(4000);
System.out.println(importantInfo[i]);
}
}

Happy Coding ;)

 

Java versions of Domino (Lotus Notes Server)

March 17, 2011

Java was integrated into Lotus Notes beginning with Release 4.5. With Release 5, Java support was greatly enhanced and expanded, and JavaScript was added. The latest release of Lotus Notes 8.5.2 supports java 1.6. The available java versions for the different Domino Server Versions are :

1.0 to 3.x : Not Available
4.5 to 4.6.7a : JRE/JDK 1.1
5.0 to 5.0.13a : JRE/JDK 1.1.8
6.0 to 6.5.6 : JRE/JDK 1.3.1
7.0 to 7.0.3 : JRE/JDK 1.4.2
8.0 to 8.0.1 : JRE/JDK 1.5.0
8.5 to 8.5.2 : JRE/JDK 1.6.0

For more details see here :: Supported versions of Java with IBM Lotus Products

Happy Coding :)

Get the version of Java runtime

March 16, 2011

There may be occasions when you will need to do different processing for different servers based on the java versions present. You can simple use the System.getProperty(“java.version”) to get the current java version of the runtime.

public class Test {
public static void main(String[] args){
System.out.println(System.getProperty(“java.version”));
}
}

If you want to get the major minor version of Java you can use :

System.out.println(System.getProperty(“java.class.version”));

Similarly there are a set of system properties which can come handy at many times. To get a complete list of all the java properties, run the following program ::

import java.util.Enumeration;
import java.util.Properties;

public class Test {

public static void main(String[] args){
Properties p = System.getProperties();
Enumeration e = p.propertyNames();
while(e.hasMoreElements()){
String pro = (String) e.nextElement();
System.out.println(pro + ” :: ” + System.getProperty(pro));
}
}
}

Happy Coding :)

Browser cache issue with Flex HTTPService

January 25, 2011

I struggled to figure this one out, whcih was causing lots of issues in my application. I was using one httpservice to call one URL which will return one xml. This xml in turn forms the dataprovider for one datagrid. The xml is generated based on some selections, but after loading for the first time, after the selection changes, the httpservice was not returning the new xml, in turn it provides the same XML. Hence the datagrid was not getting refreshed. From first view itself I guessed the problem is with caching, since it works well each time I clear the IE cache. Finally the solution is here :

Problem: Repeated HTTPService calls when made from Flex many a times ends up with no external HTTP call. It appears the data is served from cache.

Reason: The Flash Player piggybacks on the browser to make the HTTP call. IE caches the response from the HTTP GET calls and on occurrence of the same URL returns the response from the cache.

Solution: The problem can be solved either at the server side or at the client side.

Server side solution: Set the HTTP headers of the response to avoid returning response from cache.

In HTML: (in the header)

<META HTTP-EQUIV=”Cache-Control” CONTENT=”no-cache”>
<META HTTP-EQUIV=”expires” CONTENT=”0″>

In JSP: (before writing to the output stream)response.setHeader(“Cache-Control”,”no-cache”);
response.setDateHeader (“Expires”, 0);

Client side solution: (1) Make HTTP POST call — only HTTP GET calls are served from cache or (2) Make sure the HTTP GET URL is different every time.

(1) Make HTTP POST call – set method=”POST”

(2) Append a unique parameter to the HTTP GET call so that the URL is different every time. A unique time stamp is a good choice.
The following sample code, may do the job:

var params:Object = {};
var noCache:Date = new Date() ;
params.noCache = noCache.getTime().toString() ;
httpService.send(params)

I have named the parameter “noCache”. You can name it anything else you please. The name does not matter. What matters is that the timestamp makes the HTTP GET URL unique.

That’s it guyss.. Enjoy and Happy Coding.. Cheers.


Follow

Get every new post delivered to your Inbox.

Join 78 other followers