Thursday, March 7, 2013

File not found issue with tomcat: URL Encoding

I have to admit, today was lot better than yesterday. My struggle started when I was asked to quickly ( I mean it, in 10 minutes or so..) deploy a POC application on amazon ec2 instance. It is a windows 2008 server, so installed tomcat as service under "Program Files" and installed java 7. Hoping my 5 pages application will fly in no time that's when I noticed an error stack on the tomcat console :(. Main root cause was:

"Caused by: java.io.FileNotFoundException: File 'C:\Program%20Files\apache-tomcat-7.0.29\webapps\xyz\WEB-INF\classes\createData.sql' does not exist"

I could see the file under WEB-INF\classes and that's when I realized that my URL is getting encoded, hence all spaces are getting replaced with %20.

I had below line of code which was causing the issues since there is no folder with name "Program%20Files" in the system.

String path = FileUtils.class.getClassLoader().getResource("createData.sql").getPath();
List readLines = FileUtils.readLines(new File(path));
I wrapped it using URI and BOOM problem went away.

String path = FileUtils.class.getClassLoader().getResource("createData.sql").getPath();
List readLines = FileUtils.readLines(new File(new URI(path).getPath()));

as usual I happy soul again.
Manisha                

Wednesday, February 6, 2013

Install applications without admin permission on windows machine


Off late I am living under processes and restriction world. Got confused correct? I am consulting at a place where I don't have any permission on my own laptop. I am not complaining about installing stuff but coping under certain directory and changing the environment varibales are restricted too.
Being a developer I live in code and wants full access to environment where I am living.  I found out that if you don't have Admin access to your machine you can still do things like administrator.
Create a "Temp" directory under c:\ drive. create a wintv directory under it and copy the cmd.exe underneath it. Boom now you can do anything on your machine. Copy any exe here and you can run them, install them. To change the environment variable just open the command prompt from c:\Temp\wintv directory and open Windows\System32\sysdm.cpl file. BOOM BOOM!!
You are unstoppable again :) Manisha

"." to open the home directory in windowns


Yesterday I was tweaking with my setting.xml which resides under ~\.m2\conf directory.

I was opening my explorer and then was making hundreds of click to reach there :O. A friend (Sachin) who was standing next to me showed me a nice shortcut to open home directory with one click.
Start -> Run -> Type just "." and hit enter.. Tadaaaa it will open your home directory. Thank you for showing Sachin.   I will remember, it now. Manisha

Maven: Could not calculate build plan: Failure to transfer

Last day a very simple thing took little while for us to resolve. I was trying to setup a new project on a machine and did not have all my environment variables setup properly.


I was trying both the command line and eclipse options interchangibly. Initially I had the wrong repository settins in settings.xml so it could not download any jars and I ended up having *.lastUpdated files all over.

Eclipse maven plugin seems to stop downloading the dependencies if it seems *.lastUpdated files. I had to remove the all the *.lastUpdated file and plugin starts to work agian.
I read same clean up can be done using mvn -U clean too.   happy reading Manisha

Thursday, August 2, 2012

Mysql's THE GROUP_CONCAT() function

I must have written multiple custom code in Java to aggregate my results from a table. i.e. Assume a table like below.

Table A
-----------
name
-----------
Manisha
Nilanjan
Nima

To display (Manisha, Nilanjan, Nima ) I always use to aggregate all the values, using some sort of loop. With mysql group_concat function it can be done very easily.

SELECT GROUP_CONCAT(name) As Name FROM A;

Then the output will be:
name
----
Manisha, Nilanjan, Nima

We can also use some format of GROUP_CONCAT(). Like

SELECT GROUP_CONCAT( Language SEPARATOR ‘-’)-> It will use ‘-’ instead of ‘,’
SELECT GROUP_CONCAT( Language ORDER BY Language DESC ) -> To change the order and shorting output

Another thing to remember that GROUP_CONCAT() ignores NULL values. Group_Concat function can be used in join scenarios too. i.e.

Employee_TYPE Table
--------------------
Id | Desc
1        |  1 | FT  
2        |  2 | PT  


Employee table
---------------
ID | Name
1  | Manisha
1  | Nilanjan
2  | Nima

And to display something like below group_concat can be used too.
ID   | Desc | Name
   1 |  FT  |  Manisha, Nilanjan
   2 |  PT  |  Nima 

SELECT Employee_Type.*, (
    SELECT GROUP_CONCAT(Name SEPARATOR ',')
    FROM Employee
    WHERE Employee_Type.id = Employee.person_id
) as employee
FROM Employee_Type
   
Cool isn't it. 
Manisha 

Tuesday, July 31, 2012

Snippet to generate lines for TestSuite

I wanted to generate the TestSuite for all my classes and was not ready to click on all 200 test classes to generate from eclipse So wrote a small testcase which printed the lines for me to add into my TestSuite. Here is the code snippet.



    @Test
    public void testShouldPrintAllTheNameOfTestClasses() throws Exception {
        File dir = new File("/Project/DIF/dif_parent");
        LinkedList files = (LinkedList)FileUtils.listFiles(
                  dir,
                  new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$"),
                  DirectoryFileFilter.DIRECTORY
                );
        for (File file: files) {
          System.out.println("suite.addTestSuite(" + file.getName().replace("java","class" ) + ");");
        }
    }



Thanks
Manisha

Wednesday, July 25, 2012

Attaching Source code with Maven

Initially I use to download the source jars and then use to attach them to project. Then started adding source jar as dependencies to pom for dev profile. Recently I found a maven command which does i it seamlessly for us.

I ran below commands to download and attach the source code of all the projects in Eclipse for me.

mvn dependency:sources

mvn eclipse:eclipse -DdownloadSources=true 

Boom! refresh all project in Eclipse and the source code must be attached to your project.                    
Enjoy looking into source
Manisha