Tuesday, August 23, 2016

Using Guard for null check in Swift





Coming straight to the point. Guard improves readability. An hour back I was struggling why my nil check is not working as it is supposed to work. 

I wrote it something like this in my function. 


func setXYX -> Void{

     if (a != nil) {
        return;
    }
   
    if ( b != nil) {
        return;
    }

    print (" a and b is not null");
}

But even if both a and b is null, the print function is getting executed. Then I looked around for ways to do null check in swift.

I modified the above function to be like this

func setXYX -> Void{

     guard a != nil  else {
        return;
    }
   
    guard  b != nil else {
        return;
    }

    print (" a and b is not null");
}

Thats as simple as it can be.

The other good thing about using guard is about the scope of the variable. After the guard, the scope of the variable is still valid. You can print value of a and b if they are not nil.


I am using the guard for the first time and I by mistake used the let keyword. The error, "Variable binding in a condition requires an initializer" is really misleading to me. But here it is so that someone else wont make the same mistake.

func setXYX -> Void{

     // Compile time error
     guard let a != nil  else {
        return;
    }

     // Compile time error    
    guard  let b != nil else {
        return;
    }

    print (" a and b is not null");
}









Friday, September 30, 2011

Android Https support

For providing https support in your android App.

Requirements :
JDK 1.6 (keytool), bcprov-jdk16-146.jar, open ssl.

Steps :

1. Generate the key store first.

2. Copy paste the following

 a. export CLASSPATH with the following.
    export CLASSPATH=$CLASSPATH:/home/name/Downloads/bcprov-jdk16-146.jar

b. Generate the keystore. mystore.bks will get generated at /home/uname/workspace/

keytool \
      -importcert \
      -v \
      -trustcacerts \
      -alias 0 \
      -file <(openssl x509 -in /home/uname/workspace/contentstore.net.crt) \
      -keystore /home/uname/workspace/mystore.bks \
      -storetype BKS \
      -provider org.bouncycastle.jce.provider.BouncyCastleProvider \
      -providerpath /home/uname/Downloads/bcprov-jdk16-146.jar \
      -storepass psswd
3. I will provide more detailed instructions for using the keystore in your android app, for now i will give summary of how it can be done.
   use the generated keystore file in your android app as a raw resource. Then override the createClientConnectionManager method of DefaultHttpClient that you were using.

 You can find how to override at various places in the net. Just Google Https support for Android.

4. If you are directly using the DefaultHttpClient, you are good to go. But if you  are using a restTemplate from Spring, then all that you need to do is:


RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(new MyHttpClient());



Apache Solr - Deployment in Tomcat

I am going to cover how to install solr search service on your local ubuntu dev box. I used the src code version instead of the binary version. Hope this will help you. It ran into couple of issues and it took time for me to deploy it on tomcat instead of using the jetty container that comes out of the box with solr.

Requirements :
Jdk verion 1.6, Tomcat, Ant

Download and install the package from the solr website
1. http://www.apache.org/dyn/closer.cgi/lucene/solr/
2. I downloaded 3.2.0 version of solr, you can try the latest. (apache-solr-3.2.0-src.tgz)
3. unzip the tar.
tar -xvf apache-solr-3.2.0-src.tgz
4. Now you should find the apache-solr-3.2.0 folder in the current directory.
5. Give command ant - this will compile and will test everything on your system. Most probably it will take at least 30 mins for the build to complete. Once the build is successful, navigate to solr directory within the apache-solr-3.2.0 folder.
6. First create the example configuration. To do so issue ant command
ant example.

- The reason we need this example configuration is that, we get solr/home directory for free.

7. Then issue "ant dist" command.
   - With this you should have a war archive in your dist directory. You can use this for deploying on your tomcat server.
8. Now you should find two folders being created if they already don't exist. 1. "example" directory and the other one is the "dist" directory.
9. Let us first create the solr home directory. You need to copy solr directory from the following path
Navigate to example directory
a. cd apache-solr-3.2.0/solr/example/
b. cp -r solr /opt/

  Please note that you just created a home directory for solr service. if you navigate to /opt/solr/, you will find conf, bin folders and solr.xml file.
conf directory is a required directory. It contains all important files like schema.xml, solrconfig.xml file and others.

10. Now create dist directory in your solr home directory.  you are going to put your war archive that you created in step 7 in this directory.
    mkdir dist
    cd  apache-solr-3.2.0/solr/dist/
    cp apache-solr-3.2-SNAPSHOT.war /opt/solr/dist/
11. At this point you have everything almost ready to go except that you need to tell tomcat where it can find the deployable war and solr home for that war to work.
a. navigate to your tomcat localhost directory at /opt/tomcat/conf/Catalina/localhost/
b. now create solr.xml file in this directory.
c.
<?xml version="1.0" encoding="utf-8"?>
<Context docBase="/opt/solr/dist/solr.war" debug="0" crossContext="true">
  <Environment name="solr/home" type="java.lang.String" value="/opt/solr" override="true"/>
</Context>
Note that this will get deleted when your war is undeployed.

d. Now restart your tomcat.
12. Open your browser and type localhost:8080/solr to pull the admin page of solr service.
13. Now it is time to upload some files. To do so, we will use our example configuration that came for free.
       a. Navigate to exampledocs directory.
            cd apache-solr-3.2.0/solr/example/exampledocs
       b. Upload one file to start with. To do so, first modify the post.sh in exampledocs directory to have the port properly configured.
          Edit the post.sh as following:
          URL=http://localhost:8080/solr/update
      c. sh post.sh monitor.xml
      d. Now you have uploaded and indexed some content to your solr search service.
14. To verify your data that was uploaded. You can either use the admin page at "http://localhost:8080/solr/admin/" or you can have  a browse page at "http://localhost:8080/solr/browse". Enter monitor, and you should see the results.
15. Few starting points for you to explore more.
     /opt/solr/conf/schema.xml is the one that governs how your search filtering is happening. It has fields and fieldType. Some of these fieldTypes have analyzers that you want to configure them in a different way for a result according to your specification. solrconfig.xml file is an another file that governs your search results. search for /browse among the searchhandlers. It is here everything starts.
16. To become more proficient start reading through solr wiki.