Archived

This forum has been archived. Please start a new discussion on GitHub.

Another solution about package mapping in Java

According to page 331 in Ice 3.1.0 manul, when we want to use a common package prefix, we have to do like this:
1.Add a global meta in slice definition:
[["java:package:com.acme"]]
module Workflow {
    class Document {
    // ...
    };
};


2.Add one confication in config files:
Ice.Package.Workflow=com.acme
or
Ice.Default.Package=com.acme

This solution has one big shortcoming: the application assembler should has some knowedge about the code, that is, he or she should know that "com.acme" or "com.xxx" package names; Furthermore, in practical applications, that having to configure these packages is very boring.

The following is another solution to this problem. The heart of it is the annotation feature in Java 5.
1.Add a global meta in slice definition(This is the same as above):
[["java:package:com.acme"]]
module Workflow {
    class Document {
        // ...
    };
};

2.change the behavior of slice2java a litte. Add an annotation to the java file:
package com.acme.Workflow;

@SomeAnnotation("com.acme")
public abstract class Document {
    //...
};    

3.When Ice begins to run, the class loader scans the class file. If it finds an @SomeAnnotation, it gets a map. That is:
::Workflow::Document  ==> com.acme.Workflow.Document

4.When Ice run time unmarshals an exception or concrete class , it can contact the map first.

If we do like this, the application assembler don't need to any configration about packages.

Comments

  • Is there any comment ?
  • mes
    mes California
    Hi,

    Unless I'm mistaken, this approach would require the Ice run time to scan every class in the program's classpath to find those that have the package annotation, which is not something I would be eager to do.

    Take care,
    - Mark
  • mes wrote:
    Hi,

    Unless I'm mistaken, this approach would require the Ice run time to scan every class in the program's classpath to find those that have the package annotation, which is not something I would be eager to do.

    - Mark

    Yes, you are right, this approach require the Ice to scan all classes in the program's classpath to find those that have the package annotation. However, annotation and this corresponding finding are just common things in modern java5 software, such as JBoss/Hibernate/Spring/WebLogic/Websphere. This finding can be done at the very beginning of program, so it has almost no impact on the program's performance. Further, this finding can be done in just a helper function , so it would not pollute the Ice's core code.

    In my point of view, these two config items(Ice.Package.Workflow/Ice.Default.Package) are very ugly and they pollute the Ice's simplicity and consistency because they require the assembler/tester to know some code detail of Ice program.
  • Another solution

    1.Add a global meta in slice definition(This is the same as above) :
    [["java:package:com.acme"]]                                                                                             
    module Workflow {                                                                                                       
        class Document {                                                                                                    
            // ...                                                                                                          
        };                                                                                                                  
    };                                                                                                                      
    

    2.change the behavior of slice2java a litte, which generates the following two files:
    1)The first one is just as usual:
    package com.acme.Workflow;                                                                                              
                                                                                                                            
    public abstract class Document {                                                                                        
        //...                                                                                                               
    };                                                                                                                      
    
    2)The second one's package always begins with "com.zeroc":
    package com.zeroc.Workflow;
    public class Document {
        public static final string CLASSNAME = "com.acme.Workflow.Document";
    }
    
    3.When Ice run time gets a type id such as "::Workflow:: Document", it follow these two steps to resolve it:
    1) Try to load Workflow.Document class.
    2) If 1) fails, try to load com.zeroc.Workflow.Document class. If this succeeds, it further tries to load the class with the name of com.zeroc.Workflow.Document.CLASSNAME, that is, com.acme.Workflow.Document.
    3) Otherwise, this type id can not be resolved.