Saturday, August 24, 2013

Order matters

While getting used to java i sometimes forget about order importance. Actually in java there are also moments that declaration order is important. It is class initialization but now I wanted to write about perl. I have quite not small test written in my favorite scripting language. I really spent a while to find out why my subroutine stopped working after moving to separate module:
use 5.014;

package Foo;
my $foo = 1;
sub getFoo{
    say "Will get foo";
    say $foo;


package Bar;
our $bar = 2;
sub getBar {
    say "Will get bar";
    say $bar; 
Variable bar is undefined:
Will get foo
Will get bar

At first I thought it's scoping related but adding our didn't change anything. The only problem was order because subroutines and vars are available since beginning but package variable is initialized after perl parses every line above it. The are two solutions to it. First is moving package to separate file. Including it by use will initialize variable in right moment. Second one is safer and easier:
package Bar;
our $bar;
 $bar = 2;
sub getBar {
  say "Will get bar";
  say $bar; 

This secret subroutine called BEGIN is run before normal subs are called so package like that will result in singleton like behavior wherever we declare it.

Tuesday, August 20, 2013

Easy way to organize Gists into sets

Gist is just quite geeky pastebin but like everybody i like to have all things in one place. That was reason why I've created my first own homepage. The differ from regular repositories with only few things. The biggest one is that you don't have to push changes and merge branches. Also changelog is automatically filled so its perfect for fast snippets.

Here are my gists (UPDATE I moved it to regular github repository)

The thing I did: Gists sorts files in single gist and displays first one. The dot symbol is used for hidden files in Unix and almost guaranties that file with . will appear before any other uploaded files. To feel really eccentric I used Plain Old Documentation but anyone can use GitHub Markdown if wants to.

Anyway it is good question whether gist is good service at all because it not differs much from normal git repository without some features.. 

Thursday, August 8, 2013

Perl SOAP::Lite rpc:literal tomcat quick start

I've lost almost half a day on trying to connect tomcat web service. I didn't want to use java for that because I wanted to this as fast as possible [in coding time not in run time :)]. After debugging I found out that problem is namespace in XML so I replaced default_ns with ns. The only problem is warning (so I run with -X) about undefined value but my services are tested now.
  use Soap::Lite;
  my $soap = SOAP::Lite->new( service=> $wsdl, proxy => $url, uri => $url);
  my $som =  $soap->call('methodName', 
    die $som->faultstring if ($som->fault);
Another interesting thing is that SOAP::Lite stores all definitions between services creation so creating new $soap object with same WSDL will result with subroutine redefinition warning by eval injected code.

Tuesday, August 6, 2013

Function style

Have you ever wanted to make fast subroutinte to assign result to a variable?
my $b= {
 my $a=9;
 say "Inside";
say $b

Or If you want to use perl5
my $a = &{
    my $b=-10;

print "perl", $a, "\n"

Wednesday, July 31, 2013

start-stop-daemon output redirect

I was working on startup scripts that are pretending to become state of art :). I have even used inline perl to make it more compact. While working on it I found following problem:

echo  "Starting Witch"
   start-stop-daemon -v --start \
   --pidfile $PID_WITCH --make-pidfile --background  \
   --exec "$APP_DIR/Witch/Witch" -- $WITCH_OPT 2>$DIR/WitchErr.log
I've tried almost everything except writing separate bash script for my application. After some time, much time to be honest, I realized that problem is that output redirect is bash feature and application runs outside the shell. The solution was changing moment that sends app into background.

echo  "Starting Witch"
   start-stop-daemon -v --start \
   --pidfile $PID_WITCH --make-pidfile \
   --exec "$APP_DIR/Witch/Witch" -- $WITCH_OPT 2>$DIR/WitchErr.log
I heard that newer versions of start-stop-daemon will have --no-close feature that won't need hacks like this. But even Ubuntu still uses an old version.
If interested in embedding script inside bash:
perl <<'EOF'
     use feature 'say';
     say "Your script hoes here";

Thursday, May 16, 2013

Web service SSL fight

Web services can designed in secure way. There is an option to connect via encrypted connection and even provide authorization. Trouble starts when there is something wrong with your certificate. Especially before being production ready nobody cares them to be verifiable. I had install it on MS Windows. That's why snippets sometimes use Linux, sometimes Windows syntax.

First strike: Self-signed certificate

When certificate if self signed it definetely not reliable. Bu it is very common in testing code. I had to get it somehow to make it trusted.
openssl s_client -connect | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/certname.cert
Certificate must be added to keystore:

C:\jdk1.7.0\bin>keytool -import -trustcacerts -keystore cccc -storepass secretpassword -noprompt -file c:\tmp\certificate.certCertificate was added to keystore
To inform the java to set certificates during runtime:

System.setProperty("","C:\\jdk1.7.0\\bin\\cccc");  System.setProperty("", "secretpassword");

Second strike: Missing class?

Really I don't understand this error but I had to deal with this. Somebody found out that this class can be implemented with little help of  jsslUtuils which is fortunately LGPL.

PKIXSSLContextFactory sslContextFactory = new PKIXSSLContextFactory();
sslContextFactory.setTrustManagerWrapper(new X509TrustManagerWrapper() {
 public X509TrustManager wrapTrustManager(final X509TrustManager origManager) {
  return new X509TrustManager() {
 public X509Certificate[] getAcceptedIssuers() {
  return origManager.getAcceptedIssuers();
 public void checkServerTrusted(X509Certificate[] chain,
 String authType)
 throws CertificateException {
 try {
  origManager.checkServerTrusted(chain, authType);
  } catch (CertificateExpiredException e) {
 // TODO log or do something else to rethrow
 // the exception if chain[0] isn't the certificate
 // for which you want to make this special case.
public void checkClientTrusted(X509Certificate[] chain,
 String authType)
 throws CertificateException {
 origManager.checkClientTrusted(chain, authType);
SSLContext sslContext = sslContextFactory.buildSSLContext();

Final encounter: Wrong address

We need to disable another check to make it happen:
TLSClientParameters tlsParams = new TLSClientParameters();
Client client2 = ClientProxy.getClient(service);
HTTPConduit http = (HTTPConduit) client2.getConduit(); 
Yeah! We finally can consume the service.


If service serve unexpected elements You can disable validation:

Services sh= new Services(new URL(""));
ServicesPortType port = sh.getServicesHttpSoap11Endpoint();
java.util.Map requestContext =
                       (( port).getRequestContext();
requestContext.put("set-jaxb-validation-event-handler",  "false");

End of transmission

Saturday, February 16, 2013

Temporay Solution for WiFi problems with Realtek Card under Ubuntu

From time to time my network manager goes wild and is not able to connect to any WiFi network  I don't have to tell anyone how annoying is that when happens in the middle of work. Solution is so simple that I don't know why I didn't try it out earlier:

# lspci|grep RTL
07:00.0 Network controller: Realtek Semiconductor Co., Ltd. RTL8191SEvA Wireless LAN Controller (rev 10)
# rmmod rtlwifi
ERROR: Module rtlwifi is in use by rtl8192se
# rmmod rtl8192se
# rmmod rtlwifi
# modprobe rtlwifi
# modprobe rtl8192se

Simple reloading module makes wireless works normal again