Home Navigation

Wednesday 26 October 2016

Asymptotic notation

The running time of an algorithm uses a combination of two ideas.
  • First, we need to determine how long the algorithm takes, in terms of the size of its input. 
  • The second idea is that we must focus on how fast a function grows with the input size. We call this the rate of growth of the running time. 

Here's a chart showing values of 6n^2 and 100n + 300 for values of n from 0 to 100:

We'll see three forms of asymptotic notation: big-Theta Θ notation, big-O notation and big-Omega Ώ notation.

Big-Θ (Big-Theta) notation: When we say that a particular running time is Θ(n), we're saying that once n gets large enough, the running time is at least k1.n and at most k2.n for some constants k1 and k2. Here's how to think of Θ(n):

When we use big-Θ notation, we're saying that we have an asymptotically tight bound on the running time. "Asymptotically" because it matters for only large values of n. "Tight bound" because consider the running time to within a constant factor above and below.

Here's a list of functions in asymptotic notation that we often encounter, listed from fastest to slowest in performance.
  • Θ(1) 
  • Θ(lgn) 
  • Θ(n) 
  • Θ(nlgn) 
  • Θ(n^2) 
  • Θ(n^2lgn) 
  • Θ(n^3) 
  • Θ(2^n)
Note that an exponential function a^n, where a>1, grows faster than any polynomial function n^b, where b is any constant.

Big-O notation: We use big-Θ notation to asymptotically bound the growth of a running time to within constant factors above and below. Sometimes we want to bound from only above. For example, although the worst-case running time of binary search is Θ(lgn), it would be incorrect to say that binary search runs in Θ(lgn) time in all cases. What if we find the target value upon the first guess? Then it runs in Θ(1) time. The running time of binary search is never worse than Θ(lgn), but it's sometimes better. It would be convenient to have a form of asymptotic notation that means "the running time grows at most this much, but it could grow more slowly." We use "big-O" notation for just such occasions.

We say that the running time is "big-O of f(n)" or just "O of f(n)." We use big-O notation for asymptotic upper bounds, since it bounds the growth of the running time from above for large enough input sizes.



Now we have a way to characterize the running time of binary search in all cases. We can say that the running time of binary search is always O(lgn). We can make a stronger statement about the worst-case running time: it's Θ(lgn). But for a blanket statement that covers all cases, the strongest statement we can make is that binary search runs in O(lgn) time.

Big-Ω(Big-Omega) notation:  Sometimes, we want to say that an algorithm takes at least a certain amount of time, without providing an upper bound. We use big-Ω notation; that's the Greek letter "omega."

If a running time is Ω(f(n)), then for large enough nn, the running time is at least k.f(n) for some constant k. Here's how to think of a running time that is Ω(f(n)):

We say that the running time is "big-Ωf(n)." We use big-Ω notation for asymptotic lower bounds, since it bounds the growth of the running time from below for large enough input sizes.

:Common Data Structure Operations:

Data Structure Time Complexity Space Complexity
Average Worst Worst
Access Search Insertion Deletion Access Search Insertion Deletion
Array Θ(1) Θ(n) Θ(n) Θ(n) O(1) O(n) O(n) O(n) O(n)
Stack Θ(n) Θ(n) Θ(1) Θ(1) O(n) O(n) O(1) O(1) O(n)
Queue Θ(n) Θ(n) Θ(1) Θ(1) O(n) O(n) O(1) O(1) O(n)
Singly-Linked List Θ(n) Θ(n) Θ(1) Θ(1) O(n) O(n) O(1) O(1) O(n)
Doubly-Linked List Θ(n) Θ(n) Θ(1) Θ(1) O(n) O(n) O(1) O(1) O(n)
Skip List Θ(log(n)) Θ(log(n)) Θ(log(n)) Θ(log(n)) O(n) O(n) O(n) O(n) O(n log(n))
Hash Table N/A Θ(1) Θ(1) Θ(1) N/A O(n) O(n) O(n) O(n)
Binary Search Tree Θ(log(n)) Θ(log(n)) Θ(log(n)) Θ(log(n)) O(n) O(n) O(n) O(n) O(n)
Cartesian Tree N/A Θ(log(n)) Θ(log(n)) Θ(log(n)) N/A O(n) O(n) O(n) O(n)
B-Tree Θ(log(n)) Θ(log(n)) Θ(log(n)) Θ(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Red-Black Tree Θ(log(n)) Θ(log(n)) Θ(log(n)) Θ(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Splay Tree N/A Θ(log(n)) Θ(log(n)) Θ(log(n)) N/A O(log(n)) O(log(n)) O(log(n)) O(n)
AVL Tree Θ(log(n)) Θ(log(n)) Θ(log(n)) Θ(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
KD Tree Θ(log(n)) Θ(log(n)) Θ(log(n)) Θ(log(n)) O(n) O(n) O(n) O(n) O(n)

Array Sorting Algorithms 
Algorithm Time Complexity Space Complexity
Best Average Worst Worst
Quicksort Ω(n log(n)) Θ(n log(n)) O(n^2) O(log(n))
Mergesort Ω(n log(n)) Θ(n log(n)) O(n log(n)) O(n)
Timsort Ω(n) Θ(n log(n)) O(n log(n)) O(n)
Heapsort Ω(n log(n)) Θ(n log(n)) O(n log(n)) O(1)
Bubble Sort Ω(n) Θ(n^2) O(n^2) O(1)
Insertion Sort Ω(n) Θ(n^2) O(n^2) O(1)
Selection Sort Ω(n^2) Θ(n^2) O(n^2) O(1)
Tree Sort Ω(n log(n)) Θ(n log(n)) O(n^2) O(n)
Shell Sort Ω(n log(n)) Θ(n(log(n))^2) O(n(log(n))^2) O(1)
Bucket Sort Ω(n+k) Θ(n+k) O(n^2) O(n)
Radix Sort Ω(nk) Θ(nk) O(nk) O(n+k)
Counting Sort Ω(n+k) Θ(n+k) O(n+k) O(k)
Cubesort Ω(n) Θ(n log(n)) O(n log(n)) O(n)

Ref: Khan Academy

Wednesday 12 October 2016

Some important file location for WebSphere Application Server

Published Application EAR File:

{WEBSHPERE_INSTALLED_LOCATION}\profiles\{PROFILE_NAME eg AppSrv1}\installedApps\{CELL_NAME eg WVWNO006C0634Node01Cell}\{YOUR_APP_NAME}

Log file Location:

{WEBSHPERE_INSTALLED_LOCATION}\profiles\{PROFILE_NAME eg AppSrv1}\logs\server1

Tuesday 11 October 2016

How to configure custom datasource properties using websphere application server console like currentSQLID

Open WebSphere application server console, from server tab right click on the server and choose Administration->Run Administrative console

Then on the left hand side menu go to resources section and pick Data Sources

Click on the datasource you need to add/change properties

Then follow the below path

{DATA_SOURCE} -> custom properties 

Now you can pick the properties you want to change/add.

PSCP Command to transfer file from/to Unix server

PSCP is a freeware SCP (Secure CoPy) program for the Windows command line processor. You can use this program instead of FTP for copying files to or from the Unix servers at the NBER offices. FTP is not allowed from clients outside nber.org to servers inside the firewall because FTP leaves passwords in plain-text and vulnerable to eavesdropping. PSCP should work with any host supporting SSH, not just at NBER.

pscp username@yourhost:file.foo c:\temp\file.foo


pscp -v c:\"my documents"\file.foo username@nber.org:file.foo

Thursday 6 October 2016

Some useful ant commands

Delete files inside a zip/jar/war/ear:

<project name="myproject">
<target name="updateZip">
<zip destfile="updatedZip.zip">
<zipfileset src="baseZipFromWhereToDelete.zip" excludes="path1,path2" />
</zip>
</target>
</project>

Inject/update some content inside zip/jar/war/ear:

<project name="myproject">
<target name="updateZip">
<zip update="true" destfile="theFileNeedsToBeUpdated.zip" baseDir="TheContentWhichWillBeIjected" />
</target>
</project>

How to compress/zip a set of files:

<zip destfile="TheZipFileNameWhichWillBeCreated.zip" basedir="TheContentWhichAreGoingToBeCompressed" />

Setting up data source in TomEE at EAR level

Prerequisite:
  1. Configure TomEE to deploy EAR Application in Apps directory
    • Got to the directory {TOMEE_HOME}\conf\
    • Open the file tome.xml in a text editor
    • <Deployments dir="apps" /> add this tag to tome.xml
    • And Create a directory apps in {TOMEE_HOME}
  2. Configure data source at EAR level
      • Add the data-source information according to the database you have chosen. And make sure you add the schema as well. You can copy the below schema to your application.xml file

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd"
version="6"

      • To get the database class name please follow the below link http://tomee.apache.org/common-datasource-configurations.html
      • Now open in a text editor web.xml file from WEB-INF directory of webmodule project and declare the resource reference you created in application.xml file in EAR module. Make sure the reference name is the same as data-source name in application.xml file.  
      • You can now call the datasource from your code. Below is an example of calling datasource from a jsp page.    
      • Build the application and copy the .ear file to {TOMEE_HOME}\apps directory.
      • Test the application web application from web browser.

Publish jar to file system local repository maven deploy

  • Create a local repository directory for example C:\localRepo
  • Here you have two way to publish jar in local repo, either you can create a batch script to publish all of the jars together or open command terminal and execute batch command one by one.
Command Line:
>mvn install:install-file -DgroupId=%groupId% -DartifactId=%artifactid% -Dversion=%versionId% -Dpackaging=jar -Dfile=%JarFile% -DlocalRepositoryPath=C:\localRepo 

BatchScript:
I have added eight jar files to publish, you can adjust the script according to your need.

@echo off
title repository workbrain local setup
color 1f
:: ###################################
cls
:start
echo Maven local workbrain repository setup
call mvn install:install-file -Dfile=axis.jar  -DgroupId=axis -Dversion=axis -DartifactId=axis -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=commons-discovery.jar  -DgroupId=commons-discovery -Dversion=commons-discovery -DartifactId=commons-discovery -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=commons-logging.jar  -DgroupId=commons-logging -Dversion=commons-logging -DartifactId=commons-logging -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=jaxrpc.jar  -DgroupId=jaxrpc -Dversion=jaxrpc -DartifactId=jaxrpc -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=log4j.jar  -DgroupId=log4j -Dversion=log4j -DartifactId=log4j -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=saaj.jar  -DgroupId=saaj -Dversion=saaj -DartifactId=saaj -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=soap.jar  -DgroupId=soap -Dversion=soap -DartifactId=soap -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=wsdl4j.jar  -DgroupId=wsdl4j -Dversion=wsdl4j -DartifactId=wsdl4j -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
echo.
echo ######################################################
echo All done :-)
echo ######################################################
pause>null



Open pom.xml file of your project and add the below tag under repositories tag


<repository>
      <id>internal local repository</id>
       <url>file://C:\localRepo</url>
</repository>

Then you can add your dependency in the  dependency tag.

Build your project

Publish file system library (jar, war, ear) to remote repository ( nexus) using maven deploy command.

I have created a batch script to publish all the libraries inside a directory, you have to just place the batch script in the directory and execute it.

Before you execute the script make sure that you have defined a server in maven settings.xml (it will be found in {USER_HOME}/.m2 directory)

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <servers>
    <server>
      <id>central</id>
    </server>
    <server>
      <id>snapshots</id>
    </server>
    <server>
  <id>nexus</id> <!-- this is the repository id in batch script -->
  <username>deployment</username>
  <password>deployment123</password>
  </server>
  </servers>

Batch script to publish jars

@echo off
color 1f
::#############################################################################
cls
:start

set repositoryUrl=http://192.168.0.19:8082/nexus/content/repositories/releases/
set repositoryId=nexus
set packaging=jar
set groupId={give_a_group_id}
set versionId={give_a_version}

setlocal EnableDelayedExpansion

echo Reading jar files to publish nexus repository %repositoryUrl%
for %%a in (*.jar) do (
    echo deploying jar file %%~na
    call mvn deploy:deploy-file -DgroupId=%groupId% -DartifactId=%%~na -Dversion=%versionId% -Dpackaging=jar -Dfile=%%~na.jar -DrepositoryId=%repositoryId% -Durl=%repositoryUrl%              
               
)

echo Finished publishing to nexus repository %repositoryUrl%

pause > null

Set cusomize class loader policy to PARENT_LAST for enterprise application in WAS

So, what is the best way to customize the classloader policy for an EAR application? we can consider 4 options.
  • Change class loader policy to PARENT_LAST after every deployment from administrative console
  • Set the server profile setting to use a PARENT_LAST classloader policy for all apps. However this might not be a POLICY for all of the app deployed in WAS.
  • Write and execute a jython script after every publish to set the classloader policy.
  • Create deployment.xml using RSA deployment descriptor editor J2EE plugin tool

You can choose any of the option according to your business needs.
But the best thing would be found out a solutions to application specific and set at EAR application project level so that any topology can suite your solution.

So here the best approach I think, create a deployment.xml file and put it in the EAR application root level.

<?xml version="1.0" encoding="UTF-8"?>
<appdeployment:Deployment xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:appdeployment="http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi" xmi:id="Deployment_124111296412322">
 <deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_23423402344" startingWeight="1" warClassLoaderPolicy="SINGLE">
    <classloader xmi:id="Classloader_23432543596" mode="PARENT_LAST"/>
    <modules xmi:type="appdeployment:WebModuleDeployment" xmi:id="WebModuleDeployment_12411129640345" startingWeight="10000" uri="yourwarproject.war">
      <classloader xmi:id="Classloader_12411324234"/>
    </modules>
  </deployedObject>
</appdeployment:Deployment>

You can use any method to package your application, you can generate/copy deployment.xml via ANT or any tool.

    <ear destfile="${artifactsDir}/${earName}.ear" appxml="${projectName}_EAR/application.xml">
         <fileset dir="${artifactsDir}" includes="${yourwarproject}.war"/>
         <fileset dir="${projectName}_EAR/" includes="deployment.xml"/>
    </ear>

Wednesday 5 October 2016

WebSphere Deployment Descriptors

  • Open WebSphere Programming Model Extensions Descriptor : (For applications that target WebSphere Application Server) The WebSphere bindings section provides a place to add WebSphere Programming Model Extensions.
  • Open WebSphere Bindings: (For applications that target WebSphere Application Server) The WebSphere bindings section provides a place to add users and groups to the security roles.
  • Open WebSphere Extensions: (For applications that target WebSphere Application Server) The WebSphere Extensions section provides a place to add WebSphere Extensions.
Use the General Information section to view the display name and description for the enterprise application, as stored in the application.xml file.

Class loader in JVM, J2EE and WAS

Lets first discuss a little  bit about the class loader. Class loaders enable the JVM to load Java classes for use by applications during deployment and operation.

When you start a JVM, it uses the following class loaders:

  • Bootstrap class loader Loads only the core Java libraries in...
    JAVA_HOME/jre/lib
    This class loader, which is part of the core JVM, is written in native code.
  • Extensions class loader, Loads the code in the extensions directories...JAVA_HOME/jre/lib/ext ...or any other directory specified by the property...
    java.ext.dirs system, This class loader is implemented by the class... sun.misc.Launcher$ExtClassLoader
  • Application class loader Loads code found on...
    java.class.path
    ...which ultimately maps to the system CLASSPATH variable. This class loader is implemented by the class... sun.misc.Launcher$AppClassLoader
WAS and Java EE application class loaders

When working with Java EE applications, two additional types of class loaders are involved:

  • The application server class loaders, which loads all of the classes needed for the application server in which the enterprise applications are running.
  • The application class loaders, which loads the application classes as defined in web.xml


WAS provides several custom delegated class loaders, similar to those class loaders, but it implements the extensions as OSGi packages.

It was just an introduction how class loader loads library, 

Monday 3 October 2016

Installing WebSphere 8.5.5.2 in RSA, Java 7 Profile

Rational Software Architect is a modeling and development environment that uses the Unified Modeling Language (UML) for designing architecture for C++ and Java EE (JEE) applications and web services. Rational Software Architect is built on the Eclipse open-source software framework and includes capabilities focused on architectural code analysis, C++, and model-driven development (MDD) with the UML for creating applications and web services.

By default WebSphere 8.5.5.0 supports Java 6, so to install java 7 profile in existing WebSphere 8.5.5.0 is a bit complicated. So to install you have to login to IBM Passport Advantage with your IBM id and access to passport advantage site and download the repo.

WebSphere 8.5.5.2, supports java 7 profile and ships with Java 7 SDK.

If you wish to install you can download it from http://www.ibm.com/support/docview.wss?uid=swg24037250  and login with your IBM user id. After login go to the download section and download the highlighted sources.

Also download the java 7 sdk from the same page 7.1.3.10_0001-WS-IBMWASJAVA-Win.zip

To install Fix Pack 8.5.5.2 using a local repository, point Installation Manager to the repository.xml file on your local machine. If part1 and part2 .zip files exist, unzip the part1 and part2 .zip files to the same folder on your local machine. Both .zip files are required for installation of the fix pack. When unzipping part2, the Windows operating system might warn you that a directory with the same name already exists. Allow the tool to merge the directories; the tool does not overwrite any files.

To add the repository:
  1. Start Installation Manager. 
  2. In the top menu, click File > Preferences. 
  3. Select Repositories. 
  4. Click Add Repository. 
  5. Enter the path to the repository.xml file in the location containing the repository files.
    • This is a sample path for a local repository on Windows operating system:C:\repositories\product_name\local-repositories
    • This is a sample path for a local repository on AIX, HP-UX, Linux, Solaris operating system:/var/repositories/product_name/local-repositories
Also add the 7.1.3.10_0001-WS-IBMWASJAVA-Win.zip repository. ( you don't have to unzip this source)

Installation
  1. Click install
  2. Then follow the instruction in the screen, select the websphere 8.5.5.2 and Java 7 
  3. Click next next

You might see some error while installing the websphere 8.5.5.2 if you have existing websphere installed, so make sure to uninstall/delete the existing one.

After successful installation, go to the directory location {WEBSPHERE_INSTALLATION_LOCATION}/bin and execute the below command

>managesdk.bat -listAvailable
>managesdk.bat -enableProfile -profileName {ProfileName} -sdkname {sdkname}

{profileName} = you can see from RSA server section, when you right click on server and properties
{sdkname} = it will show the name of sdk once you execute the first command