Tuesday, August 1, 2017

Java Lambdas Tips

Creating a lookup method for an Enum



Without lambdas:

public enum Type {
     
     INFO,
     WARNING,
     ERROR;

     final static Map<String, Type> lookup = new HashMap<String, Type>();
     
     static {
      for(Type type : Type.values()) {
       lookup.put(type.name().toLowerCase(), type);
      }
     }

       
     public static Type of(String value){
         Type val = lookup.get(value == null ? null : value.toLowerCase());
         if(val == null){
             val = ERROR;
         }
         return val;
     }
}

With lambda:


public enum Type {
     INFO,
     WARNING,
     ERROR;

     final static Map<String, Type> lookup = Arrays.stream(Type.values())
          .collect(Collectors.toMap(t -> t.name().toLowerCase(), Function.identity()));

     public static Type of(String value){
         Type val = lookup.get(value == null ? null : value.toLowerCase());
         if(val == null){
            val = ERROR;
         }
         return val;
     }
}

Wednesday, January 11, 2017

Switching DNS in Ubuntu Linux

One approach to switch among different development environments (QA, Integration, Stage, etc) is to have different DNS configs for routing one domain.

In Ubuntu you can setup different network connections with different DNS servers and be able to switch from network top menu,

1. Simply add a new network connection (may need two, one for Ethernet and one for WiFi)


2. Configure DNS settings


3. Switch to new connection from top menu


4. Test with nslookup command

Friday, March 11, 2016

Curl tips

Get response headers sending headers and cookies

curl --header "BB-App:androidNativeStore, 1.0.1" --cookie "lc=US|en" -I http://bitwise-api2.dev:8080/store-api/preferences/store-flag?store=us

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
BB-Host: bitwise-api2.dev
Location: https://store.bbcomcdn.com/deploy/images/common/flags/flag_us.jpg
Content-Length: 0
Date: Fri, 11 Mar 2016 16:41:41 GMT

Sunday, October 18, 2015

Oracle Troubleshooting: ORA-01114, ORA-01110, ORA-27091, ORA-27041

During troubleshooting on an issue in Oracle DB I was getting these errors:

sqlplus / as sysdba

SQL> startup
ORACLE instance started.

Total System Global Area 1068937216 bytes
Fixed Size      2233344 bytes
Variable Size    809503744 bytes
Database Buffers   251658240 bytes
Redo Buffers      5541888 bytes
Database mounted.
ORA-01114: IO error writing block to file 5 (block # 1)
ORA-01110: data file 5: '/data/oracle/bb/bb1.dbf'
ORA-27091: unable to queue I/O
ORA-27041: unable to open file
Linux-x86_64 Error: 13: Permission denied
Additional information: 3

I changed the permissions to data file and problem solved:

sudo chmod 660 /data/oracle/bb/bb1.dbf

sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Fri Oct 16 17:59:36 2015

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> startup
ORACLE instance started.

Total System Global Area 1068937216 bytes
Fixed Size      2233344 bytes
Variable Size    809503744 bytes
Database Buffers   251658240 bytes
Redo Buffers      5541888 bytes
Database mounted.
Database opened.

Friday, September 18, 2015

ATG QueryBuilder: How to create nested queries / subqueries

I had to deal a good amount of time finding a way to query repositoy items that would match some relationships among different item descriptors. I was trying to look for a way to perform a join over different item descriptors, but at the end was able to solved it in a nested queries fashion. You know,

the typical thing in SQL where you filter by matching a column from the result of another query:

SELECT column_name [, column_name ]
FROM   table1 [, table2 ]
WHERE  column_name OPERATOR
      (SELECT column_name [, column_name ]
      FROM table1 [, table2 ]
      [WHERE])

This in ATG can be done using a query builder operation called: createIncludesItemQuery. You can pass as parameter another query and the colum matching expression.


// Get guide items where tabs in the guide matches some other tabs query
RepositoryItemDescriptor guideRepositoryItemDescriptor = 
                        getProductRepository().getItemDescriptor("guide");
RepositoryView guidesRepositoryView = guideRepositoryItemDescriptor.getRepositoryView();               

QueryBuilder guidesQueryBuilder = guidesRepositoryView.getQueryBuilder();
QueryExpression tabsPropertyExpression =     
        guidesQueryBuilder.createPropertyQueryExpression("tabs");                    
// productTabsQuery is another query built
Query productGuidesQuery = 
      guidesQueryBuilder.createIncludesItemQuery(tabsPropertyExpression, productTabsQuery);
RepositoryItem[] guides = guidesRepositoryView.executeQuery(productGuidesQuery);