ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/FilterThread.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/filter/FilterThread.java (file contents):
Revision 1.13 by tdb, Wed Dec 6 22:57:45 2000 UTC vs.
Revision 1.34 by tdb, Fri Mar 22 16:42:54 2002 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.filter;
2 > package uk.org.iscream.cms.server.filter;
3  
4   //---IMPORTS---
5   import java.io.*;
6   import java.net.*;
7   import java.util.*;
8 < import uk.ac.ukc.iscream.core.*;
9 < import uk.ac.ukc.iscream.filter.*;
10 < import uk.ac.ukc.iscream.util.*;
8 > import uk.org.iscream.cms.server.core.*;
9 > import uk.org.iscream.cms.server.componentmanager.*;
10 > import uk.org.iscream.cms.server.filter.*;
11 > import uk.org.iscream.cms.server.util.*;
12  
13   /**
14 < * Handle an incoming UDP packet as a separate thread.
14 > * Handle an incoming packet as a separate thread.
15 > * Passes the data through various plugins, then
16 > * passes it on to the parent filter.
17   *
18 + * Now grabs data from a single queue, rather than
19 + * waiting to be contacted.
20 + *
21   * @author  $Author$
22   * @version $Id$
23   */
# Line 27 | Line 33 | public class FilterThread extends Thread{
33   //---STATIC METHODS---
34  
35   //---CONSTRUCTORS---
36 <
37 <    // Class constructor. Obtains the byte[] from a DatagramPacket.
38 <    public FilterThread(DatagramPacket packet, Filter parent){
39 <        _parent = parent;
40 <        _rawPacket = packet.getData();
41 <        _logger.write(this.toString(), Logger.DEBUG, "created");
36 >  
37 >    /**
38 >     * Constructs an instance of a FilterThread
39 >     *
40 >     * @param queue the Queue this filter is using
41 >     */
42 >    public FilterThread(Queue queue){
43 >        // set the Thread name
44 >        setName("filter.FilterThread");
45          
46 +        _queue = queue;
47 +        _logger.write(toString(), Logger.SYSINIT, "created");
48      }
38    
39    // Class constructor for passing XML Strings.
40    public FilterThread(String xml, Filter parent){
41        _parent = parent;
42        _rawPacket = xml.getBytes();
43        _logger.write(this.toString(), Logger.DEBUG, "created");
44    }
49  
50   //---PUBLIC METHODS---
51  
52 +    /**
53 +     * Runs the thread, getting data from the Queue and
54 +     * sending it on to the parent filter.
55 +     */
56      public void run(){
57 <
58 <        // Get a string without any null characters in it.
59 <        //  -- maybe String.trim() would be better here ?
60 <        String xml = new String(_rawPacket);
61 <        if (xml.indexOf(0) != -1) {
62 <            xml = xml.substring(0, xml.indexOf(0));
57 >        // setup the XMLPacketMaker
58 >        XMLPacketMaker xmlPacketMaker = new XMLPacketMaker();
59 >        // get a queue for ourselves
60 >        int n = _queue.getQueue();
61 >        // keep these out here, saves recreating the object
62 >        String xml = null;
63 >        String parentFilterName = "";
64 >        Filter parent = null;
65 >        while(true) {
66 >            // get a String of xml
67 >            try {
68 >                xml = (String) _queue.get(n);
69 >            }
70 >            catch (InvalidQueueException e) {
71 >                _logger.write(toString(), Logger.ERROR, "Queue error: "+e);
72 >            }
73 >            
74 >            // Get a string without any null characters in it.
75 >            //  -- maybe String.trim() would be better here ?
76 >            if (xml.indexOf(0) != -1) {
77 >                xml = xml.substring(0, xml.indexOf(0));
78 >            }
79 >            
80 >            // Bundle the XML all on one line (saves space and simplifies
81 >            // the protocol between clientinterface and client).
82 >            StringTokenizer tokenizer = new StringTokenizer(new String(xml), "\n");
83 >            xml = "";
84 >            while (tokenizer.hasMoreTokens()) {
85 >                xml += tokenizer.nextToken();
86 >            }
87 >            
88 >            // Use XMLPacketMaker to make an XMLPacket object.
89 >            XMLPacket packet = null;
90 >            try {
91 >                packet = xmlPacketMaker.createXMLPacket(xml);
92 >            } catch(InvalidXMLException e) {
93 >                _logger.write(toString(), Logger.ERROR, "Invalid XML: "+e);
94 >                // skip the rest of this loop iteration
95 >                continue;
96 >            }
97 >            
98 >            // get parent
99 >            try {
100 >                String newParent = ConfigurationProxy.getInstance().getProperty("Filter." + FilterMain.NAME, "Filter.parentFilter");
101 >                if(!parentFilterName.equals(newParent)) {
102 >                    parentFilterName = newParent;
103 >                    parent = FilterHelper.narrow(_refman.getCORBARef("iscream.Filter." + parentFilterName));
104 >                    _logger.write(toString(), Logger.DEBUG, "Parent filter changed to: "+parentFilterName);
105 >                }
106 >            } catch (PropertyNotFoundException e) {
107 >                continue;
108 >            }
109 >                    
110 >            // XMLPacket is ok, run filters...
111 >            if(PluginFilterManager.getInstance().runFilters(packet)) {
112 >                // and pass it on...
113 >                parent.receiveXML(xml);
114 >            }
115 >            else {
116 >                // ... or filtered it
117 >                _logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system.");
118 >            }
119          }
56        else {
57            xml = xml.substring(0, xml.length());
58        }
59        
60        // Use my XMLPacketMaker to make an XMLPacket object.
61        XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml);
62        XMLPacket packet = xmlPacketMaker.createXMLPacket();
63              
64        if(packet != null && PluginFilterManager.getInstance().runFilters(packet)) {
65            // packet is not null
66            // packet was not dropped by a plugin
67            // ... best pass it on !
68            _parent.receiveXML(xml);
69        }
70        else {
71            // either we had a null, or a plugin dropped it
72            _logger.write(this.toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system.");
73        }
74        
120      }
121  
122      /**
123       * Overrides the {@link java.lang.Object#toString() Object.toString()}
124       * method to provide clean logging (every class should have this).
125       *
126 +     * This uses the uk.org.iscream.cms.server.util.NameFormat class
127 +     * to format the toString()
128 +     *
129       * @return the name of this class and its CVS revision
130       */
131      public String toString() {
132 <        return this.getClass().getName() + "{" + _name + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
132 >        return FormatName.getName(
133 >            _name,
134 >            getClass().getName(),
135 >            REVISION);
136      }
137  
138   //---PRIVATE METHODS---
# Line 89 | Line 140 | public class FilterThread extends Thread{
140   //---ACCESSOR/MUTATOR METHODS---
141  
142   //---ATTRIBUTES---
143 +    
144 +    /**
145 +     * The Queue object
146 +     */
147 +    Queue _queue;
148 +    
149 +    /**
150 +     * This is the friendly identifier of the
151 +     * component this class is running in.
152 +     * eg, a Filter may be called "filter1",
153 +     * If this class does not have an owning
154 +     * component,  a name from the configuration
155 +     * can be placed here.  This name could also
156 +     * be changed to null for utility classes.
157 +     */
158 +    private String _name = FilterMain.NAME;
159  
160 <    Filter _parent;
161 <    byte[] _rawPacket;
162 <    Logger _logger = ReferenceManager.getInstance().getLogger();
163 <    String _name = ReferenceManager.getInstance().getName();
160 >    /**
161 >     * This holds a reference to the
162 >     * system logger that is being used.
163 >     */
164 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
165 >
166 >    /**
167 >     * A reference to the reference manager in use
168 >     */
169 >    private ReferenceManager _refman = ReferenceManager.getInstance();
170  
171   //---STATIC ATTRIBUTES---
172  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines