Stephen on Software aka SOS

July 30, 2009

The code does not lie !

Filed under: Programming — Tags: — sljm @ 10:04 pm

Today I was trying to figure some programming issue which has somethings to do with the architecture of the system (or more commonly know as “How do you do this (correctly)” or “How does this actually work” kind of question) today, you know the kind at work where you asked how this thing worked its suppose to work and then it’s gets all confusing because every time you asked someone, they sort of give you a slightly different answer.

On and off over the past few months (yes yes I did procrastinate a bit in this regard), I asked different people and it’s all getting confusing as to who is correct. Guess I should be asking the person who wrote the library from the start, but then I don’t know who did it just the general group that did it. I am going to go somewhat straight to the horses mouth tomorrow i guess to resolve this once and for all.

But meanwhile in order to start understanding, I decompiled the code (yes it is in one of those decompilable languages, so I am lucky!). Why would I want to do that? Because ……

THE CODE DOES NOT LIE !!!

Yeap the CODE DOES NOT LIE, guess no matter what people say, the code will be  the absolute truth. And in programming it is the best way of learning how to do something. But often we just cut and paste blindly, I suggest that we behave just like in school when we forgot to do a math assignment cause we play too much Final Fantasy or MGS, we always say we “copy with understanding” from our classmates when our teachers catch us for having very similar answers.

Some people are very sensitive about other people seeing their own code, I have no idea why? If people can give comments on how to improve wouldn’t that be great. Especially in a project, no code is own by an individual, all the code is up for others to edit and improve and add functionality. Just remember that if you break it you fix it. If someone found a bug in my code, I will be glad to accept it cause it probably saved me a few debugging hours later on. Some are afraid that others will see them take shortcuts, use bad naming conventions and so on. Then don’t do it in the first place, let’s all join the C.L.U.B.

I guess this is why Open Sourced Projects (at least the ones with lots of people in it) are doing great in terms of code quality. They have many people reviewing and writing code to make it better.

I think this is the longest rant i got so far in my blog i should stop here gotta get some sleep. Till the next time.

JAX-WS and Http Proxy

Filed under: Programming — Tags: , — sljm @ 11:19 am

With Java 1.5, Sun provides a new way of setting the http proxy using ProxySelectors.

In essence, you will need to provide your own custom ProxySelector to replace the default. So anything using a URLConnection will automatically use the proxy that is set using the ProxySelector. That means JAX-WS based stubs/proxies will also be able to use ProxySelector for passing traffic through the http proxy.

Sample code for writing your own ProxySelector can be found in the first 1st link. Authentication can be found in the second link.

Links

  1. Java Networking and Proxies
  2. JAX-WS and Http Proxy Authentication

July 22, 2009

Binding WPF Treeview and Objects

Filed under: Programming — Tags: — sljm @ 9:49 pm

I have been trying to find some resources for using custom objects as data for WPF TreeView for my own little project.

I most useful/closest to what I wanted to do was by Mike Hillberg but still its a bit complex for my needs. I just needed something simple. What I had was a Dir objects that has a List of Directories inside, which i wanted to display in the TreeView

public class Dir
{
public Dir(){}

public List<Dir> Directories{get;set;}
....
}

The code for the Dir class.

What you need now is a template or more specifically a HierarchicalDataTemplate that tells the TreeView how to read your object.

<Window x:Class="TaggerGui.Window1"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="clr-namespace:TaggerGui"
 Title="Window1" Height="600" Width="600">

.......

<TreeView Grid.Column="0" Name="FileTree" ItemsSource="{x:Static local:Window1.dirs}">
 <TreeView.ItemTemplate>
 <HierarchicalDataTemplate ItemsSource="{Binding Path=Directories}">
 <StackPanel Orientation="Horizontal">
 <TextBlock Text="{Binding Path=Name}"/>
 </StackPanel>
 </HierarchicalDataTemplate>
 </TreeView.ItemTemplate>
 </TreeView>

.....
</Window>

In this case I declare a static public ObservableCollection<File> dirs=new ObservableCollection<File>(); in the cs file and bind that using the x:static binding. Remember that you have to add the reference to the namespace that you are using at the top of the xaml file, (the  xmlns:local=”clr-namespace:TaggerGui”) part

Okies now for the next problem, let say i want to display different colors in the tree depending on how the presence of a certain file in the directory. How would you do that ? Well I am looking at the MVVM or the Model View View Model Pattern, that would be another blog post.

Links:

An Example of using Objects with Observable Collection in TreeView – Mike Hillberg

Gird Splitter

Hierarchical Databinding in WPF

July 15, 2009

Bad Code – Bad API, Bad Configuration

Filed under: bad code — Tags: — sljm @ 2:00 pm

Was trying to use an library/jar file in my project today. The jar was a wrap around to call a webservice (lets call the interface Service and the implementation ServiceImpl), the code has interface and all that and even had a factory class (ServiceFactory) built in to initialize the Service for you. There was no source code, I only had the class files, so i browsed the classes through NetBeans to see how to best use it.

I am a big fan of dependency injection, I thought i could just use ServiceImpl to initialize the service and inject into the classes that need it, but there was something wrong. Look at the code below.

public class ServiceImpl{
private ServiceImpl(){}

public String methodOne(String string){}
public String methodTwo(String string){}
}

Did you see the problem? The constructor is marked private! There is no way to initialize the class! Except through ServiceFactory. Take note that this is suppose to be a class that calls a webservice, so I am still looking for a place where to put in the webservice url.

When I went to another colleague to ask how do I use it, he showed me sample code below.

Service s=ServiceFactory.getInstance();
s.methodONe(someString);

I asked how do I set the URL for the webservice? He told me the one (and only way) was to use the configuration file in the jar itself.

These are the problems as i see it.

  1. ServiceImpl constructor is private. That means I can’ test it by constructing a new object., i can’t inject it into my code since the only way to construct it is through ServiceFactory. I can’t mock it too since everything is done in the factory. This is not good, hard to test and hard to mock.
  2. There is only one way to configure the Service and that is through ServiceFactory and the config file. That means I can’t programatically configure it through code, that makes testing hard, that makes using other forms of configuration hard (like you own custom config file, Spring applicationContext and so on).  Singletons are Evil use DI frameworks if you can.
  3. The API is unclear, from the api you can’t really tell what is needed to use it. Except that you know you need to get it through a Factory. But since all the confguration and setting up is done in the factory, you can’t tell by looking at the API what is needed to set it up.

The last point is the main one i guess, the API is not clear, we can’t tell how to use except though some internal knowledge. It didn’t help that there were no javadocs, some of the parameters were names arg0, arg1, string1 (you get the drift).

For people who are writing apis for other people to use, perhaps take a look at Spring and Hibernate’s configuration design.

In short, if you allow your objects to be instantiated programatically , you allow the possibility of using properties files, xml files or custom files, look at Spring and Hibernate. This is especially important when writing apis/libraries that are going to be used by different projects.

Make the apis clear, if it needs a URL put the need somewhere in the constructor of the class. So that people know what is needed to instantiate it, the api can be the documentation especially with modern IDE like Eclipse and NetBeans.

Some links

Singletons are Evil

Singletons are not Evil

July 9, 2009

Securing Tomcat Manager to localhost access

Filed under: Tips — Tags: — sljm @ 2:49 pm

Needed to secure my tomcat manager to localhost access only.

This is what you need to do.
In your /conf/catalina/localhost, create or edit a file call manager.xml.

Inside put the following.

<Context path="/manager" debug="0" privileged="true">

      <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127.0.0.1"/>

      <!-- Link to the user database we will get roles from
      <ResourceLink name="users" global="UserDatabase"
type="org.apache.catalina.UserDatabase"/>
        -->
</Context>

This should only allow localhost to connect to the manager application on tomcat.tom

Network connections folder empty ! After installing Win 2003 SP2!

Filed under: Tips — Tags: — sljm @ 10:39 am

After installing Win 2003 Service Pack 2 on a few machines, some started having this problem where the Network Connections folder is empty! My network connections still work (I still can connect and so on), just that I can’t see the network connections and hence can’t change the settings. WTF a service pack did this. Surprisingly only one of my servers was not affected.

Look around the net, didn’t really help, until I came to a post from Crapola. Apprantly his AD had problems after install Sp2. The solution was actually quite simple.

  1. Change Network Connection service to “Automatic” (Don’t know why it was set to manual)
  2. Look for the RPC service and changed the Log on user to “Local System Account”

And all should work. Dumb service pack…

Blog at WordPress.com.