KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > extend > ScriptConduit


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.extend;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.ServletOutputStream JavaDoc;
21
22 import org.directwebremoting.ScriptBuffer;
23 import org.directwebremoting.util.LocalUtil;
24
25 /**
26  * While a Marshaller is processing a request it can register a ScriptConduit
27  * with the ScriptSession to say - pass scripts straight to me and bypass the
28  * temporary storage area.
29  * This interface allows this to happen.
30  * @author Joe Walker [joe at getahead dot ltd dot uk]
31  */

32 public abstract class ScriptConduit implements Comparable JavaDoc
33 {
34     /**
35      * All ScriptConduit need a rank
36      * @param rank How does this ScriptConduit sort
37      */

38     public ScriptConduit(int rank)
39     {
40         this.rank = rank;
41     }
42
43     /**
44      * ScriptConduits have a rank that indicates the priority order in which we
45      * should send scripts to them.
46      * The rank is a number from 1 to 10, and should only be one of the defined
47      * values: ScriptConduit.RANK_*.
48      * @see ScriptConduit#RANK_PROCEDURAL
49      * @see ScriptConduit#RANK_FAST
50      * @see ScriptConduit#RANK_SLOW
51      * @return The rank of this ScriptConduit
52      */

53     public int getRank()
54     {
55         return rank;
56     }
57
58     /**
59      * Indicates that this ScriptConduit is used for control-flow and will
60      * probably not actually convey the script, but does need to tell someone
61      * else about it
62      */

63     public static final int RANK_PROCEDURAL = 10;
64
65     /**
66      * Indicates that this ScriptConduit is a very good way of getting scripts
67      * to the client and should be used as a preferred method
68      */

69     public static final int RANK_FAST = 5;
70
71     /**
72      * Indicates that this ScriptConduit is a poor way of getting scripts to the
73      * client and should only be used as a last resort.
74      */

75     public static final int RANK_SLOW = 1;
76
77     /**
78      * Add a script to the list bound for remote execution.
79      * <p>It is not an error to refuse to handle the script and return false, it
80      * just indicates that this ScriptConduit did not accept the script.
81      * If the ScriptConduit can no longer function then it should throw an
82      * exception and it will be asumed to be no longer useful.
83      * If you want to implement this method then you will probably be doing
84      * something like calling {@link ServletOutputStream#print(String)} and
85      * passing in the results of calling ScriptBufferUtil.createOutput().
86      * @param script The script to execute
87      * @return true if this ScriptConduit handled the script.
88      * @throws IOException If this conduit is broken and should not be used
89      * @throws MarshallException If objects in the script can not be marshalled
90      */

91     public abstract boolean addScript(ScriptBuffer script) throws IOException JavaDoc, MarshallException;
92
93     /* (non-Javadoc)
94      * @see java.lang.Comparable#compareTo(java.lang.Object)
95      */

96     public int compareTo(Object JavaDoc obj)
97     {
98         ScriptConduit that = (ScriptConduit) obj;
99
100         int rankdiff = this.getRank() - that.getRank();
101         if (rankdiff != 0)
102         {
103             return rankdiff;
104         }
105
106         return (int) (this.id - that.id);
107     }
108
109     /* (non-Javadoc)
110      * @see java.lang.Object#toString()
111      */

112     public String JavaDoc toString()
113     {
114         if (classname == null)
115         {
116             classname = LocalUtil.getShortClassName(getClass());
117         }
118
119         return classname + "[id=" + id + "]";
120     }
121
122     /**
123      * Cached short classname for toString()
124      */

125     private static String JavaDoc classname = null;
126
127     /**
128      * The rank of this ScriptConduit
129      */

130     private int rank;
131
132     /**
133      * Our ID, to get around serialization issues
134      */

135     private final long id = getNextId();
136
137     /**
138      * Get the next unique ID in a thread safe way
139      * @return a unique id
140      */

141     private static synchronized long getNextId()
142     {
143         return nextId++;
144     }
145
146     /**
147      * The next ID, to get around serialization issues
148      */

149     private static long nextId = 0L;
150 }
151
Popular Tags