KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > appclient > jws > DynamicContent


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.appclient.jws;
25
26 import java.util.Date JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 /**
31  *
32  * @author tjquinn
33  */

34 /**
35  *Represents dynamic content in template form.
36  *<p>
37  *This class also keeps track of the most recent times the object's template
38  *was used to generated different content from the previous time. This
39  *information is used in responding to HTTP HEAD requests. Java Web Start uses
40  *HEAD requests to find out if a document on the server is more recent than
41  *the locally cached copy on the client. If so, then Java Web Start will
42  *request the updated version with a routine GET request.
43  *<p>
44  *To avoid incorrectly reporting obsolete cached documents as correct, this
45  *class keeps track of when the content generated by the template is different
46  *from the previous generation.
47  *<p>
48  *The generated content can depend on request-time information
49  *(such as command line arguments passed in the query string of the HTTP request).
50  *We save and track only a few individual response instances, because the assumption
51  *is that requests that carry query strings (which are converted into command line
52  *arguments passed to ACC and on through to the app client) are likely to change
53  *frequently and not necessarily be reused often. Keeping a few allows caching
54  *the different content resulting from a small number of different argument
55  *value settings, but avoids the problems of caching every single response
56  *which could become a large memory drain if each request specified a
57  *different set of argument (for instance, one of the arguments could be a
58  *timestamp that would change every time).
59  */

60 public class DynamicContent extends Content {
61
62     /** maximum number of instances of content to keep for each template */
63     private static final int MAX_INSTANCES = 4;
64     
65     /** JNLP element to request full permissions - used if the app client jar is signed */
66     protected static final String JavaDoc ALL_PERMISSIONS_JNLP_SETTING = "<security><all-permissions/></security>";
67     
68     /** JNLP element to request no permissions - used if the app client jar is not signed - no perms is the JNLP default */
69     protected static final String JavaDoc NO_PERMISSIONS_JNLP_SETTING = "";
70
71     /**
72      *the template which will be used at runtime to create the actual response
73      *to the HTTP request
74      */

75     private String JavaDoc template;
76
77     /** the MIME type of the data represented by this DynamicContent instance */
78     protected String JavaDoc mimeType;
79
80     /** content instances resulting from previous HTTP GET requests */
81     private LinkedList JavaDoc<Instance> instances = new LinkedList JavaDoc<Instance>();
82     
83     /** records whether this content should request elevated permissions */
84     private boolean requiresElevatedPermissions;
85     
86     /**
87      *Returns a new instance of DynamicContent.
88      *@param origin the ContentOrigin for the new content instance
89      *@param contentKey the content key used to store and retrieve the content
90      *@param path the path relative to the subcategory in which this document is addressable
91      *@param mimeType the MIME type of data represented by the content generated by this
92      *object.
93      *@return new DynamicContent object
94      */

95     public DynamicContent(ContentOrigin origin, String JavaDoc contentKey, String JavaDoc path, String JavaDoc template, String JavaDoc mimeType) {
96         this(origin, contentKey, path, template, mimeType, false /* requiresElevatedPermissions */);
97     }
98
99     /**
100      *Returns a new instance of DynamicContent.
101      *@param origin the ContentOrigin for the new content instance
102      *@param contentKey the content key used to store and retrieve the content
103      *@param path the path relative to the subcategory in which this document is addressable
104      *@param mimeType the MIME type of data represented by the content generated by this
105      *object.
106      *@return new DynamicContent object
107      */

108     public DynamicContent(ContentOrigin origin, String JavaDoc contentKey, String JavaDoc path, String JavaDoc template, String JavaDoc mimeType, boolean requiresElevatedPermissions) {
109         super(origin, contentKey, path);
110         this.template = template;
111         this.mimeType = mimeType;
112         this.requiresElevatedPermissions = requiresElevatedPermissions;
113      }
114
115     /**
116      *Returns the DynamicContent.Instance for this template corresponding to
117      *the specified substitution token values.
118      *@param tokenValues the name/value pairs to be substituted in the template
119      *@param createIfAbsent selects whether a new DynamicContent.Instance should
120      *be created for the resulting text if the content text resulting from the
121      *substitution is not already cached by this DynamicContent.
122      *@return the instance corresponding to the content generated by the tokenValues;
123      *null if no such instance already exists for this DynamicContent and createIfAbsent
124      *was false.
125      */

126     public Instance findInstance(Properties JavaDoc tokenValues, boolean createIfAbsent) {
127         Instance result = null;
128         String JavaDoc textWithPlaceholdersReplaced = Util.replaceTokens(template, tokenValues);
129         
130         /*
131          *Look for an instance with its text matching the just-computed replacement text.
132          */

133         synchronized (instances) {
134             for (Instance i : instances) {
135                 if (i.textEquals(textWithPlaceholdersReplaced)) {
136                     result = i;
137                     break;
138                 }
139             }
140             if (result == null && createIfAbsent) {
141                 result = new Instance(textWithPlaceholdersReplaced);
142                 addInstance(result);
143             }
144         }
145         return result;
146     }
147     
148     /**
149      *Adds a new content instance to this dynamic content. If adding the instance
150      *makes the cache too long, discards the oldest instance.
151      *@param newInstance the new instance to be added to the cache
152      */

153     private void addInstance(Instance newInstance) {
154         synchronized (instances) {
155             instances.addFirst(newInstance);
156             if (instances.size() > MAX_INSTANCES) {
157                 instances.removeLast();
158             }
159         }
160     }
161     
162
163     /**
164      *Returns the MIME type associated with this content.
165      *@return the MIME type for this content
166      */

167     public String JavaDoc getMimeType() {
168         return mimeType;
169     }
170
171     /**
172      *Returns the appropriate JNLP security setting for this content, based on
173      *whether it requires elevated permissions or not.
174      *@return the JNLP element string or an empty string if no privs are needed
175      */

176     public String JavaDoc getJNLPSecuritySetting() {
177         return requiresElevatedPermissions ? ALL_PERMISSIONS_JNLP_SETTING : NO_PERMISSIONS_JNLP_SETTING;
178     }
179     
180     /**
181      *Returns whether this dynamic content requires elevated permissions.
182      *@return true if the related static content does require elevated permissions
183      */

184     public boolean requiresElevatedPermissions() {
185         return requiresElevatedPermissions;
186     }
187     
188     /**
189      *Clears the cached instances.
190      */

191     protected void clearInstances() {
192         instances.clear();
193     }
194     
195     /**
196      *Returns a string representation of the DynamicContent.
197      */

198     public String JavaDoc toString() {
199         return super.toString() + ", template=" + template + ", MIME type=" + mimeType;// + ", most recent change in generated content=" + timestamp;
200
}
201
202     /**
203      *Represents the result of substituting a single set of token values into
204      *the DynamicContent's template.
205      */

206     public class Instance {
207         
208         /** when this instance was created */
209         private Date JavaDoc timestamp;
210         
211         /** the content of this instance */
212         private String JavaDoc text;
213         
214         /**
215          *Creates a new instance of Instance (!) holding the result of a
216          *specific substitution of values for placeholders.
217          *@param text the content for this new Instance
218          */

219         private Instance(String JavaDoc text) {
220             this.text = text;
221             timestamp = new Date JavaDoc();
222         }
223         
224         /**
225          *Compares the specified String to the text content of the Instance.
226          *@param other the text to compare to this Instance's text
227          *@return whether the text matches or not
228          */

229         private boolean textEquals(String JavaDoc other) {
230             return text.equals(other);
231         }
232         
233         /**
234          *Returns the time stamp associated with this content Instance.
235          *@return the Date representing when this Instance was created
236          */

237         public Date JavaDoc getTimestamp() {
238             return timestamp;
239         }
240         
241         /**
242          *Returns the content associated with this Instance.
243          *@return the text content stored in the Instance
244          */

245         public String JavaDoc getText() {
246             return text;
247         }
248     }
249 }
Popular Tags