<?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/"
	>

<channel>
	<title>Code Snippets</title>
	<atom:link href="http://www.swadge.co.uk/code/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.swadge.co.uk/code</link>
	<description>Useful code for those awkward moments</description>
	<lastBuildDate>Thu, 19 Apr 2012 10:42:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using SubClipse To Access An External SVN Behind a Proxy Server</title>
		<link>http://www.swadge.co.uk/code/?p=91</link>
		<comments>http://www.swadge.co.uk/code/?p=91#comments</comments>
		<pubDate>Wed, 25 Jan 2012 09:12:55 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[SVN]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[misc]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[sliksvn]]></category>
		<category><![CDATA[subclipse]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.swadge.co.uk/code/?p=91</guid>
		<description><![CDATA[Proxy servers can be frustrating things! I was trying to set up access to a SVN server through Eclipse, added my proxy settings to Eclipse, installed SlikSVN &#38; SubClipse. I didn&#8217;t think it was too much to ask to expect it to pick up the proxy settings &#38; work &#8216;out of the box&#8217;. Seems I [...]]]></description>
			<content:encoded><![CDATA[<p>Proxy servers can be frustrating things! I was trying to set up access to a SVN server through Eclipse, added my proxy settings to Eclipse, installed SlikSVN &amp; SubClipse. I didn&#8217;t think it was too much to ask to expect it to pick up the proxy settings &amp; work &#8216;out of the box&#8217;. Seems I was wrong! Instead I got a message &#8220;<strong>RA layer request failed</strong>&#8220;. A big thanks to Mark&#8217;s <a title="The MKVille Blog" href="http://www.mkville.com/blog/index.cfm/2007/11/8/Using-Subclipse-Behind-a-Proxy-Server" target="_blank">blog</a> for the answer of how to overcome this most inconvenient of hurdles.</p>
<p>In a Windows development environment, open the file: <strong>C:\Documents and Settings\MyUserId\Application Data\Subversion\servers</strong> in your favourite text editor. Near the bottom of that file is a <strong>[global]</strong> section with <strong>http-proxy-host</strong> and <strong>http-proxy-port</strong> settings. Uncommented those two lines, modify them for your proxy server. Go back to the SVN Repository view in Eclipse, refresh the external SVN repository and it <strong>*should*</strong> work.</p>
<p>The tools I was using was <a title="Eclipse Downloads" href="http://www.eclipse.org/downloads/" target="_blank">Eclipse</a> (Indigo), <a title="SlikSVN Download Site" href="http://www.sliksvn.com/en/download/" target="_blank">SlikSVN</a> (1.7.2 64-bit), and <a title="SubClipse" href="http://subclipse.tigris.org/" target="_blank">SubClipse</a> (1.8 via the <a title="Eclipse Update Site" href="http://subclipse.tigris.org/update_1.8.x" target="_blank">Eclipse Update Site</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=91</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Inheritance with Generics</title>
		<link>http://www.swadge.co.uk/code/?p=42</link>
		<comments>http://www.swadge.co.uk/code/?p=42#comments</comments>
		<pubDate>Tue, 11 Oct 2011 08:12:15 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[hierarchy]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://localhost/code-snippets/?p=42</guid>
		<description><![CDATA[Sometimes you want to set a type as a more specific type on a subclass than is defined by the super class. The below example shows how to do this. This enables on the MostSpecific class for getModel() and model to be of type MySpecificModel within the MostSpecific class, yet it is of type MyModel [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you want to set a type as a more specific type on a subclass than is defined by the super class. The below example shows how to do this.</p>
<pre>
<pre class="brush: java; title: ; notranslate">
public class MostSpecific extends MediumSpecific&lt;MySpecificModel&gt; { }

public class MediumSpecific&lt;T extends MyModel&gt; extends LeastSpecific&lt;T&gt; { }

public class LeastSpecific&lt;T extends MyModel&gt; implements ILeastSpecificInterface {
	protected T model;
	public T getModel() {
		return model;
	}
}
</pre>
</pre>
<p>This enables on the MostSpecific class for getModel() and model to be of type MySpecificModel within the MostSpecific class, yet it is of type MyModel in the classes MediumSpecific and LeastSpecific.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=42</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Escaping Regular Expression Pattern in Java</title>
		<link>http://www.swadge.co.uk/code/?p=38</link>
		<comments>http://www.swadge.co.uk/code/?p=38#comments</comments>
		<pubDate>Wed, 08 Jun 2011 15:46:07 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[escape]]></category>
		<category><![CDATA[escaping]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regexp]]></category>

		<guid isPermaLink="false">http://localhost/code-snippets/?p=38</guid>
		<description><![CDATA[Sometimes you need to dynamically create regular expressions, which often means you have to escape them. The easiest and best way to do this is provided for you in the Java API. Your pattern will become .*[Q!"£$_%^&#38;*()-=[];&#8217;#,./`¬¦&#124;&#60;&#62;?:@~{}+E].*, which is escaped. java.util.regex.Pattern.html#quote(java.lang.String)]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to dynamically create regular expressions, which often means you have to escape them. The easiest and best way to do this is provided for you in the Java API.</p>
<pre>
<pre class="brush: java; title: ; notranslate">
String regexPattern = &quot;.*[&quot; + Pattern.quote( &quot;!\&quot;£$_%^&amp;amp;*()-=[];'#,./\`¬¦|&amp;lt;&amp;gt;?:@~{}+&quot; ) + &quot;].*&quot;;
</pre>
</pre>
<p>Your pattern will become <strong>.*[Q!"£$_%^&amp;*()-=[];&#8217;#,./`¬¦|&lt;&gt;?:@~{}+E].*</strong>, which is escaped.</p>
<p><a title="Java 1.5 API for java.util.regex.Pattern" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html#quote(java.lang.String)" target="_blank">java.util.regex.Pattern.html#quote(java.lang.String)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=38</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Struts 2 Collection and Map Handling &#8211; Pitfalls</title>
		<link>http://www.swadge.co.uk/code/?p=26</link>
		<comments>http://www.swadge.co.uk/code/?p=26#comments</comments>
		<pubDate>Tue, 07 Jun 2011 09:49:32 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Struts 2]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[struts2]]></category>
		<category><![CDATA[type conversion]]></category>

		<guid isPermaLink="false">http://localhost/code-snippets/?p=26</guid>
		<description><![CDATA[With Java Generics you must be careful! The Struts 2 documentation states that &#8220;The framework cannot instantiate an object if it can&#8217;t determine an appropriate implementation. It recognizes well-known collection interfaces (List, Set, Map, etc) but cannot instantiate MyCustomInterface when all it sees is the interface. In this case, instantiate the target implementation first (eg. [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">With Java Generics you must be careful!</p>
</div>
<div>The Struts 2 documentation states that &#8220;The framework cannot instantiate an object if it can&#8217;t determine an appropriate implementation. It recognizes well-known collection interfaces (List, Set, Map, etc) but cannot instantiate MyCustomInterface when all it sees is the interface. In this case, instantiate the target implementation first (eg. in a prepare method) or substitute in an implementation.&#8221;, but you have to be aware that the section &#8220;It recognizes well-known collection interfaces (List, Set, Map, etc) but cannot instantiate MyCustomInterface when all it sees is the interface&#8221; is quite loose. It would appear that on your action you must ensure not to use Java Generics. i.e. use java.util.List instead of java.util.List&lt;com.mycompany.storename.basket.ShoppingItem&gt; as Struts 2 cannot handle the typing on the List.</p>
</div>
<div>
<div>Official documentation: <a title="Struts 2 - Type Conversion - Collection and Map Support" href="http://struts.apache.org/2.x/docs/type-conversion.html#TypeConversion-CollectionandMapSupport" target="_blank">http://struts.apache.org/2.x/docs/type-conversion.html#TypeConversion-CollectionandMapSupport</a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=26</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to fix orphaned SQL Server users</title>
		<link>http://www.swadge.co.uk/code/?p=19</link>
		<comments>http://www.swadge.co.uk/code/?p=19#comments</comments>
		<pubDate>Wed, 25 May 2011 11:46:04 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[users]]></category>

		<guid isPermaLink="false">http://localhost/code-snippets/?p=19</guid>
		<description><![CDATA[Summary When you restore a Microsoft SQL Server database on a different machine, you cannot access the database until you fix the permissions. Detail The problem is that the user in the database is an &#8220;orphan&#8221;. This means that there is no login id or password associated with the user. This is true even if [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary</strong><br />
When you restore a Microsoft SQL Server database on a different machine, you cannot access the database until you fix the permissions.</p>
<p><strong>Detail</strong><br />
The problem is that the user in the database is an &#8220;orphan&#8221;. This means that there is no login id or password associated with the user. This is true even if there is a login id that matches the user, since there is a GUID (called a SID in Microsoft-speak) that has to match as well.</p>
<p>This used to be a pain to fix, but currently (SQL Server 2000, SP3) there is a stored procedure that does the heavy lifting.</p>
<p>All of these instructions should be done as a database admin, with the restored database selected.</p>
<p>First, make sure that this is the problem. This will lists the orphaned users:</p>
<pre>
<pre class="brush: sql; title: ; notranslate">EXEC sp_change_users_login 'Report'</pre>
</pre>
<p>If you already have a login id and password for this user, fix it by doing:</p>
<pre>
<pre class="brush: sql; title: ; notranslate">EXEC sp_change_users_login 'Auto_Fix', 'user'</pre>
</pre>
<p>If you want to create a new login id and password for this user, fix it by doing:</p>
<pre>
<pre class="brush: sql; title: ; notranslate">EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=19</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>InputStream to String</title>
		<link>http://www.swadge.co.uk/code/?p=14</link>
		<comments>http://www.swadge.co.uk/code/?p=14#comments</comments>
		<pubDate>Tue, 24 May 2011 08:38:17 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://localhost/code-snippets/?p=14</guid>
		<description><![CDATA[In Java 1.4 simply replace the StringBuilder with a StringBuffer.]]></description>
			<content:encoded><![CDATA[<p>In Java 1.4 simply replace the StringBuilder with a StringBuffer.</p>
<pre>
<pre class="brush: java; title: ; notranslate">
private String convertStreamToString( InputStream in ) throws Exception {
	StringBuilder out = new StringBuilder();
	byte[] b = new byte[4096];
	for ( int n; ( n = in.read( b ) ) != -1; ) {
		out.append( new String( b, 0, n ) );
	}
	return out.toString();
}
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=14</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EL Expression from ${xyz.abc} to %{xyz.abc}</title>
		<link>http://www.swadge.co.uk/code/?p=10</link>
		<comments>http://www.swadge.co.uk/code/?p=10#comments</comments>
		<pubDate>Mon, 23 May 2011 10:10:28 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[JSP]]></category>
		<category><![CDATA[OGNL]]></category>
		<category><![CDATA[Struts 2]]></category>
		<category><![CDATA[EL]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://localhost/code-snippets/?p=10</guid>
		<description><![CDATA[${xyz.abc} can be accessed using &#60;c:out value=&#8221;${xyz.abc}&#8221;/&#62; but cannot be accessed by &#60;s:property value=&#8221;${xyz.abc}/&#62;. Instead you must place the expression onto the request using &#60;c:set&#62; and then access it from the request using &#60;s:property&#62;.]]></description>
			<content:encoded><![CDATA[<div>${xyz.abc} can be accessed using &lt;c:out value=&#8221;${xyz.abc}&#8221;/&gt; but cannot be accessed by &lt;s:property value=&#8221;${xyz.abc}/&gt;. Instead you must place the expression onto the request using &lt;c:set&gt; and then access it from the request using &lt;s:property&gt;.</div>
<pre>
<pre class="brush: java; title: ; notranslate">
&lt;c:set var=&quot;painless&quot; value=&quot;${xyz.abc}&quot; scope=&quot;request&quot;/&gt;
&lt;s:property escapeJavaScript=&quot;true&quot; value=&quot;%{ #request['painless'] }&quot;/&gt;
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=10</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Generics Cast</title>
		<link>http://www.swadge.co.uk/code/?p=6</link>
		<comments>http://www.swadge.co.uk/code/?p=6#comments</comments>
		<pubDate>Fri, 15 Apr 2011 11:20:13 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[casting]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://localhost/code-snippets/?p=4</guid>
		<description><![CDATA[When your cast simply won&#8217;t compile, you need to get a bit sneaky. Suppose you have a Map with the generic type of Map&#60;String, List&#60;String&#62;&#62; and you want to cast it to a Map&#60;String, Object&#62; to satisfy a return type of a method. Now if you are like me, you would have thought that List&#60;String&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>When your cast simply won&#8217;t compile, you need to get a bit sneaky. Suppose you have a Map with the generic type of <em>Map&lt;String, List&lt;String&gt;&gt;</em> and you want to cast it to a <em>Map&lt;String, Object&gt;</em> to satisfy a return type of a method.</p>
<pre>
<pre class="brush: java; title: ; notranslate">
public interface MyInterface {
	Map&lt;String, Object&gt; getAttributes();
}

public class MyClass implements MyInterface {
	private Map&lt;String, List&lt;String&gt;&gt; attributes = new HashMap&lt;String, List&lt;String&gt;&gt;();

	public Map&lt;String, Object&gt; getAttributes() {
		return attributes; // Compile error here!
	}
}
</pre>
</pre>
<p>Now if you are like me, you would have thought that List&lt;String&gt; is an object, but it doesn&#8217;t work like that. Instead you must use a cast with a bit more flexibility. The following cast will remove the compile error and should run like a charm.</p>
<pre>
<pre class="brush: java; title: ; notranslate">
@Override
public Map&lt;String, Object&gt; getAttributes() {
	return cast(attributes); // Compile error gone!
}

@SuppressWarnings( &quot;unchecked&quot; )
private &lt;T&gt; T cast( Object o ) {
	return (T) o;
}
</pre>
</pre>
<p><b>Update:</b> Perhaps a more correct way of doing this is already built into Java &#8211; an example is below adapted from <a href="http://stackoverflow.com/questions/1555326/java-class-cast-vs-cast-operator" target="_blank">stackoverflow.com</a>.</p>
<pre>
<pre class="brush: java; title: ; notranslate">
public Map&lt;String, Object&gt; getAttributes() {
	return attributes.getClass().cast( attributes );
}
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.swadge.co.uk/code/?feed=rss2&#038;p=6</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

