tag:blogger.com,1999:blog-79974023297056336602023-07-27T06:47:36.681-04:00Microsoft Dot NetVivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.comBlogger217125tag:blogger.com,1999:blog-7997402329705633660.post-68038121448802221232017-11-28T17:32:00.005-05:002017-11-28T17:32:49.118-05:00Find port number used by a process in windows<div dir="ltr" style="text-align: left;" trbidi="on">
If your process is launched in a java process, then sysinternal tools by itself is not easy enough to figure out the port used by your process, because there are usually many java processes running on a machine.<br />
<br />
One way to get around this is<br />
<br />
1>Create a unique local user on the machine<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-gfWBJj9MVkY/Wh3jJhUmlqI/AAAAAAAACBw/xJEyRcYvP7s-5FtvcDxvcAy_ahMHVM0uwCLcBGAs/s1600/createUser.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="502" data-original-width="721" height="222" src="https://1.bp.blogspot.com/-gfWBJj9MVkY/Wh3jJhUmlqI/AAAAAAAACBw/xJEyRcYvP7s-5FtvcDxvcAy_ahMHVM0uwCLcBGAs/s320/createUser.png" width="320" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
2>Start the process as that local user.<br />
3>Launch procexp.exe in sysinternal tools and sort by username. Locate the only process running under the username created in step 1. Note its PID (process ID).<br />
4>Launch tcpview in the sysinternal tools and locate the PID found in step 3. This will give you the port number used by the process.<br />
<br /></div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-87510555137953526932016-09-15T16:00:00.002-04:002016-09-15T17:13:35.485-04:00Command to connect to scvmm from powershell<div dir="ltr" style="text-align: left;" trbidi="on">
<b>For scvmm 2012</b><br />
<br />
Import-Module 'C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1'<br />
<br />
$username = "xxx" <br />
$password = "yyy"<br />
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))<br />
<br />
Get-VMMServer –Computername "sssss" -TCPPort 8100 -Credential $credentials<br />
<br />
<br />
<b>For scvmm 2012 r2</b><br />
<br />
Import-Module 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1'<br />
<br />
The rest is the same</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-39206831603203531162016-05-01T15:20:00.003-04:002016-05-02T10:19:24.306-04:00Windbg download location<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Get windbg by installing windows debugging tools for Windows 10</b><br />
<br />
https://msdn.microsoft.com/en-us/windows/hardware/hh852365.aspx<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-QyFXWi5zjt8/Vydh492Sq-I/AAAAAAAABmA/UW4dLkHIgN0HfH7NJJqQAYJMpGHwAI--gCLcB/s1600/DebuggingTools.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="438" src="https://1.bp.blogspot.com/-QyFXWi5zjt8/Vydh492Sq-I/AAAAAAAABmA/UW4dLkHIgN0HfH7NJJqQAYJMpGHwAI--gCLcB/s640/DebuggingTools.png" width="640" /></a></div>
<br />
<br />
When you run the install just pick Debugging Tools for Windows<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-ZdhfmmrWqD0/VydT6NltdBI/AAAAAAAABls/texSdNIhwtE7MRtzBh-pmBh6zD6-jW6HQCLcB/s1600/windbgDownload.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="470" src="https://1.bp.blogspot.com/-ZdhfmmrWqD0/VydT6NltdBI/AAAAAAAABls/texSdNIhwtE7MRtzBh-pmBh6zD6-jW6HQCLcB/s640/windbgDownload.png" width="640" /></a></div>
<br />
<br />
<br />
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b><br /></b>
Once you install the debugging tools you will find windbg at<br />
<span style="background-color: #cccccc;"><br /></span>
<span style="background-color: #cccccc;">C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\windbg.exe</span><br />
<b><br /></b>
<b>Another way to get windbg is by installing Windows driver kit.</b><br />
<br />
https://msdn.microsoft.com/en-us/windows/hardware/hh852365.aspx<br />
<br />
This Windows driver kit install includes windbg which works for<br />
<br />
<span style="font-family: inherit; font-size: 15px; font-style: inherit; font-weight: inherit; line-height: 1.5;">Windows 10</span><br />
<span style="font-family: inherit; font-size: 15px; font-style: inherit; font-weight: inherit; line-height: 1.5;">Windows 8.1</span><br />
<span style="font-family: inherit; font-size: 15px; font-style: inherit; font-weight: inherit; line-height: 1.5;">Windows 8</span><br />
<span style="font-family: inherit; font-size: 15px; font-style: inherit; font-weight: inherit; line-height: 1.5;">Windows 7</span><br />
<span style="font-family: inherit; font-size: 15px; font-style: inherit; font-weight: inherit; line-height: 1.5;">Windows Server 2016 Technical Preview</span><br />
<span style="font-family: inherit; font-size: 15px; font-style: inherit; font-weight: inherit; line-height: 1.5;">Windows Server 2008 R2</span><br />
<div>
<br /></div>
<div>
<span style="font-family: "segoe ui" , "segoe ui web" , "segoe ui symbol" , "helvetica neue" , "bbalpha sans" , "s60 sans" , "arial" , sans-serif;"><span style="font-size: 15px; line-height: 22.5px;">Once you install Windows driver kit, you will find windbg at </span></span></div>
<div>
<span style="font-family: "segoe ui" , "segoe ui web" , "segoe ui symbol" , "helvetica neue" , "bbalpha sans" , "s60 sans" , "arial" , sans-serif;"><span style="font-size: 15px; line-height: 22.5px;"><br /></span></span></div>
<div>
<span style="font-family: "segoe ui" , "segoe ui web" , "segoe ui symbol" , "helvetica neue" , "bbalpha sans" , "s60 sans" , "arial" , sans-serif;"><span style="font-size: 15px; line-height: 22.5px;">C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\windbg.exe</span></span></div>
<div>
<span style="font-family: "segoe ui" , "segoe ui web" , "segoe ui symbol" , "helvetica neue" , "bbalpha sans" , "s60 sans" , "arial" , sans-serif;"><span style="font-size: 15px; line-height: 22.5px;">and</span></span></div>
<div>
<span style="font-family: "segoe ui" , "segoe ui web" , "segoe ui symbol" , "helvetica neue" , "bbalpha sans" , "s60 sans" , "arial" , sans-serif;"><span style="font-size: 15px; line-height: 22.5px;">C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\windbg.exe</span></span></div>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-33445083942288011302016-01-19T14:46:00.003-05:002016-01-19T15:25:17.001-05:00Powershell log all commands<div dir="ltr" style="text-align: left;" trbidi="on">
If you want to log every single powershell command, run on your machine , including the ones run through C# code, one way to do it is to <a href="https://www.microsoft.com/en-us/download/details.aspx?id=48729">upgrade to powershell 5</a>.<br />
<br />
Once you install powershell 5 all you need to do is to update a registry key to turn it on<br />
<br />
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;">HKLM:\Software\Policies\Microsoft\Windows\PowerShell\Transcription</span><br />
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-t-Hqk7aHnLM/Vp6SdkJsNVI/AAAAAAAABkA/sPQfBGKCq7M/s1600/psLog.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="640" src="http://2.bp.blogspot.com/-t-Hqk7aHnLM/Vp6SdkJsNVI/AAAAAAAABkA/sPQfBGKCq7M/s640/psLog.png" width="516" /></a></div>
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;"><br /></span>
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;"><br /></span>
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;"><br /></span>
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;"><br /></span>
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;"><br /></span>
<span style="background-color: white; color: darkred; font-family: "lucida console"; font-size: 12px;"><br /></span>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<a href="http://blogs.msdn.com/b/powershell/archive/2015/06/09/powershell-the-blue-team.aspx">Here is the powershell script to do this</a><br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;">function Enable-PSTranscription </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;">{ </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> [CmdletBinding()] </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> param( </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> $OutputDirectory, </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> [Switch] $IncludeInvocationHeader </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> ) </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: #3d85c6;"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> ## Ensure the base path exists </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> $basePath = "HKLM:\Software\Policies\Microsoft\Windows\PowerShell\Transcription" </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"><br /></span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> if(-not (Test-Path $basePath)) </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> { </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> $null = New-Item $basePath –Force </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> }</span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: #3d85c6;"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> ## Enable transcription </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> Set-ItemProperty $basePath -Name EnableTranscripting -Value 1</span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: #3d85c6;"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> ## Set the output directory </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> if($PSCmdlet.MyInvocation.BoundParameters.ContainsKey("OutputDirectory")) </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> { </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> Set-ItemProperty $basePath -Name OutputDirectory -Value $OutputDirectory </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> }</span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: #3d85c6;"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> ## Set the invocation header </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> if($IncludeInvocationHeader) </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> { </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> Set-ItemProperty $basePath -Name EnableInvocationHeader -Value 1 </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;"> } </span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="color: #3d85c6; font-size: 12px;">}</span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="font-size: 12px;"><br /></span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0pt; margin-top: 12px;">
<span style="color: darkblue; font-family: "lucida console";"><span style="font-size: 12px;">Enable-PSTranscription -OutputDirectory 'C:\PowershellLogs'</span></span><br />
<span style="color: darkblue; font-family: "lucida console";"><span style="font-size: 12px;"><br /></span></span>
<span style="color: darkblue; font-family: "lucida console";"><span style="font-size: 12px;"><br /></span></span>
<b><br /></b>
<b>To enable more detailed logging run <a href="http://blogs.msdn.com/b/powershell/archive/2015/06/09/powershell-the-blue-team.aspx">this powershell command </a>below after you performed the above steps</b><br />
<span style="color: #3d85c6;"><br /></span>
<span style="color: #3d85c6;">function Enable-PSScriptBlockLogging </span><br />
<span style="color: #3d85c6;">{ </span><br />
<span style="color: #3d85c6;"> $basePath = "HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" </span><br />
<br />
<span style="color: #3d85c6;"> if(-not (Test-Path $basePath)) </span><br />
<span style="color: #3d85c6;"> { </span><br />
<span style="color: #3d85c6;"> $null = New-Item $basePath –Force </span><br />
<span style="color: #3d85c6;"> }</span><br />
<span style="color: #3d85c6;"> </span><br />
<span style="color: #3d85c6;"> Set-ItemProperty $basePath -Name EnableScriptBlockLogging -Value "1" </span><br />
<span style="color: #3d85c6;">}</span><br />
<span style="color: #3d85c6;"><br /></span>
<span style="color: #3d85c6;"><br /></span>
<span style="color: #3d85c6;">Enable-PSScriptBlockLogging </span><br />
<span style="color: #3d85c6;"><br /></span>
<br /></div>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-37717297936224913802015-08-13T18:51:00.001-04:002015-08-13T18:51:23.746-04:00Java DeepCopy using JAXB<div dir="ltr" style="text-align: left;" trbidi="on">
<pre style="background-color: white; font-family: 'Courier New';"><span style="color: grey; font-style: italic;">
</span><span style="color: navy; font-weight: bold;">import </span>javax.xml.bind.JAXBElement;
<span style="color: navy; font-weight: bold;">import </span>javax.xml.namespace.QName;
<span style="color: navy; font-weight: bold;">import </span>java.io.Serializable;
<span style="color: grey; font-style: italic;">/**</span><span style="color: grey; font-style: italic;"> * Returns a deep copy of any object that implements Serializable.</span><span style="color: grey; font-style: italic;"> * This class uses JAXB to serialize and deserialize objects</span><span style="color: grey; font-style: italic;"> */</span><span style="color: navy; font-weight: bold;">public class </span>Copier<<span style="color: #20999d;">T </span><span style="color: navy; font-weight: bold;">extends </span>Serializable> {
<span style="color: navy; font-weight: bold;">private final </span>Class<<span style="color: #20999d;">T</span>> <span style="color: #660e7a; font-weight: bold;">type</span>;
<span style="color: navy; font-weight: bold;">public </span>Copier(Class<<span style="color: #20999d;">T</span>> type) {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">type </span>= type;
}
<span style="color: navy; font-weight: bold;">public </span><span style="color: #20999d;">T </span>GetCopy(<span style="color: #20999d;">T </span>objectToBeCopied) {
JAXBElement<<span style="color: #20999d;">T</span>> serializedObject = <span style="color: navy; font-weight: bold;">new </span>JAXBElement(<span style="color: navy; font-weight: bold;">new </span>QName(<span style="color: green; font-weight: bold;">"T"</span>), <span style="color: #660e7a; font-weight: bold;">type</span>, objectToBeCopied);
<span style="color: #20999d;">T </span>deserialized = serializedObject.getValue();
<span style="color: navy; font-weight: bold;">return </span>(<span style="color: #20999d;">T</span>) (deserialized);
}
}<span style="font-size: xx-small;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';">
</pre>
<pre style="background-color: white; font-family: 'Courier New';">
</pre>
<pre style="background-color: white; font-family: 'Courier New';">
</pre>
<pre style="background-color: white; font-family: 'Courier New';">Example that uses the code4 above</pre>
<pre style="background-color: white; font-family: 'Courier New';">
</pre>
<pre style="background-color: white; font-family: 'Courier New';"><pre style="font-family: 'Courier New';">Copier<Person> copier =<span style="color: navy; font-weight: bold;">new </span>Copier(Person.<span style="color: navy; font-weight: bold;">class</span>);
Person deepCopyPerson= copier.GetCopy(<span style="color: #660e7a; font-weight: bold;">myPerson</span>); </pre>
</pre>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-9480878800373909122015-07-24T14:11:00.001-04:002015-07-24T14:11:19.669-04:00Java collections tutorial<div dir="ltr" style="text-align: left;" trbidi="on">
The two most popular collections used in Java are <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">List </a>and <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Map.html">Map</a>. Both are generic interfaces. Java framework library provides many implementations of both these interfaces.<br />
<br />
The most popular implementation of List is the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html">ArrayList </a>class. ( If you are from the C# world, note that Arraylist in java has nothing to do with Arraylist in C#. ArrayList in java is strongly typed and more similar to List<T> in C#.).<br />
<br />
The most popular implementation of Map in Java is the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html">HashMap</a>.<br />
<br />
<br />
<br />
<span style="color: #3d85c6; font-size: x-large;"><b><u>An example of List in java</u></b></span><br />
<br />
<pre style="background-color: white; font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><span style="color: navy; font-size: 7.2pt;"><b> </b></span>
<span style="color: navy; font-weight: bold;">import </span>java.util.ArrayList;
<span style="color: navy; font-weight: bold;">import </span>java.util.List;
<span style="color: navy; font-weight: bold;">public class </span>Main {
<span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) {
List<Student> students = <span style="color: navy; font-weight: bold;">new </span>ArrayList<Student>();
Student student1 = <span style="color: navy; font-weight: bold;">new </span>Student() {{
setName(<span style="color: green; font-weight: bold;">"Jack"</span>);
setCollege(<span style="color: green; font-weight: bold;">"MIT"</span>);
setMajor(<span style="color: green; font-weight: bold;">"Computer Science"</span>);
setSsn(<span style="color: green; font-weight: bold;">"123-45-5678"</span>);
}};
Student student2 = <span style="color: navy; font-weight: bold;">new </span>Student() {{
setName(<span style="color: green; font-weight: bold;">"Sandra"</span>);
setCollege(<span style="color: green; font-weight: bold;">"Harvard"</span>);
setMajor(<span style="color: green; font-weight: bold;">"Medicine"</span>);
setSsn(<span style="color: green; font-weight: bold;">"999-45-5678"</span>);
}};
students.add(student1);
students.add(student2);
<span style="color: navy; font-weight: bold;">for </span>(Student s : students) {
System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(s.getName());
}
}
}</pre>
</pre>
<br />
<pre style="background-color: white; font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><span style="color: navy;"><b> </b></span>
<span style="color: navy; font-weight: bold;">public class </span>Student {
<span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">name</span>;
<span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">college</span>;
<span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">major</span>;
<span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">ssn</span>;
<span style="color: navy; font-weight: bold;">public </span>String getName() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">name</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setName(String name) {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">name </span>= name;
}
<span style="color: navy; font-weight: bold;">public </span>String getMajor() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">major</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setMajor(String major) {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">major </span>= major;
}
<span style="color: navy; font-weight: bold;">public </span>String getCollege() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">college</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setCollege(String college) {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">college </span>= college;
}
<span style="color: navy; font-weight: bold;">public </span>String getSsn() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">ssn</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setSsn(String ssn) {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">ssn </span>= ssn;
}
}</pre>
<pre style="font-family: 'Courier New'; font-size: 7.2pt;"></pre>
</pre>
<pre style="background-color: white; font-family: 'Courier New';"></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">Note here that Lists are allowed to have duplicates. In the above example you can enter the same student object multiple times into the List.</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"></pre>
<pre style="background-color: white; font-family: 'Courier New';"><b style="color: #3d85c6; font-family: 'Times New Roman'; font-size: xx-large; white-space: normal;"><u>An example of Map in java</u></b></pre>
<pre style="background-color: white; font-family: 'Courier New';"><b style="color: #3d85c6; font-family: 'Times New Roman'; font-size: xx-large; white-space: normal;"><u>
</u></b></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">You use a Map when you want to look up values by a key. For example you could store people's information in a Map and look them up by their Social Security number (key). </span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"> </pre>
<pre style="background-color: white; font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><span style="color: navy; font-weight: bold;">import </span>java.util.HashMap;
<span style="color: navy; font-weight: bold;">import </span>java.util.Map;
<span style="color: navy; font-weight: bold;">public class </span>Main {
<span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) {
Map<String, Student> students = <span style="color: navy; font-weight: bold;">new </span>HashMap<String, Student>();
Student student1 = <span style="color: navy; font-weight: bold;">new </span>Student() {{
setName(<span style="color: green; font-weight: bold;">"Jack"</span>);
setCollege(<span style="color: green; font-weight: bold;">"MIT"</span>);
setMajor(<span style="color: green; font-weight: bold;">"Computer Science"</span>);
setSsn(<span style="color: green; font-weight: bold;">"123-45-5678"</span>);
}};
Student student2 = <span style="color: navy; font-weight: bold;">new </span>Student() {{
setName(<span style="color: green; font-weight: bold;">"Sandra"</span>);
setCollege(<span style="color: green; font-weight: bold;">"Harvard"</span>);
setMajor(<span style="color: green; font-weight: bold;">"Medicine"</span>);
setSsn(<span style="color: green; font-weight: bold;">"999-45-5678"</span>);
}};
students.put(student1.getSsn(), student1);
students.put(student2.getSsn(), student2);
System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(students.get(<span style="color: green; font-weight: bold;">"999-45-5678"</span>).getName());<span style="color: grey; font-style: italic;">//Sandra</span><span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;"> </span>}
}</pre>
</pre>
</pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;"><span style="font-size: x-small;">
</span></span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;"><span style="font-size: x-small;">
</span></span></pre>
<pre style="background-color: white; font-family: 'Courier New';"></pre>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-80473033221114768042015-07-20T18:27:00.000-04:002015-07-21T11:29:42.463-04:00Builder pattern tutorial Java (Beginners introduction to builder pattern)<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #3d85c6; font-size: large;"><b>The requirement</b></span><br />
<br />
Suppose you need a Sandwich class which has 3 ingredients bread, cheese and meat. The requirement is you can pick any combination of these items. And once the Sandwich class is initialized, you shouldn't be able to change these ingredients. One easy way to achieve this is<br />
<br />
<pre style="background: white;"><span style="font-size: 12pt;">
</span><b><span style="color: navy; font-size: 12.0pt;">public class </span></b><span style="font-size: 12pt;">Sandwich {
</span><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">bread</span></b><span style="font-size: 12pt;">;</span><i><span style="color: grey; font-size: 12.0pt;">//immutable
</span></i><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">cheese</span></b><span style="font-size: 12pt;">;</span><i><span style="color: grey; font-size: 12.0pt;">//immutable
</span></i><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">meat</span></b><span style="font-size: 12pt;">;</span><i><span style="color: grey; font-size: 12.0pt;">//immutable
</span></i><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">Sandwich(String bread, String cheese, String meat) {
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">bread </span></b><span style="font-size: 12pt;">= bread;
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">cheese </span></b><span style="font-size: 12pt;">= cheese;
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">meat </span></b><span style="font-size: 12pt;">= meat;
}
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">String getBread() {
</span><b><span style="color: navy; font-size: 12.0pt;">return </span></b><b><span style="color: #660e7a; font-size: 12.0pt;">bread</span></b><span style="font-size: 12pt;">;
}
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">String getCheese() {
</span><b><span style="color: navy; font-size: 12.0pt;">return </span></b><b><span style="color: #660e7a; font-size: 12.0pt;">cheese</span></b><span style="font-size: 12pt;">;
}
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">String getMeat() {
</span><b><span style="color: navy; font-size: 12.0pt;">return </span></b><b><span style="color: #660e7a; font-size: 12.0pt;">meat</span></b><span style="font-size: 12pt;">;
}
}<o:p></o:p></span></pre>
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p><br /></o:p></div>
<div class="MsoNormal">
<o:p><b><span style="color: navy; font-family: "Calibri",sans-serif; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">public class </span></b><span style="font-family: Calibri, sans-serif; font-size: 12pt; line-height: 115%;">Main {<br />
</span><b><span style="color: navy; font-family: "Calibri",sans-serif; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">public static void </span></b><span style="font-family: Calibri, sans-serif; font-size: 12pt; line-height: 115%;">main(String[] args) {<br />
Sandwich mySandwich = </span><b><span style="color: navy; font-family: "Calibri",sans-serif; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">new </span></b><span style="font-family: Calibri, sans-serif; font-size: 12pt; line-height: 115%;">Sandwich(</span><b><span style="color: green; font-family: "Calibri",sans-serif; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">"ItalianBread"</span></b><span style="font-family: Calibri, sans-serif; font-size: 12pt; line-height: 115%;">, </span><b><span style="color: green; font-family: "Calibri",sans-serif; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">"Swiss"</span></b><span style="font-family: Calibri, sans-serif; font-size: 12pt; line-height: 115%;">, </span><b><span style="color: green; font-family: "Calibri",sans-serif; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">""</span></b><span style="font-family: Calibri, sans-serif; font-size: 12pt; line-height: 115%;">);<br />
}<br />
}</span></o:p></div>
<div class="MsoNormal">
<o:p><span style="font-family: Calibri, sans-serif; font-size: 12pt; line-height: 115%;"><br /></span></o:p></div>
<div class="MsoNormal">
The above code doesn't let you change the ingredients once you initialize it, which meets our requirement at the same time you have to specify all the ingredients even if you don't need it. (We didn't want meat in our sandwich so we just passed an empty string for meat).</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: #3d85c6; font-size: large;"><b>A more graceful code that uses builder pattern</b></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
A more graceful way of handling the above requirement is by using a builder pattern. In this pattern, we use a nested static class. </div>
<div class="MsoNormal">
<br />
<b><span style="color: navy; font-size: 12.0pt;">public class </span></b><span style="font-size: 12pt;">Sandwich {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">bread</span></b><span style="font-size: 12pt;">;</span><i><span style="color: grey; font-size: 12.0pt;">// immutable<br />
</span></i><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">cheese</span></b><span style="font-size: 12pt;">;</span><i><span style="color: grey; font-size: 12.0pt;">///immutable<br />
</span></i><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">meat</span></b><span style="font-size: 12pt;">;</span><i><span style="color: grey; font-size: 12.0pt;">///immutable<br />
<br />
</span></i><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">Sandwich(<span style="background-color: yellow;">Builder builder</span>) {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">bread </span></b><span style="font-size: 12pt;">= builder.</span><b><span style="color: #660e7a; font-size: 12.0pt;">bread</span></b><span style="font-size: 12pt;">;<br />
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">cheese </span></b><span style="font-size: 12pt;">= builder.</span><b><span style="color: #660e7a; font-size: 12.0pt;">cheese</span></b><span style="font-size: 12pt;">;<br />
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">meat </span></b><span style="font-size: 12pt;">= builder.</span><b><span style="color: #660e7a; font-size: 12.0pt;">meat</span></b><span style="font-size: 12pt;">;<br />
}<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public static class </span></b><span style="font-size: 12pt;">Builder {<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">bread</span></b><span style="font-size: 12pt;">;<br />
</span><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">cheese</span></b><span style="font-size: 12pt;">;<br />
</span><b><span style="color: navy; font-size: 12.0pt;">private </span></b><span style="font-size: 12pt;">String </span><b><span style="color: #660e7a; font-size: 12.0pt;">meat</span></b><span style="font-size: 12pt;">;<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">Builder() {<br />
}<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">Builder bread(String bread) {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">bread </span></b><span style="font-size: 12pt;">= bread;<br />
<span style="background-color: yellow;"> </span></span><b><span style="background-color: yellow; color: navy; font-size: 12pt;">return this</span></b><span style="font-size: 12pt;">;</span><i><span style="color: grey; font-size: 12.0pt;">//We are returning a reference to itself so that we can call other methods on it like cheese() meat() and so on<br />
</span></i><span style="font-size: 12pt;">}<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">Builder cheese(String cheese) {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">cheese </span></b><span style="font-size: 12pt;">= cheese;<br />
<span style="background-color: yellow;"> </span></span><b><span style="background-color: yellow; color: navy; font-size: 12pt;">return this</span></b><span style="font-size: 12pt;"><span style="background-color: yellow;">;</span><br />
}<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">Builder meat(String bread) {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b><span style="font-size: 12pt;">.</span><b><span style="color: #660e7a; font-size: 12.0pt;">meat </span></b><span style="font-size: 12pt;">= </span><b><span style="color: #660e7a; font-size: 12.0pt;">meat</span></b><span style="font-size: 12pt;">;<br />
<span style="background-color: yellow;"> </span></span><b><span style="background-color: yellow; color: navy; font-size: 12pt;">return this</span></b><span style="font-size: 12pt;"><span style="background-color: yellow;">;</span><br />
}<br />
<br />
<span style="background-color: yellow;"> </span></span><span style="background-color: yellow;"><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">Sandwich build() {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">return new </span></b><span style="font-size: 12pt;">Sandwich(</span><b><span style="color: navy; font-size: 12.0pt;">this</span></b></span><span style="font-size: 12pt;"><span style="background-color: yellow;">);<br />
}</span><br />
}<br />
<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">String getBread() {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">return </span></b><b><span style="color: #660e7a; font-size: 12.0pt;">bread</span></b><span style="font-size: 12pt;">;<br />
}<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">String getCheese() {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">return </span></b><b><span style="color: #660e7a; font-size: 12.0pt;">cheese</span></b><span style="font-size: 12pt;">;<br />
}<br />
<br />
</span><b><span style="color: navy; font-size: 12.0pt;">public </span></b><span style="font-size: 12pt;">String getMeat() {<br />
</span><b><span style="color: navy; font-size: 12.0pt;">return </span></b><b><span style="color: #660e7a; font-size: 12.0pt;">meat</span></b><span style="font-size: 12pt;">;<br />
}<br />
<br />
}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 12pt;"><br /></span></div>
<pre style="background: white;"><b><span style="color: navy; font-size: 12.0pt;">public class </span></b><span style="font-size: 12pt;">Main {
</span><b><span style="color: navy; font-size: 12.0pt;">public static void </span></b><span style="font-size: 12pt;">main(String[] args) {
Sandwich.Builder sandwichBuilder=</span><b><span style="color: navy; font-size: 12.0pt;">new </span></b><span style="font-size: 12pt;">Sandwich.Builder();
sandwichBuilder.bread(</span><b><span style="color: green; font-size: 12.0pt;">"ItalianBread"</span></b><span style="font-size: 12pt;">).cheese(</span><b><span style="color: green; font-size: 12.0pt;">"swiss"</span></b><span style="font-size: 12pt;">);
Sandwich mySandwich=sandwichBuilder.build();
}
}<o:p></o:p></span></pre>
<div class="MsoNormal">
<span style="font-size: 12pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 12pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 12pt;"><br /></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-33678716699608322072015-07-10T20:48:00.005-04:002015-07-10T20:48:41.833-04:00Java InputStreamReader simple example <div dir="ltr" style="text-align: left;" trbidi="on">
To follow along go to <a href="http://www.asciitohex.com/">a ascii to binary converter </a>and get a binary array for your text.<br />
<br />
The text I am using is<br />
Hi<br />
9<br />
<br />
This text when converted to binary looks like this<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-1whoSUt0FUs/VaBe62e3K9I/AAAAAAAABg0/ngS4BwUU4vg/s1600/asciiToBinary.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="345" src="http://3.bp.blogspot.com/-1whoSUt0FUs/VaBe62e3K9I/AAAAAAAABg0/ngS4BwUU4vg/s640/asciiToBinary.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
In the java program below these are things I will do<br />
<br />
Create a byte array for my data<br />
Create an InputStream object with the byte array<br />
Use an InputStreamReader to interpret that InputStream data as ASCII data<br />
Read the data in InputStreamReader with a BufferedReader<br />
<br />
<span style="font-size: x-small;">
</span>
<br />
<pre style="background: white;"><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">import </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">java.io.*;</span><i><span style="color: grey; font-family: "Times New Roman",serif; font-size: 12.0pt;">
</span></i><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">public class </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">Play {
</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">public static void </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">main(String[] args) </span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">throws </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">IOException {
</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">byte</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">[] arrayOfBytes = </span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">new byte</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">[]
{
(</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">byte</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">) </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 12.0pt;">0b01001000</span><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">,
(</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">byte</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">) </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 12.0pt;">0b01101001</span><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">,
(</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">byte</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">) </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 12.0pt;">0b00001101</span><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">,
(</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">byte</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">) </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 12.0pt;">0b00001010</span><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">,
(</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">byte</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">) </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 12.0pt;">0b00111001
</span><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">};
InputStream is = </span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">new </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">ByteArrayInputStream(arrayOfBytes);
InputStreamReader isr = </span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">new </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">InputStreamReader(is, </span><b><span style="color: green; font-family: "Times New Roman",serif; font-size: 12.0pt;">"ASCII"</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">);
BufferedReader br = </span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">new </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">BufferedReader(isr);
String line;
</span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">while </span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">((line = br.readLine()) != </span><b><span style="color: navy; font-family: "Times New Roman",serif; font-size: 12.0pt;">null</span></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">) {
System.</span><b><i><span style="color: #660e7a; font-family: "Times New Roman",serif; font-size: 12.0pt;">out</span></i></b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">.println(line);
}</span></pre>
<pre style="background: white;"><span style="font-family: 'Times New Roman', serif; font-size: 12pt;">
}
}<o:p></o:p></span></pre>
<br />
<br />
<br />
<br />
<pre style="background-color: white; font-family: 'Courier New';"></pre>
<pre style="background-color: white; font-family: 'Courier New';"></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">Dont forget to put 0b as a prefix when inputting binary data into the byte array. </span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-YQAM_AO2bQA/VaBiSuXCewI/AAAAAAAABhA/EpHNOhNw9sg/s1600/outPut.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-YQAM_AO2bQA/VaBiSuXCewI/AAAAAAAABhA/EpHNOhNw9sg/s1600/outPut.png" /></a></div>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">You can also print out non ascii characters. You just need to change the setting of your IDE to allow that. In intelliJ this is done by changing File Encoding under your project settings</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-4S7zqVWDSs8/VaBjnEhM4rI/AAAAAAAABhI/Ps4SlDb_bwY/s1600/projectSettings.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="396" src="http://1.bp.blogspot.com/-4S7zqVWDSs8/VaBjnEhM4rI/AAAAAAAABhI/Ps4SlDb_bwY/s640/projectSettings.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<pre style="background-color: white;"><span style="font-family: 'Times New Roman'; white-space: normal;">Here is an example that prints out </span><span style="background-color: transparent; white-space: normal;"><span style="font-family: Times New Roman;">您好 (Hello in Chinese)</span></span></pre>
<pre style="background-color: white;"><span style="background-color: transparent; white-space: normal;"><span style="font-family: Times New Roman;">
</span></span></pre>
<pre style="background-color: white;"><span style="background-color: transparent; white-space: normal;"><span style="font-family: Times New Roman;">
</span></span></pre>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-bF05tmLNFf4/VaBnkvDasMI/AAAAAAAABhU/8RrMGFNX13M/s1600/unicodePrint.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-bF05tmLNFf4/VaBnkvDasMI/AAAAAAAABhU/8RrMGFNX13M/s1600/unicodePrint.png" /></a></div>
<pre style="background-color: white;"><span style="background-color: transparent; white-space: normal;"><span style="font-family: Times New Roman;">
</span></span></pre>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-91600464171542437832015-06-05T11:16:00.001-04:002015-06-23T18:04:36.615-04:00Monitoring localhost to localhost Restful calls in Windows 7 (Monitor Windows 7 localhost traffic in wireshark)<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #3d85c6; font-size: large;"><u><b>Easy way:</b> Force all localhost traffic thru your network card.</u></span><br />
<br />
If you run <b>route print </b>you would see something like this<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-sZdIzKTJRX4/VYLl_iJPqmI/AAAAAAAABf0/mSkAbH6vyak/s1600/route%2Bprint.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="612" src="http://4.bp.blogspot.com/-sZdIzKTJRX4/VYLl_iJPqmI/AAAAAAAABf0/mSkAbH6vyak/s640/route%2Bprint.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Here <b>10.118.183.252 is my local ipv4 address</b>. As you can see any request to that ipaddress is short circuited by the operating system. It doesn't go thru my network card. To see the local host traffic in wireshark you need to perform these two steps<br />
<br />
<b>1>Modify routes to send all traffic for local ip address thru your default gateway </b>(in this case 10.118.183.253)<br />
<br />
route delete 10.118.183.252<br />
route ADD 10.118.183.252 MASK 255.255.255.255 10.118.183.253<br />
<br />
(This change is temporary. These changes will be lost when you restart your machine. to make these chages persistent use route <b>-p</b> instead of route)<br />
<br />
<b>2>Instead of localhost use your local ipaddress in all urls.</b><br />
<br />
<br />
There you are all set!<br />
<br />
<br />
<br />
<span style="color: #3d85c6; font-size: large;"><b><u>An Alternative way</u></b></span><br />
<br />
<br />
1> <a href="http://www.oracledistilled.com/windows/configuring-the-loopback-adapter-on-microsoft-windows/">Install microsoft loopback adapter.</a><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-fXOaZ5-Sn3U/VXHAyBZcTAI/AAAAAAAABfU/qtnaHKCvu-o/s1600/LoopBackAdapter.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="214" src="http://3.bp.blogspot.com/-fXOaZ5-Sn3U/VXHAyBZcTAI/AAAAAAAABfU/qtnaHKCvu-o/s640/LoopBackAdapter.png" width="640" /></a></div>
<br />
2>Make sure you can ping loop back adapter by its static ip address. Otherwise further steps would fail.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-7GhT2_zQ4fM/VXHAPr9UTHI/AAAAAAAABfM/IWUtV74u0xY/s1600/pingLoopBack.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="416" src="http://2.bp.blogspot.com/-7GhT2_zQ4fM/VXHAPr9UTHI/AAAAAAAABfM/IWUtV74u0xY/s640/pingLoopBack.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
After successfully completing this step, restart the machine.<br />
<br />
2>Install Wireshark. (with winpcap, when prompted)<br />
If wireshark is already installed, reinstall after restart. Otherwise Wireshark won't see this new network interface.<br />
<br />
3>Install <a href="http://www.netresec.com/?page=RawCap">rawcap</a><br />
<br />
4>Start rawcap by double clicking it. Select the Loop back adapter when prompted.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-0z2L80rkyfs/VXG_HRWLTdI/AAAAAAAABfE/3ZoJiS4YmU4/s1600/rawcap.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="416" src="http://4.bp.blogspot.com/-0z2L80rkyfs/VXG_HRWLTdI/AAAAAAAABfE/3ZoJiS4YmU4/s640/rawcap.png" width="640" /></a></div>
<br />
<br />
5>After you have captured all the traffic you need, stop rawcap by pressing ctrl+c.<br />
<br />
6>Now doubleclick and open dumpfile.pcap.<br />
<br />
I tested this for tomcat and IIS. </div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-17649732093424821592015-06-01T17:45:00.002-04:002015-08-18T14:03:28.232-04:00Extjs dropdownlist<div dir="ltr" style="text-align: left;" trbidi="on">
You could extend <span style="background-color: white; color: green; font-family: 'Courier New'; font-weight: bold;">Ext.form.ComboBox </span>to create a dropdownlist. In the example below all I have set is editable:false. That is enough to make it a dropdownlist.<br />
<br />
<br />
<span style="color: #3d85c6; font-size: large;"><b><u>The dropdownlist control</u></b></span><br />
<pre style="background-color: white; font-family: 'Courier New';"><span style="font-size: xx-small;">
</span>
<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">define</span>(<span style="color: green; font-weight: bold;">'DropDownList'</span>, {
<span style="color: #660e7a; font-weight: bold;">extend</span>: <span style="color: green; font-weight: bold;">'Ext.form.ComboBox'</span>,
<span style="color: #660e7a; font-weight: bold;">editable</span>: <span style="color: navy; font-weight: bold;">false</span>,
<span style="color: #660e7a; font-weight: bold;">alias</span>: <span style="color: green; font-weight: bold;">'widget.dropdownlist'</span>,
<span style="color: #7a7a43;">initComponent</span>: <span style="color: navy; font-weight: bold;">function</span>() {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #7a7a43;">callParent</span>([<span style="color: #458383;">arguments</span>]);
},
<span style="color: #7a7a43;">onRender</span>: <span style="color: navy; font-weight: bold;">function</span>() {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #7a7a43;">callParent</span>();
}
});</pre>
<pre style="background-color: white; font-family: 'Courier New';"></pre>
<pre style="background-color: white; font-family: 'Courier New';"></pre>
<pre style="background-color: white; font-family: 'Courier New'; font-size: 7.2pt;"></pre>
<pre style="background-color: white; font-family: 'Courier New'; font-size: 7.2pt;"></pre>
<pre style="background-color: white; font-family: 'Courier New'; font-size: 7.2pt;"></pre>
<span style="color: #3d85c6; font-size: large;"><b><u>The app.js that uses this control</u></b></span><b><u><br /></u></b>
<br />
<pre style="background-color: white; font-family: 'Courier New'; font-size: 7.2pt;"></pre>
<pre style="background-color: white; font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><span style="color: navy; font-weight: bold;">var </span><span style="color: #660e7a; font-style: italic; font-weight: bold;">states </span>= <span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">create</span>(<span style="color: green; font-weight: bold;">'Ext.data.Store'</span>, {
<span style="color: #660e7a; font-weight: bold;">fields</span>: [<span style="color: green; font-weight: bold;">'abbr'</span>, <span style="color: green; font-weight: bold;">'name'</span>],
<span style="color: #660e7a; font-weight: bold;">data</span>: [{
<span style="color: #660e7a; font-weight: bold;">"abbr"</span>: <span style="color: green; font-weight: bold;">"AL"</span>,
<span style="color: #660e7a; font-weight: bold;">"name"</span>: <span style="color: green; font-weight: bold;">"Alabama"</span><span style="color: green; font-weight: bold;"> </span>}, {
<span style="color: #660e7a; font-weight: bold;">"abbr"</span>: <span style="color: green; font-weight: bold;">"AK"</span>,
<span style="color: #660e7a; font-weight: bold;">"name"</span>: <span style="color: green; font-weight: bold;">"Alaska"</span><span style="color: green; font-weight: bold;"> </span>}, {
<span style="color: #660e7a; font-weight: bold;">"abbr"</span>: <span style="color: green; font-weight: bold;">"AZ"</span>,
<span style="color: #660e7a; font-weight: bold;">"name"</span>: <span style="color: green; font-weight: bold;">"Arizona"</span><span style="color: green; font-weight: bold;"> </span>}]
});
<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">application</span>({
<span style="color: #660e7a; font-weight: bold;">name</span>: <span style="color: green; font-weight: bold;">'MyApp'</span>,
<span style="color: #7a7a43;">launch</span>: <span style="color: navy; font-weight: bold;">function </span>() {
<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">create</span>(<span style="color: green; font-weight: bold;">'Ext.form.Panel'</span>, {
<span style="color: #660e7a; font-weight: bold;">items</span>: [
{
<span style="color: #660e7a; font-weight: bold;">xtype</span>: <span style="color: green; font-weight: bold;">'dropdownlist'</span>,
<span style="color: #660e7a; font-weight: bold;">hideLabel</span>: <span style="color: navy; font-weight: bold;">false</span>,
<span style="color: #660e7a; font-weight: bold;">title</span>: <span style="color: green; font-weight: bold;">'ComboBox Test'</span>,
<span style="color: #660e7a; font-weight: bold;">fieldLabel</span>: <span style="color: green; font-weight: bold;">'Choose State'</span>,
<span style="color: #660e7a; font-weight: bold;">store</span>: <span style="color: #660e7a; font-style: italic; font-weight: bold;">states</span>,
<span style="color: #660e7a; font-weight: bold;">displayField</span>: <span style="color: green; font-weight: bold;">'name'</span>,
<span style="color: #660e7a; font-weight: bold;">valueField</span>: <span style="color: green; font-weight: bold;">'abbr'</span>,
<span style="color: #660e7a; font-weight: bold;">renderTo</span>: <span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #660e7a; font-weight: bold;">getBody</span>()
}
]
});
}
});</pre>
</pre>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-8856536327225039842015-06-01T15:10:00.001-04:002015-08-18T14:03:39.908-04:00Extjs Hello World application (Begginers introduction to extjs)<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;"><b><u>Show Hello World Alert</u></b></span><br />
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><!DOCTYPE </span><span style="background-color: #efefef; color: blue; font-weight: bold;">html</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">html</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">head </span><span style="background-color: #efefef; color: blue; font-weight: bold;">lang=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"en"</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">meta </span><span style="background-color: #efefef; color: blue; font-weight: bold;">charset=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"UTF-8"</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">title</span><span style="background-color: #efefef;">></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">title</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">head</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">body</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script </span><span style="background-color: #efefef; color: blue; font-weight: bold;">src=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all.js"</span><span style="background-color: #efefef;">></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script </span><span style="background-color: #efefef; color: blue; font-weight: bold;">type=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"text/javascript"</span><span style="background-color: #efefef;">></span>
<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">application</span>({
<span style="color: #660e7a; font-weight: bold;">name</span>: <span style="color: green; font-weight: bold;">'MyApp'</span>,
<span style="color: #7a7a43;">launch</span>: <span style="color: navy; font-weight: bold;">function </span>() {
<span style="color: #7a7a43;">alert</span>(<span style="color: green; font-weight: bold;">"Hello World"</span>);
}
});
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">body</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">html</span><span style="background-color: #efefef;">></span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><b style="font-family: 'Times New Roman'; font-size: x-large; white-space: normal;"><u>Show Hello World HTML</u></b></span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><b style="font-family: 'Times New Roman'; font-size: x-large; white-space: normal;"><u>
</u></b></span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><b style="font-family: 'Times New Roman'; font-size: x-large; white-space: normal;"><u>
</u></b></span></pre>
<pre><pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><!DOCTYPE </span><span style="background-color: #efefef; color: blue; font-weight: bold;">html</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">html</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">head </span><span style="background-color: #efefef; color: blue; font-weight: bold;">lang=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"en"</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">meta </span><span style="background-color: #efefef; color: blue; font-weight: bold;">charset=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"UTF-8"</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">title</span><span style="background-color: #efefef;">></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">title</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">head</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">body</span><span style="background-color: #efefef;">></span>
<pre style="font-family: 'Courier New';"><span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script </span><span style="background-color: #efefef; color: blue; font-weight: bold;">src=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all.js"</span><span style="background-color: #efefef;">></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script</span><span style="background-color: #efefef;">></span></pre>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script </span><span style="background-color: #efefef; color: blue; font-weight: bold;">type=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"text/javascript"</span><span style="background-color: #efefef;">></span>
<pre style="font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><span style="color: navy; font-weight: bold;">var </span><span style="color: #660e7a; font-style: italic; font-weight: bold;">myComponent</span>=<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">create</span>(<span style="color: green; font-weight: bold;">'Ext.Component'</span>,{
<span style="color: #660e7a; font-weight: bold;">html</span>: <span style="color: green; font-weight: bold;">'HelloWorld'</span>});
<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">application</span>({
<span style="color: #660e7a; font-weight: bold;">name</span>: <span style="color: green; font-weight: bold;">'MyApp'</span>,
<span style="color: #7a7a43;">launch</span>: <span style="color: navy; font-weight: bold;">function </span>() {
<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">create</span>(<span style="color: green; font-weight: bold;">'Ext.container.Viewport'</span>, {
<span style="color: #660e7a; font-weight: bold;">items</span>: [
<span style="color: #660e7a; font-style: italic; font-weight: bold;">myComponent</span><span style="color: #660e7a; font-style: italic; font-weight: bold;"> </span>]
});
}
});</pre>
</pre>
</pre>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">body</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">html</span><span style="background-color: #efefef;">></span></pre>
<span style="font-family: Courier New;"><span style="background-color: #efefef;">
</span></span>
The above code can also be written in a short hand way using xtype (preferred way)
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><pre style="background-color: white; font-family: 'Courier New';"><span style="background-color: #efefef;"><!DOCTYPE </span><span style="background-color: #efefef; color: blue; font-weight: bold;">html</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">html</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">head </span><span style="background-color: #efefef; color: blue; font-weight: bold;">lang=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"en"</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">meta </span><span style="background-color: #efefef; color: blue; font-weight: bold;">charset=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"UTF-8"</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">title</span><span style="background-color: #efefef;">></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">title</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">head</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">body</span><span style="background-color: #efefef;">></span>
<pre style="font-family: 'Courier New';"><span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script </span><span style="background-color: #efefef; color: blue; font-weight: bold;">src=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all.js"</span><span style="background-color: #efefef;">></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script</span><span style="background-color: #efefef;">></span></pre>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script </span><span style="background-color: #efefef; color: blue; font-weight: bold;">type=</span><span style="background-color: #efefef; color: green; font-weight: bold;">"text/javascript"</span><span style="background-color: #efefef;">></span>
<pre style="font-family: 'Courier New';"><pre style="font-family: 'Courier New';"><span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">application</span>({
<span style="color: #660e7a; font-weight: bold;">name</span>: <span style="color: green; font-weight: bold;">'MyApp'</span>,
<span style="color: #7a7a43;">launch</span>: <span style="color: navy; font-weight: bold;">function </span>() {
<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #7a7a43;">create</span>(<span style="color: green; font-weight: bold;">'Ext.container.Viewport'</span>, {
<span style="color: #660e7a; font-weight: bold;">items</span>: [
{
<span style="color: #660e7a; font-weight: bold;">xtype</span>: <span style="color: green; font-weight: bold;">'component'</span>,
<span style="color: #660e7a; font-weight: bold;">html</span>: <span style="color: green; font-weight: bold;">'HelloWorld'</span><span style="color: green; font-weight: bold;"> </span>}
]
});
}
});</pre>
</pre>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">script</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">body</span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">html</span><span style="background-color: #efefef;">></span></pre>
</span></pre>
</span></pre>
<span style="font-family: Courier New;"><span style="background-color: #efefef;">
</span></span></pre>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-711733529574777992015-05-26T20:41:00.000-04:002015-08-18T15:47:31.176-04:00Extjs class example. <div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #3d85c6; font-size: large;"><b><u>A car object in pure javascript</u></b></span><br />
<br />
<span style="color: #674ea7;"><b>var myCar = new Object();</b></span><br />
<span style="color: #674ea7;"><b>myCar.company = "Ford";</b></span><br />
<span style="color: #674ea7;"><b>myCar.model = "Mustang";</b></span><br />
<span style="color: #674ea7;"><b>myCar.year = 2007</b></span><br />
<br />
alert(myCar.company);//displays Ford<br />
<br />
<b style="color: #3d85c6;"><u><span style="font-size: large;">A car class in ExtJS</span></u></b><br />
<b style="color: #3d85c6;"><u><br /></u></b>
<br />
<br />
Ext.application({<br />
name: 'Fiddle',<br />
<br />
launch: function() {<br />
<br />
<span style="color: #674ea7;"><b> Ext.define("Car",</b></span><br />
<span style="color: #674ea7;"><b> {</b></span><br />
<span style="color: #674ea7;"><b> company: "Ford",</b></span><br />
<span style="color: #674ea7;"><b> model: "Mustang",</b></span><br />
<span style="color: #674ea7;"><b> year: 2007</b></span><br />
<span style="color: #674ea7;"><b> }</b></span><br />
<span style="color: #674ea7;"><b> );</b></span><br />
<br />
var myCar = Ext.create("Car");<br />
alert(myCar.company);<br />
<br />
}<br />
<br />
<br />
});<br />
<br />
<br />
<br />
The general syntax for defining classes in Extjs is<br />
<br />
<a href="https://docs.sencha.com/extjs/5.1/core_concepts/classes.html">Ext.define(className, members, onClassCreated);</a><br />
<br />
<b>className</b>: The class name<br />
<b>members</b> is an object that represents a collection of class members in key-value pairs<br />
<b>onClassCreated</b> is an optional function callback that is invoked when all dependencies of the defined class are ready and the class itself is fully created. Due to the asynchronous nature of class creation, this callback can be useful in many situations.<br />
<br />
<span style="color: #3d85c6; font-size: large;"><b><u>The config member</u></b></span><br />
<br />
Extjs classes have a special property called config. If you put properties inside this property, then you can call set and get on them. You also can define an apply on these properties that run as soon as a set is called on them. The set get and apply names are by convention (see example below)<br />
<br />
Ext.application({<br />
name: 'Fiddle',<br />
<br />
launch: function() {<br />
<br />
<br />
Ext.define("Car", {<br />
company: "Ford",<br />
model: "Mustang",<br />
<span style="color: #674ea7;"> config: {</span><br />
<span style="color: #674ea7;"> year: 2007</span><br />
<span style="color: #674ea7;"> },</span><br />
<br />
<span style="color: #674ea7;"> applyYear: function(year) {</span><br />
<span style="color: #674ea7;"> alert("applying year");</span><br />
<span style="color: #674ea7;"> return year;</span><br />
<span style="color: #674ea7;"> }</span><br />
<br />
});<br />
<br />
var myCar = Ext.create("Car");<br />
<br />
myCar.setYear(2010);<br />
alert(myCar.getYear()); //Displays 2010<br />
<br />
myCar.setCompany("Toyota"); //fails because not inside config<br />
myCar.getCompany(); //fails because not inside config<br />
}<br />
<br />
<br />
});</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-18930594329755725362015-05-26T17:47:00.003-04:002015-05-27T08:54:18.812-04:00Extjs TaskRunner Example (Extjs delay)<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; font-family: 'Courier New'; text-align: left;">
<span style="color: navy; font-weight: bold;">function </span><span style="font-style: italic;">ConsoleWrite</span>() {<br />
<span style="color: #660e7a; font-weight: bold;">console</span>.<span style="color: #7a7a43;">log</span>(<span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.<span style="color: #660e7a; font-weight: bold;">Date</span>.<span style="color: #7a7a43;">format</span>(<span style="color: navy; font-weight: bold;">new </span>Date(), <span style="color: green; font-weight: bold;">'g:i:s A'</span>))<br />
}</div>
<div style="background-color: white; font-family: 'Courier New'; text-align: left;">
<span style="color: navy; font-weight: bold;"><br /></span></div>
<div style="background-color: white; font-family: 'Courier New'; text-align: left;">
<span style="color: navy; font-weight: bold;">var </span><span style="color: #660e7a; font-style: italic; font-weight: bold;">runner </span>= <span style="color: navy; font-weight: bold;">new </span><span style="color: #660e7a; font-style: italic; font-weight: bold;">Ext</span>.util.TaskRunner();</div>
<div style="background-color: white; font-family: 'Courier New'; text-align: left;">
<br />
<span style="color: #660e7a; font-style: italic; font-weight: bold;">runner</span>.<span style="color: #7a7a43;">start</span>({<br />
<span style="color: #660e7a; font-weight: bold;">run</span>: <span style="font-style: italic;">ConsoleWrite</span>,<br />
<span style="color: #660e7a; font-weight: bold;">interval</span>: <span style="color: blue;">1000</span>});<br />
<br />
<br />
You will see something like this in the chrome F12 tools console<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-7Nxn-aYeu-A/VWTqf_lJO1I/AAAAAAAABes/7El9v0s19vc/s1600/TaskRunner.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-7Nxn-aYeu-A/VWTqf_lJO1I/AAAAAAAABes/7El9v0s19vc/s1600/TaskRunner.png" /></a></div>
<br /></div>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-17995882123286753502015-05-18T12:34:00.001-04:002015-05-18T12:34:05.985-04:00Command Pattern Tutorial C# (Also known as Action or Transaction Pattern)<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #3d85c6; font-size: large;"><b>What is the problem we are trying to solve?</b></span><br />
<br />
Look at the example blow<br />
<br />
<pre style="background: white; font-family: Consolas; font-size: 13px;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Consolas;"><span style="color: blue;">using</span> System;
<span style="color: blue;">namespace</span> CommandPatternTutorial
{
<span style="color: blue;">internal</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Program</span>
{
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args)
{
<span style="color: blue;">var</span> commandExecutor = <span style="color: blue;">new</span> <span style="color: #2b91af;">CommandExecutor</span>();
commandExecutor.ProcessOrders(<span style="color: #a31515;">"Update"</span>);
}
}
<span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CommandExecutor</span>
{
<span style="color: blue;">public</span> <span style="color: blue;">void</span> ProcessOrders(<span style="color: #2b91af;">String</span> commandName)
{
<span style="color: blue;">switch</span> (commandName)
{
<span style="color: blue;">case</span> <span style="color: #a31515;">"Insert"</span>:
Insert();
<span style="color: blue;">break</span>;
<span style="color: blue;">case</span> <span style="color: #a31515;">"Update"</span>:
Update();
<span style="color: blue;">break</span>;
<span style="color: blue;">case</span> <span style="color: #a31515;">"Delete"</span>:
Delete();
<span style="color: blue;">break</span>;
<span style="color: blue;">default</span>:
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Command not found"</span>);
<span style="color: blue;">break</span>;
}
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> Insert()
{
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Insert Order"</span>);
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> Update()
{
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Update Order"</span>);
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> Delete()
{
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Delete Order"</span>);
}
}
}</pre>
</pre>
<br />
If you need to add a new command in there, you would have to modify the CommandExecutor class. This violates the <a href="http://dotnetanalysis.blogspot.com/2011/10/principles-of-design-patterns.html">Solid Open-Closed and the single responsibility design principles.</a> To avoid these violations we will apply the Command and <a href="http://dotnetanalysis.blogspot.com/2013/07/factory-pattern-tutorial-c.html">factory</a> patterns.<br />
<br />
<br />
<b style="color: #3d85c6; font-size: x-large;">Command Pattern</b><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-Aeyl--a0N_M/VVoQLWYrtcI/AAAAAAAABeE/pd_CNc1xcfg/s1600/CommandPattern.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-Aeyl--a0N_M/VVoQLWYrtcI/AAAAAAAABeE/pd_CNc1xcfg/s1600/CommandPattern.png" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
The complete code is given below. The crux of the command pattern is the ICommand interface which has just the Execute method. So command pattern in itself is very simple, simultaneous application of factory pattern is what makes it a bit more harder to understand.<br />
<br />
<h4 style="text-align: left;">
Program.cs</h4>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Interfaces;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">internal</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">class</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Program</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Main(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> commandName = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"UpdateCommand"</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> commandFactory = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">CommandFactory</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> command = commandFactory.GetCommand(commandName);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> command.Execute();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<h4 style="text-align: left;">
ICommand.cs</h4>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Interfaces<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">interface</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Execute();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<h4 style="text-align: left;">
ICommandFactory.cs</h4>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Interfaces;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">interface</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommandFactory</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> GetCommand(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> commandName);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<h4 style="text-align: left;">
CommandFactory.cs</h4>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System.Collections.Generic;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System.Reflection;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Commands;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Interfaces;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">internal</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">class</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">CommandFactory</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> : </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommandFactory</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Dictionary</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Type</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">> _commandsDictionary;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandFactory()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> LoadCommands();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> GetCommand(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> commandName)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> commandName =
commandName.ToLower();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> command;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (_commandsDictionary.ContainsKey(commandName))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Type</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> type = _commandsDictionary[commandName];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (type != </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">null</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> command = (</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">) </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Activator</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">.CreateInstance(type);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">else</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> command = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">NullCommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">else</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> command = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">NullCommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> command;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> LoadCommands()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> _commandsDictionary = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Dictionary</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Type</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">//Get
the interfaces and classes in the current assembly</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Type</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[] typesinCurrentAssembly = </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Assembly</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">.GetExecutingAssembly().GetTypes();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> interfaceName = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">typeof</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">).ToString(); </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">//"ICommand"</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">foreach</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Type</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> type </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">in</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> typesinCurrentAssembly)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">//Find
the types that implement ICommand and add it to the dictionary _commandsDictionary</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (type.GetInterface(interfaceName) != </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">null</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> typeName =
type.Name.ToLower();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">
_commandsDictionary.Add(typeName, type);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<h4 style="text-align: left;">
UpdateCommand.cs</h4>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Interfaces;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Commands<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">internal</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">class</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">UpdateCommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> : </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Execute()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Console</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Update
Order"</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<h4 style="text-align: left;">
NullCommand.cs</h4>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Interfaces;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> CommandPatternTutorial.Commands<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">internal</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">class</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">NullCommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> : </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ICommand</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Execute()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Console</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Command Not
Found"</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
Now if you need to add a new command, all you need to do is write a class that implements ICommand interface.</div>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-58202283867146961472015-04-23T16:49:00.002-04:002015-04-23T16:53:07.454-04:00Java copy one object to another<div dir="ltr" style="text-align: left;" trbidi="on">
You could use the<a href="https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtilsBean.html#copyProperties(java.lang.Object, java.lang.Object)"> copyProperties method in the org.apache.commons.beanutils.BeanUtilsBean class </a>to copy properties by name from one class to another. If some property name doesn't match, it simply ignores that property.<br />
<br />
In the example below I am copying properties of Ferrari into Toyota.<br />
<br />
<br />
<span style="color: #3d85c6; font-size: large;"><b>Main Program</b></span><br />
<span style="font-family: inherit;"><br />
</span><br />
<pre style="background-color: white;"><span style="font-family: inherit;"><span style="color: navy; font-weight: bold;">package </span>com.company;
<span style="color: navy; font-weight: bold;">import </span>org.apache.commons.beanutils.BeanUtilsBean;
<span style="color: navy; font-weight: bold;">import </span>java.io.IOException;
<span style="color: navy; font-weight: bold;">import </span>java.lang.reflect.InvocationTargetException;
<span style="color: navy; font-weight: bold;">public class </span>Main {
<span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) <span style="color: navy; font-weight: bold;">throws </span>IOException, InvocationTargetException, IllegalAccessException {
Ferrari ferrari = <span style="color: navy; font-weight: bold;">new </span>Ferrari();
ferrari.setColor(<span style="color: green; font-weight: bold;">"red"</span>);
ferrari.setHorsePower(<span style="color: blue;">300</span>);
Toyota toyota = <span style="color: navy; font-weight: bold;">new </span>Toyota();
BeanUtilsBean beanUtilsBean = <span style="color: navy; font-weight: bold;">new </span>BeanUtilsBean();
beanUtilsBean.copyProperties(toyota, ferrari);<span style="color: grey; font-size: 12pt; font-style: italic;">//copy ferrari into toyota</span>
System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(toyota.getColor());<span style="color: grey; font-style: italic;">//red</span></span></pre>
<pre style="background-color: white;"><span style="font-family: inherit;"><span style="color: grey; font-style: italic;"> </span>System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(toyota.getHorsePower());<span style="color: grey; font-style: italic;">//300</span><span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;"> </span>}
}</span></pre>
<br />
<br />
<b><span style="color: #3d85c6; font-size: large;">Ferrari class</span></b><br />
<span style="font-size: x-small;"><br /></span>
<br />
<pre style="background-color: white;"><span style="font-family: inherit;"><span style="color: navy; font-weight: bold;">package </span>com.company;
<span style="color: navy; font-weight: bold;">public class </span>Ferrari {
<span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">HorsePower</span>;
<span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">Color</span>;
<span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">mileage</span>;
<span style="color: navy; font-weight: bold;">public int </span>getHorsePower() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">HorsePower</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setHorsePower(<span style="color: navy; font-weight: bold;">int </span>horsePower) {
<span style="color: #660e7a; font-weight: bold;">HorsePower </span>= horsePower;
}
<span style="color: navy; font-weight: bold;">public </span>String getColor() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">Color</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setColor(String color) {
<span style="color: #660e7a; font-weight: bold;">Color </span>= color;
}
<span style="color: navy; font-weight: bold;">public int </span>getMileage() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">mileage</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setMileage(<span style="color: navy; font-weight: bold;">int </span>mileage) {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">mileage </span>= mileage;
}
}</span></pre>
<br />
<b><span style="color: #3d85c6; font-size: large;">Toyota class</span></b><br />
<span style="font-family: inherit;"><br />
</span><br />
<pre style="background-color: white;"><span style="font-family: inherit;"><span style="color: navy; font-weight: bold;">package </span>com.company;
<span style="color: navy; font-weight: bold;">public class </span>Toyota{
<span style="color: navy; font-weight: bold;">private int </span><span style="color: #660e7a; font-weight: bold;">HorsePower</span>;
<span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">Color</span>;
<span style="color: navy; font-weight: bold;">private </span>String <span style="color: #660e7a; font-weight: bold;">useLess</span>=<span style="color: green; font-weight: bold;">""</span>;
<span style="color: navy; font-weight: bold;">public int </span>getHorsePower() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">HorsePower</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setHorsePower(<span style="color: navy; font-weight: bold;">int </span>horsePower) {
<span style="color: #660e7a; font-weight: bold;">HorsePower </span>= horsePower;
}
<span style="color: navy; font-weight: bold;">public </span>String getColor() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">Color</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setColor(String color) {
<span style="color: #660e7a; font-weight: bold;">Color </span>= color;
}
<span style="color: navy; font-weight: bold;">public </span>String getUseLess() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">useLess</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>setUseLess(String useLess) {
<span style="color: navy; font-weight: bold;">this</span>.<span style="color: #660e7a; font-weight: bold;">useLess </span>= useLess;
}
}</span></pre>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-86068838089164683892015-01-28T14:14:00.001-05:002015-01-28T14:16:32.747-05:00C# Simple HttpClient Console application<div dir="ltr" style="text-align: left;" trbidi="on">
In this example we will get content from google.com using the <a href="https://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.118%29.aspx">System.Net.Http.HttpClient</a> class<br />
<br />
Create a new .net console app in Visual Studio 2012. Add a reference to System.Net.http.dll (.net version 4.0)<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-MlpRbrCF-Ts/VMk0s6BrRQI/AAAAAAAABaI/zaHemULg2PA/s1600/Untitled.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-MlpRbrCF-Ts/VMk0s6BrRQI/AAAAAAAABaI/zaHemULg2PA/s1600/Untitled.png" height="640" width="609" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
Copy paste the code below.<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System.Net.Http;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">using</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> System.Threading.Tasks;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> HttpClientTest<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">internal</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">class</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Program</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Main(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> httpClient = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">HttpClient</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Task</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> asyncThread = httpClient.GetAsync(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"https://www.google.com"</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">).<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> ContinueWith(taskWithResponse
=><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">HttpResponseMessage</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">
response = taskWithResponse.Result;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Task</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">> readTask =
response.Content.ReadAsStringAsync();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Console</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">.WriteLine(readTask.Result);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> );<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> asyncThread.Wait();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">}</span><o:p></o:p></div>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-78909140215138931282015-01-25T16:20:00.000-05:002015-01-25T16:20:11.181-05:00Accessing Tomcat manager<div dir="ltr" style="text-align: left;" trbidi="on">
To be able to access your tomcat manager app at https://localhost:8443/ add this to<br />
apache-tomcat-7.0.55\conf\tomcat-users.xml<br />
<br />
<tomcat-users><br />
<role rolename="manager-gui"/><br />
<role rolename="manager-script"/><br />
<role rolename="manager-jmx"/><br />
<role rolename="manager-status"/><br />
<role rolename="admin-gui"/><br />
<role rolename="admin-script"/><br />
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/><br />
</tomcat-users><br />
<br />
Now you can access everything in the manager gui using<br />
<b>username</b>: tomcat<br />
<b>password</b>: tomcat</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-75272077694662062492014-12-09T18:45:00.003-05:002014-12-09T18:45:56.313-05:00Redirect tomcat console log output to a file<div dir="ltr" style="text-align: left;" trbidi="on">
1>Set the environment variable CATALINA_HOME to point to whereever your tomcat is installed. For me that was C:\Program Files\apache-tomcat-7.0.55<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-fC1tJuyvQHA/VIeIuHMbkwI/AAAAAAAABX8/nuQg6jl_-vQ/s1600/catalina_home.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-fC1tJuyvQHA/VIeIuHMbkwI/AAAAAAAABX8/nuQg6jl_-vQ/s1600/catalina_home.png" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
2>Save the script below as a *.ps1<br />
<br />
<div style="background-color: #e7e6dd;">
clear-host<br />
<br />
$catalinaBat = $env:CATALINA_HOME+"\bin\catalina.bat"<br />
<br />
$logOutputPath="$env:CATALINA_HOME"+"\logs\"<br />
<br />
$logOutputFile= "catalina.out"<br />
<br />
<br />
<br />
<br />
set-location "$logOutputPath"<br />
<br />
Start-Process -FilePath "$catalinaBat" -ArgumentList "run > $logOutputFile 2>&1"<br />
<br />
write-host "Your tomcat log is being written out to "$logOutputPath$logOutputFile</div>
<br />
<br />
This script will run launch tomcat and write the output to a file.</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-41865590165139512062014-11-30T19:21:00.001-05:002014-11-30T20:54:24.721-05:00C# Return data from a thread<div dir="ltr" style="text-align: left;" trbidi="on">
The easiest way to get a return value from a thread in C# is to start it as a Task.<br />
<br />
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: #2b91af;">String</span>[] args)
{
<span style="color: #2b91af;">Task</span><<span style="color: blue;">int</span>> task = <span style="color: #2b91af;">Task</span>.Run(() => Add(1, 2));
<span style="color: blue;">int</span> result = task.Result;
}
<span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">int</span> Add(<span style="color: blue;">int</span> a, <span style="color: blue;">int</span> b)
{
<span style="color: blue;">return</span> a + b;
}</pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;">You can also add multiple lines of code to the task</span></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;">
</span></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Consolas;"><span style="color: #2b91af;">Task</span><<span style="color: blue;">int</span>> task = <span style="color: #2b91af;">Task</span>.Run(() =>
{
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Adding.."</span>);
<span style="color: blue;">return</span> Add(1, 2);
});</pre>
</pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;">
</span></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;">Note here that </span><span style="color: blue;">int</span> result = task.Result;<span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;"> blocks the main thread until the result is obtained.</span></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;">
</span></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;">
</span></pre>
<pre style="background: white; font-family: Consolas;"><span style="color: #3d85c6; font-family: 'Times New Roman'; font-size: large; white-space: normal;"><b><u>Catching Exceptions in tasks</u></b></span></pre>
<pre style="background: white; font-family: Consolas;"><span style="color: #3d85c6; font-family: 'Times New Roman'; font-size: large; white-space: normal;"><b><u>
</u></b></span></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><span style="font-family: 'Times New Roman'; font-size: small; white-space: normal;">
</span></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Consolas;"><span style="color: blue;"> try</span>
{
<span style="color: #2b91af;">Task</span><<span style="color: blue;">int</span>> task = <span style="color: #2b91af;">Task</span>.Run(() => Add(1, 2));
task.Wait();
}
<span style="color: blue;">catch</span> (<span style="color: #2b91af;">AggregateException</span> ex)
{
<span style="color: #2b91af;">Console</span>.WriteLine(ex.InnerException.Message);
}</pre>
</pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"></pre>
<pre style="background: white; font-family: Consolas;"><u><b><span style="color: #3d85c6; font-size: large;">Asynchronously running a thread</span></b></u></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;">You don't have to wait for the result. You can kick off the process to fetch the result and then continue doing something else. You can then run some other code when the result is finally available.</pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Consolas;"></pre>
<div class="separator" style="clear: both; text-align: center;">
</div>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Consolas;"></pre>
</pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"></pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Consolas;"><span style="color: blue;">using</span> System;
<span style="color: blue;">using</span> System.Runtime.CompilerServices;
<span style="color: blue;">using</span> System.Threading;
<span style="color: blue;">using</span> System.Threading.Tasks;
<span style="color: blue;">internal</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Test</span>
{
<span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: #2b91af;">String</span>[] args)
{
<span style="color: #2b91af;">Task</span><<span style="color: blue;">int</span>> task = <span style="color: #2b91af;">Task</span>.Run(() => Add(1, 2));
<span style="color: #2b91af;">TaskAwaiter</span><<span style="color: blue;">int</span>> awaiter = task.GetAwaiter();
awaiter.OnCompleted(() =>
{
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"The result is </span><span style="color: mediumseagreen;">{0}</span><span style="color: #a31515;">"</span>, awaiter.GetResult());
});
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"past awaiter"</span>);
<span style="color: #2b91af;">Thread</span>.Sleep(20000);
<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"The end"</span>);
}
<span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">int</span> Add(<span style="color: blue;">int</span> a, <span style="color: blue;">int</span> b)
{
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = 0; i < <span style="color: blue;">int</span>.MaxValue; i++)
{
<span style="color: blue;">int</span> x = 10;
}
<span style="color: blue;">return</span> a + b;
}
}</pre>
</pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;">
</pre>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-CLIb9Vo3rHY/VHvKR4FGETI/AAAAAAAABXs/J7cAigI6DHk/s1600/Untitled.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-CLIb9Vo3rHY/VHvKR4FGETI/AAAAAAAABXs/J7cAigI6DHk/s1600/Untitled.png" height="172" width="640" /></a></div>
<pre style="background: white; font-family: Consolas; font-size: 13px;">
</pre>
<pre style="background: white; font-family: Consolas; font-size: 13px;"></pre>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-41659400500059727612014-11-09T23:08:00.001-05:002014-11-10T19:21:03.327-05:00Logstash config for IIS logs<div dir="ltr" style="text-align: left;" trbidi="on">
Taking help from <a href="https://adammills.wordpress.com/2014/02/21/logstash-and-iis/">this </a>article, I came up with a logstash.conf file that works both for IIS and apache tomcat at the same time.<br />
<br />
<br />
<div style="background-color: #e7e6dd;">
<br />
<br />
input {<br />
<br />
file { <br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> path => ["C:/inetpub/logs/LogFiles/W3SVC1/*.log"]<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> type => ["iislog"]<br />
}<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>file { <br />
<br />
path => ["C:/Program Files/apache-tomcat-7.0.55/logs/*.txt"]<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> type => ["tomcatTxtLog"]<br />
}<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
}<br />
<br />
<br />
filter {<br />
if [type] == "iislog" {<br />
<br />
#ignore log comments<br />
if [message] =~ "^#"<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> {<br />
drop {}<br />
}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> grok {<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> match => ["message", "%{TIMESTAMP_ISO8601:log_timestamp} %{WORD:iisSite} %{IPORHOST:site} %{NOTSPACE:Sip} %{NOTSPACE:verb} %{URIPATH:request} %{NOTSPACE:QueryString} %{NUMBER:port} %{NOTSPACE:Hyphen1} %{NOTSPACE:Cip} %{NOTSPACE:httpversion} %{NOTSPACE:UserAgent} %{NOTSPACE:Hyphen2} %{NOTSPACE:Hyphen3} %{NOTSPACE:referer} %{NUMBER:response} %{NUMBER:subresponse} %{NUMBER:scstatus} %{NUMBER:bytes:int} %{NUMBER:timetaken:int}"]<br />
}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> date<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> {<br />
match => [ "log_timestamp", "YYYY-MM-dd HH:mm:ss" ]<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> timezone => "Etc/GMT"<br />
}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> mutate {<br />
remove_field => [ "Hyphen1","Hyphen2","Hyphen3","Sip","Cip","log_timestamp"]<br />
}<br />
}<br />
else if [type] == "tomcatTxtLog" {<br />
#ignore log comments<br />
if [message] =~ "^#"<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> {<br />
drop {}<br />
}<br />
grok {<br />
match => ["message", "%{COMMONAPACHELOG}"]<br />
}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> date<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> { <br />
match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]<br />
timezone => "Etc/GMT"<br />
}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> mutate {<br />
remove_field => [ "timestamp"]<br />
}<br />
<br />
}<br />
}<br />
<br />
output{<br />
elasticsearch {<br />
cluster=>"VivekLocalMachine"<br />
port => "9200"<br />
protocol => "http"<br />
}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
<br />
<br /></div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-7332685095645233332014-11-05T17:04:00.002-05:002015-06-23T09:09:45.983-04:00Viewing tomcat logs with Logstash in a Windows 7 machine (Logstash beginner level tutorial/walkthrough)<div dir="ltr" style="text-align: left;" trbidi="on">
If you want to monitor logs from multiple sources, for example tomcat, IIS and so on from a single location, logstash is your friend. The way logstash works is that it takes log data from multiple sources and formats all of them into the same format and pushes that data for storage into elastic search. You then use Kibana to monitor that data.<br />
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-LGFjENxNOZw/VFlkxunIqSI/AAAAAAAABV4/bfTN3FhTuDw/s1600/LogStash.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="132" src="http://4.bp.blogspot.com/-LGFjENxNOZw/VFlkxunIqSI/AAAAAAAABV4/bfTN3FhTuDw/s1600/LogStash.jpg" width="640" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
In this article we will just look at a very basic example to help you get started. Do these steps to follow along</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Prerquisites</h3>
<div>
<ul style="text-align: left;">
<li>You need jdk (I have jdk1.7.0_67)</li>
<li>You need to have apache tomcat installed</li>
</ul>
</div>
<div>
<br /></div>
<div style="text-align: left;">
<span style="color: #3d85c6; font-size: large;"><b><u>Steps</u></b></span></div>
<div style="text-align: left;">
<br /></div>
<div>
<b>Install logstash</b></div>
<div>
<br /></div>
<div>
<a href="https://download.elasticsearch.org/logstash/logstash/logstash-1.4.2.tar.gz">Download Logstash-1.4.2</a></div>
<div>
<br /></div>
<div>
Unzip that file and copy it into C:\Program Files</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-FKZ94twJj5Y/VFloLpmNIuI/AAAAAAAABWM/J5yHCS3yREY/s1600/copyLogStash.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-FKZ94twJj5Y/VFloLpmNIuI/AAAAAAAABWM/J5yHCS3yREY/s1600/copyLogStash.png" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div style="text-align: left;">
<h3 style="text-align: left;">
<b>Install and start up Elastic search</b></h3>
</div>
<div>
<br /></div>
<div>
<a href="https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.3.4.zip">Download elasticsearch 1.3.4</a></div>
<div>
<br /></div>
<div>
Unzip it and copy it into C:\Program Files\elasticsearch-1.3.4</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-QQSOag1xlms/VFlomECgvBI/AAAAAAAABWU/-gWaORi4hhY/s1600/copyElasticSearch.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-QQSOag1xlms/VFlomECgvBI/AAAAAAAABWU/-gWaORi4hhY/s1600/copyElasticSearch.png" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br />
Open C:\Program Files\elasticsearch-1.3.4\config\elasticsearch.yml in notepad<br />
Change clustername to whatever you like. I set mine to VivekLocalMachine<br />
<br />
<br />
<div style="background-color: #e7e6dd;">
################################### Cluster ###################################<br />
<br />
# Cluster name identifies your cluster for auto-discovery. If you're running<br />
# multiple clusters on the same network, make sure you're using unique names.<br />
#<br />
cluster.name: VivekLocalMachine</div>
</div>
<div>
<br />
Double click C:\Program Files\elasticsearch-1.3.4\bin\elasticsearch.bat to start up elastic search service.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-AoozCKmNtiE/VFqgRoXcBJI/AAAAAAAABWk/_LsOeUuQl-k/s1600/ElasticSearch.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="98" src="http://4.bp.blogspot.com/-AoozCKmNtiE/VFqgRoXcBJI/AAAAAAAABWk/_LsOeUuQl-k/s1600/ElasticSearch.png" width="640" /></a></div>
<br /></div>
<div>
<br /></div>
<div>
<h3 style="text-align: left;">
<b>Install Kibana and point it to the elastic search service</b></h3>
</div>
<div>
<br /></div>
<div>
<a href="https://download.elasticsearch.org/kibana/kibana/kibana-3.1.1.zip">Download Kibana 3.1.1</a></div>
<div>
<br /></div>
<div>
Copy Kibana to your tomcat webapps folder, for me that was C:\Program Files\apache-tomcat-7.0.55\webapps<br />
<br />
Overwrite<br />
C:\Program Files\apache-tomcat-7.0.55\webapps\<b>kibana-3.1.1\app\dashboards\default.json</b><br />
with the contents of<br />
C:\Program Files\apache-tomcat-7.0.55\webapps\<b>kibana-3.1.1\app\dashboards\logstash.json</b></div>
<div>
<br /></div>
<div>
Open C:\Program Files\apache-tomcat-7.0.55\webapps\<b>kibana-3.1.1\config.js</b> in notepad.</div>
<div>
Check the value of elasticsearch. That is where Kibana would be looking for elastic search database.</div>
<div>
<br /></div>
<div>
Open kibana web ui in browser. You can open it directly from tomcat manager. For me that url was</div>
<div>
http://localhost:8080/kibana-3.1.1/#/dashboard/file/default.json</div>
<div>
<br />
<br />
If you are hosting Kibana in IIS instead of tomcat, you need to <a href="http://stackoverflow.com/questions/8158193/how-to-allow-download-of-json-file-with-asp-net">add json mime type to IIS</a>.</div>
<div>
<br />
<h3 style="text-align: left;">
<b>Set logstash configuration to read tomcat log and pump that data into elastic search</b></h3>
<br />
First we need to create a *.conf file (you can give it any name). This file is what will tell logstash where to read the data from and where to send that data to. I am going to create a file called logstash.conf in C:\Program Files\logstash-1.4.2\bin.<br />
<br />
<br /></div>
<div>
Logstash conf file consists of three parts shown below<br />
<br />
input{<br />
<br />
}<br />
filter<br />
{<br />
<br />
}<br />
output<br />
{<br />
<br />
}<br />
<br />
<b>Input</b>: This tells logstash where the data is coming from. For example File, eventlog, twitter, tcp and so on. <a href="http://logstash.net/docs/1.4.2/">All the supported inputs can be found here</a>.</div>
<div>
<br />
<b>Filter</b>: This tells logstash what you want to do to the data before you output it into your log store (in our case elasticsearch). This accepts regular expressions. Most people use precreated regular expressions called <a href="http://logstash.net/docs/1.4.2/filters/grok">grok</a>, instead of writing their own regular expressions to parse log data. The complete list of filters you can use can be seen <a href="http://logstash.net/docs/1.4.2/">here</a>.<br />
<br />
<b><a href="http://logstash.net/docs/1.4.2/outputs/elasticsearch">Output</a>: </b>This tells logstash where to putput this filtered data to. We are going to output it into elasticsearch. You can have multiple outputs if you want.<br />
<br />
<br />
This is how my logstash.conf looks like
<br />
<div style="background-color: #e7e6dd;">
<br />
<br />
input {<br />
<br />
file { <br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> path => ["C:/Program Files/apache-tomcat-7.0.55/logs/*.log"]<br />
}<br />
<br />
}<br />
<br />
filter {<br />
<br />
<br />
}<br />
<br />
output{<br />
<br />
elasticsearch {<br />
cluster=>"VivekLocalMachine"<br />
port => "9200"<br />
protocol => "http"<br />
}<br />
}<br />
<br />
<br /></div>
<br />
The cluster name should match what you set in C:\Program Files\elasticsearch-1.3.4\config\elasticsearch.yml<br />
<br />
<span style="color: red;"><b>Many times when I made changes to my logstash.conf , I have had to restart the machine that hosted logstash, for these changes to take effect. Restarting logstash didn't always help.</b></span><br />
<br />
<br />
<h3 style="text-align: left;">
<b>Start up logstash</b></h3>
<br />
In the command prompt cd into<br />
C:\Program Files\logstash-1.4.2\bin<br />
<br />
Then run this<br />
<div style="background-color: #e7e6dd;">
logstash.bat agent -f logstash.conf</div>
<br />
<br />
<br />
Now you should start seeing data in real time in your kibana website. For me that url is<br />
http://localhost:8080/kibana-3.1.1/index.html</div>
<div>
<br /></div>
<div>
<h3 style="text-align: left;">
Adding a filter in logstash conf file</h3>
</div>
<div>
<div style="background-color: white;">
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<br /></div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
Lets take this example one step further by adding a filter in the C:\Program Files\logstash-1.4.2\bin\l<b>ogst<wbr></wbr>ash.conf </b>file. </div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<br /></div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
We will be adding a "<a href="http://logstash.net/docs/1.4.2/filters/grok" style="color: #1155cc;" target="_blank">grok</a>" filter. The purpose of adding this filter is to format the log text into a format that is easier to read. Log stash comes with many pre created regular expressions in </div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<br /></div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
C:\Program Files\logstash-1.4.2\patterns\</div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<br /></div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
All these regular expressions are given names in these patterns files. We will be using those names in our grok filter.<br />
<br />
Given below in the logstash.conf file with a filter applied. </div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<br /></div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
</div>
<div style="background-color: #e7e6dd;">
input {<br />
<br />
file { <br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> path => ["C:/Program Files/apache-tomcat-7.0.55/logs/*.txt"]<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> type => ["tomcatTxtLog"]<br />
}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
}<br />
<br />
filter {<br />
grok {<br />
match => ["message", "%{COMMONAPACHELOG:myCustomColumn}"]<br />
}<br />
}<br />
<br />
<br />
output{<br />
<br />
elasticsearch {<br />
cluster=>"VivekLocalMachine"<br />
port => "9200"<br />
protocol => "http"<br />
}<br />
}<br />
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<br /></div>
<div style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<br /></div>
</div>
</div>
</div>
<br />
This is how the kibana output looks like with the filter applied.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-rN5_wVGDduI/VFwHw21ll3I/AAAAAAAABW0/6PPt6Jf1RjQ/s1600/GrokFilter.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="338" src="http://4.bp.blogspot.com/-rN5_wVGDduI/VFwHw21ll3I/AAAAAAAABW0/6PPt6Jf1RjQ/s1600/GrokFilter.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Use<a href="http://grokdebug.herokuapp.com/"> this tool </a>to play around with grok filters.<br />
<br />
To test the example above put these values into the grok Debugger<br />
<br />
<div style="background-color: #e7e6dd; border-style: inset; width: 600px;">
0:0:0:0:0:0:0:1 - - [30/Oct/2014:17:08:41 -0400] "GET / HTTP/1.1" 200 11418</div>
<br />
<br />
<div style="background-color: #e7e6dd; border-style: inset; width: 600px;">
%{COMMONAPACHELOG}</div>
<br />
<br />
To check the indices created by logstash in elastic search go to this url<br />
http://localhost:9200/_cat/indices?v<br />
<br />
<span style="background-color: white;">Check </span><a href="http://logstash.net/docs/1.4.2/filters/grok" style="background-color: white;">this for a more detailed explanation on logstash filters</a><span style="background-color: white;">.</span><br />
<span style="background-color: white;">Check <a href="http://logstash.net/docs/1.4.2/configuration">this for more on logstash configuration files.</a></span></div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com1tag:blogger.com,1999:blog-7997402329705633660.post-24286763856456051622014-10-13T19:54:00.003-04:002014-12-03T12:04:46.220-05:00Powershell replace files<div dir="ltr" style="text-align: left;" trbidi="on">
The powershell script below replaces all occurrences of the file Hello.txt in the path C:\Users\menonv\Desktop with that from C:\test\Hello.txt<br />
<br />
<br />
<b>Clear-Host</b><br />
<br />
<b>$searchPath= "C:\Users\menonv\Desktop"</b><br />
<b>$fileToBeReplaced="Hello.txt"</b><br />
<b>$replacementFile="C:\test\Hello.txt"</b><br />
<br />
<b><br /></b>
<b>set-location $searchPath</b><br />
<br />
<b><br /></b>
<b>$ReplaceTheseFiles=get-childitem $fileToBeReplaced -rec</b><br />
<b><br /></b>
<b>foreach ($file in $ReplaceTheseFiles)</b><br />
<b>{</b><br />
<b>Write-Host "Replacing File" $file with $replacementFile</b><br />
<b><br /></b>
<b>Copy-Item $replacementFile $file</b><br />
<b>}</b><br />
<b><br /></b>
</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-91450914386000786302014-09-29T12:30:00.001-04:002015-02-05T11:34:08.017-05:00Git rollback latest changeset<div dir="ltr" style="text-align: left;" trbidi="on">
In git to rollback the latest changeset, you do a git reset hard and specify the commit ID of the changeset before that.<br />
<br />
For example suppose your latest commit id is fd1793a0a8366bdd48a85f7afe4fe26339f1149c. And the commit id of the previous commit is a6755301e8b283a0c7191028bb7611feff003cdd. To roll back fd1793a0a8366bdd48a85f7afe4fe26339f1149c this is what you do<br />
<br />
<br />
<div style="background-color: lightgrey;">
git reset --hard a6755301e8b283a0c7191028bb7611feff003cdd
</div>
<br />
<br />
This points your Head to a6755301e8b283a0c7191028bb7611feff003cdd, effectively rolling back fd1793a0a8366bdd48a85f7afe4fe26339f1149c.</div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-91528622047532369642014-09-15T16:26:00.002-04:002016-11-12T19:46:54.874-05:00Debug tomcat web apps using intellij (IntelliJ attach to process)<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
1>Go to Run>Edit Configurations</div>
<br />
Create a configuration under remote<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-KfqVG2tEjJ0/VBdJy1nwPaI/AAAAAAAABQQ/lhae_H8x8mA/s1600/Untitled.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="409" src="https://2.bp.blogspot.com/-KfqVG2tEjJ0/VBdJy1nwPaI/AAAAAAAABQQ/lhae_H8x8mA/s1600/Untitled.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
In "Search sources using Module's classpath", select the project that you want to debug<br />
<br />
(In your catalina.bat you will have something like this set JPDA_ADDRESS=localhost:8000)<br />
<br />
<div style="text-align: left;">
2>Start tomcat server using the command (the switch jpda is what enables debugging)</div>
<div style="text-align: left;">
<b>catalina.bat jpda start</b><br />
<br />
You can also use the below command, if you want to make a copy of tomcat console into a file<br />
<b>catalina.bat jpda run > catalina.out </b></div>
<br />
<div style="text-align: left;">
3>Click the bug symbol on the upper right corner of intelliJ</div>
<div style="text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-KJc8vPJ9nsc/VBdK0PkfFVI/AAAAAAAABQY/pvMYaCIQNQo/s1600/Untitled.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://2.bp.blogspot.com/-KJc8vPJ9nsc/VBdK0PkfFVI/AAAAAAAABQY/pvMYaCIQNQo/s1600/Untitled.png" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
4> If everything goes well, you should this in your console.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-22JkG4vmPSM/VBdMiIx50QI/AAAAAAAABQg/w4NmPjtLnCY/s1600/Untitled.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://1.bp.blogspot.com/-22JkG4vmPSM/VBdMiIx50QI/AAAAAAAABQg/w4NmPjtLnCY/s1600/Untitled.png" /></a></div>
<br />
<br />
<br />
<br /></div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0tag:blogger.com,1999:blog-7997402329705633660.post-85479838310634631792014-09-14T13:53:00.001-04:002014-09-14T13:53:28.385-04:00Enabling SQL trace on PostgreSQL 9.3 (on 64 bit Windows 7)<div dir="ltr" style="text-align: left;" trbidi="on">
Edit C:\Program Files\PostgreSQL\9.3\data\postgresql.conf and update the following values<br />
<br />
#log_statement = 'all'<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
#log_min_error_statement = error<br />
<br />
<br />
Restart the service postgresql-x64-9.3<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-Ut6apZa3QPM/VBXUzZMiIlI/AAAAAAAABQA/8pxUUB6r-kU/s1600/postGresService.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-Ut6apZa3QPM/VBXUzZMiIlI/AAAAAAAABQA/8pxUUB6r-kU/s1600/postGresService.png" height="55" width="400" /></a></div>
<br />
<br />
<br />
<br />
<br />
Now you can see all the SQL queries against postgreSQL DB in<br />
<br />
C:\Users\<username>\AppData\Roaming\postgresql\pgadmin_histoqueries.xml<br />
<br />
<br />
<br />
<br /></div>
Vivekhttp://www.blogger.com/profile/08541974152182708949noreply@blogger.com0