Optimize your Ruby code

This may seem obvious, but a Rails application is essentially ruby code that will have to be run. Make sure your code is efficient from a Ruby standpoint. Take a look at your code and ask yourself if some refactoring is in order, keeping in mind performance considerations and algorithmic efficiency. Profiling tools are, of course, very helpful in identifying slow code, but the following are some general considerations (some of them may appear admittedly obvious to you):

  1. When available use the built-in classes and methods, rather than rolling your own;
  2. Use Regular Expressions rather than costly loops, when you need to parse and process all but the smallest text;
  3. Use Libxml rather than the slower REXML if you are processing XML documents;
  4. Sometimes you may want to trade off just a bit of elegance and abstraction for speed (e.g. define_method and yield can be costly);
  5. The best way to resolve slow loops, is to remove them if possible. Not always, but in a few cases you can avoid loops by restructuring your code;
  6. Simplify and reduce nested if/unless as much as you can and remember that the operator ||= is your friend;
  7. Hashes are expensive data structures. Consider storing the value for a given key in a local variable if you need to recall the value a few times. More in general, it’s a good idea to store in a variable (local, instance or class variable) any frequently accessed data structure.

Permanent link to this article: https://blog.openshell.in/2010/11/optimize-your-ruby-code/

Group operations in a transaction

ActiveRecord wraps the creation or update of a record in a single transaction. Multiple inserts will then generate many transactions (one for each insert). Grouping multiple inserts in one single transaction will speed things up.

Insead of:

 my_collection.each do |q|
   Report.create({:phrase => q})
 end

Use:

Report.transaction do
 my_collection.each do |q|
   Report.create({:phrase => q})
 end
end

or for rolling back the whole transaction if any insert fails, use:

Report.transaction do
 my_collection.each do |q|
   report = Report.new({:phrase => q})
   report.save!
 end
end

Permanent link to this article: https://blog.openshell.in/2010/11/group-operations-in-a-transaction/

IBM DB2 Security Mechanisms

For every e-business solution, the company should give more sound for database security. The database security plan should define:

  • who is allowed to the instance and/or database
  • where and how a user’s password will be verified
  • authority level that a user is granted
  • commands that a user is allowed to execute
  • data that a user is allowed to read and/or alter
  • database objects a user is allowed to create, alter, and/or drop

DB2 Security Mechanisms

There are 3 main mechanisms within DB2 that allow a DBA to implement a database security plan:

  1. Authentication
  2. Authorization
  3. Privileges

1. Authentication

Authentication is the first security feature you’ll encounter when you attempt to access a DB2 instance or database. It works closely with the security features of the underlying operating system to verity the user IDs and passwords. It can also work with security protocols like kerberos to authenticate users.

2. Authorization

It involves determining the operations that user and/or groups can perform, and the data objects that they may access. An user’s ability to perform high level database and instance management is determined by the authorities that they have been assigned. The 5 different authority levels within DB2 are

  1. SYSADM
  2. SYSCTRL
  3. SYSMAINT
  4. DBADM
  5. LOAD

3. Privileges

A bit more granular than authorities, and can be assigned to users and/or groups. It defines the commands that a user can use to access objects like tables, views, index and packages.

Permanent link to this article: https://blog.openshell.in/2010/11/ibm-db2-security-mechanisms/

Locate ASP.Net AJAX AutoCompleteExtender inside TabContainer

ASP.Net Ajax contains rich array of controls used to create an interactive Web experience.While creating a ajax website you might face problems in binding two controls.For example locating AutoCompleteExtender inside the tab Container.Let us see how to solve this above problem.

1.   First, create the a script manager in your .aspx page.like this,

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
</asp:ScriptManager>

As a server-side control, ScriptManager reacts to events in the ASP.NET page life cycle and uses those events to coordinate the activities of all the controls, options, and code employed by ASP.NET AJAX.

2.   Create the Tab Container,inside it create one or more Tab Panel like

<cc1:TabContainer ID=”Container1″ runat=”server”  ActiveTabIndex=”1″ >

<cc1:TabPanel ID=”tpanel1″ runat=”server” HeaderText=”Sample Tab”>

<ContentTemplate>

//place other controls here(Eg:labels,buttons,textboxes,etc..)</ContentTemplate>

</cc1:TabPanel>

</cc1:TabContainer>

3.  Here AutoCompleteExtender is also acting as a controls of tpanel1,So your will be look like this

<cc1:TabContainer ID=”Container1″ runat=”server”  ActiveTabIndex=”1″ >

<cc1:TabPanel ID=”tpanel1″ runat=”server” HeaderText=”Sample Tab”>

<ContentTemplate>

//place other controls here(Eg:labels,buttons,textboxes,etc..)  including the textbox(txt_sample)

//here is the code for autocompleteextender

<cc1:AutoCompleteExtender ID=”AutoCompleteExtender1″  runat=”server” ServiceMethod=”GetCompletionList1″
DelimiterCharacters=”” Enabled=”True” ServicePath=”” CompletionInterval =”500″ MinimumPrefixLength=”1″
UseContextKey=”True”  TargetControlID=”txt_sample” >
</cc1:AutoCompleteExtender>

</ContentTemplate>

</cc1:TabPanel>

</cc1:TabContainer>

4. Create a Service Method in .cs page for working with autocomplete textbox.

Have a  Happy Coding...

Permanent link to this article: https://blog.openshell.in/2010/11/locate-asp-net-ajax-autocompleteextender-inside-tabcontainer/

Configuring JNDI data source in Tomcat

By using Java Naming and Directory Interface (JNDI) service, one can connect resources of different technologies.

A typical web application in Tomcat has a file called as context.xml in which the context path (root) of the web application is defined. Usually this file is located in META-INF directory of you web application (see figure 1). You can configure a JNDI data source in Tomcat by adding a declaration for your resource to this file. This is the application’s context container, which enables you to specify application meta-data necessary for the server in order to deploy and run the application. There are various locations where you can specify context elements, such as your server’s global i.e. tomcat-install-dir/conf/context.xml

context.xml

Figure 1: META-INF/context.xml

Add the following Resource tag as a declaration for the JNDI resource. The code below is an example of how you can declare JNDI resource for MySQL database.

<?xml version="1.0"?>
<Context path="/abc">
<Resource name="jdbc/jndi-name"
auth="container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="yourusername"
password="yourpassword"
driverClass="com.mysql.jdbc.Driver"
url="jdbc:mysql://hostname:3306/databasename" />
</Context>

Permanent link to this article: https://blog.openshell.in/2010/11/configuring-jndi-data-source-in-tomcat/

Prototyping Visualization

if you want to design a website or user interface for a website, one of the fundamental things to do is to make sure you have the interface designed well before you start building. The most general way to plan an interface are to use Wireframes.

Wireframes are line drawings, comparable to blueprints, which demonstrates the general navigation and the building blocks e.g. content, functionality etc. that will go into making of the website. The main rationale behind creating Wireframes is to sustain the flow of your particular logical and business functions by classifying all the entrance and exit points for every single page of your website.

Wireframes can be created manually on a paper. It is the easiest and fastest way of creating Wireframes. When time is short just put your layout ideas on a paper with a focus on what elements should go where.

Wireframes are crucial component of the initial development stage as they generate user expectations and help to boost the understanding and flow of a website by increasing acquaintance with the site.

During the creation of the project, Wireframes function as a firm and established base on which to consider changes and new requirements. Common page structure and content needs for individual web pages are easily conveyed using Wireframes.

Wireframes mostly lead to a better output in terms of superior product because they are also immensely helpful in getting valuable and accurate input.

Wireframes/Prototyping makes it easy to compare and evaluate different ideas and rival designs, which further makes it easier to incorporate changes.

Development cost and time are ultimately cut down by sizeable amount by getting a signoff on a detailed Wireframes because programmers making never-ending changes to their code have become a passe.

Some people prefer Visio, Adobe Illustrator, Photoshop, Freehand, Fireworks, and of course even PowerPoint, Dreamweaver, Adobe Indesign etc.

But the following are some of the other tools, which are less popular but have good stuff.

Dia
Website Wireframes Tools
Oversite
Gliffy
Protoshare
Jumpchart
Pencil
Denim

Permanent link to this article: https://blog.openshell.in/2010/11/prototyping-visualization/

Find out IP address

We can get the IP address of any visitor by using PHP. Finding the IP address is very important requirement for many scripts where we store the members or visitors details. For security reason we can store IP address of our visitors who are doing any purchases or recording the geographical location of the visitor can be done this way. Some time based on the IP address we can redirect the browser to different areas of the site. There are many applications using this and here is the PHP code to know the IP address of any visitor to the site.

<?php
     //Get IP Address
     $ip = $_SERVER['REMOTE_ADDR'];
     echo "<b>IP Address= $ip</b>";
?>

Permanent link to this article: https://blog.openshell.in/2010/11/find-out-ip-address/

Send HTML Mail With PHP

Sending html email in php is extremely easy. All you have to do is call the “mail” function with some extra header.

Have a look on example:

<?php
 //define the receiver of the email
 $to = 'youraddress@example.com';

 //define the subject of the email
 $subject = 'Test HTML email';

 //create a boundary string. It must be unique
 //so we use the MD5 algorithm to generate a random hash
 $random_hash = md5(date('r', time()));
 //define the headers we want passed. Note that they are separated with rn
 $headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";

 //add boundary string and mime type specification
 $headers .= "rnContent-Type: multipart/alternative; boundary="PHP-alt-".$random_hash.""";

 //define the body of the message.
 ob_start(); //Turn on output buffering
 Content-Type: text/html; charset="iso-8859-1"
 Content-Transfer-Encoding: 7bit

 <h2>Hello World!</h2>
 <p>This is something with <b>HTML</b> formatting.</p> 

 echo $random_hash;

 //copy current buffer contents into $message variable and delete current output buffer
 $message = ob_get_clean();

 //send the email
 $mail_sent = @mail( $to, $subject, $message, $headers );

 //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
 echo $mail_sent ? "Mail sent" : "Mail failed";
 ?>

Permanent link to this article: https://blog.openshell.in/2010/11/send-html-mail-with-php/

Single quotes, Double quotes

It’s easy to just use double quotes when concatenating strings because it parses everything neatly without having to deal with escaping characters and using dot values. However, using single quotes has considerable performance gains, as it requires less processing.

For Example:

<?php
//Consider this string
$howdy = 'everyone';
$foo = 'hello $howdy';
$bar = "hello $howdy";

// Concatenating this strings
$howdy = 'everyone';
$foo = 'hello $howdy';
$bar = "hello $howdy";
?>

$foo outputs to “hello $howdy” and $bar gives us “hello everyone”. That’s one less step that PHP has to process. It’s a small change that can make significant gains in the performance of the code.

Permanent link to this article: https://blog.openshell.in/2010/11/single-quotes-double-quotes/

Scripts Execution Time Limits

Putting a time limit on your PHP scripts is a very critical thing. There are times when your scripts will fail, and when they do, you’ll want to use the set_time_limit function to avoid infinite loops and database connection timeouts. The set_time_limit puts a time limit on the maximum number of seconds a script will run (the default is 30). After that time period, a fatal error is thrown.

Permanent link to this article: https://blog.openshell.in/2010/11/scripts-execution-time-limits/