tag:blogger.com,1999:blog-40988856719181631592024-03-13T04:59:54.492-07:00Dollop of DesiTips and tricks on Datawarehousing, Java, linux and Nonstop programming.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.comBlogger33125tag:blogger.com,1999:blog-4098885671918163159.post-57491622861757759412015-03-27T12:15:00.001-07:002015-07-08T15:18:15.151-07:00Accessing samba bookmarks from linux command line<span style="font-size: small;">You can setup bookmarks to any folder on a host running samba with something like this<br />smb://DOMAIN-NAME;username@host/folder-name/<br /><br />After providing the credentials. you can browse through the folder on file explorer(for example nautilus on ubuntu).<br /><br />To access the same from command line, you don't need to mount any network drives, it's already mounted for you.</span><br />
<br />
Try <br />
<br />
<span style="color: blue;">ls -l /var/run/user/1001/gvfs/</span><br />
<br />
where 1001 happens to be the folder assigned for my user id.Yours could be different.<br />
<br />
This should list all the samba shares that are active.<br />
<br />
<span style="color: blue;">$ ls -l /var/run/user/1001/gvfs/</span><br />
<span style="color: blue;">total 0</span><br />
<h2>
<span style="color: blue;"></span></h2>
<span style="color: blue;"><br />drwx------ 1 myuser myuser 0 Mar 19 11:07 smb-share:domain=mydomain,server=host,share=<folder-name>,user=myuser<br />drwx------ 1 myuser myuser 0 Feb 16 13:29 smb-share:domain=mydomain,server=host,share=<folder-name>,user=myuser</folder-name></folder-name></span><br />
<br />
<span style="color: blue;"><folder-name><folder-name><span style="color: black;">To access the different shares all you have to do is cd to it. That's it, job done.</span> </folder-name></folder-name></span>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-69112824123852360162012-10-22T11:09:00.001-07:002012-10-22T11:25:46.725-07:00post-download-artifact or post-retrieve-artifact trigger?Are you having a problem with ivy post-download-artifact trigger? Is the ant call target not firing for you? Then read on.<br />
<br />
Here is a sample post-download-artifact trigger for handling a zip artifact.<br />
<pre><triggers>
<ant -call prefix="dep" target="unzip"
event="post-download-artifact" filter="type=zip"/>
</ant>
</triggers>
</pre>
<br />
<br />
Maybe the post-download-artifact event is not firing because the artifact is being fetched from the cache and not getting downloaded from a repository. Well, in that case, post-retrieve-artifact is the solution for you. post-retrieve-artifact will fire after getting the artifact either from a download or from the local cache. Problem solved.<br />
<br />
Now does it make post-download-artifact redundant? Yes of course! So you better replace all the post-download-artifact ivy triggers in ivysettings file to post-retrieve-artifact triggers. That will take care of the issue.<br />
<br />
This was discovered by my brilliant co-worker Mike. You can browse his blog <a href="http://christiansons.net/mike/blog" target="_blank">here</a>. Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-8623675540970160312012-09-13T10:31:00.000-07:002012-09-13T10:31:08.003-07:00Improving performance of Jenkins<a href="http://jenkins-ci.org/" target="_blank">Jenkins</a> slows down over a period of time. Builds start taking more time than usual. The start up time increases to manifolds. You may wonder that throwing in more memory to the Jenkins process or making the JVM process do garbage collection frequently, etc might make it better. But if that doesn't do the trick, then read on.<br />
<br />
One way to check if builds in Jenkins are taking a long time is by running them outside of
Jenkins. if you see a considerable difference in the times then Jenkins
may be slowing down.<br />
<br />
Here is a <a href="http://www.cloudbees.com/sites/default/files/whitepapers/7WaysToOptimizeJenkins.pdf" target="_blank">white paper</a> on optimizing Jenkins written by it's creator with some really nice tips.<br />
<br />
But in our case the real reason for the slow performance turned out to be the temp files! Jenkins creates a boatload of them, around 100s (this depends on how many active jobs you have) each hour and doesn't bother to clean them up at all.<br />
<br />
On windows platform Jenkins writes to <i><span style="color: #0b5394;">C:\Temp</span></i> and on Linux it is <i><span style="color: #3d85c6;">/var/tmp</span></i>. Luckily there is a <a href="https://wiki.jenkins-ci.org/display/JENKINS/Tmp+Cleaner+Plugin" target="_blank">plugin</a> which can be configured to clean out these directories. For example you could setup the plugin to keep files which are a day old and get rid of the rest.<br />
<br />
Try it out and see if that makes Jenkins sprint. Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-75586416488394384852012-09-09T18:38:00.001-07:002012-09-09T18:42:51.301-07:00Process Explorer for WindowsTask Manager has been the default process manager on windows systems for a long time and even today. <a href="http://technet.microsoft.com/en-US/sysinternals" target="_blank">Sysinternals</a>, a company Microsoft acquired way back in 2006, has a suite of troubleshooting tools.The process monitor from Sysinternals called <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" target="_blank">Process Explorer</a>, is much more useful, cool and powerful than the good old windows Task manager.<br />
You can see things like the runtime program directives, process tree and many more for a live process.<br />
You could query a process, on which files it has open or find out all processes that have a handle on a data file. Those are really helpful features for trouble shooting.<br />
<br />
Here is a first look at process explorer<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjapFWHvhriw_gL2xgjbqdYUiLDavpuG_yCpbWpdd6B02hnmhYT_L0HTksj6OQNaMvF0rpjLlKzzmYF3vDYh-GRkxDIck1LXY0SRvi-S5i2Jf_a-aMs0_Dca8CB6zCbaIahyphenhyphencDKgCA61U8/s1600/ProcessExplorerMain.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="261" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjapFWHvhriw_gL2xgjbqdYUiLDavpuG_yCpbWpdd6B02hnmhYT_L0HTksj6OQNaMvF0rpjLlKzzmYF3vDYh-GRkxDIck1LXY0SRvi-S5i2Jf_a-aMs0_Dca8CB6zCbaIahyphenhyphencDKgCA61U8/s320/ProcessExplorerMain.bmp" width="320" /></a></div>
<br />
<br />
To look at the files that are used by a process, You can turn on process tree from the view menu. Here is the process tree.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF-mwMrR07L_PiRzu-llpXxgZgYY6gWS83XQWT7gQDefEfT2_sRRDldotukrCHXi_GqFS-HTGRKotLz3DAr0PDJ_i39oJkLaI3MxUup-1i2GLNSWGYUZJRXCMnOjUJ80TcCmhgk67sTZI/s1600/ProcessTree.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="261" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF-mwMrR07L_PiRzu-llpXxgZgYY6gWS83XQWT7gQDefEfT2_sRRDldotukrCHXi_GqFS-HTGRKotLz3DAr0PDJ_i39oJkLaI3MxUup-1i2GLNSWGYUZJRXCMnOjUJ80TcCmhgk67sTZI/s320/ProcessTree.bmp" width="320" /></a></div>
<br />
<br />
<br />
<br />
Further if you want to find the process holding on to a file then use the search feature and let's search for processes using java.exe<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8aR6POwi5MwnbKXCmtOwgpN94dR0tqD3A7azzd0wpR5hy_z2rxkDgucVXrcJDLaOubRtf_n0HyrUcfXvJN2AoC9EaAIbXNWDvVa20IZLQq4-clPLaKjgctW3PR2uL7dSAQyggHqq8TTU/s1600/FindProcessesUsingaFile.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="218" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8aR6POwi5MwnbKXCmtOwgpN94dR0tqD3A7azzd0wpR5hy_z2rxkDgucVXrcJDLaOubRtf_n0HyrUcfXvJN2AoC9EaAIbXNWDvVa20IZLQq4-clPLaKjgctW3PR2uL7dSAQyggHqq8TTU/s400/FindProcessesUsingaFile.bmp" width="400" /></a></div>
That of course lists tons of processes. Now let's try a more refined search, for a log file.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ5_orFk7axXvL7wYd30A4o_t434Bk1XGrc5Dju6uaghuORzSmiUUocgt2MHRABCDByPr5ohn_wntI7Oox7Lt1dSxpI5BoeF0LzzkzQNaP4O8UqxUwGxt7M6KXsdwlHpM1-rm_ECfIaWQ/s1600/ExampleofFindingProcessesUsingaFile.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="218" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ5_orFk7axXvL7wYd30A4o_t434Bk1XGrc5Dju6uaghuORzSmiUUocgt2MHRABCDByPr5ohn_wntI7Oox7Lt1dSxpI5BoeF0LzzkzQNaP4O8UqxUwGxt7M6KXsdwlHpM1-rm_ECfIaWQ/s400/ExampleofFindingProcessesUsingaFile.bmp" width="400" /></a></div>
There are many more useful features that you can explore further with Process Explorer. I always felt Process Explorer could be included as part of the regular windows distribution!Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-82417376816853645312012-08-09T13:03:00.001-07:002012-08-09T13:03:27.414-07:00Overriding Ivy dependencyIvyDE (the eclipse plugin for Ivy) manages dependencies by automatically downloading them from the repository. It also attaches the sources(if they are available in the repository) to binaries under the hood without any custom settings. Helps to debug the source for any dependency.<br />
<br />
That's great but what if you are working on multiple projects at the same time and want to test/debug the new code that you are writing in a dependency module.<br />
<br />
One way to do that is to comment out the dependency(let's call it project X) entry in ivy.xml for X and then include the source for X by adding project X to the buildpath of the main project(let's call it project A).<br />
<br />
Now if the dependency is used in multiple projects(say B and C) and B and C are dependencies of project A, then you have much more work to do.<br />
Like<br />
<ul>
<li>Comment out entries for projects B and C in project A's ivy.xml and then add project B and C to project A's build path. </li>
</ul>
<ul>
<li> Comment out entries for X in project's B and C's ivy.xml</li>
<li>Add project X to the build path for projects B and C. </li>
</ul>
<br />
A quicker and much more cleaner alternative is to add the dependency (project X) to the main project's (A) buildpath and move the order of X to the top of the classpath. In Eclipse IDE, this is available under "Order and Export" tab of "Java Build Path" menu under project properties. As shown in the picture below.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil6gv0kr8B_HxD7nOHfK0YH_ZWJ5yp2qqnXmKAKF_72ZDbNt_buXzVmCND_eBjrv9WC11IK2wnbjlGHW6LUjfjygudM0g11iPTSCU08mpQ4cA85s3IvuARIuzekEGHBX_Hn7ZsuppoWcI/s1600/Order+and+Export+for+Project+A.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="222" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil6gv0kr8B_HxD7nOHfK0YH_ZWJ5yp2qqnXmKAKF_72ZDbNt_buXzVmCND_eBjrv9WC11IK2wnbjlGHW6LUjfjygudM0g11iPTSCU08mpQ4cA85s3IvuARIuzekEGHBX_Hn7ZsuppoWcI/s320/Order+and+Export+for+Project+A.png" width="320" /></a></div>
<br />
You are done. Set a breakpoint within project X and see how it goes!<br />
<br />
<br />Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-47006134479507024652012-05-31T13:18:00.000-07:002012-05-31T13:23:55.091-07:00Debugging dependency source with IvyDE<a href="http://ant.apache.org/ivy/ivyde/" target="_blank">IvyDE</a> an Eclipse plug-in for Ivy makes it a lot easier to work with Ivy. Some of the notable things that IvyDE does are automatic resolving of dependencies when ivy.xml is modified or the first time a project is loaded, ability to clear caches by providing a sub menu, Error checking/validation of ivy configurations and so on.<br />
Once you install IvyDE and enable a project to use IvyDE for managing dependencies, you lose the ability to attach a source to any of the jars in the classpath. That makes it difficult to debug the source for any dependency.
The crude way I was doing it was to comment out the said dependency in ivy.xml. Saving the file should trigger a resolve on the project. That should remove the dependency from the build path. Then add the desired project to the classpath.<br />
<br />
I found out later that it doesn't have to be that way though. IvyDE provides a neat way out. Even though there are no examples of how exactly to setup configure. After numerous trials and errors I got it working. Hope somebody else who is trying to Debug source with Ivy finds this post useful enough.<br />
<br />
I created a tutorial at the following <a href="https://github.com/chaninja/experiments/tree/master/IVY-Debugging" style="color: blue;">location</a>.
<a href="https://github.com/chaninja/experiments/tree/master/IVY-Debugging/Utility" style="color: blue;">Utility</a> is the project which publishes a binary Utility.jar and Utility-source.zip artifacts. <a href="https://github.com/chaninja/experiments/tree/master/IVY-Debugging/TRY-IVY-DEBUG" style="color: blue;">TRY-IVY-DEBUG</a> is the project which uses Utility.jar and has a simple class <a href="https://github.com/chaninja/experiments/blob/master/IVY-Debugging/TRY-IVY-DEBUG/src/org/ivy/test/Test.java">Test.java</a> that can be used to debug into the source.
<br />
<pre>
<configurations>
<conf name="default" description="binary" />
<conf name="sources" description="source codes" />
</configurations>
<publications defaultconf="default">
<artifact name="Utility" type="jar" conf="default" ext="jar" />
<artifact name="Utility-sources" type="source" conf="sources" ext="zip" />
</publications>
<dependencies/>
</pre>
There are two configurations defined, default and source. The description attribute is self explanatory. The artifacts are Utility.jar and Utility-sources.
The key here is <b>type=”source”</b>. That's one of the two things IvyDE is looking for when trying to bind the sources to the jars. The other requirement is that the source artifact name suffix needs to match the ones in default IvyDE configuration as shown below.
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqNEUWZvgnCWj1_T9JwYnsQpQWZlxuUaPASp55ZxcwSeGvEGKjEwyj85oErEWgZ9Wuk0VfJOvcjI8NpH75qBL1dUqrlwUJzOv3pw11JaT3R_ky8cwmdH7qAv0CalIYM_hyphenhyphenIo4_XoCMyqk/s1600/IvyDE+preferences.png" imageanchor="1"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqNEUWZvgnCWj1_T9JwYnsQpQWZlxuUaPASp55ZxcwSeGvEGKjEwyj85oErEWgZ9Wuk0VfJOvcjI8NpH75qBL1dUqrlwUJzOv3pw11JaT3R_ky8cwmdH7qAv0CalIYM_hyphenhyphenIo4_XoCMyqk/s320/IvyDE+preferences.png" width="291" /></a></div>
If you load the two projects mentioned above in eclipse. Build the Utility project using the included build.xml and perform a resolve on the TRY-IVY-DEBUG project. That should make eclipse download and attach the Utility-sources.zip to the Utility.jar artifact.You should now be able to debug Test.java class and step into the attached source.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-86691940823802526212012-03-18T23:24:00.014-07:002012-03-18T23:35:28.073-07:00Publishing to a branch using Ivy<div style="font-family: "Courier New",Courier,monospace;"><span style="font-size: small;">Ivy off the shelf works fine if you are not dealing with branches. The examples in the samples that can be downloaded don't have much for using branches. That's because branches are optional for Ivy. All it cares about is the module name and revision number for getting the last build number or publishing an artifact to the repository.</span></div><div style="font-family: "Courier New",Courier,monospace;"><br />
</div><div style="font-family: "Courier New",Courier,monospace;">If you can live without branches then things are fine with Ivy as a dependency manager. But life is not that simple! Once I started using branches everything went haywire. Because I had not made the configuration changes in all the right places. Having figured it out the hard way, would like to share the list of fixes.</div><div style="font-family: "Courier New",Courier,monospace;">The things to do when configuring ivy for branches are</div><ul style="font-family: "Courier New",Courier,monospace;"><li> <span style="font-size: small;">Include branch attribute in artifact and ivy pattern. Like shown below here</span></li>
</ul><div style="font-family: "Courier New",Courier,monospace;"> ivy.artifact.pattern=[organisation]/[module]/[<b>branch</b>]/[revision]/[artifact].[ext]</div><div style="font-family: "Courier New",Courier,monospace;"> ivy.pattern=[organisation]/[module]/[<b>branch</b>]/[revision]/ivy.xml</div><ul style="font-family: "Courier New",Courier,monospace;"><li>Set default branch attribute in ivysettings file as shown below.</li>
</ul><pre style="font-family: "Courier New",Courier,monospace;"><ivysettings>
<properties file="ivysettings.properties"/>
<settings defaultCacheDir="${ivy.settings.dir}/ivy-cache" <b>defaultBranch</b>="trunk" defaultResolver="chain" latest="latest-compatible"/>
<resolvers>
<chain name="chain">
<url name="projects">
<artifact pattern="http://buildserver/${repository.dir}/${ivy.artifact.pattern}" />
<ivy pattern="http://buildserver/${repository.dir}/${ivy.pattern}" />
</url>
<ibiblio name="libraries" m2compatible="true" usepoms="false" />
<ibiblio name="java-net-maven2" root="http://download.java.net/maven/2/" m2compatible="true" />
</chain>
</resolvers>
</ivysettings> </pre><br />
<ul style="font-family: "Courier New",Courier,monospace;"><li>Specify branch attribute in ivy.xml under the info section.</li>
</ul><pre style="font-family: "Courier New",Courier,monospace;"><ivy-module version="1.0">
<info
organisation="org.coastal"
module="mymodule"
branch="RB-1.0.0"
status="integration"
revision="1.0"/>
<publications/>
<dependencies/>
</ivy-module> </pre><br />
<ul style="font-family: "Courier New",Courier,monospace;"><li>Make sure the buildnumber ivy target has branch parameter too. Like shown below.</li>
</ul><pre style="font-family: "Courier New",Courier,monospace;"><target name="ivy-new-version" depends="" unless="ivy.new.revision">
<!-- default module version prefix value -->
<property name="module.version.prefix" value="${ivy.revision}." />
<!-- gets next version number from ivy repository -->
<ivy:info file="${ivy.file}" />
<ivy:buildnumber
organisation="${ivy.organisation}" module="${ivy.module}"
<b>branch</b>="${ivy.branch}"
revision="${module.version.prefix}" defaultBuildNumber="1" revSep=""/>
</target></pre><br />
<ul style="font-family: "Courier New",Courier,monospace;"><li>The publish ivy target has branch attribute as well.</li>
</ul><pre style="font-family: "Courier New",Courier,monospace;"><target name="publish-remote" depends="build" description="--> publish this project to ivy repository">
<ivy:publish artifactspattern="${dist.dir}/[artifact].[ext]"
resolver="projects"
pubrevision="${version}"
<b>pubbranch</b>="${ivy.branch}"
update="true"
status="${ivy.status}"
/>
<echo message="project ${ant.project.name} released with version ${version}" />
</target>
</pre>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-76840273780308324522011-12-21T15:37:00.000-08:002011-12-21T15:40:08.204-08:00Java 7 monitoring files/foldersJava 7 has this new <a href="http://docs.oracle.com/javase/tutorial/essential/io/notification.html" target="_blank">watchservice </a>which is a welcome development and you can read about it in many blogs as well as the <a href="http://docs.oracle.com/javase/tutorial/essential/io/notification.html" target="_blank">oracle</a> site. I tried playing with it using the code sample from some of the blogs. But unfortunately it seemed to work only the very first time and not after that.<br />
Here is the code<br />
<br />
<pre>public FileWatcher(String directoryPath) throws IOException {
this.directoryPath = directoryPath;
FileSystem fileSystem = FileSystems.getDefault();
watcher = fileSystem.newWatchService();
Path myDir = fileSystem.getPath(this.directoryPath);
myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
monitorFolder();
}
private void monitorFolder() {
executor = Executors.newSingleThreadScheduledExecutor();
executor.submit(this);
}
public void run() {
WatchKey watckKey = null;
try {
while(!shutdown ) {
watckKey = watcher.take();
events = watckKey.pollEvents();
listener.processEvent(events);
}
} catch (InterruptedException e) {
logger.error("Error monitoring directory", e);
}
} </pre><br />
Some more beating around the bush and googling I discovered that the WatchKey needs to be reset for subsequent events to flow through to the listener.<br />
Wonder why this strange behaviour. Anyhow here is the fix and after that everything was fine and dandy! <br />
<br />
<pre>public void run() {
WatchKey watchKey = null;
try {
while(!shutdown ) {
watchKey = watcher.take();
events = watchKey.pollEvents();
listener.processEvent(events);
<span style="color: blue;"><b>watchKey.reset();</b></span> }
} catch (InterruptedException e) {
logger.error("Error monitoring directory", e);
}
}
</pre><br />
Found out later by paying more attention(!!) to the article on the oracle <a href="http://docs.oracle.com/javase/tutorial/essential/io/notification.html" target="_blank">site</a> that resetting the watchkey is key<br />
to getting future events.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-62263089301930560742011-09-23T14:19:00.000-07:002011-09-29T12:41:11.684-07:00Interleaving audio files to different channels<div class="separator" style="clear: both; text-align: center;">
</div>
Audio files have inherent characteristics like number of channels, sample size, frame size, sample rate, file type, number of samples, etc. to quote a few. <br />
<br />
<br />
Here is a nice description of samples and channels from the java sound faq.<br />
<br />
<div style="color: #999999; font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;">Each second of sound has so many (on a CD, 44,100)
digital samples of sound pressure per second. The number of
samples per second is called sample rate or sample
frequency. In PCM (pulse code modulation) coding, each
sample is usually a linear representation of amplitude as a
signed integer (sometimes unsigned for 8 bit). <i> </i></span></div>
<div style="color: #999999; font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><i>There is one
such sample for each channel, one channel for mono, two
channels for stereo, four channels for quad, more for
surround sound. One sample frame consists of one sample for
each of the channels in turn, by convention running from
left to right.</i></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><i><span style="color: #999999;">
</span></i></span></div>
<div style="color: #999999; font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><i>Each sample can be one byte (8 bits), two bytes (16
bits), three bytes (24 bits), or maybe even 20 bits or a
floating-point number. Sometimes, for more than 16 bits per
sample, the sample is padded to 32 bits (4 bytes) The order
of the bytes in a sample is different on different
platforms. In a Windows WAV soundfile, the less significant
bytes come first from left to right ("little endian" byte
order). In an AIFF soundfile, it is the other way round, as
is standard in Java ("big endian" byte
order). </i></span></div>
<br />
Some more audio file fundamentals from the javadocs...<br />
<br />
frameSize is the number of bytes in each frame of a sound that has this format.<br />
<br />
sampleRate is the number of samples played or recorded per second, for sounds that have this format.<br />
<br />
More definitions can be found <a href="http://download.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioFormat.html">here. </a><br />
Audio files are made up of samples and samples are made up of bytes.<br />
Audio
files come in two types, mono and stereo. Mono files have only one
channel (perhaps was recorded with one receiver). Stereo audio
files can have from two to as many channels.<br />
<br />
Mono audio
files when played, samples from the single channel are automatically
duplicated and sent to all the channels. For example if speakers are
attached to a computer, that would be two channels.In the case of a stereo audio file, it is pre-recorded for multiple channels.<br />
<br />
Now what if you want to interleave two different audio files into one audio file such a way that each audio file is played on separate channels. There are many possible use cases for doing that. One could be to get the remix effect. For example you can take a song and mix it with some background music or add some percussion effect. Of course there are many sophisticated audio software out there which can do this and much more. But the goal here is to demonstrate a simple java program to achieve interleaving of audio files.<br />
<br />
Here I am going to take two mono audio files of the same format (wave file), encoding (PCM) and sample rate. Interleave them to produce another wave file with different audio playing on each channel.<br />
<br />
I am using Java Sound API for this exercise. Java supports only AU, AIFF and WAV formats. Extensions/plug-ins for MP3 and other audio formats are available through 3rd party <a href="http://www.javazoom.net/javalayer/javalayer.html">vendors</a>.<br />
The audio files used in this example are <span style="color: blue;">leftChannelAudio.wav</span> and <span style="color: blue;">rightChannelAudio.wav</span><br />
<br />
The format details for the audio files are<br />
<br />
<span style="color: blue;">rightChannelAudio.wav</span><br />
<br />
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">nbChannel = 1</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">frameRate = 44100.0</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">frameSize = 2</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">sampleSize(bits)= 16</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">nbSamples = 1105408</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">encoding = PCM_SIGNED</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">sample rate = 44100.0</span></div>
<br />
<span style="color: blue;">leftChannelAudio.wav</span><br />
<br />
<span style="font-size: x-small;">nbChannel = 1</span><br />
<span style="font-size: x-small;">frameRate = 44100.0</span><br />
<span style="font-size: x-small;">frameSize = 2</span><br />
<span style="font-size: x-small;">sampleSize(bits) = 16</span><br />
<span style="font-size: x-small;">nbSamples = 1069056</span><br />
<span style="font-size: x-small;">encoding = PCM_SIGNED</span><br />
<span style="font-size: x-small;">sample rate = 44100.0</span><br />
<br />
Here is the main method of the class.<br />
<pre>public static void main(String[] args) {
try {
String soundFileLeft = "leftChannelAudio.wav";
File fileLeft = new File(soundFileLeft); // This is the file we'll be playing on the left channel.
String soundFileRight = "rightChannelAudio.wav";
File fileRight = new File(soundFileRight);
float sampleRate = 44100.0f;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = false;
AudioFormat targetFormat = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
AudioMixer mixAudio = new AudioMixer(fileLeft, fileRight, targetFormat);
File outFile = new File("outSingleSingleMixer.wav");
mixAudio.mixnWrite(AudioFileFormat.Type.WAVE, outFile);
} catch(Exception e) {
e.printStackTrace();
}
}
</pre>
<br />
<br />
<br />
<br />
The interleaving of audio bytes is done in <b>mixIntoStereoAudio</b> method as shown below.<br />
<pre> private AudioInputStream mixIntoStereoAudio(AudioInputStream leftAudioInputStream,
AudioInputStream rightAudioInputStream) throws IOException{
ArrayList<byte[]> byteArrays = new ArrayList<byte[]>();
int nbChannels = 1;
byte[] compiledStream = null;
int leftAudioBytes = -1;
int rightAudioBytes = -1;
byteArrays.add(convertStream(leftAudioInputStream));
byteArrays.add(convertStream(rightAudioInputStream));
long maxSamples;
if (leftAudioInputStream.getFrameLength() > rightAudioInputStream.getFrameLength()) {
maxSamples = leftAudioInputStream.getFrameLength();
nbChannels = leftAudioInputStream.getFormat().getChannels();
} else {
maxSamples = rightAudioInputStream.getFrameLength();
nbChannels = rightAudioInputStream.getFormat().getChannels();
}
long maxOutputSizeinBytes = maxSamples * sampleSizeinBytes;
if (nbChannels == 1)
maxOutputSizeinBytes = maxOutputSizeinBytes * 2;
compiledStream = new byte[(int) maxOutputSizeinBytes]; //max size of number of bytes
log.info("Output bytes size: " + compiledStream.length);
for(int i = 0; i < compiledStream.length; i += sampleSizeinBytes){
leftAudioBytes = writeSamplestoChannel(byteArrays, 0,
sampleSizeinBytes, compiledStream, leftAudioBytes, i);
i += sampleSizeinBytes;
rightAudioBytes = writeSamplestoChannel(byteArrays, 1,
sampleSizeinBytes, compiledStream, rightAudioBytes, i);
}
AudioInputStream newaudioStream = generateNewAudioStream(compiledStream);
return newaudioStream;
}</byte[]></byte[]>
</pre>
<br />
<br />
In this case the sample has two bytes. In a stereo audio file samples are written out in the following fashion. First sample for left channel and second for right and so on.<br />
This example uses a simple technique of filling a sample from each audio file for each channel respectively.Filling in silence value whenever we run out of samples from an audio file. This is because the two audio files being interleaved might not have the same number of samples.<br />
<br />
This technique can be extended easily to handle stereo audio files or list of audio files for each channel.<br />
Go ahead and run this program with the sample audio files provided or you can just listen to how the interleaved output file plays out.<br />
<br />
The complete program and sample audio files are available <a href="https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0Bw_Ej8uaENYXNTkxOWZhMjUtYThiYi00OGIyLWIyYjktOTFkNDljNzY1Yzcz&hl=en_US">here.</a><br />
<br />
Some good resources on using Java Sound API are listed below.<br />
<br />
<a href="http://www.jsresources.org/faq_audio.html">http://www.jsresources.org/faq_audio.html</a><br />
<a href="http://www.builogic.com/java/javasound-read-write.html">http://www.builogic.com/java/javasound-read-write.html</a>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-61598366559771928882011-07-08T12:22:00.000-07:002011-07-08T12:22:50.676-07:00Routing Serial data to a socketYou don't need a hardware device (like Lantronix adapter) to route data from a Serial port to a socket. Thanks to <a href="http://www.dest-unreach.org/socat/">socat</a>, you can relay data from a serial port to another or serial port to a socket or socket to socket and plenty more such combos, all in one line! socat is exhaustive in what it can take as arguments. Check it out.<br />
<br />
Here is an example of making available data from serial port /dev/ttyS0 on a linux environment (usually this is COM1 on windows boxes) through a socket (port = 8023).<br />
<br />
<span style="color: blue;">socat TCP-LISTEN:8023,fork /dev/ttyS0,raw,b9600,echo=0</span><br />
<br />
The baud rate specified here is 9600. <i>fork</i> option lets you make multiple connections to the source of data, which in this case is the serial port.<br />
<br />
<span style="color: blue;"><span style="color: black;">You can verify the data </span> <span style="color: black;">from the socket by using a telnet or similar application.</span></span><br />
<span style="color: blue;"><span style="color: black;"><br />
</span></span><br />
<span style="color: blue;"><span style="color: black;"><span style="color: blue;">telnet localhost 8023</span></span></span><br />
<br />
<div style="color: black;"><br />
</div>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com1tag:blogger.com,1999:blog-4098885671918163159.post-2386524114806739532011-06-02T17:10:00.000-07:002011-06-08T17:21:40.326-07:00Monitoring data from a Serial portSerialports have gone out of vogue! You no longer get serial ports on a computer except on Desktop PCs, unless you custom order it. Notebooks/Laptops, no such luck. But there there are many people and companies which deal with serial ports on a daily basis.<br />
<br />
In the windows world we had the good old Hyperterminal program which was shipped with all windows distributions until recently. Now <a href="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CCwQFjAA&url=http%3A%2F%2Fwww.hilgraeve.com%2Fhyperterminal%2F&rct=j&q=hyperterminal&ei=DBPoTb7UO4i2sAO2yPj7DQ&usg=AFQjCNFbMrokbE_qcmXk9gtmj-PJloBXfA&sig2=L4uZIsJMoSGHh0Zb726low&cad=rja">Hyperterminal</a> is a payware utility and can be purchased separately. No free utility available out there for windows! But then in the linux world, things are friendlier. There is minicomm and many more free utilities that come with the operating system.<br />
<br />
Well the point of this blog post is not to berate windows or anything. Just wanted to share a way you could look at the data coming in from a serial port without downloading/purchasing any software. This is very handy for troubleshooting, say if you are looking for any control characters or any noise in the serial line, etc.All this while an app is running using the serialport. This can be done outside the app space to monitor activity on a serial port.<br />
To see the data from a serial port on command line as well as save it to a file here is what you can do.<br />
<br />
The script command writes data from a terminal to the specified output file.<br />
<br />
<div style="color: blue;">script -af serialOut.txt </div><br />
hexdump reads from a serial port and prints out the bytes in hex and ASCII <br />
<br />
<span style="color: blue;">hexdump -vC /dev/ttyS0</span><br />
<br />
Here is what the whole thing looks like after executing the commands above...<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk4dI0wj6MmL9u2J9voUtM1Tssl4GOK29UPxXZd-jW3LDBFy0XLzlsreJBSPrYjDWoi1kdGtoPyXXCHvdcyXO4TN5ht9k4wh-Kbe2N89nkbsSGDNEjh2kFP8bDFFlx6GjtKb7rvja4zY8/s1600/Screenshot-hexdump-1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="256" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk4dI0wj6MmL9u2J9voUtM1Tssl4GOK29UPxXZd-jW3LDBFy0XLzlsreJBSPrYjDWoi1kdGtoPyXXCHvdcyXO4TN5ht9k4wh-Kbe2N89nkbsSGDNEjh2kFP8bDFFlx6GjtKb7rvja4zY8/s320/Screenshot-hexdump-1.png" width="320" /></a></div><br />
The top window shows the output from the terminal and the bottom one shows the contents of the output file in real time.<br />
<br />
You can do this and much more on a windows platform too! By installing <a href="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CCUQFjAA&url=http%3A%2F%2Fwww.cygwin.com%2F&rct=j&q=cygwin&ei=0B7oTbHQO4qisQPD9qXYDQ&usg=AFQjCNG181i4HT_pzwalR1pEs9gAFGqazQ&sig2=aXjyo0Tu7X8gpmaiaH-aYg&cad=rja">cygwin</a> and running linux commands within the cygwin environment.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-85680256139805889612011-03-18T10:42:00.000-07:002011-03-18T10:42:07.372-07:00Advanced Installer - Changing the package Install DriveThe default drive for installing an application on a windows system is C. In fact on launching the .msi or .exe installer file it does show you the complete path where the package will be installed. but if you preset the drive to say D on the .aip file, the msi installer chokes on boxes where there is no D drive.<br />
<br />
The fix here is to create some custom action and execute the custom action as part of the install/uninstall sequence. See the custom action screen snapshot below.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLZ0mIyPeAHm3G5ZCAofOsOb_smI0Ye7vwlJCDKbp943I7f0hX3t4n-_ILphZY3hDyckgSNJ4N8_ac5V79kS79jv-a3t5l4OLDnmjLQ3y3NGyawCkuu9OPHFMe_jP8l3WOrHlwx23E8-A/s1600/Custom+Action.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="246" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLZ0mIyPeAHm3G5ZCAofOsOb_smI0Ye7vwlJCDKbp943I7f0hX3t4n-_ILphZY3hDyckgSNJ4N8_ac5V79kS79jv-a3t5l4OLDnmjLQ3y3NGyawCkuu9OPHFMe_jP8l3WOrHlwx23E8-A/s320/Custom+Action.JPG" width="320" /></a></div><div class="separator" style="clear: both; text-align: center;">Here we set the value for APPDIR property pointing to D drive if the condition D_DRIVE holds true. The INSTALL_DIR property is set in the Install parameters tab as shown below.</div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3DayN9SxS5pWPFRKP43Ti8vGvOC8NB8uGhu2NtDBhh5KCnsHDnaypmYTUDml_w4N6gfUrfbWa-77O0BCpvaUO-7DAEmykZbeN26FV6gGiGoCSVcnjwyd3EDy07r2d3YB9GzZ_UwdfeaE/s1600/InstallParameters.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="245" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3DayN9SxS5pWPFRKP43Ti8vGvOC8NB8uGhu2NtDBhh5KCnsHDnaypmYTUDml_w4N6gfUrfbWa-77O0BCpvaUO-7DAEmykZbeN26FV6gGiGoCSVcnjwyd3EDy07r2d3YB9GzZ_UwdfeaE/s320/InstallParameters.JPG" width="320" /></a></div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;">Last but not the least is defining the D_DRIVE condition.</div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmfbQcQa1zKzg42hIaYROT_baPaKExQpx7j3FYBuV3ek5q8PTm2tvg9Yjkca5B5LxSF-giS4en46qhbbCbs0DAIFpwnI0gm_KweWkilhvdcrXSYctv6vDmXhFimjEmJfMDpcGn3nqx5B0/s1600/Search.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="246" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmfbQcQa1zKzg42hIaYROT_baPaKExQpx7j3FYBuV3ek5q8PTm2tvg9Yjkca5B5LxSF-giS4en46qhbbCbs0DAIFpwnI0gm_KweWkilhvdcrXSYctv6vDmXhFimjEmJfMDpcGn3nqx5B0/s320/Search.JPG" width="320" /></a></div><div class="separator" style="clear: both; text-align: center;"><br />
</div>Following the above steps should enable you to install on D drive or for that matter any drive (if it exists) by default.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com5tag:blogger.com,1999:blog-4098885671918163159.post-77981902004454901492011-01-19T10:30:00.000-08:002011-01-24T10:15:02.311-08:00Unable to login to Ubuntu. Screensaver unresponsive!Generally I don't power off my machine at the end of the day or for that matter anytime unless there are updates that prompt me to restart. Also I leave firefox ON with 10-15 tabs! So the system is ON for weeks sometimes even a month or two! Ofcourse some of the processes start getting slower and slower. especially Firefox. But then all I need to fix that is to restart Firefox.<br />
But in the last 6 months or so I have been encountering this weird problem.<br />
when I come to work the next day...many times I couldn't get to the login dialog screen, no matter how hard I tried with the mouse or keyboard. Seemed like the gnome screen saver had gone into the weeds! The machine was ON and everything was running. The solution to this is to login through a command line session CTRL-ALT-F6 and kill the gnome-screen saver process. I used <i style="color: blue;">top</i> utility to find the process id (look for gnome-screensaver in the <i style="color: blue;">command</i> column) and also kill it. and then try ALT-F7 and everything is fine.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-30430013710901739052010-07-14T11:34:00.000-07:002010-08-19T16:34:39.102-07:00SOAP vs REST in Java land<div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">SOAP and REST are the two most popular web service technologies in use today. REST has become the more preferred one, since it deals directly with URIs and can handle requests in plain text directly over HTTP. Testing it with a browser or tools like curl is super easy.</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">Let's go through some examples here. Since our focus is on the Java landscape.All the examples are in the Java environment.</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">The development environment I used is eclipse 3.5, Ubuntu 10.04 LTS the Lucid Lynx, JDK 1.16.20</span></div><div style="font-family: Arial,Helvetica,sans-serif;"></div><br />
<div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">With Java annotations kicking off in big style with Java 5. Now the coolest way to develop a web service is by using POJOs and some sprinkling of annotations here and there.</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">Let's start with a simple Java class (POJO) and make a web service out of it!</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">Let's take the same Java class (CalcWSImpl.java and Calculator.java are pretty much identical except for the annotations) and setup a web service using SOAP as well as REST. </span></div><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">Developing a SOAP based web service</span></h2><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">There are many nice tools available today bundled with the JRE like <a href="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBcQFjAA&url=http%3A%2F%2Fdownload.oracle.com%2Fdocs%2Fcd%2FE17409_01%2Fjavase%2F6%2Fdocs%2Ftechnotes%2Ftools%2Fshare%2Fwsimport.html&ei=z7Y8TMiXDYGmsQOpvoHaCg&usg=AFQjCNF_ivfNilLDf3lsSAyc4NhWdwGHgA&sig2=cz_fjcHzaErLv_RVWjtzbQ">wsimport</a>, <a href="http://www.google.com/url?sa=t&source=web&cd=2&ved=0CBkQFjAB&url=https%3A%2F%2Fjax-ws.dev.java.net%2Fnonav%2F2.1.2%2Fdocs%2Fwsgen.html&ei=97Y8TKvtF4j6sAOLj43aCg&usg=AFQjCNHQWcrd-PlH-MBOmti58q_42y7S8g&sig2=3pwmydaumYapwK-GztsHJQ">wsgen</a> to generate web service client source code and wrapper classes to build a web service.</span><br />
<br />
<span style="font-size: small;">We start with a simple class Calculator.java. see the listing below.</span><span style="font-size: small;"> </span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"> </span> </div><br />
<div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"></div><br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0gafiCJFUge8suqAmdBytmizDcTn2qOL4dIm4XpaMA4nBfL-n_nX1tR3RFNKaq8MAttGGsWoIspW9Mw9_eOOEjZ6v_t1uptiUXwIKc1r2Sh7YgL5L7yQOgq38uvg8s1OX1UkHtF8rCuc/s1600/Screenshot-Java+-+WS+SOAP-src-org-webservice-server-Calculator.java+-+Eclipse+.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0gafiCJFUge8suqAmdBytmizDcTn2qOL4dIm4XpaMA4nBfL-n_nX1tR3RFNKaq8MAttGGsWoIspW9Mw9_eOOEjZ6v_t1uptiUXwIKc1r2Sh7YgL5L7yQOgq38uvg8s1OX1UkHtF8rCuc/s320/Screenshot-Java+-+WS+SOAP-src-org-webservice-server-Calculator.java+-+Eclipse+.png" /></a></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span><br />
<div class="separator" style="clear: both; text-align: center;"></div><br />
<br />
<span style="font-size: small;"> This class already has the following JAX-WS annotations...</span></div><br />
<div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">@WebService - to identify itself as an endpoint class.</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div align="LEFT" style="color: black; font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">@SOAPBinding(style=SOAPBinding.Style.<i>RPC</i>) – style for messages used in a webservice.</span></div><div align="LEFT" style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div align="LEFT" style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><span style="color: black;">@WebMethod – to expose the method as a webservice operation.</span></span></div><div align="LEFT" style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div align="LEFT" style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><span style="color: black;">For a complete list of all the JAX-WS annotations refer to <a href="https://jax-ws.dev.java.net/jax-ws-ea3/docs/annotations.html">JAX-WS annotations</a></span></span></div><div align="LEFT" style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><span style="color: black;">Just with these annotations , we are ready to expose this class through a web service.</span></span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">Take a look at TestCalc.java, which launches a web service.</span><br />
<br />
<pre><span style="font-size: small;"></span><span style="font-size: small;">package org.webservice.server;
import javax.xml.ws.Endpoint;
public class TestCalc {
public static void main(String[] args) {
Calculator calcWS = new Calculator();
Endpoint.publish("http://localhost:8085/calc", calcWS);
}
}</span>
<span style="font-size: small;"></span></pre><span style="font-size: small;"></span><span style="font-size: small;"><br />
</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">Run it and you have a running webservice at </span><span style="color: #2a00ff; font-size: small;"><a href="http://localhost:8085/calc">http://localhost:8085/calc</a>.</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="color: #2a00ff; font-size: small;">The wsdl is available at <a href="http://localhost:8085/calc?wsdl">http://localhost:8085/calc?wsdl</a></span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="color: #2a00ff; font-size: small;">This is what the generated .wsdl looks like.</span><br />
<span style="color: #2a00ff; font-size: small;"></span><br />
<pre><span style="color: #2a00ff; font-size: small;"></span><span style="color: #2a00ff; font-size: small;"><?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.webservice.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.webservice.org/" name="CalcWS">
<types></types>
<message name="div">
<part name="arg0" type="xsd:int"></part>
<part name="arg1" type="xsd:int"></part>
</message>
<message name="divResponse">
<part name="return" type="xsd:float"></part>
</message>
<message name="add">
<part name="arg0" type="xsd:int"></part>
<part name="arg1" type="xsd:int"></part>
</message>
<message name="addResponse">
<part name="return" type="xsd:int"></part>
</message>
<message name="list"></message>
<message name="listResponse">
<part name="return" type="xsd:int"></part>
</message>
<portType name="Calc">
<operation name="div" parameterOrder="arg0 arg1">
<input message="tns:div"></input>
<output message="tns:divResponse"></output>
</operation>
<operation name="add" parameterOrder="arg0 arg1">
<input message="tns:add"></input>
<output message="tns:addResponse"></output>
</operation>
<operation name="list">
<input message="tns:list"></input>
<output message="tns:listResponse"></output>
</operation>
</portType>
<binding name="CalcWSPortBinding" type="tns:Calc">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"></soap:binding>
<operation name="div">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="http://server.webservice.org/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="http://server.webservice.org/"></soap:body>
</output>
</operation>
<operation name="add">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="http://server.webservice.org/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="http://server.webservice.org/"></soap:body>
</output>
</operation>
<operation name="list">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="http://server.webservice.org/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="http://server.webservice.org/"></soap:body>
</output>
</operation>
</binding>
<service name="CalcWS">
<port name="CalcWSPort" binding="tns:CalcWSPortBinding">
<soap:address location="http://localhost:8085/calc"></soap:address>
</port>
</service>
</definitions> </span>
<span style="color: #2a00ff; font-size: small;"></span>
<span style="color: #2a00ff; font-size: small;"></span></pre><span style="color: #2a00ff; font-size: small;"><br />
</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><definitions name="CalcWS" targetnamespace="http://server.webservice.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.webservice.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"><service name="CalcWS"><port binding="tns:CalcWSPortBinding" name="CalcWSPort"><soap:address location="http://localhost:8085/calc"></soap:address></port></service></definitions><span style="font-size: small;"> </span><span style="font-size: small;">Now we are ready to test the newly created web service. Either you can go use the free SOAP clients available on the Internet like <a href="http://www.blogger.com/%20http://www.soapclient.com/soaptest.html">soap client</a> or tools like wsimport (that comes with JDK) to generate wrapper classes to test a web service endpoint.</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">Now using the live web service and wsimport tool, let's generate some client classes. Run the following command from the root folder of the project.</span></div><div style="color: #b45f06; font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">wsimport -d bin -s test http://localhost:8085/calc?wsdl </span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">The last step creates an interface Calc..... </span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;"></span><br />
<pre><span style="font-size: small;"></span><span style="font-size: small;">
package org.webservice.server;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.6 in JDK 6
* Generated source version: 2.1
*
*/
@WebService(name = "Calc", targetNamespace = "http://server.webservice.org/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Calc {
/**
*
* @param arg1
* @param arg0
* @return
* returns float
*/
@WebMethod
@WebResult(partName = "return")
public float div(
@WebParam(name = "arg0", partName = "arg0")
int arg0,
@WebParam(name = "arg1", partName = "arg1")
int arg1);
/**
*
* @param arg1
* @param arg0
* @return
* returns int
*/
@WebMethod
@WebResult(partName = "return")
public int add(
@WebParam(name = "arg0", partName = "arg0")
int arg0,
@WebParam(name = "arg1", partName = "arg1")
int arg1);
/**
*
* @return
* returns int
*/
@WebMethod
@WebResult(partName = "return")
public int list();
}
</span>
<span style="font-size: small;"></span></pre><span style="font-size: small;"></span><br />
<span style="font-size: small;">.....and a service class CalcWS</span> <br />
<span style="font-size: small;"></span><br />
<pre><span style="font-size: small;"></span><span style="font-size: small;">
package org.webservice.server;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.6 in JDK 6
* Generated source version: 2.1
*
*/
@WebServiceClient(name = "CalcWS", targetNamespace = "http://server.webservice.org/", wsdlLocation = "http://localhost:8085/calc?wsdl")
public class CalcWS
extends Service
{
private final static URL CALCWS_WSDL_LOCATION;
private final static Logger logger = Logger.getLogger(org.webservice.server.CalcWS.class.getName());
static {
URL url = null;
try {
URL baseUrl;
baseUrl = org.webservice.server.CalcWS.class.getResource(".");
url = new URL(baseUrl, "http://localhost:8085/calc?wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:8085/calc?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
CALCWS_WSDL_LOCATION = url;
}
public CalcWS(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CalcWS() {
super(CALCWS_WSDL_LOCATION, new QName("http://server.webservice.org/", "CalcWS"));
}
/**
*
* @return
* returns Calc
*/
@WebEndpoint(name = "CalcWSPort")
public Calc getCalcWSPort() {
return super.getPort(new QName("http://server.webservice.org/", "CalcWSPort"), Calc.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns Calc
*/
@WebEndpoint(name = "CalcWSPort")
public Calc getCalcWSPort(WebServiceFeature... features) {
return super.getPort(new QName("http://server.webservice.org/", "CalcWSPort"), Calc.class, features);
}
}
</span>
<span style="font-size: small;"></span></pre><span style="font-size: small;"></span><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">Now let's run the test client CalcWSClient</span><br />
<span style="font-size: small;"></span><br />
<pre><span style="font-size: small;"></span><span style="font-size: small;">package org.webservice.server;
import javax.xml.ws.WebServiceRef;
public class CalcWSClient {
@WebServiceRef(wsdlLocation="http://localhost:8085/calc?wsdl")
static CalcWS service = new CalcWS();
public static void main(String[] args) {
try {
CalcWSClient client = new CalcWSClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
System.out.println("Retrieving the port from the following service: " + service);
Calc port = service.getCalcWSPort();
System.out.println("Invoking the add operation on the port.");
int response = port.add(1, 3);
System.out.println(response);
} catch(Exception e) {
e.printStackTrace();
}
}
} </span>
<span style="font-size: small;"></span></pre><span style="font-size: small;"></span><span style="font-size: small;"><br />
</span></div><div style="font-family: Arial,Helvetica,sans-serif; margin-bottom: 0in;"><span style="font-size: small;">The picture below shows the results on eclipse console.</span><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDlmCCj8aVgFaOkpDPi4ht8XbVBN_1T8Fio8OQV_jXdLcfKvpoxT6UFZLgyvfSfELtV1Uz3GZvSW7xvXOaLkOdWODRLZ-_QuzZPowkp7Z1oqm1GIOgLwbN7Kt2q9DLiQNm_fj5d2cIUwI/s1600/Screenshot-Java+-+WS+SOAP-test-org-webservice-server-CalcWSClient.java+-+Eclipse+.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="243" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDlmCCj8aVgFaOkpDPi4ht8XbVBN_1T8Fio8OQV_jXdLcfKvpoxT6UFZLgyvfSfELtV1Uz3GZvSW7xvXOaLkOdWODRLZ-_QuzZPowkp7Z1oqm1GIOgLwbN7Kt2q9DLiQNm_fj5d2cIUwI/s320/Screenshot-Java+-+WS+SOAP-test-org-webservice-server-CalcWSClient.java+-+Eclipse+.png" width="320" /></a></div></div><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"> </span></h2><h2 class="western" style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">Developing a RESTful web service</span></h2><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">Using JAX-RS the API for REST based web service. There are a couple frameworks out there. The popular ones are Jersey and Apache CXF. This example was tested using Jersey 1.2 (you can download from <a href="http://www.blogger.com/%20http://download.java.net/maven/2/com/sun/jersey/">jersey</a>), the Sun's open source implementation.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">Just with few annotations and some associated classes, Jersey lets you expose POJOs as web services. It also provides support for JSON.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">You can find all the literature and code at <a href="https://jersey.dev.java.net/nonav/documentation/1.2/user-guide.html#chapter_deps">jersey docs.</a></span></div><br />
<div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">All the annotations are defined in jsr311-api.jar.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">Here is a nice <a href="http://blogs.sun.com/enterprisetechtips/entry/implementing_restful_web_services_in">article</a> about implementing REST in java. So in short RESTful architecture is about exposing resources and it's operations.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">A resource class is a Java class with JAX-RS annotations to identify itself as a web resource.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">The Root resource class used here is CalcWSImpl.java. It has three resource methods, add, div and list. </span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">The code listing is shown below.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"></span><br />
<pre><span style="font-size: small;"></span><span style="font-size: small;">package org.rest.examples;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.json.JSONException;
import org.json.JSONObject;
//The Java class will be hosted at the URI path "/calc"
@Path("/calc/")
public class CalcWSImpl {
// The Java method will process HTTP GET requests
@GET
@Path("add/{input1}/{input2}")
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String add(@PathParam("input1") int a, @PathParam("input2") int b) {
return String.valueOf(a+b);
}
@GET
@Path("list/")
@Produces("application/json")
public String list() throws JSONException {
JSONObject list = new JSONObject();
list.put("mercedes", "20");
list.put("porsche", "25");
list.put("audi", "32");
list.put("lexus", "35");
return list.toString();
}
// The Java method will process HTTP GET requests
@GET
@Path("div/{input1}/{input2}")
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String div (@PathParam("input1") int a, @PathParam("input2") int b) {
return String.valueOf(a/b);
}
}</span>
<span style="font-size: small;"></span></pre><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">Some brief explanations of the annotations used...</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #646464; font-size: small;"><span style="color: blue;">@Path</span> </span><span style="color: #646464; font-size: small;">the URI path for a class or method. This is path you add to the base URL for the webservice to access the resource. For example </span><span style="color: black; font-size: small;">http://localhost:9998/calc/</span><span style="color: navy; font-size: small;">list</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #646464; font-size: small;"><span style="color: blue;">@GET</span> method will process HTTP GET methods</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #646464; font-size: small;"><span style="color: blue;">@Produces</span> MIME media type of objects returned by a method</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #646464; font-size: small;"><span style="color: blue;">@PathParam</span> parameters in the URI path like in the following URI </span><span style="font-size: small;"><a href="http://localhost:9998/calc/add/6/7/">http://localhost:9998/calc/add/6/7/</a></span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #646464; font-size: small;">6 and 7 are parameters as you can see from the @Path annotation </span><span style="color: #2a00ff; font-size: small;">add/{input1}/{input2} </span><span style="color: black; font-size: small;">for the add method.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">To find all the JSR-311 annotations refer to <a href="https://jsr311.dev.java.net/nonav/releases/1.1/index.html">JAX-RS annotations</a></span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">Now to get going we would need the following jars</span></div><div style="color: #444444; font-family: Arial,Helvetica,sans-serif;"><b><span style="font-size: x-small;">asm-3.1.jar</span></b></div><div style="color: #444444; font-family: Arial,Helvetica,sans-serif;"><b><span style="font-size: x-small;">jersey-bundle-1.2.jar</span></b></div><div style="color: #444444; font-family: Arial,Helvetica,sans-serif;"><b><span style="font-size: x-small;">jsr311-api-1.11.jar</span></b></div><div style="color: #444444; font-family: Arial,Helvetica,sans-serif;"><b><span style="font-size: x-small;">json.jar</span></b></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">You can download them from </span><span style="font-size: small;"><a href="http://download.java.net/maven/2/com/sun/jersey/">here.</a></span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><br />
</span> <br />
<span style="font-size: small;">We are ready to launch the webservice. Let's look at Main.java </span><br />
<span style="font-size: small;"></span><br />
<pre><span style="font-size: small;"></span><span style="font-size: small;">package org.rest.examples;
import com.sun.net.httpserver.HttpServer;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServerFactory.create("http://localhost:9998/");
server.start();
System.out.println("Server running");
System.out.println("Visit: http://localhost:9998/list");
System.out.println("Hit return to stop...");
System.in.read();
System.out.println("Stopping server");
server.stop(0);
System.out.println("Server stopped");
}
} </span>
<span style="font-size: small;"></span></pre><span style="font-size: small;"></span><span style="font-size: small;">Run Main.java from eclipse as a java application. You have a running web service.</span><br />
<span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">Let's try some examples here using curl.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">~$ curl http://localhost:9998/calc/add/6/7/ </span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">13</span><br />
<span style="font-size: small;"> </span><span style="color: black; font-size: small;">~$</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">~$ curl http://localhost:9998/calc/list </span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">{"lexus":"35","audi":"32","porsche":"25","mercedes":"20"}</span><br />
<span style="color: black; font-size: small;">~$ </span><br />
<span style="color: black; font-size: small;"> </span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><b>Deploying...</b></span><br />
<span style="font-size: small;"><span style="font-family: Arial,Helvetica,sans-serif;">Let's deploy to a servlet container other than Tomcat :-) , Jetty for instance.</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"> here is what the web.xml should look like.</span></span><br />
<span style="font-size: small;"><b> </b> </span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"></span><br />
<pre><span style="color: black;"><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><?</span><span style="color: #3f7f7f;">xml</span>
<span style="color: #7f007f;">version</span><span style="color: #1a1a1a;">=</span><span style="color: #2a00ff;"><i>"1.0"</i></span>
<span style="color: #7f007f;">encoding</span><span style="color: #1a1a1a;">=</span><span style="color: #2a00ff;"><i>"UTF-8"</i></span><span style="color: teal;">?></span></span></span></span>
<div align="LEFT" style="margin-bottom: 0in;"><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><!</span><span style="color: #3f7f7f;">DOCTYPE</span>
<span style="color: teal;">web-app</span> <span style="color: grey;">PUBLIC</span>
<span style="color: teal;">"-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN"</span>
<span style="color: #3f7f5f;">"http://java.sun.com/dtd/web-app_2_3.dtd"</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">web-app</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: #1a1a1a;">
</span><span style="color: teal;"><</span><span style="color: #3f7f7f;">servlet</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">servlet-name</span><span style="color: teal;">></span><span style="color: #1a1a1a;">JerseyTest</span><span style="color: teal;"></</span><span style="color: #3f7f7f;">servlet-name</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">servlet-class</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
<span style="font-family: Monospace;"><span style="font-size: x-small;">com.sun.jersey.spi.container.servlet.ServletContainer</span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"></</span><span style="color: #3f7f7f;">servlet-class</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">init-param</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
<span style="font-family: Monospace;"><span style="font-size: x-small;"> </span><span style="color: teal;"><</span><span style="color: #3f7f7f;">param-name</span><span style="color: teal;">></span><span style="color: #1a1a1a;">com.sun.jersey.config.property.packages</span><span style="color: teal;"></</span><span style="color: #3f7f7f;">param-name</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
<span style="font-family: Monospace;"><span style="font-size: x-small;"> </span><span style="color: teal;"><</span><span style="color: #3f7f7f;">param-value</span><span style="color: teal;">></span><span style="color: #1a1a1a;">org.rest.examples</span><span style="color: teal;"></</span><span style="color: #3f7f7f;">param-value</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
<span style="font-family: Monospace;"><span style="font-size: x-small;"> </span><span style="color: teal;"></</span><span style="color: #3f7f7f;">init-param</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">load-on-startup</span><span style="color: teal;">></span><span style="color: #1a1a1a;">1</span><span style="color: teal;"></</span><span style="color: #3f7f7f;">load-on-startup</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"></</span><span style="color: #3f7f7f;">servlet</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">servlet-mapping</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">servlet-name</span><span style="color: teal;">></span><span style="color: #1a1a1a;">JerseyTest</span><span style="color: teal;"></</span><span style="color: #3f7f7f;">servlet-name</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"><</span><span style="color: #3f7f7f;">url-pattern</span><span style="color: teal;">></span><span style="color: #1a1a1a;">/*</span><span style="color: teal;"></</span><span style="color: #3f7f7f;">url-pattern</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="color: #1a1a1a;">
</span><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"></</span><span style="color: #3f7f7f;">servlet-mapping</span><span style="color: teal;">></span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"></</span><span style="color: #3f7f7f;">web-app</span><span style="color: teal;">></span></span></span>
<span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"> </span></span></span></div><div align="LEFT" style="margin-bottom: 0in;"><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"></span></span></span></div></pre><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: teal;"> </span></span></span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">With jersey configured as a servlet. </span> <style type="text/css">
<!--
@page { margin: 0.79in }
P { margin-bottom: 0.08in }
-->
</style> <br />
<div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">All we are doing here is providing the package name (“ <span style="color: #1a1a1a;">org.rest.examples”)</span> which has resource classes.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><style type="text/css">
<!--
@page { margin: 0.79in }
P { margin-bottom: 0.08in }
-->
</style> </div><span style="color: black; font-size: small;">We need another class to instantiate Jetty! Yes we are going to start an embedded instance programmatically. Here is RunServlet.java </span><br />
<span style="color: black; font-size: small;"></span><br />
<pre><span style="color: black; font-size: small;"></span><span style="color: black; font-size: small;">package org.rest.examples;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;
public class RunServlet {
public static void main(String[] args) throws Exception {
Server server = new Server(8081);
WebAppContext context = new WebAppContext();
context.setParentLoaderPriority(true);
context.setDescriptor("./src/web/WEB-INF/web.xml");
context.setResourceBase("./src/web");
server.setHandler(context);
server.start();
server.join();
}
} </span>
<span style="color: black; font-size: small;"></span></pre><span style="color: black; font-size: small;"><br />
</span><span style="color: black; font-size: small;">Need the following Jetty jars to be in the classpath.</span><br />
jetty-6.1.18.jar<br />
jetty-util-6.1.1.18.jar<br />
servlet-api-2.5-20081211.jar<br />
<br />
<div style="font-family: Arial,Helvetica,sans-serif;"><style type="text/css">
<!--
@page { margin: 0.79in }
P { margin-bottom: 0.08in }
-->
</style> </div><span style="color: black; font-size: small;">On running RunServlet, see the console output below. The JerseyTest servlet scans the package org.rest.examples and finds HelloWorld and CalcWSImpl resource classes.</span><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUzG8d-Zx0Jgo2K4O0eDjOiUTiLM8HKeUfjobmrRgdjmuti8HWlHWra5qNWCO8QvUXVrA9qhDxUMdVlacFLY_wj8RvB9dVlGulfAMpG0UWU4CwlneCAPn5ycaCH_KQWDvPqzCQkmfoaFY/s1600/Screenshot-Java+-+Running+Jetty+server+.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="243" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUzG8d-Zx0Jgo2K4O0eDjOiUTiLM8HKeUfjobmrRgdjmuti8HWlHWra5qNWCO8QvUXVrA9qhDxUMdVlacFLY_wj8RvB9dVlGulfAMpG0UWU4CwlneCAPn5ycaCH_KQWDvPqzCQkmfoaFY/s320/Screenshot-Java+-+Running+Jetty+server+.png" width="320" /></a></div><br />
<style type="text/css">
<!--
@page { margin: 0.79in }
P { margin-bottom: 0.08in }
-->
</style> <br />
<div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">Now let's see how it works.</span> <span style="color: black; font-size: small;"><br />
</span></div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirfMDyOq5CZNv0_PQ7jNKTSbcTt4ocsE-l-Numzf-3It2Y6Yfd1CfqNUnMywHfDemkEHTOXfPG7J34dJDt4tnATyXtofjwJhyQHqGzsF5PAbEtsH_eNv92YY8GV5ZltcxxERx80UnANv4/s1600/Screenshot-+REST+Jetty+test:+%7E.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="222" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirfMDyOq5CZNv0_PQ7jNKTSbcTt4ocsE-l-Numzf-3It2Y6Yfd1CfqNUnMywHfDemkEHTOXfPG7J34dJDt4tnATyXtofjwJhyQHqGzsF5PAbEtsH_eNv92YY8GV5ZltcxxERx80UnANv4/s320/Screenshot-+REST+Jetty+test:+%7E.png" width="320" /></a></div><span style="color: black; font-size: small;">All you need is just a few jars and resource classes and you are done creating a RESTful web service! Couldn't be easier than this.</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><br />
</span> </div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">Conclusions:</span></div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: black; font-size: small;">As you can see, with frameworks like Jersey writing/deploying/testing REST based web services is much simpler and straight forward than it's SOAP equivalent. But of course SOAP has many more features that you may or may not need. So depending on your internal computing environment and external client needs you can choose SOAP or put everything to REST!</span></div>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com8tag:blogger.com,1999:blog-4098885671918163159.post-25495422154208008712010-02-24T16:48:00.000-08:002010-02-24T16:48:13.929-08:00Finding process/program using a portMany a times you would get a "Bind Exception", "Address already in use" kind of errors and need to find the process that's using the port. For example say the port is 8080. The following command will get you the pid and program name as well.<br />
<br />
<span style="color: blue;">netstat -nlept | grep "8080"</span><br />
<br />
The result might look something like this...<br />
<br />
<span style="color: orange;">Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name </span><br />
<span style="color: blue;">tcp6 0 0 :::8080 :::* LISTEN 1000 994652 4024/java </span> <br />
Where 4024 is the pid and it's a java app.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-13502804437570160182010-02-02T15:34:00.000-08:002010-02-02T16:23:31.205-08:00Upgraded process manager for Linux<div class="separator" style="clear: both; text-align: center;"><a href="http://htop.sourceforge.net/index.php?page=main" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="216" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO5SIc3ALrBcC47DSyAXOYWld3LFB_YtRVd8dly0LMHC-Z8dygjyZtnyaVHyvbhsGYkVZ5I01ZAUk8mAsn2JUERTjYlv37OjGhjSi-HlotCW3i-U8aiA-iys6kpxU4CLLoW8IswcAb17c/s320/htop-0.5.png" width="320" /></a></div>Check this out <a href="http://htop.sourceforge.net/index.php?page=main">htop</a> which is a more interactive and intuitive upgrade of the good old 'top' has many pluses to it like Color themes, mouse enabled and so on.Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-31726249237166118702010-01-04T11:08:00.000-08:002010-01-05T15:48:01.398-08:00XML parsing in Java and GroovyXML is everywhere. Somebody said that XML is like violence. if it doesn't work for you then you are not using enough of it! Well like it or not you have to deal with XML everywhere. To say that XML handling in java is not easy is an understatement. What if you have to deal with SAX, DOM parsers, etc. Well there are some libraries out there which make the job a little easier. Like XOM, XSTREAM, etc. But what if you want to read an XML config file into a Java object?<br />
<br />
XSTREAM has a fairly simple way of doing that. Here is an <a href="http://xstream.codehaus.org/tutorial.html">example</a>.<br />
<br />
One other option is to look at the many jvm based languages like groovy, scala, jruby, etc. They all handle XML super easy. Let's see How easy it is in Groovy.<br />
Groovy has two APIs XMLParser and XMLSlurper for dealing with XML.<br />
<br />
Groovy lets you easily cut to the metadata instead of going through each node and getting the child and more.<br />
Using a scripting language gives you a tradeoff for complexity and since it is run by the same jvm you don't loose any performance either. A call to groovy functionality can be embedded in java or invoked through a shell. That way any scripting language can be used.<br />
<br />
The example here uses a simple xml data file (Cars.xml) shown below.<br />
<br />
<pre><records>
<carList>
<car name='HSV Maloo' make='Holden' year='2006'>
<country>Australia</country>
<record type='speed'>Production Pickup Truck with speed of 271kph</record>
</car>
<car name='P50' make='Peel' year='1962'>
<country>Isle of Man</country>
<record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
</car>
<car name='Royale' make='Bugatti' year='1931'>
<country>France</country>
<record type='price'>Most Valuable Car at $15 million</record>
</car>
</carList>
</records>
</pre><br />
The data structure to represent the XML data is defined in <br />
Records.java.<br />
<br />
<pre><records><carlist>package org.xml.example;
import java.util.List;
public class Records {
private List<car> carList = null;
public static class Car {
String name;
String make;
String year;
Country country;
Record record;
public static class Record {
String type;
String info;
public String toString(){
return "Record type:" + type + ", info:" + info;
}
}
public static class Country {
String name;
public String toString(){
return "Country: " + name;
}
}
public String toString(){
return "name: " + name + ", make:" + make + ", year:" + year + " "
+ country + " " + record;
}
}
public List<car> getCars() {
return carList;
}
public String toString(){
StringBuilder sb = new StringBuilder();
for (Car car : carList) {
sb.append(car.toString());
sb.append(System.getProperty("line.separator"));
}
return sb.toString();
}
}</car></car></carlist></records></pre><br />
The main program is XMLParseExample.java.<br />
<br />
<pre>package org.xml.example;
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.thoughtworks.xstream.XStream;
public class XMLParseExample {
public static void main(String[] args) throws FileNotFoundException {
// parseXMLinGroovy();
parseXMLinXstream();
}
private static void parseXMLinXstream() throws FileNotFoundException {
XStream xstream = new XStream();
xstream.aliasType("records", Records.class);
xstream.aliasType("car", Records.Car.class);
xstream.useAttributeFor(Records.Car.class, "name");
xstream.useAttributeFor(Records.Car.class, "make");
xstream.useAttributeFor(Records.Car.class, "year");
xstream.aliasType("country", Records.Car.Country.class);
xstream.aliasType("record", Records.Car.Record.class);
xstream.useAttributeFor(Records.Car.Record.class, "type");
Records records = (Records)xstream.fromXML(new FileReader("/home/csrinivasan/Documents/Nigeria/Cars.xml"));
String xml = xstream.toXML(records);
System.err.println(xml);
}
private static void parseXMLinGroovy() {
String[] roots = new String[] { "./scripts/" };
GroovyScriptEngine gse;
try {
gse = new GroovyScriptEngine(roots);
Binding binding = new Binding();
gse.run("ParseXML.groovy", binding);
} catch (Exception e) {
e.printStackTrace();
}
}
}
</pre><br />
The groovy script is ParseXML.groovy.<br />
<br />
<pre>import org.xml.example.*;
import org.xml.example.Records.Car.*
class ParseRecords {
private List cars
ParseRecords () {
cars = new ArrayList()
}
def parseRecords() {
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader()
def data = sysClassLoader.getResource("Cars.xml").text
def xmlSlurper = new XmlParser()
def records = xmlSlurper.parseText(data)
Records recs = new Records()
records.carList.each {
recs.carList = new ArrayList<records.car>()
it.car.each {
Records.Car car = new Records.Car()
car.name = it.attribute("name")
car.make = it.attribute("make")
car.year = it.attribute("year")
car.country = new Records.Car.Country()
car.country.name = it.country.text()
car.record = new Records.Car.Record()
it.record.each {
car.record.type = it.attribute("type")
}
car.record.info = it.record.text()
recs.carList.add(car)
}
}
println recs
}
ParseRecords algConfig = new ParseRecords()
algConfig.parseRecords()</records.car>
</pre>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com1tag:blogger.com,1999:blog-4098885671918163159.post-24478203019423270002009-10-13T16:42:00.000-07:002009-10-14T09:33:57.790-07:00Lookup Source Filter option in Informatica<div class="separator" style="clear: both; text-align: center;"><br />
</div><i><span style="font-size: x-small;"> <span style="font-size: small;">Informatica version 8.6.1 has a feature where Lookup Source filter could be used. This feature wouldn't work if there are multiple lookups with different source filter based on the same source table that uses the same lookup cache. However, this could be overcome by writing a sql override</span></span></i><br />
<i><span style="font-size: x-small;"><span style="font-size: small;">as shown below..... </span><br />
</span></i><br />
<br />
<div><br />
</div><div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEX41c-wH135AF4U4448s1Bk7uViAWssfKoQQky-0WJeM1Wt0PZzTjoCpTl5bUL72KBUmiAVqG4HMnlI8s8kVdgiyphMYySs1OMmoOkDBwkYdf7MDxfF0e3aQuIrKz54RN-lOXig2uBb0/s1600-h/Filter+Expression+lookup.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEX41c-wH135AF4U4448s1Bk7uViAWssfKoQQky-0WJeM1Wt0PZzTjoCpTl5bUL72KBUmiAVqG4HMnlI8s8kVdgiyphMYySs1OMmoOkDBwkYdf7MDxfF0e3aQuIrKz54RN-lOXig2uBb0/s320/Filter+Expression+lookup.jpg" /></a><br />
</div>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-28963652725072462562009-10-04T13:51:00.000-07:002009-10-05T09:38:50.989-07:00Updating default database in Sybase version 9 and above<span style="font-family: Arial,Helvetica,sans-serif; font-size: small;">If multiple repositories are built in the same database platform and they all reside on the same server it's a good practice to check what the default database is, before installing or upgrading. </span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif; font-size: small;"> Say for example you have <b>Dev </b>and <b>Test </b>repo set on <<server_name>> server. If <b>Dev </b>repo is installed and now you would like to install Informatica in <b>Test </b>repo. You would have to issue the following command to update the default database in Sybase.</server_name></span><br />
<br />
<div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;">For example in Rapid SQL...</span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
<pre>login with uid to old repository database(say old is Dev)
sp_modifylogin uid,defdb, TEST
go
sp_displaylogin uid
</pre><br />
</div>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-5629386724615755602009-09-21T16:42:00.000-07:002009-09-22T22:03:45.014-07:00Bare bones JMXSome times you wish there was a way to monitor objects in a app while it is running to check for potential problems, or just to troubleshoot a problem. Well JMX can do that and much more. JMX can also do notification for example if a Queue is full or database is out of space, things like that. But I wanted to get started with just being able to monitor an object whenever I wished to. <br />
<br />
The example used here is an application which is basically a Socket server serving clients with data from a queue. The use case here is to look at the app and query for number of clients connected or number of objects in the queue. pretty simple huh?<br />
<br />
Here is the code for the app.<br />
We will use JConsole to connect to the app process and invoke the operations defined.<br />
<br />
Now you might say that this can be done by logging the information (queue size and number of clients). True but like I said earlier using JMX gives you a way to check on things at runtime. also you could have an operation to clear the queue for a secure user (how? check for later posts!). Also why do you want to waste cpu cycles and disk space to keep logging stuff which you may or not look for.<br />
<br />
The Java artifacts you need are an interface that declares the methods that you want to expose, a class that implements that interface. That's it. TestJMX.java is just to create and register a JMX bean.<br />
<br />
Interface....<br />
<pre><span style="font-size: x-small;">package com.example.testJMX;
public interface DataSocketServerMBean {
// operations
public int numberofClients();
public int dataQueueSize();
}
</span></pre><br />
The implementor...<br />
<br />
<pre>package com.example.testJMX;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.log4j.Logger;
public class DataSocketServer extends Thread implements DataSocketServerMBean {
private final static Logger log = Logger.getLogger(DataSocketServer.class);
private final static int LISTENING_PORT = 3000;
private ServerSocket weatherDataServer;
public boolean shutdown = false;
protected LinkedBlockingQueue outputDataQueue;
private ConnectToNewClient newClient;
private boolean byteData;
private int listeningPort;
public DataSocketServer() throws Exception {
weatherDataServer = new ServerSocket(LISTENING_PORT);
log.info("Server listening on port " + LISTENING_PORT);
this.start();
//start a new thread for writing data to each client socket
newClient = new ConnectToNewClient();
}
public DataSocketServer(LinkedBlockingQueue dataQueue) {
this.outputDataQueue = dataQueue;
try {
weatherDataServer = new ServerSocket(LISTENING_PORT);
} catch (IOException e) {
log.error("Error creating ServerSocket :", e);
}
log.info("Server listening on port " + LISTENING_PORT);
this.setName("WeatherDataSocketServerfor" + listeningPort);
this.start();
//start a new thread for writing data to each client socket
newClient = new ConnectToNewClient();
}
public void run() {
while (true) {
try {
log.debug("Waiting for connections.");
if (weatherDataServer != null){
Socket client = weatherDataServer.accept();
log.info("Accepted a connection from: " + client.getInetAddress());
newClient.addClient(new ClientSocket(client));
}
Thread.sleep(500);
} catch (InterruptedException ix) {
log.error("Shutting down socket server", ix);
newClient.interrupt();
break;
} catch (Exception e) {
log.error("Error accepting connections", e);
}
}
}
public void ConnectToClient(Socket clientSocket) {
}
/**
* Helper class to handle each client as a separate thread.
*/
class ConnectToNewClient extends Thread {
// DECLARE
private List<clientsocket> clientSockets = Collections.synchronizedList(new ArrayList<clientsocket>());
// INIT
public ConnectToNewClient() {
this.setName("ConnectToNewClientFor" + DataSocketServer.this.listeningPort);
this.start();
}
public void addClient(ClientSocket clientSocket) {
clientSockets.add(clientSocket);
}
public ConnectToNewClient(ArrayList<clientsocket> clientList) {
this.clientSockets = clientList;
this.setName("ConnectToNewClientFor" + DataSocketServer.this.listeningPort);
this.start();
}
// ACTION
public void run() {
Object qobj = null;
try {
while (true) {
while (clientSockets.size() > 0 && (qobj = DataSocketServer.this.outputDataQueue.peek()) != null) {
log.info("Number of clients =" + clientSockets.size());
//loop through each client and write to it.
for (int i = 0; i < clientSockets.size(); i++) {
ClientSocket clientSocket = null;
try {
//check if the socket is active.
clientSocket = clientSockets.get(i);
if (clientSocket.checkConnection()) {
DataSocketServer.log.info("Sending data to client "
+ clientSocket.getInetAddress());
if (DataSocketServer.this.byteData && qobj instanceof byte[])
clientSocket.writeBytes((byte[]) qobj);
else
clientSocket.writeObject(qobj);
DataSocketServer.this.outputDataQueue.remove(qobj);
} else {
//remove client from list
DataSocketServer.log.info("Removing client from list : "
+ clientSocket.getInetAddress());
clientSocket.close();
clientSockets.remove(clientSocket);
clientSocket = null;
}
} catch (RuntimeException e) {
DataSocketServer.log.error("Error sending data to client ", e);
//the client socket may be dead. remove it
clientSockets.remove(clientSocket);
clientSocket = null;
}
}
}
Thread.sleep(1000);
}
} catch (Exception e) {
DataSocketServer.log.error("Error :", e);
DataSocketServer.log.error("outputDataQueue =" + DataSocketServer.this.outputDataQueue);
DataSocketServer.log.error("qobj = " + qobj);
}</clientsocket></clientsocket></clientsocket>
}
}
/**
* Socket wrapper for each client.
*/
class ClientSocket {
// DECLARE
private Socket socket = null;
private ObjectInputStream ois = null;
private OutputStream sos = null;
private ExecutorService executor;
private ObjectOutputStream oos = null;
private BufferedOutputStream bos = null;
// INIT
public ClientSocket(Socket clientSocket) {
this.socket = clientSocket;
try {
sos = socket.getOutputStream();
if (DataSocketServer.this.byteData)
bos = new BufferedOutputStream(sos);
else
oos = new ObjectOutputStream(new BufferedOutputStream(sos));
} catch (Exception e1) {
try {
socket.close();
} catch (Exception e) {
log.error("Error creating client socket :" , e);
}
}
}
public void close() {
try {
socket.close();
if (oos != null)
oos.close();
if (bos != null)
bos.close();
} catch (Exception e) {
}
socket = null;
}
public void writeObject(Object o) {
try {
oos.flush();
if (o instanceof byte[]){
byte[] buf = (byte[])o;
bos.write(buf);
} else {
oos.writeObject(o);
}
oos.flush();
} catch (IOException e) {
// close socket.. connection maybe lost. The only way to find out is when
//we get a socketexception!!!!!!
log.error("Error writing to socket. closing socket...", e);
try {
socket.close();
} catch (IOException e1) {
}
socket = null;
} catch (Exception ex) {
log.error("Error writing data to client socket for client " + this.getInetAddress(),ex);
}
}
// UTIL
public boolean checkConnection() {
if (socket != null && !socket.isClosed() && socket.isConnected() && socket.isBound())
return true;
return false;
}
// ACCESS
public String getInetAddress() {
if (socket != null)
return socket.getInetAddress().getHostName();
return "";
}
public Socket getSocket() {
return socket;
}
public void writeBytes(byte[] buf) {
try {
if (socket != null) {
log.info("writing data " + new String(buf));
bos.flush();
bos.write(buf);
bos.flush();
}
} catch (IOException e) {
// close socket.. connection maybe lost. The only way to find out is when
//we get a socketexception!!!!!!
log.error("Error writing to socket. listening port: " + DataSocketServer.this.listeningPort + " closing socket...", e);
try {
socket.close();
} catch (IOException e1) {
}
socket = null;
} catch (Exception ex) {
log.error("Error writing data to client socket for client " + this.getInetAddress(),ex);
}
}
}
public int dataQueueSize() {
if (outputDataQueue != null)
return outputDataQueue.size();
return 0;
}
public int numberofClients() {
if (newClient != null)
return newClient.clientSockets.size();
return 0;
}
}</pre><br />
and finally the TestJMX class... <br />
<pre><span style="font-size: x-small;">package com.example.testJMX;
import java.lang.management.*;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import javax.management.*;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
// Get the Platform MBean Server
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
LinkedBlockingQueue<string> testQueue = new LinkedBlockingQueue<string>();
JSONObject sampleObj = new JSONObject();
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
sampleObj.put("The current time on host machine", new Date());
testQueue.add(sampleObj.toString());
// Construct the ObjectName for the MBean we will register
ObjectName name = new ObjectName("com.example.testJMX:type=DataSocketServer");
// Create the jmx MBean
DataSocketServer mbean = new DataSocketServer(testQueue);
// Register the jmx MBean
mbs.registerMBean(mbean, name);
// Wait forever
System.out.println("Waiting forever...");
Thread.sleep(Long.MAX_VALUE);
}
}</string></string></span> </pre><br />
Now run the TestJMX java class.<br />
Launch JConsole and connect to the process<br />
Invoking dataQueueSize and numberofClients operations...<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-CisSPf3SP9tjc3_YPVOFUZg1CfEZD1UW2dsRzxWYNfdHHGDGqpUdAagkHbEItO8WH04GJRYL2a1JWz8wh_xzbSeRhiLXGIv4WDIh16KZdCZAweGFCBINIcixkpVYg47lF7ADL1TGW2s/s1600-h/Screenshot-JConsole+dataQueueSize.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-CisSPf3SP9tjc3_YPVOFUZg1CfEZD1UW2dsRzxWYNfdHHGDGqpUdAagkHbEItO8WH04GJRYL2a1JWz8wh_xzbSeRhiLXGIv4WDIh16KZdCZAweGFCBINIcixkpVYg47lF7ADL1TGW2s/s200/Screenshot-JConsole+dataQueueSize.png" /></a><br />
</div><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTswzuMbzjNAyE1ERGI-BNf5Cjk_WJgxyR7QaRY8ufV9oDRHTrPLsxMZNwhOHQ5srwNu09UHNeYnD1zHeAgTQmtAtsFDwRqW3PHobYG8gnLVor5pxbReqNm6fx2i1IfBADs5ucJ0kq9sE/s1600-h/Screenshot-JConsole+numberofClients.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTswzuMbzjNAyE1ERGI-BNf5Cjk_WJgxyR7QaRY8ufV9oDRHTrPLsxMZNwhOHQ5srwNu09UHNeYnD1zHeAgTQmtAtsFDwRqW3PHobYG8gnLVor5pxbReqNm6fx2i1IfBADs5ucJ0kq9sE/s200/Screenshot-JConsole+numberofClients.png" /></a><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
connect to the socket server using telnet/netcat or any of those tools.<br />
On linux ...<br />
netcat localhost 4233<br />
Now invoking the same operations again gives you the following results...<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnPCdZnVGyPUXeUXB6Uhxr1UTJ_lF-4XpNSQgSLWhZVoxFuXz2QTvrOIOl4QNmBoVTrRZjbDgaUDkCFEJ4ofLh3kJFrNqc4YGze8onR3bOoHHBQMLo1oA769mi-Lclr2_GeUdvXBwrsDw/s1600-h/Screenshot-JConsole+numberofclients-+1client.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnPCdZnVGyPUXeUXB6Uhxr1UTJ_lF-4XpNSQgSLWhZVoxFuXz2QTvrOIOl4QNmBoVTrRZjbDgaUDkCFEJ4ofLh3kJFrNqc4YGze8onR3bOoHHBQMLo1oA769mi-Lclr2_GeUdvXBwrsDw/s200/Screenshot-JConsole+numberofclients-+1client.png" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoVvgBWlE28xnRXqLB7VmjvKrR4DD-D_quqWYLDfC3FMTIp7dMzcSwIk7WbvxxVvrnK591FEVyQL8SCtKNJqSP_3dmmuq_7OnQDKoXf4QWyhgr7cHJvgDMeShcAFRmY1CuWD7wHTUNnRk/s1600-h/Screenshot-JConsole+numberofclients-+1client.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoVvgBWlE28xnRXqLB7VmjvKrR4DD-D_quqWYLDfC3FMTIp7dMzcSwIk7WbvxxVvrnK591FEVyQL8SCtKNJqSP_3dmmuq_7OnQDKoXf4QWyhgr7cHJvgDMeShcAFRmY1CuWD7wHTUNnRk/s200/Screenshot-JConsole+numberofclients-+1client.png" /></a><br />
</div>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-44421913384646136592009-09-08T13:51:00.000-07:002009-09-15T09:11:49.650-07:00Capturing jvm Thread Dump in linuxMany a times it has happened that a Java app froze and I needed to take a thread dump to look for deadlocks or any other issues. In this case you need to perform a Thread dump on a running process. Using kill command with argument 3 produces a Thread dump, but the output goes to the stdout of the process, Which could be a console on your favorite IDE. What if the IDE is frozen too?<br />
<br />
Well in that case using the /proc command comes in handy.<br />
<span style="color: blue; font-family: Arial,Helvetica,sans-serif;">ls /proc</span> lists all the processes running on the system currently.<br />
<br />
<pre>my-desktop:~$ ls /proc
1 2133 24921 3132 3615 3744 fb partitions
10 2134 25 3135 3616 4 filesystems sched_debug
11 2141 25214 3139 3618 4763 fs schedstat
12 2142 26 3159 3629 5 interrupts scsi
12087 22 26408 3163 3633 5587 iomem self
13 2213 26426 3165 3639 6 ioports slabinfo
14 2258 2881 3183 3645 7 irq stat
1495 2281 29 3184 3654 722 kallsyms swaps
15 2283 2913 3198 3656 8 kcore sys
16 22886 2935 32 3658 856 key-users sysrq-trigger
16653 22894 2938 3211 3664 9 kmsg sysvipc
17 2306 2939 3240 3669 acpi kpagecount timer_list
18 23177 294 33 3671 asound kpageflags timer_stats
1865 23186 3 3316 3673 buddyinfo latency_stats tty
19 23191 30 3344 3674 bus loadavg uptime
19405 23194 3002 34 3679 cgroups locks version
19406 23197 30137 3466 3683 cmdline mdstat version_signature
19407 2330 3032 35 3685 cpuinfo meminfo vmallocinfo
19445 23701 3077 3536 3691 crypto misc vmstat
19988 2389 3080 3549 3694 devices modules zoneinfo
2 24 3081 36 3707 diskstats mounts
20 2431 309 3606 3724 dma mtrr
20798 24772 3099 3609 3728 driver net
21 24803 31 3610 3740 execdomains pagetypeinfo</pre><br />
Say we want to take a thread dump of a java process (PID 24772). <span style="color: blue; font-family: Arial,Helvetica,sans-serif;"> </span><br />
<span style="color: blue; font-family: Arial,Helvetica,sans-serif;">/proc/24772/fd/1</span> gets you the stdout interface of the Java process where 1 is the file-descriptor for stdout.<br />
You can capture the output to stdoutby issuing the command<br />
<span style="color: blue;">cat /proc/24772/fd/1</span><br />
<span style="color: blue;"><span style="color: black;">Now to generate a thread dump issue this from another session<br />
</span></span><br />
<span style="color: blue;"><span style="color: black;">kill -3 24772</span></span><br />
<span style="color: blue;"><span style="color: black;">You should see the thread dump output like this..</span> <br />
</span><br />
<pre>my-desktop:~$ cat /proc/24772/fd/1
2009-09-08 10:41:19
Full thread dump Java HotSpot(TM) Client VM (14.2-b01 mixed mode, sharing):
"Low Memory Detector" daemon prio=10 tid=0x09060c00 nid=0x60d0 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"CompilerThread0" daemon prio=10 tid=0x0905dc00 nid=0x60ce waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" daemon prio=10 tid=0x0905c400 nid=0x60cd waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" daemon prio=10 tid=0x0904dc00 nid=0x60cc in Object.wait() [0xb5326000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x8bcd8918> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
- locked <0x8bcd8918> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)
"Reference Handler" daemon prio=10 tid=0x09049400 nid=0x60ca in Object.wait() [0xb5377000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x8bcd89a0> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:485)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
- locked <0x8bcd89a0> (a java.lang.ref.Reference$Lock)
"main" prio=10 tid=0x09023400 nid=0x60c6 runnable [0xb75e9000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390)
- locked <0x8b8364b8> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept(ServerSocket.java:453)
at java.net.ServerSocket.accept(ServerSocket.java:421)
at org.coastal.log4jext.SocketServerAsWinService.main(SocketServerAsWinService.java:41)
"VM Thread" prio=10 tid=0x09047800 nid=0x60c9 runnable
"VM Periodic Task Thread" prio=10 tid=0x09062800 nid=0x60d1 waiting on condition
JNI global references: 945
Heap
def new generation total 960K, used 488K [0x8b7d0000, 0x8b8d0000, 0x8bcb0000)
eden space 896K, 47% used [0x8b7d0000, 0x8b83a3b8, 0x8b8b0000)
from space 64K, 100% used [0x8b8c0000, 0x8b8d0000, 0x8b8d0000)
to space 64K, 0% used [0x8b8b0000, 0x8b8b0000, 0x8b8c0000)
tenured generation total 4096K, used 467K [0x8bcb0000, 0x8c0b0000, 0x8f7d0000)
the space 4096K, 11% used [0x8bcb0000, 0x8bd24f00, 0x8bd25000, 0x8c0b0000)
compacting perm gen total 12288K, used 373K [0x8f7d0000, 0x903d0000, 0x937d0000)
the space 12288K, 3% used [0x8f7d0000, 0x8f82d6a0, 0x8f82d800, 0x903d0000)
ro space 8192K, 74% used [0x937d0000, 0x93dca2a8, 0x93dca400, 0x93fd0000)
rw space 12288K, 59% used [0x93fd0000, 0x946e7878, 0x946e7a00, 0x94bd0000)
</pre>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com1tag:blogger.com,1999:blog-4098885671918163159.post-71888252929744495012009-07-21T16:45:00.000-07:002009-07-31T16:02:07.605-07:00Search and replace using regular expression groupsRegular expressions come in very handy for searching and replacing texts in data files. Most IDEs (eclipse on which this was tested) and text editors (TextPad, JEdit, etc.) support regular expressions. Take for example an input data file that you need to create/modify.<br />Here is the data file we are interested in...<br /><br /><pre><br /><span style="color: rgb(51, 102, 255);">07/10/09,11:10:20 AM,63463284,credit,salary electronic deposit,$1000.00<br />07/11/09,11:10:20 AM,63463284,credit,CD interest,$22.00<br />07/12/09,11:10:20 AM,63463284,credit,tax refund,$300.00<br />07/13/09,11:10:20 AM,63463284,credit,ATM deposit,$500.00<br />07/14/09,11:10:20 AM,63463284,debit,credit card payment,$137.00<br />07/15/09,11:10:20 AM,63463284,credit,checking a/c interest,$20.00<br />07/16/09,11:10:20 AM,63463284,debit,power bill,$80.00<br />07/17/09,11:10:20 AM,63463284,credit,Check #8774,$350.00<br />07/18/09,11:10:20 AM,63463284,debit,overdraft fee,$5.00<br />07/19/09,11:10:20 AM,63463284,credit,Check #167,$1000.00<br />07/10/09,11:10:20 AM,77667529,credit,salary electronic deposit,$2000.00<br />07/11/09,11:10:20 AM,77667529,credit,CD interest,$22.00<br />07/12/09,11:10:20 AM,77667529,credit,tax refund,$300.00<br />07/13/09,11:10:20 AM,77667529,credit,ATM deposit,$500.00<br />07/14/09,11:10:20 AM,77667529,debit,credit card payment,$137.00<br />07/15/09,11:10:20 AM,77667529,credit,checking a/c interest,$20.00<br />07/16/09,11:10:20 AM,77667529,debit,power bill,$80.00<br />07/17/09,11:10:20 AM,77667529,credit,Check #7766,$350.00<br />07/18/09,11:10:20 AM,77667529,debit,Check copy fee,$10.00<br />07/19/09,11:10:20 AM,77667529,credit,Check #125,$1000.00<br />07/10/09,11:10:20 AM,33445398,credit,salary electronic deposit,$2000.00<br />07/11/09,11:10:20 AM,33445398,credit,CD interest,$22.00<br />07/12/09,11:10:20 AM,33445398,credit,tax refund,$300.00<br />07/13/09,11:10:20 AM,33445398,credit,ATM deposit,$500.00<br />07/14/09,11:10:20 AM,33445398,debit,credit card payment,$137.00<br />07/15/09,11:10:20 AM,33445398,credit,checking a/c interest,$20.00<br />07/16/09,11:10:20 AM,33445398,debit,power bill,$80.00<br />07/17/09,11:10:20 AM,33445398,credit,Check #1255,$350.00<br />07/18/09,11:10:20 AM,33445398,debit,overdraft fee,$5.00<br />07/19/09,11:10:20 AM,33445398,credit,Check #433,$1000.00<br /></span><br /><br /></pre>The data is about bank transactions conducted on accounts.<br />Let's take a look at the first few lines of data...<br /><br /><span style="color: rgb(51, 102, 255);font-size:100%;" ><span style="font-family:arial;">Date Time account # type description currency amount</span><br /><span style="font-family:arial;">------- ------- --------------- ------- ------------------ -------------- ------------</span><br /><span style="font-family:arial;">07/10/09,11:10:20 AM,63463284,credit,salary electronic deposit,$1000.00</span> <span style="font-family:arial;"><br />07/11/09,11:10:20 AM,63463284,credit,CD interest,$22.00</span> <span style="font-family:arial;"><br />07/12/09,11:10:20 AM,63463284,credit,tax refund,$300.00</span> <span style="font-family:arial;"><br />07/13/09,11:10:20 AM,63463284,credit,ATM deposit,$500.00</span><br /><span style="font-family:arial;">07/14/09,11:10:20 AM,63463284,debit,credit card payment,$137.00</span> <span style="font-family:arial;"><br />07/15/09,11:10:20 AM,63463284,credit,checking a/c interest,$20.00</span> <span style="font-family:arial;"><br />07/16/09,11:10:20 AM,63463284,debit,power bill,$80.00</span> <span style="font-family:arial;"><br />07/17/09,11:10:20 AM,63463284,credit,Check #8774,$350.00</span> <span style="font-family:arial;"><br />07/18/09,11:10:20 AM,63463284,debit,overdraft fee,$5.00</span> <span style="font-family:arial;"><br />07/19/09,11:10:20 AM,63463284,credit,Check #167,$1000.00</span> <span style="font-family:arial;"><br />..</span><br /><span style="font-family:arial;">..</span><br /><span style="font-family:arial;">..</span></span><br /><br />Say we want to give credit to all overdraft fees charged for all accounts by adding an adjustment(credit) for the same amount. That is we want to add a new data line for every line which contains 'overdraft fee'. For example if you look at the lines of data above. For every line like this...<br />07/18/09,11:10:20 AM,63463284,debit,overdraft fee,$5.00<br />we want to add a new line like this....<br />07/18/09,11:10:20 AM,63463284,credit,overdraft fee adjustment,$5.00<br /><br />The regular expression .*overdraft fee.*\n will get us the line containing overdraft fee. But we also need the timestamp at the beginning of the line. So let's refine the search string by adding a group.<br /><span style="font-weight: bold;">^(.*)debit,overdraft fee,(.*)\n</span><br />So everything from the start of line upto debit forms group 1, which is the timestamp and account number.<br />The replacement string should be the entire last line which is $0 plus the same timestamp and account number($1) plus new text (credit,overdraft fee adjustment, $2) plus platform independent line feed (\R). Putting them all together the replacement string should be<br /><span style="font-weight: bold;">$0$1credit,overdraft fee adjustment,$2\R</span><br />This was tested in eclipse IDE. In case of text editors like Textpad, the search string will be<br /><span style="font-weight: bold;">^\\(.*\\)debit,overdraft fee,\\(.*\\)\n</span><br />and the replace string will be <span style="font-weight: bold;">\0\1credit,overdraft fee adjustment,\2\n</span><br /><br />Using the search and replace sub menu item of eclipse<span style="text-decoration: underline;"><br /><br /></span><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirFh6RUUezOb813Y6Z_ICJHR84YL1hmJk-7aQ4bevUR5aEMyY4JQ2cME-BOiPTiBbvmJHJYbI9VGZ5MKJGitHQMplo318CaYVukDcFfxYsdEZiL6njKxHt2WGQp_oisNFwHMm_viVFwZ4/s1600-h/eclipse-regularexpression-Replace+.png"><img style="cursor: pointer; width: 320px; height: 295px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirFh6RUUezOb813Y6Z_ICJHR84YL1hmJk-7aQ4bevUR5aEMyY4JQ2cME-BOiPTiBbvmJHJYbI9VGZ5MKJGitHQMplo318CaYVukDcFfxYsdEZiL6njKxHt2WGQp_oisNFwHMm_viVFwZ4/s320/eclipse-regularexpression-Replace+.png" alt="" id="BLOGGER_PHOTO_ID_5364759961323395474" border="0" /></a>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-41618986000871479772009-07-09T10:45:00.000-07:002009-07-14T10:01:22.695-07:00Java System Tray icon in UbuntuDownload JDIC.<br /><div style="text-align: center;"><div style="text-align: left;"><ul><li> Using Synaptic package manager<br />search for jdic and download both libjdic -java and libjdic-bin packages.</li></ul><ul><li>downloading from JDIC website<br />download the linux distribution from <a href="https://jdic.dev.java.net/">here</a>.<br />Say you extract it to the directory /home/myuser/jdic<br />You should see a linux folder which has jdic.jar and some .so files.<br /></li></ul>Here is a code example for creating a tray icon.<br /><br /><br /><pre><span style="color: rgb(51, 51, 255);font-size:85%;" >// Start Tray Icon Code<br />SystemTray tray = SystemTray.getDefaultSystemTray();<br />TrayIcon ti;<br /><br />try {<br />if (Integer.parseInt(System.getProperty("java.version").substring(2, 3)) >= 5)<br />System.setProperty("javax.swing.adjustPopupLocationToFit", "false");<br />menu = new JPopupMenu("MY APP Menu");<br /><br />// "Quit" menu item<br />menuItem = new JMenuItem("Exit");<br />menuItem.addActionListener(this);<br />menu.add(menuItem);<br /><br />ImageIcon i = new ImageIcon(MyClass.class.getResource("/images/appImage.png"));<br /><br />ti = new TrayIcon(i, "Welcome to My App", menu);<br /><br />ti.setIconAutoSize(true);<br />ti.addActionListener(new ActionListener() {<br />public void actionPerformed(ActionEvent e) {<br /> MyApp.this.setVisible(!MyApp.this.isVisible());<br /> if (MyAppFrame.this.isVisible() == false) {<br /> ti.displayMessage(<br /> "Welcome to MyApp",<br /> "To open MyApp Application click the icon. To exit the application right click the icon and click the Quit menu item.",<br /> 0);<br /> }<br /> }<br />});<br /><br />tray.addTrayIcon(ti);<br /><br />// Construct the GUI for balloon message.<br />ActionListener al = new ActionListener() {<br /><br />public void actionPerformed(ActionEvent e) {<br />MyApp.this.setVisible(!MyApp.this.isVisible());<br />if (MyApp.this.isVisible() == false) {<br />ti.displayMessage(<br /> "MyApp",<br /> "To open the MyApp Application click the icon. To exit the application right click the icon and click the Quit menu item.",<br /> 0);<br /> }<br />}<br />};<br />exitMnu.addActionListener(al);<br /><br />} catch (Exception e) {<br />logger.error("Unhandled error", e);<br />}<br /><br />try {<br />this.setIconImage(new ImageIcon(MyApp.class<br /> .getResource("/images/myApp.png")).getImage());<br />} catch (Exception e) {<br />logger.error("Unhandled error", e);<br />}<br />// End Tray Icon Code</span><span style="color: rgb(51, 51, 255);"> </span><br /></pre><br /><br />For this to work, you need to add jdic jar to the build path and also add share library files location(<span style="font-weight: bold;">/usr/lib/jni</span> for the first case and <span style="font-weight: bold;">/home/myuser/jdic/linux</span> for the second) to the native library path for the project as shown in the picture below<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-BPPWMiet91TwzYjVrFmq9ngPDFPWyArvpuAGFrJBGLF3qR3F2xQlozm9zPBxMxb_yi-37QMzYIT2KHo3AaiHAMxmYITSNNCQVOUDwH4LR99LSW2ZoeHmZM4PsUlqYFDwtdI5ov_w4Kc/s1600-h/build+path+for+jdic+.png"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 320px; height: 222px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-BPPWMiet91TwzYjVrFmq9ngPDFPWyArvpuAGFrJBGLF3qR3F2xQlozm9zPBxMxb_yi-37QMzYIT2KHo3AaiHAMxmYITSNNCQVOUDwH4LR99LSW2ZoeHmZM4PsUlqYFDwtdI5ov_w4Kc/s320/build+path+for+jdic+.png" alt="" id="BLOGGER_PHOTO_ID_5356530607359536722" border="0" /></a>.</div></div>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com0tag:blogger.com,1999:blog-4098885671918163159.post-61244825466891938262009-07-06T17:15:00.000-07:002009-07-08T13:08:23.687-07:00Mounting windows partition at bootup in Ubuntu<span style="font-size:100%;"><span style="font-family:arial;">I have a dual booting linux system at work. I share the java projects (located on the windows partition) on both windows and Linux. But when I launch eclipse, all the projects show up closed since the windows partition is not mounted yet. The solution to this is to mount windows partition at boot up time.</span><br /><br /><span style="font-family:arial;">To mount a windows partition at startup, you need to do the following.</span><br /><span style="font-family:arial;">- get the UUID of the device</span></span><br /><pre><span style="color: rgb(51, 51, 255);">blkid</span><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">/dev/sda1: UUID="9044866844865140" TYPE="ntfs" </span><br /><span style="color: rgb(51, 51, 255);">/dev/sda2: LABEL="GHOST DATA" UUID="FCF8-3D81" TYPE="vfat"</span><br /><span style="color: rgb(51, 51, 255);">/dev/sda5: UUID="b2293182-981b-4291-96ff-dba50f73510d" TYPE="ext3"</span><br /><span style="color: rgb(51, 51, 255);">/dev/sda6: TYPE="swap" UUID="1d3b683c-c559-43e2-ac46-f3500e27dbfd"</span><span style="font-size:100%;"><br /></span></pre><span style=";font-family:arial;font-size:100%;" >- create an entry in /etc/fstab file for the windows NTFS drive</span><br /><pre><span style="color: rgb(51, 51, 255);"># <file system> <mount point> <type> <options> <dump> <pass></span><br /><span style="color: rgb(51, 51, 255);">UUID="9044866844865140" /media/disk ntfs-3g defaults,locale=en_US.utf8 0 0</span><br /></pre><span style="font-size:100%;">Instead of UUID specifying the device also works. see below</span><br /><pre style="color: rgb(51, 51, 255);">/dev/sda1 /media/disk ntfs-3g defaults,locale=en_US.utf8 0 0 </pre><span style="font-size:100%;">Note : both ntfs and ntfs-3g works fine for a windows partition.<br /><br />The options for fstab are clearly explained in the ubuntu help docs <a href="https://help.ubuntu.com/community/Fstab">here.</a></span>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com1tag:blogger.com,1999:blog-4098885671918163159.post-75422809609907515232009-06-04T16:41:00.000-07:002009-07-09T14:02:48.096-07:00Ant build script for Flex projectsIt took me a while to get the build script working for Flex. I started out by googgling for them. Took some scripts out there and put it together. But ran into some errors like"Unable to transcode swf", "Out of memory heap space". Having fixed them all, wanted to share what i ended up with.<br /><br />One compile error I was getting regularly was for embedded objects like swf or image files. Care needs to be taken to make sure that the paths are correct. For example inside a class which is deep in the source package tree, the path needs to be absolute. But for mxml files at the root level, relative paths are OK. Flex builder IDE or eclipse with Flex plugin will not raise a compile error for this issue. Only an ant build script will do that. FLEX_HOME environment variable needs to be set to the flex sdk directory.<br /><br />Also the mxmlc compile task can take a lot of memory, so added maxmemory attribute to take care of that.You need to use fork=true (launch it's own jvm process instead of running under ant's jvm).<br /><br /><span style="font-weight: bold;">build.xml</span><br /><br /><br /><pre style="border: 1px dashed rgb(136, 136, 136); padding: 1em 0pt; overflow: scroll; left: 1em; width: 97%; font-family: 'Lucida Console',monospace; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; position: relative; background-color: rgb(248, 250, 255); font-size: 80%;"><br /><?xml version='1.0'?><br /><project name='MYUIProject' default='masterbuild' basedir='.'><br /><target name='init'><br /><!--FLEX_HOME environment property should be set to where flex is installed--><br /><property environment="env"/><br /><property name="FLEX_HOME" value="${env.FLEX_HOME}"/><br /><property name="DEFAULT.LOCAL" value="en_US"/><br /><property name="ENCODING" value="UTF-8"/><br /><!-- directories--><br /><property name="application.name" value="AppMain"/><br /><property name="src.dir" value="src"/><br /><property name="bin.dir" value="bin"/><br /><property name="build.dir" value="build"/><br /><property name="lib.dir" value="lib"/><br /><property name="vendor.lib.dir" value="${lib.dir}/vendor"/><br /><property name="dist.dir" value="${basedir}/dist"/><br /><property name="wrapper.dir" value="html-template"/><br /><property name="html.file" value="${dist.dir}/${application.name}.html"/><br /><property name="main.class" value="${src.dir}/AppMain.mxml"/><br /><property name="swf.export" value="${bin.dir}/AppMain.swf"/><br /><br /><!--ASSETS DIR--><br /><property name="assets.dir" value="${src.dir}/assets/"/><br /><path id="project.classpath"><br /><pathelement location="${build.dir}" /><br /><fileset dir="${lib.dir}"><br /> <include name="**/*.swc" /><br /></fileset><br /><fileset dir="${vendor.lib.dir}"><br /> <include name="**/*.jar" /><br /></fileset><br /></path><br /><br /><!-- Configure the custom Ant tasks --><br /><taskdef resource="flexTasks.tasks" classpath="${lib.dir}/vendor/flexTasks.jar" /><br /><taskdef resource="com/adobe/ac/ant/tasks/tasks.properties" classpath="${FLEX.ANT.TASK.DIR}"/><br /><br /></target><br /><br /><target name='initdirs' depends="init"><br /><mkdir dir='${dist.dir}'/><br /></target><br /><br /><!-- define all .mxml files and .css files that need to be converted into a .swf file<br />for the application--><br /><target name="compile" depends="init, clean"><br /><antcall target="build-mxml-module"><br /><param name="module" value="AppMain" /><br /></antcall><br /><antcall target="build-css-module"><br /><param name="module" value="core" /><br /></antcall><br /><antcall target="build-css-module"><br /><param name="module" value="day" /><br /></antcall><br /><antcall target="build-css-module"><br /><param name="module" value="night" /><br /></antcall><br /></target><br /><br /><!--Compile the "mxml" file passed in as an argument into a .swf file --><br /><target name="build-mxml-module" ><br /><mxmlc fork="true" maxmemory="128m" file="${src.dir}/${module}.mxml"<br />keep-generated-actionscript="true"<br />output="${dist.dir}/${module}.swf"<br />actionscript-file-encoding="UTF-8"<br />optimize="true"<br />debug="false"<br />incremental="true"><br /><!-- Include the necessary libraries --><br /><load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/><br /><source-path path-element="${FLEX_HOME}/frameworks"/><br /><compiler.source-path path-element="${src.dir}"/><br /><compiler.include-libraries dir="${lib.dir}" append="true"><br /> <include name="as3crypto.swc" /><br /> <include name="AS3IteratorImplementation.swc" /><br /> <include name="Cairngorm.swc" /><br /> <include name="flexunit.swc" /><br /> <include name="flexlib-.2.1.swc" /><br /></compiler.include-libraries><br /></mxmlc><br /></target><br /><br /><!--Compile the "css" file passed in into a .swf file --><br /><target name="build-css-module" ><br /><mxmlc fork="true" file="${src.dir}/${module}.css"<br />keep-generated-actionscript="true"<br />output="${dist.dir}/${module}.swf"<br />actionscript-file-encoding="UTF-8"<br />optimize="true"<br />debug="false"<br />incremental="true"><br /><!-- Include the necessary libraries --><br /><load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/><br /><source-path path-element="${FLEX_HOME}/frameworks"/><br /><compiler.source-path path-element="${src.dir}"/><br /><compiler.include-libraries dir="${lib.dir}" append="true"><br /> <include name="as3crypto.swc" /><br /> <include name="AS3IteratorImplementation.swc" /><br /> <include name="as3-rpclib.swc" /><br /> <include name="Cairngorm.swc" /><br /> <include name="flexunit.swc" /><br /> <include name="primitives.swc" /><br /> <include name="util.swc" /><br /></compiler.include-libraries><br /></mxmlc><br /></target><br /><br /><target name="clean" depends='initdirs'><br /><delete dir="${dist.dir}" /><br /></target><br /><br /><!-- Create HTML wrapper --><br /><target name="wrapper" depends="compile" description="Creates the HTML wrapper"><br /><html-wrapper<br /> title="Welcome to My Flex App"<br /> file="${application.name}.html"<br /> height="800"<br /> width="800"<br /> bgcolor="red"<br /> application="app"<br /> swf="${application.name}"<br /> version-major="9"<br /> version-minor="0"<br /> version-revision="0"<br /> history="true"<br /> template="express-installation"<br /> output="${dist.dir}"/><br /><br /></target><br /><br /><!-- Copy non-embedded files to output folder --><br /><target name="output" depends="compile" description="Copies non-embedded files to output folder"><br /><!-- Copy the assets dir --><br /><copy todir="${dist.dir}/assets"><br /><fileset dir="${assets.dir}"><br /></fileset><br /></copy><br /><!-- Copy the swf files from src dir --><br /><copy todir="${dist.dir}"><br /><fileset dir="${src.dir}" includes="*.swf"><br /></fileset><br /></copy><br /></target><br /><br /><!-- Run all, default --><br /><target name="masterbuild"<br /> depends="clean, compile, output, wrapper"<br /> description="Complete build in efficient sequence"/><br /></project><br /></pre>Vishayamhttp://www.blogger.com/profile/07433414598837113307noreply@blogger.com5