<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>On the BENch</title>
	<atom:link href="http://bposerow.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bposerow.wordpress.com</link>
	<description>Thoughts about programming and other things</description>
	<lastBuildDate>Wed, 13 Jan 2010 04:55:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='bposerow.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>On the BENch</title>
		<link>http://bposerow.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://bposerow.wordpress.com/osd.xml" title="On the BENch" />
	<atom:link rel='hub' href='http://bposerow.wordpress.com/?pushpress=hub'/>
		<item>
		<title>The Factory Pattern In C# and Java</title>
		<link>http://bposerow.wordpress.com/2010/01/13/the-factory-pattern-in-c-and-java/</link>
		<comments>http://bposerow.wordpress.com/2010/01/13/the-factory-pattern-in-c-and-java/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 04:55:50 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bposerow.wordpress.com/?p=75</guid>
		<description><![CDATA[In my Java coding throughout the years, I have made extensive use of factories to produce desired objects. Of course, in recent years, the rise of dependency injection frameworks, particularly Spring, have eliminated the need for some of this. However, Spring does not really eliminate the factory; it simply becomes the factory itself. Furthermore, sometimes [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=75&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my Java coding throughout the years, I have made extensive use of factories to produce desired objects.  Of course, in recent years, the rise of dependency injection frameworks, particularly Spring, have eliminated the need for some of this.  However, Spring does not really eliminate the factory; it simply becomes the factory itself.  Furthermore, sometimes you need to perform some specialized object initialization beyond what Spring can support; then you need to either use a Spring factory method or a factory bean.  And then you are back to a factory.</p>
<p>I was working on a C# project recently, and we were discussing my architecture.  He was struck by my use of factory classes and told me that in the .NET world, developers simply do not create factory classes.  Instead, they will create static methods or extension methods, such as a static Create() method on a domain object.  He pointed out that this has the benefit of simplicity:  the maintainer of the code only has to get their head around one class (the object created with the factory method) rather than two classes (an object and its factory).  Also, you get the benefit of your code editor:  a developer using your code does not even need to know about your factory method initially but will discover it via Intellisense.    </p>
<p>These are certainly reasonable points, but of course, by using static factory methods, we lose the benefit of polymorphism.  What if we want a class to be provided a factory so it can create objects, but the factory that it is provided could be one of several variants?  For example, say I wanted a Worker class to create a IContentProvider according to some IContentProviderFactory that is injected into it.  There could be multiple implementations of IContentProvider:  FileContentProvider, DbContentProvider, etc.  Static Create() methods on the FileContentProvider, DbContentProvider, etc. would force the Worker class to be aware of the various types of content providers rather than be agnostic of them.  </p>
<p>In this case, if we are just producing one object dynamically at runtime, we could provide a delegate or lambda property or constructor parameter on our object.  The method passed in could create the desired object and therefore serve as a factory.</p>
<p>However, if we have something more like an abstract factory pattern, in which we wish to have a factory that creates a series of related objects and you have more than one implementation of this factory, then this technique will not work very cleanly.   In this case, we really DO need a factory class.  </p>
<p>Any thoughts?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/75/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=75&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2010/01/13/the-factory-pattern-in-c-and-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
		<item>
		<title>Multifactor Authentication</title>
		<link>http://bposerow.wordpress.com/2009/11/19/multifactor-authentication/</link>
		<comments>http://bposerow.wordpress.com/2009/11/19/multifactor-authentication/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 01:48:15 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Multi factor security]]></category>
		<category><![CDATA[Public-private key]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://bposerow.wordpress.com/?p=54</guid>
		<description><![CDATA[On my current project, there is a need to support multi factor authentication for our web site. For those of you not in the know, this means users always must provide three types (factors) of credentials to authenticate themselves to the site: something they have, something they know, and something they are. The idea is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=54&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On my current project, there is a need to support <strong>multi factor authentication </strong> for our web site.  For those of you not in the know, this means <em>users always must provide three types (factors) of credentials to authenticate themselves to the site:  something they have, something they know, and something they are</em>.  The idea is that adding each factor increases exponentially the difficulty of unauthorized entry into the account.</p>
<p>It should be noted that you can have more than these 3 factors, and this will add additional security.  But at the very least, supplying these three fundamental factors should be a basis for your security model.</p>
<p>I have not always been directly involved (but have been involved with many of the design decisions), but the isuses involved are quite fascinating and I still wanted to share it with you, my faithful blog reader.  By the way, the chief developer on this is Christian Jungers:  www.CM3Consulting.com.  (I am not a representative or employee of CM3 Consulting, and my opinions do<br />
not necessarily reflect theirs, or theirs reflect mine.)</p>
<p>Some interesting design decisions we made:</p>
<h3> The Factors We Chose </h3>
<h4>Who You Are</h4>
<h5>What We Use</h5>
<ul>
<li>We are allowing any arbitrary user to access the site, so we don&#8217;t want to require users to provide biometric data or use other means to assure that we are dealing with a particular user. </li>
<li>Thus we are using the machine that the user is using as a &#8220;proxy&#8221; for the user themselves.</li>
<li>Specifically we use the MAC address of the machine.</li>
</ul>
<h5>How We Obtain it</h5>
<ul>
<li>How do we determine this from a browser?  We bring back the ancient concept of a Java applet <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  </li>
<li>We make use of a small amount of Javascript to grab the MAC address from the applet and place it in a hidden form field which can then be submitted to the server as part of a user login. </li>
<li>By logging in, we add the MAC address to a list of MAC addresses associated with the user and store this in persistent storage.  In this way,  we effectively register the machine and associate it with the user.</li>
</ul>
<p> <b> Preventing Man-in-the-Middle Attacks </b></p>
<ul>
<li>We don&#8217;t want to send the MAC address in the clear to the server, because otherwise a &#8220;man in the middle&#8221; could steal the MAC address and then pretend to be that machine (by spoofing its own MAC address as that address)!</li>
<li> Hence, we need to encrypt the MAC address sent to the user.</li>
<li> <em>Is it sufficient to simply use an HTTPS connection to the server?  No! </em> </li>
<li> The connection always uses the same public key to encrypt content sent over the wire all the time (based on the certificate from the server), so <em>the man in the middle can intercept the encrypted MAC address and then use this to authenticate themselves to the server as if they were the original server</em>.</li>
<li>Thus, we generate a new public/private key pair for each session and use this to encrypt the MAC address.  This makes it very hard for the man-in-the-middle to intercept the encrypted MAC address and do something useful with it.</li>
</ul>
<p><b> A Sample of How We Foil Man-in-the-Middle </b>  </p>
<ol>
<li>User Angel goes to login page.</li>
<li>Among the values passed to the page is a public key that is used to encrypt the MAC address which has been generated by the server for Angel&#8217;s session.
<p>        Let us call the public/private key pair generated public key A and private key A, respectively.  </li>
<li>The MAC address portlet runs and gets the MAC address of the user&#8217;s machine.</li>
<li>The MAC address is encrypted with public key A and is sent to what it thinks is the server.</li>
<li>The man-in-the-middle, named User Mallory (the malicious user), is listening/sniffing the connection and sees the encrypted MAC address on the line.</li>
<li>Mallory goes to the login site but performs some spoofing magic (by tweaking the page and what is sent) so as to send the encrypted MAC address as if it were her own MAC address.</li>
<li> When Mallory goes to the login page, the server generates a new public/private key pair, let us call these public key B and private key B.</li>
<li>When the server receives the encrypted MAC address, she attempts to decrypt it with the private key B.  However, it was originally encrypted with public key A, and thus the decryption fails to match the MAC address that user A registered.</li>
<li>Thus Mallor is unable to login as if she were Angel.</li>
</ol>
<p> <b> Efficiency Measures &#8211; Generating Keys on Server Startup </b> </p>
<ul>
<li>Generating a separate key for each session is time consuming (about 0.5 a second on our dual core desktops).   Of course, this will be considerably faster on the server but this still leaves us vulnerable to denial of service attacks.</li>
<li><em>Instead we can generate a number of keys (say 100 or 1000) on server startup, and then each time we start a new session, we will randomly grab a key out of the pool.</em>  </li>
<li>This makes user login much quicker, but it really doesn&#8217;t compromise security in any meaningful way.</li>
<li> It still would be very hard for an eavesdropper to determine when a key is reused and therefore he would most likely not be able to hijack somebody else&#8217;s session). </li>
</ul>
<p>   <b> Dealing with the Several MAC Addresses in Your Machine </b></p>
<ul>
<li>Actually, your machine does not have just one MAC address but several (corresponding to the various network cards/ network interfaces that your machine has.</li>
<li>Our heuristic: <em> we store all of these MAC addresses but consider a user&#8217;s machine a match as long as at least one of these MAC addresses match</em></li>
</ul>
<p>    <b> Allowing the User to Login with Multiple Machines </b></p>
<ul>
<li>We store a number (8 to be specified) of MAC addresses for each user; if the user logs in with more than the allowed number of machines, roll the oldest machine off the list</li>
<li><em>Should we have instead not let the user log in with more than 8 machines?</em></li>
</ul>
<h4>What You Have</h4>
<h5> What We Use </h5>
<p> <b> First Logging onto a New Machine </b></p>
<ul>
<li>When the user is first created, the user is emailed a temporary password and they must login with it (and then be forced to change the password immediately) </li>
<li>When the user signs in on a new machine, they will be emailed an Auth code which the user will have to provide before being allowed to login with that machine.</li>
</ul>
<p>   <b> Subsequent Logins &#8211; Provide a Cookie </b></p>
<ul>
<li> A revision to what I said above:  actually we don&#8217;t just sign the MAC address.  The user is also provided a random &#8220;seed&#8221; that comes down to the user via a cookie.  </li>
<li> The seed and MAC address are combined, signed, and then stored on the server as one of the machines that is associated with the user.</li>
<li>This cookie thus has to be provided with each subsequent request from that machine.</li>
<li>Thus, whether the user is logging in for the first time, using a machine for the first time, or just logging in other scenarios, the user always has to provide something.</li>
</ul>
<h4>What You Know</h4>
<h5> What We Currently Use </h5>
<ul>
<li>The user generally has to provide a password.</li>
<li>Right now, the user has to provide their email address when they log in which we are considering &#8220;something they know&#8221; when they login for first time.  But this is a weak factor because it is generally publicly known or at least easily known.</li>
<li>The email address would be the only &#8220;thing they know&#8221; when they log in for first time (because the temporary password is provided via email so it is something they have, not something they know). </li>
</ul>
<h5>What We Should Use</h5>
<ul>
<li> <em>Probably better to provide for a series of challenge questions that a call center representative can obtain when initially registering the user.</em> </li>
<li> Then the user will be required to provide answers to them when they log in (thus showing what they know).</li>
</ul>
<p>I know, very involved, but also hopefully very informative of some of the things going through our minds as we implemented this.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=54&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2009/11/19/multifactor-authentication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
		<item>
		<title>LINQ Custom Providers</title>
		<link>http://bposerow.wordpress.com/2009/11/07/linq-custom-providers/</link>
		<comments>http://bposerow.wordpress.com/2009/11/07/linq-custom-providers/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 00:17:31 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bposerow.wordpress.com/?p=50</guid>
		<description><![CDATA[I will be giving a talk tomorrow at .NET Code Camp in Stamford, CT. The presentation, which I had previously given, is located here. For this presentation, I had developed a sample LINQ provider that queries Yahoo Finance for some basic stock and stock option information and forms simple associations between them. I cheated a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=50&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I will be giving a talk tomorrow at .NET Code Camp in Stamford, CT.</p>
<p>The presentation, which I had previously given, is located <a href='http://fairfieldwestchester.net/getattachment/ef76ab33-2984-45ac-963b-422a684ef701/Custom-LINQ-Providers.aspx'>here</a>.</p>
<p>For this presentation, I had developed a sample LINQ provider that queries <a href="http://finance.yahoo.com" /> Yahoo Finance </a> for some basic stock and stock option information and forms simple associations between them.  I cheated a little bit and used Google Finance for one small piece of this as well.</p>
<p>Lessons learned:</p>
<ul>
<li>  Writing a LINQ provider is quite hard.  LINQ basically is providing you something like a framework for compilation of a fairly restricted language, but you are building part of a compiler nonetheless, which is never easy.
<p>Specifically the .NET compiler compiles your LINQ query into something called an expression tree, which is a static representation of the LINQ query code, e.g. what filters are applied by the query, what kinds of objects is it selecting, etc.  So the parsing of the query is done for you.  But you are responsible for the translation and execution of the expression tree, i.e. the actual calls to the real data source, whether this is a web service, database, API, or anything else. </li>
<li> Using a LINQ provider is really easy.  This is the most important part.  You learn LINQ once, and then you know how to use pretty much any LINQ provider.
<p>Look how easy it is to use my new LINQ provider:</p>
<pre>
from stockOption in optionContext.Query
join stock in stockContext.Query on stockOption.UnderlyingStock equals stock
where (stock.Symbol == "IBM" &amp;&amp; stockOption.Strike &gt; 35
                &amp;&amp; stockOption.Premium &gt; 5)
select new {stockOption.Symbol, stockOption.Premium,
                StockSymbol = stock.Symbol,
                stockOption.Strike})
</pre>
<p>First a small bit of financial knowledge.  An option is a right but not an obligation to buy or sell a certain amount of some underlying<br />
instrument, in this case stock for a certain price.  It is time limited (it must be exercised by a certain date or it expires).  So for<br />
a stock option, the underlying instrument is a stock.    The strike price of an option is the price at which you now have to right to buy or sell the stock.  So if I have an option to buy IBM stock for $50 by November 1, and the price of IBM stock rises to $65 on October 15th, I may choose to exercise the stock option.  This would allow me to buy the IBM stock for $50, rather than the $65<br />
current price of the stock.  I could also choose to wait it out, thinking that IBM stock might dip below $50, in which case I could buy<br />
it for cheaper than $50.  However, if the option expires (we pass November 1st) and I have not executed it, the option expires worthless.  One other note:  the option also has a premium, which is the price I have to pay to obtain the option.  Even if the<br />
option becomes worthless, the most I could pay out of pocket is the premium.</p>
<p>It is easily explained what the query is doing above.  I am finding all options currently available with the underlying stock &#8220;IBM&#8221; and then finding all stock options for IBM that have a strike price greater than $35 and for which the premium is greater than $5.   I then return a list of anonymous objects, something like a tuple, that contains the stock symbol, the stock option symbol, and the stock option premium and strike price.  I return one result for each stock option that matches the criteria.  </p>
<p>The key thing here is that while you had to have a little bit of business/domain knowledge to understand the query, you really did not require technical knowledge beyond basic LINQ knowledge to understand it.  Furthermore, you didn&#8217;t even really need LINQ knowledge to understand it, because it looks so much like standard SQL.  </p>
<p><strong>So it is clear (at least anecdotally) that LINQ is designed with the assumption that a few developers will experience the pain of creating providers with the understanding that the masses will benefit from simplicity of use.</strong>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=50&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2009/11/07/linq-custom-providers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
		<item>
		<title>Weak references and in-memory caching</title>
		<link>http://bposerow.wordpress.com/2009/10/30/weak-references-and-in-memory-caching/</link>
		<comments>http://bposerow.wordpress.com/2009/10/30/weak-references-and-in-memory-caching/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 00:43:24 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[Garbage collection]]></category>
		<category><![CDATA[IBatis]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Weak references]]></category>

		<guid isPermaLink="false">http://bposerow.wordpress.com/?p=43</guid>
		<description><![CDATA[In my app, we are using IBatis, and specifically its built in caching. Eventually I plan to use something like EhCache with IBatis, but this works for our purposes for now. So in one of my mapping files, I had the following tag: &#60;cacheModel id="objCache" type="com.ibatis.sqlmap.engine.cache.memory.MemoryCacheController" readOnly="true" serialize="false"&#62; &#60;/cacheModel&#62; I then applied this cache model [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=43&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my app, we are using IBatis, and specifically its built in caching.  Eventually I plan to use something like EhCache with IBatis, but this works for our purposes for now.</p>
<p>So in one of my mapping files, I had the following tag:</p>
<pre>
&lt;cacheModel id="objCache"
        type="com.ibatis.sqlmap.engine.cache.memory.MemoryCacheController"
        readOnly="true" serialize="false"&gt;
 &lt;/cacheModel&gt;
</pre>
<p>I then applied this cache model to one of my result maps:</p>
<pre>
&lt;select id="getSomething" parameterClass="..."
		resultMap="keyValueMap" cacheModel="objCache"&gt;
....
&lt;select&gt;
</pre>
<p>And I noticed an unexpected result: <strong> my query results were not actually being cached</strong>!  In other words, despite the caching, the query was being executed against the database every single time.</p>
<p>The problem had to do with a setting on the IBatis MemoryCacheController:  strong vs. weak caching.  This has to do with a concept that has been there since the Java 1.3 days:  <strong>weak references</strong>.    <a href="http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html">Here&#8217;s</a> one link that explains the various types of Java references:    </p>
<p>We are used to strong references:  when we have a reference to an object, the object will not be garbage collected until the reference is released.  Objects referred to by weak references, on the other hand, can be reclaimed assuming <b> there are no other references to the object </b>.  </p>
<p>By default, the MemoryCacheController uses weak caching by default.  However, because our application only held temporary references to the cached objects (other than the cache itself), the cached objects were reclaimed by the garbage collector.  Hence, the next time IBatis attempted to check the cache associated with a particular query, it did not find anything cached and ran the query again!  Repeat this process, ad infinitum, and you understand why caching seemed to be nonexistent.</p>
<p>So I changed this:</p>
<pre>
&lt;select id="getSomething" parameterClass="..."
		resultMap="keyValueMap" cacheModel="objCache"&gt;
    	&lt;property name="referenceType" value="STRONG"/&gt;
  	&lt;/cacheModel&gt;
</pre>
<p>Note I am now resetting the reference type to STRONG, which causes the cache to use strong references rather than weak references.  This solved the problem, because now the reference to the cached object itself will prevent the cached object from being garbage collected.</p>
<p>It is interesting to note that before I solved the problem, I noticed something strange:  this problem did not occur when I ran the debugger.  In other words, the debugger caused the cache to work correctly!  The cached objects were not being garbage collected, and thus the IBatis cache did not have to run the queries again.  This is not an application of the Heisenberg uncertainty principle (in which watching a quantum phenomenon changes its progression), but actually was due to the compiler itself holding references to the cached objects, whcih caused the objects to remain cached.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=43&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2009/10/30/weak-references-and-in-memory-caching/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
		<item>
		<title>Sharing Sessions Across Hosts WIth Tomcat</title>
		<link>http://bposerow.wordpress.com/2009/10/12/fun-with-cookies-and-tomcat/</link>
		<comments>http://bposerow.wordpress.com/2009/10/12/fun-with-cookies-and-tomcat/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 14:20:10 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[Class loading]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JEE]]></category>
		<category><![CDATA[JEE Portals]]></category>
		<category><![CDATA[Liferay]]></category>
		<category><![CDATA[Sessions]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://bposerow.wordpress.com/?p=12</guid>
		<description><![CDATA[Working with a colleague of mine, we ran into an interesting technical issue involving Liferay portal engine, cookie domains, and Tomcat. This touched on the following areas: Tomcat connectors Application server session management, specifically the management of the JSESSIONID cookie Cookie domains Virtual hosting and clustering Liferay public and private pages Let me step you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=12&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Working with a colleague of mine, we ran into an interesting technical issue involving Liferay portal engine, cookie domains, and Tomcat. </p>
<p>This touched on the following areas:</p>
<ul>
<li>Tomcat connectors </li>
<li>Application server session management, specifically the management of the JSESSIONID cookie </li>
<li>Cookie domains </li>
<li>Virtual hosting and clustering </li>
<li>Liferay public and private pages</li>
</ul>
<p>Let me step you through it.</p>
<p><strong>Liferay and Virtual Hosts</strong> </p>
<p>We started out by making use of Liferay virtual hosts. Liferay organizations represent different deployments of a portal site to discrete groups of users or roles. Virtual hosting in Liferay specifically is a feature in Liferay that enables one to map domain names to Liferay organizations. This became essential in our project, because we turned on clustering, and thus we needed a mechanism to map a common URL to whatever host we are using to serve a user&#8217;s request. One important note to make is that we are making use of sticky sessions (no session replication for now), so all requests made by a given client will be routed to the same server. We also wanted to provide different domain names for different Liferay organizations, so as to make these appear to be completely different sites. We could have accomplished some of these goals via Apache URL mappings, but we wanted to leverage the Liferay support for this (for reasons beyond the scope of this article which I am not even sure are justified).</p>
<p>Liferay has a concept of public and private pages for a given organization. Public pages are pages that can be accessed before a user logs into Liferay, while private pages are only accessible once a user has logged into Liferay. This seems like an inappropriate security architecture to me, in that traditionally, one would provide something like an access control list for each page or groups of pages that defines what access (if any) that a user has to those pages. But this is an issue for another day, another post. I bring this up because Liferay has a weird restriction with their virtual hosting: Liferay seems to make you enter a different virtual host for public pages than for private pages. The rest of our troubles began with that.</p>
<p><strong> The Problem: Liferay, Virtual Hosts, and Session Sharing </strong></p>
<p>In our application, when a user was logging in, we are storing information in the session. We are also causing a &#8220;Liferay login&#8221;, i.e. logging in as a Liferay user, which causes Liferay to allow the user to then access the private pages. The problem we started to notice was that when a user logged in, the information that was added to the session was no longer available once the user logged in. More specifically, it appeared like the session information added while on the public virtual host was not available on the private virtual host.</p>
<p><strong>The Diagnosis: Sharing Sessions Across Different Hosts </strong></p>
<p>The first naive but incorrect answer is that a different server is processing the request to initially log in the user from the request issued after the user is logged in. However, remember this is actually not correct, because we are making use of sticky sessions, so all requests by a given user are handled by the same server. So even though Liferay switches the virtual host being used when the user logs in, all requests are being served by the same underlying server. So this is not being caused by having different sessions on different servers. Probing further &#8230;</p>
<p>So what actually happened? Remember that a Java application server such as Tomcat creates a JSESSIONID cookie on the client&#8217;s desktop with the session id of the current session (unless, of course, we make use of URL rewriting, but we weren&#8217;t). The domain of the JSESSIONID cookie is the entire virtual host name, i.e. if our public virtual host is &#8220;login.xyz.com&#8221; and the private virtual host is &#8220;secure.xyz.com&#8221;, then while on the public site, one JSESSIONID was being stored for domain &#8220;login.xyz.com&#8221; and then on the private site, another JSESSIONID cookie was being stored for domain &#8220;secure.xyz.com&#8221;. Now you can start to see the problem. We are creating two sessions, one on the public virtual host &#8220;login.xyz.com&#8221; and one on the private virtual host &#8220;secure.xyz.com&#8221;. Why?  <em>When the user hits the public site, a JSESSIONID with the domain &#8220;login.xyz.com&#8221; is created. When the user is then redirected to the private site, the domain of the existing JSESSIONID is not available because the domain of that cookie does not match the new host &#8220;secure.xyz.com&#8221;. Thus, it appears as if no session is available, and a new session is created on the server. Correspondingly, a new JSESSIONID cookie is created on the client as well for host secure.xyz.com.</em></p>
<p><strong>First Solution Attempt:  An Incorrect Custom Tomcat Connector Implementation </strong></p>
<p>How can solve this? Well, if I could create a JSESSIONID cookie that had a domain of &#8220;xyz.com&#8221;, then such a cookie would match both the public and private sites, because both host names are suffixed with xyz.com. But how can I do this? I initially started by trying to create my own JSESSIONID cookies, but this turned out to be complicated in that Tomcat continued to create JSESSIONID cookies on its own. So what I really needed was to somehow hook into Tomcat to change the way it creates JSESSIONID.</p>
<p>How to proceed? Well, my colleague found this: http://issues.liferay.com/browse/LEP-6976. One of the comments on this post indicates a possible fix:   <em>make use of the Liferay class PortalStandardService, an extension of the Tomcat class StandardService, configure this in server.xml, and everything will just start to work. In theory this should have worked.  </em> Basically, what this class does is that it overrides the method addConnector. Normally this method is called for each Tomcat connector configured in server.xml (or a default one, if none are specified). The Liferay version overrides this to wrap the Connector passed to it by Liferay with its own version: PortalConnector. PortalConnector, in turn, (a) copies a series of attributes from the Tomcat Connector it wraps and (b) overrides createRequest() method to create Liferay&#8217;s own version of the HttpServletRequest returned by Tomcat. The CreateRequest() override does something similar to what we are seeing so far: it wraps the HttpServletRequest created by Tomcat with a PortalRequest (again a Liferay class).</p>
<p>The Liferay class PortalRequest is where the magic happens. This class overrides a method configureSessionCookie, a method exposed by the Tomcat Request class (which in turn extends HttpServletRequest). This method receives the JSESSIONID Cookie from each request, and gives us an opportunity to set fields on it. In this case, PortalRequest takes the opportunity to modify the domain of the cookie to the common domain (&#8220;xyz.com&#8221;) by removing the first level of the domain. This, in theory, makes it possible to share the same JSESSIONID cookie across all hosts that are suffixed with &#8220;xyz.com&#8221;, so we can share the same session between the public and private sites.</p>
<p>So what&#8217;s wrong? It doesn&#8217;t work due to a bug in the Liferay class PortalConnector. In order to understand this bug, it is important to note that the Tomcat class Connector makes use of something called a ProtocolHandler, which it instantiates dynamically based on the protocol of the connector specified. Dynamic attributes set on the Connector are passed through, via property reflection, to the ProtocolHandler. The problem with the Liferay implementation of Connector, PortalConnector, was that it did not properly copy over these attributes, and hence the underlying ProtocolHandler created for PortalConnector did not contain the attributes/property values from the originally wrapped Tomcat Connector (and its internal ProtocolHandler). The key here is the the original Tomcat Connector creates its own ProtocolHandler(s) via reflection and our wrapping PortalConnector does the same (because it extends the Tomcat Connector). Hence, there needs to be a separate step to copy over the attributes from the original ProtocolHandler (of the original Tomcat Connector) to the Liferay wrapping connector, which is not being performed. Specifically, among the attributes that needed to be copied were the address and port, which because they were not assigned on the new ProtocolHandler, they are set to defaults, e.g. Port was set to 8080. However, the server already had something running on that port and received an &#8220;Address already in use&#8221; exception.</p>
<p><strong>The Real Solution:  The Correct Tomcat Connector For Creating Cross Domain Sessions </strong></p>
<p>The solution was to provide our own implementation of the Tomcat StandardService. All of the components of our implementation were the same, except we retrieved the ProtocolHandler from the wrapped Connector and assigned it to the appropriate member variable of our subclass of Connector. This worked, because <em>rather than using the newly instantiated (via reflection) ProtocolHandler that was created for us when we created PortalConnector, we instead reassign the protocolHandler member variable to use the Connector that Tomcat originally created from the server.xml configuration</em>. So rather than copying over the attributes from one ProtocolHandler to another, we simply change the ProtocolHandler that our new PortalConnector is using, so it will use the correct attributes. So the new ProtocolHandler now has the correct port, and now we can finally make use of the magic that correctly sets the JSESSIONID domain, and everything works.</p>
<p><strong>Related Scenarios</strong></p>
<p>These issues are not restricted to the scenario delineated above.  Other scenarios include</p>
<ul>
<li>Redirecting from http to https (non-secure to secure site)</li>
<li>Intercepting application server operations that run on every request (through custom connector mechanism described above)</li>
<li>Maintaining session across different sites in the same domain</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=12&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2009/10/12/fun-with-cookies-and-tomcat/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
		<item>
		<title>Interesting .NET AssemblyResolve Behavior</title>
		<link>http://bposerow.wordpress.com/2009/10/03/interesting-net-assemblyresolve-behavior/</link>
		<comments>http://bposerow.wordpress.com/2009/10/03/interesting-net-assemblyresolve-behavior/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 00:09:36 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[App Domains]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Class loading]]></category>

		<guid isPermaLink="false">http://bposerow.wordpress.com/?p=10</guid>
		<description><![CDATA[This happened a couple of months ago on a project I am working on outside of work, but I didn&#8217;t have this blog then, so I wanted to create a blog post on this now for you, my trusty reader. I had a class to load assemblies upon AssemblyResolve events in a new AppDomain: public [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=10&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This happened a couple of months ago on a project I am working on outside of work, but I didn&#8217;t have this blog then, so I wanted to create a blog post on this now for you, my trusty reader.</p>
<p>I had a class to load assemblies upon AssemblyResolve events in a new AppDomain:</p>
<pre class="brush: java;">
public abstract class AssemblyLoader : IAssemblyLoader
{
     public void Start(MyRequest request)
    {
               AppDomain.CurrentDomain.AssemblyResolve += (
                         (sender, resolveEventArgs)
                         =&gt; CurrentDomainAssemblyResolve(sender,
                                           resolveEventArgs,
                                           request));
     }

     public void Stop(MyRequest request)
     {
              AppDomain.CurrentDomain.AssemblyResolve -= ((sender, resolveEventArgs)
                         =&gt; CurrentDomainAssemblyResolve(sender,
                                           resolveEventArgs,
                                           request));
     }

     protected abstract Assembly CurrentDomainAssemblyResolve(object sender,
                   ResolveEventArgs args,
                   MyRequest request);
}
</pre>
<p>So this abstract class exposes an abstract method CurrentDomainAssemblyResolve which gets passed the standard AssemblyResolve arguments plus a custom request object.</p>
<p>I then created a child class to load the assemblies from disk:</p>
<pre class="brush: csharp;">
public class DirectoryAssemblyLoader : AssemblyLoader
{
       protected override Assembly CurrentDomainAssemblyResolve(
                 object sender,
                 ResolveEventArgs args,
                 MyRequest request)
      {
             var assemblyNames = GetAssemblyNames(request);
             foreach (var assemblyName in assemblyNames)
            {
                  if (args.Name.Split(',')[0] != assemblyName) continue;

                  var assemblyPath =  GetAssemblyPath(assemblyName);
                  var assemblyBytes = File.ReadAllBytes(assemblyPath);
                  Assembly assembly = Assembly.Load(assemblyBytes);

                  return assembly;
              }

             return null;
       }
}
</pre>
<p>A couple of things about this code:</p>
<p>- First of all, this code was an intermediate step.  I eventually wanted to get the bytes for my assembly not from the file system but rather from another form of persistent storage which would store the bytes of the assembly.  That is why I first read the file containing the assembly from the file system, getting an array of bytes and then call Assembly.Load with the bytes (as opposed to using Assembly.LoadFrom file system.)</p>
<p>- This code checks that the assembly requested corresponds to the MyRequest argument, which contains configuration/metadata about which assemblies can be used satisfy the request.  The loop in the middle of code deals with this.  Basically we don&#8217;t want to load arbitrary assemblies that do not correspond to the request.    We are trying to find which assembly in the request metadata corresponds to the assembly which is currently being requested (which we find in the ResolveEventArgs argument)</p>
<p>- I noticed that this code was broken if I tried to resolve the same or equivalent assembly name (as specified by ResolveEventArgs.Name) more than once.  <i>Even though these multiple resolution attempts are requesting the same assembly name, the assemblies returned are considered to be different assemblies.</i></p>
<p>- Because of this, if I try to load the same type more than once using this DirectoryAssemblyLoader class, it will consider the types loaded to be different types, leading to exceptions at runtime.</p>
<p>- The following post provides details about the different assembly load contexts:  http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx.  This example would be considered neither Load context or LoadFrom context.</p>
<p>BTW, if I had used the LoadFrom context instead (used Assembly.LoadFrom), this all would have worked, because if an assembly is loaded in this way more than once from the same place, the LoadFrom context recognizes these assemblies loaded from the same place as the same assembly.  In fact, the second resolution attempt will result in return of the already loaded assembly.  (although if an assembly with the same strong name was already loaded in the Load context via Assembly.Load, it may return that one instead, but that was not the case here)</p>
<p><i> The solution here was to cache the loaded assemblies by AssemblyName </i>, so that the next time we attempt to access the assembly, we first look in the cache to see if we have already found the assembly:</p>
<pre class="brush: csharp;">
     var aName = new AssemblyName(args.Name);

     // Here I am re-using the assemblies that I already loaded so
     // that the types resolve properly.

    foreach (var pair in assemblyCache)
        if (AssemblyName.ReferenceMatchesDefinition(aName, pair.Key))

    return pair.Value;

..........

      Assembly assembly = Assembly.Load(assemblyBytes);

      assemblyCache.Add(assembly.GetName(), assembly);

      return assembly;
</pre>
<p>So now the code first sees if an equivalently named assembly has been stored in our cache, and if so, it returns it.  If not, it loads the assembly as before but then adds it to the cache.</p>
<p>Hope this really interesting problem sheds light on how the different Assembly loading contexts work.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=10&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2009/10/03/interesting-net-assemblyresolve-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
		<item>
		<title>Load testing</title>
		<link>http://bposerow.wordpress.com/2009/09/30/load-testing/</link>
		<comments>http://bposerow.wordpress.com/2009/09/30/load-testing/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 23:38:35 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Load]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://bposerow.wordpress.com/2009/09/30/load-testing/</guid>
		<description><![CDATA[Overview We have just been involved with extensive load testing on our current project, a portlet based project running on Liferay Portal Server (which in turn runs on top of Tomcat), and making heavy use of Spring Portlet MVC for front end development.  The following considerations came into play: Understand The Top Command: Average Load [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=9&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Overview</h3>
<p>We have just been involved with extensive load testing on our current project, a portlet based project running on Liferay Portal Server (which in turn runs on top of<br />
Tomcat), and making heavy use of Spring Portlet MVC for front end development.  The following considerations came into play:</p>
<h5> Understand The Top Command:  Average Load versus CPU Percentage </h5>
<p>Our Tomcat/Liferay portlet web app runs on a LINUX server.  Therefore, for a high level understanding of how our load was being handled by the server, we made use of the &#8220;top&#8221; command.  We initially used the CPU percentage as an indicator of server load but soon discovered this is a high misleading measurement.  Take a look at <a href="http://www.linuxjournal.com/article/9001">http://www.linuxjournal.com/article/9001</a> to understand why CPU percentage is an inaccurate proxy for the load.  It really only captures snapshots of how frequently some task is running on the processor but it does not properly capture how many tasks are queuing up to run on the available processors, how much contention/context switching is occuring as a result of the contention for the processors, etc.  Also, note that the load can and likely will exceed the number of available processors. Finding the point at which the average load (for a time analogous to the period of the test) equals four or five times the number of available processors, in the case of largely I/O bound processes, or closer to the number of processors for processes that are largely CPU-bound,  can help indicate the  sweet spot at which the server is smoothly processing requests without major contention.</p>
<h5>  Use the Right Load Average </h5>
<p>top provides 1, 5, and 15 minute running averages.  For quick burst tests, these measures will not at all be accurate.  So it is important to run tests that run at least a couple of minutes (in order to get an accurate 1 minute running average) which ramp up load, peak load in the middle of the testing period, and then bring load back down. </p>
<h5> Perform Baseline Observations </h5>
<p>Make sure to perform baseline observations of the server (to see what basic load is on the server before the test as well as memory usage).  It probably pays to take several measurements of this to make sure that your app server is not currently running other tasks with unpredictable load or performance.  Then perform observations under load, and then additional observations after the test is completed to make sure that load drops back to baseline and that memory usage as well (i.e. there are no memory leaks).</p>
<h5> Make Sure Connection Pool Settings Are Correct </h5>
<p>Liferay, the Java portal server we are using, uses connection pooling internally by default for its metadata, but its defaults are set very high, and we ended up using an excessive  number of connections to the database, especially when we started to ramp up our load tests.  Try to guard against this by setting minimum and maximum pool sizes to reasonable amounts, both for any web apps with which you are testing as well as for application server metadata database connections.</p>
<h5> Set Idle Connection Settings Appropriately </h5>
<p>As an extension to the previous point, it is important that the settings include reclamation of idle connections.  We are using c3p0 for connection pooling, and it has a setting for an idle connection timeout.  Make use of these settings, but also make sure your Spring configuration specifies a destroy-method on the connection pool (&#8220;close&#8221; method in the case of the C3P0 pool class com.mchange.v2.c3p0.ComboPooledDataSource), so that the connections are reclaimed (at least down to min pool size) when the server shuts down.  Note this works even if server is shut down ungraceful, but obviously it will not work under abrupt machine shutdown.</p>
<h5> Identify Proper User Cases </h5>
<p>This can be a challenge in itself.  Ideally, these can be identified by existing performance data, perhaps by web server or HTTP server log analysis, looking at query execution times, etc.  However, this is not always possible because such data does not exist before you begin load testing.  Using a profiling tool might help here, because it is better to measure than guess.  However, often it is helpful to find some use cases that seem like good candidates for profiling, given that there might be a large number of cases out there, and it is not possible to profile every one, or perhaps differences in performance do not become apparent until you hit the application with load.  </p>
<p>We identified such cases by combining knowledge of </p>
<p>(i) the use cases most commonly exercised by users or some proxy for this (perhaps the links at the front of your site) </p>
<p>(ii) queries with particularly complex joins or contain conditions that are not well indexed, </p>
<p>(iii) calls to other external systems for which network communication latency would be the bottleneck.  </p>
<p>Existing load data was not available, or we would have made use of this instead.  We tried to first identify those use cases that were most likely to fit these critieria, and then we further categorized these so as to narrow down to a small set of use cases that will realistically exercise the most likely problem points of the application. </p>
<h5> Clearly Identify Your Load Testing Goals </h5>
<p>Are you trying to find the breaking load for the application, e.g. find out how far the app can scale?  Are you looking at response times under realistic load?  Are you trying to identify use cases with performance problems?  This identification can affect which use cases you choose and how you perform the tests.</p>
<h5> Determine Appropriate Load </h5>
<p>Exactly what load should you use in your load test?  If your users have some idea of what the load will be, maybe because there is a previous web site that you are now replacing, use this as a starting point, but you will likely have to double, triple, or use an even greater scale factor to determine the real load for testing, given that load can increase unexpectedly, especially if you succeed in improving the site significantly.  To be safe, you probably should try to find the breaking point for the test, and then try to determine if that maximum load could ever be realistically exceeded.</p>
<h5> Build Up Loads Gradually </h5>
<p>If you perform a test that submits requests at a set time interval, note load can increase quickly, due to fact that previous requests might not have been completed before new requests are submitted.  This is why it pays to start out with minimal loads (say 1 user) and then build up to higher, more realistic loads to get an idea how the app behaves under load.</p>
<h5> Account for Database Caching </h5>
<p>One trickiness with load testing:  if you perform multiple requests but each request retrieves the same read only data, caching may come into play, which can skew your results.  See if there are ways you can introduce variability into your load testing scripts.</p>
<h5> Perfrom Several Load Testing Phases </h5>
<p>1) Simple tests of our automated scripts and will generally be run against a highly controllable environment (maybe a desktop server) with a load perhaps of only 1 user. <br />
 <br />
2) We ran more extensive tests against our test server machine, first with individual test cases and then combining multiple test<br />
cases, gradually stepping up load, to see whether server performed adequately under load.  Specifically, we were trying to see whether server could handle expected loads (with a scaling factor for expected increase in load once server is in production) with reasonable response times, no timeouts, reasonable server load, no memory leaks, etc.<br />
Any issues we identified we investigated using a Java profiling tool. </p>
<p>3) Rinse and repeat for future iterations if changes need to be made to meet performance targets (in our case we met performance targets so this was not necessary). </p>
<p>4) Following this we will need to perform iterations to determine maximum possible server load (try to break the<br />
application by increasing load until requests start to fail or response times become unacceptable) in order to understand the limits of the app. </p>
<p>5) In addition, we will need to perform another few iterations to tweak settings to achieve maximum performance and scalaiblity, i.e. adjust Java heap size, play with connection pool settings, app server<br />
settings, etc. </p>
<p>6) It may be a requirement on your project, as it is on ours, to also run iterations in which real clients join the load so they can get a personal feel for how the app performs under load.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=9&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2009/09/30/load-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://bposerow.wordpress.com/2009/09/22/hello-world/</link>
		<comments>http://bposerow.wordpress.com/2009/09/22/hello-world/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 19:00:59 +0000</pubDate>
		<dc:creator>bposerow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Let me introduce myself.  My name is Ben Poserow, and I have been an employee of Sungard Consulting Services for 3 years but have been developing for 12 years.  I have worked for multiple types of clients, including a major publishing company, an investment bank, a hedge fund, the Federal Reserve Bank, and a prepaid [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=1&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:x-small;">Let me introduce myself.  My name is Ben Poserow, and I have been an employee of Sungard Consulting Services for 3 years but have been developing for 12 years.  I have worked for multiple types of clients, including a major publishing company, an investment bank, a hedge fund, the Federal Reserve Bank, and a prepaid card vendor.  I have spent the majority of my career as a Java/JEE/Spring developer, and I have a lot of experience around Spring, Hibernate, IBatis, Spring MVC, and Struts as well as detailed core Java and application server experience.  I also have been gaining significant experience as a .NET developer as well, and have in the last year or two become extremely interested in probing .NET in depth.  In addition, I recently have given a couple of presentation on .NET topics, specifically App Domains and Custom LINQ Providers, to the Westchester/Fairfield .NET User Group.    My next areas of .NET interest include Windows Azure and advanced WCF.  As you can see, I am very intellectually curious and try to continuously expand my knowledge about business areas (finance especially), design and architectural patterns, .NET and Java, and in the future, I would like to expand my limited knowledge and experience about scripting and functional languages.</p>
<p>My current project involves heavy use of the Liferay Portal Engine and Spring Portal MVC.   I am currently managing a team of 4 developers and 2 designers on this project as well as actively developing and architecting on the project.</p>
<p>Outside of technology, I am very interested in politics, especially foreign affairs, music, independent film, and most recently running.  I just ran a 15k and am now preparing for a half marathon.  I have been married for 6 years to an amazingly talented woman who sings opera and is also religious clergy as well.<br />
</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bposerow.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bposerow.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bposerow.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bposerow.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bposerow.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bposerow.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bposerow.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bposerow.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bposerow.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bposerow.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bposerow.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bposerow.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bposerow.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bposerow.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bposerow.wordpress.com&amp;blog=9605506&amp;post=1&amp;subd=bposerow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bposerow.wordpress.com/2009/09/22/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9d23fa7378b7da44c25d77b6c7c852f9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bposerow</media:title>
		</media:content>
	</item>
	</channel>
</rss>
