Tech Ramblings http://blog.nelsonsilva.eu by Nelson Silva posterous.com Tue, 18 Oct 2011 02:58:00 -0700 Securing JS - "window.location" can we trust in it ? http://blog.nelsonsilva.eu/securing-js-windowlocation-can-we-trust-in-it http://blog.nelsonsilva.eu/securing-js-windowlocation-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 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Thu, 07 Jul 2011 08:18:00 -0700 V8 powered Web/Desktop/Mobile GL http://blog.nelsonsilva.eu/v8-powered-webdesktopmobile-gl http://blog.nelsonsilva.eu/v8-powered-webdesktopmobile-gl

I finally got our V8 powered WebGL javascript framework running on desktop, browser and Android devices ;)

For more info: http://labs.inevo.pt/v8-powered-webdesktopmobile-gl

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Mon, 14 Feb 2011 02:27:00 -0800 Pentaho on JBoss Portal SSO http://blog.nelsonsilva.eu/pentaho-on-jboss-portal-sso http://blog.nelsonsilva.eu/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</code></p>

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Tue, 01 Feb 2011 12:57:00 -0800 Emscripten - an LLVM-to-JavaScript compiler http://blog.nelsonsilva.eu/emscripten-an-llvm-to-javascript-compiler http://blog.nelsonsilva.eu/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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Tue, 01 Feb 2011 04:45:00 -0800 A letter to the Notion Ink EAP Team http://blog.nelsonsilva.eu/a-letter-to-the-notion-ink-eap-team http://blog.nelsonsilva.eu/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 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Mon, 24 Jan 2011 04:35:00 -0800 Sammy http://blog.nelsonsilva.eu/sammy http://blog.nelsonsilva.eu/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).

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Sat, 22 Jan 2011 08:03:00 -0800 The disposable academic - Why doing a PhD is often a waste of time http://blog.nelsonsilva.eu/the-disposable-academic-why-doing-a-phd-is-of http://blog.nelsonsilva.eu/the-disposable-academic-why-doing-a-phd-is-of

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

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Sat, 22 Jan 2011 04:43:57 -0800 7 things that make Lift stand out http://blog.nelsonsilva.eu/7-things-that-make-lift-stand-out http://blog.nelsonsilva.eu/7-things-that-make-lift-stand-out Lift is a fabulous stateful web framework written in Scala.
These are seven of the things that make it stand out from the crowd (besides being written in Scala :p) :

http://seventhings.liftweb.net/index

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Fri, 21 Jan 2011 10:00:00 -0800 inEvo's new site is online! http://blog.nelsonsilva.eu/inevos-new-site-is-online http://blog.nelsonsilva.eu/inevos-new-site-is-online

Our revamped website just went online.
We hope to get positive feedback from everyone and keep coming back 'cause we'll keep this one up to date ;)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Thu, 20 Jan 2011 02:10:00 -0800 Spark http://blog.nelsonsilva.eu/40400132 http://blog.nelsonsilva.eu/40400132

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)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Wed, 19 Jan 2011 15:08:04 -0800 Semantic ECM http://blog.nelsonsilva.eu/semantic-ecm http://blog.nelsonsilva.eu/semantic-ecm Using wikipedia and hadoop to train OpenNLP to extract semantic entities in an ECM :
http://blogs.nuxeo.com/dev/2011/01/mining-wikipedia-with-hadoop-and-pig-for-n...

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Mon, 10 Jan 2011 04:06:00 -0800 Travel Budget http://blog.nelsonsilva.eu/travel-budget http://blog.nelsonsilva.eu/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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Mon, 10 Jan 2011 04:01:00 -0800 inEvo Labs http://blog.nelsonsilva.eu/inevo-labs http://blog.nelsonsilva.eu/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 ;)

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Fri, 17 Dec 2010 14:19:00 -0800 Is this the time for JavaScript http://blog.nelsonsilva.eu/is-this-the-time-for-javascript http://blog.nelsonsilva.eu/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.

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Fri, 17 Dec 2010 05:06:52 -0800 Moved to Posterous http://blog.nelsonsilva.eu/moved-to-posterous http://blog.nelsonsilva.eu/moved-to-posterous I just moved this blog from WordPress to Posterous.

So far so good ;)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Tue, 14 Dec 2010 11:47:00 -0800 Go vs Scala vs NodeJS http://blog.nelsonsilva.eu/2010/12/14/go-vs-scala-vs-nodejs http://blog.nelsonsilva.eu/2010/12/14/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 ;)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Sat, 31 Oct 2009 00:23:00 -0700 Scala on Android 101 - Proguard, XmlParser and Function2AsyncTask http://blog.nelsonsilva.eu/2009/10/31/scala-on-android-101-proguard-xmlparser-and-function2asynctask http://blog.nelsonsilva.eu/2009/10/31/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);

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Fri, 05 Jun 2009 19:58:26 -0700 Reuse your Flex SDK with Sprouts http://blog.nelsonsilva.eu/2009/06/05/reuse-your-flex-sdk-with-sprouts http://blog.nelsonsilva.eu/2009/06/05/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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Fri, 22 May 2009 18:16:00 -0700 Create RoR model diagrams with yUMLmeRails http://blog.nelsonsilva.eu/2009/05/22/create-ror-model-diagrams-with-yumlmerails http://blog.nelsonsilva.eu/2009/05/22/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 ;)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva
Mon, 15 Dec 2008 13:14:00 -0800 Rails + PostgreSQL accent insensitive search http://blog.nelsonsilva.eu/2008/12/15/rails-postgresql-accent-insensitive-search http://blog.nelsonsilva.eu/2008/12/15/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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/912536/3743045397_20218264db-1.jpg http://posterous.com/users/1l1tNi609KCZ Nelson Silva nelsonsilva Nelson Silva