tag:blogger.com,1999:blog-88740943618447744942024-02-20T11:33:51.139+01:00Oliver Wahlen's BlogBlog on Java based Web Frameworks and Technologies.Anonymoushttp://www.blogger.com/profile/00609318660663162007noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-8874094361844774494.post-17251896873933741282016-06-02T23:26:00.002+02:002016-06-02T23:49:18.927+02:00Transfer of "The DAO" tokens from your local wallet to another Ethereum address using geth<h3>
Unable to transfer DAO tokens using the Mist Wallet</h3>
Fascinated by the idea of a distributed autonomous organization I bought some DAO tokens sending ethereum money to the address <i>0xbb9bc244d798123fde783fcc1c72d3bb8c189413</i> as explained by <a href="https://daohub.org/">The DAO</a>.<br />
Due to the nice GUI I decided to use the <a href="https://github.com/ethereum/mist/releases">Mist Wallet</a> to maintain my ethereum and to do money transfers.
The <a href="https://daohub.org/voting.html">voting page</a> of the project explains quite well how to configure mist to track your DAO. In combination with the getting started article on <a href="https://blog.ethereum.org/2015/12/03/how-to-build-your-own-cryptocurrency/">howto build your own crypto currency</a> it should be quite clear how to setup the mist wallet and how to transfer DAO tokens to another address. However for the time being it does not work on my mac with mist wallet 0.7.4. The wallet simply hangs when doing the transfer.<br/><br/>
<h3>
Falling back to the command line</h3>
As an alternative it is also possible to use the command line tool <i>geth</i> to do the transfer. If you have not yet installed it you can follow the instructions on the <a href="https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Mac">geth github page</a>.<br />
The following steps need to be performed in order to transfer <i>The DAO</i> tokens from your local wallet to another address (e.g. on the poloniex exchange):<br />
<ol>
<li>First create a file with the name <i>password</i> containing the password of your ethereum wallet followed by a newline:<br /><span style="font-family: "courier new" , "courier" , monospace;">$ vi password</span></li>
<li>Open a terminal and unlock your local wallet. Note that the hex address in the following example should be the address of your local wallet account.<br />
<span style="font-family: "courier new" , "courier" , monospace;">$ geth --password password --unlock 0x0df977bb21cf972a538187f72299614121549450</span></li>
<li>In another terminal attach to the running geth instance.<br />
<span style="font-family: "courier new" , "courier" , monospace;">$ geth attach</span></li>
<li>The command opens an interactive shell. In this shell define a variable that holds the ABI of <i>The DAO</i> by pasting the json content (do not add quotes) from the following address: <a href="https://gist.github.com/fivedogit/4f3e9a035262b99b2e7356a4fdf6dd34">https://gist.github.com/fivedogit/4f3e9a035262b99b2e7356a4fdf6dd34</a><br />
<span style="font-family: "courier new" , "courier" , monospace;">$ var abi = <pasted json content>;</span></li>
<li>Define the contract using the fixed <i>The DAO</i> address<br />
<span style="font-family: "courier new" , "courier" , monospace;">$ var contract = web3.eth.contract(abi).at("0xbb9bc244d798123fde783fcc1c72d3bb8c189413");</span></li>
<li>Check the balance of your DAO tokens in your local wallet. If you have tokens associated with your ethereum address the value should be greater than 0. Note that the result must be devided by 10^16.<br />
<span style="font-family: "courier new" , "courier" , monospace;">$ contract.balanceOf("0x0df977bb21cf972a538187f72299614121549450")/10000000000000000;</span></li>
<li>Now in order to transfer the tokens to a poloniex deposit address first login and identify the address in the <a href="https://poloniex.com/balances">balances</a> section of your poloniex account by clicking <i>Deposit</i> in the line for DAO. For this example we assume a deposit address of 0x8686c1799fad1a10f044b64533ecf0200416fc50.</li>
<li>Execute the following command to transfer 14000 tokens to this address.<br />
<span style="font-family: "courier new" , "courier" , monospace;">$ contract.transfer("0x8686c1799fad1a10f044b64533ecf0200416fc50", 140000000000000000000, {from:"0x0df977bb21cf972a538187f72299614121549450"});</span></li>
<li>Recheck that your balance is now 0<br />
<span style="font-family: "courier new" , "courier" , monospace;">$ contract.balanceOf("0x0df977bb21cf972a538187f72299614121549450")/10000000000000000;</span></li>
<li>It can take about an hour until the tokens are transferred.</li>
<li>After you are done do not forget to remove the password file<br />
<span style="font-family: "courier new" , "courier" , monospace;">$ rm password</span></li>
</ol>
Anonymoushttp://www.blogger.com/profile/00609318660663162007noreply@blogger.com2tag:blogger.com,1999:blog-8874094361844774494.post-41976827375726830342013-11-14T10:48:00.000+01:002013-11-14T10:51:22.032+01:00Testing private methods in Java and GroovyIf you search the internet for the topic <i>testing private methods</i> you will find several opinions if this is good practice or not. Some people even consider it a misconception. I personally think that it is a good idea to encapsulate logic that makes only sense in the context of a single class into a private method. If the code inside of this method exceeds a certain complexity it should be tested.<br />
<div>
<br /></div>
<div>
In this post however I do not want to start a discussion on this topic but show how it can be done easily in Groovy and Java. Let's have a look at the following code:</div>
<code><br /></code>
<code>
public class SomeClass {</code><br />
<code> private String methodToBeTested(String input) {</code><br />
<code> return input.toLowerCase(); </code><br />
<code> } </code><br />
<code>}</code><br />
<code><br /></code><code>// inside of a test</code><br />
<code>SomeClass someClass = new SomeClass();</code><br />
<code>assert "foo" == someClass.methodToBeTested("Foo");
</code>
<br />
<br />
<div>
If you run this code in Groovy you may expect an Error because the test calls a private method of SomeClass. However as of Groovy 2.1.9 this is currently not the case due to a bug in Groovy:<br />
<a href="http://jira.codehaus.org/browse/GROOVY-1875">http://jira.codehaus.org/browse/GROOVY-1875</a><br />
Consequently testing private methods in Groovy <u>currently</u> is really simple: just do it.<br />
<br />
However if <code>SomeClass</code> is a class produced by the Java compiler the test will fail with an error like this:<br />
<br />
<code>methodToBeTested(java.lang.String) has private access in SomeClass</code>
<code> someClass.methodToBeTested("Foo");</code><br />
<code><br /></code>
The Groovy developers are working on a fix for the issue that will hopefully make it into Groovy 3.
So the Groovy code shown above is eventually not compatible with future Groovy releases.<br />
<br />
To overcome this testing problem one can use the Java Reflection API.<br />
The test code must be changed like this:<br />
<br />
<code>// inside of a test</code>
<code>SomeClass</code><br />
<code>someClass = new SomeClass();</code><br />
<code><code>
<code>Method method = SomeClass.class.getDeclaredMethod("methodToBeTested", String.class)</code></code></code><br />
<code><code>
<code>method.setAccessible(true);</code></code></code><br />
<code><code>
<code>assert "foo" == method.invoke(someClass, "Foo");</code><br />
</code></code><br />
Note that the reflection methods can throw several Exceptions. If the test is written in Java these must be caught or the test method must be adjusted accordingly.</div>
Anonymoushttp://www.blogger.com/profile/00609318660663162007noreply@blogger.com0tag:blogger.com,1999:blog-8874094361844774494.post-31880684647400909212010-12-01T01:55:00.003+01:002010-12-01T02:11:13.346+01:00Tracking Session Lifecycle in GrailsWeb applications often need to keep track when new user sessions are created and when user sessions are terminated - either by timing out or by active invalidation.<br />
The Java Servlet Specification defines Event Listeners for this purpose. Have a look at <a href="http://onjava.com/pub/a/onjava/2001/04/12/listeners.html">this</a> article for an introduction on how to utilize these servlet technologies.<br />
<br />
I want to explain here how to integrate HttpSessionListeners smoothly with <a href="http://www.grails.org/">Grails</a>.<br />
<br />
We start by registering a listener in the deployment descriptor - the file <span style="font-family: "Courier New",Courier,monospace;">web.xml</span>.<br />
By default this file is not visible when developing with Grails since it is generated dynamically. You can get a static version by executing the following <a href="http://www.grails.org/Artifact+and+Scaffolding+Templates">command</a> in the application's root directory:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">grails install-templates</span><br />
<br />
Instead of executing this command and creating a static <span style="font-family: "Courier New",Courier,monospace;">web.xml</span> file we are going to choose the more modular approach of extending the <span style="font-family: "Courier New",Courier,monospace;">web.xml</span> generation process.<br />
<br />
The first thing to do is to create the file <span style="font-family: "Courier New",Courier,monospace;">_Events.groovy</span> in the <span style="font-family: "Courier New",Courier,monospace;">scripts</span> directory of the application root. Note that this directory was created when executing <span style="font-family: "Courier New",Courier,monospace;">grails create-app</span>. The contents of <span style="font-family: "Courier New",Courier,monospace;">_Events.groovy</span> should look something like this:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">import groovy.xml.StreamingMarkupBuilder</span><br />
<span style="font-family: "Courier New",Courier,monospace;">eventWebXmlEnd = {String tmpfile -></span><span style="font-family: "Courier New",Courier,monospace;"> </span><br />
<span style="font-family: "Courier New",Courier,monospace;"> def root = new XmlSlurper().parse(webXmlFile)</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> </span><span style="font-family: "Courier New",Courier,monospace;">root.appendNode {</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> 'listener' { </span><br />
<span style="font-family: "Courier New",Courier,monospace;"> 'listener-class' (</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> 'org.test.MyHttpSessionListener'</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> ) </span><br />
<span style="font-family: "Courier New",Courier,monospace;"> }</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> }</span><span style="font-family: "Courier New",Courier,monospace;"> </span><br />
<span style="font-family: "Courier New",Courier,monospace;"> webXmlFile.text = new StreamingMarkupBuilder().bind {</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> mkp.declareNamespace(</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> "": "http://java.sun.com/xml/ns/j2ee")</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> mkp.yield(root)</span><span style="font-family: "Courier New",Courier,monospace;"> </span><br />
<span style="font-family: "Courier New",Courier,monospace;"> }</span><br />
<span style="font-family: "Courier New",Courier,monospace;">}</span><br />
<br />
This defines a closure that is called by the Grails build mechanism right after creation of <span style="font-family: "Courier New",Courier,monospace;">web.xml</span>. In the closure Groovy's <a href="http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper">XmlSlurper</a> is used to parse the <span style="font-family: "Courier New",Courier,monospace;">web.xml</span> into a tree data structure. Xml elements for <span style="font-family: "Courier New",Courier,monospace;">listener</span> and <span style="font-family: "Courier New",Courier,monospace;">listener-class</span> are added and the tree data structure is written back using Groovy's <a href="http://groovy.codehaus.org/Creating+XML+using+Groovy%27s+StreamingMarkupBuilder">StreamingMarkupBuilder</a>.<br />
<br />
The class <span style="font-family: "Courier New",Courier,monospace;">org.test.MyHttpSessionListener</span> is registered as event listener.<br />
The file <span style="font-family: "Courier New",Courier,monospace;">src/groovy/org/test/MyHttpSessionListener.groovy</span> contains this class. Obviously the <span style="font-family: "Courier New",Courier,monospace;">org/test</span> part of the path depends on the package name you want to choose. The file's contents should look like this:<br />
<br />
<div style="font-family: "Courier New",Courier,monospace;">package org.test<br />
<br />
import org.codehaus.groovy.grails.commons.ApplicationHolder<br />
import javax.servlet.http.HttpSession<br />
import javax.servlet.http.HttpSessionEvent<br />
import javax.servlet.http.HttpSessionListener</div><div style="font-family: "Courier New",Courier,monospace;">class MyHttpSessionListener implements HttpSessionListener {<br />
HttpSessionService httpSessionService<br />
</div><div style="font-family: "Courier New",Courier,monospace;"> // called by servlet container upon session creation </div><div style="font-family: "Courier New",Courier,monospace;"> void sessionCreated(HttpSessionEvent event) {<br />
HttpSession session = event.getSession()<br />
getHttpSessionService().sessionCreated(session)<br />
}</div><div style="font-family: "Courier New",Courier,monospace;"><br />
</div><div style="font-family: "Courier New",Courier,monospace;"> // called by servlet container upon session destruction </div><div style="font-family: "Courier New",Courier,monospace;"> void sessionDestroyed(HttpSessionEvent event) {<br />
HttpSession session = event.getSession()</div><div style="font-family: "Courier New",Courier,monospace;"> getHttpSessionService().sessionDestroyed(session)<br />
}</div><div style="font-family: "Courier New",Courier,monospace;"><br />
</div><div style="font-family: "Courier New",Courier,monospace;"> private synchronized HttpSessionService</div><div style="font-family: "Courier New",Courier,monospace;"> getHttpSessionService() {</div><div style="font-family: "Courier New",Courier,monospace;"> if (httpSessionService == null) {<br />
httpSessionService =</div><div style="font-family: "Courier New",Courier,monospace;"> (HttpSessionService) ApplicationHolder</div><div style="font-family: "Courier New",Courier,monospace;"> .getApplication().getMainContext()</div><div style="font-family: "Courier New",Courier,monospace;"> .getBean("httpSessionService")<br />
}<br />
return httpSessionService<br />
}<br />
}</div><br />
The class implements the <span style="font-family: "Courier New",Courier,monospace;">sessionCreated</span> and <span style="font-family: "Courier New",Courier,monospace;">sessionDestroyed</span> methods as defined in the <span style="font-family: "Courier New",Courier,monospace;">HttpSessionListener</span> interface. These methods are called by the servlet container upon session creation and destruction. However to integrate our listener with the rest of the Grails artefacts it would be nicer if conventional Grails Service methods were called on these events. <br />
<br />
To achieve the desired Service calls we cannot utilize Spring injections in our SessionListener. We therefore manually fetch a bean called <span style="font-family: "Courier New",Courier,monospace;">httpSessionService</span> from the application's MainContext. This bean is a conventional Grails Service that we have to implement in a file called <span style="font-family: "Courier New",Courier,monospace;">grails-app/services/org/test/HttpSessionService.groovy</span>. The file could have the following contents:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">package org.test</span><br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">import javax.servlet.http.HttpSession</span><br />
<span style="font-family: "Courier New",Courier,monospace;"></span><br />
<span style="font-family: "Courier New",Courier,monospace;">class HttpSessionService {</span><br />
<br />
<span style="font-family: "Courier New",Courier,monospace;"> // method called upon session creation</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> def sessionCreated(HttpSession session) {</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> log.info("Session created: "+session.id)</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> }</span><br />
<br />
<span style="font-family: "Courier New",Courier,monospace;"> //method called upon session destruction</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> </span><span style="font-family: "Courier New",Courier,monospace;"> def sessionDestroyed(HttpSession session) {</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> log.info("Session destroyed: "+session.id)</span><br />
<span style="font-family: "Courier New",Courier,monospace;"></span><span style="font-family: "Courier New",Courier,monospace;"> }</span><br />
<span style="font-family: "Courier New",Courier,monospace;">}</span><br />
<br />
Other services can be injected into the HttpSessionService and relevant business logic can be called from the <span style="font-family: "Courier New",Courier,monospace;">sessionCreated</span> and <span style="font-family: "Courier New",Courier,monospace;">sessionDestroyed</span> Service methods.Anonymoushttp://www.blogger.com/profile/00609318660663162007noreply@blogger.com5tag:blogger.com,1999:blog-8874094361844774494.post-1642931633597034682008-08-11T18:58:00.006+02:002008-08-11T19:28:47.612+02:00Oliver Wahlen's Blog OnlineSo this is my first post on Blogger.<br />As an IT consultant I have done several projects using <a href="http://www.netbeans.org/">Netbeans</a> and its Visual Web JSF framework based on the <a href="https://woodstock.dev.java.net/">Woodstock</a> components.<br />I think that JSF is in general one of the best approaches to create robust web applications.<br /><br />However the whole web technology sector is evolving with an enormous speed and to stay "in business" developers have to get comfortable with new technologies at an enormous rate.<br /><br />I want to use this blog for two purposes:<br /><ol><li>to present new technology approaches, and to help developers getting comfortable with these technologies.</li><li>to share my personal technology evaluation results with others and to challenge other opinions. I think such discussions will help people to get a better level of judgement concerning state of the arte technologies. Therefore I encourage everybody to comment my posts!<br /></li></ol>And now: Happy blogging!Anonymoushttp://www.blogger.com/profile/00609318660663162007noreply@blogger.com0tag:blogger.com,1999:blog-8874094361844774494.post-73504978286362614402008-08-11T18:22:00.019+02:002008-08-11T20:08:04.425+02:00Netbeans Visual Web Tables with thousands of recordsMaybe you have noticed that the table component in Netbeans visual<br />web tends to become quite slow if it has to deal with a huge amount of<br />records (e.g. coming from a database).<br />This is true even if pagination is used.<br /><br />The reason is that the typical dataproviders (e.g. ObjectArrayDataProvider) on the server side require a complete fetch of all data from the database.<br />Based on this data they analyze the amount of records which in turn is used to operate the augmented table header and the pagination controls.<br />Thus the application does not only become slow (because of the sql operations) but also consumes a large amount of memory.<br /><br />To overcome this problem I have created a new dataprovider called ObjectControllerDataprovider.<br />Like all the other dataprovider this class can directly be used in the table layout of the visual table component.<br />This ObjectControllerDataprovider is in turn connected an ObjectController.<br />In more detail the ObjectControllerDataprovider is connected to a class that is to be written manually which extends the abstract ObjectController class.<br />The manual written class finally contains the functionality to efficiently retrieve small pages from a database utilizing a caching window concept.<br /><br />You can download a working Netbeans 6.1 example project using the new dataprovider from the following link: <a href="http://sites.google.com/site/owahlen/Home/ObjectControllerTest.tar.gz">http://sites.google.com/site/owahlen/Home/ObjectControllerTest.tar.gz</a>Anonymoushttp://www.blogger.com/profile/00609318660663162007noreply@blogger.com1