:: EGOTHOR

Registration and Use of Filters

All the hard steps are done, and what is left is to announce the elementary filter to the world. This can be done in either of two ways.

Adding Filters by Hand. 

        Finder f = new Finder(); 1
        f.add(DefineReader.IN(), new DefineReader()); 2
1

The registrar is a Finder. You can allocate as many registrars as you like and need. Every registrar can then find other plans for different tasks (not only related to filtering).

2

Register our service.

Adding Filters Automatically. 

	Finder f = new Finder(); 1
        f.scanPackages();   2
1

As above.

2

This method registers all connectors found in the crushers-connectors file, which is merely a text file list of the connectors available in this particular module. The files are searched for in the root of all JAR packages which are found on your classpath. For instance, see the file src/kernel/resources/crusher-connectors. When Egothor is compiled, this file is placed in egothor-x.x.x.jar at the root level.

As you have probably already guessed, the second method has distinct advantages over the first. The first is, it's just easier. You won't have to write f.add( ) for all of your filters. Secondly, and more importantly, scanPackages( ) does its work at run-time, looking for filters that are available, relieving the programmer of having to know what filters are available.

The usage is then straighforward:

        String[] from = {"FILENAME"}; 1

        Path p = f.find("java.lang.String", from, "java.io.Reader", null); 2
        if (p == null) {
            System.out.println("Mission impossible :-)"); 3
        } else {
            System.out.println(p.toString()); 4

            Mill m = p.compile(); 5
            Hashtable h = new Hashtable();
            m.sow(h); 6
            m.initialize("autoexec.bat"); 7
            try {
                java.io.Reader d = (java.io.Reader) m.process(); 8
                /* read from d, if you like */
            } catch (Exception x) {
                x.printStackTrace();
            } finally {
                m.initialize(null); 9
            }
        }
1

Our entry flag.

2

The registrar is asked for the plan from "java.lang.String" with flag "FILENAME" to "java.io.Reader" and we do not put any restrictions on its flags - if we mentioned some flags, all must be then set up by the plan.

3

The plan does not exist (it is not our case, because our elementary filter does the thing, so registrar must find this trivial case).

4

Print out the string representation of the plan for the operator.

5

Compile the plan.

6

Send the initial attributes of the Mill.

7

Initialize the input ("java.lang.String").

8

Let the Mill turn the wheel and read the output we requested.

9

Reinitialize the Mill, so that it can be used again.

If you want to reuse the Path, you can. There are two ways - you will always compile a new Mill:

            m = p.compile();
            h = new Hashtable();
            m.sow(h);
            m.initialize("autoexec.bat");
            .... m.process();
            m.initialize(null);
            m = p.compile();
            h = new Hashtable();
            m.sow(h);
            m.initialize("autoexec.bat");
            .... m.process();
            m.initialize(null);
            m = p.compile();
            h = new Hashtable();
            m.sow(h);
            m.initialize("autoexec.bat");
            .... m.process();
            m.initialize(null);
etc.

or you will use the algorithm that was shown above:

            m = p.compile();
            h = new Hashtable();
            m.sow(h);
            m.initialize("autoexec.bat");
            .... m.process();
            m.initialize(null);
            h = new Hashtable();
            m.sow(h);
            m.initialize("autoexec.bat");
            .... m.process();
            m.initialize(null);
            h = new Hashtable();
            m.sow(h);
            m.initialize("autoexec.bat");
            .... m.process();
            m.initialize(null);
etc.

Which is better? Obviously, the second approach, because if the filter is complicated, e.g., it contains HTML parsers, the new instance must first be constructed and it takes a lot of time. On the other hand, you may say, the first approach need not call m.initialize(null), because it always uses a fresh Mill. Yes, it could, but the files which were opened by the elementary filters, may stay in an open state. It is never a good idea to leave a file open, so this approach must be taken with care.

[Tip]Tip

If you use N threads, you can construct one Path and then let the threads compile it if they need the respective Mill.

Prev Up Next
Writing Your Own Filter Home Predefined Filters
© 2003-2004 Egothor Developers