Haven't posted anything for quite long. That is sad, but I've had very tense time at work with a couple of new projects. Working quite hard with Python and Reportlab right now and have Java/Groovy and Grails project as long-term task. Would say I am happy right now about this tough schedule which helps me to gain new knowledge ;)
Despite of my schedule I've found some time at home and have finished my personal web-page. I've decided to make a tiny portal about myself and consolidate all links and all information about myself in one place. Here it is: [konakov.info]. This blog is published on that site via atom feed.
Would like to add that Python and Django inspired me so much, that I've created my page using them. And this page is hosted on Google App Engine. Those technologies are great!
Hope to see ya soon again ;)
9/29/2011
4/25/2011
Bash exercises
At work we have some legacy, proprietary, in-house built HTML Template System for web-page generation. This solution looks reasonable and solves the tasks it was written for. But it causes one small problem to me, as to Developer/User of this System. It has no Source Version Control Tool "beyond". Just two servers. One for development/testing and the other for production. All source code is located just in folders on both Servers. And there are scripts to sync folders between Servers.
That evolves a bunch of problems (according to my restless mind):
1) Process demands to make backups before you upload changes on dev/test Server. You can change Template directly on Server, but anyway you need a backup.
2) You have to ask your colleagues if somebody works on some ticket for this System or not. Just to eliminate possible collisions.
3) It is quite boring and hard to remember all files you've changed locally. Just not to forget to upload them on Server.
I decided to write a couple of scripts using Bash, Scp and Git to make my life quite easy. The Idea is quite simple:
a) You provide to the script the ticket number, Server-Doc-Base and Template folder for download. The script creates local folder with name of the ticket number, copy all files from Template folder on Server to your local machine and commit everything in Git. Using aliases and functions in .bashrc you can define a bunch of "shortcuts" and eliminate the second parameter (Server-Doc-Base) for your scripts. This parameter is changed quite rarely and we can fix it.
b) The whole magic, actually, will be done by Git. After we change a few files and want to test our changes, we just ask Git about local changes, check those files on Server (they should not be changed while we were working on them) and upload them with final commit to local Git.
That is it. Git allows you to do many very interesting things, this is why I like it.
But actually I would like to mention a small issue I encountered during work with Bash. Unfortunately, this code:
COUNTER=0 git status | while read LINEdo ((COUNTER++)) echo $COUNTER done echo $COUNTERwill work not as you would expect it. The last counter value will be ZERO! I had spent quite long time before I understood what was going on and found this very helpful post.But anyway, Bash is a powerful tool, use it, like it, and it will help you to make your daily routine much easy.4/19/2011
Python + Lambda
I was in the middle of search some quick "How-To" question when encountered this SOF post.
Such small examples of FP as Lambda for "map" or "filter" collection functions I use almost every day. Used to work with them in Java (google-collection, apache collections) and definitely use it in Python. When I see some iteration using "for" or "while" for some collection only for filter it makes me nervous :)
Didn't think that for someone it may looks like "obscurity". Can't say I got FP so deep. Sometimes it is challenging for me to get idea from FP-style code, but such small things in daily use I find elegant and robust.
4/08/2011
Python + Django
I am sitting at work, playing with Django run through pdb (Python debugger).
Don't know why I like it so much ;)
Don't know why I like it so much ;)
4/07/2011
Google Desktop
I've discovered for myself the "Google Desktop". Not in terms of search, I haven't used this feature too much.
But in terms of gadgets on toolbox. Using "ToDo List", "Gmail" and "Google Documents". Looks convenient.
4/06/2011
Petclinic
A friend of mine asked me a few weeks ago about a good project to refresh his J2EE knowledge. I had a quick thought and decided that the "petclinic" demo-app from Spring Framework should be quite good.
A couple of weeks later I decided to have a look at this demo-app myself and was a little bit confused. I remember that this application was a part of Spring Framework distro - but not anymore. Now you can get it on your local computer using SVN repo which is quite tricky to find, first of all. The second disappointment was that this application is quite hard to deploy-and-run.
I have quite good exp. dealing with J2EE but it took some time to setup this app for quick deploy/run scheme in Eclipse. And finally I decided it may be useful for someone to have such "out of the box" application. I decided to try and use GitHub and publish the end result of my experiments there. At least my friend will be able to get it, import in Eclipse and run. It may ease the start of his refreshment in the J2EE world.
So, using this link "git@github.com:denkonakov/petclinic.git" you can pull "petclinic" demo-app on your computer, just import it in Eclipse, add this application to your Tomcat Server in Eclipse Server Plugin and start using it.
Enjoy!
7/15/2010
Oh, this Inner classes!
Hi! There was a long time since my last post. Actually, there is a lot of work with my current project now. And today I would like to tell you some interesting fact I encountered yesterday in our java source-code.
So, I were in the middle of my battle with our JUnit test (hope I'll find time to write about it) when saw this thing. Imagine that you have the following code (this is not a real piece of code, ye, ye - NDA, you know):
despite of the fact that this is not only 'Thread Safe' but also 'Lazy Init' Singleton, the strange thing is... Actually, what do you think, how many *.classes files there should be? Yes, you right, TWO:
if you are using the Eclipse compiler. Otherwise, if you use something else:
denlion@denlion-laptop:~/workspace/For Test/bin$ java -version
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1)
OpenJDK Server VM (build 14.0-b16, mixed mode)
denlion@denlion-laptop:~/workspace/For Test/bin$
you'll see something different:
denlion@denlion-laptop:~/workspace/For Test/src$ javac BrokenSingleton.java
denlion@denlion-laptop:~/workspace/For Test/src$ ls -al
total 24
drwxr-xr-x 2 denlion denlion 4096 2010-07-15 23:22 .
drwxr-xr-x 5 denlion denlion 4096 2010-06-29 22:06 ..
-rw-r--r-- 1 denlion denlion 196 2010-07-15 23:22 BrokenSingleton$1.class
-rw-r--r-- 1 denlion denlion 518 2010-07-15 23:22 BrokenSingleton.class
-rw-r--r-- 1 denlion denlion 467 2010-07-15 23:22 BrokenSingleton$InstanceHolder.class
-rw-r--r-- 1 denlion denlion 331 2010-07-15 22:57 BrokenSingleton.java
denlion@denlion-laptop:~/workspace/For Test/src$
Yes! THREE files. I saw the same. Checked twice, checked my eyes, and again - THREE! But what is this BrokenSingleton$1.class? So, together with my colleagues, we armed with 'javap' and started our investigation:
denlion@denlion-laptop:~/workspace/For Test/src$ javap BrokenSingleton\$1
Compiled from "BrokenSingleton.java"
class BrokenSingleton$1 extends java.lang.Object{
}
What is this? What is going on? What is this empty class for? So, decompilation of the rest lets us see that there is a very interesting thing inside the BrokenSingleton.class:
denlion@denlion-laptop:~/workspace/For Test/src$ javap BrokenSingleton
Compiled from "BrokenSingleton.java"
public class BrokenSingleton extends java.lang.Object{
public static BrokenSingleton getInstance();
BrokenSingleton(BrokenSingleton$1);
}
But there is NO such construnctor inside BrokenSingleton, right? I think we are wrong :) With the strong suspicion let's write a small test using java reflection api:
And output for this test is:
denlion@denlion-laptop:~/workspace/For Test/src$ java BrokenSingletonTest
true
false
BrokenSingleton(BrokenSingleton$1)
denlion@denlion-laptop:~/workspace/For Test/src$
That's it! We have broken the Singleton and made a small hole in the Java security :) So, as you must have noticed, the Open Jdk compiler has not allowed me to write 'newInstance((BrokenSingleton$1) null)', showing error that it cannot find the symbol (BrokenSingleton$1). But as we can see, there IS the constructor with 'BrokenSingleton$1' and package default access modificator. And by using it, you can easily create as many instances of your 'Single'-pattern as you wish. Honestly, we have made a couple of experiments (Eclipse compiler, Sun Hot Spot v1.6 under Windows, JRockit) they all use the similar technique with slight differences. But Sun Hot Spot allowed us to compile code with 'BrokenSingleton$1' cast on Windows at work.
Here we invented the theory, that Inner classes (actually JVM does not have instructions for inner classes, this is only a syntactic sugar as far as I understand) actually DO NOT have (because this is not a JVM construction) access to the private methods of outher class. And for screwing this, compiler generates such code. This unique constructor with 'BrokenSingleton$1' parameter and with package access allows it to create an instance of our Singleton.
You may think: "How about the private methods of outher class?" And you will be absolutely right - there is almost the same technique (generation of additional method with name like 'access$100' and package default access). You can easily check it yourself :)
So, be aware, my friend! If you are using inner classes, you can not be sure anymore that your private method is not accessible outside the class.
Good luck!
PS: Yes, elimination of inner class solves this problem perfectly :)
So, I were in the middle of my battle with our JUnit test (hope I'll find time to write about it) when saw this thing. Imagine that you have the following code (this is not a real piece of code, ye, ye - NDA, you know):
public class BrokenSingleton {
// ---- Thread Safe Singleton pattern -----
static class InstanceHolder {
static final BrokenSingleton instance = new BrokenSingleton();
}
public static BrokenSingleton getInstance() {
return InstanceHolder.instance;
}
private BrokenSingleton() {
// Do your initialization here
}
}
despite of the fact that this is not only 'Thread Safe' but also 'Lazy Init' Singleton, the strange thing is... Actually, what do you think, how many *.classes files there should be? Yes, you right, TWO:
denlion@denlion-laptop:~/workspace/For Test/bin$ ls -al
total 16
drwxr-xr-x 2 denlion denlion 4096 2010-07-15 22:58 .
drwxr-xr-x 5 denlion denlion 4096 2010-06-29 22:06 ..
-rw-r--r-- 1 denlion denlion 544 2010-07-15 22:57 BrokenSingleton.class
-rw-r--r-- 1 denlion denlion 524 2010-07-15 22:57 BrokenSingleton$InstanceHolder.class
denlion@denlion-laptop:~/workspace/For Test/bin$
if you are using the Eclipse compiler. Otherwise, if you use something else:
denlion@denlion-laptop:~/workspace/For Test/bin$ java -version
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1)
OpenJDK Server VM (build 14.0-b16, mixed mode)
denlion@denlion-laptop:~/workspace/For Test/bin$
you'll see something different:
denlion@denlion-laptop:~/workspace/For Test/src$ javac BrokenSingleton.java
denlion@denlion-laptop:~/workspace/For Test/src$ ls -al
total 24
drwxr-xr-x 2 denlion denlion 4096 2010-07-15 23:22 .
drwxr-xr-x 5 denlion denlion 4096 2010-06-29 22:06 ..
-rw-r--r-- 1 denlion denlion 196 2010-07-15 23:22 BrokenSingleton$1.class
-rw-r--r-- 1 denlion denlion 518 2010-07-15 23:22 BrokenSingleton.class
-rw-r--r-- 1 denlion denlion 467 2010-07-15 23:22 BrokenSingleton$InstanceHolder.class
-rw-r--r-- 1 denlion denlion 331 2010-07-15 22:57 BrokenSingleton.java
denlion@denlion-laptop:~/workspace/For Test/src$
Yes! THREE files. I saw the same. Checked twice, checked my eyes, and again - THREE! But what is this BrokenSingleton$1.class? So, together with my colleagues, we armed with 'javap' and started our investigation:
denlion@denlion-laptop:~/workspace/For Test/src$ javap BrokenSingleton\$1
Compiled from "BrokenSingleton.java"
class BrokenSingleton$1 extends java.lang.Object{
}
What is this? What is going on? What is this empty class for? So, decompilation of the rest lets us see that there is a very interesting thing inside the BrokenSingleton.class:
denlion@denlion-laptop:~/workspace/For Test/src$ javap BrokenSingleton
Compiled from "BrokenSingleton.java"
public class BrokenSingleton extends java.lang.Object{
public static BrokenSingleton getInstance();
BrokenSingleton(BrokenSingleton$1);
}
But there is NO such construnctor inside BrokenSingleton, right? I think we are wrong :) With the strong suspicion let's write a small test using java reflection api:
public class BrokenSingletonTest {
/**
* @param args
*/
public static void main(String[] args) {
Constructor[] constr = BrokenSingleton.getInstance().getClass().getDeclaredConstructors();
try {
System.out.println(BrokenSingleton.getInstance() == BrokenSingleton.getInstance());
System.out.println(constr[1].newInstance((BrokenSingleton) null) == BrokenSingleton.getInstance());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(constr[1]);
}
}
And output for this test is:
denlion@denlion-laptop:~/workspace/For Test/src$ java BrokenSingletonTest
true
false
BrokenSingleton(BrokenSingleton$1)
denlion@denlion-laptop:~/workspace/For Test/src$
That's it! We have broken the Singleton and made a small hole in the Java security :) So, as you must have noticed, the Open Jdk compiler has not allowed me to write 'newInstance((BrokenSingleton$1) null)', showing error that it cannot find the symbol (BrokenSingleton$1). But as we can see, there IS the constructor with 'BrokenSingleton$1' and package default access modificator. And by using it, you can easily create as many instances of your 'Single'-pattern as you wish. Honestly, we have made a couple of experiments (Eclipse compiler, Sun Hot Spot v1.6 under Windows, JRockit) they all use the similar technique with slight differences. But Sun Hot Spot allowed us to compile code with 'BrokenSingleton$1' cast on Windows at work.
Here we invented the theory, that Inner classes (actually JVM does not have instructions for inner classes, this is only a syntactic sugar as far as I understand) actually DO NOT have (because this is not a JVM construction) access to the private methods of outher class. And for screwing this, compiler generates such code. This unique constructor with 'BrokenSingleton$1' parameter and with package access allows it to create an instance of our Singleton.
You may think: "How about the private methods of outher class?" And you will be absolutely right - there is almost the same technique (generation of additional method with name like 'access$100' and package default access). You can easily check it yourself :)
So, be aware, my friend! If you are using inner classes, you can not be sure anymore that your private method is not accessible outside the class.
Good luck!
PS: Yes, elimination of inner class solves this problem perfectly :)
Subscribe to:
Posts (Atom)