Sunday, March 20, 2011

Vmware Virtualization API

In my current project , I am working on Vmware vSphere API (VI sdk ). Here are the key notes that we can take from the vSphere API.

What is vSphere API and vsphere SDK ?
VMware vSphere API is a set of interfaces for managing vSphere. You can use the API to do almost all the things the vSphere Client can do. The API is defined as SOAP Web Services with WSDL.
The vSphere SDK is a set of libraries that support vSphere API, as well as tools and samples that assist your development efforts. The vSphere SDK is also known as VI SDK, vCenter SDK, ESX SDK, etc.
In most discussions, the vSphere API and vSphere SDK are used interchangeably.

Who would use and care about the API?
Both system administrators and software developers would be interested in vSphere automation and integration. VMware customers can use it to automate vSphere; partners can use it to integrate with vSphere platform.

Where Should I Get Started?
Before getting started with vSphere API, you should learn the vSphere product first. It should give you basic concepts regarding virtual machine, host, networking, storage, etc. The vSphere API is just a “view” into the vSphere product. Also very helpful is the Managed Object Browser(MOB) which allows you to explore the inventory and see the managed object internals. Optionally, you can use VIJava Browser.
Once you have a good understanding of vSphere, you should check out the object model of vSphere API. The UML diagram gives you a high level overview on managed objects available in the API. For a complete listing of all the object types, you can check out here and also the API reference.
After that, you have several choices on the language bindings. If you use Java, you want to read this tutorial which helps you to run your first HelloWorld like application in 5 minutes. For others, check out the language binding section.

Where can I download vSphere SDK?
Check out the Web Service SDK at developer community download center.
Note: if you are using the open source VI Java API, go to the http://vijava.sf.net.

Where can I find documentations?

All the official documentation of vSphere SDK is here. You can also buy VMware VI and vSphere SDKfrom Amazon.

Where can I get help?

You can post your questions to the VMware management API forum. For VI Java API related questions, post them to the VI Java API forum.

What language should I use for vSphere API?
The vSphere API is based on SOAP Web Services. As a result, you can use any programming language that supports SOAP. For out of box support, see next question.

What language bindings are available for vSphere API?
Java: http://vijava.sf.net
.Net: http://sourceforge.net/projects/vitfordotnet/
Ruby: https://github.com/rlane/rbvmomi
Perl: http://www.vmware.com/support/developer/viperltoolkit/
PowerShell: http://www.vmware.com/support/developer/PowerCLI/

You can use VI Java API with JVM based languages like JRuby (Ruby), Jython (Python), Scala, etc. Check out the vSphere Java API section.Currently there is no PHP support for vSphere API.

vSPHERE JAVA API

What is VI Java API?
Virtual Infrastructure API is an open source project created by Steve Jin from VMware R&D. It aims to simplify the use of VI SDK and improve the performance. This API is also known as vSphere Java API because of the name change of VI to vSphere.

How is this API different from VI SDK?
This API is built on top of the web services interfaces defined in WSDL included in VI SDK. To use this API 1.0, you have to get VI SDK installed and configured first. For more information on how to install VI SDK, please refer Developer’s Setup Guide.
Since version 2.0, you don’t need any installation and configuration of VI SDK. All you need is two jar files of this API and dom4j, which are all included in the binary download.

What does this API mean to me?
If you are using Java to develop applications talking to VMware Infrastructure (vSphere), this API is for you. Using this API instead of the VI SDK web services interfaces results in significantly less line of code, much faster, and much more readable code.
Here are two samples with the same functionality written with VI SDK (part of 100 lines) and with VI Java API (all 28 lines). You can tell the difference by yourself.
Also, take a look at the testimonials from the community.

What is released?
The binary, source code, and over 50 samples showing how to use the API. There are two different bundles: one with binary only for runtime; the other with all source and samples.

Where should I get started?
First, please check out the Get Started Tutorial. Optionally, read these two short documents: Get started with VI Java API (2 pages), and Object model of VI Java API (3 pages) listed at VI Java API documentation Links to PDF files are included in the documents.
Secondly, go to the download page and download the latest release. Once the downloaded jar file is included in your existing VI SDK project’s classpath, you can start to program your first HelloVM program. Note: With 2.0 and beyond, you don’t need any jar files from VI SDK.


Where can I find Javadoc of the API?
There is unfortunately no Javadoc coming with API. One reason for this is that the API has faithfully followed the object model of vSphere API, and as a result the vSphere API reference can be used. The key differences are: for the every property of managed object, VI Java API has an equivalent getter method; for the every method in vSphere API reference, forget about the first _this parameter and use real types instead of ManagedObjectReference.

Where can I find the sample code and API source code?
Click here for Sample code. Note: before you run the samples, make sure you change the URL/username/password, and possible other parameters in the samples.
For API source code, check here.

How can I get an managed object if I have its ID, for example task-id?
Read this article.

How to find a folder for virtual machine or hosts?
The folder itself is a type which can hold datacenters, virtual machines, hosts, etc. It’s determined by its property “childType.” Check this post for in-depth discussion

How to set up proxy with the VI Java API?
It’s no different for other Java applications. Check out details here.

Can I set up connection timeout in VI Java?
Yes, check here.

How can I ignore the SSL certificate? Should I?
It really depends. If you want to ignore the certificate, you can simple provide a “true” value to the ServiceInstance constractor as follows:

ServiceInstance si = new ServiceInstance(new URL(urlStr), username, password, true);

Even you ignore the certificate, the traffics between are still encrypted. But the client won’t authenticate if the server is what it claims to be. I recommend you to ignore the certificate in development time, just for convenience.
If you put false, you have to import the server SSL key into Java keystore and provide the path to the keystore when you run the application.

VI JAVA Browser

You can optionally use the VI Java Browser.

Can I export and import OVF with vSphere API?
Yes. More are here and here.

How can I delete a virtual machine?
Check out here.

Can I create my own Task and Event using vSphere API?
Yes. Check here.

How to get Event type?
Check here.

Why isn’t there event type for network port group?
Not everything happened with vSphere has a corresponding event. This post discusses this is in great details.

Is there an easy to keep update with property changes?
Yes. Check out the caching framework in VI Java API.

Troubleshooting

Why can’t I use the API with ESXi via API?
It’s possible that you are using free version of ESXi which doesn’t fully support the API. Check this post, and upgrade your license.

What are the common faults?
Check out here.

What Should I do if I got an exception?
First of all, you need to isolate the exception to see if it’s from vSphere API or a normal programming exception. One simple way is check the full name of the exception. The vSphere API exception normally have package name like com.vmware.vim25.
When it’s a vSphere API exception, check the API reference to see what’s the nature of the exception and what could possibly cause it. With that in mind, check your API parameters and the managed object conditions like power status of a virtual machine. See other FAQs below. You also use server logs and Onyx to help.

Why return com.vmware.vim25.NotSupported when calling cloneVM?
Some method for example cloneVM_Task() is only supported on vCenter. When you call ESX or ESXi, you may get this NotSupported exception. So, switch to vCenter server instead. It’s also possible that you got this with vCenter in some rare cases. The solution is to switch to ESX or ESXi.

Why did I get javax.net.ssl.SSLHandshakeException?
It’s possible that you haven’t correctly set up the certificate with your Java keystore. If you are in development phase, you can temporarily bypass this by ignore the certificate as described in a previous FAQ.

What can I do if I got com.vmware.vim25.InvalidRequest exception?
The InvalidRequest means your parameter to vSphere API is not expected by the server: either missing some required parameters, or giving inappropriate values. It’s mostly hard to find out which parameters. You want to check out the sample code and compare them with yours. If there is no clue, you want to use Oynx to record vSphere Client and compare that of your application.

Why did I get com.vmware.vim25.InvalidProperty exception?
It’s most likely you’ve specified a wrong property name or a wrong property path while using the PropertyCollector managed object. The VI Java API has provider getter methods for all direct properties of all managed objects. You will much less like to get this InvalidProperty exception using VI Java API. Even so it’s still possible to get this exception. Check out this article for in-depth discussion.

Why did I get com.vmware.vim25.TaskInProgress?
The TaskInProgress exception is caused by multiple calls at the same time. If you can limit one call on one VM at a time, the issue may go away. Check the chapter 18 of my book (http://amzn.to/97ZyAg) on multithreading issues in details.

Why I got java.lang.InstantiationError while running VI Java API?
Most likely you use VI Java 2.0 and newer, but still have jar files of Apache AXIS in your classpath. Remove all of them, and you should be able to run.

Why does it fail with some username/password?
You need to escape the special characters like <, >, ‘, “, & in a string passed into the API because VI Java does not escape them for you in 2.0 and 2.1 and thus fails the XML parser on the server side. We may change this in the future.

How to turn on logs of vCenter and ESX?
For ESX, check this article. For vCenter, check out LucD’s blog.

My server application works fine in lab but doesn’t scale in production. Any thought?
It’s possible with session management. Check out this article for tips.

What tools to troubleshoot an application built on vSphere API?
First of all, your application can be debugged as any other applications. You should get familiar with the debuggers of your language and IDE.
Besides the debuggers, you want to look at Onyx. This is essentially a proxy which can intercept the traffics between your application and vCenter/ESX. You can analyze the XML messages and compare them with those recorded between vSphere Client and vCenter/ESX.
If you want to monitor every possible IP packet, you can take a look at WireShark. It may take a little effort to set up the SSL certificate for decrypting the messages, and set up the filters for interested packets.
You can also switch HTTPS to HTTP and use tools like TcpMon to log the HTTP traffic. Because HTTP is not enabled by default, I wouldn’t recommend it when Oynx can serve your need.


Reference :

http://www.doublecloud.org/vmware-vsphere-api-and-sdk-faq/


Thanks To ALL !!!!






Sunday, January 16, 2011

Log4J

In this tutorial you will learn about Log4J, which is one of the most used Logging API by the Java programmers. It is fast and easy to use and provides customizable log formats. Here we are providing many examples of the Log4J framework.

Log4J is an Open Source logging framework from the apache foundation. It's a Java based framework and can be used in any java program. It was originally developed by Ceki Gülcü and now its part of Apache Software project.

It can be used by the Java programmers to add logging functionality in the projects. These days most of the Java projects uses Apache Log4J for logging.

It provides powerful yet efficient and fast API for the Java programmers. Java programmers easily uses Log4J in their code to log the error and information messages into a log file.

Log4j simply inserts a log statement in the application code and manage them externally without going back to its application source code. Programmer's can control the logging message with the help of external configuration file e.g. log4.xml or log4j.properties file. The external properties file can be used to set the log format as well as the level of logging (DEBUG, INFO, FATAL etc..)

Three main component of Log4J

The architecture of Log4J framework is layered and consists of three main components. There components of the Log4J are:


  1. Logger
  2. Appender
  3. Layout

1. Logger

Logger is the most essential component of the logging process. It is responsible for capturing the logging information. There are 5 different log levels of the Logger. The level of Logger are as following:

There are 5 normal levels of logger:

  • DEBUG : Most useful to debug an application.
  • INFO : It provides informational messages.
  • WARN : It provides that application may have harmful events.
  • ERROR : It provides that application having error events but that might allow it to continue running.
  • FATAL : It denotes the severe error events which lead the application to abort.

In addition, there are two special levels also they are:

  • ALL : It is intended to turn on all logging
  • OFF : It is intended to turn off logging


2. Appenders in Log4J:

The Appender is responsible for publishing the log to a destination. It controls how the logging provides the output.

There are few list of appenders listed below:

  • ConsoleAppender
  • DailyRollingFileAppender
  • FileAppender
  • RollingFileAppender
  • WriterAppender
  • SMTPAppender
  • SocketAppender
  • SocketHubAppender
  • SyslogAppendersends
  • TelnetAppender


FileAppender appender = new FileAppender(new PatternLayout(),"filename");
 WriterAppender is used as:
appender = new WriterAppender(new PatternLayout(),new FileOutputStream("filename"));

3. Layouts in Log4J:

For each Appender it needs to have an associated Layout, which guides how to format the output. The Layout is responsible for formatting the log output in different layouts. User can control the output format by modifying the Log4J configuration file.

There are basically three types of Layout:

  1. HTMLLayout : It formats the output in the HTML table
  2. PatternLayout : It formats the output in the conversion pattern
  3. SimpleLayout : It formats the output in a simple manner, it prints the level then place a dash and then the user specified log message.

Configuring Log4J

For running application using Log4J you need to download the latest version log4j jar file and then add this to the classpath.

There are two ways to configure the Log4J one is by using properties file and other by using xml file. Using xml for Log4J is quite popular and it is recommended also.


Pattern Layout :

In our this section we are describing about most general layout PatternLayout. It is a flexible layout configurable with its pattern string. A conversion pattern consists of literal text and format control expressions. Here is the list of some of the conversion characters used for formatting:

Character Effect
cUsed to output the category of logging event
CUsed to output the fully qualified class name of the caller issuing the logging request
dUsed to output date of the logging event
mUsed to output the application supplied message associated with the logging event
nOutputs the platform dependent line separator character or characters

p

Used to output the priority of the logging event
rUsed to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event
tUsed to output name of thread of logging event
FUsed to output the file name


For More information:

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html


Sample Log4.properties file

# Set root category priority to INFO and its appender to LOGFILE instead of the

# the default CONSOLE.

#log4j.rootCategory=INFO, CONSOLE

log4j.rootCategory=INFO, LOGFILE

# Set the enterprise logger category to FATAL and its appender to LOGILE.

#log4j.logger.org.apache.axis.enterprise=FATAL, LOGFILE

# Set root logger level to DEBUG and its only appender to A1.

#log4j.rootLogger=DEBUG, A1


# A1 is set to be a ConsoleAppender.

#log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.

#log4j.appender.A1.layout=org.apache.log4j.PatternLayout

#log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x (%F%l)- %m%n

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.


# This section is currently not being used. However due to some reasons, the

# the logging mechanism fails, then the console output will be based on the

# the pattern we define here.


#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

#set back to WARN before shipping

#log4j.appender.CONSOLE.Threshold=WARN

#log4j.appender.CONSOLE.Threshold=INFO

#log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

#log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n


# LOGFILE is set to be a Rolling File appender using a PatternLayout.


#RollingFileAppender creates backup of old files if the current filesize increases

#beyond MaxFileSize (if property not defined, default is 10MB).

log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender


#The filename should be kept consistent with the executable name.

log4j.appender.LOGFILE.File=G:/example.log

# The logfile will always append and not start from beginning unless the size

# limit is reached.

log4j.appender.LOGFILE.Append=true


# The default level is set to INFO. This can be changed to DEBUG, INFO, FATAL,

# ERROR, WARN, TRACE

#set back to WARN before shipping

#log4j.appender.LOGFILE.Threshold=WARN

#log4j.appender.LOGFILE.Threshold=INFO


# Use the pattern layout for displaying date-time etc

log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout


# The actual pattern to be used.

#log4j.appender.LOGFILE.layout.ConversionPattern=%-4r %d [%t] %-5p %c %x - %m%n

log4j.appender.LOGFILE.layout.ConversionPattern=%-4r %d [%t] %-5p %c %x (%F%l)- %m%n

#log4j.appender.LOGFILE.layout.ConversionPattern= %d %-5p %c %x - %m%n

#log4j.appender.LOGFILE.layout.ConversionPattern=%p %t %c - %m%n


# The max file size of the axis log.

log4j.appender.LOGFILE.MaxFileSize=10MB


# The number of backup's to be created for the logfile before the first is

# cleared and reused.

# Currently setting to 1 as we require only one backup for the File.

log4j.appender.LOGFILE.MaxBackupIndex=1




Friday, January 14, 2011

Singleton Design Pattern

What is Singleton Design Pattern ?

Singleton Design Pattern is a convention for ensuring one and only object is instantiated for a given class.

What use is that?
There are many objects that we need only one of: thread pools, caches , objects that handle preferences and registry settings.
The singleton pattern also gives us a global point of access , just like a global variable.With Singleton pattern you can:

  • Ensure that only one instance of a class is created
  • Provide a global point of access to the object
  • Allow multiple instances in the future without affecting a singleton class's clients.

public class Singleton {

private static Singleton uniqueInstance ;
// other useful instance here
private Singleton () { }
public static Singleton getInstance() {

if ( uniqueInstance == null ) {

uniqueInstance = new Singleton ();
}
return uniqueInstance ;
}


Dealing with Multihtreading ?

public class Singleton {

private static Singleton uniqueInstance ;
// other useful instance here
private Singleton () { }
public static synchronized Singleton getInstance() {

if ( uniqueInstance == null ) {

uniqueInstance = new Singleton ();
}
return uniqueInstance ;
}





Sunday, August 22, 2010

Path and Classpath in JAVA

JAVA is a high level programming language based on OOPS ,implemented by Sun MicroSystems.Java applications are typically compiled to bytecode that can run on any Java Virtual Machine(JVM) regardless of computer architecture.

Java is currently one of the most popular programming languages in use, and is widely used from application software to web applications.

Principles

There were five primary goals in the creation of the Java language:[20]

  1. It should be "simple, object oriented, and familiar".
  2. It should be "robust and secure".
  3. It should be "architecture neutral and portable".
  4. It should execute with "high performance".
  5. It should be "interpreted, threaded, and dynamic".

What is path and classpath in JAVA

PATH
In JDK the path variable contains directories where binary files (.exe files in Windows ) will be looked for.When we have written a java programs we require some commands to execute source code .As javac for compilation of the code and java for execution.So the path of these files is termed as PATH .

We set the path variable like this ie C:\java\jdk1.6.0_3\bin

There are two ways to set the path .

On command prompt(a temporary way)
C:\>set path =%path;C:\Java\jdk1.6.0_03\bin%

By temporary way means we will set the path for the current command window only .As we close the command window and try compiling the source code then you will get a error message as "Command not Found".

CLASSPATH
In JDK the classpath contains directories or JAR files which your java compiler/runtime will look for .class files .
For example if you have ArrayList class in your source code then it will look for the .class file of the ArrayList class
which will be read from the classpath location.

For setting classpath on command prompt
C:\>set classpath =%classpath;C:\java\jdk1.6.0_03\lib%