Vert.x vs node.js

    I kind of gave up on posting benchmarks on the web but I just found a post on the Vert.x blog with a benchmark that blew node.js out of the water so I just  did a quick run of those benchmarks and bottom line is I got ~ 60K/s using 4 vert.x instances and ~ 8K/s using 4 node.js processes.

    I also ran vert.x directly using java (got the command line by looking at ps aux | grep java) in order to pass some JVM flags. Since I'm using a 64 bit OS I believe -server is on by default so I just experimented with -Xmx128m to reduce memory usage.

    Here is my run of the benchmark:

    Processor: Intel i7 Q820 - 4 cores + HT

    OS: Ubuntu 11.04 x64

    Java  1.7.0_01-b08

    node v0.6.17

    vert.x 1.0.final

     

    Benchmark: ab -k -n 100000 -c 100 http://localhost:8080/

    Mem analysis:

    Using pmap: with pmap $PID | tail -1 >> mem.txt; SLEEP 0.001

    Using system monitor: non scientific analysis by looking at the processes :P

     

    vert.x (1 instance)

    Mem (pmap): 2. 4G

    Mem (System Monitor): 120M

    Requests per second:   ~ 30K/s

    (Added JVM flags -Xmx128m)

    Mem (pmap): 446M

    Mem (System Monitor): 75M

    Requests per second:   ~ 30K/s

     

    vert.x (4 instances)

    Mem (pmap): 2. 4G

    Mem (System Monitor): 120M

    Requests per second:   ~ 60K/s

    (Added JVM flags -Xmx128m)

    Mem (pmap): 513M

    Mem (System Monitor): 75M

    Requests per second:   ~ 60K/s

     

    vert.x (2 instances)

    Requests per second:   ~ 50K/s

     

    Node Server (1 process)

    Mem (pmap):  ~ 640M

    Mem (System Monitor): ~ 65M

    Requests per second: ~ 2.6K/s

     

    Node Cluster (4 processes)

    Mem (pmap): ~ 640M per process ( +1 for the main process) => 3.2G

    Mem (System Monitor): 4.5M + 4 * 65M => 264.5M

    Requests per second: ~ 8K/s

     

    Disclaimer

    This is mainly for personal reference so please refrain from starting a flame war. 

    I am using node.js for some current projects and really like it so this is not, by any means some node.js trashing post... just use what you like!

    If you feel some of these benchmarks are unfair please perform your own and link to them in the comments... I'll gladly take a look.

    Securing JS - "window.location" can we trust in it ?

    We need a way to sell JS widgets and license these on a domain basis.

    The idea is to provide a key which locks the widget to a given domain.

    Basically we will be using window.location.host (and/or others similar like "hostname").

    So, for instance  if we do

    window.location.host == "mydomain.com"

    we can lock the widget to this domain. Or can we ?

    window.location.host = "localhost"

    doesn't work cause the setter will cause the browser to navigate to "localhost". But what if we did (using Google Chrome):

    window.location.__defineGetter__("host", function(){return "localhost";})

    now window.location.host returns "localhost"!

    The solution would be to use __lookupGetter__

    window.location.__lookupGetter__("host")

    If you call this before defining you own getter you get "undefined" but after defining the getter it returns a function. This way you can see if they tampered with the getter.

    But what if they did

    window.location.__lookupGetter__ = function(name){ return undefined;}

    Now our previous method doesn't work!

    We should do a 

    /\[native code\]/.test(window.location.__lookupGetter__.toString())

     to make sure they haven't tampered with the __lookupGetter__ function.

    Apparently Firefox doesn't provide __defineGetter__ and __lookupGetter__ for native objects like window.location so you can trust it....

    We will be experimenting with this further but it just shows that JS is not to be trusted when it comes to securing your apps or widgets 

    Pentaho on JBoss Portal SSO

    This is mostly a reference for myself but I though it might be useful for someone...

    The idea is to use JBoss Portal's authentication and authorization to control access to Pentaho. We have a legacy project and I won't be migrating to GateIn soon, although I have Pentaho working in GateIn (if you're interested get in touch with me).

    I'm using Pentaho 3.7 which uses Spring Security so it is really flexible and makes it easier to integrate with custom authentication/authorization solutions.

    First, thanks to this post, we'll move the portal's application policy to the server's root so it applies to all our webapps:

    server/default/conf/login-config.xml :

     <application-policy name='portal'>
            <authentication>
               <login-module code='org.jboss.portal.identity.auth.IdentityLoginModule' flag='required'>
                        <module-option name='unauthenticatedIdentity'>guest</module-option>
                        <module-option name='userModuleJNDIName'>java:/portal/UserModule</module-option>
                        <module-option name='roleModuleJNDIName'>java:/portal/RoleModule</module-option>
                        <module-option name='userProfileModuleJNDIName'>java:/portal/UserProfileModule</module-option>
                        <module-option name='membershipModuleJNDIName'>java:/portal/MembershipModule</module-option>
                        <module-option name='additionalRole'>Authenticated</module-option>
                        <module-option name='password-stacking'>useFirstPass</module-option>
                     </login-module>
    
                           <login-module code = 'org.jboss.portal.identity.auth.DBIdentityLoginModule' flag='sufficient'>
                        <module-option name='dsJndiName'>java:/PortalDS</module-option>
                        <module-option name='principalsQuery'>SELECT jbp_password FROM jbp_users WHERE jbp_uname=?</module-option>
                        <module-option name='rolesQuery'>
                          SELECT jbp_roles.jbp_name, 'Roles' 
                          FROM  jbp_role_membership 
                          INNER JOIN jbp_roles ON jbp_role_membership.jbp_rid = jbp_roles.jbp_rid
                          INNER JOIN jbp_users ON jbp_role_membership.jbp_uid = jbp_users.jbp_uid 
                          WHERE jbp_users.jbp_uname=?
                        </module-option>
                        <module-option name='hashAlgorithm'>MD5</module-option>
                        <module-option name='hashEncoding'>HEX</module-option>
                        <module-option name='additionalRole'>Authenticated</module-option>
                     </login-module>
                           </authentication> 
            </application-policy>
    

    Now, enable SSO using Tomcat's Valve:

    server/default/deploy/jboss-web.deployer/server.xml:

    <valve className='org.apache.catalina.authenticator.SingleSignOn' />
    

    Set the security domain in Pentaho to portal:

    server/default/deploy/pentaho.war/WEB-INF/jboss-web.xml:

    <security-domain>java:jaas/portal</security-domain>
    

    Now, add the relevant security rolet to pentaho's webapp. This will be used by our preauth filter to get the roles of the Principal. We could probably create our own roles retriever but I'm just using a WebXmlMappableAttributesRetriever

    server/default/deploy/pentaho.war/WEB-INF/web.xml:

    <security-role> 
        <role-name>User</role-name> 
      </security-role> 
        <security-role> 
        <role-name>Admin</role-name> 
      </security-role> 
      <security-role> 
        <role-name>Authenticated</role-name> 
      </security-role>
    

    Remove securityContextHolderAwareRequestFilter and add a j2eePreAuthFilter before authenticationProcessingFilter:

    pentaho-solutions/system/applicationContext-spring-security.xml:

    <bean id='j2eePreAuthFilter' class='org.springframework.security.ui.preauth.j2ee.J2eePreAuthenticatedProcessingFilter'>
        <property name='authenticationManager' ref='authenticationManager' />
        <property name='authenticationDetailsSource' ref='authenticationDetailsSource' />
    </bean>
        
    <bean id='preAuthenticatedAuthenticationProvider' class='org.springframework.security.providers.preauth.PreAuthenticatedAuthenticationProvider'>
      <property name='preAuthenticatedUserDetailsService' ref='preAuthenticatedUserDetailsService' />
    </bean>
    
    <bean id='preAuthenticatedUserDetailsService' class='org.springframework.security.providers.preauth.PreAuthenticatedGrantedAuthoritiesUserDetailsService' />
    
    <bean id='authenticationDetailsSource' class='org.springframework.security.ui.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource'>
      <property name='mappableRolesRetriever' ref='j2eeMappableRolesRetriever' />
      <property name='userRoles2GrantedAuthoritiesMapper' ref='j2eeUserRoles2GrantedAuthoritiesMapper' />
    </bean>
    
    <bean id='j2eeUserRoles2GrantedAuthoritiesMapper' class='org.springframework.security.authoritymapping.SimpleAttributes2GrantedAuthoritiesMapper'>
          <property name='convertAttributeToUpperCase' value='false' />
          <property name='attributePrefix' value='' />
    </bean>
    
      <bean id='j2eeMappableRolesRetriever' class='org.springframework.security.ui.preauth.j2ee.WebXmlMappableAttributesRetriever'>
      <property name='webXmlInputStream'><bean factory-bean='webXmlResource' factory-method='getInputStream' />
    </property>
    </bean>
    
    <bean id='webXmlResource' class='org.springframework.web.context.support.ServletContextResource'>
      <constructor-arg ref='servletContext' />
      <constructor-arg value='/WEB-INF/web.xml' />
    </bean>
    
    <bean id='servletContext' class='org.springframework.web.context.support.ServletContextFactoryBean' />
    
    <bean id='authenticationManager' class='org.springframework.security.providers.ProviderManager'>
      <property name='providers'>
              <list>
                  <ref bean='preAuthenticatedAuthenticationProvider' />
                      <ref bean='daoAuthenticationProvider' />
                      <ref local='anonymousAuthenticationProvider' />
              </list>
      </property>
    </bean>
    

     

    And that should work, allowing any JBoss Portal User to get access to Pentaho. I'm using Form based authentication and have 

    <security-constraint>
        <web-resource-collection>
           <web-resource-name>Authenticated</web-resource-name>
           <description></description>
           <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
           <role-name>Authenticated</role-name>
        </auth-constraint>
     </security-constraint>
    

    in my jboss/server/default/deploy/jboss-portal.sar/portal-server.war/WEB-INF/web.xml. So any user which is logged in gets the Authenticated role which, fortunatelly, maps nicely to the roles in Pentaho.

    We can create new roles easy, just add the, to the security roles in pentaho's WEB-INF since the role mappes just goes through these and figures out if the user has that role.

     

    JAAS

    I will probably also try to get direct login to Pentaho working with JAAS so these are a couple of notes I've got on that:

    Add a jaasAuthenticationProvider,,,

    pentaho-solutions/system/applicationContext-spring-security.xml:

    <bean id='authenticationManager' class='org.springframework.security.providers.ProviderManager'>
      <property name='providers'>
              <list>
                  <ref bean='preAuthenticatedAuthenticationProvider' />
                  <ref bean='jaasAuthenticationProvider' />
                      <ref bean='daoAuthenticationProvider' />
                      <ref local='anonymousAuthenticationProvider' />
              </list>
      </property>
    </bean>
    
    <bean id='jaasAuthenticationProvider' class='org.springframework.security.providers.jaas.JaasAuthenticationProvider'>
      <property name='loginConfig'>
          <value>auth.conf</value>
      </property>
      <property name='loginContextName'>
          <value>pentaho</value>
      </property>
      <property name='callbackHandlers'>
          <list>
          <bean class='org.springframework.security.providers.jaas.JaasNameCallbackHandler' />
          <bean class='org.springframework.security.providers.jaas.JaasPasswordCallbackHandler' />
          </list>
      </property>
      <property name='authorityGranters'>
          <list>
            <ref bean='jaasAuthorityGranter' />
          </list>
      </property>
    </bean>        
    
    <bean id='jaasAuthorityGranter' class='pt.inevo.pentaho.jaas.JaasAuthorityGranter' />
    

    When using JAAS we'll be performing the queries to java:/PortalDS and since we have different Hibernate versions we get into trouble. I haven't done much work regarding this issue but changing the libs in the JBoss server should work.

    Also a couple of issues with the query AST might come up so use:

     

    server/default/deploy/ejb3.deployer/META-INF/persistence.properties:

    hibernate.bytecode.provider=cglib
    

     

    Emscripten - an LLVM-to-JavaScript compiler

    Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode (which can be generated from C/C , using llvm-gcc or clang, or any other language that can be converted into LLVM) and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).

    Demos:

    • Python - CPython compiled to JavaScript. Note: Currently broken in Firefox 4 (may crash your browser)
    • Bullet physics - The Bullet physics engine, combined with WebGL rendering
    • Ray tracing - A simple C ray tracer, rendering to a canvas element
    • Lua - The Lua interpreter.

    A letter to the Notion Ink EAP Team

    Dear EAP team,

    We, at inEvo, were fortunate to be selected by Notion Ink to be part of the Early Access Program Phase 2 back in late November 2010.

    We made a commitment to you to not disclose any information about our selection, the process involved and other related details with anyone. As such we've refrained to posting comments in both our and your blogs.

    Expectations were high as we'd be getting access to the Software Development Kit in the 1st Week of December, receiving a Customized Sample Device by the 3rd week of December and also getting a URL and other details for booking our EAP samples, with a little customization which you could do according to our needs.

    On December 9th we read in your blog (thanks for the heads up) that preorders would be available from 00:00 to 06:00 IST. So we got in touch with you in order to get the URL beforehand.
    All went as expected and you quickly replied us only 2 days later, on December 12th, that we could just use the pre-order link to place our orders, like everyone else.

    Since we got this very personalized message, despite knowing that all the PixelQ equiped units - which is what we wanted - were already sold out ,we promptly replied to you asking for 2 Notion Ink's with the Pixel Qi screen and WiFi.

    It's been two months and still nothing... We're now on 2011 and only a couple of weeks from the release of several Honeycomb based tablets  ( well, at least one ) and would like to thank you for preventing us from jumping too soon and making a wrong call regarding our tablet of choice.

    Notion Ink set apart from the competition thanks to the transparency of the development process and the friendly community around it. Had we known better we wouldn't have applied for the EAP and would just have commented like there was no tomorrow in your posts.

    The lesson was learnt!

    Thank you,

        Nelson Silva 

    Sammy

    Sammy is a tiny javascript framework built on top of jQuery. It’s RESTful Evented JavaScript. Not only does it allow you to respond to specific URLs, but utilizing the URL hash (#) you can create single page applications that still respond to the back button in your browser (ala Gmail).

     

    The disposable academic - Why doing a PhD is often a waste of time

    This article from the Economist is a must for all involved in academic work. With sentences like "universities have discovered that PhD students are cheap, highly motivated and disposable labour" or "Writing lab reports, giving academic presentations and conducting six-month literature reviews can be surprisingly unhelpful in a world where technical knowledge has to be assimilated quickly and presented simply to a wide audience" critics compare research doctorates to Ponzi or pyramid schemes

    Spark

    Spark is a MapReduce-like cluster computing framework written in Scala and running on top of the Mesos cluster manager, it makes it easy to write parallel jobs.

    Estimating Pi with Spark:

    val spark = new SparkContext(<Mesos master>)
    var count = spark.accumulator(0)
    for (i <- spark.parallelize(1 to 10000, 10)) {
      val x = Math.random * 2 - 1
      val y = Math.random * 2 - 1
      if (x*x + y*y < 1) count += 1
    }
    println("Pi is roughly " + 4 * count.value / 10000.0)

    Travel Budget

    We've been playing with Scala + Akka for a while and Tiago had too much fun with actors and STM so he put together Travel Budget

    Travel Budget is a web application for discovering travel possibilities. It works with a budget and a time-frame to maximize travelling tours and options.

    It goes through tens of millions of possible solutions and gives you those that are within your budget and other constraints.

    We hope you like it. Feel free to send us your comments/suggestions.

    inEvo Labs

    inEvo Labs

    inEvo just released inEvo Labs where we'll be showcasing some prototypes/experiments resulting from our research activities. We hope you like it ;)

     

    Is this the time for JavaScript

    I was wondering if there were any decent benchmarks of the V8 Javascript engine and I found this:

    http://shootout.alioth.debian.org/u32/javascript.php

    Does this mean Javascript is one of the baskets we should put our eggs in, now that Apache has left the JCP and we don't know what Oracle has in store for Java ?

    I've been toying with the idea of having a stateful web framework written in Javascript. Imagine Lift but written in Javascript.
    A view first approach where the snippets use jQuery selectors to bind to the XHTML nodes and where the framework "magically" divides the function calls into server/client code (it's all javascript isn't it).

    Wouldn't it be nice to be able to do:

    function myForm() {  
      $("#button").onclick = function () { 
        alert("You clicked on a button ... now i'll work my magic ;)"); // This is client side stuff so it goes with the final page 
        // Here we must create a server call 
        fs.readFile('/etc/passwd', function (err, data) { 
          if (!err) 
            $("#result").append(data); // And back to the client 
        }); 
     }
    }


    If you look at Lift they're pretty close to this but they use a JavaScript DSL which I'm not really fond of. I really think this could be pulled off and what I feel is the best thing about the concept is the "magical" division of server/client code.
    We could make it explicit initially but it should be like a JIT for the web where the optimizations are done in runtime with clever distribution of the load between the server and its clients.

     

    Go vs Scala vs NodeJS

    These test are run with:

    ab -c 100 -n 50000 http://localhost:8000/

    The memory usage is analyzed with : pmap $PID | tail -1 >> mem.txt; SLEEP 0.001

      Req/s Mem  
    Go 9428.90 ~18.5M  
    NodeJS 5775.84 ~646M  
    Java(Netty) 7086.34 ~2 434M (No flags)
    " 6931.62 ~512M (-server -Xmx128m -XX:+UseConcMarkSweepGC)
    " 9682.42 ~577M (after a couple of runs for JIT warmup )

    Conclusion ?

    The mem test takes into account all the libraries loaded by the process as this seemed to be the fairest comparison.

    The Java version might be doing a bit more than the the others since it was an experience I had here... Without explicit JVM flags it gets very memory hungry and I bet that a -server flag without memory restrictions would be a huge mem hog. Fortunately the last tests show that with a memory usage similar to NodeJS's it does a whole lot better (I'm happy cause I can keep using Scala instead of Javascript :P)

    NodeJS has a lot of hype around it and it definetly deserves it but without the web workers support it is not capable of taking full advantage of several cores and you'd have to go with a local pool and a load balancer in front of it. It's memory usage is not the best.

    Finally Google's Go is surely the big winner here. With very little memory usage and good performance it is the undisputed champion. Also, I'm using the 6g compiler but have read somewhere that gccgo could bring a 50% improvement. The language's not great but it sure beats C, C++ and even Java in my book ;)

    Scala on Android 101 - Proguard, XmlParser and Function2AsyncTask

    I've neglected my blog for a while so I though I'd share my latest experiments with running Scala on Android. In order to try a couple of things with Scala on Android I'm creating an Activity which  basically trying to consume a WebService and display the results in a List.Please keep in mind that I'm only learning Scala now so I'll gladly get any comments and/or suggestions regarding my solutions.These are the problems I've found  :

    • PROBLEM #1
      • The scala-library.jar is too big to dex so you have to use ProGuard to keep things small. Try making it work properly with maven and you'll have issues with getting all the plugins working in the right order!
    • SOLUTION #1
      • Ditch maven! "But having it manage my dependencies is so cool..." I hear you say.. well let's use Ivy:

    Just use one of the numerous ANT build scripts for Android and add the targets for Ivy and ProGuard and the scala repository to your ivysettings.xml:

    .....
    <url name="scala-tools.org">
         <artifact pattern="http://scala-tools.org/repo-releases/[organisation]/[module]/[revision]/[module]-[revision].[ext]" />
      </url>
    ....

    add your dependencies to your ivy.xml:...

    <dependency org="org/scala-lang" name="scala-library" rev="2.8.0.r18462-b20090811081019"/>
    <dependency org="org/scala-lang" name="scala-compiler" rev="2.8.0.r18462-b20090811081019"/>
    <dependency org="net/sf/proguard" name="proguard" rev="4.3"/>

    ....

    Add some nice Ivy targets:

    <taskdef resource="org/apache/ivy/ant/antlib.xml"
         uri="antlib:org.apache.ivy.ant" classpath="${ivy.jar.dir}/ivy.jar"/>
     <target name="init-ivy" depends="download-ivy">
             <ivy:settings file="${basedir}/ivysettings.xml" />
            <ivy:retrieve />
    </target>
      <target name="download-ivy">
        <mkdir dir="${ivy.jar.dir}"/>
        <get src="http://www.integratebutton.com/repo/
           ${ivy.install.version}/ivy-2.0.0-beta2.jar"
          dest="${ivy.jar.file}" usetimestamp="true"/>
      </target>

    a nice ProGuard target:

        <target name="proguard" depends="compile">
          <taskdef resource="proguard/ant/task.properties"
                   classpath="${external-libs}/proguard-4.3.jar" />
          <proguard>
    -injars ${outdir}/classes:${external-libs}/scala-library-2.8.0.r18462-b20090811081019.jar(!META-INF/MANIFEST.MF,!library.properties)
    -outjars ${outdir}/classes.min.jar
    -libraryjars ${android-jar}
    -dontwarn
    -dontoptimize
    -dontobfuscate
    -dontskipnonpubliclibraryclasses
    -dontskipnonpubliclibraryclassmembers
    -allowaccessmodification
    -keep public class <your main class here>
    -keep interface scala.ScalaObject
          </proguard>
        </target>

    and you're good to go!

    • PROBLEM #2
      • Scala uses the Java SAX parser which kept complaining about the use of namespace prefixes so I had to come up with a way to toggle this feature.
    • SOLUTION #2
      • Use my own SAX parser instance with the right features toggled :
    object XmlParser {
      // Workaround for namespace prefix
      private val namespacePrefixes = "http://xml.org/sax/features/namespace-prefixes"
      val parser = javax.xml.parsers.SAXParserFactory.newInstance()
      parser.setNamespaceAware(false)
      parser.setFeature(namespacePrefixes, true)
      def load(i:InputStream) = XML.withSAXParser(parser.newSAXParser()).load(i)
    }
    •  PROBLEM #3 :
      • AyncTask requires you to override an abstract method with varargs which is a no no right now in scala Ticket #1459
    • SOLUTION #3
      • Create a java class which extends AsyncTask, I called it MyAsyncTask:
    import android.os.AsyncTask;
    public abstract class MyAsyncTask<T1,T2,T3> extends AsyncTask<T1,T2,T2>{
            protected T2 doInBackground(T1 ...f) {
                    return doInBackground(f[0]);
            }
            abstract protected T2 doInBackground(T1 f);
    }

    As you can see now doInBackground gets a single argument... which in my current implementation is a Function =)I've created an AsyncTask class in scala which extends MyAsyncTask and gets a Function as it's constructor arg :

    class AsyncTask(f:()=>Unit) {
        def doInBackground {
            new _root_.pt.inevo.android.meo.MyAsyncTask[Function0[Unit],Void,Void]  {
               override protected def doInBackground(f: () => Unit):Void = {
                   f()
                   return null;
               }
              def onProgressUpdate(progress:Int*) { }
             override def  onPostExecute(result:Void) { }
       }.execute(f);         } }

    I also added an implicit to convert from Function into my new AsyncTask class and wrapped it in an object:

    object AsyncTask {
         implicit def function2AsyncTask(f: ()=>Unit):AsyncTask=new AsyncTask(f)
    }

    With this I can simply have regular functions and just call doInBackground:

    val getChannelList = () => {
          for( c <- EPG.getChannelList \\ "Channel" ) {
             Log.v("getChannelList", c \ "Name" text )
    }
    override def onCreate(savedInstanceState: Bundle) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.main)
          getChannelList.doInBackground
    }

    I hope this helps some of you get started with Scala on Android. I'll be working on the layout stuff trying to get ListView and the Adapters to work properly.P.S: I'm using the great DataBinder Dispatch library for Scala to call the service through REST so my Service object is something like:

     object EPG {
        val req = :/("services.sapo.pt") / "EPG"
        def getChannelList=Http(req / "GetChannelList" >> as_xml)
    }
     where the as_xml function is defined in my XmlParser class as:
    def as_xml(res:InputStream) = XmlParser.load(res);

    Reuse your Flex SDK with Sprouts

    Well, I just started taking a look at Sprouts to manage my Flex builds by I didn't want to download the Flex SDK again! As soon as I saw that Sprouts was trying to download the flex_sdk_3.zip file I just Ctrl-C and did the following:
    1. touch ~/.sprouts/cache/sprout-flex3sdk-tool-3.3.0/flex_sdk_3.zip
    2. ln -s /opt/Adobe Flex/flex_sdk_3 ~/.sprouts/cache/sprout-flex3sdk-tool-3.3.0/archive
    This just fools Sprouts which is advised against in the google groups discussions but saves my some time :P I did a similar process for the Flash Player 10 which I had downloaded but you could also use the one that came with the Flex SDK. Basically you need to :
    1. touch  ~/.sprouts/cache/0.7/sprout-flashplayer-tool-10.22.1/flash_player_10_linux_dev.tar.gz
    2. mkdir ~/.sprouts/cache/0.7/sprout-flashplayer-tool-10.22.1/archive
    3. ln -s <wherever>/flash_player_10_linux_dev ~/.sprouts/cache/0.7/sprout-flashplayer-tool-10.22.1/archive/flash_player_10_linux_dev
    This is a simple hack but might be useful to some of you.

    Create RoR model diagrams with yUMLmeRails

    I just hacked together a simple plugin for RoR apps in order to show a simple model diagram.
    I've been a fan of RailRoad since the beginning, it generates simple dot and neato diagrams from your rails application. Now I just found out about the cool http://yuml.me beta and decided to use RailRoad to create yUML diagrams.

    I created a GitHub project so you can look at the code and make whatever changes you want. The project page has more info on the plugin. I was also thinking of adding a Shoes app to create the diagrams but for now I just use it to display the diagram. Please keep in mind that the source's all messy and is far from finished,  perhaps someone else would like to make it a whole lot better ;)

    Rails + PostgreSQL accent insensitive search

    For a project we're currently working on the need for accent insensitive search came up. I googled for a couple of solutions, most of which required applying a translate() to the records or using to_ascii()  in order to replace accented characters with plain ones. For me this should be a considerable performance hit as the DB had to apply the function to every record before performing the query. I decided to go with a case insensitive using the operator '~*'.

    Basically, for each character which has accented variations I introduce an expression (ex: a -> (a|à|á|â|ã) resulting in a query like :

    select * from my_table where name ~* '(a|à|á|â|ã)ndr(e|é|è|ê)'

    This will search for the name "André" in the database. Since I'm using ruby I created a new method in the String class called accent_insensitive_regexp:

    class String  
      @@ACCENT_INSENSITIVE_REGEXES=[ "(a|á|à|â|ã)","(e|é|è|ê)","(i|í|ì)","(o|ó|ò|ô|õ)","(u|ú|ù)","(c|ç)" ]  
      
      def accent_insensitive_regexp 
       res=self.@@ACCENT_INSENSITIVE_REGEXES.each {|exp| res.gsub! Regexp.new(exp), exp } 
       res 
      end  
    end

    Now i can my case and accent insensitive search working by using something like: User.find :all, :conditions=>[" name ~* '?' ",name.downcase.accent_insensitive_regexp] Those who've tried to use the downcase and upcase methods in Rails will notice that this will not work properly since these methods to not deal with the accented characters. For dealing with that issue I changed those two methods in the String class to:

    alias_method :old_upcase, :upcase 
      alias_method :old_downcase, :downcase 
      
      @@DOWNCASE_ACCENT_CHARS="çàáèéìíòóùúâêôãõ"
      @@UPCASE_ACCENT_CHARS="ÇÀÁÈÉÌÍÒÓÙÚÂÊÔÃÕ"  
    
      def upcase 
        self.old_upcase.tr(@@DOWNCASE_ACCENT_CHARS,@@UPCASE_ACCENT_CHARS) 
      end  
    
      def downcase 
        self.old_downcase.tr(@@UPCASE_ACCENT_CHARS,@@DOWNCASE_ACCENT_CHARS) 
      end

    And there you have it. I quick and dirty hack to get this stuff to work properly in RoR. Please notice I haven't spent much time cleaning the code I just though this could get someone with similar problems started.

    JavaFX + Maven on Ubuntu

    Well, I've been following JavaFx for a long time but I've only started trying out yesterday. I need to create some GUI for testing some libraries I've been working on and I felt this was the right time to try out JavaFX.

    The first challenge was to get the JavaFX Preview SDK working on my Ubuntu machine. After googling around I found that the MacOSX version of the SDK works on Linux so I downloaded it from here.
    Then I got the Netbeans JavaFX Plugin from the JavaFX_NB_Daily project.
    After installing the nbms I decided to switch the SDK provided by the plugin with the one I downloaded from Sun's JavaFX page. For that I went into my netbeans directory "~/netbeans6-1" onto the "javafx" directory, renamed the sdk dir and unziped sun's sdk. This step is probably not necessary but I'd like to make sure I was working with Sun's JavaFX SDK.
    Well, with everything setup I started netbeans, created a project with the sample weather app and it ran just fine :)
    The next step was to get maven to compile a JavaFX project. I normally have several dependencies for each new project and I've really come to appreciate Maven for dealing with these for me. Again I googled for it and found this: http://m2-javafxc.sourceforge.net/. Well it did what I needed, just had to figure out how to use my javafxc compiler. This is what I came up with in the end:

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>pt.inevo.javafx</groupId>
    <artifactId>javaFXMaven</artifactId>
    <name>javaFXMaven</name>
    <version>1.0</version>
    <url>http://www.inevo.pt</url>
    <properties>
    <javafx.home>/home/nfgs/netbeans-6.1/javafx/javafx-sdk1.0pre1</javafx.home>
    <netbeans.hint.useExternalMaven>true</netbeans.hint.useExternalMaven>
    </properties>
    <dependencies>
    …
    <dependency>
    <groupId>javafx</groupId>
    <artifactId>javafxrt</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${javafx.home}/lib/javafxrt.jar</systemPath>
    </dependency>
    <dependency>
    <groupId>javafx</groupId>
    <artifactId>javafx-swing</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${javafx.home}/lib/javafx-swing.jar</systemPath>
    </dependency>
    <dependency>
    <groupId>javafx</groupId>
    <artifactId>Scenario</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${javafx.home}/lib/Scenario.jar</systemPath>
    </dependency>
    <dependency>
    <groupId>javafx</groupId>
    <artifactId>javafxgui</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${javafx.home}/lib/javafxgui.jar</systemPath>
    </dependency>
    </dependencies>
    <pluginRepositories>
    <pluginRepository>
    <id>m2-javafxc</id>
    <name>Sourceforge M2-javafxc static repo</name>
    <url>http://m2-javafxc.sourceforge.net/m2repo</url>
    </pluginRepository>
    </pluginRepositories>
    <build>
    <sourceDirectory>src/main/javafx</sourceDirectory>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.6</source>
    <target>1.6</target>
    <compilerId>javafxc</compilerId>
    <include>**/*.fx</include>
    <compilerArguments>
    <jfxHome>false</jfxHome>
    </compilerArguments>
    </configuration>
    <dependencies>
    <dependency>
    <groupId>net.sf.m2javafxc</groupId>
    <artifactId>plexus-compiler-javafxc</artifactId>
    <version>0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
    <groupId>javafx</groupId>
    <artifactId>javafxc</artifactId>
    <version>0.1-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${javafx.home}/lib/javafxc.jar</systemPath>
    </dependency>
    </dependencies>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <archive>
    <manifest>
    <mainClass>fxsketch.Main</mainClass>
    </manifest>
    </archive>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>

    Now I can store my code in src/main/javafx and just open my project with netbeans (since it reads the pom.xml file). I'm using the external maven since I've had a few problems with the internal version. I hope this helps some of you. I'm really starting to appreciate JavaFx and can't wait to get the Media SDK and the SG3D to really come up with something interesting ;)

    Dealing with UTF-8 with appengine's bulk loading

    We just uploaded our first app to Google Appengine's servers. It's called YouDIG - You Draw I Guess - and we believe it to be a fun game to play online. Being a word based game we had to have an easy way to upload new Riddles to the online app. So, I created a very simple bulkload.py which just calls the ImportCSV function from the SDK. The problem was that it doesn't work with UTF-8 files! Here's what I did (I'm still a Python newbie, so feel free to send me your comments/suggestions): 1 - Edited the google/appengine/tools/bulkload_client.py

    def ContentGenerator
    ....
      if rows_written > 0:
        yield rows_written, unicode(content.getvalue(),'utf-8')
    
      def PostEntities
      ....
      body = urllib.urlencode({
        constants.KIND_PARAM: kind,
        constants.CSV_PARAM: content.encode("utf-8"),
        })

    this basically unicodes everything and encodes it as UTF-8 before sending the POST request. 2 - Created my Loaders (similar to the ones described in the docs):

    class RiddleLoader(bulkload.Loader):
    
      def HandleEntity(self, entity):
        entity['approved']=True
        return entity
    
      def __init__(self):
        bulkload.Loader.__init__(self, 'Riddle',
          [('word', Riddle.lowerCase),('level', str),
           ('language', Language.get_key_by_code ),
           ('category', Category.get_key_by_sys_name)])
    
    if __name__ == '__main__':
      mybulkload.main(RiddleLoader())

    This is a simple loader that extends the bulkload.Loader. The "Language.get_key_by_code" and "Category.get_key_by_sys_name" are static functions that allow me to get the entity based on a string. This way I can import Languages, Categories and Riddles and have the relations set using simple string keys (since I don't know and entity's key before it's saved!). The main difference from the standard bulkloading is in the main method. The "mybulkload" which is a class extending "BulkLoad" and allows to receive UTF-8 CSV POST data. 3 - The mybulkload package:

    def utf_8_encoder(unicode_csv_data):
      for line in unicode_csv_data:
        yield line.encode('utf-8')
    
    class MyBulkLoad(BulkLoad):
    """ A handler for bulk load requests.
    """
    
      def Load(self, kind, data):
        Validate(kind, basestring)
        Validate(data, basestring)
           output = []
    
        try:
          loader = Loader.RegisteredLoaders()[kind]
        except KeyError:
          output.append('Error: no Loader defined for kind %s.' % kind)
          return (httplib.BAD_REQUEST, ''.join(output))
    
        buffer = StringIO.StringIO(data)
        reader = csv.reader(utf_8_encoder(buffer), skipinitialspace=True)
    
        entities = []
    
        line_num = 1
    
        for row in reader:
          try:
           output.append('\nLoading from line %d...' % line_num)
           entities.extend(loader.CreateEntity([unicode(cell,'utf-8') for cell in row]))
           output.append('done.')
          except:
           exc_info = sys.exc_info()
           stacktrace = traceback.format_exception(*exc_info)
           output.append('error:\n%s' % stacktrace)
           return (httplib.BAD_REQUEST, ''.join(output))
    
          line_num += 1
    
         for entity in entities:
          datastore.Put(entity)
    
         return (httplib.OK, ''.join(output))
    
    def main(*loaders):
    """Starts bulk upload.
    Raises TypeError if not, at least one Loader instance is given.
    Args:
    loaders: One or more Loader instance.
    """
      if not loaders:
         raise TypeError('Expected at least one argument.')
    
      for loader in loaders:
        if not isinstance(loader, Loader):
         raise TypeError('Expected a Loader instance; received %r' % loader)
    
      application = webapp.WSGIApplication([('.*', MyBulkLoad)])
      wsgiref.handlers.CGIHandler().run(application)
    
    if __name__ == '__main__':
      main()

    i just copied this stuff from the __init.py__ in google/appengine/ext/bulkloa, added the utf8_encoder function  and extended the Bulkload class overloading the Load method. Here what I used:

    reader = csv.reader(utf_8_encoder(buffer), skipinitialspace=True)

    to encode the stuff send to the CSV reader and:

    entities.extend(loader.CreateEntity([unicode(cell,'utf-8') for cell in row]))

    to unicode everything before creating the entities. Perhaps there was an easier way but this is working for me so I hope this can help some of you. Next I'll write an entity eraser to bulk delete entities from the AppEngine's production servers...

    Flex FlickrFlow

    I needed a simple Flickr gallery viewer to show user's photos but I knew I'd seen a couple of cool Papervision based viewers so I decided to search for one. I found FlickrFlow and downloaded the source code and started hacking my way through it. First I made the background white and reduced the reflection size, this just required me to fill the bitmap white and create a smaller gradient box for the reflection. Then I decided it made no sense to use the full size image to show only a smaller one, so I switched to the AS3 Flick API and used the GetSizes API call to get the URL for the "Small" sized image. This worked and now it is a lot faster than it used to be. Still, FlickrShow would first fetch all the images and only then start loading then into CoverFlow, so I changed this as well.... Finally, I chose to always select the middle image instead of having a fixed index for selection. There are still a couple of bugs to fix, mainly regarding mouse click events which sometimes don't work unless you first use your mouse wheel or keyboard... in the meantime here is a small sample which searches for images with the tag "Madeira Island".

     

    Apache proxy for crossdomain problems

    Well, the Flash player requires a crossdomain.xml file to reside on every server you call for remote services. We needed to display RSS feeds for user's blogs and we couldn't enforce every single blog provider to have a crossdomain file. The solution was to use a local proxy to perform requests on our behalf. Common solutions use a server side script to fetch the contents but I didn't want to have yet another PHP, JAVA, Ruby, etc script laying around so I used Apache's mod_rewrite with a very simple rule:
    RewriteEngine On RewriteRule ^/my_rss_proxy/(.*) $1 [P]
    Since we were already using Apache's proxies to forward request to my HAProxy I just added this simple rule and now we can use:
    http://my_server_url/my_rss_proxy/http://rss_url

    RIA Wars

    The growing bandwith has allowed developers to provide rich Web based user interfaces and thus bring together Web and Dektop applications. You have Javascript with several new libraries sprouting almost everyday, this allows us to bring live to otherwise static HTML pages through manipulation of the DOM and using AJAX calls to get data from the server. Nonetheless javascript depends on the browser's implementation and your source code is there for anyone to see. AJAX calls are pretty cool but XML is a major overhead with large messages and slow parsing. Adobe has a huge browser penetration, close to 98% if i'm not mistaken. Most people dislike Flash and think it is only used by designers to create sexy and annoying banners. The truth is that Flash uses Mozilla's Tamarin VM for running ActionScript. Both Javascript and ActionScript are ECMAScript but with ActionScript you have the advantage that it get's compiled and your source code is not there for anyone to see (unless they disassemble it). Besides, to reproduce most of the functionality you get with Flash you need a lot of javascript libraries, especially if we're using Flex. Flex provides us a growing number of prebuild components and the UI is specified using MXML with offers a clear separation of controller and view. What's more upcoming versions of Flash will enable browsers to cache commonly used flash modules (like the Flex framework) so, you only download the library the first time you visit a site using Flex. The AMF protocol, used by Flash is now Open and there is a reference implementation freely available. This binary protocol is a lot faster than using XML or JSON and it supports pushing information thus we don't need to have clients constantly pooling our server. There are some more contenders in this RIA war, JavaFX and Silverlight. JavaFX suffers, for now, from the same problems every JAVA applet has. It does not fit well in a page, it takes too long to load and there are still many people without JAVA. When JAVA offers an easier instalation and preloads in the OS's memory then we could have a serious alternative but for now.. well it is just not up to the challenge. Silverlight has some cool ideas, it allows us to program in a myriad of languages since they share a common runtime, has hardware acceleration and, since it is from Microsoft will come preinstalled in Windows machines. Still, there is no real showcase (Microsoft is using it to build a part of their site now in beta), there have been a couple of releases which were incompatible with each other (you had to keep reinstalling the darn thing), and to get a development environment up and ready it took ages just to figure out the right one! I really like the idea of having Desktop like applications on the Web but i'd also like to have them in my desktop. Most of these allows developing both for the Web and for the Desktop but Flash and Java might just have an edge on this one. Adobe AIR allows you to develop some pretty cool desktop apps and JAVA has been improving Swing with LaF. To sum up, I like the fact that Javascript has been evolving at a fast pace but I still feel stranded by browser dependencies, I don't like to have the source code exposed and I just think that Adobe's offer is a better deal. ActionScript is a pretty decent language and the Flex framework offers us a lot of cool components. Add to that the AMF protocol with the Open Source Data Services and you have a very good solution for RIA development. What's more you have a couple of 3D libraries,Thermo's coming up ( pixel shaders ;) ) and we'll get hardware acceleration!

    Mobile Platforms thoughts for 2008

    Nowadays tech junkies have a real problem.. there are just too many cool gadgets to carry around. The convergence has started and I personally believe that the cell phone will become the "do it all" device of the future. We already have cell phones with decent cameras, PIM software and GPS. Add to this a good storage solution and  you got everything you'll need on a daily basis. The problem so far has been the platform. You've got Windows Mobile which has been around for a while, still it seems sluggish and is missing the "cool" factor many of us geeks miss... Nokia has the Maemo platform for the N800 which is pretty cool but there is no GSM support. Nokia also has the Symbian OS which allows us to develop in C and Python but it is not compatible with most other manufacturers.  Java has been around for most mobile platforms and there are some pretty decent apps and games but you get the feeling that JAVA is not build right into the platform.. some phones even have a separate JAVA area for you to start your JAVA applications. Apple has released the sexy iPhone and it is really appealling. Still, the SDK took long to come out and the platform remains too closed for any serious investment in it. Besides that, here in Europe we need 3,5G to have decent Internet access and the iPhone does not provide that. Then, Google came up with Android. It's impressive... Google can generate quite a hype around a mobile platform for which there is no hardware yet! This seems to be the way to go.. this way when it arrives at the market there should be lots of cool free software around the web for it, especially with money being offered to the best apps! Nonetheless Apple's iPhone still has the sex appeal that most phones only dream of having.... Adobe has been focusing on AIR, Thermo, etc.. they have come up with great technologies for Web and Desktop but delayed the Adobe Flash Lite. Personally I'm not very interested in a stripped down AS2 only Flash version... this could be because they're hopping the hardware will evolve to support the whole Flash runtime... The actionscript engine uses the TamarinVM so it should be ported to most of mainstreammobile platforms soon. Well, this post is becoming too long, I just though i'd write here my 2 cents. I'll be keeping and eye on Google's Android and the iPhone.. wouldn't it be a beautiful mariage?

    AS3 Physics and shape recognizer

    Lately I've been hacking some AS3 code. Just some miscelaneous experiments or AS ports from existing source code. I've tried the AS3 Box2D physics package, since i'm pretty fond of simple physics games or experiments, and I've also ported some of the CALI's code to AS. I've read about a project that aims to automatic code porting from C++ to AS3 so this could just be useless in a couple of weeks...

    Nonetheless here goes what I've come up with in my free time. It's a very simple editor that recognizes lines, circles, rectangles and triangles (i still have to tune the rectangles and triangles) and also some simple commands like delete and wavy line. I've hooked up the lines and circles with the physics engine so they will fall as soon as their created. I still haven't found the time to do the same for the other shapes. You can also use the mouse to move circles and lines around the world. AS3 Physics and shape recognizer Try evoPhysics.

    Back online!

    After a couple of months without posting to my previous Mephisto powered blog I decided it was time for a fresh start… This is it! My WordPress powered blog… I’ll try to keep this one updated.