Saturday, September 28, 2013

weblog; Apache CXF; getting message-level encryption to work

Bash

Bash Arrays | Linux Journal

http://www.linuxjournal.com/content/bash-arrays

Provide a list in open/close parentheses, unadorned.

array=(one two three four [5]=five)

echo "Array size: ${#array[*]}"

echo "Array items:"
for item in ${array[*]}
do
    printf "   %s\n" $item
done

Bash For Loop Examples

http://www.cyberciti.biz/faq/bash-for-loop/

The array here doesn't use parentheses.

for VARIABLE in 1 2 3 4 5 .. N
do
 command1
 command2
 commandN
done

bash - escaping newlines in sed replacement string - Stack Overflow

http://stackoverflow.com/questions/8991275/escaping-newlines-in-sed-replacement-string

Newlines will be recognized in the replace clause (the second half), but not in the match clause. Instead, you will need to use the N and D flags.

echo 'abc' | sed 's/b/\ 
> /'
a
c

Gather dependencies from pom files

http://www.grymoire.com/Unix/Sed.html#uh-51

Navigate to the root directory of Maven project with sub-modules, copy and paste the following code. The code depends on the correct order of a dependency declaration: groupId, articleId, version. Otherwise, all bets are off. This is intended to be quick and dirty; a SAX parser would be more robust.

The following temp files are used for each pom.xml in turn.

  • work.txt: contains ''
  • work2.txt: strips off '', leaving groupId:articleId:version

results.txt: The final file, providing a compilation of dependencies for all pom.xml files.

for file in $(find . -name "pom.xml")
do

cfile=$(printf '%q' $file)

sed '
# look for a <groupId>...</groupId>
/[^<]*<groupId>\([^<]*\)<\/groupId>.*$/ {
# Found one - now read in the next line
 N
# delete the <groupId>...</groupId> and replace with <maven-dependency>
 s/[^<]*<groupId>\([^<]*\)<\/groupId>[\n\r]*[^<]*/<maven-dependency>\1/
}

/<artifactId>\([^<]*\)<\/artifactId>.*$/ {
 N
 s/<artifactId>\([^<]*\)<\/artifactId>[\n\r]*[^<]*/:\1/
}

s/<version>\([^<]*\)<\/version>.*/:\1<\/maven-dependency>/

' $file > work.txt

echo -e "\n${cfile}\n===========================================" > work2.txt

cat work.txt | grep "<maven-dependency>[^<]*<\/maven-dependency>" | sed "s/<maven-dependency>\([^<]*\)<\/maven-dependency>/\1/" >> work2.txt; cat work2.txt

cat work2.txt >> results.txt

done

Along the same lines, there is also a little Ruby script for extracting out the articleId from the groupId:articleId:version:

#[Extract dependency name (Ruby)]

#E.g., 
dependencies = %w[
edu.ucmerced.ucpath.idm:ucm-ucpath-idm:0.0.2-SNAPSHOT
it.svario.xpathapi:xpathapi-jaxp:RELEASE
org.eclipse.m2e:lifecycle-mapping:1.0.0
org.apache.maven.plugins:maven-install-plugin:2.4
com.google.code.maven-replacer-plugin:replacer:1.5.2
org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2
org.apache.maven.plugins:maven-install-plugin:2.4
com.google.code.maven-replacer-plugin:replacer:1.5.2
]

puts dependencies.map{|x| first = x.index(/:/); x.slice(first+1, x.index(/:/, first+1) - first-1)}

How to reference a variable within sed? - The UNIX and Linux Forums

http://www.unix.com/shell-programming-scripting/39175-how-reference-variable-within-sed.html
tmp="abcdefg"
sed "s/${tmp}/good"

Replace the single quotes with double quotes. Single quotes prevent variable expansion.

http://www.unix.com/shell-programming-scripting/39175-how-reference-variable-within-sed.html

escape string in bash script so it can be used in command line

http://www.linuxquestions.org/questions/linux-software-2/escape-string-in-bash-script-so-it-can-be-used-in-command-line-360664/

Use double quotes.

You don't need to escape a string if you quote it - say you want to pass all the arguments to ls, instead of

ls $*

write

ls "$*"

Java; web services

Web Service Definition Language (WSDL)

http://www.w3.org/TR/wsdl#_soap:address

What purpose does soap:address serve? It appears to be the actual endpoint.

<definitions .... >
    <port .... >
        <binding .... >
           <soap:address location="uri"/> 
        </binding>
    </port>
</definitions>
? Apache CXF -- WS-SecurityPolicy http://cxf.apache.org/docs/ws-securitypolicy.html

Configuration for message-level encryption can be easily accomplished. The following server and client configurations should be loaded either in the Server/Client Java class or in the pom.xml configuration

Java Class

  SpringBusFactory bf = new SpringBusFactory();
  URL busFile = new ClassPathResource("wssec-server.xml").getURL();
  Bus bus = bf.createBus(busFile.toString());
  BusFactory.setDefaultBus(bus);

pom.xml

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>edu.ucmerced.ucpath.idm.Runner</mainClass>
                <arguments>
                    <argument>../ws-ora-idm-wsdl/src/main/resources/wsdl/IDMServices/IDMServices.wsdl</argument>
                </arguments>
                <systemProperties>
                  <systemProperty>
                    <key>cxf.config.file</key>
                    <value>cxf-client.xml</value>     
                  </systemProperty>
                </systemProperties>
            </configuration>
        </execution>
    </executions>
</plugin>

Server

    <jaxws:endpoint id="server"
      implementor="demo.wssec.server.GreeterImpl"
      endpointName="s:SoapPort"
      serviceName="s:SOAPService"
      address="http://localhost:9001/SoapContext/SoapPort"
      wsdlLocation="wsdl/hello_world.wsdl"
      xmlns:s="http://apache.org/hello_world_soap_http">
        
      <jaxws:properties>
         <entry key="ws-security.signature.properties" value="serviceKeystore.properties"/>
         <entry key="ws-security.signature.username" value="myservicekey"/>

         <entry key="ws-security.callback-handler" 
                value="demo.wssec.server.ServerCallbackHandler"/>

         <entry key="ws-security.encryption.properties" value="serviceKeystore.properties"/>
         <entry key="ws-security.encryption.username" value="myclientkey"/>
      </jaxws:properties> 
    </jaxws:endpoint>

Client

    <jaxws:client name="{http://apache.org/hello_world_soap_http}SoapPort" createdFromAPI="true">
       <jaxws:properties>
           <entry key="ws-security.signature.properties" value="clientKeystore.properties"/>
           <entry key="ws-security.signature.username" value="myclientkey"/>
           <entry key="ws-security.callback-handler" 
                  value="demo.wssec.client.ClientCallbackHandler"/>
           <entry key="ws-security.encryption.properties" value="clientKeystore.properties"/> 
           <entry key="ws-security.encryption.username" value="myservicekey"/>
       </jaxws:properties>
   </jaxws:client>

Propery files should include location of certificates and keystore password. The private key password cannot be provided here, but should use the callback to provide the correct private key.

Properties

org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=jks
org.apache.ws.security.crypto.merlin.keystore.password=sspass
org.apache.ws.security.crypto.merlin.keystore.alias=myservicekey
org.apache.ws.security.crypto.merlin.keystore.file=keys/servicestore.jks

Java callback-handler

public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof WSPasswordCallback) {
            WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
            if ("myservicekey".equals(pc.getIdentifier())) {
                pc.setPassword("skpass");
                break;
            }
        }
    }
}

See https://github.com/dcvezzani/mustached-batman for a complete example.

Maven Repository: org.apache.ws.security » wss4j

» 1.5.6 http://mvnrepository.com/artifact/org.apache.ws.security/wss4j/1.5.6

Pom.xml dependency entry.

<dependency>
 <groupId>org.apache.ws.security</groupId>
 <artifactId>wss4j</artifactId>
 <version>1.5.6</version>
</dependency>

WS-SecurityPolicy 1.2

http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/ws-securitypolicy-1.2-spec-os.html

Documentation of the nodes in the WS-SecuirtyPolicy namespace.

XML Namespace Document for WS-Security-Policy 1.3

http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802

Documentation of the nodes in the WS-SecuirtyPolicy namespace.

Re: Eclipse, CXF and WS-SecurityPolicy

http://mail-archives.apache.org/mod_mbox/cxf-users/201307.mbox/%3CF172D30F-6747-44B2-A4CE-7EFBD7710DEA@indivica.com%3E

An error like what follows indicates that possibly the wrong namespace is being specified or the namespace is missing altogether.

> Jul 27, 2013 12:41:56 AM org.apache.cxf.ws.policy.AssertionBuilderRegistryImpl handleNoRegisteredBuilder
> WARNING: No assertion builder for type {http://schemas.xmlsoap.org/ws/2005/07/securitypolicy}RequiredParts registered.

Maven Repository: org.apache.cxf » cxf-rt-ws-security

» 2.4.1 http://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-security/2.4.1

Entry description

<dependency>
 <groupId>org.apache.cxf</groupId>
 <artifactId>cxf-rt-ws-security</artifactId>
 <version>2.4.1</version>
</dependency>

Java web services: WS-Security with CXF

http://www.ibm.com/developerworks/library/j-jws13/

Walk-through for creating a web service, using WS-Security with the Apache CXF web services stack

X.509 Certificates

X.509 - Wikipedia, the free encyclopedia

http://en.wikipedia.org/wiki/X.509

Was researching what exactly the significance is of signed certificates.

As far as I can tell, most of the time it's only purpose is to validate a trusted public key.

The Most Common Java Keytool Keystore Commands

http://www.sslshopper.com/article-most-common-java-keytool-keystore-commands.html

A great resource common keytool commands.

Tuesday, September 24, 2013

weblog; a miraculous build

Maven

Developing Web services using Apache CXF and Maven

http://www.ctrl-alt-dev.nl/Articles/CXF-Maven/CXF-Maven.html

Step-by-step instructions on how to develop a web service using CXF and Maven. Starts off by creating a web service without CXF, adds tests and then replaces non-test code with code generated by CXF. Should be a worth while walkthrough.

Oracle

WadeLiu Blog On Oracle: query and kill oracle connection session

My tests failed initially due to the fact I had not added new trusted certificate entries into the existing client certificate. This happened repeatedly and must have used up all the connections in the database pool. They were eventually released, but I tried to see if I could hurry that cleanup along.

I had permissions to execute the first two lines, but not the last one.

SQL> SELECT s.sid, s.serial#, s.osuser, s.program FROM v$session s; 

SQL> select sid from v$session where audsid= userenv('SESSIONID'); 

SQL> ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE; 

weblog; hello, raspberry pi

Rails

ruby on rails - Routing to static html page in /public - Stack Overflow

http://stackoverflow.com/questions/5631145/routing-to-static-html-page-in-public

Tried to host wsdl and xsd resources. This seemed to be more trouble than it was worth, so I turned to Rack to get the job done. That being said, I decided it would be more consistent to simple grab resources relatively from the file system.

Rails Routing from the Outside In — Ruby on Rails Guides

http://guides.rubyonrails.org/routing.html

I was trying to figure out how to redirect to a static page. This didn't end up helping. Ended up using Rack instead.

get '/stories', to: redirect('/posts')

get '/stories/:name', to: redirect('/posts/%{name}')

get '/stories/:name', to: redirect {|params, req| "/posts/#{params[:name].pluralize}" }

get '/stories', to: redirect {|p, req| "/posts/#{req.subdomain}" }

Layouts and Rendering in Rails — Ruby on Rails Guides

http://guides.rubyonrails.org/layouts_and_rendering.html

Tried to render xml and xsd files. Again, I ended up using Rack instead.

render xml: @product

render file: filename, content_type: "application/rss"

How can I get a rails route to keep the extension as part of the id? - Stack Overflow

http://stackoverflow.com/questions/3409395/how-can-i-get-a-rails-route-to-keep-the-extension-as-part-of-the-id
  • http://stackoverflow.com/questions/948886/rails-restful-resources-using-to-param-for-a-field-that-contains-separator-chara
  • http://poocs.net/2007/11/14/special-characters-and-nested-routes

At first, I was having success with rendering the wsdl, but the xsd files have periods throughout the file name. I wanted to be able to ignore the periods, but Rails thought what followed the period was the file extension.

map.resources :websites, :requirements => { :id => /[a-zA-Z0-9\-\.]+/ } do |websites|
    websites.with_options :requirements => { :website_id => /[a-zA-Z0-9\-\.]+/ }  do |websites_requirements|
        websites_requirements.resources :dns_records
    end
end

map.resources :users,
  :requirements => { :id => %r([^/;,?]+) }

For Rails3 use :constraints instead of :requirements

http://stackoverflow.com/questions/3409395/how-can-i-get-a-rails-route-to-keep-the-extension-as-part-of-the-id

Ruby on Rack #1 - Hello Rack! - (m.onkey.org)

http://m.onkey.org/ruby-on-rack-1-hello-rack

Lovely, little lightweight web server. I ended up using this instead of Rails to host wsdl and xsd resources.

require 'rubygems'
require 'rack'

class HelloWorld
  def call(env)
    [200, {"Content-Type" => "text/html"}, "Hello Rack!"]
  end
end

Rack::Handler::Mongrel.run HelloWorld.new, :Port => 9292

Maven

jax ws - Use CXF JaxWsServerFactoryBean exception Cannot find any registered HttpDestinationFactory from the Bus - Stack Overflow

http://stackoverflow.com/questions/13617691/use-cxf-jaxwsserverfactorybean-exception-cannot-find-any-registered-httpdestinat
Caused by: java.io.IOException: Cannot find any registered HttpDestinationFactory from the Bus.
        at org.apache.cxf.transport.http.HTTPTransportFactory.getDestination(HTTPTransportFactory.java:295)
        at org.apache.cxf.binding.soap.SoapTransportFactory.getDestination(SoapTransportFactory.java:143)
        at org.apache.cxf.endpoint.ServerImpl.initDestination(ServerImpl.java:93)
        at org.apache.cxf.endpoint.ServerImpl.<init>(ServerImpl.java:72)
        at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:160)

If this error is logged, check and make sure the Jetty library is in the classpath.

 <dependencies>

    ...

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>2.7.6</version>
    </dependency>
 </dependencies>

SLF4J Error Codes

http://www.slf4j.org/codes.html#StaticLoggerBinder

Apparently, there are way too many logging libraries. Unfortunately, some libraries package in their own perferred logging library. This poses some challenges when there are conflicts. I had to disable a javadoc generator plugin because it bundled a logging library that was compatible with another logging library in a different JAR resource.

Do your best to exclude those libraries causing conflicts.

Check the dependency tree and add excludes.
mvn dependency:tree -Dincludes=velocity:velocity
Next, exclude inner dependencies. And then if you have to, remove libraries that bundle too much for their own good.
<dependencies>

  ..

  <dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-tools-api</artifactId>
    <version>2.5.1</version>

    <exclusions>
      <exclusion>
        <groupId>jetty</groupId>
        <artifactId>jetty</artifactId>
      </exclusion>
    </exclusions>

  </dependency>
</dependencies>

Maven Repository: ch.qos.logback » logback-classic

» 1.0.13 http://mvnrepository.com/artifact/ch.qos.logback/logback-classic/1.0.13

Dependency entry for logback logging library. There are so many logging libraries out there; this one seems to have a greater level of popularity.


 ch.qos.logback
 logback-classic
 1.0.13

Logback should be used in conjunction with SLF4J.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;

public class HelloWorld2 {

  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2");
    logger.debug("Hello world.");

    // print internal state
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    StatusPrinter.print(lc);
  }
}

Logback is intended as a successor to the popular log4j project. [It] is faster and has a smaller footprint than all existing logging systems...

http://mvnrepository.com/artifact/ch.qos.logback/logback-classic/1.0.13

Maven - Introduction to the Build Lifecycle

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Without understanding the Maven lifecycle, you will be navigating the waters of Maven blindly.

java - Different dependencies for different build profiles in maven - Stack Overflow

http://stackoverflow.com/questions/166895/different-dependencies-for-different-build-profiles-in-maven

Each profile block supports the tag

<profiles>
    <profile>
     <id>debug</id>
     …
     <dependencies>
      <dependency>…</dependency>
     </dependencies>
     …
    </profile>
    <profile>
     <id>release</id>
     …
     <dependencies>
      <dependency>…</dependency>
     </dependencies>
     …
    </profile>
</profiles>

Maven Repository: org.apache.cxf » cxf-rt-transports-http-jetty

» 2.7.6 http://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty/2.7.6

Dependency entry for the Jetty web server library.

<dependency>
 <groupId>org.apache.cxf</groupId>
 <artifactId>cxf-rt-transports-http-jetty</artifactId>
 <version>2.7.6</version>
</dependency>

Maven Dependency plugin - Filtering the dependency tree

http://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html
  • http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

When there are problems with dependencies, this is a vital tool to get some insight and resolve the problem. Sometimes those issues can be resolved by including an <excludes> block. Other times dependencies need to be moved around, either in a different order in a given pom.xml or moved into a different pom.xml altogether.

Maven Repository: org.apache.maven.plugins » maven-javadoc-plugin » 2.9.1

http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-javadoc-plugin/2.9.1
  • https://bitbucket.org/atlassian/maven-javadoc-plugin
  • http://stackoverflow.com/questions/tagged/maven-javadoc-plugin

Got to get my Javadocs up to par...

<dependency>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-javadoc-plugin</artifactId>
 <version>2.9.1</version>
</dependency>

Versions Maven Plugin - Checking for new plugin updates

http://mojo.codehaus.org/versions-maven-plugin/examples/display-plugin-updates.html

The display-plugin-updates goal will check all the plugins and reports used in your project and display a list of those plugins with newer versions available.

mvn versions:display-plugin-updates

CXF

Developing Web services using Apache CXF and Maven

http://www.ctrl-alt-dev.nl/Articles/CXF-Maven/CXF-Maven.html

Step-by-step instructions on how to develop a web service using CXF and Maven. Starts off by creating a web service without CXF, adds tests and then replaces non-test code with code generated by CXF. Should be a worth while walkthrough.

Java

How to get the filepath of a file in Java

http://www.mkyong.com/java/how-to-get-the-filepath-of-a-file-in-java/

Get the full path for a given file. Needed this for my Rails to static page html converter.

File file = File("C:\\abcfolder\\textfile.txt");
System.out.println("Path : " + file.getAbsolutePath());

Frequently Asked Questions about SLF4J

http://slf4j.org/faq.html

I need to get to know the SLF4J logging library a little better since it appears Log4j is ancient now.

Hibernate Annotations

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

Authoritative reference on Hibernate annotations. With this, we don't have to juggle hundreds of Hibernate configuration files. The Hibernate configuration is combined with the java code that defines the DAOs.

CSS text-decoration property

http://www.w3schools.com/cssref/pr_text_text-decoration.asp

Needed a reminder on how to implement strike-through

h2 {text-decoration:line-through}

UCPath

UCPath:System Design:Interface FDDs

https://sp2010.ucop.edu/sites/ucpath/System%20Design/Forms/AllItems.aspx?Paged=TRUE&p_SortBehavior=0&p_FileLeafRef=DS%20140%20Functional%20Design%20I%2d601%20CONEXIS%20DirectBill%20OB%2edocx&p_ID=148&RootFolder=%2fsites%2fucpath%2fSystem%20Design%2fInterface%20FDDs&PageFirstRow=91&TreeField=Folders&TreeValue=Interface%20FDDs&ProcessQStringToCAML=1&&View={B1F1C4C1-3FDB-400F-9D41-942503D1AF92}

Functional design documents.

Raspberry Pi

CoolGear® USB 3.0 to SATA and IDE Hard Drive Adapter Universal 2.5/3.5/5.25 Drives

open http://www.amazon.com/gp/product/B009VKSNS6/ref=oh_details_o04_s00_i00?ie=UTF8&psc=1 http://www.amazon.com/gp/product/B009VKSNS6/ref=oh_details_o04_s00_i00?ie=UTF8&psc=1
  • http://www.amazon.com/SATA-Drive-Adapter-Universal-Drives/dp/B004FEQ8MG

Device that can be used to rescue data from an external or internal hard drive due to an interface gone bad. This saved many, many of my wife's photos. They were not corrupt; the hard drive simply wouldn't mount due to a faulty interface.

Check out this video for instructions on how to use the device to rescue your files.

Apache CXF -- JAX-WS Configuration

http://cxf.apache.org/docs/jax-ws-configuration.html

Configuring an Endpoint. This Spring configuration handles setting up the webservice so we don't have to write the Java code. Thank you, Apache CXF.

<jaxws:endpoint id="classImpl"
    implementor="org.apache.cxf.jaxws.service.Hello"
    endpointName="e:HelloEndpointCustomized"
    serviceName="s:HelloServiceCustomized"
    address="http://localhost:8080/test"
    xmlns:e="http://service.jaxws.cxf.apache.org/endpoint"
    xmlns:s="http://service.jaxws.cxf.apache.org/service"/>

The Only Raspberry Pi XBMC Tutorial You Will Ever Need

http://mymediaexperience.com/raspberry-pi-xbmc-with-raspbmc/

If I get around to building a home network, this is how I will cut my teeth on a Raspberry Pi.

Raspberry Pi

NETGEAR G54/N150 WiFi USB Micro Adapter WNA1000M | Staples®

http://www.staples.com/NETGEAR-G54-N150-WiFi-USB-Micro-Adapter-WNA1000M/product_927666?cid=PS:GooglePLAs:927666&KPID=927666
  • http://www.superbiiz.com/detail.php?p=IMTW311M&c=fr&pid=36a601e944c3b844313893c45c178493ad8db092420bcb54ace3c24f8cdbcf68&gclid=CNvytr7M3LkCFeV7QgodPUwA1w
  • http://www.monoprice.com/products/product.asp?seq=1&format=2&p_id=6146&CAWELAID=1329452226&catargetid=320013720000011065&cadevice=c&cagpspn=pla&gclid=CPSX6K7M3LkCFUSCQgodvmEABw
  • http://www.amazon.com/USB-Wireless-Lan-Adapter-150Mbps/dp/B002XGDU9M

Use this (or something like unto it) to provide wifi for a Raspberry Pi.

Language

Google Translate

http://translate.google.com/#pt/en/celebrar

When I need a little help with my Spanish or Portuguese.

Google Translate

Thursday, September 12, 2013

weblog; saving my neck with git-reflog and namespace conflicts

Bash

bash - how to represent multiple conditions in shell script? - Stack Overflow

http://stackoverflow.com/questions/3826425/how-to-represent-multiple-conditions-in-shell-script
if [[ ( $g == 1 && $c == 123 ) || ( $g == 2 && $c == 456 ) ]]

Only process the file entries with 'webapp-idm' and 'webapp' in their name.

if [[ ( $cp =~ "webapp-idm" ) || ( $cp =~ "webapp" ) ]]
then
echo "processing $cp"
fi

bash - how to loop list of file names returned by find - Stack Overflow

http://stackoverflow.com/questions/9612090/how-to-loop-list-of-file-names-returned-by-find
for i in $(find -name \*.iso); do
    process "$i"
done

Loop for all .classpath files found recursively from the current directory.

for cp in $(find . -name ".classpath")
do
echo $cp
done

Other Comparison Operators

http://tldp.org/LDP/abs/html/comparison-ops.html
-eq

    is equal to

    if [ "$a" -eq "$b" ]

regex - use regular expression in if-condition in bash - Stack Overflow

http://stackoverflow.com/questions/2348379/use-regular-expression-in-if-condition-in-bash
$ for file in *; do [[ $file =~ "..g" ]] && echo $file ; done
abg
degree
..g

Java

XmlType (Java EE 5 SDK)

http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlType.html#name%28%29

When processing WSDLs and XSDs to generate Java code, node name clashes could be a problem.

Git

git ready » reflog, your safety net

http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html http://stackoverflow.com/questions/4786972/list-of-all-git-commits
git reflog

Pack up / clean up your repository (beware!)

# git reflog expire --expire=1.minute refs/heads/master
# git fsck --unreachable      
# git prune                   
# git gc                      

...you can use it as a safety net: you shouldn’t be worried that a merge, rebase, or some other action will destroy your work since you can find it again using this command.

http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html

Tuesday, September 10, 2013

weblog; bash, sed

Bash: creating scripts for cleaning up documentation files

bash script: get just filename from path

http://stackoverflow.com/questions/3362920/bash-script-get-just-filename-from-path
a=/tmp/file.txt
b=$(basename $a)
echo $b

<a href="file.txt" target="_new">file.txt</a>

Used this to extract the filename only from the path. I'm storing files that haven't been processed yet in 'unprocessed'.

#[command line]
find unprocessed -maxdepth 1 -type f -name "[^.]*" | xargs bin/convert_rails_html_to_dev_docs.sh


#[bin/convert_rails_html_to_dev_docs.sh]
for xpathfile in $@
do

xfile=$(basename $xpathfile)
<div class="notes">
  <p>.</p>
</div>

done

xargs: How To Control and Use Command Line Arguments; {} as the argument list marker

http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/
find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files

-0 -I {} is especially important; I still need to find out what it means though. -I {} indicates what the replacement symbol is.

escaping newlines in sed replacement string

http://stackoverflow.com/questions/8991275/escaping-newlines-in-sed-replacement-string
[jaypal:~/Temp] echo 'abc' | sed 's/b/\ 
> /'

At first, I tried to enter a ^M () at the point I desired a new line. After reading this SO I decided to try simply putting in a straight-out carriage return and it worked! Like the SO says, you still need to escape the carriage return.

sed 's_.* name="csrf-token" />$_<link href="assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />\
<script src="assets/application.js?body=1" type="text/javascript"></script>_' $xfile.tmp > work/$xfile

Bash For Loop Examples

http://www.cyberciti.biz/faq/bash-for-loop/
for VARIABLE in file1 file2 file3
do
 command1 on $VARIABLE
 command2
 commandN
done

Or to get all arguments that may have been provided as arguments to the script when it was run (e.g., when using xargs)

for VARIABLE in $@
do
 command1 on $VARIABLE
 command2
 commandN
done

How to read command line arguments in a bash script

http://how-to.wikia.com/wiki/How_to_read_command_line_arguments_in_a_bash_script
command: ./script.bash alpha beta gamma
Variables: $1=='alpha'; $2=='beta'; $3=='gamma' 

using a user defined bash function in xargs [duplicate]

http://stackoverflow.com/questions/11232782/using-a-user-defined-bash-function-in-xargs

I decided to create an actual bash script to funnel xargs to.

find unprocessed -maxdepth 1 -type f -name "[^.]*" | xargs bin/convert_rails_html_to_dev_docs.sh
find work -maxdepth 1 -type f -name "[^.]*" | xargs bin/install_work_files.sh

Recursively list non-Hidden Files

http://ubuntuforums.org/archive/index.php/t-931966.html
find /path -type d -name "[^.]*"

When you need to exclude file entries from your list. This leaves out any .*.swp files that might be hanging around.

find work -maxdepth 1 -type f -name "[^.]*" | xargs bin/install_work_files.sh

Here's another way using ls, although I wasn't able to get it working.

Bash: How list only the files?

http://stackoverflow.com/questions/10574794/bash-how-list-only-the-files
find . -maxdepth 1 -type f

Listing just the file entries (without any directories).

find work -maxdepth 1 -type f

Find all file entries under the work directory.

How do I tell if a file does not exist in bash?

http://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash
if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi

I used this to determine whether I needed to create a directory or not. My script exited when the directory already existed and I attempted to create it again.

if [ ! -e "unprocessed" ]
then
  mkdir unprocessed
fi

Advanced Bash-Scripting Guide: File test operators

http://tldp.org/LDP/abs/html/fto.html
-e       file exists

Of course, there are many other options listed in this resource besides this one. This is just the one I was looking for.

Passing parameters to a bash function

http://stackoverflow.com/questions/6212219/passing-parameters-to-a-bash-function
function_name () {
   command...
} 

This is how to create a bash function. I was going to use this, but finally gave up and decided to use a shell script instead.

Sed - An Introduction and Tutorial by Bruce Barnett: Using \1 to keep part of the pattern

http://www.grymoire.com/Unix/Sed.html#uh-0
sed 's/\([a-z]*\).*/\1/'

This looks quite comprehesive. As the author says, I fall into the category of those not really knowing how to use sed. I've started my journey :)

Anyhow, sed is a marvelous utility. Unfortunately, most people never learn its real power. The language is very simple, but the documentation is terrible.

http://www.grymoire.com/Unix/Sed.html#uh-0

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