Tuesday, September 10, 2013

weblog; java, html, vim

Vim

How to select between brackets (or quotes or …) in vim?

http://stackoverflow.com/questions/1061933/how-to-select-between-brackets-or-quotes-or-in-vim/1062001#1062001 http://vimdoc.sourceforge.net/htmldoc/motion.html#object-select

To select between the single quotes I usually do a vi' (select inner single quotes).

Inside a parenthesis block, I use vib (select inner block)

Inside a curly braces block you can use viB (capital B)

To make the selections "inclusive" (select also the quotes, parenthesis or braces) you can use a instead of i.

You can read more about the Text object selections on the manual.

http://stackoverflow.com/questions/1061933/how-to-select-between-brackets-or-quotes-or-in-vim/1062001#1062001

Java

JAXBElement Response from WebServices

https://www.java.net//node/694561
Method m = item.getClass().getMethod("getFirstName");
JAXBElement firstName = (JAXBElement)m.invoke(item);
System.out.println(firstName.getValue());

m = item.getClass().getMethod("getLastName");
JAXBElement lastName = (JAXBElement)m.invoke(item);
System.out.println(lastName.getValue());

How do I prevent JAXBElement from being generated in a CXF Web Service client?

http://stackoverflow.com/questions/4413281/how-do-i-prevent-jaxbelementstring-from-being-generated-in-a-cxf-web-service-c http://stackoverflow.com/questions/18573468/wsdl2java-cxf-generating-jaxbelement-list-instead-of-fields
<jaxb:bindings version="2.1" 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
   <jaxb:globalBindings generateElementProperty="false"/> 
</jaxb:bindings> 
<jaxb:globalBindings generateElementProperty="false" fixedAttributeAsConstantProperty="true" choiceContentProperty="true">

Html

Href attribute for JavaScript links: “#” or “javascript:void(0)”?

http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid0
javascript:void(0)

Monday, September 9, 2013

weblog; jquery floating menus

JQuery

JQuery Floating Menus

http://www.jquery4u.com/menus/floating-message-plugins/

JQuery Floating Menu example

http://manos.malihu.gr/jquery-floating-menu/ http://manos.malihu.gr/tuts/jquery-floating-menu.html

Vim

Markdown syntax : Syntax file for Markdown text-to-HTML language

http://www.vim.org/scripts/script.php?script_id=1242

Looks interesting since I tried creating my own pseudo-markdown for my blog entries.

Friday, September 6, 2013

weblog; css; page curls and blockquotes

CSS

How to Create CSS3 Paper Curls Without Images

http://www.sitepoint.com/pure-css3-paper-curls/

Wonderful tutorial on making page curls without baking them into your graphics. Works for Firefox and any other CSS3 compliant browser. Thanks for not being compliant, IE.

Better Blockquotes

http://css-tricks.com/examples/Blockquotes/

Several ways to make blockquotes more pretty.

Different browsers have different built-in styling for blockquotes, often just a simple left margin. If you use a lot of quotes, as bloggers often do, it is a good idea to take control of this element and give it some CSS style!

http://css-tricks.com/examples/Blockquotes/

Thursday, September 5, 2013

weblog; cucumber, hibernate and certificates

Cucumber

Github: cucumber/cucumber-jvm

https://github.com/cucumber/cucumber-jvm http://cukes.info/install-cucumber-jvm.html https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-helloworld

Cucumber-JVM is a pure Java implementation of Cucumber that supports the most popular programming languages for the JVM.

https://github.com/cucumber/cucumber-jvm

Hibernate

How do we count rows using Hibernate?

http://stackoverflow.com/questions/1372317/how-do-we-count-rows-using-hibernate
Integer count = (Integer) session.CreateQuery("select count(*) from Books").UniqueResult();

Where 'Books' is the name off the class - not the table in the database.

hibernate.connection.autocommit

https://forum.hibernate.org/viewtopic.php?f=1&t=944848

It sounds like it's a good idea not to turn on auto-commit.

JPA + Hibernate + autocommit

http://stackoverflow.com/questions/1228086/jpa-hibernate-autocommit

The syntax is slightly different for the version of Hibernate I'm using.

NO!
<property name="hibernate.connection.autocommit" value="false"/>

Yes
<property name="hibernate.connection.autocommit">false</property>

remaining connection slots are reserved for non-replication superuser connections

http://community.webfaction.com/questions/12239/remaining-connection-slots-are-reserved-for-non-replication-superuser-connections http://stackoverflow.com/questions/5108876/kill-a-postgresql-session-connection

Manually locate the hanging Postres processes and kill them.

" extract pids from list of  processes
" after running 'ps -ef|grep postgres'
:%s/  \d\+ \(\d\+\) .\+\n\+/ \1/g

Supposedly, a similar outcome can be achieved with this query to the database:

"Before executing this query, you have to REVOKE the CONNECT privileges to avoid new connections:
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;

"Let us begin...
SELECT 
    pg_terminate_backend(pid) 
FROM 
    pg_stat_activity 
WHERE 
    -- don't kill my own connection!
    pid <> pg_backend_pid()
    -- don't kill the connections to other databases
    AND datname = 'database_name'
    ;

org.hibernate Interface Session

http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html#method_summary http://stackoverflow.com/questions/4588406/replacing-existing-rows-with-new-ones-causes-duplicate-key-exception

When in doubt, read the documentation. There are several things I learned here.

#evict: even after deleting a record, the identifier is still in the cache. Perhaps I ran into this problem because I hadn't closed the session yet or called #commit. Perhaps #evict was a better decision for performance sake in my particular case setting up my integration test.

#rollback: the Session#beginTransaction -> Transaction#commit block does not automatically issue a #rollback if an exception is thrown. You will need to capture exceptions and manually issue a rollback.

#persist: saves a record to the database; not sure if it is supposed to be used for updating, but I use it to save records to the database and use the identifier that I supply. #save will attempt to generate an id before data is persisted to the database.

#flush: queries are queued and reordered in a given transaction. Insert statements come before delete statements, so even if a delete statement is coded before the insert statement, when #commit is invoked, the insert will still come before the delete. Using flush in the transaction tells Hibernate to execute all queries currently in the queue before handling the remaining statements in the transaction. And yes, a #rollback will rollback code both before the #flush and after as long as it is still between the Session#beginTransaction and Transaction#commit.

A typical transaction should use the following idiom:

 Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.

http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html#method_summary

SSL and Certificates

Steps to create a csr using keytool and sign with verisign

http://wls4mscratch.wordpress.com/2010/06/08/steps-to-create-a-csr-and-sign-with-verisign/ http://mihail.stoynov.com/2009/03/12/certificates-keystores-java-keytool-utility-and-openssl/
keytool -genkey -alias server -keyalg RSA -sigalg SHA1withRSA -keysize 4096 -keystore server.jks -dname "CN=server, OU=Some Organization, O=Some Office, L=Some City, ST=CA, C=US" -validity 1095 -storepass $STORE_PASS

keytool -certreq -alias server -keystore server.jks -storepass $STORE_PASS > server.csr

keytool - Key and Certificate Management Tool

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/keytool.html

Comprehensive documentation on how to use keytool.

[K]eytool is a key and certificate management utility. It allows users to administer their own public/private key pairs and associated certificates for use in self-authentication... or data integrity and authentication services, using digital signatures.

Tuesday, September 3, 2013

weblog; Ruby game development

Java game library + JRuby + awesome DSL = Gemini

http://dkoontz.wordpress.com/2008/07/14/java-game-library-jruby-awesome-dsl-gemini/

It appears that Gemini is dead and no longer available. There continue to be folks saying that game development in Ruby is just not work while because of performance reasons.

Prelude of the Chambered (JRuby port)

https://github.com/peterc/potc-jruby

Cool clone for FPS like Castle Wolfenstein. In the middle of trying to understand how items are assigned to loot locations.

Slick2D: 2D Java Game Library

http://slick.ninjacave.com/

Slick2D is an easy to use set of tools and utilites wrapped around LWJGL OpenGL bindings to make 2D Java game development easier.

http://slick.ninjacave.com/

Let’s Build a Simple Video Game with JRuby: A Tutorial

http://www.rubyinside.com/video-game-ruby-tutorial-5726.html

Ruby isn't known for its game development chops despite having a handful of interesting libraries suited to it. Java, on the other hand, has a thriving and popular game development scene flooded with powerful libraries, tutorials and forums. Can we drag some of Java's thunder kicking and screaming over to the world of Ruby? Yep! - thanks to JRuby. Let's run through the steps to build a simple 'bat and ball' game now.

http://www.rubyinside.com/video-game-ruby-tutorial-5726.html
Ruby is not for game development. http://gafferongames.com/2009/01/11/ruby-is-not-at-all-suitable-for-game-development/

I love ruby. It’s beautiful language, with elegant and expressive syntax – perfect as a scripting language, and great for prototyping new ideas quickly… I use it every chance I get, and truly enjoy coding in it.

But is it suitable for game development?

Unfortunately the answer is a resounding no!

weblog; Hibernate, PostgreSql, Maven, JRuby

Hibernate

Hibernate opening/closing session, the correct approach for DAO

http://stackoverflow.com/questions/8841207/hibernate-opening-closing-session-the-correct-approach-for-dao

Experiencing problems with session, I think. After deleting records from the database, should the session be closed and opened again to stop getting exceptions?

Hibernate Error: a different object with the same identifier value was already associated with the session

http://stackoverflow.com/questions/16246675/hibernate-error-a-different-object-with-the-same-identifier-value-was-already-a

It seems this is where I'm having my current code hangup. Should I be closing the session between deletes and inserts? Or is there something being updated instead and that's throwing exceptions because I'm inadvertently attempting to update a record that has been destroyed in the same session?

PostgreSql

PostgreSQL: Get the second to last MAX(date)

http://stackoverflow.com/questions/16567632/postgresql-get-the-second-to-last-maxdate

Maven

Maven: Filtering the dependency tree

http://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html http://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html
mvn dependency:tree -Dincludes=velocity:velocity
mvn dependency:tree -Dverbose -Dincludes=commons-collections

I used this to resolve issues I was experiencing with log jars. I was including a version of one collection of jars. Another resource was pulling them in too, only a different version. This was raising some warnings anytime I ran maven. I used a call like this one to figure our where my conflicts existed.

JRuby

JRuby and Java Code Examples

https://github.com/jruby/jruby/wiki/JRubyAndJavaCodeExamples

How to call Ruby from Java code and Java from Ruby code.

JRuby limited openssl loaded - how to eliminate?

http://stackoverflow.com/questions/9017412/jruby-limited-openssl-loaded-how-to-eliminate

Moving an Existing Rails App to run on JRuby: Install JRuby locally

https://devcenter.heroku.com/articles/moving-an-existing-rails-app-to-run-on-jruby#specify-jruby-in-your-gemfile

Best way of invoking getter by reflection

http://stackoverflow.com/questions/2638590/best-way-of-invoking-getter-by-reflection

While trying to come up with my own testing utility for stubbing/mocking, I found this. Could be a good read, but I reverted back to JMockit.

Maven: Filtering the dependency tree http://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html

Call JRuby, Jython or other JVM scripting language from Maven

http://matschaffer.com/2009/10/call-jruby-jython-from-maven/
  <executions>
    <execution>
      <id>my_script</id>
      <phase>compile</phase>
      <configuration>
        <tasks>
          <java classname="org.jruby.Main" failonerror="yes">
            <arg value="${basedir}/src/main/ruby/myscript.rb" />
          </java>
        </tasks>
      </configuration>
    </execution>
  </executions>

Guide to installing 3rd party JARs

http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

Using JRuby with Maven

https://github.com/jruby/jruby/wiki/Using-JRuby-with-Maven
    <dependency>
      <groupId>org.jruby</groupId>
      <artifactId>jruby-core</artifactId>
      <version>1.7.0.RC1</version>
    </dependency>

weblog; Rails views; associations and ajax

Dynamic creation of variables in ruby

    inst_var_name = params[:controller].gsub(/\//, "_").singularize
    instance_variable_set("@#{inst_var_name}", @model)
http://www.funonrails.com/2010/02/dynamic-creation-of-variables-in-ruby.html

How to use dynamic attributes / columns in Squeel statements?

Interesting and has possibilities, but it was taking too much time to figure out a generic approach, so I fell back to typical active record call.

    klass = guess_model_class
    foreign_key_id = "#{klass.reflect_on_all_associations.first.name.to_s}_id"
    @models = klass.find(:all, conditions: ["#{foreign_key_id} = ?", params[foreign_key_id.to_sym]])

Squeel demands that an non-existent variable or method be called so that #method_missing gets called and uses the undefined name to generate sql.

    klass = guess_model_class
    foreign_key_id = "#{klass.reflect_on_all_associations.first.name.to_s}_id"

    # works, but is not generic
    @models = klass.where{new_and_returning_member_progress_id == params[:new_and_returning_member_progress_id]}

    # doesn't work
    @models = klass.where{klass.columns[0] == params[foreign_key_id.to_sym]}

    # doesn't work
    @models = klass.where{table_name.send(foreign_key_id.to_sym) == params[foreign_key_id.to_sym]}
http://stackoverflow.com/questions/12612889/how-to-use-dynamic-attributes-columns-in-squeel-statements

Test if a word is singular or plural in Ruby on Rails

def test_singularity(str)
  str.pluralize != str and str.singularize == str
end
    inst_var_name = params[:controller].gsub(/\//, "_").singularize
http://stackoverflow.com/questions/1801698/test-if-a-word-is-singular-or-plural-in-ruby-on-rails

polymorphic_url: Universal partial

    <div class="well"><%= link_to "Parents", polymorphic_url([@new_and_returning_member_progress, :family, :parents], layout: "none") %></div>
http://apidock.com/rails/ActionController/PolymorphicRoutes/polymorphic_url