Archive for category Uncategorized

About CSS2 Selectors

Some days ago I was trying to customize my blog’s look and feel to remove my pic from each one of my entries.
At my blog’s theme, every <img> tag (which contains my pic for each entry) is the first child of a <td> tag.
I discovered a very handy CSS2 selector syntax to accomplish this task:

E:first-child: Matches element E when E is the firstchild of its parent (The :first-child pseudo-class)

So, the custom CSS ended up being td > img:first-child {display: none;}, which removes my user pic from every post, but keep the one on the left sidebar.

Just now you are seeing only one pic of mine 🙂

Tags: , , ,

DomainDrivenDesign: Domain Services or Method on an Entity?

There has been too much discussion on the DDD list regarding where to put the business logic control, whether in a service or entity. Being more specifically, in order to ship an order, the followthings should happen:

  1. Validate that the order can be shipped
  2. Update quantity
  3. Set the status to shipped
  4. Save the order
  5. Send an email to the customer that the order has been shipped

So, nickgieschen suggested the following C# implementations:

1. Everything in the domain:

 1:class Order
 2:{
 3:    IOrderShippedNotificationPolicy _notificationPolicy
 4:    IOrderRepository _orderRepository
 5:
 6:    void Ship()
 7:    {
 8:        if (!CheckIfOkayToShip()) {
 9:            throw new InvalidObjectException();
10:        }
11:        UpdateQuantity();
12:        _orderRepository.Add(this);
13:        _notificationPolicy.Notify(this);
14:    }
15:}
16:
17:class OrderShippedNotifyByEmailPolicy : INotificationPolicy
18:{
19:    // The object that gets injected is implemented
20:    // in the infrastructure layer
21:    IEmailGateway _emailGateway
22:    
23:    void Send(Order this)
24:    {
25:        // Create email here
26:        _emailGateway.Send(email);
27:    }
28:}

2. Or have an application service coordinate:

   1:class OrderService
   2:{
   3:    // _orderRepository and _orderShippedNotificationPolicy 
   4:    //  are injected dependencies
   5:
   6:    void ShipOrder(Order order)
   7:    {
   8:        order.Ship(); // in this case it only validates and updates quantity
   9:        _orderRepository.Save(order);
  10:        _orderShippedNotificationPolicy.Notify(order);
  11:    }
  12:}

There’s been a lot of replies also. I highlighted the interesting ones:

“The advantage of the latter scenario is that you’re calling _orderRepository. Save in the application layer, which I prefer since it’s easier to see the transactional control. The problem with the latter scenario is that it seems it’s putting things in the application layer which don’t need to be there. The action to Ship() seems to me an atomic, domain centered action and should therefore sit in the domain. I consider the application layer to be like a thin domain facade as defined by Fowler. That is, it is only there to direct/coordinate domain activities. Like I said, Ship() seems like it should be considered one activity, and therefore coordination from a service layer shouldn’t be necessary.”

“The way I look at it – what needs to go into Ship() is the stuff that _must_ happen before shipping can happen. And shipping can happen without the notification part. You only have a rule that says “send a notification to the customer upon shipping the order”. You don’t have a rule that says “make sure the customer gets the notification or there are no shipments”.

“So, perhaps as a rough, preliminary rule we can say anything which affects the state of the domain should go be placed in the domain. (Of course, the application layer can affect the state of the domain, but only by using domain items to do so maybe think of it as the Law of Demeter among layers.) The email doesn’t have any meaning within the domain – it’s simply a reflection of the domain.

“Notificitation of an order and the order itself is two separate concepts.”

“This could as easily be implemented using AOP.”

I think DDD advocates are a little bit extremists with some concep
ts, like repository. I wouldn’t have designed it on the domain layer, because I want transactional control on the application service layer. I think the domain has to deal with its particularities, not with sending email or adding things to a repository, even being decoupled of theirs implementations (the domain has a reference only to interfaces). So I prefer the latter approach. And you? What are yout thoughts about this design? How would u have designed it? Everything on the domain or have an application service coordinating the activities?

Tags: , , , ,

Generating LGPL notices with Python support

When we started developing JFileContentManager, we didn’t even think to release it as an open-source project. So, when we finished the project, I particularly thought about this idea, because I was very interested in participating on an community involving project. So, we decided to release it under the LGPL licence, because we wanted the project audience to be wider as possible. But, before turning it public, we needed to add the LGPL license term to every Java file of the project.
Opening and editing each Java class, one by one, would be a tedious task, taking a lot of time and adding up to the fact I’m very interested on learning dynamic languages (like Python, Ruby), I developed a Python script to automate this task for us. Our Python code is intended to open each Java file, adding the license term on the start of the file as a Java comment and writing it to the disk. The script is as follows:

 1:# Python script to add the LGPL notices to each java file of the FileContentManager project.
 2:import os, glob, sys
 3:License = """\
 4:/**
 5:*FileContentManager is a Java based file manager desktop application, 
 6:*it can show, edit and manipulate the content of the files archived inside a zip.
 7:*
 8:*Copyright (C) 2008 
 9:*
10:*Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal [http://rnaufal.livejournal.com] 
11:and Rodrigo [[email protected]]
12:*
13:*FileContentManager is free software; you can redistribute it and/or
14:*modify it under the terms of the GNU Lesser General Public
15:*License as published by the Free Software Foundation; either
16:*version 2.1 of the License, or (at your option) any later version.
17:*
18:*This library is distributed in the hope that it will be useful,
19:*but WITHOUT ANY WARRANTY; without even the implied warranty of
20:*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21:*Lesser General Public License for more details.
22:*
23:*You should have received a copy of the GNU Lesser General Public
24:*License along with FileContentManager; if not, write to the Free Software
25:*Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """
26:
27:size = len(sys.argv)
28:if size == 1 or size > 2:
29:    print "Usage: AddLicense.py $1"
30:    sys.exit(1)
31:inputPath = sys.argv[1]
32:if not os.path.exists(inputPath):
33:    print inputPath, "does not exist on disk"
34:    sys.exit(1)
35:if not os.path.isdir(inputPath):
36:    print inputPath, "isn't a dir"
37:    sys.exit(1)   
38:for path, dirs, files in os.walk(inputPath):
39:    fileWithLicense = ''
40:    for filepath in [ os.path.join(path, f)
41:            for f in files if f.endswith(".java")]:
42:        content = file(filepath).read()
43:        f = file(filepath, "w")
44:        print >>f, License + "\n" + content
45:        f.close()
46:        
47:    

License is a Python multiline string, denoted by the triple-quotes. The main action begins with the os.walk() expression that walks every file in the directory, entered by the user as a comannd line argument, like this:

python AddLicense.py ${path}

where ${path} defines user project root directory. The list comprehension syntax produces the full path of each of the Java files. When the match is found, the file is opened and its content is assigned to the variable content. So, the License string is added to the start of the file and the file is then written to the disk. The print statements use a redirection syntax, for example: print >>f, License + "\n" + content. The ‘>>f’ sends the results to f rather than the console. So, this Python script helped our team to release the project early and at the same time automated the task of adding the LGPL notices. I think Python is very useful for these kind of tasks. And you?

Tags: , , , ,

Annoucing JFileContentManager 1.2!

A new version of JFileContentManager has been released! Below are the changes release:

  • The tree UI component was corrected to reflect the zip hierarchy content;
  • Added support to show the contents of:
    • Java files
    • Properties files
    • Shell script files
    • XHTML / HTML files
    • XML files
  • The About Dialog has been integrated with JDIC;

Check this out!

Tags: , , ,

Effective Java Programming with Joshua Bloch

Nice video from Joshua Block, Chief Java Architect at Google, talking about his Effective Javaâ„¢ Programming Language Guide book and also about the consequences of adding new features on the Java language, like the closures’ proposals. He also offers an advice to the programmers, when designing API’s, classes or methods:

“When in doubt, leave it out”.

Tags: , , , ,

Is Java on a evolutionary dead end way?

Artima is running an article where Bruce Eckel talks about about Java’s objective on backwards compatibilities and the problem of combinatorial complexity when you combine a new feature in every possible way with the other language features already present.

It’s the combinatorial complexity that you get when you combine a new feature in every possible way with the other language features. The combinatorial complexity can produce horrifying surprises, typically after the feature is added when it’s too late to do anything about it.

It’s clear Eckel is mentioning the Java closures’ proposals to become part of the language on the Java 7 and the major improvements occured when Java 5 appeared.

If Java is unwilling to break backwards compatibility, then it is inevitable that it will acquire both unproductive complexity and incomplete implementation of new features. I’ve made the case in Thinking in Java 4e that Java Generics are only a pale imitation of real generics, and one of the more appealing suggestions for closures is an incomplete implementation of true closures, but it would actually be preferable to a complete implementation because it produces clearer, more straightforward code.

I particularly agree with him, when he also says

Fundamental new features should be expressed in new languages, carefully designed as part of the ecosystem of a language, rather thanbeing inserted as an afterthought.

He also considers Scala as an alternative for the Java language. Eckel also declares

Java values backward compatibility over the clarity of its abstractions.

I don’t agree with him at this later point, the Collections’ framework is an example of good API design rules with a lot of well designed abstractions by Joshua Block, separating things that change from things that stay the same.

And you? What do u think about it? Should Scala be considered as an exit for Java?

Tags: , , , ,

JFileContentManager has been released!

JFileContentManager is a Java based file manager desktop application, it can show, edit and manipulate the content of the files archived inside a zip.
Showing a simple and friendly GUI, the user can see the content archived inside a zip file in a tree view way, select one file per time and see their content (either text or image). It’s also possible to reopen the last opened zip files through the history option on the file menu.

Below are the current features:

  • See the zip files’ list in a tree view way;
  • Select one by one of the files and see their content (either text or image);
  • Open the last four opened zip files through the history option on the file menu and
  • Download the bundled executable Jar.

and future goals:

  • Support other file formats, like .tar, .tgz, .tar.gz, .gzip, .rar, .7z.
  • Edit the content of a text file;
  • Save the edition etiher inside the opened zip or at another location on the disk;
  • Select one or more files and zip them together;
  • Edit the image’s file content with the help of a picture toolbar and
  • Bundle the project in Java Web Start.

The project is released under the LGPL license. Misfit, rnaufal and roddy will be very proud and happy if you take a look at this project, running the application, downloading the source code on SVN and commenting about it. Thanks and enjoy!

Tags: , , ,

Strongly Java typed safe delegates

This great post explores an interesting Java implementation about using the delegate programming feature, in a type safe way. Delegate is a form of function pointer, commonly used to implement callbacks. It specifies a method to call, which is dispatched in runtime. From Wikipedia, we have:

The short definition is that delegation defines method dispatching the way it is defined for virtual methods in inheritance: It is always the most specific method which is chosen during method-lookup – Hence it is the original receiver entity which is the start of method lookup even though it has passed on control to some other object(through a delegation link, not an object reference). Delegation has the advantage that it can take place at run-time and affect only a subset of entities of some type and can even be removed at run-time. Inheritance on the other hand typically targets the type rather than the instances and is restricted to compile time.

We almost know weakly typed Java delegate implementations, relying upon Strings to delegate to named methods. This way, it lets you mistype the method name easily and it also does not not allow method completion in the IDE. Alex shows us a type-safe Java delegate implementation, which enhances readibility and supports code completion on the IDE, using the cglib dynamic proxy facility. The article is very worth reading. As a developer on a daily basis I’ve never seen a concrete use of this programming feature, despite being very interesting. Have you ever used the delegate feature?

Tags: , , ,

TestSuite Merlin javadoc

Running through the Java TestSuite API I’ve found this funny javadoc..

Tags: , ,

Swing tips and tricks

Some days go I was given the task to customize the Java Swing widgets for a project in our company. The default look and feel of a JDialog, specifically, has the Java logo trademark icon on the upper corner left, the close button on the right one and the title panel is painted according to the default MetalLookAndFeel probably. As an example, we have something like this:

We wanna have an option to change the title’s panel color, to remove the close button and not to have the Java trademark logo showing on the panel. The final JDialog should look like this:

I’ve thought it was a nearly difficult task, because I had to override a default look and feel and some hidden properties, sometimes difficult to find. Shame on me. Searching through some forums, I’ve found some interesting tips and tricks about how to customize a the entire dialogs of a Swing application, even without creating a look and feel or overriding an old one.

For your knowledge, here are some overridden properties to make the JDialog seems like the latter above. These properties let you define new colors for the active caption of your widgets, other font colors, custom family types and much more (for other properties see through the documentation API or the forums below).

UIManager.put(“activeCaption”, Color.BLUE);
UIManager.put(“activeCaptionText”, Color.WHITE);
UIManager.put(“InternalFrame.titleFont”, new Font(“Arial-12-i-2”, Font.BOLD, 11));
Border cuverdBorder = new LineBorder(new Color(210, 210, 210), 2, true);
dialog.getRootPane().setBorder(cuverdBorder);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);
dialog.setUndecorated(true);

As a little example, below is the Java code to remove the close button from the panel’s title. This little code snippet below traverses the component hierarchy, passed as a parameter, in a recursive method.

   1:public static void removeCloseButton(Component comp)
   2:{
   3:    if (comp instanceof AbstractButton)
   4:    {
   5:        Action action = ((AbstractButton) comp).getAction();
   6:        String cmd = (action == null) ? "" : action.toString();
   7:        if (cmd.contains("CloseAction"))
   8:        {
   9:            comp.getParent().remove(comp);
  10:        }
  11:    } else if (comp instanceof Container)
  12:    {
  13:        Component[] children = ((Container) comp).getComponents();
  14:        for (int i = 0; i < children.length; ++i)
  15:        {
  16:            removeCloseButton(children[i]);
  17:        }
  18:    }
  19:}

Here are some Sun forums I’ve searched so far to find the possible helps for this solution:

Hope you make use of those tricks in your projects :-).

Tags: , ,